Replace interface{} with properly defined interfaces
- Add VirtualMachine interface with GetID(), GetActorID(), GetState()
- Add VMState type with idle/running/paused/stopped states
- Add RuntimeModel interface for event storming model contracts
- Add RuntimeMessage interface for actor message contracts
- Add VMProvider interface for decoupled VM access
- Update VMRegistry.GetActiveVMs() to return map[string]VirtualMachine
- Update Runtime interface to use RuntimeModel and RuntimeMessage
- Update DistributedVMRegistry to use VMProvider instead of interface{}
- Add SetVMProvider method to DistributedVM for dependency injection
This improves type safety, makes contracts explicit, and enables better
IDE support while avoiding import cycles through interface segregation.
Closes #37
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -12,7 +12,7 @@ import (
|
||||
type DistributedVM struct {
|
||||
nodeID string
|
||||
cluster *ClusterManager
|
||||
localRuntime Runtime // Interface to avoid import cycles
|
||||
localRuntime Runtime
|
||||
sharding *ShardManager
|
||||
discovery *NodeDiscovery
|
||||
natsConn *nats.Conn
|
||||
@@ -20,17 +20,29 @@ type DistributedVM struct {
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
// Runtime interface to avoid import cycles with main aether package
|
||||
// Runtime defines the interface for a local runtime that executes actors.
|
||||
// This interface decouples the cluster package from specific runtime implementations.
|
||||
type Runtime interface {
|
||||
// Start initializes and starts the runtime
|
||||
Start() error
|
||||
LoadModel(model interface{}) error
|
||||
SendMessage(message interface{}) error
|
||||
// LoadModel loads an EventStorming model into the runtime
|
||||
LoadModel(model RuntimeModel) error
|
||||
// SendMessage sends a message to an actor in the runtime
|
||||
SendMessage(message RuntimeMessage) error
|
||||
}
|
||||
|
||||
// DistributedVMRegistry implements VMRegistry using DistributedVM's local runtime and sharding
|
||||
// DistributedVMRegistry implements VMRegistry using DistributedVM's local runtime and sharding.
|
||||
// It provides the cluster manager with access to VM information without import cycles.
|
||||
type DistributedVMRegistry struct {
|
||||
runtime interface{} // Runtime interface to avoid import cycles
|
||||
sharding *ShardManager
|
||||
vmProvider VMProvider
|
||||
sharding *ShardManager
|
||||
}
|
||||
|
||||
// VMProvider defines an interface for accessing VMs from a runtime.
|
||||
// This is used by DistributedVMRegistry to get VM information.
|
||||
type VMProvider interface {
|
||||
// GetActiveVMs returns a map of actor IDs to their VirtualMachine instances
|
||||
GetActiveVMs() map[string]VirtualMachine
|
||||
}
|
||||
|
||||
// NewDistributedVM creates a distributed VM runtime cluster node
|
||||
@@ -67,16 +79,19 @@ func NewDistributedVM(nodeID string, natsURLs []string, localRuntime Runtime) (*
|
||||
cancel: cancel,
|
||||
}
|
||||
|
||||
// Create VM registry and connect it to cluster manager
|
||||
vmRegistry := &DistributedVMRegistry{
|
||||
runtime: localRuntime,
|
||||
sharding: sharding,
|
||||
}
|
||||
cluster.SetVMRegistry(vmRegistry)
|
||||
|
||||
return dvm, nil
|
||||
}
|
||||
|
||||
// SetVMProvider sets the VM provider for the distributed VM registry.
|
||||
// This should be called after the runtime is fully initialized.
|
||||
func (dvm *DistributedVM) SetVMProvider(provider VMProvider) {
|
||||
vmRegistry := &DistributedVMRegistry{
|
||||
vmProvider: provider,
|
||||
sharding: dvm.sharding,
|
||||
}
|
||||
dvm.cluster.SetVMRegistry(vmRegistry)
|
||||
}
|
||||
|
||||
// Start begins the distributed VM cluster node
|
||||
func (dvm *DistributedVM) Start() error {
|
||||
// Start local runtime
|
||||
@@ -103,7 +118,7 @@ func (dvm *DistributedVM) Stop() {
|
||||
}
|
||||
|
||||
// LoadModel distributes EventStorming model across the cluster with VM templates
|
||||
func (dvm *DistributedVM) LoadModel(model interface{}) error {
|
||||
func (dvm *DistributedVM) LoadModel(model RuntimeModel) error {
|
||||
// Load model locally first
|
||||
if err := dvm.localRuntime.LoadModel(model); err != nil {
|
||||
return fmt.Errorf("failed to load model locally: %w", err)
|
||||
@@ -121,7 +136,7 @@ func (dvm *DistributedVM) LoadModel(model interface{}) error {
|
||||
}
|
||||
|
||||
// SendMessage routes messages across the distributed cluster
|
||||
func (dvm *DistributedVM) SendMessage(message interface{}) error {
|
||||
func (dvm *DistributedVM) SendMessage(message RuntimeMessage) error {
|
||||
// This is a simplified implementation
|
||||
// In practice, this would determine the target node based on sharding
|
||||
// and route the message appropriately
|
||||
@@ -162,13 +177,16 @@ func (dvm *DistributedVM) handleClusterMessage(msg *nats.Msg) {
|
||||
switch clusterMsg.Type {
|
||||
case "load_model":
|
||||
// Handle model loading from other nodes
|
||||
if model := clusterMsg.Payload; model != nil {
|
||||
// Note: Payload comes as interface{} from JSON, need type assertion
|
||||
// In practice, this would deserialize to the proper model type
|
||||
if model, ok := clusterMsg.Payload.(RuntimeModel); ok {
|
||||
dvm.localRuntime.LoadModel(model)
|
||||
}
|
||||
|
||||
case "route_message":
|
||||
// Handle message routing from other nodes
|
||||
if message := clusterMsg.Payload; message != nil {
|
||||
// Note: Similar type handling needed here
|
||||
if message, ok := clusterMsg.Payload.(RuntimeMessage); ok {
|
||||
dvm.localRuntime.SendMessage(message)
|
||||
}
|
||||
|
||||
@@ -208,11 +226,12 @@ func (dvm *DistributedVM) GetClusterInfo() map[string]interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
// GetActiveVMs returns a map of active VMs (implementation depends on runtime)
|
||||
func (dvr *DistributedVMRegistry) GetActiveVMs() map[string]interface{} {
|
||||
// This would need to access the actual runtime's VM registry
|
||||
// For now, return empty map to avoid import cycles
|
||||
return make(map[string]interface{})
|
||||
// GetActiveVMs returns a map of active VMs from the VM provider
|
||||
func (dvr *DistributedVMRegistry) GetActiveVMs() map[string]VirtualMachine {
|
||||
if dvr.vmProvider == nil {
|
||||
return make(map[string]VirtualMachine)
|
||||
}
|
||||
return dvr.vmProvider.GetActiveVMs()
|
||||
}
|
||||
|
||||
// GetShard returns the shard number for the given actor ID
|
||||
|
||||
Reference in New Issue
Block a user