-
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
Add -rename_tables
to DropSources
#6383
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,27 @@ const ( | |
DirectionBackward | ||
) | ||
|
||
// TableRemovalType specifies the way the a table will be removed | ||
type TableRemovalType int | ||
|
||
// The following consts define if DropSource will drop or rename the table | ||
const ( | ||
DropTable = TableRemovalType(iota) | ||
RenameTable | ||
) | ||
|
||
func (trt TableRemovalType) String() string { | ||
types := [...]string{ | ||
"DROP TABLE", | ||
"RENAME TABLE", | ||
} | ||
if trt < DropTable || trt > RenameTable { | ||
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. following the above if t, ok := tableRemovalTypeMap[trt] ; ok {
return t
}
return "Unknown" |
||
return "Unknown" | ||
} | ||
|
||
return types[trt] | ||
} | ||
|
||
// accessType specifies the type of access for a shard (allow/disallow writes). | ||
type accessType int | ||
|
||
|
@@ -293,7 +314,7 @@ func (wr *Wrangler) SwitchWrites(ctx context.Context, targetKeyspace, workflow s | |
} | ||
|
||
// DropSources cleans up source tables, shards and blacklisted tables after a MoveTables/Reshard is completed | ||
func (wr *Wrangler) DropSources(ctx context.Context, targetKeyspace, workflow string, dryRun bool) (*[]string, error) { | ||
func (wr *Wrangler) DropSources(ctx context.Context, targetKeyspace, workflow string, removalType TableRemovalType, dryRun bool) (*[]string, error) { | ||
ts, err := wr.buildTrafficSwitcher(ctx, targetKeyspace, workflow) | ||
if err != nil { | ||
wr.Logger().Errorf("buildTrafficSwitcher failed: %v", err) | ||
|
@@ -328,7 +349,7 @@ func (wr *Wrangler) DropSources(ctx context.Context, targetKeyspace, workflow st | |
} | ||
switch ts.migrationType { | ||
case binlogdatapb.MigrationType_TABLES: | ||
if err := sw.dropSourceTables(ctx); err != nil { | ||
if err := sw.removeSourceTables(ctx, removalType); err != nil { | ||
return nil, err | ||
} | ||
if err := sw.dropSourceBlacklistedTables(ctx); err != nil { | ||
|
@@ -1122,17 +1143,23 @@ func doValidateWorkflowHasCompleted(ctx context.Context, ts *trafficSwitcher) er | |
|
||
} | ||
|
||
func (ts *trafficSwitcher) dropSourceTables(ctx context.Context) error { | ||
func (ts *trafficSwitcher) removeSourceTables(ctx context.Context, removalType TableRemovalType) error { | ||
return ts.forAllSources(func(source *tsSource) error { | ||
for _, tableName := range ts.tables { | ||
ts.wr.Logger().Infof("Dropping table %s.%s\n", source.master.DbName(), tableName) | ||
query := fmt.Sprintf("drop table %s.%s", source.master.DbName(), tableName) | ||
if removalType == DropTable { | ||
ts.wr.Logger().Infof("Dropping table %s.%s\n", source.master.DbName(), tableName) | ||
} else { | ||
renameName := fmt.Sprintf("_%.63s", tableName) | ||
ts.wr.Logger().Infof("Renaming table %s.%s to %s.%s\n", source.master.DbName(), tableName, source.master.DbName(), renameName) | ||
query = fmt.Sprintf("rename table %s.%s TO %s.%s", source.master.DbName(), tableName, source.master.DbName(), renameName) | ||
} | ||
_, err := ts.wr.ExecuteFetchAsDba(ctx, source.master.Alias, query, 1, false, true) | ||
if err != nil { | ||
ts.wr.Logger().Errorf("Error dropping table %s: %v", tableName, err) | ||
ts.wr.Logger().Errorf("Error removing table %s: %v", tableName, err) | ||
return err | ||
} | ||
ts.wr.Logger().Infof("Dropped table %s.%s\n", source.master.DbName(), tableName) | ||
ts.wr.Logger().Infof("Removed table %s.%s\n", source.master.DbName(), tableName) | ||
|
||
} | ||
return nil | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Worth extracting the above code outside the method, since it always generates the same. Also, suggesting to make it a map, such that e.g.
types[DropTable]="DROP TABLE"
.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.
@shlomi-noach from a logical perspective, it makes total sense to move the code outside a method that gets repeatedly called. However, I don't understand what you mean by "make it a map". I mean, I could figure out how, but I don't get why.
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.
@tomkrouper yeah, so I believe it would be a more idiomatic way; for example, right now the code runs
if trt < DropTable || trt > RenameTable {
, but, if we add a new, third option, then we'd need to changetrt > RenameTable
totrt > SomethingNew
; where in fact we don't care about ranges, but of existence, and that's where maps are idiomatic and their use is widespread.So, if we have:
we can then:
and that answers "is trt known or unknown to us".
Anyway, that's the why the way I understand it, but I may be just biased towards my own habits.
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.
Makes total sense. Thanks.