/
wal_transaction.go
280 lines (231 loc) · 5.79 KB
/
wal_transaction.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package listener
import (
"context"
"log/slog"
"strconv"
"strings"
"sync"
"time"
"github.com/goccy/go-json"
"github.com/google/uuid"
"github.com/ihippik/wal-listener/v2/publisher"
)
// ActionKind kind of action on WAL message.
type ActionKind string
// kind of WAL message.
const (
ActionKindInsert ActionKind = "INSERT"
ActionKindUpdate ActionKind = "UPDATE"
ActionKindDelete ActionKind = "DELETE"
)
type transactionMonitor interface {
IncFilterSkippedEvents(table string)
}
// WalTransaction transaction specified WAL message.
type WalTransaction struct {
log *slog.Logger
monitor transactionMonitor
LSN int64
BeginTime *time.Time
CommitTime *time.Time
RelationStore map[int32]RelationData
Actions []ActionData
pool *sync.Pool
}
// NewWalTransaction create and initialize new WAL transaction.
func NewWalTransaction(log *slog.Logger, pool *sync.Pool, monitor transactionMonitor) *WalTransaction {
const aproxData = 300
return &WalTransaction{
pool: pool,
log: log,
monitor: monitor,
RelationStore: make(map[int32]RelationData),
Actions: make([]ActionData, 0, aproxData),
}
}
func (k ActionKind) string() string {
return string(k)
}
// RelationData kind of WAL message data.
type RelationData struct {
Schema string
Table string
Columns []Column
}
// ActionData kind of WAL message data.
type ActionData struct {
Schema string
Table string
Kind ActionKind
OldColumns []Column
NewColumns []Column
}
// Column of the table with which changes occur.
type Column struct {
log *slog.Logger
name string
value any
valueType int
isKey bool
}
// AssertValue converts bytes to a specific type depending
// on the type of this data in the database table.
func (c *Column) AssertValue(src []byte) {
var (
val any
err error
)
if src == nil {
c.value = nil
return
}
strSrc := string(src)
const (
timestampLayout = "2006-01-02 15:04:05"
timestampWithTZLayout = "2006-01-02 15:04:05.999999999-07"
)
switch c.valueType {
case BoolOID:
val, err = strconv.ParseBool(strSrc)
case Int2OID, Int4OID:
val, err = strconv.Atoi(strSrc)
case Int8OID:
val, err = strconv.ParseInt(strSrc, 10, 64)
case TextOID, VarcharOID:
val = strSrc
case TimestampOID:
val, err = time.Parse(timestampLayout, strSrc)
case TimestamptzOID:
val, err = time.ParseInLocation(timestampWithTZLayout, strSrc, time.UTC)
case DateOID, TimeOID:
val = strSrc
case UUIDOID:
val, err = uuid.Parse(strSrc)
case JSONBOID:
var m any
if src[0] == '[' {
m = make([]any, 0)
} else {
m = make(map[string]any)
}
err = json.Unmarshal(src, &m)
val = m
default:
c.log.Warn(
"unknown oid type",
slog.Int("pg_type", c.valueType),
slog.String("column_name", c.name),
)
val = strSrc
}
if err != nil {
c.log.Error(
"column data parse error",
slog.String("err", err.Error()),
slog.Int("pg_type", c.valueType),
slog.String("column_name", c.name),
)
}
c.value = val
}
// Clear transaction data.
func (w *WalTransaction) Clear() {
w.CommitTime = nil
w.BeginTime = nil
w.Actions = nil
}
// CreateActionData create action from WAL message data.
func (w *WalTransaction) CreateActionData(
relationID int32,
oldRows []TupleData,
newRows []TupleData,
kind ActionKind,
) (a ActionData, err error) {
rel, ok := w.RelationStore[relationID]
if !ok {
return a, errRelationNotFound
}
a = ActionData{
Schema: rel.Schema,
Table: rel.Table,
Kind: kind,
}
oldColumns := make([]Column, 0, len(oldRows))
for num, row := range oldRows {
column := Column{
log: w.log,
name: rel.Columns[num].name,
valueType: rel.Columns[num].valueType,
isKey: rel.Columns[num].isKey,
}
column.AssertValue(row.Value)
oldColumns = append(oldColumns, column)
}
a.OldColumns = oldColumns
newColumns := make([]Column, 0, len(newRows))
for num, row := range newRows {
column := Column{
log: w.log,
name: rel.Columns[num].name,
valueType: rel.Columns[num].valueType,
isKey: rel.Columns[num].isKey,
}
column.AssertValue(row.Value)
newColumns = append(newColumns, column)
}
a.NewColumns = newColumns
return a, nil
}
// CreateEventsWithFilter filter WAL message by table,
// action and create events for each value.
func (w *WalTransaction) CreateEventsWithFilter(ctx context.Context, tableMap map[string][]string) <-chan *publisher.Event {
output := make(chan *publisher.Event)
go func(ctx context.Context) {
for _, item := range w.Actions {
if err := ctx.Err(); err != nil {
w.log.Debug("create events with filter: context canceled")
break
}
dataOld := make(map[string]any, len(item.OldColumns))
for _, val := range item.OldColumns {
dataOld[val.name] = val.value
}
data := make(map[string]any, len(item.NewColumns))
for _, val := range item.NewColumns {
data[val.name] = val.value
}
event := w.pool.Get().(*publisher.Event)
event.ID = uuid.New()
event.Schema = item.Schema
event.Table = item.Table
event.Action = item.Kind.string()
event.Data = data
event.DataOld = dataOld
event.EventTime = *w.CommitTime
actions, validTable := tableMap[item.Table]
validAction := inArray(actions, item.Kind.string())
if validTable && validAction {
output <- event
continue
}
w.monitor.IncFilterSkippedEvents(item.Table)
w.log.Debug(
"wal-message was skipped by filter",
slog.String("schema", item.Schema),
slog.String("table", item.Table),
slog.String("action", string(item.Kind)),
)
}
close(output)
}(ctx)
return output
}
// inArray checks whether the value is in an array.
func inArray(arr []string, value string) bool {
for _, v := range arr {
if strings.EqualFold(v, value) {
return true
}
}
return false
}