-
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d8cf0a
commit c630499
Showing
3 changed files
with
219 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package protocol | ||
|
||
import "fmt" | ||
|
||
type OffsetsPartition struct { | ||
Partition int32 | ||
Timestamp int64 // -1 to receive latest offset, -2 to receive earliest offset | ||
} | ||
|
||
type OffsetsTopic struct { | ||
Topic string | ||
Partitions []*OffsetsPartition | ||
} | ||
|
||
type OffsetsRequest struct { | ||
ReplicaID int32 | ||
Topics []*OffsetsTopic | ||
MaxNumOffsets int32 | ||
} | ||
|
||
func (r *OffsetsRequest) Encode(e PacketEncoder) error { | ||
var err error | ||
e.PutInt32(-1) | ||
err = e.PutArrayLength(len(r.Topics)) | ||
if err != nil { | ||
return err | ||
} | ||
for _, t := range r.Topics { | ||
err = e.PutString(t.Topic) | ||
if err != nil { | ||
return err | ||
} | ||
err = e.PutArrayLength(len(t.Partitions)) | ||
if err != nil { | ||
return err | ||
} | ||
for _, p := range t.Partitions { | ||
e.PutInt32(p.Partition) | ||
e.PutInt64(p.Timestamp) | ||
} | ||
} | ||
e.PutInt32(r.MaxNumOffsets) | ||
return nil | ||
} | ||
|
||
func (r *OffsetsRequest) Decode(d PacketDecoder) error { | ||
var err error | ||
r.ReplicaID, err = d.Int32() | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("hey hey") | ||
bd := d.(*ByteDecoder) | ||
fmt.Printf("bytes: %v, offset: %d, curr: %v\n", bd.b, bd.off, bd.b[bd.off:]) | ||
topicCount, err := d.ArrayLength() | ||
if err != nil { | ||
fmt.Println("errror!", err) | ||
return err | ||
} | ||
fmt.Println("topic count:", topicCount) | ||
r.Topics = make([]*OffsetsTopic, topicCount) | ||
for i := range r.Topics { | ||
ot := new(OffsetsTopic) | ||
ot.Topic, err = d.String() | ||
if err != nil { | ||
return err | ||
} | ||
partitionCount, err := d.ArrayLength() | ||
if err != nil { | ||
return err | ||
} | ||
ot.Partitions = make([]*OffsetsPartition, partitionCount) | ||
for j := range ot.Partitions { | ||
p := new(OffsetsPartition) | ||
p.Partition, err = d.Int32() | ||
if err != nil { | ||
return err | ||
} | ||
p.Timestamp, err = d.Int64() | ||
if err != nil { | ||
return err | ||
} | ||
ot.Partitions[j] = p | ||
} | ||
r.Topics[i] = ot | ||
} | ||
r.MaxNumOffsets, err = d.Int32() | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package protocol | ||
|
||
type PartitionResponse struct { | ||
Partition int32 | ||
ErrorCode int16 | ||
// Timestamp int64 | ||
Offsets []int64 | ||
} | ||
|
||
type OffsetResponse struct { | ||
Topic string | ||
PartitionResponses []*PartitionResponse | ||
} | ||
|
||
type OffsetsResponse struct { | ||
Responses []*OffsetResponse | ||
} | ||
|
||
func (r *OffsetsResponse) Encode(e PacketEncoder) error { | ||
e.PutArrayLength(len(r.Responses)) | ||
for _, r := range r.Responses { | ||
e.PutString(r.Topic) | ||
e.PutArrayLength(len(r.PartitionResponses)) | ||
for _, p := range r.PartitionResponses { | ||
e.PutInt32(p.Partition) | ||
e.PutInt16(p.ErrorCode) | ||
// e.PutInt64(p.Timestamp) | ||
e.PutInt64Array(p.Offsets) | ||
// e.PutInt64(p.Offset) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (r *OffsetsResponse) Decode(d PacketDecoder) error { | ||
var err error | ||
l, err := d.ArrayLength() | ||
if err != nil { | ||
return err | ||
} | ||
r.Responses = make([]*OffsetResponse, l) | ||
for i := range r.Responses { | ||
resp := new(OffsetResponse) | ||
r.Responses[i] = resp | ||
resp.Topic, err = d.String() | ||
if err != nil { | ||
return err | ||
} | ||
pl, err := d.ArrayLength() | ||
if err != nil { | ||
return err | ||
} | ||
ps := make([]*PartitionResponse, pl) | ||
for j := range ps { | ||
p := new(PartitionResponse) | ||
p.Partition, err = d.Int32() | ||
if err != nil { | ||
return err | ||
} | ||
p.ErrorCode, err = d.Int16() | ||
if err != nil { | ||
return err | ||
} | ||
// v1: | ||
// p.Timestamp, err = d.Int64() | ||
// if err != nil { | ||
// return err | ||
// } | ||
p.Offsets, err = d.Int64Array() | ||
// v1: | ||
// p.Offset, err = d.Int64() | ||
if err != nil { | ||
return err | ||
} | ||
ps[j] = p | ||
} | ||
resp.PartitionResponses = ps | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (r *OffsetsResponse) Version() int16 { | ||
return 0 | ||
} | ||
|
||
func (r *OffsetResponse) Key() int16 { | ||
return 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters