_populateZkHierarchy(cb) { async.each([ LEADERS, OWNERS, PROVISIONS, ], (zkPath, done) => this._client.create(zkPath, err => { if (err && err.getCode() !== zookeeper.Exception.NODE_EXISTS) { this._log.error('error populating zk node', { zkPath: `${this._zkEndpoint}${zkPath}`, error: err }); return done(err); } this._log.debug('populated zk node', { zkPath }); return done(); }), cb); }
_addProvisionZkNodes(provisionList, cb) { async.eachLimit( provisionList.map( item => `${PROVISIONS}/${item}`), 20, (zkPath, done) => this._client.create(zkPath, err => { if (err && err.getCode() !== zookeeper.Exception.NODE_EXISTS) { this._log.error('error adding provision node', { zkPath: `${this._zkEndpoint}${zkPath}`, error: err }); return done(err); } this._log.debug('added new provision zk node', { zkPath: `${this._zkEndpoint}${zkPath}` }); return done(); }), cb); }
_registerOwner(cb) { this._myName = _genRandomHexChars(12); const zkPath = `${OWNERS}/${this._myName}`; this._withRandomDelayIfTest( () => this._client.create( zkPath, null, zookeeper.ACL.OPEN_ACL_UNSAFE, zookeeper.CreateMode.EPHEMERAL, err => { if (err) { return cb(err); } this._log.debug('registered owner zk node', { zkPath: `${this._zkEndpoint}${zkPath}` }); return cb(null, zkPath); }) ); }
_registerLeader(cb) { // register in election queue const zkPath = `${LEADERS}${LEADER}`; this._withRandomDelayIfTest( () => this._client.create( zkPath, null, zookeeper.ACL.OPEN_ACL_UNSAFE, zookeeper.CreateMode.EPHEMERAL_SEQUENTIAL, (err, _path) => { if (err) { return cb(err); } this._myLeaderName = path.basename(_path); this._log.debug('registered leader zk node', { zkPath: `${this._zkEndpoint}${path}` }); return cb(); }) ); }