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

Refresh metadata on any connection error #958

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/cluster/__tests__/brokerPool.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
secureRandom,
} = require('testHelpers')
const { KafkaJSProtocolError, KafkaJSConnectionError } = require('../../errors')
const { createErrorFromCode, errorCodes } = require('../../protocol/error')
const BrokerPool = require('../brokerPool')
const Broker = require('../../broker')

Expand Down Expand Up @@ -393,14 +394,14 @@ describe('Cluster > BrokerPool', () => {
expect(broker.isConnected()).toEqual(true)
})

it('recreates the connection on connection errors', async () => {
it('recreates the connection on ILLEGAL_SASL_STATE error', async () => {
const nodeId = 'fakebroker'
const mockBroker = new Broker({
connection: createConnection(),
logger: newLogger(),
})
jest.spyOn(mockBroker, 'connect').mockImplementationOnce(() => {
throw new KafkaJSConnectionError('Connection lost')
throw createErrorFromCode(errorCodes.find(({ type }) => type === 'ILLEGAL_SASL_STATE').code)
})
brokerPool.brokers[nodeId] = mockBroker

Expand Down
23 changes: 23 additions & 0 deletions src/cluster/__tests__/findBroker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,27 @@ describe('Cluster > findBroker', () => {

expect(cluster.refreshMetadata).toHaveBeenCalled()
})

test('refresh metadata on KafkaJSConnectionError Connection Timeout', async () => {
const nodeId = 0
const mockBroker = new Broker({
connection: createConnection(),
logger: newLogger(),
})

jest.spyOn(mockBroker, 'connect').mockImplementationOnce(() => {
throw new KafkaJSConnectionError('Connection timeout', { broker: mockBroker })
})

jest.spyOn(cluster, 'refreshMetadata')
cluster.brokerPool.brokers[nodeId] = mockBroker

await expect(cluster.findBroker({ nodeId })).rejects.toHaveProperty(
'name',
'KafkaJSConnectionError'
)

await expect(cluster.findBroker({ nodeId })).resolves.toBeInstanceOf(Broker)
expect(cluster.refreshMetadata).toHaveBeenCalled()
})
})
15 changes: 9 additions & 6 deletions src/cluster/brokerPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Broker = require('../broker')
const createRetry = require('../retry')
const shuffle = require('../utils/shuffle')
const arrayDiff = require('../utils/arrayDiff')
const { KafkaJSBrokerNotFound } = require('../errors')
const { KafkaJSBrokerNotFound, KafkaJSProtocolError } = require('../errors')

const { keys, assign, values } = Object
const hasBrokerBeenReplaced = (broker, { host, port, rack }) =>
Expand Down Expand Up @@ -311,13 +311,15 @@ module.exports = class BrokerPool {
} catch (e) {
if (e.name === 'KafkaJSConnectionError' || e.type === 'ILLEGAL_SASL_STATE') {
await broker.disconnect()
}

// Connection refused means this node is down, or the cluster is restarting,
// which requires metadata refresh to discover the new nodes
if (e.code === 'ECONNREFUSED') {
return bail(e)
}
// To avoid reconnecting to an unavailable host, we bail on connection errors
// and refresh metadata on a higher level before reconnecting
if (e.name === 'KafkaJSConnectionError') {
return bail(e)
}

if (e.type === 'ILLEGAL_SASL_STATE') {
// Rebuild the connection since it can't recover from illegal SASL state
broker.connection = await this.connectionBuilder.build({
host: broker.connection.host,
Expand All @@ -326,6 +328,7 @@ module.exports = class BrokerPool {
})

this.logger.error(`Failed to connect to broker, reconnecting`, { retryCount, retryTime })
throw new KafkaJSProtocolError(e, { retriable: true })
}

if (e.retriable) throw e
Expand Down
2 changes: 1 addition & 1 deletion src/cluster/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ module.exports = class Cluster {
if (
e.name === 'KafkaJSBrokerNotFound' ||
e.name === 'KafkaJSLockTimeout' ||
e.code === 'ECONNREFUSED'
e.name === 'KafkaJSConnectionError'
) {
await this.refreshMetadata()
}
Expand Down
4 changes: 2 additions & 2 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class KafkaJSNonRetriableError extends KafkaJSError {
}

class KafkaJSProtocolError extends KafkaJSError {
constructor(e) {
super(e, { retriable: e.retriable })
constructor(e, { retriable = e.retriable } = {}) {
super(e, { retriable })
this.type = e.type
this.code = e.code
this.name = 'KafkaJSProtocolError'
Expand Down