Skip to content

Commit

Permalink
feat(driver): added Monitor to fabric driver for missed events
Browse files Browse the repository at this point in the history
Signed-off-by: Sandeep Nishad <sandeep.nishad1@ibm.com>
  • Loading branch information
sandeepnRES committed Apr 25, 2023
1 parent b1758e6 commit b6fa3ac
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 194 deletions.
2 changes: 2 additions & 0 deletions weaver/core/drivers/fabric-driver/.env.docker.template
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ WALLET_PATH=
TLS_CREDENTIALS_DIR=<dir-with-tls-cert-and-key>
LEVELDB_LOCKED_MAX_RETRIES=<max-attempts-in-retry>
LEVELDB_LOCKED_RETRY_BACKOFF_MSEC=<retry-back-off-time-in-ms>
ENABLE_MONITOR=<true|false>
MONITOR_SYNC_PERIOD=<monitor-sync-period-in-seconds>
DOCKER_IMAGE_NAME=ghcr.io/hyperledger-labs/weaver-fabric-driver
DOCKER_TAG=1.4.0
EXTERNAL_NETWORK=<docker-bridge-network>
Expand Down
4 changes: 3 additions & 1 deletion weaver/core/drivers/fabric-driver/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ DB_PATH=driverdbs
WALLET_PATH=
DEBUG=true
LEVELDB_LOCKED_MAX_RETRIES=
LEVELDB_LOCKED_RETRY_BACKOFF_MSEC=
LEVELDB_LOCKED_RETRY_BACKOFF_MSEC=
ENABLE_MONITOR=false
MONITOR_SYNC_PERIOD=
2 changes: 2 additions & 0 deletions weaver/core/drivers/fabric-driver/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ services:
- DEBUG=false
- LEVELDB_LOCKED_MAX_RETRIES=${LEVELDB_LOCKED_MAX_RETRIES}
- LEVELDB_LOCKED_RETRY_BACKOFF_MSEC=${LEVELDB_LOCKED_RETRY_BACKOFF_MSEC}
- ENABLE_MONITOR=${ENABLE_MONITOR}
- MONITOR_SYNC_PERIOD=${MONITOR_SYNC_PERIOD}
volumes:
- ${CONNECTION_PROFILE}:/fabric-driver/ccp.json
- ${DRIVER_CONFIG}:/fabric-driver/config.json
Expand Down
6 changes: 5 additions & 1 deletion weaver/core/drivers/fabric-driver/server/dbConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class LevelDBConnector implements DBConnector {
dbRetryBackoffTime: number;

constructor(
dbName: string
dbName: string,
retryTimeout: number = 0
) {
if (!dbName || dbName.length == 0) {
dbName = "driverdb";
Expand All @@ -59,6 +60,9 @@ class LevelDBConnector implements DBConnector {
this.dbOpenMaxRetries = process.env.LEVELDB_LOCKED_MAX_RETRIES ? parseInt(process.env.LEVELDB_LOCKED_MAX_RETRIES) : 250;
// Retry back off time in ms, default 20ms
this.dbRetryBackoffTime = process.env.LEVELDB_LOCKED_RETRY_BACKOFF_MSEC ? parseInt(process.env.LEVELDB_LOCKED_RETRY_BACKOFF_MSEC) : 20;
if (retryTimeout > 0) {
this.dbOpenMaxRetries = Math.floor(retryTimeout / this.dbRetryBackoffTime);
}
}

async open(
Expand Down
446 changes: 258 additions & 188 deletions weaver/core/drivers/fabric-driver/server/listener.ts

Large diffs are not rendered by default.

22 changes: 20 additions & 2 deletions weaver/core/drivers/fabric-driver/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import events_grpc_pb from '@hyperledger-labs/weaver-protos-js/relay/events_grpc
import state_pb from '@hyperledger-labs/weaver-protos-js/common/state_pb';
import { invoke, packageFabricView } from './fabric-code';
import 'dotenv/config';
import { loadEventSubscriptionsFromStorage } from './listener'
import { loadEventSubscriptionsFromStorage, monitorBlockForMissedEvents } from './listener'
import { walletSetup } from './walletSetup';
import { subscribeEventHelper, unsubscribeEventHelper, signEventSubscriptionQuery, writeExternalStateHelper } from "./events"
import * as path from 'path';
import { handlePromise, relayCallback, getRelayClientForQueryResponse, getRelayClientForEventSubscription } from './utils';
import { handlePromise, relayCallback, getRelayClientForQueryResponse, getRelayClientForEventSubscription, delay } from './utils';
import { dbConnectionTest, eventSubscriptionTest } from "./tests"
import driverPb from '@hyperledger-labs/weaver-protos-js/driver/driver_pb';
import logger from './logger';
Expand Down Expand Up @@ -253,6 +253,22 @@ const configSetup = async () => {
logger.info(`Load Event Subscriptions Status: ${status}`);
};

const monitorService = async () => {
const delayTime: number = parseInt(process.env.MONITOR_SYNC_PERIOD ? process.env.MONITOR_SYNC_PERIOD : '30');
const networkName = process.env.NETWORK_NAME ? process.env.NETWORK_NAME : 'network1';
const flagEnable = process.env.ENABLE_MONITOR === 'false' ? false : true;
if (flagEnable) {
logger.info("Starting monitor...");
logger.info(`Monitor sync period: ${delayTime}`);
} else {
logger.info("Monitor disabled.");
}
while (flagEnable) {
await monitorBlockForMissedEvents(networkName);
await delay(delayTime * 1000);
}
}

// SERVER: Start the server with the provided url.
// TODO: We should have credentials locally to ensure that the driver can only communicate with the local relay.
if (process.env.DRIVER_TLS === 'true') {
Expand All @@ -268,13 +284,15 @@ if (process.env.DRIVER_TLS === 'true') {
configSetup().then(() => {
logger.info('Starting server with TLS');
server.start();
monitorService();
});
});
} else {
server.bindAsync(`${process.env.DRIVER_ENDPOINT}`, ServerCredentials.createInsecure(), (cb) => {
configSetup().then(() => {
logger.info('Starting server without TLS');
server.start();
monitorService();
});
});
}
2 changes: 1 addition & 1 deletion weaver/core/drivers/fabric-driver/server/walletSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const getDriverKeyCert = async (): Promise<any> => {
const walletPath = process.env.WALLET_PATH ? process.env.WALLET_PATH : path.join(process.cwd(), `wallet-${process.env.NETWORK_NAME ? process.env.NETWORK_NAME : 'network1'}`);
const config = getConfig();
const wallet = await Wallets.newFileSystemWallet(walletPath);
logger.debug(`Wallet path: ${walletPath}`);
logger.info(`Wallet path: ${walletPath}, relay id: ${config.relay.name}`);

const [keyCert, keyCertError] = await handlePromise(
InteroperableHelper.getKeyAndCertForRemoteRequestbyUserName(wallet, config.relay.name)
Expand Down
2 changes: 1 addition & 1 deletion weaver/core/drivers/fabric-driver/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"target": "es2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
Expand Down

0 comments on commit b6fa3ac

Please sign in to comment.