-
Notifications
You must be signed in to change notification settings - Fork 269
/
message.go
169 lines (146 loc) · 4.53 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package common
import (
"encoding/binary"
"encoding/json"
"time"
"github.com/pingcap/tiflow/cdc/model"
"github.com/pingcap/tiflow/pkg/config"
"github.com/tikv/client-go/v2/oracle"
)
// MaxRecordOverhead is used to calculate message size by sarama kafka client.
// reference: https://github.com/IBM/sarama/blob/
// 66521126c71c522c15a36663ae9cddc2b024c799/async_producer.go#L233
// For TiCDC, minimum supported kafka version is `0.11.0.2`,
// which will be treated as `version = 2` by sarama producer.
const MaxRecordOverhead = 5*binary.MaxVarintLen32 + binary.MaxVarintLen64 + 1
// Message represents an message to the sink
type Message struct {
Key []byte
Value []byte
Ts uint64 // reserved for possible output sorting
Schema *string // schema
Table *string // table
Type model.MessageType // type
Protocol config.Protocol // protocol
rowsCount int // rows in one Message
Callback func() // Callback function will be called when the message is sent to the sink.
// PartitionKey for pulsar, route messages to one or different partitions
PartitionKey *string
}
// Length returns the expected size of the Kafka message
// We didn't append any `Headers` when send the message, so ignore the calculations related to it.
// If `ProducerMessage` Headers fields used, this method should also adjust.
func (m *Message) Length() int {
return len(m.Key) + len(m.Value) + MaxRecordOverhead
}
// PhysicalTime returns physical time part of Ts in time.Time
func (m *Message) PhysicalTime() time.Time {
return oracle.GetTimeFromTS(m.Ts)
}
// GetRowsCount returns the number of rows batched in one Message
func (m *Message) GetRowsCount() int {
return m.rowsCount
}
// SetRowsCount set the number of rows
func (m *Message) SetRowsCount(cnt int) {
m.rowsCount = cnt
}
// IncRowsCount increase the number of rows
func (m *Message) IncRowsCount() {
m.rowsCount++
}
// GetSchema returns schema string
func (m *Message) GetSchema() string {
if m.Schema == nil {
return ""
}
return *m.Schema
}
// GetTable returns the Table string
func (m *Message) GetTable() string {
if m.Table == nil {
return ""
}
return *m.Table
}
// SetPartitionKey sets the PartitionKey for a message
// PartitionKey is used for pulsar producer, route messages to one or different partitions
func (m *Message) SetPartitionKey(key string) {
m.PartitionKey = &key
}
// GetPartitionKey returns the GetPartitionKey
func (m *Message) GetPartitionKey() string {
if m.PartitionKey == nil {
return ""
}
return *m.PartitionKey
}
// NewDDLMsg creates a DDL message.
func NewDDLMsg(proto config.Protocol, key, value []byte, event *model.DDLEvent) *Message {
return NewMsg(
proto,
key,
value,
event.CommitTs,
model.MessageTypeDDL,
&event.TableInfo.TableName.Schema,
&event.TableInfo.TableName.Table,
)
}
// NewResolvedMsg creates a resolved ts message.
func NewResolvedMsg(proto config.Protocol, key, value []byte, ts uint64) *Message {
return NewMsg(proto, key, value, ts, model.MessageTypeResolved, nil, nil)
}
// NewMsg should be used when creating a Message struct.
// It copies the input byte slices to avoid any surprises in asynchronous MQ writes.
func NewMsg(
proto config.Protocol,
key []byte,
value []byte,
ts uint64,
ty model.MessageType,
schema, table *string,
) *Message {
ret := &Message{
Key: nil,
Value: nil,
Ts: ts,
Schema: schema,
Table: table,
Type: ty,
Protocol: proto,
rowsCount: 0,
}
if key != nil {
ret.Key = make([]byte, len(key))
copy(ret.Key, key)
}
if value != nil {
ret.Value = make([]byte, len(value))
copy(ret.Value, value)
}
return ret
}
// ClaimCheckMessage is the message sent to the claim-check external storage.
type ClaimCheckMessage struct {
Key []byte `json:"key"`
Value []byte `json:"value"`
}
// UnmarshalClaimCheckMessage unmarshal bytes to ClaimCheckMessage.
func UnmarshalClaimCheckMessage(data []byte) (*ClaimCheckMessage, error) {
var m ClaimCheckMessage
err := json.Unmarshal(data, &m)
return &m, err
}