func consume(br *broker.B, cfg *ConsumeConfig) error { req := &broker.Request{ ClientID: clientID, RequestMessage: &broker.FetchRequest{ ReplicaID: -1, MaxWaitTime: int32(time.Second / time.Millisecond), MinBytes: int32(1024), FetchOffsetInTopics: []broker.FetchOffsetInTopic{ { TopicName: cfg.Topic, FetchOffsetInPartitions: []broker.FetchOffsetInPartition{ { Partition: int32(cfg.Partition), FetchOffset: int64(cfg.Offset), MaxBytes: int32(1000), }, }, }, }, }, } resp := broker.FetchResponse{} if err := br.Do(req, &resp); err != nil { return err } fmt.Println(toJSON(resp)) return nil }
func timeOffset(br *broker.B, cfg *TimeConfig) error { req := &broker.Request{ ClientID: clientID, RequestMessage: &broker.OffsetRequest{ ReplicaID: -1, TimeInTopics: []broker.TimeInTopic{ { TopicName: cfg.Topic, TimeInPartitions: []broker.TimeInPartition{ { Partition: int32(cfg.Partition), Time: int64(cfg.Time), MaxNumberOfOffsets: 10, }, }, }, }, }, } resp := broker.OffsetResponse{} if err := br.Do(req, &resp); err != nil { return err } fmt.Println(toJSON(&resp)) return nil }
func offset(br *broker.B, cfg *OffsetConfig) error { req := &broker.Request{ ClientID: clientID, RequestMessage: &broker.OffsetFetchRequestV1{ ConsumerGroup: cfg.GroupName, PartitionInTopics: []broker.PartitionInTopic{ { TopicName: cfg.Topic, Partitions: []int32{int32(cfg.Partition)}, }, }, }, } resp := broker.OffsetFetchResponse{} if err := br.Do(req, &resp); err != nil { return err } fmt.Println(toJSON(&resp)) for i := range resp { t := &resp[i] if t.TopicName == cfg.Topic { for j := range resp[i].OffsetMetadataInPartitions { p := &t.OffsetMetadataInPartitions[j] if p.Partition == int32(cfg.Partition) { if p.ErrorCode.HasError() { return p.ErrorCode } } } } } return nil }
func meta(br *broker.B, cfg *MetaConfig) error { resp, err := br.TopicMetadata(cfg.Topics...) if err != nil { return err } fmt.Println(toJSON(&resp)) return nil }
func coord(br *broker.B, coord *CoordConfig) error { reqMsg := broker.GroupCoordinatorRequest(coord.GroupName) req := &broker.Request{ ClientID: clientID, RequestMessage: &reqMsg, } resp := &broker.GroupCoordinatorResponse{} if err := br.Do(req, resp); err != nil { return err } fmt.Println(toJSON(&resp)) if resp.ErrorCode.HasError() { return resp.ErrorCode } return nil }
func commit(br *broker.B, cfg *CommitConfig) error { req := &broker.Request{ ClientID: clientID, RequestMessage: &broker.OffsetCommitRequestV1{ ConsumerGroupID: cfg.GroupName, OffsetCommitInTopicV1s: []broker.OffsetCommitInTopicV1{ { TopicName: cfg.Topic, OffsetCommitInPartitionV1s: []broker.OffsetCommitInPartitionV1{ { Partition: int32(cfg.Partition), Offset: int64(cfg.Offset), // TimeStamp in milliseconds TimeStamp: time.Now().Add(time.Duration(cfg.Retention)*time.Millisecond).Unix() * 1000, }, }, }, }, }, } resp := broker.OffsetCommitResponse{} if err := br.Do(req, &resp); err != nil { return err } for i := range resp { t := &resp[i] if t.TopicName == cfg.Topic { for j := range t.ErrorInPartitions { p := &t.ErrorInPartitions[j] if int(p.Partition) == cfg.Partition { if p.ErrorCode.HasError() { return p.ErrorCode } } } } } return nil }