Skip to content

Commit

Permalink
FABN-917: Add unresponded event hubs to timeout message
Browse files Browse the repository at this point in the history
Changes for submitTransaction() API.

Change-Id: I8aa19dfb0c3b8fc5a55faa07bbb5e66771bf4dab
Signed-off-by: Mark S. Lewis <mark_lewis@uk.ibm.com>
  • Loading branch information
bestbeforetoday committed Sep 14, 2018
1 parent 08b920c commit dac25b4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
11 changes: 11 additions & 0 deletions fabric-network/lib/impl/event/defaulteventhandlermanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ const EventHubFactory = require('./eventhubfactory');
const logger = require('../../logger').getLogger('DefaultEventHandlerManager');

class DefaultEventHandlerManager {
/**
* @typedef {Object} EventHandlerOptions
* @property {Number} [timeout = 0] Number of seconds to wait for transaction completion. A value of zero indicates
* that the handler should wait indefinitely.
*/

/**
* Constructor.
* @param {Network} network Network on which events will be processed.
* @param {String} mspId Member Services Provider identifier.
* @param {EventHandlerOptions} options Additional options for event handling behaviour.
*/
constructor(network, mspId, options) {
this.network = network;
this.channel = network.channel;
Expand Down
24 changes: 17 additions & 7 deletions fabric-network/lib/impl/event/transactioneventhandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ const util = require('util');
* @class
*/
class TransactionEventHandler {
/**
* @typedef {Object} TransactionEventHandlerOptions
* @property {Number} [timeout = 0] Number of seconds to wait for transaction completion. A value of zero indicates
* that the handler should wait indefinitely.
*/

/**
* Constructor.
* @private
Expand All @@ -42,6 +36,7 @@ class TransactionEventHandler {
logger.debug('constructor:', util.format('transactionId = %s, options = %O', this.transactionId, this.options));

this.eventHubs = manager.getEventHubs();
this.respondedEventHubs = new Set();

this.notificationPromise = new Promise((resolve, reject) => {
this._txResolve = resolve;
Expand Down Expand Up @@ -73,14 +68,24 @@ class TransactionEventHandler {
logger.debug('_setListenTimeout:', `setTimeout(${this.options.commitTimeout}) for transaction ${this.transactionId}`);

this.timeoutHandler = setTimeout(() => {
this._strategyFail(new Error('Event strategy not satisfied within the timeout period'));
this._timeoutFail();
}, this.options.commitTimeout * 1000);
}

_timeoutFail() {
const unrespondedEventHubs = this.eventHubs
.filter((eventHub) => !this.respondedEventHubs.has(eventHub))
.map((eventHub) => eventHub.getName())
.join(', ');
const message = 'Event strategy not satisfied within the timeout period. No response received from event hubs: ' + unrespondedEventHubs;
this._strategyFail(new Error(message));
}

_onEvent(eventHub, txId, code) {
logger.debug('_onEvent:', util.format('received event for %j with code %j', txId, code));

eventHub.unregisterTxEvent(this.transactionId);
this._receivedEventHubResponse(eventHub);
if (code !== 'VALID') {
const message = util.format('Peer %s has rejected transaction %j with code %j', eventHub.getPeerAddr(), txId, code);
this._strategyFail(new Error(message));
Expand All @@ -93,9 +98,14 @@ class TransactionEventHandler {
logger.info('_onError:', util.format('received error from peer %s: %s', eventHub.getPeerAddr(), err));

eventHub.unregisterTxEvent(this.transactionId);
this._receivedEventHubResponse(eventHub);
this.strategy.errorReceived(this._strategySuccess.bind(this), this._strategyFail.bind(this));
}

_receivedEventHubResponse(eventHub) {
this.respondedEventHubs.add(eventHub);
}

/**
* Callback for the strategy to indicate successful commit of the transaction.
* @private
Expand Down
14 changes: 13 additions & 1 deletion fabric-network/test/impl/event/transactioneventhandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const ChannelEventHub = require('fabric-client').ChannelEventHub;

const TransactionEventHandler = require('../../../lib/impl/event/transactioneventhandler');
const DefaultEventHandlerManager = require('../../../lib/impl/event/defaulteventhandlermanager');
const EventHandlerStrategies = require('../../../lib/impl/event/defaulteventhandlerstrategies');

describe('TransactionEventHandler', () => {
const transactionId = 'TRANSACTION_ID';
Expand Down Expand Up @@ -194,5 +193,18 @@ describe('TransactionEventHandler', () => {
stubEventHub._onEventFn(transactionId, 'VALID');
return expect(handler.waitForEvents()).to.be.fulfilled;
});

it('timeout failure message includes event hubs that have not responded', async () => {
stubEventHandlerManager.options = {
strategy: stubStrategy,
commitTimeout: 418
};
handler = new TransactionEventHandler(stubEventHandlerManager, transactionId);
await handler.startListening();
const promise = handler.waitForEvents();
clock.runAll();
const eventHubName = stubEventHub.getName();
return expect(promise).to.be.rejectedWith(eventHubName);
});
});
});

0 comments on commit dac25b4

Please sign in to comment.