Skip to content

Commit

Permalink
api: make api similar to tnt vshard router
Browse files Browse the repository at this point in the history
* rename RouterRouteAll -> RouteAll, RouterRoute -> Route, RouterBucketIDStrCRC32 -> BucketIDStrCRC32.
* remove BucketResolve, BucketDiscovery, RouterBucketID, RouterBucketIDMPCRC32.
* update README.md, README_ru.md.
* ClusterBootstrap: eliminate direct access to r.idToReplicaset.
* remove unused Makefile target.
* replace github links to master branch with permalinks.
* remove discovery_test.go, move tests/tnt/discovery_test.go -> tarantool_test.go.
  • Loading branch information
nurzhan-saktaganov committed Jan 13, 2025
1 parent d0e58ea commit da5b71e
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 196 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
## Unreleased

BUG FIXES:

* ClusterBootstrap: eliminate direct access to r.idToReplicaset.

CHANGES:

* Instance UUID no more required, use instance name instead.
* Removed toolchain go1.23.3.
* Refactored GetTyped interface and logic. Now we use raw msg buffer instead raw messages. Interface works and looks
like go-tarantool response.
* ReplicaCall, RouterCallImpl, RouterMapCallRWImpl methods was removed cause it works invalid and looks useless.
* ReplicaCall, RouterCallImpl, RouterMapCallRWImpl, BucketResolve, BucketDiscovery, RouterBucketID, RouterBucketIDMPCRC32 methods were removed cause they work invalid, look useless or redundant.
* All PR, issue references in #XYZ format in commits older than 42f363775dfb9eaf7ec2a6ed7a999847752cec00 refer to https://github.com/KaymeKaydex/go-vshard-router.
* VshardRouterCallMode type renamed to CallMode for simplicity.
* StorageResultTypedFunc type removed as useless type.
* Updated msgpack version from v5.3.5 to v5.4.1.
* Replicaset identifier now is replicaset name instead uuid.
* Remove unused Makefile target.
* Update README.md, README_ru.md.
* Rename RouterRouteAll -> RouteAll, RouterRoute -> Route, RouterBucketIDStrCRC32 -> BucketIDStrCRC32.
* Replace github links to master branch with permalinks.

TESTS:

Expand All @@ -21,6 +29,7 @@ TESTS:
* TestRouterCallProto rewrote.
* Start using constants in tarantool_test.go instead duplicate variables.
* TestRouterMapCall moved to tarantool_test.go and renamed to TestRouter_RouterMapCallRWImpl.
* Remove discovery_test.go, move tests/tnt/discovery_test.go -> tarantool_test.go

## v1.3.2

Expand Down
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ test: prepare-tnt
cover: test ## Generate and open the HTML report for test coverage.
$(GO_CMD) tool cover -html=coverage.out

test/integration:
@$(MAKE) -C ./tests/integration test

generate/mocks:
mockery --name=Pool --case=underscore --output=mocks/pool --outpkg=mockpool # need fix it later
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func main() {
ID: 123,
}

bucketID := vshardrouter.BucketIDStrCRC32(strconv.FormatUint(user.ID, 10), directRouter.RouterBucketCount())
bucketID := directRouter.BucketIDStrCRC32(strconv.FormatUint(user.ID, 10))

