rehash

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnknownRehasher = errors.New("unknown rehasher")

Functions

func NewNoopRehasher

func NewNoopRehasher() *noopRehasher

NewNoopRehasher creates a no-op rehasher.

func OldShardIndex

func OldShardIndex(key string, mask uint32) uint32

OldShardIndex computes the shard index for a key using the given mask.

func Register

func Register(name string, factory RehasherFactory) error

Register adds a custom rehasher factory under the given name.

Types

type BatchRehasher

type BatchRehasher struct {
	// contains filtered or unexported fields
}

BatchRehasher migrates all remaining entries in a single Step call. This causes a one-time latency spike but avoids prolonged double-table overhead. Useful when downtime or brief pauses are acceptable.

func NewBatchRehasher

func NewBatchRehasher() *BatchRehasher

NewBatchRehasher creates a batch rehasher.

func (*BatchRehasher) IsRehashing

func (r *BatchRehasher) IsRehashing() bool

func (*BatchRehasher) Name

func (r *BatchRehasher) Name() string

func (*BatchRehasher) OldShard

func (r *BatchRehasher) OldShard(key string) Shard

func (*BatchRehasher) OldShards

func (r *BatchRehasher) OldShards() []Shard

func (*BatchRehasher) Start

func (r *BatchRehasher) Start(oldShards []Shard, oldMask uint32)

func (*BatchRehasher) Step

func (r *BatchRehasher) Step(newShards []Shard, indexFunc func(key string) uint32) (done bool, justCompleted bool)

func (*BatchRehasher) Stop

func (r *BatchRehasher) Stop()

type IncrementalRehasher

type IncrementalRehasher struct {
	// contains filtered or unexported fields
}

IncrementalRehasher performs migration gradually, moving a fixed batch of entries on each Step call (Redis-style). This minimizes latency spikes but spreads migration overhead across normal operations.

func NewIncrementalRehasher

func NewIncrementalRehasher() *IncrementalRehasher

NewIncrementalRehasher creates an incremental rehasher with a default batch size of 16 entries per step.

func (*IncrementalRehasher) IsRehashing

func (r *IncrementalRehasher) IsRehashing() bool

func (*IncrementalRehasher) Name

func (r *IncrementalRehasher) Name() string

func (*IncrementalRehasher) OldShard

func (r *IncrementalRehasher) OldShard(key string) Shard

func (*IncrementalRehasher) OldShards

func (r *IncrementalRehasher) OldShards() []Shard

func (*IncrementalRehasher) Start

func (r *IncrementalRehasher) Start(oldShards []Shard, oldMask uint32)

func (*IncrementalRehasher) Step

func (r *IncrementalRehasher) Step(newShards []Shard, indexFunc func(key string) uint32) (done bool, justCompleted bool)

func (*IncrementalRehasher) Stop

func (r *IncrementalRehasher) Stop()

type Item

type Item struct {
	Key   string
	Value []byte
	TTL   time.Duration
}

Item represents a single cache entry being migrated.

type Rehasher

type Rehasher interface {
	Name() string
	// Start begins a rehash with the old shard table and mask.
	Start(oldShards []Shard, oldMask uint32)
	// Step executes one migration step into newShards using indexFunc to locate
	// the destination shard. It returns (done, justCompleted) where done is true
	// if the rehash is fully complete, and justCompleted is true only when this
	// specific call caused the rehash to finish.
	Step(newShards []Shard, indexFunc func(key string) uint32) (done bool, justCompleted bool)
	// IsRehashing reports whether a rehash is currently in progress.
	IsRehashing() bool
	// OldShard returns the shard in the old table responsible for key, or nil.
	OldShard(key string) Shard
	// OldShards returns the full old shard table, or nil if not rehashing.
	// Callers must not modify the returned shards.
	OldShards() []Shard
	// Stop forcibly aborts the rehash and releases old state.
	Stop()
}

Rehasher defines a pluggable strategy for migrating data from an old shard layout to a new one. Implementations control the pacing and granularity of the migration.

type RehasherFactory

type RehasherFactory func() Rehasher

RehasherFactory creates a fresh Rehasher instance.

func GetFactory

func GetFactory(name string) (RehasherFactory, error)

GetFactory retrieves a rehasher factory by name.

type Shard

type Shard interface {
	ExtractN(n int) []Item
	Set(key string, value []byte, ttl time.Duration) []string
	Del(key string)
	Len() int
}

Shard is the minimal interface a rehasher needs to interact with cache shards.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL