Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit a63c19c

Browse files
FABN-955: Additional logging for tx event handling
Change-Id: Id476eb7152f24f4e894c258902dbc901c70dfaec Signed-off-by: Mark S. Lewis <mark_lewis@uk.ibm.com>
1 parent aaa6c16 commit a63c19c

12 files changed

+98
-98
lines changed

fabric-network/lib/impl/event/abstracteventstrategy.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
'use strict';
8+
79
const logger = require('fabric-network/lib/logger').getLogger('AbstractStrategy');
810

911
/**
@@ -19,23 +21,16 @@ const logger = require('fabric-network/lib/logger').getLogger('AbstractStrategy'
1921
class AbstractEventStrategy {
2022
/**
2123
* Constructor.
22-
* @param {EventHubFactory} eventHubFactory Factory for obtaining event hubs for peers.
23-
* @param {ChannelPeer[]} peers Peers from which to process events.
24+
* @param {Promise.ChannelEventHub[]} eventHubsPromise Promise to event hubs for which to process events.
2425
*/
25-
constructor(eventHubFactory, peers) {
26-
if (!eventHubFactory) {
27-
const message = 'Event hub factory not set';
28-
logger.error('constructor:', message);
29-
throw new Error(message);
30-
}
31-
if (!peers || peers.length === 0) {
32-
const message = 'Peers not set';
26+
constructor(eventHubsPromise) {
27+
if (!(eventHubsPromise instanceof Promise)) {
28+
const message = 'Expected event hubs to be a Promise but was ' + typeof eventHubsPromise;
3329
logger.error('constructor:', message);
3430
throw new Error(message);
3531
}
3632

37-
this.eventHubFactory = eventHubFactory;
38-
this.peers = peers;
33+
this.eventHubsPromise = eventHubsPromise;
3934
this.counts = {
4035
success: 0,
4136
fail: 0,
@@ -47,10 +42,11 @@ class AbstractEventStrategy {
4742
* Called by event handler to obtain the event hubs to which it should listen. Gives an opportunity for
4843
* the strategy to store information on the events it expects to receive for later use in event handling.
4944
* @async
45+
* @returns ChannelEventHubs[] connected event hubs.
5046
* @throws {Error} if the connected event hubs do not satisfy the strategy.
5147
*/
5248
async getConnectedEventHubs() {
53-
const eventHubs = await this.eventHubFactory.getEventHubs(this.peers);
49+
const eventHubs = await this.eventHubsPromise;
5450
const connectedEventHubs = eventHubs.filter((eventHub) => eventHub.isconnected());
5551

5652
if (connectedEventHubs.length === 0) {

fabric-network/lib/impl/event/allfortxstrategy.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
'use strict';
8+
79
const AbstractEventStrategy = require('fabric-network/lib/impl/event/abstracteventstrategy');
810

9-
const logger = require('../../logger').getLogger('AllForTxStrategy');
11+
const logger = require('fabric-network/lib/logger').getLogger('AllForTxStrategy');
1012

1113
/**
1214
* Event handling strategy that:
@@ -19,17 +21,11 @@ const logger = require('../../logger').getLogger('AllForTxStrategy');
1921
* @class
2022
*/
2123
class AllForTxStrategy extends AbstractEventStrategy {
22-
/**
23-
* @inheritdoc
24-
*/
25-
constructor(eventHubFactory, peers) {
26-
super(eventHubFactory, peers);
27-
}
28-
2924
/**
3025
* @inheritdoc
3126
*/
3227
checkCompletion(counts, successFn, failFn) {
28+
logger.debug('checkCompletion:', counts);
3329
const isAllResponsesReceived = (counts.success + counts.fail === counts.expected);
3430
if (isAllResponsesReceived) {
3531
if (counts.success > 0) {

fabric-network/lib/impl/event/anyfortxstrategy.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
'use strict';
8+
79
const AbstractEventStrategy = require('fabric-network/lib/impl/event/abstracteventstrategy');
810

9-
const logger = require('../../logger').getLogger('AnyForTxStrategy');
11+
const logger = require('fabric-network/lib/logger').getLogger('AnyForTxStrategy');
1012

1113
/**
1214
* Event handling strategy that:
@@ -19,17 +21,11 @@ const logger = require('../../logger').getLogger('AnyForTxStrategy');
1921
* @class
2022
*/
2123
class AnyForTxStrategy extends AbstractEventStrategy {
22-
/**
23-
* @inheritdoc
24-
*/
25-
constructor(eventHubFactory, peers) {
26-
super(eventHubFactory, peers);
27-
}
28-
2924
/**
3025
* @inheritdoc
3126
*/
3227
checkCompletion(counts, successFn, failFn) {
28+
logger.debug('checkCompletion:', counts);
3329
const isAllResponsesReceived = (counts.success + counts.fail === counts.expected);
3430
if (counts.success > 0) {
3531
successFn();

fabric-network/lib/impl/event/defaulteventhandlermanager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
const EventHandlerStrategies = require('./defaulteventhandlerstrategies');
1010
const TransactionEventHandler = require('./transactioneventhandler');
1111
const EventHubFactory = require('./eventhubfactory');
12-
const logger = require('../../logger').getLogger('DefaultEventHandlerManager');
12+
const logger = require('fabric-network/lib/logger').getLogger('DefaultEventHandlerManager');
1313

1414
class DefaultEventHandlerManager {
1515
/**

fabric-network/lib/impl/event/defaulteventhandlerstrategies.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const AnyForTxStrategy = require('fabric-network/lib/impl/event/anyfortxstrategy
1818
*/
1919
function MSPID_SCOPE_ALLFORTX(eventHubFactory, network, mspId) {
2020
const peers = network.getPeerMap().get(mspId);
21-
return new AllForTxStrategy(eventHubFactory, peers);
21+
return new AllForTxStrategy(eventHubFactory.getEventHubs(peers));
2222
}
2323

2424
/**
@@ -29,7 +29,7 @@ function MSPID_SCOPE_ALLFORTX(eventHubFactory, network, mspId) {
2929
*/
3030
function MSPID_SCOPE_ANYFORTX(eventHubFactory, network, mspId) {
3131
const peers = network.getPeerMap().get(mspId);
32-
return new AnyForTxStrategy(eventHubFactory, peers);
32+
return new AnyForTxStrategy(eventHubFactory.getEventHubs(peers));
3333
}
3434

3535
/**
@@ -42,7 +42,7 @@ function MSPID_SCOPE_ANYFORTX(eventHubFactory, network, mspId) {
4242
//eslint-disable-next-line no-unused-vars
4343
function NETWORK_SCOPE_ALLFORTX(eventHubFactory, network, mspId) {
4444
const peers = network.getChannel().getPeers();
45-
return new AllForTxStrategy(eventHubFactory, peers);
45+
return new AllForTxStrategy(eventHubFactory.getEventHubs(peers));
4646
}
4747

4848
/**
@@ -54,7 +54,7 @@ function NETWORK_SCOPE_ALLFORTX(eventHubFactory, network, mspId) {
5454
//eslint-disable-next-line no-unused-vars
5555
function NETWORK_SCOPE_ANYFORTX(eventHubFactory, network, mspId) {
5656
const peers = network.getChannel().getPeers();
57-
return new AnyForTxStrategy(eventHubFactory, peers);
57+
return new AnyForTxStrategy(eventHubFactory.getEventHubs(peers));
5858
}
5959

6060
module.exports = {

fabric-network/lib/impl/event/eventhubfactory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
'use strict';
88

9-
const logger = require('../../logger').getLogger('EventHubFactory');
9+
const logger = require('fabric-network/lib/logger').getLogger('EventHubFactory');
1010

1111
/**
1212
* Factory for obtaining event hubs for peers on a given channel.

fabric-network/lib/impl/event/transactioneventhandler.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
'use strict';
88

9-
const logger = require('../../logger').getLogger('TransactionEventHandler');
9+
const logger = require('fabric-network/lib/logger').getLogger('TransactionEventHandler');
1010
const util = require('util');
1111

1212
/**
@@ -49,14 +49,12 @@ class TransactionEventHandler {
4949
* @async
5050
*/
5151
async startListening() {
52-
this._setListenTimeout();
53-
54-
for (const eventHub of this.eventHubs) {
55-
logger.debug('startListening:', `registerTxEvent(${this.transactionId}) for event hub:`, eventHub.getName());
56-
57-
eventHub.registerTxEvent(this.transactionId,
58-
(txId, code) => this._onEvent(eventHub, txId, code),
59-
(err) => this._onError(eventHub, err));
52+
if (this.eventHubs.length > 0) {
53+
this._setListenTimeout();
54+
this._registerTxEventListeners();
55+
} else {
56+
logger.debug('startListening: No event hubs');
57+
this._txResolve();
6058
}
6159
}
6260

@@ -72,6 +70,16 @@ class TransactionEventHandler {
7270
}, this.options.commitTimeout * 1000);
7371
}
7472

73+
_registerTxEventListeners() {
74+
for (const eventHub of this.eventHubs) {
75+
logger.debug('_registerTxEventListeners:', `registerTxEvent(${this.transactionId}) for event hub:`, eventHub.getName());
76+
77+
eventHub.registerTxEvent(this.transactionId,
78+
(txId, code) => this._onEvent(eventHub, txId, code),
79+
(err) => this._onError(eventHub, err));
80+
}
81+
}
82+
7583
_timeoutFail() {
7684
const unrespondedEventHubs = this.eventHubs
7785
.filter((eventHub) => !this.respondedEventHubs.has(eventHub))
@@ -135,13 +143,16 @@ class TransactionEventHandler {
135143
* @throws {Error} if the transaction commit is not successful within the timeout period.
136144
*/
137145
async waitForEvents() {
146+
logger.debug('waitForEvents called');
138147
await this.notificationPromise;
139148
}
140149

141150
/**
142151
* Cancel listening for events.
143152
*/
144153
cancelListening() {
154+
logger.debug('cancelListening called');
155+
145156
clearTimeout(this.timeoutHandler);
146157
for (const eventHub of this.eventHubs) {
147158
eventHub.unregisterTxEvent(this.transactionId);

fabric-network/test/impl/event/defaulteventhandlermanager.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@ describe('DefaultEventHandlerManager', () => {
5656

5757
describe('#constructor', () => {
5858
it('has a default strategy if no options supplied', () => {
59-
const handler = new DefaultEventHandlerManager(stubNetwork, 'MSP_ID', {commitTimeout: 300});
59+
const handler = new DefaultEventHandlerManager(stubNetwork, 'MSP_ID', {});
6060
expect(handler.options.strategy).to.equal(EventHandlerStrategies.MSPID_SCOPE_ALLFORTX);
6161
});
6262

63-
it('allows a timeout option to be specified', () => {
64-
const handler = new DefaultEventHandlerManager(stubNetwork, 'MSP_ID', {commitTimeout: 300, strategy: EventHandlerStrategies.MSPID_SCOPE_ANYFORTX});
63+
it('allows a strategy to be specified', () => {
64+
const options = {
65+
strategy: EventHandlerStrategies.MSPID_SCOPE_ANYFORTX
66+
};
67+
const handler = new DefaultEventHandlerManager(stubNetwork, 'MSP_ID', options);
6568
expect(handler.options.strategy).to.equal(EventHandlerStrategies.MSPID_SCOPE_ANYFORTX);
6669
});
6770
});
@@ -93,8 +96,8 @@ describe('DefaultEventHandlerManager', () => {
9396
stubNetwork.getPeerMap.returns(mockPeerMap);
9497
handler.channel = mockChannel;
9598
await handler.initialize();
96-
handler.initialized.should.equal(true);
97-
handler.useFullBlocks.should.equal(true);
99+
expect(handler.initialized, 'initialized').to.be.true;
100+
expect(handler.useFullBlocks, 'useFullBlocks').to.be.true;
98101
});
99102
});
100103

@@ -104,8 +107,8 @@ describe('DefaultEventHandlerManager', () => {
104107
handler.initialized = true;
105108
handler.availableEventHubs = [ { disconnect: () => {} }];
106109
handler.dispose();
107-
handler.availableEventHubs.length.should.equal(0);
108-
handler.initialized.should.equal(false);
110+
expect(handler.availableEventHubs).to.have.lengthOf(0);
111+
expect(handler.initialized).to.be.false;
109112
});
110113
});
111114

fabric-network/test/impl/event/defaulteventhandlerstrategies.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('DefaultEventHandlerStrategies', () => {
3232
stubEventHub.isconnected.returns(true);
3333

3434
stubEventHubFactory = sinon.createStubInstance(EventHubFactory);
35-
stubEventHubFactory.getEventHubs.returns([stubEventHub]);
35+
stubEventHubFactory.getEventHubs.resolves([stubEventHub]);
3636

3737
stubPeer = {
3838
_stubInfo: 'peer',

fabric-network/test/impl/event/eventhubfactory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const sinon = require('sinon');
1313
const Channel = require('fabric-client').Channel;
1414
const ChannelEventHub = require('fabric-client').ChannelEventHub;
1515

16-
const EventHubFactory = require('../../../lib/impl/event/eventhubfactory');
16+
const EventHubFactory = require('fabric-network/lib/impl/event/eventhubfactory');
1717

1818
describe('EventHubFactory', () => {
1919
let stubChannel;

0 commit comments

Comments
 (0)