Skip to content
Open
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
30 changes: 28 additions & 2 deletions lib/clients/ZookeeperManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ class ZookeeperManager extends EventEmitter {
this._connect();
}

zkDetails(client) {
const localAddress = client.connectionManager?.socket?.address();
const sessionId = client.getSessionId?.();
return {
sessionId: sessionId ? `0x${sessionId.toString('hex')}` : null,
socket: localAddress ? `${localAddress.address || ''}:${localAddress.port || ''}` : null,
state: client.getState?.() || null,
xid: client.connectionManager?.xid,
zxid: client.connectionManager?.zxid?.toString('hex'),
nextServerIndex: client.connectionManager?.nextServerIndex,
serverAttempts: client.connectionManager?.serverAttempts,
};
}

/**
* Establishes a connection to the ZooKeeper server.
*
Expand Down Expand Up @@ -51,6 +65,10 @@ class ZookeeperManager extends EventEmitter {
async.series([
// Check zookeeper client is connected
next => this.client.once('connected', next),
next => {
this.log.info('connected to zookeeper', { zk: this.zkDetails(this.client) });
next();
},
// Create namespace if it exists and options.autoCreateNamespace is true
next => {
// TODO: ARTESCA-10337 The 'autoCreateNamespace' functionality is currently specific to
Expand Down Expand Up @@ -82,7 +100,12 @@ class ZookeeperManager extends EventEmitter {
const hostPort = this.connectionString.slice(0, nsIndex);
rootZkClient = zookeeper.createClient(hostPort, this.options);
rootZkClient.connect();
rootZkClient.once('connected', () => next());
rootZkClient.once('connected', () => {
this.log.info('connected to root zookeeper', {
zk: this.zkDetails(rootZkClient),
});
next();
});
},
// Once connected, use the root zookeeper client to create the namespace
next => rootZkClient.mkdirp(namespace, err => {
Expand All @@ -99,15 +122,17 @@ class ZookeeperManager extends EventEmitter {
this.client.once('expired', () => {
this.log.info('zookeeper client expired', {
method: 'ZookeeperManager.once.expired',
zk: this.zkDetails(this.client),
});
// establish a new session with the ZooKeeper.
this._connect();
});

this.client.on('state', state => {
this.log.debug('zookeeper new state', {
this.log.info('zookeeper new state', {
state,
method: 'ZookeeperManager.on.state',
zk: this.zkDetails(this.client),
});
});

Expand All @@ -131,6 +156,7 @@ class ZookeeperManager extends EventEmitter {
* @return {undefined}
*/
close() {
this.log.info('closing existing zookeeper client', { zk: this.zkDetails(this.client) });
this.client.close();
return;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/clients/ZookeeperManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ describe('ZookeeperManager', () => {
beforeEach(() => {
// Create a mock client
mockClient = {
connectionManager: {
socket: {
address: () => ({ address: '127.0.0.1', port: 42000 }),
},
xid: 1,
zxid: Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0xFF]),
nextServerIndex: 1,
serverAttempts: 1,
},
getSessionId: () => Buffer.from([0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]),
getState: () => ({ name: 'SYNC_CONNECTED', code: 3 }),
on: sinon.stub(),
once: sinon.stub(),
connect: sinon.stub(),
Expand Down
Loading