-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.go
More file actions
301 lines (253 loc) · 8.35 KB
/
sync.go
File metadata and controls
301 lines (253 loc) · 8.35 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Package sync provides synchronization orchestration between etcd and PostgreSQL.
package sync
import (
"context"
"fmt"
"time"
"github.com/sirupsen/logrus"
clientv3 "go.etcd.io/etcd/client/v3"
)
// Service orchestrates bidirectional synchronization between etcd and PostgreSQL
type Service struct {
pgPool PgxIface
etcdClient *EtcdClient
prefix string
pollingInterval time.Duration
}
// NewService creates a new synchronization service
func NewService(pgPool PgxIface, etcdClient *EtcdClient, pollingInterval time.Duration) *Service {
return &Service{
pgPool: pgPool,
etcdClient: etcdClient,
pollingInterval: pollingInterval,
}
}
// Start begins the bidirectional synchronization process
func (s *Service) Start(ctx context.Context) error {
logrus.Info("Starting etcd_fdw bidirectional synchronization")
// Perform initial sync from etcd to PostgreSQL
if err := s.initialSync(ctx); err != nil {
return fmt.Errorf("initial sync failed: %w", err)
}
// Start continuous synchronization in both directions
errChan := make(chan error, 2)
// Start etcd to PostgreSQL sync
go func() {
errChan <- s.syncEtcdToPostgreSQL(ctx)
}()
// Start PostgreSQL to etcd sync
go func() {
errChan <- s.syncPostgreSQLToEtcd(ctx)
}()
// Wait for either goroutine to error or context cancellation
select {
case err := <-errChan:
return fmt.Errorf("sync error: %w", err)
case <-ctx.Done():
logrus.Info("Synchronization stopped due to context cancellation")
return ctx.Err()
}
}
// initialSync performs the initial bulk sync from etcd to PostgreSQL
func (s *Service) initialSync(ctx context.Context) error {
logrus.Info("Starting initial sync from etcd to PostgreSQL")
// Get all keys from etcd with the specified prefix
pairs, err := s.etcdClient.GetAllKeys(ctx, s.prefix)
if err != nil {
return fmt.Errorf("failed to get all keys from etcd: %w", err)
}
if len(pairs) == 0 {
logrus.Info("No keys found in etcd for initial sync")
return nil
}
// Convert to PostgreSQL records
records := make([]KeyValueRecord, len(pairs))
for i, pair := range pairs {
records[i] = KeyValueRecord{
Key: pair.Key,
Value: pair.Value,
Revision: pair.Revision,
Ts: time.Now(),
Tombstone: pair.Tombstone,
}
}
// Bulk insert using COPY
if err := BulkInsert(ctx, s.pgPool, records); err != nil {
return fmt.Errorf("failed to bulk insert records: %w", err)
}
logrus.WithField("count", len(records)).Info("Initial sync completed successfully")
return nil
}
// syncEtcdToPostgreSQL continuously watches etcd for changes and syncs to PostgreSQL
func (s *Service) syncEtcdToPostgreSQL(ctx context.Context) error {
logrus.Info("Starting etcd to PostgreSQL sync watcher")
// Get the latest revision from PostgreSQL to resume from
latestRevision, err := GetLatestRevision(ctx, s.pgPool)
if err != nil {
return fmt.Errorf("failed to get latest revision: %w", err)
}
// Start watching from the next revision with automatic recovery
watchChan := s.etcdClient.WatchWithRecovery(ctx, latestRevision)
for {
select {
case <-ctx.Done():
return ctx.Err()
case watchResp, ok := <-watchChan:
if !ok {
// Watch channel closed, likely due to context cancellation
return ctx.Err()
}
if watchResp.Canceled {
// This should be handled by WatchWithRecovery, but log it
logrus.Warn("etcd watch was canceled - recovery should be automatic")
continue
}
if err := watchResp.Err(); err != nil {
logrus.WithError(err).Error("etcd watch error - recovery should be automatic")
continue
}
// Process all events in this watch response
for _, event := range watchResp.Events {
err := RetryWithBackoff(ctx, DefaultRetryConfig(), func() error {
return s.processEtcdEvent(ctx, event)
})
if err != nil {
logrus.WithError(err).WithField("key", string(event.Kv.Key)).Error("Failed to process etcd event after retries")
// Continue processing other events rather than failing entirely
}
}
}
}
}
// processEtcdEvent processes a single etcd event and syncs it to PostgreSQL
func (s *Service) processEtcdEvent(ctx context.Context, event *clientv3.Event) error {
key := string(event.Kv.Key)
revision := event.Kv.ModRevision
var record KeyValueRecord
record.Key = key
record.Revision = revision
record.Ts = time.Now()
switch event.Type {
case clientv3.EventTypePut:
value := string(event.Kv.Value)
record.Value = value
record.Tombstone = false
logrus.WithFields(logrus.Fields{
"key": key,
"revision": revision,
"type": "PUT",
}).Debug("Processing etcd PUT event")
case clientv3.EventTypeDelete:
record.Value = ""
record.Tombstone = true
logrus.WithFields(logrus.Fields{
"key": key,
"revision": revision,
"type": "DELETE",
}).Debug("Processing etcd DELETE event")
default:
return fmt.Errorf("unknown event type: %v", event.Type)
}
// Insert the record into PostgreSQL
if err := BulkInsert(ctx, s.pgPool, []KeyValueRecord{record}); err != nil {
return fmt.Errorf("failed to insert event into PostgreSQL: %w", err)
}
logrus.WithFields(logrus.Fields{
"key": key,
"revision": revision,
"type": event.Type.String(),
}).Info("Synced etcd event to PostgreSQL")
return nil
}
// syncPostgreSQLToEtcd polls for pending records and syncs them to etcd
func (s *Service) syncPostgreSQLToEtcd(ctx context.Context) error {
logrus.Info("Starting PostgreSQL to etcd sync poller with polling mechanism")
ticker := time.NewTicker(s.pollingInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
if err := s.pollAndProcessPendingRecords(ctx); err != nil {
logrus.WithError(err).Error("Failed to poll and process pending records")
}
}
}
}
func (s *Service) pollAndProcessPendingRecords(ctx context.Context) error {
// Get pending records (revision = -1) using SELECT FOR UPDATE SKIP LOCKED
pendingRecords, err := GetPendingRecords(ctx, s.pgPool)
if err != nil {
return fmt.Errorf("failed to get pending records: %w", err)
}
if len(pendingRecords) == 0 {
return nil // No pending records to process
}
logrus.WithField("count", len(pendingRecords)).Debug("Found pending records to sync to etcd")
// Process each pending record with retry logic
for _, record := range pendingRecords {
err := RetryWithBackoff(ctx, DefaultRetryConfig(), func() error {
return s.processPendingRecord(ctx, record)
})
if err != nil {
logrus.WithError(err).WithField("key", record.Key).Error("Failed to process pending record after retries")
// Continue processing other records rather than failing entirely
}
}
return nil
}
// processPendingRecord processes a single pending record and syncs it to etcd
func (s *Service) processPendingRecord(ctx context.Context, record KeyValueRecord) error {
logrus.WithFields(logrus.Fields{
"key": record.Key,
"tombstone": record.Tombstone,
}).Debug("Processing pending record")
// Apply the change to etcd with retry logic
var newRevision int64
if record.Tombstone {
// Delete operation
err := RetryEtcdOperation(ctx, func() error {
resp, delErr := s.etcdClient.Delete(ctx, record.Key)
if delErr != nil {
return delErr
}
newRevision = resp.Header.Revision
return nil
})
if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"key": record.Key,
"operation": "etcd_delete",
}).Error("Failed to sync delete to etcd after retries")
return fmt.Errorf("failed to delete key from etcd: %w", err)
}
logrus.WithFields(logrus.Fields{
"key": record.Key,
"revision": newRevision,
}).Info("Synced PostgreSQL change to etcd (DELETE)")
} else {
// Put operation
err := RetryEtcdOperation(ctx, func() error {
resp, putErr := s.etcdClient.Put(ctx, record.Key, record.Value)
if putErr != nil {
return putErr
}
newRevision = resp.Header.Revision
return nil
})
if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"key": record.Key,
"operation": "etcd_put",
}).Error("Failed to sync put to etcd after retries")
return fmt.Errorf("failed to put key to etcd: %w", err)
}
logrus.WithFields(logrus.Fields{
"key": record.Key,
"revision": newRevision,
}).Info("Synced PostgreSQL change to etcd (PUT)")
}
// Update local record with the new etcd revision
return UpdateRevision(ctx, s.pgPool, record.Key, newRevision)
}