|
| 1 | +// Copyright 2025 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "flag" |
| 19 | + "fmt" |
| 20 | + "net/http" |
| 21 | + _ "net/http/pprof" |
| 22 | + "os" |
| 23 | + "os/signal" |
| 24 | + "runtime/debug" |
| 25 | + "syscall" |
| 26 | + |
| 27 | + "github.com/google/uuid" |
| 28 | + "github.com/pingcap/log" |
| 29 | + "github.com/pingcap/ticdc/pkg/logger" |
| 30 | + "github.com/pingcap/ticdc/pkg/version" |
| 31 | + "go.uber.org/zap" |
| 32 | + "golang.org/x/sync/errgroup" |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + logPath string |
| 37 | + logLevel string |
| 38 | +) |
| 39 | + |
| 40 | +func main() { |
| 41 | + debug.SetMemoryLimit(14 * 1024 * 1024 * 1024) |
| 42 | + var ( |
| 43 | + upstreamURIStr string |
| 44 | + configFile string |
| 45 | + enableProfiling bool |
| 46 | + ) |
| 47 | + groupID := fmt.Sprintf("ticdc_kafka_iceberg_consumer_%s", uuid.New().String()) |
| 48 | + consumerOption := newOption() |
| 49 | + |
| 50 | + flag.StringVar(&configFile, "config", "", "config file for changefeed") |
| 51 | + flag.StringVar(&upstreamURIStr, "upstream-uri", "", "Kafka uri") |
| 52 | + flag.StringVar(&logPath, "log-file", "cdc_kafka_iceberg_consumer.log", "log file path") |
| 53 | + flag.StringVar(&logLevel, "log-level", "info", "log level") |
| 54 | + flag.BoolVar(&enableProfiling, "enable-profiling", false, "enable pprof profiling") |
| 55 | + |
| 56 | + flag.StringVar(&consumerOption.icebergWarehouse, "iceberg-warehouse", "", "Iceberg warehouse URI (s3://bucket/warehouse)") |
| 57 | + flag.StringVar(&consumerOption.icebergCatalog, "iceberg-catalog", "glue", "Iceberg catalog type (hadoop, glue)") |
| 58 | + flag.StringVar(&consumerOption.icebergNamespace, "iceberg-namespace", "", "Iceberg namespace") |
| 59 | + flag.StringVar(&consumerOption.icebergDatabase, "iceberg-database", "", "Iceberg database name") |
| 60 | + flag.StringVar(&consumerOption.icebergRegion, "iceberg-region", "us-east-1", "AWS region for Glue catalog") |
| 61 | + flag.StringVar(&consumerOption.icebergMode, "iceberg-mode", "append", "Iceberg mode (append, upsert)") |
| 62 | + flag.StringVar(&consumerOption.groupID, "consumer-group-id", groupID, "consumer group id") |
| 63 | + flag.StringVar(&consumerOption.timezone, "tz", "System", "Specify time zone of Kafka consumer") |
| 64 | + flag.StringVar(&consumerOption.ca, "ca", "", "CA certificate path for Kafka SSL connection") |
| 65 | + flag.StringVar(&consumerOption.cert, "cert", "", "Certificate path for Kafka SSL connection") |
| 66 | + flag.StringVar(&consumerOption.key, "key", "", "Private key path for Kafka SSL connection") |
| 67 | + flag.Parse() |
| 68 | + |
| 69 | + err := logger.InitLogger(&logger.Config{ |
| 70 | + Level: logLevel, |
| 71 | + File: logPath, |
| 72 | + }) |
| 73 | + if err != nil { |
| 74 | + log.Panic("init logger failed", zap.Error(err)) |
| 75 | + } |
| 76 | + version.LogVersionInfo("kafka iceberg consumer") |
| 77 | + |
| 78 | + consumerOption.Adjust(upstreamURIStr, configFile) |
| 79 | + |
| 80 | + ctx, cancel := context.WithCancel(context.Background()) |
| 81 | + g, ctx := errgroup.WithContext(ctx) |
| 82 | + |
| 83 | + if enableProfiling { |
| 84 | + g.Go(func() error { |
| 85 | + return http.ListenAndServe(":6060", nil) |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + cons := newConsumer(ctx, consumerOption) |
| 90 | + g.Go(func() error { |
| 91 | + return cons.Run(ctx) |
| 92 | + }) |
| 93 | + |
| 94 | + g.Go(func() error { |
| 95 | + sigterm := make(chan os.Signal, 1) |
| 96 | + signal.Notify(sigterm, syscall.SIGINT, syscall.SIGTERM) |
| 97 | + select { |
| 98 | + case <-ctx.Done(): |
| 99 | + log.Info("terminating: context cancelled") |
| 100 | + case <-sigterm: |
| 101 | + log.Info("terminating: via signal") |
| 102 | + } |
| 103 | + cancel() |
| 104 | + return nil |
| 105 | + }) |
| 106 | + err = g.Wait() |
| 107 | + if err != nil { |
| 108 | + log.Error("kafka iceberg consumer exited with error", zap.Error(err)) |
| 109 | + } else { |
| 110 | + log.Info("kafka iceberg consumer exited") |
| 111 | + } |
| 112 | +} |
0 commit comments