MCPcopy Create free account
hub / github.com/apecloud/myduckserver / UpdateSubscriptions

Function UpdateSubscriptions

pgserver/logrepl/subscription.go:29–104  ·  view source on GitHub ↗
(ctx *sql.Context)

Source from the content-addressed store, hash-verified

27var subscriptionMap = sync.Map{}
28
29func UpdateSubscriptions(ctx *sql.Context) error {
30 rows, err := adapter.QueryCatalog(ctx, catalog.InternalTables.PgSubscription.SelectAllStmt())
31 if err != nil {
32 return err
33 }
34 defer rows.Close()
35
36 var subMap = make(map[string]*Subscription)
37 for rows.Next() {
38 var name, conn, pub, lsn string
39 var enabled bool
40 if err := rows.Scan(&name, &conn, &pub, &lsn, &enabled); err != nil {
41 return err
42 }
43 subMap[name] = &Subscription{
44 Subscription: name,
45 Conn: conn,
46 Publication: pub,
47 LsnStr: lsn,
48 Enabled: enabled,
49 Replicator: nil,
50 }
51 }
52
53 if err = rows.Err(); err != nil {
54 return err
55 }
56
57 for tempName, tempSub := range subMap {
58 if _, loaded := subscriptionMap.LoadOrStore(tempName, tempSub); !loaded {
59 replicator, err := NewLogicalReplicator(tempName, tempSub.Conn)
60 if err != nil {
61 return fmt.Errorf("failed to create logical replicator: %v", err)
62 }
63
64 if sub, ok := subscriptionMap.Load(tempName); ok {
65 if subscription, ok := sub.(*Subscription); ok {
66 subscription.Replicator = replicator
67 }
68 }
69
70 err = replicator.CreateReplicationSlotIfNotExists(tempSub.Publication)
71 if err != nil {
72 return fmt.Errorf("failed to create replication slot: %v", err)
73 }
74 if tempSub.Enabled {
75 go replicator.StartReplication(ctx, tempSub.Publication)
76 }
77 } else {
78 if sub, ok := subscriptionMap.Load(tempName); ok {
79 if subscription, ok := sub.(*Subscription); ok {
80 if tempSub.Enabled != subscription.Enabled {
81 subscription.Enabled = tempSub.Enabled
82 if subscription.Enabled {
83 go subscription.Replicator.StartReplication(ctx, subscription.Publication)
84 } else {
85 subscription.Replicator.Stop()
86 }

Callers 7

mainFunction · 0.92
stopAllReplicationFunction · 0.92
startAllReplicationFunction · 0.92
executeDropMethod · 0.92
doCreateSubscriptionMethod · 0.92

Calls 11

StartReplicationMethod · 0.95
QueryCatalogFunction · 0.92
NewLogicalReplicatorFunction · 0.85
SelectAllStmtMethod · 0.80
LoadMethod · 0.80
CloseMethod · 0.65
DeleteMethod · 0.65
NextMethod · 0.45
ErrMethod · 0.45
StopMethod · 0.45

Tested by

no test coverage detected