-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Consumer groups on Kafka 0.9 #588
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package sarama | ||
|
||
type GroupMemberMetadata struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As for naming, we should probably call this |
||
Version int16 | ||
Topics []string | ||
UserData []byte | ||
} | ||
|
||
func (m *GroupMemberMetadata) encode(pe packetEncoder) error { | ||
pe.putInt16(m.Version) | ||
|
||
if err := pe.putStringArray(m.Topics); err != nil { | ||
return err | ||
} | ||
|
||
if err := pe.putBytes(m.UserData); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *GroupMemberMetadata) decode(pd packetDecoder) (err error) { | ||
if m.Version, err = pd.getInt16(); err != nil { | ||
return | ||
} | ||
|
||
if m.Topics, err = pd.getStringArray(); err != nil { | ||
return | ||
} | ||
|
||
if m.UserData, err = pd.getBytes(); err != nil { | ||
return | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type GroupMemberAssignment struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, We should probably also name the file |
||
Version int16 | ||
Topics []GroupMemberAssignedTopic | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
UserData []byte | ||
} | ||
|
||
type GroupMemberAssignedTopic struct { | ||
Topic string | ||
Partitions []int32 | ||
} | ||
|
||
func (m *GroupMemberAssignment) encode(pe packetEncoder) error { | ||
pe.putInt16(m.Version) | ||
|
||
if err := pe.putArrayLength(len(m.Topics)); err != nil { | ||
return err | ||
} | ||
|
||
for _, topic := range m.Topics { | ||
if err := pe.putString(topic.Topic); err != nil { | ||
return err | ||
} | ||
if err := pe.putInt32Array(topic.Partitions); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if err := pe.putBytes(m.UserData); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *GroupMemberAssignment) decode(pd packetDecoder) (err error) { | ||
if m.Version, err = pd.getInt16(); err != nil { | ||
return | ||
} | ||
|
||
var topicLen int | ||
if topicLen, err = pd.getArrayLength(); err != nil { | ||
return | ||
} | ||
|
||
m.Topics = make([]GroupMemberAssignedTopic, topicLen) | ||
for i := 0; i < topicLen; i++ { | ||
if m.Topics[i].Topic, err = pd.getString(); err != nil { | ||
return | ||
} | ||
if m.Topics[i].Partitions, err = pd.getInt32Array(); err != nil { | ||
return | ||
} | ||
} | ||
|
||
if m.UserData, err = pd.getBytes(); err != nil { | ||
return | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package sarama | ||
|
||
import ( | ||
"bytes" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
var ( | ||
groupMemberMetadata = []byte{ | ||
0, 1, // Version | ||
0, 0, 0, 2, // Topic array length | ||
0, 3, 'o', 'n', 'e', // Topic one | ||
0, 3, 't', 'w', 'o', // Topic two | ||
0, 0, 0, 3, 0x01, 0x02, 0x03, // Userdata | ||
} | ||
groupMemberAssignment = []byte{ | ||
0, 1, // Version | ||
0, 0, 0, 2, // Topic array length | ||
0, 3, 'o', 'n', 'e', // Topic one | ||
0, 0, 0, 3, // Topic one, partition array length | ||
0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 4, // 0, 2, 4 | ||
0, 3, 't', 'w', 'o', // Topic two | ||
0, 0, 0, 2, // Topic two, partition array length | ||
0, 0, 0, 1, 0, 0, 0, 3, // 1, 3 | ||
0, 0, 0, 3, 0x01, 0x02, 0x03, // Userdata | ||
} | ||
) | ||
|
||
func TestGroupMemberMetadata(t *testing.T) { | ||
meta := &GroupMemberMetadata{ | ||
Version: 1, | ||
Topics: []string{"one", "two"}, | ||
UserData: []byte{0x01, 0x02, 0x03}, | ||
} | ||
|
||
buf, err := encode(meta) | ||
if err != nil { | ||
t.Error("Failed to encode data", err) | ||
} else if !bytes.Equal(groupMemberMetadata, buf) { | ||
t.Errorf("Encoded data does not match expectation\nexpected: %v\nactual: %v", groupMemberMetadata, buf) | ||
} | ||
|
||
meta2 := new(GroupMemberMetadata) | ||
err = decode(buf, meta2) | ||
if err != nil { | ||
t.Error("Failed to decode data", err) | ||
} else if !reflect.DeepEqual(meta, meta2) { | ||
t.Errorf("Encoded data does not match expectation\nexpected: %v\nactual: %v", meta, meta2) | ||
} | ||
} | ||
|
||
func TestGroupMemberAssignment(t *testing.T) { | ||
amt := &GroupMemberAssignment{ | ||
Version: 1, | ||
Topics: []GroupMemberAssignedTopic{ | ||
{Topic: "one", Partitions: []int32{0, 2, 4}}, | ||
{Topic: "two", Partitions: []int32{1, 3}}, | ||
}, | ||
UserData: []byte{0x01, 0x02, 0x03}, | ||
} | ||
|
||
buf, err := encode(amt) | ||
if err != nil { | ||
t.Error("Failed to encode data", err) | ||
} else if !bytes.Equal(groupMemberAssignment, buf) { | ||
t.Errorf("Encoded data does not match expectation\nexpected: %v\nactual: %v", groupMemberAssignment, buf) | ||
} | ||
|
||
amt2 := new(GroupMemberAssignment) | ||
err = decode(buf, amt2) | ||
if err != nil { | ||
t.Error("Failed to decode data", err) | ||
} else if !reflect.DeepEqual(amt, amt2) { | ||
t.Errorf("Encoded data does not match expectation\nexpected: %v\nactual: %v", amt, amt2) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could check this in
Config.Validate()