fix cluster stubs: deterministic fallback, payload forwarding, and handler guards
CI / build (pull_request) Successful in 40s
CI / build (pull_request) Successful in 40s
- Replace non-deterministic map range fallback with sorted node selection in PlaceActor and RebalanceShards - Add Body field to MessagePayload to preserve message data during cross-node routing - Forward actual message body in route_message handler instead of discarding it - Add self-message guard to route_message handler to prevent loops - Add nil guard for shardMap in handleRebalanceRequest - Add self-message guard to handleRebalanceRequest in DistributedVM
This commit is contained in:
+20
-5
@@ -151,8 +151,10 @@ func (dvm *DistributedVM) SendMessage(message RuntimeMessage) error {
|
||||
// routeMessageToNode sends a message to another node for delivery to the target actor
|
||||
func (dvm *DistributedVM) routeMessageToNode(actorID string, message RuntimeMessage) error {
|
||||
hops := 0
|
||||
var body map[string]interface{}
|
||||
if mp, ok := message.(*MessagePayload); ok {
|
||||
hops = mp.Hops
|
||||
body = mp.Body
|
||||
}
|
||||
if hops >= MaxRouteHops {
|
||||
dvm.cluster.logger.Printf("Dropping message for actor %s: exceeded max hops (%d)", actorID, MaxRouteHops)
|
||||
@@ -167,6 +169,7 @@ func (dvm *DistributedVM) routeMessageToNode(actorID string, message RuntimeMess
|
||||
TargetActorID: actorID,
|
||||
Type: message.GetType(),
|
||||
Hops: hops + 1,
|
||||
Body: body,
|
||||
},
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
@@ -219,8 +222,10 @@ func (dvm *DistributedVM) handleClusterMessage(msg *nats.Msg) {
|
||||
dvm.localRuntime.LoadModel(&model)
|
||||
|
||||
case "route_message":
|
||||
// Handle message routing from other nodes
|
||||
// Re-marshal and unmarshal to convert map[string]interface{} to concrete type
|
||||
if clusterMsg.From == dvm.nodeID {
|
||||
return
|
||||
}
|
||||
|
||||
payloadBytes, err := json.Marshal(clusterMsg.Payload)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -236,11 +241,16 @@ func (dvm *DistributedVM) handleClusterMessage(msg *nats.Msg) {
|
||||
}
|
||||
|
||||
targetActor := message.TargetActorID
|
||||
msg := &MessagePayload{
|
||||
TargetActorID: targetActor,
|
||||
Type: message.Type,
|
||||
Hops: message.Hops,
|
||||
Body: message.Body,
|
||||
}
|
||||
if dvm.IsLocalActor(targetActor) {
|
||||
dvm.localRuntime.SendMessage(&message)
|
||||
dvm.localRuntime.SendMessage(msg)
|
||||
} else {
|
||||
// Relay to the correct node
|
||||
dvm.routeMessageToNode(targetActor, &message)
|
||||
dvm.routeMessageToNode(targetActor, msg)
|
||||
}
|
||||
|
||||
case "rebalance":
|
||||
@@ -260,6 +270,11 @@ func (dvm *DistributedVM) handleRebalanceRequest(msg ClusterMessage) {
|
||||
return
|
||||
}
|
||||
|
||||
if dvm.cluster.shardMap == nil {
|
||||
dvm.cluster.logger.Printf("Shard map is nil, skipping rebalance")
|
||||
return
|
||||
}
|
||||
|
||||
payloadBytes, err := json.Marshal(msg.Payload)
|
||||
if err != nil {
|
||||
dvm.cluster.logger.Printf("Failed to marshal rebalance payload: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user