Skip to content

Commit 0c228e1

Browse files
committed
Propagate latest cursor errors
1 parent d77e194 commit 0c228e1

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

lib/connect_client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (p connectClient) Read(ctx context.Context, logger DatabaseLogger, ps Plane
131131
logger.Info(preamble + "peeking to see if there's any new rows")
132132
latestCursorPosition, lcErr := p.getLatestCursorPosition(ctx, currentPosition.Shard, currentPosition.Keyspace, tableName, ps, tabletType)
133133
if lcErr != nil {
134-
return currentSerializedCursor, errors.Wrap(err, "Unable to get latest cursor position")
134+
return currentSerializedCursor, errors.Wrap(lcErr, "Unable to get latest cursor position")
135135
}
136136

137137
// the current vgtid is the same as the last synced vgtid, no new rows.
@@ -436,7 +436,7 @@ func (p connectClient) getLatestCursorPosition(ctx context.Context, shard, keysp
436436

437437
c, err := client.Sync(ctx, sReq)
438438
if err != nil {
439-
return "", nil
439+
return "", err
440440
}
441441

442442
for {

lib/connect_client_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,69 @@ func TestRead_CanEarlyExitIfNoNewVGtidInPeek(t *testing.T) {
110110
assert.Contains(t, dbl.messages[len(dbl.messages)-1].message, "no new rows found, exiting")
111111
}
112112

113+
func TestRead_ReturnsLatestCursorSyncError(t *testing.T) {
114+
dbl := &dbLogger{}
115+
ped := connectClient{}
116+
getKeyspaceTableColumnsFunc := func(ctx context.Context, keyspaceName string, tableName string) ([]MysqlColumn, error) {
117+
return []MysqlColumn{{Name: "id", Type: "bigint", IsPrimaryKey: true}, {Name: "email", Type: "varchar(256)", IsPrimaryKey: false}}, nil
118+
}
119+
mysqlClient := NewTestMysqlClient(getKeyspaceTableColumnsFunc)
120+
ped.Mysql = &mysqlClient
121+
tc := &psdbconnect.TableCursor{
122+
Shard: "-",
123+
Position: "THIS_IS_A_SHARD_GTID",
124+
Keyspace: "connect-test",
125+
}
126+
127+
cc := clientConnectionMock{
128+
syncFn: func(ctx context.Context, in *psdbconnect.SyncRequest, opts ...grpc.CallOption) (psdbconnect.Connect_SyncClient, error) {
129+
assert.Equal(t, "current", in.Cursor.Position)
130+
return nil, errors.New("sync unavailable")
131+
},
132+
}
133+
ped.clientFn = func(ctx context.Context, ps PlanetScaleSource) (psdbconnect.ConnectClient, error) {
134+
return &cc, nil
135+
}
136+
137+
sc, err := ped.Read(context.Background(), dbl, PlanetScaleSource{}, "customers", nil, tc, nil, nil, nil)
138+
assert.Nil(t, sc)
139+
assert.ErrorContains(t, err, "Unable to get latest cursor position")
140+
assert.ErrorContains(t, err, "sync unavailable")
141+
assert.Equal(t, 1, cc.syncFnInvokedCount)
142+
}
143+
144+
func TestRead_ReturnsLatestCursorRecvError(t *testing.T) {
145+
dbl := &dbLogger{}
146+
ped := connectClient{}
147+
getKeyspaceTableColumnsFunc := func(ctx context.Context, keyspaceName string, tableName string) ([]MysqlColumn, error) {
148+
return []MysqlColumn{{Name: "id", Type: "bigint", IsPrimaryKey: true}, {Name: "email", Type: "varchar(256)", IsPrimaryKey: false}}, nil
149+
}
150+
mysqlClient := NewTestMysqlClient(getKeyspaceTableColumnsFunc)
151+
ped.Mysql = &mysqlClient
152+
tc := &psdbconnect.TableCursor{
153+
Shard: "-",
154+
Position: "THIS_IS_A_SHARD_GTID",
155+
Keyspace: "connect-test",
156+
}
157+
158+
getCurrentVGtidClient := &connectSyncClientMock{}
159+
cc := clientConnectionMock{
160+
syncFn: func(ctx context.Context, in *psdbconnect.SyncRequest, opts ...grpc.CallOption) (psdbconnect.Connect_SyncClient, error) {
161+
assert.Equal(t, "current", in.Cursor.Position)
162+
return getCurrentVGtidClient, nil
163+
},
164+
}
165+
ped.clientFn = func(ctx context.Context, ps PlanetScaleSource) (psdbconnect.ConnectClient, error) {
166+
return &cc, nil
167+
}
168+
169+
sc, err := ped.Read(context.Background(), dbl, PlanetScaleSource{}, "customers", nil, tc, nil, nil, nil)
170+
assert.Nil(t, sc)
171+
assert.ErrorContains(t, err, "Unable to get latest cursor position")
172+
assert.ErrorContains(t, err, "EOF")
173+
assert.Equal(t, 1, cc.syncFnInvokedCount)
174+
}
175+
113176
func TestRead_CanPickPrimaryForShardedKeyspaces(t *testing.T) {
114177
dbl := &dbLogger{}
115178
ped := connectClient{}

0 commit comments

Comments
 (0)