Skip to content

Commit cacaca6

Browse files
committed
Reapply "add debug logs (to be removed)"
This reverts commit 4057979.
1 parent 4057979 commit cacaca6

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

x/staking/cache/cache.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ func (e *Entry[V, E]) get(ctx context.Context, cdc codec.BinaryCodec) (map[strin
6363
e.mu.RLock()
6464
defer e.mu.RUnlock()
6565

66+
fmt.Printf("[%s] get: full=%v, dirty=%v, count=%d\n",
67+
e.cacheType, e.full.Load(), e.dirty.Load(), e.count.Load())
68+
6669
result := make(map[string]V)
6770

6871
store := e.storeService.OpenMemoryStore(ctx)
@@ -85,6 +88,7 @@ func (e *Entry[V, E]) get(ctx context.Context, cdc codec.BinaryCodec) (map[strin
8588
result[key] = value
8689
}
8790

91+
fmt.Printf("[%s] get: returning %d entries\n", e.cacheType, len(result))
8892
return result, nil
8993
}
9094

@@ -115,7 +119,11 @@ func (e *Entry[V, E]) setEntry(ctx context.Context, cdc codec.BinaryCodec, key s
115119

116120
// setEntryUnsafe works the same as setEntry but the caller is responsible for holding the lock
117121
func (e *Entry[V, E]) setEntryUnsafe(ctx context.Context, cdc codec.BinaryCodec, key string, value V) error {
122+
fmt.Printf("[%s] setEntryUnsafe: key=%s, full=%v, dirty=%v, count=%d, max=%d\n",
123+
e.cacheType, key, e.full.Load(), e.dirty.Load(), e.count.Load(), e.max)
124+
118125
if e.full.Load() {
126+
fmt.Printf("[%s] setEntryUnsafe: cache is full, returning error\n", e.cacheType)
119127
return types.ErrCacheMaxSizeReached
120128
}
121129

@@ -143,20 +151,27 @@ func (e *Entry[V, E]) setEntryUnsafe(ctx context.Context, cdc codec.BinaryCodec,
143151
// Only increment counter if this is a new key
144152
if e.max > 0 && !exists {
145153
newCount := e.count.Add(1)
154+
fmt.Printf("[%s] setEntryUnsafe: incremented count to %d (max=%d, exists=%v)\n",
155+
e.cacheType, newCount, e.max, exists)
146156
if newCount >= uint64(e.max) {
147157
e.dirty.Store(true)
148158
e.full.Store(true)
159+
fmt.Printf("[%s] setEntryUnsafe: cache now FULL and DIRTY\n", e.cacheType)
149160
return types.ErrCacheMaxSizeReached
150161
}
151162
}
152163

164+
fmt.Printf("[%s] setEntryUnsafe: successfully set key=%s, count=%d\n", e.cacheType, key, e.count.Load())
153165
return nil
154166
}
155167

156168
func (e *Entry[V, E]) deleteEntry(ctx context.Context, key string) error {
157169
e.mu.Lock()
158170
defer e.mu.Unlock()
159171

172+
fmt.Printf("[%s] deleteEntry: key=%s, full=%v, dirty=%v, count=%d, max=%d\n",
173+
e.cacheType, key, e.full.Load(), e.dirty.Load(), e.count.Load(), e.max)
174+
160175
store := e.storeService.OpenMemoryStore(ctx)
161176
storeKey := e.getStoreKey(key)
162177

@@ -176,16 +191,25 @@ func (e *Entry[V, E]) deleteEntry(ctx context.Context, key string) error {
176191
// Only decrement counter if the key actually existed
177192
if e.max > 0 && exists {
178193
newCount := e.count.Add(^uint64(0)) // Subtract 1 using two's complement
194+
fmt.Printf("[%s] deleteEntry: decremented count to %d (existed=%v)\n",
195+
e.cacheType, newCount, exists)
179196
if newCount < uint64(e.max) {
180197
e.full.Store(false)
198+
fmt.Printf("[%s] deleteEntry: cache is no longer FULL (count=%d < max=%d), but dirty=%v\n",
199+
e.cacheType, newCount, e.max, e.dirty.Load())
181200
}
182201
}
183202

203+
fmt.Printf("[%s] deleteEntry: successfully deleted key=%s, count=%d, full=%v, dirty=%v\n",
204+
e.cacheType, key, e.count.Load(), e.full.Load(), e.dirty.Load())
184205
return nil
185206
}
186207

187208
// clearUnsafe clears the cache but the caller is responsible for holding the lock
188209
func (e *Entry[V, E]) clearUnsafe(ctx context.Context) error {
210+
fmt.Printf("[%s] clearUnsafe: clearing cache, count=%d, full=%v, dirty=%v\n",
211+
e.cacheType, e.count.Load(), e.full.Load(), e.dirty.Load())
212+
189213
store := e.storeService.OpenMemoryStore(ctx)
190214
prefix := e.getPrefix()
191215
iter, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix))
@@ -194,14 +218,18 @@ func (e *Entry[V, E]) clearUnsafe(ctx context.Context) error {
194218
}
195219
defer iter.Close()
196220

221+
deletedCount := 0
197222
for ; iter.Valid(); iter.Next() {
198223
if err := store.Delete(iter.Key()); err != nil {
199224
return err
200225
}
226+
deletedCount++
201227
}
202228

203229
e.count.Store(0)
204230
e.full.Store(false)
231+
fmt.Printf("[%s] clearUnsafe: cleared %d entries, count=0, full=false\n",
232+
e.cacheType, deletedCount)
205233
return nil
206234
}
207235

@@ -316,7 +344,10 @@ func NewCache(
316344
// Unbonding Validators Queue
317345

318346
func (c *ValidatorsQueueCache) checkReloadUnbondingValidatorsQueue(ctx context.Context) error {
347+
fmt.Printf("[unbonding_validators] checkReload: checking dirty flag=%v\n", c.unbondingValidatorsQueue.dirty.Load())
348+
319349
if !c.unbondingValidatorsQueue.dirty.Load() {
350+
fmt.Printf("[unbonding_validators] checkReload: not dirty, skipping reload\n")
320351
return nil
321352
}
322353

@@ -325,36 +356,52 @@ func (c *ValidatorsQueueCache) checkReloadUnbondingValidatorsQueue(ctx context.C
325356

326357
// Double-check - another goroutine might have reloaded while we waited
327358
if !c.unbondingValidatorsQueue.dirty.Load() {
359+
fmt.Printf("[unbonding_validators] checkReload: no longer dirty after acquiring lock, skipping reload\n")
328360
return nil
329361
}
330362

363+
fmt.Printf("[unbonding_validators] checkReload: STARTING RELOAD from persistent store\n")
331364
c.logger(ctx).Info("Unbonding validators queue is dirty. Reinitializing cache from store.")
332365
data, err := c.unbondingValidatorsQueue.loadFromStore(ctx)
333366
if err != nil {
367+
fmt.Printf("[unbonding_validators] checkReload: ERROR loading from store: %v\n", err)
334368
return err
335369
}
370+
fmt.Printf("[unbonding_validators] checkReload: loaded %d entries from persistent store\n", len(data))
336371

337372
if err := c.unbondingValidatorsQueue.clearUnsafe(ctx); err != nil {
373+
fmt.Printf("[unbonding_validators] checkReload: ERROR clearing cache: %v\n", err)
338374
return err
339375
}
376+
fmt.Printf("[unbonding_validators] checkReload: cleared cache\n")
340377

341378
for key, value := range data {
379+
fmt.Printf("[unbonding_validators] checkReload: setting entry key=%s during reload\n", key)
342380
if err := c.unbondingValidatorsQueue.setEntryUnsafe(ctx, c.cdc, key, value); err != nil {
381+
fmt.Printf("[unbonding_validators] checkReload: ERROR setting entry key=%s: %v\n", key, err)
343382
return err
344383
}
345384
}
346385

347386
c.unbondingValidatorsQueue.dirty.Store(false)
387+
fmt.Printf("[unbonding_validators] checkReload: RELOAD COMPLETE, dirty=false, count=%d\n",
388+
c.unbondingValidatorsQueue.count.Load())
348389

349390
return nil
350391
}
351392

352393
func (c *ValidatorsQueueCache) GetUnbondingValidatorsQueue(ctx context.Context) (map[string][]string, error) {
394+
fmt.Printf("[unbonding_validators] GetUnbondingValidatorsQueue: full=%v, dirty=%v, count=%d\n",
395+
c.unbondingValidatorsQueue.full.Load(), c.unbondingValidatorsQueue.dirty.Load(),
396+
c.unbondingValidatorsQueue.count.Load())
397+
353398
if c.unbondingValidatorsQueue.full.Load() {
399+
fmt.Printf("[unbonding_validators] GetUnbondingValidatorsQueue: cache is FULL, returning error\n")
354400
return nil, types.ErrCacheMaxSizeReached
355401
}
356402

357403
if err := c.checkReloadUnbondingValidatorsQueue(ctx); err != nil {
404+
fmt.Printf("[unbonding_validators] GetUnbondingValidatorsQueue: reload failed with error: %v\n", err)
358405
return nil, err
359406
}
360407

@@ -374,10 +421,12 @@ func (c *ValidatorsQueueCache) GetUnbondingValidatorsQueueEntry(ctx context.Cont
374421
}
375422

376423
func (c *ValidatorsQueueCache) SetUnbondingValidatorQueueEntry(ctx context.Context, key string, addrs []string) error {
424+
fmt.Printf("[unbonding_validators] SetUnbondingValidatorQueueEntry: key=%s\n", key)
377425
return c.unbondingValidatorsQueue.setEntry(ctx, c.cdc, key, addrs)
378426
}
379427

380428
func (c *ValidatorsQueueCache) DeleteUnbondingValidatorQueueEntry(ctx context.Context, key string) error {
429+
fmt.Printf("[unbonding_validators] DeleteUnbondingValidatorQueueEntry: key=%s\n", key)
381430
return c.unbondingValidatorsQueue.deleteEntry(ctx, key)
382431
}
383432

0 commit comments

Comments
 (0)