-
Notifications
You must be signed in to change notification settings - Fork 18
/
relation.go
180 lines (159 loc) · 4.5 KB
/
relation.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
package pgcat
import (
"context"
"fmt"
"strings"
"time"
"github.com/jackc/pgx/v4"
"github.com/kyleconroy/pgoutput"
"github.com/pkg/errors"
)
type relationState struct {
pgoutput.Relation
state int
localNamespace string
localName string
localFullName string
failedTime time.Time
}
type localColumn struct {
Name string
IsReplIdent bool
RemoteIdx int
}
type localTableState struct {
Name string
Columns []localColumn
localInSync bool
remoteInSync bool
}
func (state *applyState) mapTableName(sub *subscription, relation *relationState) {
// map the name
if relation.localFullName == "" {
fullName := fmt.Sprintf("%s.%s", relation.Namespace, relation.Name)
var mapping tableMapping
for _, mapping = range sub.mapping {
if fullName == mapping.src {
relation.localFullName = mapping.dst
state.Debugf("use direct mapping, %s -> %s", fullName, relation.localFullName)
break
} else if mapping.regexp.MatchString(fullName) {
relation.localFullName = mapping.regexp.ReplaceAllString(fullName, mapping.dst)
state.Debugf("use regexp mapping, %s -> %s", fullName, relation.localFullName)
break
}
}
if relation.localFullName != "" {
tmp := strings.Split(relation.localFullName, ".")
if len(tmp) != 2 {
state.Panic("invalid table mapping result, mapping=%s, from=%s, to=%s",
mapping, fullName, relation.localFullName)
}
relation.localNamespace = tmp[0]
relation.localName = tmp[1]
} else {
relation.localFullName = fullName
relation.localNamespace = relation.Namespace
relation.localName = relation.Name
}
}
}
func doRelMap(localTable *localTableState, relation *relationState, applyConn *pgx.Conn) error {
if !localTable.localInSync {
var inSync bool
row := applyConn.QueryRow(context.Background(), "select pgcat_check_table($1, $2)",
relation.localNamespace, relation.localName)
if err := row.Scan(&inSync); err != nil {
return errors.Wrap(err, "pgcat_check_table failed")
}
if !inSync {
rows, err := applyConn.Query(context.Background(), "select * from pgcat_get_table_columns($1, $2)",
relation.localNamespace, relation.localName)
if err != nil {
return err
}
defer rows.Close()
localTable.Columns = localTable.Columns[:0]
for rows.Next() {
col := localColumn{}
err := rows.Scan(&col.Name, &col.IsReplIdent)
if err != nil {
return err
}
localTable.Columns = append(localTable.Columns, col)
}
if err := rows.Err(); err != nil {
return err
}
localTable.remoteInSync = false
}
localTable.localInSync = true
}
// remove missing columns at remote side
if !localTable.remoteInSync {
for i := 0; i < len(localTable.Columns); {
col := &localTable.Columns[i]
remoteIdx := -1
for j, c := range relation.Columns {
if c.Name == col.Name {
remoteIdx = j
break
}
}
if remoteIdx == -1 {
if col.IsReplIdent {
return fmt.Errorf(
"missing replication identity at remote side, table=%s, column=%s",
relation.localFullName, col.Name)
}
localTable.Columns = append(localTable.Columns[:i], localTable.Columns[i+1:]...)
} else {
col.RemoteIdx = remoteIdx
i++
}
}
localTable.remoteInSync = true
}
return nil
}
func getLocalTable(sub *subscription, relation *relationState,
localTables map[string]*localTableState, applyConn *pgx.Conn) (*localTableState, error) {
localTable, ok := localTables[relation.localFullName]
if ok && localTable.localInSync && localTable.remoteInSync {
return localTable, nil
}
if !ok {
localTable = &localTableState{Name: relation.localFullName}
localTables[relation.localFullName] = localTable
}
if err := doRelMap(localTable, relation, applyConn); err != nil {
return nil, err
}
return localTable, nil
}
func (state *applyState) handleRelation(v pgoutput.Relation) {
if state.isSync {
if state.relation.ID == v.ID {
// update the relation info
state.relation.Relation = v
state.localTable.remoteInSync = false
doRelMap(state.localTable, state.relation, state.applyConn)
}
return
}
var relState *relationState
if oldRelState, ok := state.relations[v.ID]; ok {
relState = oldRelState
// update the relation info
relState.Relation = v
} else {
relState = &relationState{Relation: v}
state.mapTableName(state.sub, relState)
state.updateRelState("i", relState)
state.relations[v.ID] = relState
}
// invalidate relation binding
if t, ok := state.localTables[relState.localFullName]; ok {
t.remoteInSync = false
}
}