Skip to content

Commit c8a1ca7

Browse files
committed
Bug fix, corner case
1 parent 94d83f6 commit c8a1ca7

2 files changed

Lines changed: 38 additions & 28 deletions

File tree

‎Akka/src/main/scala/core/ConcurrentWorkerImp.scala‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ class Worker {
4747

4848
def apply(id: Int, sims: Seq[Actor], totalWorkers: Int): Behavior[WorkerEvent] = Behaviors.setup { ctx =>
4949
// ctx.log.debug("Register agent with receptionist")
50-
ctx.system.receptionist ! Receptionist.Register(WorkerUpdateAgentMapServiceKey, ctx.self)
51-
5250
// val containers: ListBuffer[Actor] = ListBuffer[Actor]()
5351
local_sims = new ConcurrentHashMap(mapAsJavaMap(sims.map(x => (x.id, x)).toMap))
5452
totalAgents = sims.size
@@ -67,18 +65,26 @@ class Worker {
6765
// } else {
6866
local_compute_threads = new ConcurrentHashMap(mapAsJavaMap(sims.map(a => (a.id, ctx.spawn((new LocalAgent).apply(a), f"simAgent${a.id}"))).toMap))
6967
// }
70-
71-
val workerSub = ctx.messageAdapter[Receptionist.Listing] {
72-
case WorkerUpdateAgentMapServiceKey.Listing(workers) =>
73-
if (workers.size == totalWorkers){
74-
workers.filter(i => i!= ctx.self).foreach(w => {
75-
w ! ReceiveAgentMap(workerId, local_sims.keys().toSeq.asInstanceOf[Iterable[java.lang.Long]], ctx.self)
76-
})
77-
}
78-
Prepare()
79-
}
80-
81-
ctx.system.receptionist ! Receptionist.Subscribe(WorkerUpdateAgentMapServiceKey, workerSub)
68+
69+
if (totalWorkers > 1){
70+
ctx.system.receptionist ! Receptionist.Register(WorkerUpdateAgentMapServiceKey, ctx.self)
71+
72+
val workerSub = ctx.messageAdapter[Receptionist.Listing] {
73+
case WorkerUpdateAgentMapServiceKey.Listing(workers) =>
74+
if (workers.size == totalWorkers){
75+
workers.filter(i => i!= ctx.self).foreach(w => {
76+
w ! ReceiveAgentMap(workerId, local_sims.keys().toSeq.asInstanceOf[Iterable[java.lang.Long]], ctx.self)
77+
})
78+
}
79+
Prepare()
80+
}
81+
82+
ctx.system.receptionist ! Receptionist.Subscribe(WorkerUpdateAgentMapServiceKey, workerSub)
83+
} else {
84+
ctx.system.receptionist ! Receptionist.Register(WorkerStartServiceKey, ctx.self)
85+
ctx.system.receptionist ! Receptionist.Register(WorkerStopServiceKey, ctx.self)
86+
}
87+
8288
// ctx.system.receptionist ! Receptionist.Subscribe(TimeseriesController.tsControllerServiceKey, workerSub)
8389
worker()
8490
}

‎Akka/src/main/scala/core/SequentialWorkerImpl.scala‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ class Worker {
4141
private var registeredWorkers: AtomicInteger = new AtomicInteger(0)
4242

4343
def apply(id: Int, sims: Seq[Actor], totalWorkers: Int): Behavior[WorkerEvent] = Behaviors.setup { ctx =>
44-
// ctx.log.debug("Register agent with receptionist")
45-
ctx.system.receptionist ! Receptionist.Register(WorkerUpdateAgentMapServiceKey, ctx.self)
46-
4744
local_sims = sims.map(x => (x.id, x)).toMap
4845
totalAgents = sims.size
4946
this.totalWorkers = totalWorkers
@@ -57,17 +54,24 @@ class Worker {
5754
})
5855
}
5956

60-
val workerSub = ctx.messageAdapter[Receptionist.Listing] {
61-
case WorkerUpdateAgentMapServiceKey.Listing(workers) =>
62-
if (workers.size == totalWorkers){
63-
workers.filter(i => i!= ctx.self).foreach(w => {
64-
w ! ReceiveAgentMap(workerId, simIds.asInstanceOf[Set[java.lang.Long]], ctx.self)
65-
})
66-
}
67-
Prepare()
68-
}
69-
70-
ctx.system.receptionist ! Receptionist.Subscribe(WorkerUpdateAgentMapServiceKey, workerSub)
57+
if (totalWorkers>1){
58+
ctx.system.receptionist ! Receptionist.Register(WorkerUpdateAgentMapServiceKey, ctx.self)
59+
60+
val workerSub = ctx.messageAdapter[Receptionist.Listing] {
61+
case WorkerUpdateAgentMapServiceKey.Listing(workers) =>
62+
if (workers.size == totalWorkers){
63+
workers.filter(i => i!= ctx.self).foreach(w => {
64+
w ! ReceiveAgentMap(workerId, simIds.asInstanceOf[Set[java.lang.Long]], ctx.self)
65+
})
66+
}
67+
Prepare()
68+
}
69+
70+
ctx.system.receptionist ! Receptionist.Subscribe(WorkerUpdateAgentMapServiceKey, workerSub)
71+
} else {
72+
ctx.system.receptionist ! Receptionist.Register(WorkerStartServiceKey, ctx.self)
73+
ctx.system.receptionist ! Receptionist.Register(WorkerStopServiceKey, ctx.self)
74+
}
7175
// ctx.system.receptionist ! Receptionist.Subscribe(TimeseriesController.tsControllerServiceKey, workerSub)
7276
worker()
7377
}

0 commit comments

Comments
 (0)