main.go
main.go - Overview
This file defines the ShardThread
struct and its associated methods for managing a shard within the DiceDB system. It handles the lifecycle of a shard, including initialization, periodic tasks (cron), and cleanup.
Detailed Documentation
ShardThread
type ShardThread struct {
id int // id is the unique identifier for the shard.
store *dstore.Store // store that the shard is responsible for.
globalErrorChan chan error // globalErrorChan is the channel for sending system-level errors.
lastCronExecTime time.Time // lastCronExecTime is the last time the shard executed cron tasks.
cronFrequency time.Duration // cronFrequency is the frequency at which the shard executes cron tasks.
}
- Purpose: Represents a shard within the DiceDB system, responsible for managing a portion of the data.
- Fields:
id
: The unique identifier for the shard (int).store
: Thedstore.Store
instance that the shard manages (*dstore.Store).globalErrorChan
: A channel for sending system-level errors (chan error).lastCronExecTime
: The last time the shard executed cron tasks (time.Time).cronFrequency
: The frequency at which the shard executes cron tasks (time.Duration).
NewShardThread
func NewShardThread(id int, gec chan error, evictionStrategy dstore.EvictionStrategy) *ShardThread
- Purpose: Creates a new
ShardThread
instance. - Parameters:
id
: The ID of the shard (int).gec
: The global error channel (chan error).evictionStrategy
: The eviction strategy for the store (dstore.EvictionStrategy).
- Returns: A pointer to the newly created
ShardThread
instance (*ShardThread).
Start
func (shard *ShardThread) Start(ctx context.Context)
- Purpose: Starts the shard thread, listening for incoming requests and executing cron tasks.
- Parameters:
ctx
: A context.Context for managing the shard's lifecycle.
- Returns: None.
runCronTasks
func (shard *ShardThread) runCronTasks()
- Purpose: Executes the cron tasks for the shard, including deleting expired keys.
- Parameters: None.
- Returns: None.
cleanup
func (shard *ShardThread) cleanup()
- Purpose: Handles cleanup logic when the shard stops, such as WAL flushing.
- Parameters: None.
- Returns: None.
Store
func (shard *ShardThread) Store() *dstore.Store
- Purpose: Returns the
dstore.Store
associated with the shard. - Parameters: None.
- Returns: A pointer to the
dstore.Store
(*dstore.Store).
Code Examples
None.