File size: 2,833 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package swosync

import (
	"context"
	_ "embed"
	"fmt"
	"time"

	"github.com/target/goalert/swo/swodb"
	"github.com/target/goalert/swo/swoinfo"
	"github.com/target/goalert/util/sqlutil"
)

//go:embed changelog.sql
var changelogQuery string

func triggerName(table string) string {
	return sqlutil.QuoteID(fmt.Sprintf("zz_99_change_log_%s", table))
}

// StartTrackingChanges instruments and begins tracking changes to the DB.
//
// - Creates the change_log table
// - Gets the list of tables and sequences to track
// - Creates the change trigger for each table
// - Disables triggers in the new DB
// - Waits for any in-flight transactions to finish (since these may not have picked up the change trigger)
func (l *LogicalReplicator) StartTrackingChanges(ctx context.Context) error {
	l.printf(ctx, "enabling logical replication...")
	_, err := l.srcConn.Exec(ctx, changelogQuery)
	if err != nil {
		return fmt.Errorf("create change_log and fn: %w", err)
	}

	l.tables, err = swoinfo.ScanTables(ctx, l.srcConn)
	if err != nil {
		return fmt.Errorf("scan tables: %w", err)
	}

	l.seqNames, err = swoinfo.ScanSequences(ctx, l.srcConn)
	if err != nil {
		return fmt.Errorf("scan sequences: %w", err)
	}

	for _, table := range l.tables {
		// create change trigger in source DB
		chgTrigQuery := fmt.Sprintf(`
			CREATE TRIGGER %s AFTER INSERT OR UPDATE OR DELETE ON %s
			FOR EACH ROW EXECUTE PROCEDURE fn_process_change_log()
		`, triggerName(table.Name()), sqlutil.QuoteID(table.Name()))
		_, err := l.srcConn.Exec(ctx, chgTrigQuery)
		if err != nil {
			return fmt.Errorf("create change trigger for %s: %w", table.Name(), err)
		}

		// disable triggers in destination DB
		disableTrigQuery := fmt.Sprintf(`ALTER TABLE %s DISABLE TRIGGER USER`, sqlutil.QuoteID(table.Name()))
		_, err = l.dstConn.Exec(ctx, disableTrigQuery)
		if err != nil {
			return fmt.Errorf("disable trigger for %s: %w", table.Name(), err)
		}
	}

	err = swodb.New(l.srcConn).EnableChangeLogTriggers(ctx)
	if err != nil {
		return fmt.Errorf("enable change log triggers: %w", err)
	}

	// wait for in-flight transactions to finish
	l.printf(ctx, "waiting for in-flight transactions to finish")

	db := swodb.New(l.srcConn)

	now, err := db.Now(ctx)
	if err != nil {
		return fmt.Errorf("wait for active tx: get current time: %w", err)
	}

	for {
		n, err := db.ActiveTxCount(ctx, now)
		if err != nil {
			return fmt.Errorf("wait for active tx: get active tx count: %w", err)
		}
		if n == 0 {
			break
		}

		l.printf(ctx, "waiting for %d transaction(s) to finish", n)
		err = ctxSleep(ctx, time.Second)
		if err != nil {
			return fmt.Errorf("wait for active tx: sleep: %w", err)
		}
	}

	return nil
}

func ctxSleep(ctx context.Context, dur time.Duration) error {
	select {
	case <-ctx.Done():
		return ctx.Err()
	case <-time.After(dur):
	}
	return nil
}