|
| 1 | +import { ResponseError } from '../../errors.ts' |
| 2 | +import { type NullableString } from '../../protocol/definitions.ts' |
| 3 | +import { type Reader } from '../../protocol/reader.ts' |
| 4 | +import { Writer } from '../../protocol/writer.ts' |
| 5 | +import { createAPI, type ResponseErrorWithLocation } from '../definitions.ts' |
| 6 | + |
| 7 | +export interface AlterPartitionReassignmentsRequestPartition { |
| 8 | + partitionIndex: number |
| 9 | + replicas: number[] |
| 10 | +} |
| 11 | + |
| 12 | +export interface AlterPartitionReassignmentsRequestTopic { |
| 13 | + name: string |
| 14 | + partitions: AlterPartitionReassignmentsRequestPartition[] |
| 15 | +} |
| 16 | + |
| 17 | +export type AlterPartitionReassignmentsRequest = Parameters<typeof createRequest> |
| 18 | + |
| 19 | +export interface AlterPartitionReassignmentsResponsePartition { |
| 20 | + partitionIndex: number |
| 21 | + errorCode: number |
| 22 | + errorMessage: NullableString |
| 23 | +} |
| 24 | + |
| 25 | +export interface AlterPartitionReassignmentsResponseResponse { |
| 26 | + name: string |
| 27 | + partitions: AlterPartitionReassignmentsResponsePartition[] |
| 28 | +} |
| 29 | + |
| 30 | +export interface AlterPartitionReassignmentsResponse { |
| 31 | + throttleTimeMs: number |
| 32 | + errorCode: number |
| 33 | + errorMessage: NullableString |
| 34 | + responses: AlterPartitionReassignmentsResponseResponse[] |
| 35 | +} |
| 36 | + |
| 37 | +/* |
| 38 | + AlterPartitionReassignments Request (Version: 1) => timeout_ms allow_replication_factor_change [topics] TAG_BUFFER |
| 39 | + timeout_ms => INT32 |
| 40 | + allow_replication_factor_change => BOOLEAN |
| 41 | + topics => name [partitions] TAG_BUFFER |
| 42 | + name => COMPACT_STRING |
| 43 | + partitions => partition_index [replicas] TAG_BUFFER |
| 44 | + partition_index => INT32 |
| 45 | + replicas => INT32 |
| 46 | +*/ |
| 47 | +export function createRequest ( |
| 48 | + timeoutMs: number, |
| 49 | + allowReplicationFactorChange: boolean, |
| 50 | + topics: AlterPartitionReassignmentsRequestTopic[] |
| 51 | +): Writer { |
| 52 | + return Writer.create() |
| 53 | + .appendInt32(timeoutMs) |
| 54 | + .appendBoolean(allowReplicationFactorChange) |
| 55 | + .appendArray(topics, (w, t) => { |
| 56 | + w.appendString(t.name).appendArray(t.partitions, (w, p) => { |
| 57 | + w.appendInt32(p.partitionIndex).appendArray(p.replicas, (w, r) => w.appendInt32(r), true, false) |
| 58 | + }) |
| 59 | + }) |
| 60 | + .appendTaggedFields() |
| 61 | +} |
| 62 | + |
| 63 | +/* |
| 64 | + AlterPartitionReassignments Response (Version: 1) => throttle_time_ms error_code error_message [responses] TAG_BUFFER |
| 65 | + throttle_time_ms => INT32 |
| 66 | + error_code => INT16 |
| 67 | + error_message => COMPACT_NULLABLE_STRING |
| 68 | + responses => name [partitions] TAG_BUFFER |
| 69 | + name => COMPACT_STRING |
| 70 | + partitions => partition_index error_code error_message TAG_BUFFER |
| 71 | + partition_index => INT32 |
| 72 | + error_code => INT16 |
| 73 | + error_message => COMPACT_NULLABLE_STRING |
| 74 | +*/ |
| 75 | +export function parseResponse ( |
| 76 | + _correlationId: number, |
| 77 | + apiKey: number, |
| 78 | + apiVersion: number, |
| 79 | + reader: Reader |
| 80 | +): AlterPartitionReassignmentsResponse { |
| 81 | + const errors: ResponseErrorWithLocation[] = [] |
| 82 | + |
| 83 | + const throttleTimeMs = reader.readInt32() |
| 84 | + const errorCode = reader.readInt16() |
| 85 | + const errorMessage = reader.readNullableString() |
| 86 | + |
| 87 | + if (errorCode !== 0) { |
| 88 | + errors.push(['', [errorCode, errorMessage]]) |
| 89 | + } |
| 90 | + |
| 91 | + const response: AlterPartitionReassignmentsResponse = { |
| 92 | + throttleTimeMs, |
| 93 | + errorCode, |
| 94 | + errorMessage, |
| 95 | + responses: reader.readArray((r, i) => { |
| 96 | + return { |
| 97 | + name: r.readString(), |
| 98 | + partitions: r.readArray((r, j) => { |
| 99 | + const partition = { |
| 100 | + partitionIndex: r.readInt32(), |
| 101 | + errorCode: r.readInt16(), |
| 102 | + errorMessage: r.readNullableString() |
| 103 | + } |
| 104 | + |
| 105 | + if (partition.errorCode !== 0) { |
| 106 | + errors.push([`responses/${i}/partitions/${j}`, [partition.errorCode, partition.errorMessage]]) |
| 107 | + } |
| 108 | + |
| 109 | + return partition |
| 110 | + }) |
| 111 | + } |
| 112 | + }) |
| 113 | + } |
| 114 | + |
| 115 | + if (errors.length) { |
| 116 | + throw new ResponseError(apiKey, apiVersion, Object.fromEntries(errors), response) |
| 117 | + } |
| 118 | + |
| 119 | + return response |
| 120 | +} |
| 121 | + |
| 122 | +export const api = createAPI<AlterPartitionReassignmentsRequest, AlterPartitionReassignmentsResponse>( |
| 123 | + 45, |
| 124 | + 1, |
| 125 | + createRequest, |
| 126 | + parseResponse |
| 127 | +) |
0 commit comments