Skip to content

Commit

Permalink
BE-855 Stop unnecessary sync process triggered by FabricEvent (#240)
Browse files Browse the repository at this point in the history
* BE-855 Stop unnecessary sync process triggered by FabricEvent

Signed-off-by: Atsushi Neki <nekiaiken@gmail.com>

* BE-855 Add a step for update of chaincode table to init process

Signed-off-by: Atsushi Neki <nekiaiken@gmail.com>
  • Loading branch information
nekia authored May 30, 2021
1 parent 7e3f1b6 commit 44ae9ee
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,8 @@
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"no-console": ["error"],
"import/extensions": ["off"]
"import/extensions": ["off"],
"wrap-iife": ["error", "inside"]
}
}
]
Expand Down
13 changes: 6 additions & 7 deletions app/platform/fabric/sync/FabricEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {helper} from '../../../common/helper';
import { helper } from '../../../common/helper';

const logger = helper.getLogger('FabricEvent');

Expand All @@ -12,10 +12,9 @@ const logger = helper.getLogger('FabricEvent');
* @class FabricEvent
*/
export class FabricEvent {

client : any;
fabricServices : any;
static channelEventHubs : any;
client: any;
fabricServices: any;
static channelEventHubs: any;

/**
* Creates an instance of FabricEvent.
Expand Down Expand Up @@ -47,7 +46,7 @@ export class FabricEvent {
continue;
}

this.createChannelEventHub(channel_name);
await this.createChannelEventHub(channel_name);
}
}

Expand All @@ -71,7 +70,7 @@ export class FabricEvent {
}
},
{
startBlock: 1,
// Keep startBlock undefined because expecting to start listening from the current block.
type: 'full'
}
);
Expand Down
25 changes: 13 additions & 12 deletions app/platform/fabric/sync/SyncPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,14 @@ export class SyncPlatform {
}

// Updating the client network and other details to DB
const res = await this.syncService.synchNetworkConfigToDB(this.client);
if (!res) {
return;
}

setInterval(() => {
await (async function updateNetworkConfig(sync) {
logger.info('Updating the client network and other details to DB');
this.syncService.synchNetworkConfigToDB(this.client);
}, 30000);
const res = await sync.syncService.synchNetworkConfigToDB(sync.client);
if (!res) {
logger.error('Failed to update network config to DB');
}
setTimeout(updateNetworkConfig, 30000, sync);
})(this);

// Start event
this.eventHub = new FabricEvent(this.client, this.syncService);
Expand All @@ -118,9 +117,11 @@ export class SyncPlatform {
* Setting interval for validating any missing block from the current client ledger
* Set blocksSyncTime property in platform config.json in minutes
*/
setInterval(() => {
this.isChannelEventHubConnected();
}, this.blocksSyncTime);
(function validateMissingBlocks(sync) {
sync.isChannelEventHubConnected();
setTimeout(validateMissingBlocks, sync.blocksSyncTime, sync);
})(this);

logger.debug(
'******* Initialization end for child client process %s ******',
this.network_id
Expand All @@ -137,7 +138,7 @@ export class SyncPlatform {
// Validate channel event is connected
const status = this.eventHub.isChannelEventHubConnected(channel_name);
if (status) {
await this.syncService.synchBlocks(this.client, channel_name);
await this.syncService.syncBlocks(this.client, channel_name);
} else {
// Channel client is not connected then it will reconnect
this.eventHub.connectChannelEventHub(channel_name);
Expand Down
11 changes: 8 additions & 3 deletions app/platform/fabric/sync/SyncService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ export class SyncServices {
} else {
return false;
}

// Need that chaincode table is synced up to existing chaincode at this moment
await this.insertNewChannelChaincode(client, channel_genesis_hash, null);
}
return true;
}
Expand Down Expand Up @@ -305,7 +308,7 @@ export class SyncServices {
await this.persistence
.getCrudService()
.saveChaincode(network_id, chaincode_row);
if (discoveryResults && discoveryResults.peers_by_org) {
if (discoveryResults?.peers_by_org) {
for (const org_name in discoveryResults.peers_by_org) {
const org = discoveryResults.peers_by_org[org_name];
for (const peer of org.peers) {
Expand Down Expand Up @@ -354,12 +357,13 @@ export class SyncServices {
.saveChaincodPeerRef(network_id, chaincode_peer_row);
}

async synchBlocks(client, channel_name) {
async syncBlocks(client, channel_name) {
const network_id = client.getNetworkId();

const synch_key = `${network_id}_${channel_name}`;
logger.info(`syncBlocks: Start >> ${synch_key}`);
if (this.synchInProcess.includes(synch_key)) {
logger.info(`Block synch in process for >> ${network_id}_${channel_name}`);
logger.info(`syncBlocks: Block sync in process for >> ${synch_key}`);
return;
}
this.synchInProcess.push(synch_key);
Expand Down Expand Up @@ -393,6 +397,7 @@ export class SyncServices {
}
const index = this.synchInProcess.indexOf(synch_key);
this.synchInProcess.splice(index, 1);
logger.info(`syncBlocks: Finish >> ${synch_key}`);
}

async updateDiscoveredChannel(client, channel_name, channel_genesis_hash) {
Expand Down
6 changes: 3 additions & 3 deletions app/test/SyncService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ describe('processBlockEvent', () => {
});
});

describe('synchBlocks', () => {
describe('syncBlocks', () => {
let sync: SyncServices;

before(() => {
Expand All @@ -219,7 +219,7 @@ describe('synchBlocks', () => {
const stubClient = setupClient();
const stubProcessBlockEvent = sinon.stub(sync, 'processBlockEvent');

await sync.synchBlocks(stubClient, VALID_CHANNEL_NAME);
await sync.syncBlocks(stubClient, VALID_CHANNEL_NAME);
expect(stubProcessBlockEvent.calledTwice).to.be.true;
stubProcessBlockEvent.restore();
});
Expand All @@ -230,7 +230,7 @@ describe('synchBlocks', () => {
stubProcessBlockEvent.onFirstCall().throws('Block already in processing');
stubError.reset();

await sync.synchBlocks(stubClient, VALID_CHANNEL_NAME);
await sync.syncBlocks(stubClient, VALID_CHANNEL_NAME);
expect(stubProcessBlockEvent.calledTwice).to.be.true;
expect(stubError.calledWith('Failed to process Block # 1')).to.be.true;
expect(stubError.calledWith('Failed to process Block # 2')).to.be.false;
Expand Down
2 changes: 1 addition & 1 deletion client/e2e-test/startnetwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ startNetwork = async () => {
}

// Wait for a while to get ready to start REST API server
await new Promise(r => setTimeout(r, 20000));
await new Promise(r => setTimeout(r, 40000));
};

startNetwork();

0 comments on commit 44ae9ee

Please sign in to comment.