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

Added listGroup method to admin interface #645

Merged
merged 6 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions docs/Admin.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,31 @@ Example response:
throttleTime: 0,
}
```

## <a name="list-groups"></a> List groups

List groups available on the broker.

```javascript
await admin.listGroups()
```

Example:

```javascript
const { ResourceTypes } = require('kafkajs')

await admin.listGroups()
```

Example response:

```javascript
{
errorCode: 0,
throttleTime: 0,
groups: [
{groupId: 'testgroup', protocolType: 'consumer'}
]
}
```
4 changes: 3 additions & 1 deletion src/admin/__tests__/deleteTopics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ describe('Admin', () => {
)

expect(broker.deleteTopics).toHaveBeenCalledTimes(1)
expect(loggerInstance.error).toHaveBeenCalledWith(
expect(
dfilkovi marked this conversation as resolved.
Show resolved Hide resolved
loggerInstance.error
).toHaveBeenCalledWith(
'Could not delete topics, check if "delete.topic.enable" is set to "true" (the default value is "false") or increase the timeout',
{ error: 'The request timed out', retryCount: 0, retryTime: expect.any(Number) }
)
Expand Down
78 changes: 78 additions & 0 deletions src/admin/__tests__/listGroups.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const createAdmin = require('../index')
const createProducer = require('../../producer')
const createConsumer = require('../../consumer')

const {
createCluster,
newLogger,
createTopic,
createModPartitioner,
waitForMessages,
secureRandom,
waitForConsumerToJoinGroup,
} = require('testHelpers')

describe('Admin', () => {
describe('listGroups', () => {
let admin, topicName, groupId, cluster, consumer, producer

test('list groups', async () => {
topicName = `test-topic-${secureRandom()}`
groupId = `consumer-group-id-${secureRandom()}`

await createTopic({ topic: topicName })

cluster = createCluster()
producer = createProducer({
dfilkovi marked this conversation as resolved.
Show resolved Hide resolved
cluster,
createPartitioner: createModPartitioner,
logger: newLogger(),
})

consumer = createConsumer({
cluster,
groupId,
maxWaitTimeInMs: 100,
logger: newLogger(),
})

admin = createAdmin({ cluster: cluster, logger: newLogger() })

await admin.connect()

jest.spyOn(cluster, 'refreshMetadataIfNecessary')
dfilkovi marked this conversation as resolved.
Show resolved Hide resolved

await consumer.connect()
await producer.connect()
await consumer.subscribe({ topic: topicName, fromBeginning: true })

const messagesConsumed = []
dfilkovi marked this conversation as resolved.
Show resolved Hide resolved
consumer.run({ eachMessage: async event => messagesConsumed.push(event) })
await waitForConsumerToJoinGroup(consumer)

const messages = Array(1)
.fill()
.map(() => {
const value = secureRandom()
return { key: `key-${value}`, value: `value-${value}` }
})

await producer.send({ acks: 1, topic: topicName, messages })
await waitForMessages(messagesConsumed, { number: messages.length })

const listGroupResponse = await admin.listGroups()

let foundGroupId = ''

for (const group of listGroupResponse.groups) {
if (group.groupId === groupId) foundGroupId = groupId
}

expect(foundGroupId).toEqual(groupId)
dfilkovi marked this conversation as resolved.
Show resolved Hide resolved

await admin.disconnect()
dfilkovi marked this conversation as resolved.
Show resolved Hide resolved
await consumer.disconnect()
await producer.disconnect()
})
})
})
34 changes: 34 additions & 0 deletions src/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,39 @@ module.exports = ({
}
}

/**
* List groups in a broker
*
* @return {Promise<ListGroups>}
*
* @typedef {Object} ListGroups
* @property {number} errorCode
* @property {Array<ListGroup>} groups
*
* @typedef {Object} ListGroup
* @property {string} groupId
* @property {string} protocolType
*/
const listGroups = async () => {
const retrier = createRetry(retry)

return retrier(async (bail, retryCount, retryTime) => {
try {
await cluster.refreshMetadata()
const broker = await cluster.findControllerBroker()
const response = await broker.listGroups()
return response
} catch (e) {
if (e.type === 'NOT_CONTROLLER') {
dfilkovi marked this conversation as resolved.
Show resolved Hide resolved
logger.warn('Could not list groups', { error: e.message, retryCount, retryTime })
throw e
}

bail(e)
}
})
}

/**
* @param {string} eventName
* @param {Function} listener
Expand Down Expand Up @@ -637,5 +670,6 @@ module.exports = ({
alterConfigs,
on,
logger: getLogger,
listGroups,
}
}
5 changes: 4 additions & 1 deletion src/broker/__tests__/addPartitionsToTxn.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ describe('Broker > AddPartitionsToTxn', () => {
errors: [
{
topic: topicName,
partitionErrors: [{ errorCode: 0, partition: 1 }, { errorCode: 0, partition: 2 }],
partitionErrors: [
{ errorCode: 0, partition: 1 },
{ errorCode: 0, partition: 2 },
],
},
],
})
Expand Down
7 changes: 4 additions & 3 deletions src/broker/__tests__/deleteTopics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ describe('Broker > deleteTopics', () => {

expect(response).toEqual({
throttleTime: 0,
topicErrors: [{ topic: topicName1, errorCode: 0 }, { topic: topicName2, errorCode: 0 }].sort(
topicNameComparator
),
topicErrors: [
{ topic: topicName1, errorCode: 0 },
{ topic: topicName2, errorCode: 0 },
].sort(topicNameComparator),
})
})
})
15 changes: 12 additions & 3 deletions src/broker/__tests__/txnOffsetCommit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ describe('Broker > TxnOffsetCommit', () => {
topics: [
{
topic: topicName,
partitions: [{ partition: 0, offset: 0 }, { partition: 1, offset: 0 }],
partitions: [
{ partition: 0, offset: 0 },
{ partition: 1, offset: 0 },
],
},
],
})
Expand All @@ -102,7 +105,10 @@ describe('Broker > TxnOffsetCommit', () => {
topics: [
{
topic: topicName,
partitions: [{ errorCode: 0, partition: 0 }, { errorCode: 0, partition: 1 }],
partitions: [
{ errorCode: 0, partition: 0 },
{ errorCode: 0, partition: 1 },
],
},
],
})
Expand All @@ -117,7 +123,10 @@ describe('Broker > TxnOffsetCommit', () => {
topics: [
{
topic: topicName,
partitions: [{ partition: 0, offset: 0 }, { partition: 1, offset: 0 }],
partitions: [
{ partition: 0, offset: 0 },
{ partition: 1, offset: 0 },
],
},
],
})
Expand Down
10 changes: 10 additions & 0 deletions src/broker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,16 @@ module.exports = class Broker {
)
}

/**
* Send request for list of groups
* @public
* @returns {Promise}
*/
async listGroups() {
const listGroups = this.lookupRequest(apiKeys.ListGroups, requests.ListGroups)
return await this.connection.send(listGroups())
}

/***
* @private
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ describe('Consumer > OffsetMananger > initializeConsumerOffsets', () => {
},
{
topic: 'topic-name2',
partitions: [{ partition: 0, offset: '1' }, { partition: 1, offset: '2' }],
partitions: [
{ partition: 0, offset: '1' },
{ partition: 1, offset: '2' },
],
},
])
})
Expand Down
5 changes: 4 additions & 1 deletion src/producer/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,10 @@ describe('Producer', () => {
const topicMessages = [
{
topic: topicName,
messages: [{ key: 'key-1', value: 'value-1' }, { key: 'key-2', value: 'value-2' }],
messages: [
{ key: 'key-1', value: 'value-1' },
{ key: 'key-2', value: 'value-2' },
],
},
{
topic: topicName,
Expand Down
10 changes: 8 additions & 2 deletions src/protocol/requests/addPartitionsToTxn/v0/response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ describe('Protocol > Requests > AddPartitionsToTxn > v0', () => {
errors: [
{
topic: 'test-topic-f6bab978bdca094688e3-37015-ca4f7ad4-5dcc-4bb9-9853-b1e4c4ed78a7',
partitionErrors: [{ errorCode: 0, partition: 1 }, { errorCode: 0, partition: 2 }],
partitionErrors: [
{ errorCode: 0, partition: 1 },
{ errorCode: 0, partition: 2 },
],
},
],
})
Expand All @@ -23,7 +26,10 @@ describe('Protocol > Requests > AddPartitionsToTxn > v0', () => {
errors: [
{
topic: 'test-topic',
partitionErrors: [{ errorCode: 0, partition: 1 }, { errorCode: 49, partition: 2 }],
partitionErrors: [
{ errorCode: 0, partition: 1 },
{ errorCode: 49, partition: 2 },
],
},
],
}
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/requests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const requests = {
LeaveGroup: require('./leaveGroup'),
SyncGroup: require('./syncGroup'),
DescribeGroups: require('./describeGroups'),
ListGroups: {},
ListGroups: require('./listGroups'),
SaslHandshake: require('./saslHandshake'),
ApiVersions: require('./apiVersions'),
CreateTopics: require('./createTopics'),
Expand Down
27 changes: 27 additions & 0 deletions src/protocol/requests/listGroups/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const versions = {
0: () => {
const request = require('./v0/request')
const response = require('./v0/response')
return { request: request(), response }
},
1: () => {
const request = require('./v1/request')
const response = require('./v1/response')
return { request: request(), response }
},
2: () => {
const request = require('./v2/request')
const response = require('./v2/response')
return { request: request(), response }
},
3: () => {
const request = require('./v3/request')
const response = require('./v3/response')
return { request: request(), response }
},
}

module.exports = {
versions: Object.keys(versions),
protocol: ({ version }) => versions[version],
}
17 changes: 17 additions & 0 deletions src/protocol/requests/listGroups/v0/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Encoder = require('../../../encoder')
const { ListGroups: apiKey } = require('../../apiKeys')

/**
* ListGroups Request (Version: 0)
*/

/**
*/
module.exports = () => ({
apiKey,
apiVersion: 0,
apiName: 'ListGroups',
encode: async () => {
return new Encoder()
},
})
41 changes: 41 additions & 0 deletions src/protocol/requests/listGroups/v0/response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const Decoder = require('../../../decoder')
const { failure, createErrorFromCode } = require('../../../error')

/**
* ListGroups Response (Version: 0) => error_code [groups]
* throttle_time_ms => INT32
dfilkovi marked this conversation as resolved.
Show resolved Hide resolved
* error_code => INT16
* groups => group_id protocol_type
* group_id => STRING
* protocol_type => STRING
*/

const decodeGroup = decoder => ({
groupId: decoder.readString(),
protocolType: decoder.readString(),
})

const decode = async rawData => {
const decoder = new Decoder(rawData)
const errorCode = decoder.readInt16()
const groups = decoder.readArray(decodeGroup)

return {
errorCode,
groups,
}
}

const parse = async data => {
if (failure(data.errorCode)) {
throw createErrorFromCode(data.errorCode)
}

return data
}

module.exports = {
decodeGroup,
decode,
parse,
}
Loading