github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/config/large_message.go (about) 1 // Copyright 2023 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 config 15 16 import ( 17 "github.com/pingcap/tiflow/pkg/compression" 18 cerror "github.com/pingcap/tiflow/pkg/errors" 19 ) 20 21 const ( 22 // LargeMessageHandleOptionNone means not handling large message. 23 LargeMessageHandleOptionNone string = "none" 24 // LargeMessageHandleOptionClaimCheck means handling large message by sending to the claim check storage. 25 LargeMessageHandleOptionClaimCheck string = "claim-check" 26 // LargeMessageHandleOptionHandleKeyOnly means handling large message by sending only handle key columns. 27 LargeMessageHandleOptionHandleKeyOnly string = "handle-key-only" 28 ) 29 30 // LargeMessageHandleConfig is the configuration for handling large message. 31 type LargeMessageHandleConfig struct { 32 LargeMessageHandleOption string `toml:"large-message-handle-option" json:"large-message-handle-option"` 33 LargeMessageHandleCompression string `toml:"large-message-handle-compression" json:"large-message-handle-compression"` 34 ClaimCheckStorageURI string `toml:"claim-check-storage-uri" json:"claim-check-storage-uri"` 35 } 36 37 // NewDefaultLargeMessageHandleConfig return the default Config. 38 func NewDefaultLargeMessageHandleConfig() *LargeMessageHandleConfig { 39 return &LargeMessageHandleConfig{ 40 LargeMessageHandleOption: LargeMessageHandleOptionNone, 41 LargeMessageHandleCompression: compression.None, 42 } 43 } 44 45 // AdjustAndValidate the Config. 46 func (c *LargeMessageHandleConfig) AdjustAndValidate(protocol Protocol, enableTiDBExtension bool) error { 47 if c.LargeMessageHandleOption == "" { 48 c.LargeMessageHandleOption = LargeMessageHandleOptionNone 49 } 50 51 if c.LargeMessageHandleCompression == "" { 52 c.LargeMessageHandleCompression = compression.None 53 } 54 55 // compression can be enabled independently 56 if !compression.Supported(c.LargeMessageHandleCompression) { 57 return cerror.ErrInvalidReplicaConfig.GenWithStack( 58 "large message handle compression is not supported, got %s", c.LargeMessageHandleCompression) 59 } 60 if c.LargeMessageHandleOption == LargeMessageHandleOptionNone { 61 return nil 62 } 63 64 switch protocol { 65 case ProtocolOpen, ProtocolSimple: 66 case ProtocolCanalJSON: 67 if !enableTiDBExtension { 68 return cerror.ErrInvalidReplicaConfig.GenWithStack( 69 "large message handle is set to %s, protocol is %s, but enable-tidb-extension is false", 70 c.LargeMessageHandleOption, protocol.String()) 71 } 72 default: 73 return cerror.ErrInvalidReplicaConfig.GenWithStack( 74 "large message handle is set to %s, protocol is %s, it's not supported", 75 c.LargeMessageHandleOption, protocol.String()) 76 } 77 78 if c.LargeMessageHandleOption == LargeMessageHandleOptionClaimCheck { 79 if c.ClaimCheckStorageURI == "" { 80 return cerror.ErrInvalidReplicaConfig.GenWithStack( 81 "large message handle is set to claim-check, but the claim-check-storage-uri is empty") 82 } 83 } 84 85 return nil 86 } 87 88 // HandleKeyOnly returns true if handle large message by encoding handle key only. 89 func (c *LargeMessageHandleConfig) HandleKeyOnly() bool { 90 if c == nil { 91 return false 92 } 93 return c.LargeMessageHandleOption == LargeMessageHandleOptionHandleKeyOnly 94 } 95 96 // EnableClaimCheck returns true if enable claim check. 97 func (c *LargeMessageHandleConfig) EnableClaimCheck() bool { 98 if c == nil { 99 return false 100 } 101 return c.LargeMessageHandleOption == LargeMessageHandleOptionClaimCheck 102 } 103 104 // Disabled returns true if disable large message handle. 105 func (c *LargeMessageHandleConfig) Disabled() bool { 106 if c == nil { 107 return false 108 } 109 return c.LargeMessageHandleOption == LargeMessageHandleOptionNone 110 }