Skip to content

Commit

Permalink
resolve issue #79
Browse files Browse the repository at this point in the history
* implement go-tarantool Logger interface to use the same logger as router uses
* AddInstance bugfix: pass r.cfg.PoolOpts to new instance
  • Loading branch information
nurzhan-saktaganov committed Oct 29, 2024
1 parent 22c57dd commit d0298bd
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ BUG FIXES:
* RouterCallImpl: retry on BucketResolve error.
* RouterCallImpl: do not retry on vshard error "TRANSFER_IS_IN_PROGRESS".
* RouterCallImpl: remove misleading RetryOnCall.
* AddInstance bugfix: pass r.cfg.PoolOpts to new instance.

FEATURES:

Expand All @@ -22,6 +23,7 @@ REFACTOR:

TESTS:
* Tests for BucketsSearchMode (tnt/discovery_test.go).
* Implement go-tarantool Logger interface to use the same logger as router uses (resolve issue #79).

## v1.1.0

Expand Down
59 changes: 59 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package vshard_router

import (
"context"

"github.com/tarantool/go-tarantool/v2"
)

// go-tarantool writes logs by default to stderr. Stderr might be not available, or user might use syslog or logging into file.
// So we should implement logging interface and redirect go-tarantool logs to the user's logger.
type tarantoolOptsLogger struct {
loggerf LogfProvider
ctx context.Context
}

// Does almost the same thing as defaultLogger in go-tarantool, but uses user provided logger instead of stdout logger.
// https://github.com/tarantool/go-tarantool/blob/592db69eed8649b82ce432b930c27daeee98c52f/connection.go#L90
func (l tarantoolOptsLogger) Report(event tarantool.ConnLogKind, conn *tarantool.Connection, v ...interface{}) {
switch event {
case tarantool.LogReconnectFailed:
reconnects, ok1 := v[0].(uint)
err, ok2 := v[1].(error)
if ok1 && ok2 {
l.loggerf.Errorf(l.ctx, "tarantool: reconnect (%d) to %s failed: %s", reconnects, conn.Addr(), err)
} else {
l.loggerf.Errorf(l.ctx, "tarantool: reconnect to %s failed (unexpected v... format): %+v", conn.Addr(), v)
}
case tarantool.LogLastReconnectFailed:
if err, ok := v[0].(error); ok {
l.loggerf.Errorf(l.ctx, "tarantool: last reconnect to %s failed: %s, giving it up", conn.Addr(), err)
} else {
l.loggerf.Errorf(l.ctx, "tarantool: last reconnect to %s failed (unexpected v... format): %v+", conn.Addr(), v)
}
case tarantool.LogUnexpectedResultId:
if header, ok := v[0].(tarantool.Header); ok {
l.loggerf.Errorf(l.ctx, "tarantool: connection %s got unexpected resultId (%d) in response"+
"(probably cancelled request)",
conn.Addr(), header.RequestId)
} else {
l.loggerf.Errorf(l.ctx, "tarantool: connection %s got unexpected resultId in response"+
"(probably cancelled request) (unexpected v... format): %+v",
conn.Addr(), v)
}
case tarantool.LogWatchEventReadFailed:
if err, ok := v[0].(error); ok {
l.loggerf.Errorf(l.ctx, "tarantool: unable to parse watch event: %s", err)
} else {
l.loggerf.Errorf(l.ctx, "tarantool: unable to parse watch event (unexpected v... format): %+v", v)
}
case tarantool.LogAppendPushFailed:
if err, ok := v[0].(error); ok {
l.loggerf.Errorf(l.ctx, "tarantool: unable to append a push response: %s", err)
} else {
l.loggerf.Errorf(l.ctx, "tarantool: unable to append a push response (unexpected v... format): %+v", v)
}
default:
l.loggerf.Errorf(l.ctx, "tarantool: unexpected event %d on conn %s, v...: %+v", event, conn, v)
}
}
1 change: 1 addition & 0 deletions topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (r *Router) AddInstance(ctx context.Context, rsID uuid.UUID, info InstanceI
User: r.cfg.User,
Password: r.cfg.Password,
},
Opts: r.cfg.PoolOpts,
}

idToReplicasetRef := r.getIDToReplicaset()
Expand Down
6 changes: 6 additions & 0 deletions vshard.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ func prepareCfg(cfg Config) (Config, error) {
cfg.Loggerf = emptyLogfProvider
}

// Log tarantool internal events using the same logger as router uses.
cfg.PoolOpts.Logger = tarantoolOptsLogger{
loggerf: cfg.Loggerf,
ctx: context.Background(),
}

if cfg.Metrics == nil {
cfg.Metrics = emptyMetricsProvider
}
Expand Down

0 comments on commit d0298bd

Please sign in to comment.