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

Commit ba11979

Browse files
authored
FABN-1557 NodeSDK all restart event service (#237)
Event Service would not reconnect after a disconnect. Checking to avoid connecting during a connect was not reset during the disconnect. Added more logging and testing. Signed-off-by: Bret Harrison <beharrison@nc.rr.com>
1 parent 44bbab0 commit ba11979

File tree

9 files changed

+62
-13
lines changed

9 files changed

+62
-13
lines changed

fabric-common/lib/EventService.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ class EventService extends ServiceAction {
169169
this._current_eventer.disconnect();
170170
logger.debug('%s - closing stream %s', method, this.currentStreamNumber);
171171
this._current_eventer = null;
172+
} else {
173+
logger.debug('%s - no current eventer - not shutting down stream', method);
172174
}
173175
this._close_running = false;
174176

@@ -356,16 +358,20 @@ class EventService extends ServiceAction {
356358
for (const target of this.targets) {
357359
try {
358360
if (target.stream) {
361+
logger.debug('%s - target has a stream, is already listening %s', method, target.toString());
359362
start_error = Error(`Event service ${target.name} is currently listening`);
360363
} else {
361364
if (target.isConnectable()) {
365+
logger.debug('%s - target needs to connect %s', method, target.toString());
362366
await target.connect(); // target endpoint has been previously assigned, but not connected yet
363367
}
364368
const isConnected = await target.checkConnection();
365369
if (!isConnected) {
366370
start_error = Error(`Event service ${target.name} is not connected`);
371+
logger.debug('%s - target is not connected %s', method, target.toString());
367372
} else {
368373
this._current_eventer = await this._startService(target, envelope, requestTimeout);
374+
logger.debug('%s - set current eventer %s', method, this._current_eventer.toString());
369375
}
370376
}
371377
} catch (error) {
@@ -384,8 +390,11 @@ class EventService extends ServiceAction {
384390
// if we ran through the all targets and have start_error then we
385391
// have not found a working target endpoint, so tell user error
386392
if (start_error) {
393+
logger.error('%s - no targets started - %s', method, start_error);
387394
throw start_error;
388395
}
396+
397+
logger.debug('%s - end', method);
389398
}
390399

391400
/*

fabric-common/lib/Eventer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class Eventer extends ServiceEndpoint {
117117
}
118118
}
119119

120-
logger.debug('%s - end', method);
120+
logger.debug('%s - end return:%s', method, result);
121121
return result;
122122
}
123123

fabric-common/lib/ServiceEndpoint.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class ServiceEndpoint {
120120
logger.debug(`${method} - create the grpc service for ${this.name}`);
121121
this.service = new this.serviceClass(this.endpoint.addr, this.endpoint.creds, this.options);
122122
await this.waitForReady(this.service);
123-
logger.debug(`${method} - completed the waitForReady for ${this.name}`);
123+
logger.debug(`${method} - end - completed the waitForReady for ${this.name}`);
124124
}
125125

126126
/**
@@ -135,14 +135,16 @@ class ServiceEndpoint {
135135
this.service.close();
136136
this.service = null;
137137
this.connected = false;
138+
this.connectAttempted = false;
138139
}
139140
}
140141

141142
/**
142143
* Check the connection status
143144
*/
144145
async checkConnection() {
145-
logger.debug(`checkConnection[${this.name}] - start `);
146+
const method = `checkConnection[${this.name}]`;
147+
logger.debug('%s - start - connected:%s', method, this.connected);
146148

147149
if (this.connected) {
148150
try {
@@ -153,6 +155,7 @@ class ServiceEndpoint {
153155
}
154156
}
155157

158+
logger.debug('%s - end - connected:%s', method, this.connected);
156159
return this.connected;
157160
}
158161

@@ -227,7 +230,7 @@ class ServiceEndpoint {
227230
url = this.endpoint.url;
228231
}
229232

230-
return `${this.type}- name: ${this.name}, url:${url}`;
233+
return `${this.type}- name: ${this.name}, url:${url}, connected:${this.connected}, connectAttempted:${this.connectAttempted}`;
231234
}
232235

233236
}

fabric-common/test/Channel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ describe('Channel', () => {
512512
channel.addCommitter(getCommitter('committer1'));
513513
const channel_string = channel.toString();
514514
assert.equal(channel_string,
515-
'{"name":"mychannel","committers":["Committer- name: committer1, url:<not connected>"],"endorsers":["Endorser- name: endorser1, url:<not connected>"]}',
515+
'{"name":"mychannel","committers":["Committer- name: committer1, url:<not connected>, connected:true, connectAttempted:false"],"endorsers":["Endorser- name: endorser1, url:<not connected>, connected:true, connectAttempted:false"]}',
516516
'toString has all this'
517517
);
518518
});

fabric-common/test/ServiceEndpoint.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,12 @@ describe('ServiceEndpoint', () => {
236236
describe('#toString', () => {
237237
it('should get a string url', async () => {
238238
const results = serviceEndpoint.toString();
239-
results.should.be.equal('ServiceEndpoint- name: myserviceEndpoint, url:grpc://host:2700');
239+
results.should.be.equal('ServiceEndpoint- name: myserviceEndpoint, url:grpc://host:2700, connected:true, connectAttempted:false');
240240
});
241241
it('should get a string result', async () => {
242242
serviceEndpoint.endpoint = null;
243243
const results = serviceEndpoint.toString();
244-
results.should.be.equal('ServiceEndpoint- name: myserviceEndpoint, url:<not connected>');
244+
results.should.be.equal('ServiceEndpoint- name: myserviceEndpoint, url:<not connected>, connected:true, connectAttempted:false');
245245
});
246246
});
247247
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"test:cucumber": "cucumber-js -f node_modules/cucumber-pretty ./test/scenario/features/*.feature",
3838
"test:ts-cucumber": "cucumber-js -f node_modules/cucumber-pretty ./test/ts-scenario/features/*.feature --require './test/ts-scenario/steps/**/*.ts' --require './test/ts-scenario/support/**/*.ts' --require-module ts-node/register",
3939
"test:ts-cucumberNoHSM": "cucumber-js -f node_modules/cucumber-pretty ./test/ts-scenario/features/*.feature --require './test/ts-scenario/steps/**/*.ts' --require './test/ts-scenario/support/**/*.ts' --require-module ts-node/register --tags 'not @gateway_hsm'",
40-
"test:ts-cucumber-tagged": "npm run test:ts-cucumber -- --tags @base_api_1",
40+
"test:ts-cucumber-tagged": "npm run test:ts-cucumber -- --tags @base_api_2",
4141
"testHeadless": "run-s cleanUp compile lint unitTest:all",
4242
"tapeAndCucumber": "run-s tapeIntegration dockerClean cucumberScenario"
4343
},

test/ts-scenario/features/base_api_v2.feature

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ Scenario: Using only fabric-common on V2 channel
3838
And the request named myDiscoveryRequest for client fred has a event result matching {"result":"Commit success"}
3939
And the request named myDiscoveryRequest for client fred has a commit result matching {"status":"SUCCESS"}
4040

41-
4241
When I create an event service myFilteredEventService as client fred on channel basev2channel
4342
And I regisister a block listener named myFilteredBlockListener with myFilteredEventService for startBlock 1 and endBlock 3 as client fred
4443
And I regisister a chaincode listener named myFilteredChaincodeListener with myFilteredEventService for createCar event on contract fabcar as client fred
@@ -57,3 +56,16 @@ Scenario: Using only fabric-common on V2 channel
5756
Then the event listener myFullBlockListener of myFullEventService has results matching {"block":"4"} as client fred
5857
Then the event listener myFullChaincodeListener of myFullEventService has results matching {"createCar":"Focus"} as client fred
5958
Then the event listener myFullTransactionListener of myFullEventService has results matching {"transaction":"7"} as client fred
59+
60+
When I disconnect Event Service myFilteredEventService as client fred
61+
And I regisister a block listener named myRestartListener with myFilteredEventService for startBlock 1 and endBlock 6 as client fred
62+
And I restart the event service myFilteredEventService as filtered blocks to start at block 0 and end at block 6 as client fred
63+
When I build a new endorsement request named myEventRequest for smart contract named fabcar with arguments [createCar,2008,Chrysler,PTCurser,white,Jones] as client fred on discovery channel basev2channel
64+
And I commit the endorsement request named myEventRequest as client fred on channel basev2channel
65+
Then the event listener myRestartListener of myFilteredEventService has results matching {"block":"6"} as client fred
66+
When I disconnect Event Service myFilteredEventService as client fred
67+
And I regisister a block listener named myRestartListener with myFilteredEventService for startBlock 1 and endBlock 6 as client fred
68+
And I restart the event service myFilteredEventService as filtered blocks to start at block 0 and end at block 6 as client fred
69+
When I build a new endorsement request named myEventRequest for smart contract named fabcar with arguments [createCar,2008,Chrysler,PTCurser,white,Jones] as client fred on discovery channel basev2channel
70+
And I commit the endorsement request named myEventRequest as client fred on channel basev2channel
71+
Then the event listener myRestartListener of myFilteredEventService has results matching {"block":"6"} as client fred

test/ts-scenario/steps/base_api.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ When(/^I create an event service (.+?) as client (.+?) on channel (.+?)$/, { tim
6262
await ClientHelper.createEventService(eventServiceName, clientName, channelName);
6363
});
6464

65-
Then(/^I start the event service (.+?) as (.+?) blocks to start at block (.+?) and end at block (.+?) as client (.+?)$/, { timeout: Constants.INC_SHORT as number },
66-
async (eventServiceName: string, blockType: 'filtered' | 'full' | 'private' | undefined, startBlock: string, endBlock: string, clientName: string) => {
67-
await ClientHelper.startEventService(blockType, eventServiceName, clientName, startBlock, endBlock);
65+
Then(/^I (.+?) the event service (.+?) as (.+?) blocks to start at block (.+?) and end at block (.+?) as client (.+?)$/, { timeout: Constants.INC_SHORT as number },
66+
async (start: 'start' | 'restart', eventServiceName: string, blockType: 'filtered' | 'full' | 'private' | undefined, startBlock: string, endBlock: string, clientName: string) => {
67+
await ClientHelper.startEventService(blockType, eventServiceName, clientName, startBlock, endBlock, start);
6868
});
6969

7070
Then(/^I regisister a block listener named (.+?) with (.+?) for startBlock (.+?) and endBlock (.+?) as client (.+?)$/, { timeout: Constants.INC_SHORT as number },
@@ -86,3 +86,7 @@ Then(/^the event listener (.+?) of (.+?) has results matching (.+?) as client (.
8686
async (listenerName: string, eventServiceName: string, check: string, clientName: string) => {
8787
await ClientHelper.checkEventListenerResults(eventServiceName, clientName, listenerName, check);
8888
});
89+
90+
When(/^I disconnect Event Service (.+?) as client (.+?)$/, {timeout: Constants.HUGE_TIME as number }, async (eventServiceName: string, clientName: string) => {
91+
await ClientHelper.disconnectEventService(eventServiceName, clientName);
92+
});

test/ts-scenario/steps/lib/utility/clientUtils.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,14 @@ export async function createEventService(eventServiceName: string, clientName: s
599599
}
600600
}
601601

602-
export async function startEventService(blockType: 'filtered' | 'full' | 'private' | undefined, eventServiceName: string, clientName: string, startBlock: string, endBlock: string): Promise<void> {
602+
export async function startEventService(
603+
blockType: 'filtered' | 'full' | 'private' | undefined,
604+
eventServiceName: string,
605+
clientName: string,
606+
startBlock: string,
607+
endBlock: string,
608+
start: string): Promise<void> {
609+
603610
const clientObject: any = retrieveClientObject(clientName);
604611
const client: Client = clientObject.client;
605612
const ccp: CommonConnectionProfileHelper = clientObject.ccp;
@@ -631,6 +638,12 @@ export async function startEventService(blockType: 'filtered' | 'full' | 'privat
631638
const peerNames: any = ccp.getPeersForChannel(channelName);
632639
const endpoints: Endpoint[] = [];
633640

641+
if (start === 'restart') {
642+
// we want to use the targets that we used last time
643+
await eventService.send();
644+
return;
645+
}
646+
634647
for (const peerName of peerNames) {
635648
const peerObject: any = ccp.getPeer(peerName);
636649
const endpoint: Endpoint = client.newEndpoint({
@@ -805,3 +818,11 @@ export async function checkEventListenerResults(
805818
BaseUtils.logAndThrow(`Listener object not found ${listenerName}`);
806819
}
807820
}
821+
822+
export async function disconnectEventService(
823+
eventServiceName: string, clientName: string): Promise<void> {
824+
const clientObject: any = retrieveClientObject(clientName);
825+
const eventServiceObject: any = clientObject.eventServices.get(eventServiceName);
826+
827+
eventServiceObject.eventService.close();
828+
}

0 commit comments

Comments
 (0)