Skip to content

Commit 24f4ee7

Browse files
committed
consumer: support legacy consumer CLI flags
1 parent 83f6d0d commit 24f4ee7

2 files changed

Lines changed: 78 additions & 8 deletions

File tree

‎cmd/consumer/consumer.go‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package main
1616

1717
import (
1818
"context"
19+
"net/url"
1920
"strings"
2021

2122
"github.com/google/uuid"
@@ -38,8 +39,12 @@ type consumer struct {
3839
}
3940

4041
func newConsumer(ctx context.Context, options *options) *consumer {
42+
logLevel := strings.TrimSpace(options.logLevel)
43+
if logLevel == "" {
44+
logLevel = "info"
45+
}
4146
if err := logger.InitLogger(&logger.Config{
42-
Level: "info",
47+
Level: logLevel,
4348
File: options.logFile,
4449
}); err != nil {
4550
log.Panic("consumer logger initialization failed", zap.Error(err))
@@ -48,8 +53,12 @@ func newConsumer(ctx context.Context, options *options) *consumer {
4853
version.LogVersionInfo("consumer")
4954

5055
upstreamURI := parseRequiredURI(options.upstreamURI)
56+
applyLegacyFlags(upstreamURI, options)
5157
sourceType := source.TypeOf(upstreamURI)
5258
consumerID := options.consumerID
59+
if strings.TrimSpace(consumerID) == "" {
60+
consumerID = options.consumerGroupID
61+
}
5362
if strings.TrimSpace(consumerID) == "" {
5463
consumerID = "consumer_" + uuid.NewString()
5564
}
@@ -123,3 +132,19 @@ func (c *consumer) readSource(ctx context.Context) error {
123132
}
124133
}
125134
}
135+
136+
func applyLegacyFlags(upstreamURI *url.URL, opts *options) {
137+
query := upstreamURI.Query()
138+
changed := false
139+
if opts.schemaRegistryURI != "" && query.Get("schema-registry") == "" {
140+
query.Set("schema-registry", opts.schemaRegistryURI)
141+
changed = true
142+
}
143+
if opts.upstreamTiDBDSN != "" && query.Get("upstream-tidb-dsn") == "" {
144+
query.Set("upstream-tidb-dsn", opts.upstreamTiDBDSN)
145+
changed = true
146+
}
147+
if changed {
148+
upstreamURI.RawQuery = query.Encode()
149+
}
150+
}

‎cmd/consumer/main.go‎

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
_ "net/http/pprof" //nolint:gosec // pprof is served on 127.0.0.1 only and gated by --enable-profiling.
2121
"os"
2222
"os/signal"
23+
"path/filepath"
2324
"syscall"
2425
"time"
2526

@@ -32,12 +33,16 @@ import (
3233
const profileAddress = "127.0.0.1:6060"
3334

3435
type options struct {
35-
upstreamURI string
36-
downstreamURI string
37-
configFile string
38-
consumerID string
39-
logFile string
40-
enableProfiling bool
36+
upstreamURI string
37+
downstreamURI string
38+
configFile string
39+
consumerID string
40+
logFile string
41+
logLevel string
42+
consumerGroupID string
43+
schemaRegistryURI string
44+
upstreamTiDBDSN string
45+
enableProfiling bool
4146
}
4247

4348
func main() {
@@ -96,8 +101,48 @@ func newCommand() *cobra.Command {
96101
flags.StringVar(&options.configFile, "config", "", "consumer configuration file")
97102
flags.StringVar(&options.consumerID, "consumer-id", "",
98103
"Kafka group ID or Pulsar subscription name (random if omitted)")
99-
flags.StringVar(&options.logFile, "log-file", "cdc_consumer.log", "log file path")
104+
flags.StringVar(&options.logFile, "log-file", defaultLogFileName(), "log file path")
100105
flags.BoolVar(&options.enableProfiling, "enable-profiling", false, "enable pprof on "+profileAddress)
101106

107+
// Legacy CLI flags kept hidden so copied legacy consumer names work on older branches.
108+
var ignored string
109+
flags.StringVar(&options.logLevel, "log-level", "info", "log level")
110+
flags.StringVar(&options.consumerGroupID, "consumer-group-id", "", "legacy alias for --consumer-id")
111+
flags.StringVar(&options.schemaRegistryURI, "schema-registry-uri", "", "legacy schema registry URI")
112+
flags.StringVar(&options.upstreamTiDBDSN, "upstream-tidb-dsn", "", "legacy upstream TiDB DSN")
113+
legacyStringFlags := []string{
114+
"log-level",
115+
"consumer-group-id",
116+
"schema-registry-uri",
117+
"upstream-tidb-dsn",
118+
"tz",
119+
"ca",
120+
"cert",
121+
"key",
122+
"flush-interval",
123+
"file-index-width",
124+
"oauth2-private-key",
125+
"oauth2-issuer-url",
126+
"oauth2-client-id",
127+
"oauth2-scope",
128+
"oauth2-audience",
129+
"auth-tls-certificate-path",
130+
"auth-tls-private-key-path",
131+
}
132+
for _, name := range legacyStringFlags[4:] {
133+
flags.StringVar(&ignored, name, "", "legacy compatibility flag (ignored)")
134+
}
135+
for _, name := range legacyStringFlags {
136+
_ = flags.MarkHidden(name)
137+
}
138+
102139
return command
103140
}
141+
142+
func defaultLogFileName() string {
143+
base := filepath.Base(os.Args[0])
144+
if base == "" || base == "." || base == string(filepath.Separator) {
145+
return "cdc_consumer.log"
146+
}
147+
return base + ".log"
148+
}

0 commit comments

Comments
 (0)