Skip to content

Commit

Permalink
[Feature]: Sort results of getTablets for consistency in output/reada…
Browse files Browse the repository at this point in the history
…bility (#17771)

Signed-off-by: Lucas Morduchowicz <[email protected]>
Co-authored-by: Lucas Morduchowicz <[email protected]>
  • Loading branch information
lmorduch and Lucas Morduchowicz authored Feb 13, 2025
1 parent 057bcc9 commit 000fbbe
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
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

0 comments on commit 000fbbe

Please sign in to comment.