resp, err := directRouter.Call(
ctx,
Expand Down
2 changes: 1 addition & 1 deletion README_ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func main() {
ID: 123,
}

bucketID := vshardrouter.BucketIDStrCRC32(strconv.FormatUint(user.ID, 10), directRouter.RouterBucketCount())
bucketID := directRouter.BucketIDStrCRC32(strconv.FormatUint(user.ID, 10))

resp, err := directRouter.Call(
ctx,
Expand Down
12 changes: 3 additions & 9 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (r *Router) Call(ctx context.Context, bucketID uint64, mode CallMode,

var rs *Replicaset

rs, err = r.BucketResolve(ctx, bucketID)
rs, err = r.Route(ctx, bucketID)
if err != nil {
r.metrics().RetryOnCall("bucket_resolve_error")

Expand Down Expand Up @@ -657,14 +657,8 @@ func RouterMapCallRW[T any](r *Router, ctx context.Context,
return nameToResult, nil
}

// RouterRoute get replicaset object by bucket identifier.
// alias to BucketResolve
func (r *Router) RouterRoute(ctx context.Context, bucketID uint64) (*Replicaset, error) {
return r.BucketResolve(ctx, bucketID)
}

// RouterRouteAll return map of all replicasets.
func (r *Router) RouterRouteAll() map[string]*Replicaset {
// RouteAll return map of all replicasets.
func (r *Router) RouteAll() map[string]*Replicaset {
nameToReplicasetRef := r.getNameToReplicaset()

// Do not expose the original map to prevent unauthorized modification.
Expand Down
2 changes: 1 addition & 1 deletion api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func TestVshardMode_String_NotEmpty(t *testing.T) {

func TestRouter_RouterRouteAll(t *testing.T) {
t.Parallel()
m := emptyRouter.RouterRouteAll()
m := emptyRouter.RouteAll()
require.Empty(t, m)
}
11 changes: 3 additions & 8 deletions discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
DiscoveryModeOnce
)

// BucketsSearchMode a type, that used to define policy for BucketDiscovery method.
// BucketsSearchMode a type, that used to define policy for Router.Route method.
// See type Config for further details.
type BucketsSearchMode int

Expand All @@ -45,8 +45,8 @@ const (
BucketsSearchBatchedFull
)

// BucketDiscovery search bucket in whole cluster
func (r *Router) BucketDiscovery(ctx context.Context, bucketID uint64) (*Replicaset, error) {
// Route get replicaset object by bucket identifier.
func (r *Router) Route(ctx context.Context, bucketID uint64) (*Replicaset, error) {
if bucketID < 1 || r.cfg.TotalBucketCount < bucketID {
return nil, fmt.Errorf("bucket id is out of range: %d (total %d)", bucketID, r.cfg.TotalBucketCount)
}
Expand Down Expand Up @@ -182,11 +182,6 @@ func (r *Router) bucketSearchBatched(ctx context.Context, bucketIDToFind uint64)
return rs, nil
}

// BucketResolve resolve bucket id to replicaset
func (r *Router) BucketResolve(ctx context.Context, bucketID uint64) (*Replicaset, error) {
return r.BucketDiscovery(ctx, bucketID)
}

// DiscoveryHandleBuckets arrange downloaded buckets to the route map so as they reference a given replicaset.
func (r *Router) DiscoveryHandleBuckets(ctx context.Context, rs *Replicaset, buckets []uint64) {
view := r.getConsistentView()
Expand Down
26 changes: 0 additions & 26 deletions discovery_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions examples/customer/go-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (c *controller) CustomerAddHandler(w http.ResponseWriter, r *http.Request)
return
}

bucketID := c.router.RouterBucketIDStrCRC32(strconv.Itoa(req.CustomerID))
bucketID := c.router.BucketIDStrCRC32(strconv.Itoa(req.CustomerID))

req.BucketId = bucketID

Expand Down Expand Up @@ -164,7 +164,7 @@ func (c *controller) CustomerLookupHandler(w http.ResponseWriter, r *http.Reques
return
}

bucketID := c.router.RouterBucketIDStrCRC32(customerID)
bucketID := c.router.BucketIDStrCRC32(customerID)
resp, err := c.router.Call(ctx, bucketID, vshardrouter.CallModeBRO, "customer_lookup", []interface{}{csID}, vshardrouter.CallOpts{
Timeout: time.Minute,
})
Expand Down
2 changes: 1 addition & 1 deletion replicaset.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (rs *Replicaset) bucketsDiscovery(ctx context.Context, from uint64) (bucket
// At each iteration, the algorithm either concludes or disregards at least
// one new overloaded replicaset. Therefore, its time complexity is O(N^2),
// where N is the number of replicasets.
// based on https://github.com/tarantool/vshard/blob/master/vshard/replicaset.lua#L1358
// based on https://github.com/tarantool/vshard/blob/99ceaee014ea3a67424c2026545838e08d69b90c/vshard/replicaset.lua#L1358
func CalculateEtalonBalance(replicasets []Replicaset, bucketCount uint64) error {
isBalanceFound := false
weightSum := 0.0
Expand Down
73 changes: 66 additions & 7 deletions tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestRouter_ClusterBootstrap(t *testing.T) {

err = router.ClusterBootstrap(ctx, false)
require.NoError(t, err)
for _, rs := range router.RouterRouteAll() {
for _, rs := range router.RouteAll() {
count, err := rs.BucketsCount(ctx)
require.NoError(t, err)
require.NotEqual(t, count, uint64(0))
Expand Down Expand Up @@ -262,8 +262,8 @@ func TestRouter_Call(t *testing.T) {

bucketID := randBucketID(totalBucketCount)

rs, err := router.BucketResolve(ctx, bucketID)
require.NoError(t, err, "BucketResolve with no err")
rs, err := router.Route(ctx, bucketID)
require.NoError(t, err, "router.Route with no err")

t.Run("proto test", func(t *testing.T) {
const maxRespLen = 3
Expand Down Expand Up @@ -334,7 +334,7 @@ func TestRouter_Call(t *testing.T) {
})

t.Run("router.Call simulate vshard error", func(t *testing.T) {
rsMap := router.RouterRouteAll()
rsMap := router.RouteAll()

// 1. Replace replicaset for bucketID
for k, v := range rsMap {
Expand Down Expand Up @@ -388,7 +388,7 @@ func BenchmarkCallSimpleInsert_GO_Call(b *testing.B) {
for i := 0; i < b.N; i++ {
id := uuid.New()

bucketID := router.RouterBucketIDStrCRC32(id.String())
bucketID := router.BucketIDStrCRC32(id.String())
_, err := router.Call(
ctx,
bucketID,
Expand Down Expand Up @@ -425,7 +425,7 @@ func BenchmarkCallSimpleSelect_GO_Call(b *testing.B) {
id := uuid.New()
ids[i] = id

bucketID := router.RouterBucketIDStrCRC32(id.String())
bucketID := router.BucketIDStrCRC32(id.String())
_, err := router.Call(
ctx,
bucketID,
Expand All @@ -445,7 +445,7 @@ func BenchmarkCallSimpleSelect_GO_Call(b *testing.B) {
for i := 0; i < b.N; i++ {
id := ids[i]

bucketID := router.RouterBucketIDStrCRC32(id.String())
bucketID := router.BucketIDStrCRC32(id.String())
resp, err1 := router.Call(
ctx,
bucketID,
Expand Down Expand Up @@ -552,3 +552,62 @@ func TestRouter_RouterMapCallRW(t *testing.T) {
_, err = vshardrouter.RouterMapCallRW[interface{}](router, ctx, "echo", echoArgs, callOpts)
require.NotNilf(t, err, "RouterMapCallRWImpl failed on not full cluster")
}

func TestRouterRoute(t *testing.T) {
t.Parallel()

var modes = []vshardrouter.BucketsSearchMode{
vshardrouter.BucketsSearchLegacy,
vshardrouter.BucketsSearchBatchedQuick,
vshardrouter.BucketsSearchBatchedFull,
}

for _, mode := range modes {
testRouterRouteWithMode(t, mode)
}
}

func testRouterRouteWithMode(t *testing.T, searchMode vshardrouter.BucketsSearchMode) {
ctx := context.Background()

router, err := vshardrouter.NewRouter(ctx, vshardrouter.Config{
TopologyProvider: static.NewProvider(topology),
DiscoveryTimeout: 5 * time.Second,
DiscoveryMode: vshardrouter.DiscoveryModeOn,
BucketsSearchMode: searchMode,
TotalBucketCount: totalBucketCount,
User: username,
})

require.Nilf(t, err, "NewRouter finished successfully, mode %v", searchMode)

_, err = router.Route(ctx, totalBucketCount+1)
require.Error(t, err, "invalid bucketID")

// pick some random bucket
bucketID := randBucketID(totalBucketCount)

// clean everything
router.RouteMapClean()

// resolve it
rs, err := router.Route(ctx, bucketID)
require.Nilf(t, err, "router.Route ok, mode %v", searchMode)

// reset it again
router.BucketReset(bucketID)

// call RouteAll, because:
// 1. increase coverage
// 2. we cannot get replicaset uuid by rs instance (lack of interface)
rsMap := router.RouteAll()
for k, v := range rsMap {
if v == rs {
// set it again
res, err := router.BucketSet(bucketID, k)
require.Nil(t, err, nil, "BucketSet ok")
require.Equal(t, res, rs, "BucketSet res ok")
break
}
}
}
2 changes: 1 addition & 1 deletion tests/tnt/call_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func BenchmarkCallSimpleSelect_Lua(b *testing.B) {
id := uuid.New()
ids[i] = id

bucketID := router.RouterBucketIDStrCRC32(id.String())
bucketID := router.BucketIDStrCRC32(id.String())
_, err := router.Call(
ctx,
bucketID,
Expand Down
72 changes: 0 additions & 72 deletions tests/tnt/discovery_test.go

This file was deleted.

Loading

0 comments on commit da5b71e

Please sign in to comment.