Skip to content

Commit be0ec09

Browse files
committed
Extract control replica connection config in throttler
1 parent 8ee0457 commit be0ec09

2 files changed

Lines changed: 47 additions & 6 deletions

File tree

go/logic/throttler.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ func (thlr *Throttler) collectReplicationLag(firstThrottlingCollected chan<- boo
176176
}
177177
}
178178

179+
// controlReplicaConnectionConfig returns the connection config used to read
180+
// replication lag from a control replica. In move-tables mode the lag is read
181+
// from the target cluster's replicas, otherwise from the source (inspector)
182+
// cluster's replicas.
183+
func (thlr *Throttler) controlReplicaConnectionConfig(replicaKey mysql.InstanceKey) *mysql.ConnectionConfig {
184+
if thlr.migrationContext.IsMoveTablesMode() {
185+
return thlr.migrationContext.MoveTables.ConnectionConfig.DuplicateCredentials(replicaKey)
186+
}
187+
return thlr.migrationContext.InspectorConnectionConfig.DuplicateCredentials(replicaKey)
188+
}
189+
179190
// collectControlReplicasLag polls all the control replicas to get maximum lag value
180191
func (thlr *Throttler) collectControlReplicasLag() {
181192
if atomic.LoadInt64(&thlr.migrationContext.HibernateUntil) > 0 {
@@ -213,12 +224,7 @@ func (thlr *Throttler) collectControlReplicasLag() {
213224
}
214225
lagResults := make(chan *mysql.ReplicationLagResult, instanceKeyMap.Len())
215226
for replicaKey := range *instanceKeyMap {
216-
var connectionConfig *mysql.ConnectionConfig
217-
if thlr.migrationContext.IsMoveTablesMode() {
218-
connectionConfig = thlr.migrationContext.MoveTables.ConnectionConfig.DuplicateCredentials(replicaKey)
219-
} else {
220-
connectionConfig = thlr.migrationContext.InspectorConnectionConfig.DuplicateCredentials(replicaKey)
221-
}
227+
connectionConfig := thlr.controlReplicaConnectionConfig(replicaKey)
222228

223229
if err := connectionConfig.RegisterTLSConfig(); err != nil {
224230
return &mysql.ReplicationLagResult{Err: err}

go/logic/throttler_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/stretchr/testify/assert"
1414

1515
"github.com/github/gh-ost/go/base"
16+
"github.com/github/gh-ost/go/mysql"
1617
)
1718

1819
func newTestThrottler() *Throttler {
@@ -86,6 +87,40 @@ func TestThrottleReturnsOnContextCancellation(t *testing.T) {
8687
}
8788
}
8889

90+
func TestControlReplicaConnectionConfig(t *testing.T) {
91+
replicaKey := mysql.InstanceKey{Hostname: "replica-host", Port: 3307}
92+
93+
t.Run("uses inspector connection config when not in move-tables mode", func(t *testing.T) {
94+
thlr := newTestThrottler()
95+
thlr.migrationContext.InspectorConnectionConfig.Key = mysql.InstanceKey{Hostname: "source-host", Port: 3306}
96+
thlr.migrationContext.InspectorConnectionConfig.User = "source-user"
97+
thlr.migrationContext.InspectorConnectionConfig.Password = "source-pass"
98+
99+
connectionConfig := thlr.controlReplicaConnectionConfig(replicaKey)
100+
101+
assert.False(t, thlr.migrationContext.IsMoveTablesMode())
102+
assert.Equal(t, replicaKey, connectionConfig.Key)
103+
assert.Equal(t, "source-user", connectionConfig.User)
104+
assert.Equal(t, "source-pass", connectionConfig.Password)
105+
})
106+
107+
t.Run("uses move-tables connection config when in move-tables mode", func(t *testing.T) {
108+
thlr := newTestThrottler()
109+
thlr.migrationContext.MoveTables.TableNames = []string{"my_table"}
110+
thlr.migrationContext.MoveTables.ConnectionConfig = mysql.NewConnectionConfig()
111+
thlr.migrationContext.MoveTables.ConnectionConfig.Key = mysql.InstanceKey{Hostname: "target-host", Port: 3306}
112+
thlr.migrationContext.MoveTables.ConnectionConfig.User = "target-user"
113+
thlr.migrationContext.MoveTables.ConnectionConfig.Password = "target-pass"
114+
115+
connectionConfig := thlr.controlReplicaConnectionConfig(replicaKey)
116+
117+
assert.True(t, thlr.migrationContext.IsMoveTablesMode())
118+
assert.Equal(t, replicaKey, connectionConfig.Key)
119+
assert.Equal(t, "target-user", connectionConfig.User)
120+
assert.Equal(t, "target-pass", connectionConfig.Password)
121+
})
122+
}
123+
89124
func TestThrottleCallsOnThrottledCallback(t *testing.T) {
90125
thlr := newTestThrottler()
91126
thlr.migrationContext.SetThrottled(true, "test", base.NoThrottleReasonHint)

0 commit comments

Comments
 (0)