package cluster import ( "time" ) // Default configuration values const ( // 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 const ( NodeStatusActive NodeStatus = "active" NodeStatusDraining NodeStatus = "draining" NodeStatusFailed NodeStatus = "failed" ) // NodeInfo represents information about a cluster node type NodeInfo struct { ID string `json:"id"` 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 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 } // NodeUpdateType represents the type of node update type NodeUpdateType string const ( NodeJoined NodeUpdateType = "joined" NodeLeft NodeUpdateType = "left" NodeUpdated NodeUpdateType = "updated" ) // NodeUpdate represents a node status update type NodeUpdate struct { Type NodeUpdateType `json:"type"` Node *NodeInfo `json:"node"` } // 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 UpdateTime time.Time `json:"updateTime"` } // ClusterMessage represents inter-node communication type ClusterMessage struct { Type string `json:"type"` From string `json:"from"` To string `json:"to"` Payload interface{} `json:"payload"` Timestamp time.Time `json:"timestamp"` } // 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"` } // 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" } // LeaderElectionCallbacks defines callbacks for leadership changes type LeaderElectionCallbacks struct { OnBecameLeader func() OnLostLeader func() OnNewLeader func(leaderID string) } // LeadershipLease represents a leadership lease in the cluster type LeadershipLease struct { LeaderID string `json:"leaderId"` Term uint64 `json:"term"` ExpiresAt time.Time `json:"expiresAt"` StartedAt time.Time `json:"startedAt"` }