Skip to content

Commit a29b484

Browse files
committed
code review feedback: test improvements & do not set target alias where it is not valid to set
1 parent b629a0f commit a29b484

File tree

4 files changed

+45
-51
lines changed

4 files changed

+45
-51
lines changed

go/test/endtoend/vtgate/misc_test.go

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -960,40 +960,39 @@ func TestTabletTargeting(t *testing.T) {
960960

961961
// Query different replicas and verify different server UUIDs
962962
// This proves we're actually hitting different physical tablets
963-
for range 10 {
964-
// Get server UUID from first replica
965-
useStmt = fmt.Sprintf("USE `ks:-80@replica|%s`", instances["-80"]["replica"][0])
966-
utils.Exec(t, conn, useStmt)
967-
var uuid1 string
968-
for i := range 5 {
969-
result1 := utils.Exec(t, conn, "SELECT @@server_uuid")
970-
require.NotNil(t, result1)
971-
require.Greater(t, len(result1.Rows), 0)
972-
if i > 0 {
973-
// UUID should be the same across multiple queries to same tablet
974-
require.Equal(t, uuid1, result1.Rows[0][0].ToString())
975-
}
976-
uuid1 = result1.Rows[0][0].ToString()
977-
}
978963

979-
// Get server UUID from second replica
980-
useStmt = fmt.Sprintf("USE `ks:-80@replica|%s`", instances["-80"]["replica"][1])
981-
utils.Exec(t, conn, useStmt)
982-
var uuid2 string
983-
for i := range 5 {
984-
result2 := utils.Exec(t, conn, "SELECT @@server_uuid")
985-
require.NotNil(t, result2)
986-
require.Greater(t, len(result2.Rows), 0)
987-
if i > 0 {
988-
// UUID should be the same across multiple queries to same tablet
989-
require.Equal(t, uuid2, result2.Rows[0][0].ToString())
990-
}
991-
uuid2 = result2.Rows[0][0].ToString()
964+
// Get server UUID from first replica
965+
useStmt = fmt.Sprintf("USE `ks:-80@replica|%s`", instances["-80"]["replica"][0])
966+
utils.Exec(t, conn, useStmt)
967+
var uuid1 string
968+
for i := range 5 {
969+
result1 := utils.Exec(t, conn, "SELECT @@server_uuid")
970+
require.NotNil(t, result1)
971+
require.Greater(t, len(result1.Rows), 0)
972+
if i > 0 {
973+
// UUID should be the same across multiple queries to same tablet
974+
require.Equal(t, uuid1, result1.Rows[0][0].ToString())
992975
}
976+
uuid1 = result1.Rows[0][0].ToString()
977+
}
993978

994-
// Server UUIDs should be different, proving we're targeting different tablets
995-
require.NotEqual(t, uuid1, uuid2, "different replicas should have different server UUIDs")
979+
// Get server UUID from second replica
980+
useStmt = fmt.Sprintf("USE `ks:-80@replica|%s`", instances["-80"]["replica"][1])
981+
utils.Exec(t, conn, useStmt)
982+
var uuid2 string
983+
for i := range 5 {
984+
result2 := utils.Exec(t, conn, "SELECT @@server_uuid")
985+
require.NotNil(t, result2)
986+
require.Greater(t, len(result2.Rows), 0)
987+
if i > 0 {
988+
// UUID should be the same across multiple queries to same tablet
989+
require.Equal(t, uuid2, result2.Rows[0][0].ToString())
990+
}
991+
uuid2 = result2.Rows[0][0].ToString()
996992
}
993+
994+
// Server UUIDs should be different, proving we're targeting different tablets
995+
require.NotEqual(t, uuid1, uuid2, "different replicas should have different server UUIDs")
997996
}
998997

999998
// TestDynamicConfig tests the dynamic configurations.

go/vt/vtgate/executorcontext/safe_session_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -222,20 +222,4 @@ func TestTargetTabletAlias(t *testing.T) {
222222
// Test: Clear (set to nil)
223223
session.SetTargetTabletAlias(nil)
224224
assert.Nil(t, session.GetTargetTabletAlias())
225-
226-
// Test: Thread safety - concurrent access
227-
done := make(chan bool)
228-
for i := 0; i < 100; i++ {
229-
go func(uid uint32) {
230-
testAlias := &topodatapb.TabletAlias{Cell: "cell", Uid: uid}
231-
session.SetTargetTabletAlias(testAlias)
232-
_ = session.GetTargetTabletAlias()
233-
done <- true
234-
}(uint32(i))
235-
}
236-
for i := 0; i < 100; i++ {
237-
<-done
238-
}
239-
// Just verify we didn't panic - the final value is non-deterministic
240-
assert.NotNil(t, session.GetTargetTabletAlias())
241225
}

go/vt/vtgate/executorcontext/vcursor_impl.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,13 +1041,18 @@ func (vc *VCursorImpl) SetTarget(target string) error {
10411041
if tabletAlias == nil {
10421042
vc.SafeSession.SetTargetTabletAlias(nil)
10431043
} else {
1044+
// Tablet targeting must be set before starting a transaction, not during.
1045+
// The feature is designed to pick a specific tablet and do work there,
1046+
// not to switch tablets mid-transaction.
1047+
if vc.SafeSession.InTransaction() {
1048+
return vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION,
1049+
"cannot set tablet target while in a transaction")
1050+
}
1051+
10441052
// Store tablet alias in session for routing
10451053
// Note: We don't validate the tablet exists here - if it doesn't exist or is
10461054
// unreachable, the query will fail during execution with a clear error message
10471055
vc.SafeSession.SetTargetTabletAlias(tabletAlias)
1048-
1049-
// Keep tabletType as determined by ParseDestination (defaultTabletType)
1050-
// The actual routing uses the tablet alias, so the type is only for VCursor state
10511056
}
10521057

10531058
if _, ok := vc.vschema.Keyspaces[keyspace]; !ignoreKeyspace(keyspace) && !ok {

go/vt/vtgate/scatter_conn.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,9 +903,15 @@ func actionInfo(ctx context.Context, target *querypb.Target, session *econtext.S
903903
info.alias = shardSession.TabletAlias
904904
info.rowsAffected = shardSession.RowsAffected
905905
}
906-
// Override alias if tablet-specific routing is set
906+
// Set tablet alias for routing if tablet-specific targeting is active
907907
if targetAlias := session.GetTargetTabletAlias(); targetAlias != nil {
908-
info.alias = targetAlias
908+
if info.alias == nil {
909+
info.alias = targetAlias
910+
} else if !proto.Equal(info.alias, targetAlias) {
911+
return nil, nil, vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION,
912+
"cannot change tablet target mid-transaction: session has %v, target is %v",
913+
topoproto.TabletAliasString(info.alias), topoproto.TabletAliasString(targetAlias))
914+
}
909915
}
910916
return info, shardSession, nil
911917
}

0 commit comments

Comments
 (0)