Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework CLI and worker spawning to enhance the distributed worker feature #742

Merged
merged 1 commit into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions .travis/checks-and-unit-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ set -e
set -o pipefail

# check reference Caliper package names
# publishNpmPackages.js contains the package dir names as caliper-*, those are fine
if grep -rnE --exclude-dir="caliper-publish" "['\"]caliper-(cli|core|burrow|composer|ethereum|fabric|fisco-bcos|iroha|sawtooth)['\"]" . ; then
echo "^^^ Found incorrect Caliper package names. Use the @hyperledger/ prefix for Caliper packages, e.g., @hyperledger/caliper-core"
exit 1
fi

echo "Caliper package names are correct."
./scripts/check-package-names.sh

# Bootstrap the project again
npm i && npm run repoclean -- --yes && npm run bootstrap
Expand Down
3 changes: 1 addition & 2 deletions packages/caliper-burrow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@

'use strict';

module.exports.AdminClient = require('./lib/burrow');
module.exports.WorkerFactory = require('./lib/burrowWorkerFactory');
module.exports.AdapterFactory = require('./lib/adapterFactory').adapterFactory;
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,16 @@

'use strict';

const childProcess = require('child_process');
const path = require('path');
const BurrowAdapter = require('./burrow');

/**
* Class used to spawn burrow workers
* Constructs a Burrow adapter.
* @param {number} workerIndex The zero-based index of the worker who wants to create an adapter instance. -1 for the master process.
* @return {Promise<BlockchainInterface>} The initialized adapter instance.
* @async
*/
class BurrowWorkerFactory {

/**
* Spawn the worker
* @returns {Object} the child process
*/
async spawnWorker() {
const child = childProcess.fork(path.join(__dirname, './burrowWorker.js'), process.argv.slice(2), { env: process.env});
return child;
}
async function adapterFactory(workerIndex) {
return new BurrowAdapter(workerIndex);
}

module.exports = BurrowWorkerFactory;
module.exports.adapterFactory = adapterFactory;
33 changes: 15 additions & 18 deletions packages/caliper-burrow/lib/burrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@

const fs = require('fs');
const monax = require('@monax/burrow');
const { BlockchainInterface, CaliperUtils, TxStatus } = require('@hyperledger/caliper-core');
const { BlockchainInterface, CaliperUtils, ConfigUtil, TxStatus } = require('@hyperledger/caliper-core');
const logger = CaliperUtils.getLogger('burrow.js');

/**
Read the connection details from the config file.
@param {object} config Adapter config.
@param {string} workspace_root The absolute path to the root location for the configuration files.
@return {object} url, account Connection settings.
*/
function burrowConnect(config, workspace_root) {
function burrowConnect(config) {
let host = config.burrow.network.validator.host;
if (host === null) {
throw new Error('host url not set');
Expand All @@ -38,7 +37,7 @@ function burrowConnect(config, workspace_root) {

let account;
try {
account = fs.readFileSync(CaliperUtils.resolvePath(config.burrow.network.validator.address, workspace_root)).toString();
account = fs.readFileSync(CaliperUtils.resolvePath(config.burrow.network.validator.address)).toString();
} catch (err) {
account = config.burrow.network.validator.address.toString();
}
Expand All @@ -60,14 +59,14 @@ class Burrow extends BlockchainInterface {

/**
* Create a new instance of the {Burrow} class.
* @param {string} config_path The path of the Burrow network configuration file.
* @param {string} workspace_root The absolute path to the root location for the application configuration files.
* @param {number} workerIndex The zero-based index of the worker who wants to create an adapter instance. -1 for the master process. Currently unused.
*/
constructor(config_path, workspace_root) {
super(config_path);
constructor(workerIndex) {
super();
let configPath = CaliperUtils.resolvePath(ConfigUtil.get(ConfigUtil.keys.NetworkConfig));
this.config = require(configPath);
this.statusInterval = null;
this.bcType = 'burrow';
this.workspaceRoot = workspace_root;
}

/**
Expand All @@ -80,25 +79,24 @@ class Burrow extends BlockchainInterface {

/**
* Initialize the {Burrow} object.
* @return {time} sleep
* @param {boolean} workerInit Indicates whether the initialization happens in the worker process.
*/
init() {
return CaliperUtils.sleep(2000);
async init(workerInit) {
return await CaliperUtils.sleep(2000);
}

/**
* Deploy the smart contract specified in the network configuration file.
* @return {object} Promise execution for namereg.
*/
async installSmartContract() {
let config = require(this.configPath);
let connection = burrowConnect(config, this.workspaceRoot);
let connection = burrowConnect(this.config);
let options = { objectReturn: true };
let burrow = monax.createInstance(connection.url, connection.account, options);

let data, abi, bytecode, contract;
try {
data = JSON.parse(fs.readFileSync(CaliperUtils.resolvePath(config.contract.path, this.workspaceRoot)).toString());
data = JSON.parse(fs.readFileSync(CaliperUtils.resolvePath(this.config.contract.path)).toString());
abi = data.Abi;
bytecode = data.Evm.Bytecode.Object;

Expand Down Expand Up @@ -130,12 +128,11 @@ class Burrow extends BlockchainInterface {
* @async
*/
async getContext(name, args) {
let config = require(this.configPath);
let context = config.burrow.context;
let context = this.config.burrow.context;

if (typeof context === 'undefined') {

let connection = burrowConnect(config, this.workspaceRoot);
let connection = burrowConnect(this.config);
let options = { objectReturn: true };
let burrow = monax.createInstance(connection.url, connection.account, options);

Expand Down
51 changes: 0 additions & 51 deletions packages/caliper-burrow/lib/burrowWorker.js

This file was deleted.

18 changes: 12 additions & 6 deletions packages/caliper-cli/caliper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,26 @@ const version = 'v' + require('./package.json').version;

let results = yargs
.commandDir('./lib')
.completion()
.recommendCommands()
.help()
.example('caliper bind\ncaliper benchmark run\n caliper worker launch')
.demand(1)
.demandCommand(1, 1, 'Please specify a command to continue')
.example('caliper bind\ncaliper launch master\ncaliper launch worker')
.wrap(null)
.epilogue('For more information on Hyperledger Caliper: https://hyperledger.github.io/caliper/')
.alias('v', 'version')
.alias('version', 'v')
.alias('help', 'h')
.version(version)
.describe('v', 'show version information')
.describe('version', 'Show version information')
.describe('help', 'Show usage information')
.strict(false)
.argv;

results.thePromise.then( () => {
process.exit(0);
// DO NOT EXIT THE PROCESS HERE
// The event loops of the workers are still running at this point
// The default exit code is 0 anyway
}).catch((error) => {
Logger.error(error);
Logger.error(`Error during command execution: ${error}`);
process.exit(1);
});
2 changes: 1 addition & 1 deletion packages/caliper-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

'use strict';

module.exports.version = require('./package.json');
module.exports.version = require('./package.json').version;
96 changes: 0 additions & 96 deletions packages/caliper-cli/lib/benchmark/lib/runBenchmark.js

This file was deleted.

45 changes: 0 additions & 45 deletions packages/caliper-cli/lib/benchmark/runBenchmarkCommand.js

This file was deleted.

Loading