Skip to content

Commit

Permalink
Merge pull request #10 from tarantool/ignore-uuid
Browse files Browse the repository at this point in the history
Ignore instance uuid field
  • Loading branch information
KaymeKaydex authored Jan 10, 2025
2 parents e96ec9f + f18b2ce commit 52f4c34
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

CHANGES:
* Instance UUID no more required, use instance name instead.
* Removed toolchain go1.23.3.

## v1.3.2

CHANGES:
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module github.com/tarantool/go-vshard-router

go 1.22

toolchain go1.23.3

require (
github.com/google/uuid v1.6.0
github.com/snksoft/crc v1.1.0
Expand Down
42 changes: 42 additions & 0 deletions tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,35 @@ var topology = map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo{
},
}

var noUUIDTopology = map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo{
{
Name: "storage_1",
Weight: 1,
}: {
{
Name: "storage_1_a",
Addr: "127.0.0.1:3301",
},
{
Name: "storage_1_b",
Addr: "127.0.0.1:3302",
},
},
{
Name: "storage_2",
Weight: 1,
}: {
{
Name: "storage_2_a",
Addr: "127.0.0.1:3303",
},
{
Name: "storage_2_b",
Addr: "127.0.0.1:3304",
},
},
}

func runTestMain(m *testing.M) int {
dialers := make([]tarantool.NetDialer, instancesCount)
opts := make([]test_helpers.StartOpts, instancesCount)
Expand Down Expand Up @@ -157,3 +186,16 @@ func TestRouter_RouterCallImpl_Decoding(t *testing.T) {

require.NoError(t, err)
}

// for tarantool 3.0 uuid is not required
func TestNewRouter_IgnoreUUID(t *testing.T) {
ctx := context.Background()

_, err := vshardrouter.NewRouter(ctx, vshardrouter.Config{
TotalBucketCount: 100,
TopologyProvider: static.NewProvider(noUUIDTopology),
User: "guest",
})

require.NoError(t, err)
}
2 changes: 2 additions & 0 deletions tests/tnt/cfgmaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ func (c cfgmaker) clusterCfg() map[vshardrouter.ReplicasetInfo][]vshardrouter.In
Name: fmt.Sprintf("replicaset_%d", rsID),
UUID: c.replicasetUUID(rsID),
}] = []vshardrouter.InstanceInfo{{
Name: "inst_1",
Addr: c.masterAddr(rsID),
UUID: c.masterUUID(rsID),
}, {
Name: "inst_2",
Addr: c.followerAddr(rsID),
UUID: c.followerUUID(rsID),
}}
Expand Down
2 changes: 1 addition & 1 deletion tests/tnt/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestTopology(t *testing.T) {
require.Nil(t, err, "AddReplicasets finished successfully")

// remove some random instance
err = router.RemoveInstance(ctx, rsInfo.UUID, insInfo.UUID)
err = router.RemoveInstance(ctx, rsInfo.UUID, insInfo.Name)
require.Nil(t, err, "RemoveInstance finished successfully")

// add it again
Expand Down
27 changes: 21 additions & 6 deletions topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
type TopologyController interface {
AddInstance(ctx context.Context, rsID uuid.UUID, info InstanceInfo) error
RemoveReplicaset(ctx context.Context, rsID uuid.UUID) []error
RemoveInstance(ctx context.Context, rsID, instanceID uuid.UUID) error
RemoveInstance(ctx context.Context, rsID uuid.UUID, instanceName string) error
AddReplicaset(ctx context.Context, rsInfo ReplicasetInfo, instances []InstanceInfo) error
AddReplicasets(ctx context.Context, replicasets map[ReplicasetInfo][]InstanceInfo) error
}
Expand Down Expand Up @@ -53,7 +53,7 @@ func (r *Router) AddInstance(ctx context.Context, rsID uuid.UUID, info InstanceI
}

instance := pool.Instance{
Name: info.UUID.String(),
Name: info.Name,
Dialer: tarantool.NetDialer{
Address: info.Addr,
User: r.cfg.User,
Expand All @@ -72,8 +72,8 @@ func (r *Router) AddInstance(ctx context.Context, rsID uuid.UUID, info InstanceI
return rs.conn.Add(ctx, instance)
}

func (r *Router) RemoveInstance(ctx context.Context, rsID, instanceID uuid.UUID) error {
r.log().Debugf(ctx, "Trying to remove instance %s from router topology in rs %s", instanceID, rsID)
func (r *Router) RemoveInstance(ctx context.Context, rsID uuid.UUID, instanceName string) error {
r.log().Debugf(ctx, "Trying to remove instance %s from router topology in rs %s", instanceName, rsID)

idToReplicasetRef := r.getIDToReplicaset()

Expand All @@ -82,14 +82,29 @@ func (r *Router) RemoveInstance(ctx context.Context, rsID, instanceID uuid.UUID)
return ErrReplicasetNotExists
}

return rs.conn.Remove(instanceID.String())
return rs.conn.Remove(instanceName)
}

func (r *Router) AddReplicaset(ctx context.Context, rsInfo ReplicasetInfo, instances []InstanceInfo) error {
r.log().Debugf(ctx, "Trying to add replicaset %s to router topology", rsInfo)

idToReplicasetOld := r.getIDToReplicaset()

// tarantool 3+ configuration does not require uuid
if rsInfo.UUID == uuid.Nil {
// check that such replicaset does not exist
for _, rs := range idToReplicasetOld {
if rs.info.Name == rsInfo.Name {
return ErrReplicasetExists
}
}

// we just mock this uuid value
rsInfo.UUID = uuid.New()

r.log().Warnf(ctx, "replicaset %s is assigned uuid %s; this is a temporary need to migrate from uuid to names", rsInfo.Name, rsInfo.UUID)
}

if _, ok := idToReplicasetOld[rsInfo.UUID]; ok {
return ErrReplicasetExists
}
Expand All @@ -101,7 +116,7 @@ func (r *Router) AddReplicaset(ctx context.Context, rsInfo ReplicasetInfo, insta
rsInstances := make([]pool.Instance, 0, len(instances))
for _, instance := range instances {
rsInstances = append(rsInstances, pool.Instance{
Name: instance.UUID.String(),
Name: instance.Name,
Dialer: tarantool.NetDialer{
Address: instance.Addr,
User: r.cfg.User,
Expand Down
4 changes: 2 additions & 2 deletions topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestController_AddInstance(t *testing.T) {

err := router.Topology().AddInstance(ctx, uuid.New(), InstanceInfo{
Addr: "127.0.0.1:8060",
UUID: uuid.New(),
Name: "instance_001",
})
require.True(t, errors.Is(err, ErrReplicasetNotExists))
})
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestController_RemoveInstance(t *testing.T) {
},
}

err := router.Topology().RemoveInstance(ctx, uuid.New(), uuid.New())
err := router.Topology().RemoveInstance(ctx, uuid.New(), "")
require.True(t, errors.Is(err, ErrReplicasetNotExists))
})
}
Expand Down
25 changes: 20 additions & 5 deletions vshard.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,37 @@ func (bsi *BucketStatInfo) DecodeMsgpack(d *msgpack.Decoder) error {
return nil
}

// InstanceInfo represents the information about an instance.
// This struct holds the necessary details such as the name, address, and UUID of the instance.
type InstanceInfo struct {
// Name is human-readable id for instance
// Starting with tarantool 3.0, the definition is made into a human-readable name,
// so far it is not used directly inside the library
// Name is a required field for the instance.
// Starting with Tarantool 3.0, this definition is made into a human-readable name,
// and it is now mandatory for use in the library.
// The Name should be a unique identifier for the instance.
Name string

// Addr specifies the address of the instance.
// This can be an IP address or a domain name, depending on how the instance is accessed.
// It is necessary for connecting to the instance or identifying its location.
Addr string

// UUID is an optional field that provides a globally unique identifier (UUID) for the instance.
// While this information is not mandatory, it can be useful for internal management or tracking purposes.
// The UUID ensures that each instance can be identified uniquely, but it is not required for basic operations.
UUID uuid.UUID
}

func (ii InstanceInfo) String() string {
if ii.UUID == uuid.Nil {
return fmt.Sprintf("{name: %s, addr: %s}", ii.Name, ii.Addr)
}

return fmt.Sprintf("{name: %s, uuid: %s, addr: %s}", ii.Name, ii.UUID, ii.Addr)
}

func (ii InstanceInfo) Validate() error {
if ii.UUID == uuid.Nil {
return fmt.Errorf("%w: empty uuid", ErrInvalidInstanceInfo)
if ii.Name == "" {
return fmt.Errorf("%w: empty name", ErrInvalidInstanceInfo)
}

if ii.Addr == "" {
Expand Down
10 changes: 5 additions & 5 deletions vshard_shadow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ func TestInstanceInfo_Validate(t *testing.T) {
Valid: false,
},
{
Name: "no uuid",
II: vshardrouter.InstanceInfo{Addr: "first.internal:1212"},
Valid: false,
Name: "no uuid - ok",
II: vshardrouter.InstanceInfo{Addr: "first.internal:1212", Name: "instance_123"},
Valid: true,
},
{
Name: "no addr",
II: vshardrouter.InstanceInfo{UUID: uuid.New()},
Valid: false,
},
{
Name: "ok",
Name: "no instance name",
II: vshardrouter.InstanceInfo{UUID: uuid.New(), Addr: "first.internal:1212"},
Valid: true,
Valid: false,
},
}

Expand Down

0 comments on commit 52f4c34

Please sign in to comment.