Skip to content

Commit 5a26525

Browse files
Copilotbootjp
andauthored
kv: inject logger into ShardedCoordinator to avoid global slog mutation in tests
Agent-Logs-Url: https://github.com/bootjp/elastickv/sessions/5215b977-6567-48ec-88ba-008146af1132 Co-authored-by: bootjp <1306365+bootjp@users.noreply.github.com>
1 parent 0cda05c commit 5a26525

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

kv/sharded_coordinator.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type ShardedCoordinator struct {
3636
defaultGroup uint64
3737
clock *HLC
3838
store store.MVCCStore
39+
log *slog.Logger
3940
}
4041

4142
// NewShardedCoordinator builds a coordinator for the provided shard groups.
@@ -52,6 +53,7 @@ func NewShardedCoordinator(engine *distribution.Engine, groups map[uint64]*Shard
5253
defaultGroup: defaultGroup,
5354
clock: clock,
5455
store: st,
56+
log: slog.Default(),
5557
}
5658
}
5759

@@ -211,7 +213,7 @@ func (c *ShardedCoordinator) commitSecondaryTxns(startTS uint64, primaryGid uint
211213
}
212214
r, err := commitSecondaryWithRetry(g, req)
213215
if err != nil {
214-
slog.Warn("txn secondary commit failed",
216+
c.log.Warn("txn secondary commit failed",
215217
slog.Uint64("gid", gid),
216218
slog.String("primary_key", string(primaryKey)),
217219
slog.Uint64("start_ts", startTS),
@@ -269,7 +271,7 @@ func (c *ShardedCoordinator) abortPreparedTxn(startTS uint64, primaryKey []byte,
269271
if errors.Is(err, ErrTxnAlreadyCommitted) {
270272
continue
271273
}
272-
slog.Warn("txn abort failed; locks may remain until TTL expiry",
274+
c.log.Warn("txn abort failed; locks may remain until TTL expiry",
273275
slog.Uint64("gid", pg.gid),
274276
slog.String("primary_key", string(primaryKey)),
275277
slog.Uint64("start_ts", startTS),

kv/sharded_coordinator_abort_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,10 @@ func TestShardedAbortRollback_PrepareFailOnShard2_CleansShard1Locks(t *testing.T
8787

8888
func TestAbortPreparedTxn_DoesNotWarnWhenTxnAlreadyCommitted(t *testing.T) {
8989
var buf bytes.Buffer
90-
prev := slog.Default()
91-
slog.SetDefault(slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelWarn})))
92-
defer slog.SetDefault(prev)
90+
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelWarn}))
9391

9492
coord := &ShardedCoordinator{
93+
log: logger,
9594
groups: map[uint64]*ShardGroup{
9695
1: {Txn: &failingTransactional{err: errors.WithStack(ErrTxnAlreadyCommitted)}},
9796
},

0 commit comments

Comments
 (0)