Browse Source

Add run method and GetTarget() to simulation nodes

pull/1/head
Stephanie Gredell 7 months ago
parent
commit
0c8333cf3f
  1. 4
      internal/simulation/cachenode.go
  2. 4
      internal/simulation/cdnnode.go
  3. 4
      internal/simulation/databasenode.go
  4. 3
      internal/simulation/datapipelinenode.go
  5. 55
      internal/simulation/engine.go
  6. 4
      internal/simulation/messagequeuenode.go
  7. 4
      internal/simulation/microservicenode.go
  8. 3
      internal/simulation/monitoringnode.go
  9. 4
      internal/simulation/thirdpartyservicenode.go
  10. 4
      internal/simulation/webservernode.go

4
internal/simulation/cachenode.go

@ -105,3 +105,7 @@ func (c *CacheNode) Emit() []*Request {
func (c *CacheNode) AddTarget(targetID string) { func (c *CacheNode) AddTarget(targetID string) {
c.Targets = append(c.Targets, targetID) c.Targets = append(c.Targets, targetID)
} }
func (c *CacheNode) GetTargets() []string {
return c.Targets
}

4
internal/simulation/cdnnode.go

@ -83,3 +83,7 @@ func (n *CDNNode) Emit() []*Request {
return out return out
} }
func (n *CDNNode) GetTargets() []string {
return n.Targets
}

4
internal/simulation/databasenode.go

@ -77,3 +77,7 @@ func (n *DatabaseNode) Emit() []*Request {
n.ReplicationQueue = n.ReplicationQueue[:0] n.ReplicationQueue = n.ReplicationQueue[:0]
return out return out
} }
func (n *DatabaseNode) GetTargets() []string {
return n.Targets
}

3
internal/simulation/datapipelinenode.go

@ -105,3 +105,6 @@ func (n *DataPipelineNode) Emit() []*Request {
func (n *DataPipelineNode) Type() string { return "datapipeline" } func (n *DataPipelineNode) Type() string { return "datapipeline" }
func (n *DataPipelineNode) IsAlive() bool { return n.Alive } func (n *DataPipelineNode) IsAlive() bool { return n.Alive }
func (n *DataPipelineNode) GetTargets() []string {
return n.Targets
}

55
internal/simulation/engine.go

@ -20,6 +20,7 @@ type SimulationNode interface {
Receive(req *Request) Receive(req *Request)
Emit() []*Request Emit() []*Request
IsAlive() bool IsAlive() bool
GetTargets() []string
} }
type Engine struct { type Engine struct {
@ -32,7 +33,13 @@ type Engine struct {
type TickSnapshot struct { type TickSnapshot struct {
TickMs int TickMs int
QueueSizes map[string]int QueueSizes map[string]int
NodeHealth map[string]string NodeHealth map[string]NodeState
Emitted map[string][]*Request
}
type NodeState struct {
QueueSize int
Alive bool
} }
func NewEngineFromDesign(design design.Design, duration int, tickMs int) *Engine { func NewEngineFromDesign(design design.Design, duration int, tickMs int) *Engine {
@ -123,8 +130,16 @@ func NewEngineFromDesign(design design.Design, duration int, tickMs int) *Engine
Alive: true, Alive: true,
} }
case "monitoring/alerting": case "monitoring/alerting":
// Simulation not implemented yet; optionally skip simNode = &MonitoringNode{
continue ID: n.ID,
Label: n.Props["label"].(string),
Tool: n.Props["tool"].(string),
AlertMetric: n.Props["metric"].(string),
ThresholdValue: int(n.Props["threshold"].(float64)),
ThresholdUnit: n.Props["unit"].(string),
Queue: []*Request{},
Alive: true,
}
default: default:
continue continue
} }
@ -149,3 +164,37 @@ func NewEngineFromDesign(design design.Design, duration int, tickMs int) *Engine
TickMs: tickMs, TickMs: tickMs,
} }
} }
func (e *Engine) Run() {
const tickMS = 100
currentTimeMs := 0
for tick := 0; tick < e.Duration; tick++ {
snapshot := TickSnapshot{
TickMs: tick,
NodeHealth: make(map[string]NodeState),
}
for _, node := range e.Nodes {
node.Tick(tick, currentTimeMs)
snapshot.NodeHealth[node.GetID()] = NodeState{
QueueSize: len(node.Emit()),
Alive: node.IsAlive(),
}
}
for _, node := range e.Nodes {
for _, req := range node.Emit() {
for _, targetID := range node.GetTargets() {
if target, ok := e.Nodes[targetID]; ok && target.IsAlive() {
target.Receive(req)
}
}
}
}
e.Timeline = append(e.Timeline, snapshot)
currentTimeMs += tickMS
}
}

4
internal/simulation/messagequeuenode.go

@ -91,3 +91,7 @@ func (n *MessageQueueNode) Emit() []*Request {
return allRequests return allRequests
} }
func (n *MessageQueueNode) GetTargets() []string {
return n.Targets
}

4
internal/simulation/microservicenode.go

@ -33,6 +33,10 @@ func (n *MicroserviceNode) Emit() []*Request {
return out return out
} }
func (n *MicroserviceNode) GetTargets() []string {
return n.Targets
}
func (n *MicroserviceNode) Tick(tick int, currentTimeMs int) { func (n *MicroserviceNode) Tick(tick int, currentTimeMs int) {
if !n.Alive { if !n.Alive {
return return

3
internal/simulation/monitoringnode.go

@ -64,3 +64,6 @@ func (n *MonitoringNode) Tick(tick int, currentTimeMs int) {
n.Queue = nil n.Queue = nil
} }
func (n *MonitoringNode) GetTargets() []string {
return n.Targets
}

4
internal/simulation/thirdpartyservicenode.go

@ -69,6 +69,10 @@ func (n *ThirdPartyServiceNode) Tick(tick int, currentTimeMs int) {
} }
} }
func (n *ThirdPartyServiceNode) GetTargets() []string {
return n.Targets
}
// --- Helpers --- // --- Helpers ---
func simulateThirdPartySuccess(req *Request) bool { func simulateThirdPartySuccess(req *Request) bool {

4
internal/simulation/webservernode.go

@ -67,3 +67,7 @@ func min(a, b int) int {
func (ws *WebServerNode) GetQueue() []*Request { func (ws *WebServerNode) GetQueue() []*Request {
return ws.Queue return ws.Queue
} }
func (ws *WebServerNode) GetTargets() []string {
return ws.Targets
}

Loading…
Cancel
Save