Skip to content
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

[Feature]: Sort results of getTablets for consistency in output/readability #17771

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions go/vt/vtctl/grpcvtctldserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2326,6 +2326,10 @@ func (s *VtctldServer) GetTablets(ctx context.Context, req *vtctldatapb.GetTable
tablets = append(tablets, ti.Tablet)
}

// Sort the list of tablets alphabetically by alias to improve readability of output.
sort.Slice(tablets, func(i, j int) bool {
return topoproto.TabletAliasString(tablets[i].Alias) < topoproto.TabletAliasString(tablets[j].Alias)
})
return &vtctldatapb.GetTabletsResponse{Tablets: tablets}, nil
}

Expand Down Expand Up @@ -2414,6 +2418,10 @@ func (s *VtctldServer) GetTablets(ctx context.Context, req *vtctldatapb.GetTable

adjustedTablets[i] = ti.Tablet
}
// Sort the list of tablets alphabetically by alias to improve readability of output.
sort.Slice(adjustedTablets, func(i, j int) bool {
return topoproto.TabletAliasString(adjustedTablets[i].Alias) < topoproto.TabletAliasString(adjustedTablets[j].Alias)
})

return &vtctldatapb.GetTabletsResponse{
Tablets: adjustedTablets,
Expand Down
6 changes: 3 additions & 3 deletions go/vt/vtctl/grpcvtctldserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4605,7 +4605,7 @@ func TestDeleteTablets(t *testing.T) {

resp, err := vtctld.GetTablets(ctx, &vtctldatapb.GetTabletsRequest{})
assert.NoError(t, err, "cannot look up tablets from topo after issuing DeleteTablets request")
testutil.AssertSameTablets(t, tt.expectedRemainingTablets, resp.Tablets)
utils.MustMatch(t, tt.expectedRemainingTablets, resp.Tablets)
}

// Run the test
Expand Down Expand Up @@ -8277,7 +8277,7 @@ func TestGetTablets(t *testing.T) {
}

assert.NoError(t, err)
testutil.AssertSameTablets(t, tt.expected, resp.Tablets)
utils.MustMatch(t, tt.expected, resp.Tablets)
})
}
}
Expand Down Expand Up @@ -13338,7 +13338,7 @@ func TestTabletExternallyReparented(t *testing.T) {

resp, err := vtctld.GetTablets(ctx, &vtctldatapb.GetTabletsRequest{})
require.NoError(t, err, "cannot get all tablets in the topo")
testutil.AssertSameTablets(t, tt.expectedTopo, resp.Tablets)
utils.MustMatch(t, tt.expectedTopo, resp.Tablets)
}()
}

Expand Down
13 changes: 0 additions & 13 deletions go/vt/vtctl/grpcvtctldserver/testutil/proto_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ package testutil

import (
"encoding/json"
"fmt"
"sort"
"testing"

"github.com/stretchr/testify/assert"

"vitess.io/vitess/go/test/utils"
logutilpb "vitess.io/vitess/go/vt/proto/logutil"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
)

Expand Down Expand Up @@ -108,16 +105,6 @@ func AssertPlannedReparentShardResponsesEqual(t *testing.T, expected *vtctldatap
utils.MustMatch(t, expected, actual)
}

func AssertSameTablets(t *testing.T, expected, actual []*topodatapb.Tablet) {
sort.Slice(expected, func(i, j int) bool {
return fmt.Sprintf("%v", expected[i]) < fmt.Sprintf("%v", expected[j])
})
sort.Slice(actual, func(i, j int) bool {
return fmt.Sprintf("%v", actual[i]) < fmt.Sprintf("%v", actual[j])
})
utils.MustMatch(t, expected, actual)
}

// AssertKeyspacesEqual is a convenience function to assert that two
// vtctldatapb.Keyspace objects are equal, after clearing out any reserved
// proto XXX_ fields.
Expand Down
Loading