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

Add single requestTimeout runner instead of setTimeout per request #650

Merged
merged 22 commits into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 0 additions & 1 deletion src/admin/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ describe('Admin', () => {
const emitter = new InstrumentationEventEmitter()
const cluster = createCluster({
requestTimeout: 1,
enforceRequestTimeout: true,
goriunov marked this conversation as resolved.
Show resolved Hide resolved
instrumentationEmitter: emitter,
})

Expand Down
2 changes: 0 additions & 2 deletions src/cluster/connectionBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ module.exports = ({
sasl,
clientId,
requestTimeout,
enforceRequestTimeout,
connectionTimeout,
maxInFlightRequests,
retry,
Expand Down Expand Up @@ -47,7 +46,6 @@ module.exports = ({
socketFactory,
connectionTimeout,
requestTimeout,
enforceRequestTimeout,
maxInFlightRequests,
instrumentationEmitter,
retry,
Expand Down
2 changes: 0 additions & 2 deletions src/cluster/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ module.exports = class Cluster {
authenticationTimeout,
reauthenticationThreshold,
requestTimeout,
enforceRequestTimeout,
metadataMaxAge,
retry,
allowExperimentalV011,
Expand All @@ -72,7 +71,6 @@ module.exports = class Cluster {
clientId,
connectionTimeout,
requestTimeout,
enforceRequestTimeout,
maxInFlightRequests,
retry,
})
Expand Down
1 change: 0 additions & 1 deletion src/consumer/__tests__/instrumentationEvents.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ describe('Consumer > Instrumentation Events', () => {
cluster = createCluster({
instrumentationEmitter: emitter,
requestTimeout: 1,
enforceRequestTimeout: true,
})
const requestListener = jest.fn().mockName('request_timeout')

Expand Down
2 changes: 0 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ module.exports = class Client {
authenticationTimeout,
reauthenticationThreshold,
requestTimeout,
enforceRequestTimeout = false,
retry,
socketFactory = defaultSocketFactory(),
logLevel = INFO,
Expand Down Expand Up @@ -59,7 +58,6 @@ module.exports = class Client {
authenticationTimeout,
reauthenticationThreshold,
requestTimeout,
enforceRequestTimeout,
metadataMaxAge,
instrumentationEmitter,
allowAutoTopicCreation,
Expand Down
2 changes: 0 additions & 2 deletions src/network/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ module.exports = class Connection {
clientId = 'kafkajs',
connectionTimeout = 1000,
requestTimeout = 30000,
enforceRequestTimeout = false,
maxInFlightRequests = null,
instrumentationEmitter = null,
retry = {},
Expand Down Expand Up @@ -71,7 +70,6 @@ module.exports = class Connection {
instrumentationEmitter,
maxInFlightRequests,
requestTimeout,
enforceRequestTimeout,
clientId,
broker: this.broker,
logger: logger.namespace('RequestQueue'),
Expand Down
4 changes: 1 addition & 3 deletions src/network/connection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ describe('Network > Connection', () => {

test('respect the requestTimeout', async () => {
const protocol = apiVersions()
connection = new Connection(
connectionOpts({ requestTimeout: 50, enforceRequestTimeout: true })
)
connection = new Connection(connectionOpts({ requestTimeout: 50 }))
const originalProcessData = connection.processData

connection.processData = async data => {
Expand Down
13 changes: 10 additions & 3 deletions src/network/requestQueue/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ module.exports = class RequestQueue {
instrumentationEmitter = null,
maxInFlightRequests,
requestTimeout,
enforceRequestTimeout,
clientId,
broker,
logger,
}) {
this.instrumentationEmitter = instrumentationEmitter
this.maxInFlightRequests = maxInFlightRequests
this.requestTimeout = requestTimeout
this.enforceRequestTimeout = enforceRequestTimeout
this.clientId = clientId
this.broker = broker
this.logger = logger
Expand All @@ -42,6 +40,16 @@ module.exports = class RequestQueue {
queueSize: this.pending.length,
})
}

// FIXME: How do we correctly clearInterval ?
goriunov marked this conversation as resolved.
Show resolved Hide resolved
// run requests check every 10ms to see if any inflight requests timed out
this.requestTimeoutIntervalId = setInterval(() => {
goriunov marked this conversation as resolved.
Show resolved Hide resolved
this.inflight.forEach(request => {
if (Date.now() - request.sentAt > request.requestTimeout) {
request.timeoutRequest()
}
})
}, 10)
}

/**
Expand Down Expand Up @@ -69,7 +77,6 @@ module.exports = class RequestQueue {
broker: this.broker,
clientId: this.clientId,
instrumentationEmitter: this.instrumentationEmitter,
enforceRequestTimeout: this.enforceRequestTimeout,
requestTimeout,
send: () => {
this.inflight.set(correlationId, socketRequest)
Expand Down
47 changes: 19 additions & 28 deletions src/network/requestQueue/socketRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ module.exports = class SocketRequest {
*/
constructor({
requestTimeout,
enforceRequestTimeout,
broker,
clientId,
entry,
Expand All @@ -60,7 +59,6 @@ module.exports = class SocketRequest {
}) {
this.createdAt = Date.now()
this.requestTimeout = requestTimeout
this.enforceRequestTimeout = enforceRequestTimeout
this.broker = broker
this.clientId = clientId
this.entry = entry
Expand All @@ -72,7 +70,6 @@ module.exports = class SocketRequest {
this.sentAt = null
this.duration = null
this.pendingDuration = null
this.timeoutId = null

this[PRIVATE.STATE] = REQUEST_STATE.PENDING
this[PRIVATE.EMIT_EVENT] = (eventName, payload) =>
Expand All @@ -89,32 +86,28 @@ module.exports = class SocketRequest {
this.sentAt = Date.now()
this.pendingDuration = this.sentAt - this.createdAt
this[PRIVATE.STATE] = REQUEST_STATE.SENT
}

const timeoutCallback = () => {
const { apiName, apiKey, apiVersion } = this.entry
const requestInfo = `${apiName}(key: ${apiKey}, version: ${apiVersion})`
const eventData = {
broker: this.broker,
clientId: this.clientId,
correlationId: this.correlationId,
createdAt: this.createdAt,
sentAt: this.sentAt,
pendingDuration: this.pendingDuration,
}

this.timeoutHandler()
this.rejected(new KafkaJSRequestTimeoutError(`Request ${requestInfo} timed out`, eventData))
this[PRIVATE.EMIT_EVENT](events.NETWORK_REQUEST_TIMEOUT, {
...eventData,
apiName,
apiKey,
apiVersion,
})
timeoutRequest() {
const { apiName, apiKey, apiVersion } = this.entry
const requestInfo = `${apiName}(key: ${apiKey}, version: ${apiVersion})`
const eventData = {
broker: this.broker,
clientId: this.clientId,
correlationId: this.correlationId,
createdAt: this.createdAt,
sentAt: this.sentAt,
pendingDuration: this.pendingDuration,
}

if (this.enforceRequestTimeout) {
this.timeoutId = setTimeout(timeoutCallback, this.requestTimeout)
}
this.timeoutHandler()
this.rejected(new KafkaJSRequestTimeoutError(`Request ${requestInfo} timed out`, eventData))
this[PRIVATE.EMIT_EVENT](events.NETWORK_REQUEST_TIMEOUT, {
...eventData,
apiName,
apiKey,
apiVersion,
})
}

completed({ size, payload }) {
Expand All @@ -123,7 +116,6 @@ module.exports = class SocketRequest {
next: REQUEST_STATE.COMPLETED,
})

clearTimeout(this.timeoutId)
const { entry, correlationId, broker, clientId, createdAt, sentAt, pendingDuration } = this

this[PRIVATE.STATE] = REQUEST_STATE.COMPLETED
Expand Down Expand Up @@ -151,7 +143,6 @@ module.exports = class SocketRequest {
next: REQUEST_STATE.REJECTED,
})

clearTimeout(this.timeoutId)
this[PRIVATE.STATE] = REQUEST_STATE.REJECTED
this.duration = Date.now() - this.sentAt
this.entry.reject(error)
Expand Down
78 changes: 36 additions & 42 deletions src/network/requestQueue/socketRequest.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const sleep = require('../../utils/sleep')
const SocketRequest = require('./socketRequest')
const { KafkaJSRequestTimeoutError, KafkaJSNonRetriableError } = require('../../errors')
const { KafkaJSNonRetriableError } = require('../../errors')
const InstrumentationEventEmitter = require('../../instrumentation/emitter')
const events = require('../instrumentationEvents')

Expand Down Expand Up @@ -41,37 +40,32 @@ describe('Network > SocketRequest', () => {
it('sends the request using the provided function', () => {
expect(request.sentAt).toEqual(null)
expect(request.pendingDuration).toEqual(null)
expect(request.timeoutId).toEqual(null)

request.enforceRequestTimeout = true
request.send()

expect(sendRequest).toHaveBeenCalled()
expect(request.sentAt).toEqual(expect.any(Number))
expect(request.pendingDuration).toEqual(expect.any(Number))
expect(request.timeoutId).not.toEqual(null)

clearTimeout(request.timeoutId)
})

it('does not call sendRequest more than once', () => {
request.send()
expect(() => request.send()).toThrow(KafkaJSNonRetriableError)

expect(sendRequest).toHaveBeenCalledTimes(1)
clearTimeout(request.timeoutId)
})

it('executes the timeoutHandler when it times out', async () => {
jest.spyOn(request, 'rejected')
request.enforceRequestTimeout = true
request.send()
// it('executes the timeoutHandler when it times out', async () => {
// jest.spyOn(request, 'rejected')
// request.enforceRequestTimeout = true
// request.send()

await sleep(requestTimeout + 1)
expect(request.rejected).toHaveBeenCalled()
expect(request.entry.reject).toHaveBeenCalledWith(expect.any(KafkaJSRequestTimeoutError))
expect(timeoutHandler).toHaveBeenCalled()
})
// await sleep(requestTimeout + 1)
// expect(request.rejected).toHaveBeenCalled()
// expect(request.entry.reject).toHaveBeenCalledWith(expect.any(KafkaJSRequestTimeoutError))
// expect(timeoutHandler).toHaveBeenCalled()
// })
})

describe('#completed', () => {
Expand Down Expand Up @@ -163,31 +157,31 @@ describe('Network > SocketRequest', () => {
})
})

it('emits NETWORK_REQUEST_TIMEOUT', async () => {
Nevon marked this conversation as resolved.
Show resolved Hide resolved
jest.spyOn(request, 'rejected')
emitter.addListener(events.NETWORK_REQUEST_TIMEOUT, eventCalled)
request.enforceRequestTimeout = true
request.send()

await sleep(requestTimeout + 1)

expect(timeoutHandler).toHaveBeenCalled()
expect(eventCalled).toHaveBeenCalledWith({
id: expect.any(Number),
type: 'network.request_timeout',
timestamp: expect.any(Number),
payload: {
apiKey: 0,
apiName: 'Produce',
apiVersion: 4,
broker: 'localhost:9092',
clientId: 'KafkaJS',
correlationId: expect.any(Number),
createdAt: expect.any(Number),
pendingDuration: expect.any(Number),
sentAt: expect.any(Number),
},
})
})
// it('emits NETWORK_REQUEST_TIMEOUT', async () => {
// jest.spyOn(request, 'rejected')
// emitter.addListener(events.NETWORK_REQUEST_TIMEOUT, eventCalled)
// request.enforceRequestTimeout = true
// request.send()

// await sleep(requestTimeout + 1)

// expect(timeoutHandler).toHaveBeenCalled()
// expect(eventCalled).toHaveBeenCalledWith({
// id: expect.any(Number),
// type: 'network.request_timeout',
// timestamp: expect.any(Number),
// payload: {
// apiKey: 0,
// apiName: 'Produce',
// apiVersion: 4,
// broker: 'localhost:9092',
// clientId: 'KafkaJS',
// correlationId: expect.any(Number),
// createdAt: expect.any(Number),
// pendingDuration: expect.any(Number),
// sentAt: expect.any(Number),
// },
// })
// })
})
})
1 change: 0 additions & 1 deletion src/producer/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ describe('Producer', () => {
const emitter = new InstrumentationEventEmitter()
const cluster = createCluster({
requestTimeout: 1,
enforceRequestTimeout: true,
instrumentationEmitter: emitter,
})

Expand Down