-
Notifications
You must be signed in to change notification settings - Fork 2.4k
[vtctld] Migrate ShardReplicationPositions #7690
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
Merged
deepthi
merged 7 commits into
vitessio:master
from
tinyspeck:am_vtctld_shard_replication_positions
Mar 18, 2021
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3c8dfe3
Add RPC for ShardReplicationPositions, generate and add method stub
6464539
Implement `ShardReplicationPositions` with documentation and tests
8c11584
Add TabletMap to proto response to support tablet sorting in CLI
41bd6e4
Update impl/tests to include tablet map in response
4de4987
Add CLI entrypoint for `ShardReplicationPositions`
698debf
Remiplement the old `ShardReplicationPositions` vtctl command with ne…
6e0edb6
PR feedback: whitespace and naming
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 hidden or 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 |
|---|---|---|
|
|
@@ -17,8 +17,13 @@ limitations under the License. | |
| package cli | ||
|
|
||
| import ( | ||
| "sort" | ||
|
|
||
| "vitess.io/vitess/go/mysql" | ||
| "vitess.io/vitess/go/vt/topo/topoproto" | ||
|
|
||
| replicationdatapb "vitess.io/vitess/go/vt/proto/replicationdata" | ||
| topodatapb "vitess.io/vitess/go/vt/proto/topodata" | ||
| vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata" | ||
| ) | ||
|
|
||
|
|
@@ -41,3 +46,78 @@ func ParseKeyspaceShards(args []string) ([]*vtctldatapb.Shard, error) { | |
|
|
||
| return shards, nil | ||
| } | ||
|
|
||
| // ReplicatingTablet is a struct to group a Tablet together with its replication | ||
| // Status. | ||
| type ReplicatingTablet struct { | ||
| Status *replicationdatapb.Status | ||
| Tablet *topodatapb.Tablet | ||
| } | ||
|
|
||
| type rTablets []*ReplicatingTablet | ||
|
|
||
| func (rts rTablets) Len() int { return len(rts) } | ||
| func (rts rTablets) Swap(i, j int) { rts[i], rts[j] = rts[j], rts[i] } | ||
| func (rts rTablets) Less(i, j int) bool { | ||
| l, r := rts[i], rts[j] | ||
|
|
||
| // l or r ReplicationStatus would be nil if we failed to get | ||
| // the position (put them at the beginning of the list) | ||
| if l.Status == nil { | ||
| return r.Status != nil | ||
| } | ||
|
|
||
| if r.Status == nil { | ||
| return false | ||
| } | ||
|
|
||
| // the type proto has MASTER first, so sort by that. Will show | ||
| // the MASTER first, then each replica type sorted by | ||
| // replication position. | ||
| if l.Tablet.Type < r.Tablet.Type { | ||
| return true | ||
| } | ||
|
|
||
| if l.Tablet.Type > r.Tablet.Type { | ||
| return false | ||
| } | ||
|
|
||
| // then compare replication positions | ||
| lpos, err := mysql.DecodePosition(l.Status.Position) | ||
| if err != nil { | ||
| return true | ||
| } | ||
|
|
||
| rpos, err := mysql.DecodePosition(r.Status.Position) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| return !lpos.AtLeast(rpos) | ||
| } | ||
|
|
||
| // SortReplicatingTablets returns a sorted list of replicating tablets (which is | ||
| // a struct grouping a Tablet together with its replication Status). | ||
| // | ||
| // The sorting order is: | ||
| // 1. Tablets that do not have a replication Status. | ||
| // 2. Any tablets of type MASTER. | ||
| // 3. Remaining tablets sorted by comparing replication positions. | ||
| func SortReplicatingTablets(tabletMap map[string]*topodatapb.Tablet, replicationStatuses map[string]*replicationdatapb.Status) []*ReplicatingTablet { | ||
|
Contributor
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. Maybe call this |
||
| rtablets := make([]*ReplicatingTablet, 0, len(tabletMap)) | ||
|
|
||
| for alias, tablet := range tabletMap { | ||
| if status, ok := replicationStatuses[alias]; ok { | ||
| rtablets = append(rtablets, &ReplicatingTablet{ | ||
| Status: status, | ||
| Tablet: tablet, | ||
| }) | ||
| } else { | ||
| rtablets = append(rtablets, &ReplicatingTablet{Tablet: tablet}) | ||
| } | ||
| } | ||
|
|
||
| sort.Sort(rTablets(rtablets)) | ||
|
|
||
| return rtablets | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
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.
Did you consider writing it like this?
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.
I did, and decided not to for a silly reason. Basically I was worried "what if Tablet and Status each have a field with the same name, you wouldn't be able to tell the difference" but now I remember that Go will just force you to disambiguate so it's actually not a problem at all.
I'll make this change!