-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Avoid creating MoveTables
in case of non-empty target tables
#16826
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1011,3 +1011,73 @@ func applyTargetShards(ts *trafficSwitcher, targetShards []string) error { | |
} | ||
return nil | ||
} | ||
|
||
// validateEmptyTables checks if all specified tables in the keyspace are empty across all shards. | ||
// It queries each shard's primary tablet and if any non-empty table is found, it returns an error | ||
// containing a list of non-empty tables. | ||
func validateEmptyTables(ctx context.Context, ts *topo.Server, tmc tmclient.TabletManagerClient, keyspace string, tables []string) error { | ||
rohit-nayak-ps marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if len(tables) == 0 { | ||
return nil | ||
} | ||
|
||
shards, err := ts.GetServingShards(ctx, keyspace) | ||
if err != nil { | ||
return err | ||
} | ||
if len(shards) == 0 { | ||
return fmt.Errorf("keyspace %s has no shards", keyspace) | ||
} | ||
|
||
var selectQueries []string | ||
for _, t := range tables { | ||
selectQueries = append(selectQueries, fmt.Sprintf("(select '%s' from %s limit 1)", t, t)) | ||
} | ||
query := strings.Join(selectQueries, "union all") | ||
Comment on lines
+1032
to
+1035
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need a way to handle cases where there are many tables as the number of tables is unbounded. Two factors we'll have to consider:
In either case, the user is stuck and there is no outlet. At least unless we add a new flag that skips this test / allows for tables with existing data OR splitting the workflow up (which itself might be a bit daunting to manually specify hundreds or thousands of tables). |
||
|
||
var mu sync.Mutex | ||
isFaultyTable := map[string]bool{} | ||
|
||
err = forAllShards(shards, func(shard *topo.ShardInfo) error { | ||
primary := shard.PrimaryAlias | ||
if primary == nil { | ||
return fmt.Errorf("shard does not have a primary: %v", shard.ShardName()) | ||
} | ||
|
||
ti, err := ts.GetTablet(ctx, primary) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
res, err := tmc.ExecuteFetchAsDba(ctx, ti.Tablet, true, &tabletmanagerdatapb.ExecuteFetchAsDbaRequest{ | ||
Query: []byte(query), | ||
MaxRows: uint64(len(tables)), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
mu.Lock() | ||
for _, row := range res.Rows { | ||
isFaultyTable[string(row.Values)] = true | ||
} | ||
mu.Unlock() | ||
|
||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
faultyTables := make([]string, len(isFaultyTable)) | ||
i := 0 | ||
for table := range isFaultyTable { | ||
faultyTables[i] = table | ||
i++ | ||
} | ||
|
||
if len(faultyTables) > 0 { | ||
return fmt.Errorf("target keyspace contains following non-empty table(s): %s", strings.Join(faultyTables, ", ")) | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -480,8 +480,8 @@ func (tmc *fakeTMClient) VReplicationExec(ctx context.Context, tablet *topodatap | |
} | ||
for qry, res := range tmc.vreQueries[int(tablet.Alias.Uid)] { | ||
if strings.HasPrefix(qry, "/") { | ||
re := regexp.MustCompile(qry) | ||
if re.MatchString(qry) { | ||
re := regexp.MustCompile(qry[1:]) | ||
if re.MatchString(query) { | ||
Comment on lines
-483
to
+484
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was probably a mistake. |
||
return res, nil | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can instead use
maps.Keys(hasTargetTable)
withvalidateEmptyTables
.