Skip to content

Commit a45e5d1

Browse files
BB-727: Log zookeeper details
1 parent f4614f2 commit a45e5d1

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

lib/clients/ZookeeperManager.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ class ZookeeperManager extends EventEmitter {
2121
this._connect();
2222
}
2323

24+
zkDetails(client) {
25+
const localAddress = client.connectionManager?.socket?.address();
26+
const sessionId = client.getSessionId?.();
27+
return {
28+
sessionId: sessionId ? `0x${sessionId.toString('hex')}` : null,
29+
socket: localAddress ? `${localAddress.address || ''}:${localAddress.port || ''}` : null,
30+
state: client.getState?.() || null,
31+
xid: client.connectionManager?.xid,
32+
zxid: client.connectionManager?.zxid?.toString('hex'),
33+
nextServerIndex: client.connectionManager?.nextServerIndex,
34+
serverAttempts: client.connectionManager?.serverAttempts,
35+
};
36+
}
37+
2438
/**
2539
* Establishes a connection to the ZooKeeper server.
2640
*
@@ -51,6 +65,10 @@ class ZookeeperManager extends EventEmitter {
5165
async.series([
5266
// Check zookeeper client is connected
5367
next => this.client.once('connected', next),
68+
next => {
69+
this.log.info('connected to zookeeper', { zk: this.zkDetails(this.client) });
70+
next();
71+
},
5472
// Create namespace if it exists and options.autoCreateNamespace is true
5573
next => {
5674
// TODO: ARTESCA-10337 The 'autoCreateNamespace' functionality is currently specific to
@@ -82,7 +100,12 @@ class ZookeeperManager extends EventEmitter {
82100
const hostPort = this.connectionString.slice(0, nsIndex);
83101
rootZkClient = zookeeper.createClient(hostPort, this.options);
84102
rootZkClient.connect();
85-
rootZkClient.once('connected', () => next());
103+
rootZkClient.once('connected', () => {
104+
this.log.info('connected to root zookeeper', {
105+
zk: this.zkDetails(rootZkClient),
106+
});
107+
next();
108+
});
86109
},
87110
// Once connected, use the root zookeeper client to create the namespace
88111
next => rootZkClient.mkdirp(namespace, err => {
@@ -99,15 +122,17 @@ class ZookeeperManager extends EventEmitter {
99122
this.client.once('expired', () => {
100123
this.log.info('zookeeper client expired', {
101124
method: 'ZookeeperManager.once.expired',
125+
zk: this.zkDetails(this.client),
102126
});
103127
// establish a new session with the ZooKeeper.
104128
this._connect();
105129
});
106130

107131
this.client.on('state', state => {
108-
this.log.debug('zookeeper new state', {
132+
this.log.info('zookeeper new state', {
109133
state,
110134
method: 'ZookeeperManager.on.state',
135+
zk: this.zkDetails(this.client),
111136
});
112137
});
113138

@@ -131,6 +156,7 @@ class ZookeeperManager extends EventEmitter {
131156
* @return {undefined}
132157
*/
133158
close() {
159+
this.log.info('closing existing zookeeper client', { zk: this.zkDetails(this.client) });
134160
this.client.close();
135161
return;
136162
}

tests/unit/clients/ZookeeperManager.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ describe('ZookeeperManager', () => {
1212
beforeEach(() => {
1313
// Create a mock client
1414
mockClient = {
15+
connectionManager: {
16+
socket: {
17+
address: () => ({ address: '127.0.0.1', port: 42000 }),
18+
},
19+
xid: 1,
20+
zxid: Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0xFF]),
21+
nextServerIndex: 1,
22+
serverAttempts: 1,
23+
},
24+
getSessionId: () => Buffer.from([0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]),
25+
getState: () => ({ name: 'SYNC_CONNECTED', code: 3 }),
1526
on: sinon.stub(),
1627
once: sinon.stub(),
1728
connect: sinon.stub(),

0 commit comments

Comments
 (0)