Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract stream and consumer monitoring into package #543

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions connbalancer/logger.go → api/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package connbalancer
package api

import (
"log"
)

type Logger interface {
Trace(format string, a ...any)
Debug(format string, a ...any)
Info(format string, a ...any)
Error(format string, a ...any)
Tracef(format string, a ...any)
Debugf(format string, a ...any)
Infof(format string, a ...any)
Errorf(format string, a ...any)
}

type Level uint
Expand All @@ -46,25 +46,25 @@ func NewDiscardLogger() Logger {
return &dfltLogger{lvl: ErrorLevel, logFunc: func(format string, a ...any) {}}
}

func (d *dfltLogger) Trace(format string, a ...any) {
func (d *dfltLogger) Tracef(format string, a ...any) {
if d.lvl >= TraceLevel {
d.logFunc(format, a...)
}
}

func (d *dfltLogger) Debug(format string, a ...any) {
func (d *dfltLogger) Debugf(format string, a ...any) {
if d.lvl >= DebugLevel {
d.logFunc(format, a...)
}
}

func (d *dfltLogger) Info(format string, a ...any) {
func (d *dfltLogger) Infof(format string, a ...any) {
if d.lvl >= InfoLevel {
d.logFunc(format, a...)
}
}

func (d *dfltLogger) Error(format string, a ...any) {
func (d *dfltLogger) Errorf(format string, a ...any) {
if d.lvl >= ErrorLevel {
d.logFunc(format, a...)
}
Expand Down
37 changes: 19 additions & 18 deletions connbalancer/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sync"
"time"

"github.com/nats-io/jsm.go/api"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
)
Expand All @@ -41,7 +42,7 @@ type balancer struct {
nc *nats.Conn
duration time.Duration
limits *ConnectionSelector
log Logger
log api.Logger
}

type conn struct {
Expand All @@ -50,7 +51,7 @@ type conn struct {
conn *server.ConnInfo
}

func New(nc *nats.Conn, runTime time.Duration, log Logger, connections ConnectionSelector) (Balancer, error) {
func New(nc *nats.Conn, runTime time.Duration, log api.Logger, connections ConnectionSelector) (Balancer, error) {
if connections.SubjectInterest != "" && connections.Account == "" {
return nil, fmt.Errorf("can only filter by subject if account is given")
}
Expand Down Expand Up @@ -84,14 +85,14 @@ func (c *balancer) Balance(ctx context.Context) (int, error) {
return 0, err
}

c.log.Debug("Had %d connz responses", len(connz))
c.log.Debugf("Had %d connz responses", len(connz))

matched, err := c.pickConnections(connz)
if err != nil {
return 0, err
}

c.log.Debug("Matched %d connections", len(matched))
c.log.Debugf("Matched %d connections", len(matched))

if len(matched) == 0 {
return 0, nil
Expand All @@ -100,40 +101,40 @@ func (c *balancer) Balance(ctx context.Context) (int, error) {
var sleep = c.duration / time.Duration(len(matched))
var success int

c.log.Info("Balancing %d connections with %v sleep between each balance request", len(matched), sleep)
c.log.Infof("Balancing %d connections with %v sleep between each balance request", len(matched), sleep)

for i, m := range matched {
cid, err := c.nc.GetClientID()
if err != nil {
c.log.Error("Could not exclude self from kicks: %v", err)
c.log.Errorf("Could not exclude self from kicks: %v", err)
continue
}

if m.serverId == c.nc.ConnectedServerId() && m.conn.Cid == cid {
c.log.Debug("Not kicking own connection")
c.log.Debugf("Not kicking own connection")
continue
}

res, err := c.reqMany(ctx, fmt.Sprintf("$SYS.REQ.SERVER.%s.KICK", m.serverId), &server.KickClientReq{CID: m.conn.Cid}, 1)
if err != nil {
c.log.Error("Could not kick %d on %s: %v", m.conn.Cid, m.serverId, err)
c.log.Errorf("Could not kick %d on %s: %v", m.conn.Cid, m.serverId, err)
continue
}

if len(res) != 1 {
c.log.Error("Could not kick %d on %s: expected 1 response but had %d", m.conn.Cid, m.serverId, len(res))
c.log.Errorf("Could not kick %d on %s: expected 1 response but had %d", m.conn.Cid, m.serverId, len(res))
continue
}

var resp server.ServerAPIResponse
err = json.Unmarshal(res[0].Data, &resp)
if err != nil {
c.log.Error("Could not kick %d on %s: invalid server response: %v", m.conn.Cid, m.serverId, err)
c.log.Errorf("Could not kick %d on %s: invalid server response: %v", m.conn.Cid, m.serverId, err)
continue
}

if resp.Error != nil {
c.log.Error("Could not kick %d on %s: invalid server response: %v", m.conn.Cid, m.serverId, resp.Error.Description)
c.log.Errorf("Could not kick %d on %s: invalid server response: %v", m.conn.Cid, m.serverId, resp.Error.Description)
continue
}

Expand All @@ -143,16 +144,16 @@ func (c *balancer) Balance(ctx context.Context) (int, error) {
}

if m.conn.Account != "" {
c.log.Info("Balanced client %d%s in account %s on %s", m.conn.Cid, name, m.conn.Account, m.serverName)
c.log.Infof("Balanced client %d%s in account %s on %s", m.conn.Cid, name, m.conn.Account, m.serverName)
} else {
c.log.Info("Balanced client %d%s on %s", m.conn.Cid, name, m.serverName)
c.log.Infof("Balanced client %d%s on %s", m.conn.Cid, name, m.serverName)
}

success++

if i != len(matched)-1 {
timer := time.NewTimer(sleep)
c.log.Debug("Sleeping for %v", sleep)
c.log.Debugf("Sleeping for %v", sleep)
select {
case <-timer.C:
case <-ctx.Done():
Expand Down Expand Up @@ -211,7 +212,7 @@ func (c *balancer) getConnz(ctx context.Context) ([]*server.ServerAPIConnzRespon
break
}

c.log.Info("Gathering paged connection information")
c.log.Infof("Gathering paged connection information")
}

return results, nil
Expand Down Expand Up @@ -287,7 +288,7 @@ func (c *balancer) reqMany(ctx context.Context, subj string, req any, expect int
}
}

c.log.Trace(">>> %s: %s", subj, string(jreq))
c.log.Tracef(">>> %s: %s", subj, string(jreq))

var (
mu sync.Mutex
Expand Down Expand Up @@ -317,7 +318,7 @@ func (c *balancer) reqMany(ctx context.Context, subj string, req any, expect int
mu.Lock()
defer mu.Unlock()

c.log.Trace("<<< (%dB) %s", len(m.Data), string(m.Data))
c.log.Tracef("<<< (%dB) %s", len(m.Data), string(m.Data))
if finisher != nil {
finisher.Reset(300 * time.Millisecond)
}
Expand Down Expand Up @@ -363,7 +364,7 @@ func (c *balancer) reqMany(ctx context.Context, subj string, req any, expect int
}

mu.Lock()
c.log.Debug("=== Received %d responses", ctr)
c.log.Debugf("=== Received %d responses", ctr)
mu.Unlock()

return res, nil
Expand Down
3 changes: 2 additions & 1 deletion connbalancer/balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"
"time"

"github.com/nats-io/jsm.go/api"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
)
Expand Down Expand Up @@ -128,7 +129,7 @@ func checkBalanced(t *testing.T, nc *nats.Conn, expect int, s ConnectionSelector
t.Helper()

// dont kick ourselves or connections on other servers
balancer, err := New(nc, 0, NewDiscardLogger(), s)
balancer, err := New(nc, 0, api.NewDiscardLogger(), s)
if err != nil {
t.Fatalf("create failed: %v", err)
}
Expand Down
Loading
Loading