main.go
main.go - Overview
This file contains the main entry point for the DiceDB server application. It handles server initialization, configuration, signal handling, WAL (Write-Ahead Logging) setup, shard management, profiling, and server shutdown.
Detailed Documentation
printConfiguration
- Purpose: Logs the DiceDB configuration to the console.
- Parameters: None
- Returns: None
printBanner
- Purpose: Prints the DiceDB banner to the console.
- Parameters: None
- Returns: None
Start
- Purpose: Starts the DiceDB server.
- Parameters: None
- Returns: None
This function performs the following steps:
- Prints the banner and configuration.
- Adds a default user if a password is provided in the configuration.
- Sets up a context with cancellation for handling signals.
- Initializes signal handling for SIGTERM and SIGINT.
- Initializes the Write-Ahead Log (WAL) if enabled.
- Configures the number of shards based on the number of CPU cores or configuration.
- Initializes the shard manager and watch manager.
- Starts profiling if enabled.
- Initializes and starts the
ironhawk
server. - Restores the database from WAL logs, if enabled.
- Waits for signals or errors to initiate shutdown.
- Performs graceful shutdown, including closing resources and waiting for goroutines.
runServer
- Purpose: Runs the specified server (e.g.,
ironhawk
server) in a goroutine and handles errors. - Parameters:
ctx
(context.Context): The context for controlling the server's lifecycle.wg
(*sync.WaitGroup): A WaitGroup to synchronize server shutdown.srv
(*ironhawk.Server): A pointer to theironhawk
server instance.errCh
(chan<- error): A channel to send errors that occur during server execution.
- Returns: None
This function starts the given server, monitors for errors, and sends any errors to the provided error channel. It also handles graceful shutdown if the context is canceled or an abort command is received.
startProfiling
- Purpose: Starts CPU, memory, block, and trace profiling for the DiceDB server.
- Parameters: None
- Returns:
func()
: A function to stop the profiling and clean up resources.error
: An error if profiling could not be started.
This function creates files for CPU, memory, block, and execution trace profiling. It starts the respective profiling tools and returns a cleanup function that stops the profiling and closes the files.
Code Examples
None