-
Notifications
You must be signed in to change notification settings - Fork 847
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Redpanda Migrator offset metadata
- New `redpanda_migrator_offsets` input. - Field `kafka_offset_metadata` added to the `redpanda_migrator_offsets` output. Signed-off-by: Mihai Todor <todormihai@gmail.com>
- Loading branch information
1 parent
4461a69
commit 34421d0
Showing
8 changed files
with
221 additions
and
41 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
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
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
148 changes: 148 additions & 0 deletions
148
internal/impl/kafka/enterprise/redpanda_migrator_offsets_input.go
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,148 @@ | ||
// Copyright 2024 Redpanda Data, Inc. | ||
// | ||
// Licensed as a Redpanda Enterprise file under the Redpanda Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/redpanda-data/connect/blob/main/licenses/rcl.md | ||
|
||
package enterprise | ||
|
||
import ( | ||
"slices" | ||
|
||
"github.com/twmb/franz-go/pkg/kgo" | ||
"github.com/twmb/franz-go/pkg/kmsg" | ||
|
||
"github.com/redpanda-data/benthos/v4/public/service" | ||
"github.com/redpanda-data/connect/v4/internal/impl/kafka" | ||
) | ||
|
||
const ( | ||
// Consumer fields | ||
rmoiFieldRackID = "rack_id" | ||
rmoiFieldFetchMaxBytes = "fetch_max_bytes" | ||
rmoiFieldFetchMinBytes = "fetch_min_bytes" | ||
rmoiFieldFetchMaxPartitionBytes = "fetch_max_partition_bytes" | ||
) | ||
|
||
func redpandaInputConfig() *service.ConfigSpec { | ||
return service.NewConfigSpec(). | ||
Beta(). | ||
Categories("Services"). | ||
Summary(`Redpanda Migrator consumer group offsets output using the https://github.com/twmb/franz-go[Franz Kafka client library^].`). | ||
Description(` | ||
TODO: Description | ||
== Metadata | ||
This input adds the following metadata fields to each message: | ||
` + "```text" + ` | ||
- kafka_key | ||
- kafka_topic | ||
- kafka_partition | ||
- kafka_offset | ||
- kafka_timestamp_unix | ||
- kafka_timestamp_ms | ||
- kafka_tombstone_message | ||
- kafka_offset_metadata | ||
` + "```" + ` | ||
`). | ||
Fields(redpandaInputConfigFields()...) | ||
} | ||
|
||
func redpandaInputConfigFields() []*service.ConfigField { | ||
return slices.Concat( | ||
kafka.FranzConnectionFields(), | ||
[]*service.ConfigField{ | ||
service.NewStringField(rmoiFieldRackID). | ||
Description("A rack specifies where the client is physically located and changes fetch requests to consume from the closest replica as opposed to the leader replica."). | ||
Default(""). | ||
Advanced(), | ||
service.NewStringField(rmoiFieldFetchMaxBytes). | ||
Description("Sets the maximum amount of bytes a broker will try to send during a fetch. Note that brokers may not obey this limit if it has records larger than this limit. This is the equivalent to the Java fetch.max.bytes setting."). | ||
Advanced(). | ||
Default("50MiB"), | ||
service.NewStringField(rmoiFieldFetchMinBytes). | ||
Description("Sets the minimum amount of bytes a broker will try to send during a fetch. This is the equivalent to the Java fetch.min.bytes setting."). | ||
Advanced(). | ||
Default("1B"), | ||
service.NewStringField(rmoiFieldFetchMaxPartitionBytes). | ||
Description("Sets the maximum amount of bytes that will be consumed for a single partition in a fetch request. Note that if a single batch is larger than this number, that batch will still be returned so the client can make progress. This is the equivalent to the Java fetch.max.partition.bytes setting."). | ||
Advanced(). | ||
Default("1MiB"), | ||
}, | ||
kafka.FranzReaderOrderedConfigFields(), | ||
[]*service.ConfigField{ | ||
service.NewAutoRetryNacksToggleField(), | ||
}, | ||
) | ||
} | ||
|
||
func init() { | ||
err := service.RegisterBatchInput("redpanda_migrator_offsets", redpandaInputConfig(), | ||
func(conf *service.ParsedConfig, mgr *service.Resources) (service.BatchInput, error) { | ||
tmpOpts, err := kafka.FranzConnectionOptsFromConfig(conf, mgr.Logger()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
clientOpts := append([]kgo.Opt{}, tmpOpts...) | ||
|
||
d := kafka.FranzConsumerDetails{} | ||
|
||
if d.RackID, err = conf.FieldString(rmoiFieldRackID); err != nil { | ||
return nil, err | ||
} | ||
|
||
d.Topics = []string{`__consumer_offsets`} | ||
|
||
if d.FetchMaxBytes, err = kafka.BytesFromStrFieldAsInt32(rmoiFieldFetchMaxBytes, conf); err != nil { | ||
return nil, err | ||
} | ||
if d.FetchMinBytes, err = kafka.BytesFromStrFieldAsInt32(rmoiFieldFetchMinBytes, conf); err != nil { | ||
return nil, err | ||
} | ||
if d.FetchMaxPartitionBytes, err = kafka.BytesFromStrFieldAsInt32(rmoiFieldFetchMaxPartitionBytes, conf); err != nil { | ||
return nil, err | ||
} | ||
|
||
clientOpts = append(clientOpts, d.FranzOpts()...) | ||
|
||
rdr, err := kafka.NewFranzReaderOrderedFromConfig(conf, mgr, func() ([]kgo.Opt, error) { | ||
return clientOpts, nil | ||
}, func(record *kgo.Record) *service.Message { | ||
key := kmsg.NewOffsetCommitKey() | ||
// Check the version to ensure that we process only offset commit keys | ||
if err := key.ReadFrom(record.Key); err != nil || (key.Version != 0 && key.Version != 1) { | ||
return nil | ||
} | ||
|
||
offsetCommitValue := kmsg.NewOffsetCommitValue() | ||
if err := offsetCommitValue.ReadFrom(record.Value); err != nil { | ||
// Omit records we can't decode | ||
return nil | ||
} | ||
|
||
msg := service.NewMessage(record.Value) | ||
msg.MetaSetMut("kafka_key", record.Key) | ||
msg.MetaSetMut("kafka_topic", record.Topic) | ||
msg.MetaSetMut("kafka_partition", int(record.Partition)) | ||
msg.MetaSetMut("kafka_offset", int(record.Offset)) | ||
msg.MetaSetMut("kafka_timestamp_unix", record.Timestamp.Unix()) | ||
msg.MetaSetMut("kafka_timestamp_ms", record.Timestamp.UnixMilli()) | ||
msg.MetaSetMut("kafka_tombstone_message", record.Value == nil) | ||
msg.MetaSetMut("kafka_offset_metadata", offsetCommitValue.Metadata) | ||
|
||
return msg | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return service.AutoRetryNacksBatchedToggled(conf, rdr) | ||
}) | ||
if err != nil { | ||
panic(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
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
Oops, something went wrong.