blob: 161233fc3578df957c75c2d31b5f52115e191547 [file] [log] [blame]
khenaidood948f772021-08-11 17:49:24 -04001package sarama
2
3type topicPartitionAssignment struct {
4 Topic string
5 Partition int32
6}
7
8type StickyAssignorUserData interface {
9 partitions() []topicPartitionAssignment
10 hasGeneration() bool
11 generation() int
12}
13
14// StickyAssignorUserDataV0 holds topic partition information for an assignment
15type StickyAssignorUserDataV0 struct {
16 Topics map[string][]int32
17
18 topicPartitions []topicPartitionAssignment
19}
20
21func (m *StickyAssignorUserDataV0) encode(pe packetEncoder) error {
22 if err := pe.putArrayLength(len(m.Topics)); err != nil {
23 return err
24 }
25
26 for topic, partitions := range m.Topics {
27 if err := pe.putString(topic); err != nil {
28 return err
29 }
30 if err := pe.putInt32Array(partitions); err != nil {
31 return err
32 }
33 }
34 return nil
35}
36
37func (m *StickyAssignorUserDataV0) decode(pd packetDecoder) (err error) {
38 var topicLen int
39 if topicLen, err = pd.getArrayLength(); err != nil {
40 return
41 }
42
43 m.Topics = make(map[string][]int32, topicLen)
44 for i := 0; i < topicLen; i++ {
45 var topic string
46 if topic, err = pd.getString(); err != nil {
47 return
48 }
49 if m.Topics[topic], err = pd.getInt32Array(); err != nil {
50 return
51 }
52 }
53 m.topicPartitions = populateTopicPartitions(m.Topics)
54 return nil
55}
56
57func (m *StickyAssignorUserDataV0) partitions() []topicPartitionAssignment { return m.topicPartitions }
58func (m *StickyAssignorUserDataV0) hasGeneration() bool { return false }
59func (m *StickyAssignorUserDataV0) generation() int { return defaultGeneration }
60
61// StickyAssignorUserDataV1 holds topic partition information for an assignment
62type StickyAssignorUserDataV1 struct {
63 Topics map[string][]int32
64 Generation int32
65
66 topicPartitions []topicPartitionAssignment
67}
68
69func (m *StickyAssignorUserDataV1) encode(pe packetEncoder) error {
70 if err := pe.putArrayLength(len(m.Topics)); err != nil {
71 return err
72 }
73
74 for topic, partitions := range m.Topics {
75 if err := pe.putString(topic); err != nil {
76 return err
77 }
78 if err := pe.putInt32Array(partitions); err != nil {
79 return err
80 }
81 }
82
83 pe.putInt32(m.Generation)
84 return nil
85}
86
87func (m *StickyAssignorUserDataV1) decode(pd packetDecoder) (err error) {
88 var topicLen int
89 if topicLen, err = pd.getArrayLength(); err != nil {
90 return
91 }
92
93 m.Topics = make(map[string][]int32, topicLen)
94 for i := 0; i < topicLen; i++ {
95 var topic string
96 if topic, err = pd.getString(); err != nil {
97 return
98 }
99 if m.Topics[topic], err = pd.getInt32Array(); err != nil {
100 return
101 }
102 }
103
104 m.Generation, err = pd.getInt32()
105 if err != nil {
106 return err
107 }
108 m.topicPartitions = populateTopicPartitions(m.Topics)
109 return nil
110}
111
112func (m *StickyAssignorUserDataV1) partitions() []topicPartitionAssignment { return m.topicPartitions }
113func (m *StickyAssignorUserDataV1) hasGeneration() bool { return true }
114func (m *StickyAssignorUserDataV1) generation() int { return int(m.Generation) }
115
116func populateTopicPartitions(topics map[string][]int32) []topicPartitionAssignment {
117 topicPartitions := make([]topicPartitionAssignment, 0)
118 for topic, partitions := range topics {
119 for _, partition := range partitions {
120 topicPartitions = append(topicPartitions, topicPartitionAssignment{Topic: topic, Partition: partition})
121 }
122 }
123 return topicPartitions
124}