retrieveServices() { for (const [key, val] of Object.entries(this.dependencies)) { const path = `/${this.root}/${val.interface}/providers`; this.client.getChildren( path, this.watchService.bind(this), this.resolveService(path, key, val) ); } }
set(site, state, cb) { const data = Buffer.from(state); const path = `${this.statePath}/${site}`; this.zkClient.setData(path, data, cb); }
isReady() { const state = this._zkClient && this._zkClient.getState(); return this._producer && this._producer.isReady() && state && state.code === zookeeper.State.SYNC_CONNECTED.code && this._kafkaBacklogMetrics.isReady(); }
var coordinator = function () { var self = this; self._client = zk.createClient(ZK_CONFIG.hosts) self._client.once('connected', self.lookForLeader.bind(self)); self._self._client.connect(); self.isLeader = false; self.emiter = new EventEmitter(); }
beforeEach(function () { sandbox = sinon.sandbox.create(); sandbox.stub(zkClient, "connect", function () { this.emit("connected"); }); sandbox.stub(zkClient, "getChildren", function (path, cb) { cb(zookeeper.Exception.create(zookeeper.Exception.CONNECTION_LOSS), null, 1); }); eventFired = false; });
function loadHealthcheck() { healthServer.onReadyCheck(() => { const state = ingestionPopulator.zkStatus(); if (state.code === zookeeper.State.SYNC_CONNECTED.code) { return true; } log.error(`Zookeeper is not connected! ${state}`); return false; }); log.info('Starting health probe server'); healthServer.start(); }
isReady() { return this._zookeeper && this._zookeeper.getState().code === zookeeper.State.SYNC_CONNECTED.code; }
close() { if (this.zkClient) { this.zkClient.close(); this.zkClient = null; } }
self._client.create(ZK_CONFIG.leader_znode, zk.CreateMode.EPHEMERAL, function (error, path) { if (error) { if (error.code == zk.Exception.NODE_EXISTS) { // Oops too late return self.lookForLeader(); } else { return self.emiter.emit(Events.UNEXPECTED_ZK_ERROR, error); } } self.isLeader = true; self.emiter.emit(Events.GAINED_LEADERSHIP); });
_unregisterOwner(cb) { const zkPath = `${OWNERS}/${this._myName}`; this._withRandomDelayIfTest( () => this._client.remove(zkPath, -1, err => { if (err) { return cb(err); } this._log.debug('unregistered owner zk node', { zkPath: `${this._zkEndpoint}${zkPath}` }); return 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); }) ); }
self._client.getData(ZK_CONFIG.leader_znode, function watch (event) { // Someone else is already the leader if (event.type == zk.Event.NODE_DELETED) { self.emiter.emit(Events.LEADER_CHANGED); self.claimLeadership(); } }, function (error) { if (error && error.code == zk.Exception.NO_NODE) { // Leader spot available return self.claimLeadership(); } else if (error) { return self.emiter.emit(Events.UNEXPECTED_ZK_ERROR, error); } });
initClient() { this.client = zookeeper.createClient(this.registry, { sessionTimeout: 30000, spinDelay: 1000, retries: 5 }); this.client.connect(); this.client.once("connected", this.onOnceConnected.bind(this)); this.checkConnection(); }
async function main() { const client = zookeeper.createClient('127.0.0.1:2181', { authInfo: { scheme: 'digest', auth: 'gxcsoccer:123456', }, }); await client.mkdirp('/acl'); await client.setACL('/acl', [ new ACL( Permission.ALL, new Id('auth', 'gxcsoccer:123456') ), ], -1); const acls = await client.getACL('/acl'); console.log('acls', acls); }
_unregisterLeader(cb) { // de-register from election queue const zkPath = `${LEADERS}/${this._myLeaderName}`; this._withRandomDelayIfTest( () => this._client.remove(zkPath, -1, err => { if (err) { return cb(err); } this._log.debug('unregistered leader zk node', { zkPath: `${this._zkEndpoint}${zkPath}` }); return cb(); }) ); }