-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathssh_keepalive_test.go
More file actions
272 lines (227 loc) · 8.38 KB
/
Copy pathssh_keepalive_test.go
File metadata and controls
272 lines (227 loc) · 8.38 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
package connmysql
import (
"context"
"os"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
toxiproxy "github.com/Shopify/toxiproxy/v2/client"
"github.com/stretchr/testify/require"
"github.com/PeerDB-io/peerdb/flow/connectors/utils"
"github.com/PeerDB-io/peerdb/flow/generated/protos"
"github.com/PeerDB-io/peerdb/flow/model"
"github.com/PeerDB-io/peerdb/flow/otel_metrics"
"github.com/PeerDB-io/peerdb/flow/shared"
"github.com/PeerDB-io/peerdb/flow/shared/concurrency"
)
const (
toxiproxyDownProxyPort = 42001
toxiproxyLatencyProxyPort = 42002
toxiproxyResetProxyPort = 42003
toxiproxyCDCHangProxyPort = 42004
toxiproxyCDCCloseHangProxyPort = 42005
)
func setupMySQLConnectorWithProxy(ctx context.Context, t *testing.T, proxyName string, proxyPort int,
) (*MySqlConnector, *toxiproxy.Proxy) {
t.Helper()
toxiproxyClient := utils.NewToxiproxyClient(t)
sshProxy := utils.CreateSSHProxy(t, toxiproxyClient, proxyName, proxyPort)
mysqlHost := "mysql"
if envHost := os.Getenv("CI_MYSQL_HOST"); envHost != "" {
mysqlHost = envHost
}
var mysqlPort uint32 = 3306
if envPortStr := os.Getenv("CI_MYSQL_PORT"); envPortStr != "" {
envPort, err := strconv.ParseUint(strings.TrimSpace(strings.Split(envPortStr, "#")[0]), 10, 32)
require.NoError(t, err, "Failed to parse CI_MYSQL_PORT")
mysqlPort = uint32(envPort)
}
mysqlRootPass := "cipass"
if envPass := os.Getenv("CI_MYSQL_ROOT_PASSWORD"); envPass != "" {
mysqlRootPass = envPass
}
connector, err := NewMySqlConnector(ctx, &protos.MySqlConfig{
Host: mysqlHost,
Port: mysqlPort,
User: "root",
Password: mysqlRootPass,
Database: "mysql",
SshConfig: &protos.SSHConfig{
Host: "localhost",
Port: uint32(proxyPort),
User: "testuser",
Password: "testpass",
},
DisableTls: true,
})
require.NoError(t, err)
// Test initial connection works
err = connector.ConnectionActive(ctx)
require.NoError(t, err, "Initial connection should work")
return connector, sshProxy
}
func setupMySQLConnectorWithSSH(ctx context.Context, t *testing.T, proxyName string, proxyPort int,
) (*MySqlConnector, utils.SSHKeepaliveTestConfig) {
t.Helper()
connector, sshProxy := setupMySQLConnectorWithProxy(ctx, t, proxyName, proxyPort)
keepaliveChan := connector.ssh.GetKeepaliveChan(ctx)
return connector, utils.SSHKeepaliveTestConfig{
SSHProxy: sshProxy,
KeepaliveChan: keepaliveChan,
RunLongQuery: func(ctx context.Context) error {
_, err := connector.Execute(ctx, "SELECT SLEEP(60)")
return err
},
}
}
func TestMySQLSSHKeepaliveWithToxiproxy(t *testing.T) {
t.Parallel()
if os.Getenv("CI_MYSQL_VERSION") == "maria" {
t.Skip("Skipping SSH keepalive test for MariaDB")
}
connector, cfg := setupMySQLConnectorWithSSH(t.Context(), t, "my-ssh-keepalive-test", toxiproxyDownProxyPort)
defer connector.Close()
utils.RunSSHKeepaliveDownTest(t, cfg)
}
func TestMySQLSSHKeepaliveLatency(t *testing.T) {
t.Parallel()
if os.Getenv("CI_MYSQL_VERSION") == "maria" {
t.Skip("Skipping SSH keepalive test for MariaDB")
}
connector, cfg := setupMySQLConnectorWithSSH(t.Context(), t, "my-ssh-latency-test", toxiproxyLatencyProxyPort)
defer connector.Close()
utils.RunSSHKeepaliveLatencyTest(t, cfg)
}
func TestMySQLSSHResetPeer(t *testing.T) {
t.Parallel()
if os.Getenv("CI_MYSQL_VERSION") == "maria" {
t.Skip("Skipping SSH keepalive test for MariaDB")
}
connector, cfg := setupMySQLConnectorWithSSH(t.Context(), t, "my-ssh-reset-peer-test", toxiproxyResetProxyPort)
defer connector.Close()
utils.RunSSHResetPeerTest(t, cfg)
}
func setupCDCPullRecords(
ctx context.Context, t *testing.T, connector *MySqlConnector, flowJobName string,
) (*model.PullRecordsRequest[model.RecordItems], *otel_metrics.OtelManager) {
t.Helper()
gtidOn, err := connector.GetGtidModeOn(ctx)
require.NoError(t, err)
var offsetText string
if gtidOn {
gset, err := connector.GetMasterGTIDSet(ctx)
require.NoError(t, err)
offsetText = gset.String()
} else {
pos, err := connector.GetMasterPos(ctx)
require.NoError(t, err)
offsetText = posToOffsetText(pos)
}
otelManager, err := otel_metrics.NewOtelManager(ctx, "test", false)
require.NoError(t, err)
req := &model.PullRecordsRequest[model.RecordItems]{
FlowJobName: flowJobName,
RecordStream: model.NewCDCStream[model.RecordItems](100),
TableNameMapping: map[string]model.NameAndExclude{},
TableNameSchemaMapping: map[string]*protos.TableSchema{},
LastOffset: model.CdcCheckpoint{Text: offsetText},
MaxBatchSize: 10000,
IdleTimeout: time.Minute,
ConsumedOffset: &atomic.Int64{},
}
return req, otelManager
}
func TestMySQLSSHKeepaliveCDCHang(t *testing.T) {
t.Parallel()
if os.Getenv("CI_MYSQL_VERSION") == "maria" {
t.Skip("Skipping SSH keepalive test for MariaDB")
}
ctx := t.Context()
connector, sshProxy := setupMySQLConnectorWithProxy(ctx, t, "my-ssh-cdc-down-test", toxiproxyCDCHangProxyPort)
defer connector.Close()
keepaliveChan := connector.ssh.GetKeepaliveChan(ctx)
require.NotNil(t, keepaliveChan, "SSH keepalive channel should exist")
req, otelManager := setupCDCPullRecords(ctx, t, connector, "test_ssh_cdc_hang")
pullDone := concurrency.NewLatch[error]()
go func() {
pullDone.Set(connector.PullRecords(ctx, shared.CatalogPool{}, otelManager, req))
}()
go func() {
for range req.RecordStream.GetRecords() {
}
}()
time.Sleep(2 * time.Second)
t.Log("Adding latency toxic to simulate network black hole during CDC streaming")
_, err := sshProxy.AddToxic("latency", "latency", "", 1.0, toxiproxy.Attributes{
"latency": 120000,
})
require.NoError(t, err)
// Wait for keepalive to detect the failure first — if PullRecords exits before
// the keepalive fires, something else closed the connection (driver timeout, etc.)
select {
case <-keepaliveChan:
t.Log("SSH keepalive detected failure")
case <-pullDone.Chan():
t.Fatal("PullRecords exited before SSH keepalive fired — connection closed by something other than keepalive")
case <-time.After(3 * utils.SSHKeepaliveInterval):
t.Fatal("SSH keepalive did not fire in time")
}
select {
case <-pullDone.Chan():
pullErr := pullDone.Wait()
require.Error(t, pullErr, "PullRecords should fail after SSH keepalive detects black hole")
t.Logf("PullRecords returned: %v", pullErr)
case <-time.After(10 * time.Second):
t.Fatal("PullRecords did not return after SSH keepalive closed the connection")
}
}
func TestMySQLSSHKeepaliveCDCCloseHang(t *testing.T) {
t.Parallel()
if os.Getenv("CI_MYSQL_VERSION") == "maria" {
t.Skip("Skipping SSH keepalive test for MariaDB")
}
ctx := t.Context()
connector, sshProxy := setupMySQLConnectorWithProxy(ctx, t, "my-ssh-cdc-latency-test", toxiproxyCDCCloseHangProxyPort)
defer connector.Close()
keepaliveChan := connector.ssh.GetKeepaliveChan(ctx)
require.NotNil(t, keepaliveChan, "SSH keepalive channel should exist")
req, otelManager := setupCDCPullRecords(ctx, t, connector, "test_ssh_cdc_close_hang")
// Use a short-lived context so PullRecords starts shutting down while
// the connection is blocked by latency
cancelCtx, cancel := context.WithTimeout(ctx, 7*time.Second)
defer cancel()
pullDone := concurrency.NewLatch[error]()
go func() {
pullDone.Set(connector.PullRecords(cancelCtx, shared.CatalogPool{}, otelManager, req))
}()
go func() {
for range req.RecordStream.GetRecords() {
}
}()
// Wait for CDC streaming to establish before adding latency
time.Sleep(2 * time.Second)
// Add high latency so the connection becomes unresponsive without erroring
t.Log("Adding latency toxic to block the connection during CDC streaming and cleanup")
_, err := sshProxy.AddToxic("latency", "latency", "", 1.0, toxiproxy.Attributes{
"latency": 120000,
})
require.NoError(t, err)
t.Logf("Waiting for context cancel (~5s), then PullRecords should hang until keepalive fires (within %s)", 3*utils.SSHKeepaliveInterval)
// Wait for keepalive to detect the failure — PullRecords should be hanging
// until keepalive closes the underlying connection
select {
case <-keepaliveChan:
t.Log("SSH keepalive detected failure")
case <-time.After(3 * utils.SSHKeepaliveInterval):
t.Fatal("SSH keepalive did not fire in time")
}
select {
case <-pullDone.Chan():
pullErr := pullDone.Wait()
require.ErrorIs(t, pullErr, context.DeadlineExceeded)
case <-time.After(10 * time.Second):
t.Fatal("PullRecords did not return after SSH keepalive closed the connection")
}
}