Make configuration values injectable rather than hardcoded
All checks were successful
CI / build (pull_request) Successful in 16s
CI / build (push) Successful in 15s

Add config structs with sensible defaults for tunable parameters:
- JetStreamConfig for stream retention (1 year) and replica count (1)
- HashRingConfig for virtual nodes per physical node (150)
- ShardConfig for shard count (1024) and replication factor (1)

Each component gets a new WithConfig constructor that accepts custom
configuration, while the original constructors continue to work with
defaults. Zero values in configs fall back to defaults for backward
compatibility.

Closes #38

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit was merged in pull request #43.
This commit is contained in:
2026-01-10 15:33:56 +01:00
parent 51916621ea
commit c757bb76f3
13 changed files with 353 additions and 74 deletions

View File

@@ -4,17 +4,47 @@ import (
"time"
)
// Default configuration values
const (
// NumShards defines the total number of shards in the cluster
NumShards = 1024
// VirtualNodes defines the number of virtual nodes per physical node for consistent hashing
VirtualNodes = 150
// DefaultNumShards defines the default total number of shards in the cluster
DefaultNumShards = 1024
// DefaultVirtualNodes defines the default number of virtual nodes per physical node
DefaultVirtualNodes = 150
// Leadership election constants
LeaderLeaseTimeout = 10 * time.Second // How long a leader lease lasts
HeartbeatInterval = 3 * time.Second // How often leader sends heartbeats
ElectionTimeout = 2 * time.Second // How long to wait for election
)
// HashRingConfig holds configuration for the consistent hash ring
type HashRingConfig struct {
// VirtualNodes is the number of virtual nodes per physical node (default: 150)
VirtualNodes int
}
// DefaultHashRingConfig returns the default hash ring configuration
func DefaultHashRingConfig() HashRingConfig {
return HashRingConfig{
VirtualNodes: DefaultVirtualNodes,
}
}
// ShardConfig holds configuration for shard management
type ShardConfig struct {
// ShardCount is the total number of shards (default: 1024)
ShardCount int
// ReplicationFactor is the number of replicas per shard (default: 1)
ReplicationFactor int
}
// DefaultShardConfig returns the default shard configuration
func DefaultShardConfig() ShardConfig {
return ShardConfig{
ShardCount: DefaultNumShards,
ReplicationFactor: 1,
}
}
// NodeStatus represents the health status of a node
type NodeStatus string
@@ -30,14 +60,14 @@ type NodeInfo struct {
Address string `json:"address"`
Port int `json:"port"`
Status NodeStatus `json:"status"`
Capacity float64 `json:"capacity"` // Maximum load capacity
Load float64 `json:"load"` // Current CPU/memory load
LastSeen time.Time `json:"lastSeen"` // Last heartbeat timestamp
Capacity float64 `json:"capacity"` // Maximum load capacity
Load float64 `json:"load"` // Current CPU/memory load
LastSeen time.Time `json:"lastSeen"` // Last heartbeat timestamp
Timestamp time.Time `json:"timestamp"`
Metadata map[string]string `json:"metadata"`
IsLeader bool `json:"isLeader"`
VMCount int `json:"vmCount"` // Number of VMs on this node
ShardIDs []int `json:"shardIds"` // Shards assigned to this node
VMCount int `json:"vmCount"` // Number of VMs on this node
ShardIDs []int `json:"shardIds"` // Shards assigned to this node
}
// NodeUpdateType represents the type of node update
@@ -57,9 +87,9 @@ type NodeUpdate struct {
// ShardMap represents the distribution of shards across cluster nodes
type ShardMap struct {
Version uint64 `json:"version"` // Incremented on each change
Shards map[int][]string `json:"shards"` // shard ID -> [primary, replica1, replica2]
Nodes map[string]NodeInfo `json:"nodes"` // node ID -> node info
Version uint64 `json:"version"` // Incremented on each change
Shards map[int][]string `json:"shards"` // shard ID -> [primary, replica1, replica2]
Nodes map[string]NodeInfo `json:"nodes"` // node ID -> node info
UpdateTime time.Time `json:"updateTime"`
}
@@ -74,23 +104,23 @@ type ClusterMessage struct {
// RebalanceRequest represents a request to rebalance shards
type RebalanceRequest struct {
RequestID string `json:"requestId"`
FromNode string `json:"fromNode"`
ToNode string `json:"toNode"`
ShardIDs []int `json:"shardIds"`
Reason string `json:"reason"`
Migrations []ActorMigration `json:"migrations"`
RequestID string `json:"requestId"`
FromNode string `json:"fromNode"`
ToNode string `json:"toNode"`
ShardIDs []int `json:"shardIds"`
Reason string `json:"reason"`
Migrations []ActorMigration `json:"migrations"`
}
// ActorMigration represents the migration of an actor between nodes
type ActorMigration struct {
ActorID string `json:"actorId"`
FromNode string `json:"fromNode"`
ToNode string `json:"toNode"`
ShardID int `json:"shardId"`
State map[string]interface{} `json:"state"`
Version int64 `json:"version"`
Status string `json:"status"` // "pending", "in_progress", "completed", "failed"
ActorID string `json:"actorId"`
FromNode string `json:"fromNode"`
ToNode string `json:"toNode"`
ShardID int `json:"shardId"`
State map[string]interface{} `json:"state"`
Version int64 `json:"version"`
Status string `json:"status"` // "pending", "in_progress", "completed", "failed"
}
// LeaderElectionCallbacks defines callbacks for leadership changes
@@ -107,4 +137,3 @@ type LeadershipLease struct {
ExpiresAt time.Time `json:"expiresAt"`
StartedAt time.Time `json:"startedAt"`
}