From a5344ac38445c12f490404f926e69df93d7f7340 Mon Sep 17 00:00:00 2001 From: Konovalov Maxim <43151027+KaymeKaydex@users.noreply.github.com> Date: Sat, 28 Sep 2024 10:35:11 +0300 Subject: [PATCH] add logs about replicaset Topology changes (#89) * add logs about replicaset Topology changes * add empty loggers for unit testing routers * fixed changelogs --------- Co-authored-by: Maksim Konovalov --- CHANGELOG.md | 8 ++++++-- examples/customer/go-service/go.mod | 2 +- examples/customer/go-service/go.sum | 1 + go.mod | 2 +- go.sum | 2 ++ providers.go | 2 +- topology.go | 22 ++++++++++++++++++++-- topology_test.go | 15 +++++++++++++++ vshard.go | 4 ++++ 9 files changed, 51 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c54a191..5b00079 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,12 @@ BUG FIXES: TESTS: -* Simplify test/tnt/{Makefile,router.lua) -* Indent fix for test/tnt/storage.lua +* Simplify test/tnt/{Makefile,router.lua). +* Indent fix for test/tnt/storage.lua. + +FEATURES: +* now we write topology changes to debug logs. +* now we write info logs about new replicaset adding and nodes state. ## v1.0.1 diff --git a/examples/customer/go-service/go.mod b/examples/customer/go-service/go.mod index 0a649e4..c328477 100644 --- a/examples/customer/go-service/go.mod +++ b/examples/customer/go-service/go.mod @@ -7,7 +7,7 @@ require ( github.com/google/uuid v1.6.0 github.com/spf13/viper v1.18.2 github.com/swaggo/swag v1.16.3 - github.com/tarantool/go-tarantool/v2 v2.1.1-0.20240507091106-8b2be0133e9d + github.com/tarantool/go-tarantool/v2 v2.1.1-0.20240922082035-592db69eed86 ) require ( diff --git a/examples/customer/go-service/go.sum b/examples/customer/go-service/go.sum index 5dc81b9..af304a4 100644 --- a/examples/customer/go-service/go.sum +++ b/examples/customer/go-service/go.sum @@ -91,6 +91,7 @@ github.com/tarantool/go-iproto v1.0.0 h1:quC4hdFhCuFYaCqOFgUxH2foRkhAy+TlEy7gQLh github.com/tarantool/go-iproto v1.0.0/go.mod h1:LNCtdyZxojUed8SbOiYHoc3v9NvaZTB7p96hUySMlIo= github.com/tarantool/go-tarantool/v2 v2.1.1-0.20240507091106-8b2be0133e9d h1:L8E9ZtHsGYACsBGU6EtN3fEhLeneVnQ5KwxEeZQk6rk= github.com/tarantool/go-tarantool/v2 v2.1.1-0.20240507091106-8b2be0133e9d/go.mod h1:hjm1qFxll+hs9VoU74MrvwKrXZKv8WGQLhkyeDTHh7Y= +github.com/tarantool/go-tarantool/v2 v2.1.1-0.20240922082035-592db69eed86/go.mod h1:hjm1qFxll+hs9VoU74MrvwKrXZKv8WGQLhkyeDTHh7Y= github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU= github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc= github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= diff --git a/go.mod b/go.mod index 89c5ea8..075b061 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/snksoft/crc v1.1.0 github.com/spf13/viper v1.18.2 github.com/stretchr/testify v1.9.0 - github.com/tarantool/go-tarantool/v2 v2.1.1-0.20240507091106-8b2be0133e9d + github.com/tarantool/go-tarantool/v2 v2.1.1-0.20240922082035-592db69eed86 github.com/vmihailenco/msgpack/v5 v5.3.5 go.etcd.io/etcd/client/v2 v2.305.10 golang.org/x/sync v0.6.0 diff --git a/go.sum b/go.sum index 6b6e3ea..a51d95d 100644 --- a/go.sum +++ b/go.sum @@ -66,6 +66,8 @@ github.com/tarantool/go-iproto v1.0.0 h1:quC4hdFhCuFYaCqOFgUxH2foRkhAy+TlEy7gQLh github.com/tarantool/go-iproto v1.0.0/go.mod h1:LNCtdyZxojUed8SbOiYHoc3v9NvaZTB7p96hUySMlIo= github.com/tarantool/go-tarantool/v2 v2.1.1-0.20240507091106-8b2be0133e9d h1:L8E9ZtHsGYACsBGU6EtN3fEhLeneVnQ5KwxEeZQk6rk= github.com/tarantool/go-tarantool/v2 v2.1.1-0.20240507091106-8b2be0133e9d/go.mod h1:hjm1qFxll+hs9VoU74MrvwKrXZKv8WGQLhkyeDTHh7Y= +github.com/tarantool/go-tarantool/v2 v2.1.1-0.20240922082035-592db69eed86 h1:2okp3EGd44Bb9+bFhvs75NgXPkIdXPbHhZi1vwBhMHo= +github.com/tarantool/go-tarantool/v2 v2.1.1-0.20240922082035-592db69eed86/go.mod h1:hjm1qFxll+hs9VoU74MrvwKrXZKv8WGQLhkyeDTHh7Y= github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU= github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc= github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= diff --git a/providers.go b/providers.go index 7b611cc..1e3734a 100644 --- a/providers.go +++ b/providers.go @@ -14,7 +14,7 @@ var ( _ LogProvider = (*StdoutLogger)(nil) ) -// A legacy interface for backward compatibility +// LogProvider is a legacy interface for backward compatibility. type LogProvider interface { Info(context.Context, string) Debug(context.Context, string) diff --git a/topology.go b/topology.go index ff6d80d..25b26c9 100644 --- a/topology.go +++ b/topology.go @@ -45,6 +45,8 @@ func (r *Router) Topology() TopologyController { } func (r *Router) AddInstance(ctx context.Context, rsID uuid.UUID, info InstanceInfo) error { + r.log().Debugf(ctx, "trying to add instance %s to router topology in rs %s", info, rsID) + err := info.Validate() if err != nil { return err @@ -69,7 +71,9 @@ func (r *Router) AddInstance(ctx context.Context, rsID uuid.UUID, info InstanceI return rs.conn.Add(ctx, instance) } -func (r *Router) RemoveInstance(_ context.Context, rsID, instanceID uuid.UUID) error { +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) + idToReplicasetRef := r.getIDToReplicaset() rs := idToReplicasetRef[rsID] @@ -81,6 +85,8 @@ func (r *Router) RemoveInstance(_ context.Context, rsID, instanceID uuid.UUID) e } 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() if _, ok := idToReplicasetOld[rsInfo.UUID]; ok { @@ -112,6 +118,16 @@ func (r *Router) AddReplicaset(ctx context.Context, rsInfo ReplicasetInfo, insta return err } + poolInfo := conn.GetInfo() + for instName, instConnInfo := range poolInfo { + connectStatus := "connected now" + if !instConnInfo.ConnectedNow { + connectStatus = "not connected" + } + + r.log().Infof(ctx, "[replicaset %s ] instance %s %s in role %s", rsInfo, instName, connectStatus, instConnInfo.ConnRole) + } + isConnected, err := conn.ConnectedNow(pool.RW) if err != nil { return fmt.Errorf("cant check rs pool conntected rw now with error: %s", err) @@ -154,7 +170,9 @@ func (r *Router) AddReplicasets(ctx context.Context, replicasets map[ReplicasetI return nil } -func (r *Router) RemoveReplicaset(_ context.Context, rsID uuid.UUID) []error { +func (r *Router) RemoveReplicaset(ctx context.Context, rsID uuid.UUID) []error { + r.log().Debugf(ctx, "trying to remove replicaset %s from router topology", rsID) + idToReplicasetOld := r.getIDToReplicaset() rs := idToReplicasetOld[rsID] diff --git a/topology_test.go b/topology_test.go index 01ebc0e..0d53dab 100644 --- a/topology_test.go +++ b/topology_test.go @@ -22,6 +22,9 @@ func TestController_AddInstance(t *testing.T) { t.Run("no such replicaset", func(t *testing.T) { router := Router{ idToReplicaset: map[uuid.UUID]*Replicaset{}, + cfg: Config{ + Loggerf: &emptyLogger{}, + }, } err := router.Topology().AddInstance(ctx, uuid.New(), InstanceInfo{ @@ -34,6 +37,9 @@ func TestController_AddInstance(t *testing.T) { t.Run("invalid instance info", func(t *testing.T) { router := Router{ idToReplicaset: map[uuid.UUID]*Replicaset{}, + cfg: Config{ + Loggerf: &emptyLogger{}, + }, } err := router.Topology().AddInstance(ctx, uuid.New(), InstanceInfo{}) @@ -47,6 +53,9 @@ func TestController_RemoveInstance(t *testing.T) { t.Run("no such replicaset", func(t *testing.T) { router := Router{ idToReplicaset: map[uuid.UUID]*Replicaset{}, + cfg: Config{ + Loggerf: &emptyLogger{}, + }, } err := router.Topology().RemoveInstance(ctx, uuid.New(), uuid.New()) @@ -65,6 +74,9 @@ func TestController_RemoveReplicaset(t *testing.T) { idToReplicaset: map[uuid.UUID]*Replicaset{ uuidToRemove: {conn: mPool}, }, + cfg: Config{ + Loggerf: &emptyLogger{}, + }, } t.Run("no such replicaset", func(t *testing.T) { @@ -88,6 +100,9 @@ func TestRouter_AddReplicaset_AlreadyExists(t *testing.T) { idToReplicaset: map[uuid.UUID]*Replicaset{ alreadyExistingRsUUID: {}, }, + cfg: Config{ + Loggerf: &emptyLogger{}, + }, } // Test that such replicaset already exists diff --git a/vshard.go b/vshard.go index c42eb2f..f1b25eb 100644 --- a/vshard.go +++ b/vshard.go @@ -127,6 +127,10 @@ type InstanceInfo struct { UUID uuid.UUID } +func (ii InstanceInfo) String() string { + 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)