Skip to content

Commit

Permalink
Improve logging and error handling, (#5)
Browse files Browse the repository at this point in the history
* Improve logging and error handling

This overhauls the logging and error responses from the *Conn. Namely, it
changes the logging backend to `log/slog` which makes it easier to manage the
library's logging centrally (however the old functionally to provide a custom
logger is retained). It also captures the reason why a specific connection to a
ZK server was closed in the error that is propagated to all callers. This can be
checked with `errors.Is`.

This also adds an exponential backoff feature to the client, which will make it
back off when reconnecting to a ZK server.

* Remove logger option, it messes with the logging

* Change default behavior of DNSHostProvider

* Split implementation of HostProvider, fix up tests
  • Loading branch information
PapaCharlie committed Feb 5, 2024
1 parent 5c2d9da commit 67688ea
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 230 deletions.
11 changes: 6 additions & 5 deletions cluster_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zk

import (
"log/slog"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -112,10 +113,10 @@ func TestNoQuorum(t *testing.T) {
t.Fatalf("Failed to connect and get session")
}
initialSessionID := zk.sessionID
DefaultLogger.Printf(" Session established: id=%d, timeout=%d", zk.sessionID, zk.sessionTimeoutMs)
t.Logf("Session established: id=%d, timeout=%d", zk.sessionID, zk.sessionTimeout)

// Kill the ZooKeeper leader and wait for the session to reconnect.
DefaultLogger.Printf(" Kill the leader")
t.Logf("Kill the leader")
disconnectWatcher1 := sl.NewWatcher(sessionStateMatcher(StateDisconnected))
hasSessionWatcher2 := sl.NewWatcher(sessionStateMatcher(StateHasSession))
tc.StopServer(hasSessionEvent1.Server)
Expand All @@ -135,7 +136,7 @@ func TestNoQuorum(t *testing.T) {
}

// Kill the ZooKeeper leader leaving the cluster without quorum.
DefaultLogger.Printf(" Kill the leader")
t.Logf("Kill the leader")
disconnectWatcher2 := sl.NewWatcher(sessionStateMatcher(StateDisconnected))
tc.StopServer(hasSessionEvent2.Server)

Expand All @@ -151,7 +152,7 @@ func TestNoQuorum(t *testing.T) {
// Make sure that we keep retrying connecting to the only remaining
// ZooKeeper server, but the attempts are being dropped because there is
// no quorum.
DefaultLogger.Printf(" Retrying no luck...")
t.Logf("Retrying no luck...")
var firstDisconnect *Event
begin := time.Now()
for time.Now().Sub(begin) < 6*time.Second {
Expand Down Expand Up @@ -269,7 +270,7 @@ func NewStateLogger(eventCh <-chan Event) *EventLogger {
sw.matchCh <- event
}
}
DefaultLogger.Printf(" event received: %v\n", event)
slog.Info("event received", "event", event)
el.events = append(el.events, event)
el.lock.Unlock()
}
Expand Down
Loading

0 comments on commit 67688ea

Please sign in to comment.