Restructure creating cluster from config

master
kolaente 5 years ago
parent 4be37f35ea
commit 6eca934fba
Signed by: kolaente
GPG Key ID: F40E70337AB24C9B
  1. 52
      hal/cluster.go
  2. 132
      hal/cluster_test.go

@ -19,18 +19,54 @@ func (c *Cluster) Run(ctx context.Context) error {
type ModuleMapping map[MultiThreadedIOPort]MultiThreadedIOPort type ModuleMapping map[MultiThreadedIOPort]MultiThreadedIOPort
func (mapping ModuleMapping) BuildCluster() *Cluster { func (config *ClusterConfig) BuildCluster(modules map[string]*Module) (*Cluster, error) {
modules := make(map[*Module]struct{}) // We assume all modules in the connection config exist
for host, client := range mapping { for _, c := range config.Connections {
modules[host.Module] = struct{}{}
modules[client.Module] = struct{}{} // For a connection like a -> b we need two channels:
host.Module.IO[host.IOPort] = NewMultiThreadedIO(&client) // One which is the IN channel on a and the OUT channel on b
// One which is the OUT channel on a and the IN channel on b
// That means we need to create two IOs, one for each node and two channels
// The two channels then need to be passed around
// hmmmmmmm...
outConn := &MultiThreadedIOPort{
Module: modules[c.DestNode],
IOPort: c.DestPort,
}
conn := NewMultiThreadedIO(outConn)
modules[c.SourceNode].IO[c.SourcePort] = conn
} }
// for host, client := range mapping {
// modules[host.Module] = struct{}{}
// modules[client.Module] = struct{}{}
// host.Module.IO[host.IOPort] = NewMultiThreadedIO(&client)
// }
cluster := &Cluster{modules: make([]*Module, len(modules), len(modules))} cluster := &Cluster{modules: make([]*Module, len(modules), len(modules))}
var index int64 var index int64
for module := range modules { for _, module := range modules {
cluster.modules[index] = module cluster.modules[index] = module
index++ index++
} }
return cluster return cluster, nil
}
type ClusterConnection struct {
SourcePort int64 `yaml:"source_port"`
SourceNode string `yaml:"source_node"`
DestPort int64 `yaml:"dest_port"`
DestNode string `yaml:"dest_node"`
}
type ClusterNode struct {
Name string `yaml:"name"`
// Path to the hal program
Program string `yaml:"program"`
}
type ClusterConfig struct {
Nodes []*ClusterNode `yaml:"nodes"`
Connections []*ClusterConnection `yaml:"connections"`
} }

@ -1,41 +1,125 @@
package hal package hal
import ( import (
"context"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing" "testing"
) )
func TestCluster_Run(t *testing.T) { // func TestCluster_Run(t *testing.T) {
var mapping ModuleMapping // var mapping ModuleMapping
//
// firstModule := &Module{
// programStorage: map[int64]*ProgrammedInstruction{
// 1: {
// Instruction: InstructionStart,
// },
// },
// register: []float64{0},
// IO: map[int64]IO{},
// }
// secondModule := &Module{
// programStorage: map[int64]*ProgrammedInstruction{
// 1: {
// Instruction: InstructionStop,
// },
// },
// register: []float64{0},
// IO: map[int64]IO{},
// }
//
// mapping = map[MultiThreadedIOPort]MultiThreadedIOPort{
// {
// Module: firstModule,
// IOPort: 0,
// }: {
// Module: secondModule,
// IOPort: 0,
// },
// }
// assert.NoError(t, mapping.BuildCluster().Run(context.Background()))
// }
firstModule := &Module{ func TestModuleMapping_BuildCluster(t *testing.T) {
programStorage: map[int64]*ProgrammedInstruction{ config := &ClusterConfig{
1: { Nodes: []*ClusterNode{
Instruction: InstructionStart, {
Name: "node1",
},
{
Name: "node2",
},
{
Name: "node3",
}, },
}, },
register: []float64{0}, Connections: []*ClusterConnection{
IO: map[int64]IO{}, {
} SourceNode: "node1",
secondModule := &Module{ SourcePort: 1,
programStorage: map[int64]*ProgrammedInstruction{ DestNode: "node2",
1: { DestPort: 1,
Instruction: InstructionStop, },
{
SourceNode: "node1",
SourcePort: 2,
DestNode: "node2",
DestPort: 2,
},
{
SourceNode: "node2",
SourcePort: 3,
DestNode: "node3",
DestPort: 2,
},
{
SourceNode: "node1",
SourcePort: 3,
DestNode: "node3",
DestPort: 3,
}, },
}, },
register: []float64{0},
IO: map[int64]IO{},
} }
mapping = map[MultiThreadedIOPort]MultiThreadedIOPort{ modules := make(map[string]*Module)
{ modules = map[string]*Module{
Module: firstModule, "node1": {
IOPort: 0, programStorage: map[int64]*ProgrammedInstruction{
}: { 1: {Instruction: InstructionStart},
Module: secondModule, 2: {Instruction: InstructionStop},
IOPort: 0, },
register: []float64{0},
IO: map[int64]IO{},
},
"node2": {
programStorage: map[int64]*ProgrammedInstruction{
1: {Instruction: InstructionStart},
2: {Instruction: InstructionStop},
},
register: []float64{0},
IO: map[int64]IO{},
},
"node3": {
programStorage: map[int64]*ProgrammedInstruction{
1: {Instruction: InstructionStart},
2: {Instruction: InstructionStop},
},
register: []float64{0},
IO: map[int64]IO{},
}, },
} }
assert.NoError(t, mapping.BuildCluster().Run(context.Background()))
cluster, err := config.BuildCluster(modules)
assert.NoError(t, err)
// First connection config
assert.NotNil(t, cluster.modules[0].IO[1])
assert.NotNil(t, cluster.modules[1].IO[1])
// Second
assert.NotNil(t, cluster.modules[0].IO[2])
assert.NotNil(t, cluster.modules[1].IO[2])
// Third
assert.NotNil(t, cluster.modules[1].IO[3])
assert.NotNil(t, cluster.modules[2].IO[2])
// Fourth
assert.NotNil(t, cluster.modules[0].IO[3])
assert.NotNil(t, cluster.modules[2].IO[3])
} }

Loading…
Cancel
Save