Skip to content

Commit

Permalink
feat: support kafka source rewindable (#2401)
Browse files Browse the repository at this point in the history
Signed-off-by: yisaer <[email protected]>
  • Loading branch information
Yisaer committed Nov 16, 2023
1 parent fcbc4e8 commit f94d404
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 33 deletions.
5 changes: 0 additions & 5 deletions docs/en_US/guide/sources/plugin/kafka.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ default:
groupID: ""
partition: 0
maxBytes: 1000000
offset: 0
```
### Global configurations
Expand All @@ -44,7 +43,3 @@ The partition specified when eKuiper consumes kafka messages
### maxBytes

The maximum number of bytes that a single Kafka message batch can carry, the default is 1MB

### offset

The offset specified when eKuiper starts consuming messages from kafka, -1 represents lastOffset, and -2 represents firstOffset.
5 changes: 0 additions & 5 deletions docs/zh_CN/guide/sources/plugin/kafka.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ default:
groupID: ""
partition: 0
maxBytes: 1000000
offset: 0
```
### 全局配置
Expand All @@ -44,7 +43,3 @@ eKuiper 消费 kafka 消息时所指定的 partition
### maxBytes

单个 kafka 消息批次最大所能携带的 bytes 数,默认为 1MB

### offset

eKuiper 启动向 kafka 进行消息消费时所指定的 offset, -1 代表 lastOffset,-2 代表 firstOffset。
1 change: 0 additions & 1 deletion etc/sources/kafka.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ default:
groupID: ""
partition: 0
maxBytes: 1000000
offset: 0
36 changes: 29 additions & 7 deletions extensions/sources/kafka/ext/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

type KafkaSource struct {
reader *kafkago.Reader
offset int64
}

type kafkaSourceConf struct {
Expand All @@ -38,7 +39,6 @@ type kafkaSourceConf struct {
Partition int `json:"partition"`
MaxAttempts int `json:"maxAttempts"`
MaxBytes int `json:"maxBytes"`
Offset int64 `json:"offset"`
}

func (c *kafkaSourceConf) validate() error {
Expand Down Expand Up @@ -118,13 +118,10 @@ func (s *KafkaSource) Configure(topic string, props map[string]interface{}) erro
SASLMechanism: mechanism,
}
reader := kafkago.NewReader(readerConfig)
if kConf.Offset != 0 {
if err := reader.SetOffset(kConf.Offset); err != nil {
conf.Log.Errorf("kafka offset error: %v", err)
return fmt.Errorf("set kafka offset failed, err:%v", err)
}
}
s.reader = reader
if err := s.reader.SetOffset(kafkago.LastOffset); err != nil {
return err
}
conf.Log.Infof("kafka source got configured.")
return nil
}
Expand All @@ -144,6 +141,7 @@ func (s *KafkaSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTupl
errCh <- err
return
}
s.offset = msg.Offset
dataList, err := ctx.DecodeIntoList(msg.Value)
if err != nil {
logger.Errorf("unmarshal kafka message value err: %v", err)
Expand All @@ -160,3 +158,27 @@ func (s *KafkaSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTupl
func (s *KafkaSource) Close(_ api.StreamContext) error {
return nil
}

func (s *KafkaSource) Rewind(offset interface{}) error {
conf.Log.Infof("set kafka source offset: %v", offset)
offsetV := s.offset //nolint:staticcheck
switch v := offset.(type) {
case int64:
offsetV = v
case int:
offsetV = int64(v)
case float64:
offsetV = int64(v)
default:
return fmt.Errorf("%v can't be set as offset", offset)
}
if err := s.reader.SetOffset(offsetV); err != nil {
conf.Log.Errorf("kafka offset error: %v", err)
return fmt.Errorf("set kafka offset failed, err:%v", err)
}
return nil
}

func (s *KafkaSource) GetOffset() (interface{}, error) {
return s.offset, nil
}
15 changes: 0 additions & 15 deletions extensions/sources/kafka/kafka.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,6 @@
"zh_CN": "建立连接的尝试次数"
}
},
{
"name": "offset",
"default": "0",
"optional": true,
"control": "text",
"type": "int",
"hint": {
"en_US": "the offset from which the next batch of messages will be read. -1 indicates latest offset and -2 indicates first offset.",
"zh_CN": "kafka consumer 的初始 offset, -1 默认为最新 offset, -2 默认为最旧 offset"
},
"label": {
"en_US": "offset",
"zh_CN": "offset"
}
},
{
"name": "saslAuthType",
"default": "none",
Expand Down

0 comments on commit f94d404

Please sign in to comment.