Skip to content

Commit f42f9dd

Browse files
authored
fix: aligning sleepingBarber with csm version (#41)
1 parent 406781f commit f42f9dd

8 files changed

Lines changed: 52 additions & 59 deletions

File tree

sleepingBarber/src/main/kotlin/ac/at/uibk/dps/dapr/barber/SleepingBarber.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package ac.at.uibk.dps.dapr.barber
22

3-
import ac.at.uibk.dps.dapr.barber.barber.BarberActor
43
import ac.at.uibk.dps.dapr.barber.barber.BarberActorImpl
54
import ac.at.uibk.dps.dapr.barber.customer.CustomerActor
65
import ac.at.uibk.dps.dapr.barber.customer.CustomerActorImpl
@@ -20,7 +19,6 @@ import io.micrometer.core.instrument.util.HierarchicalNameMapper
2019
import java.nio.file.Files
2120
import java.nio.file.Paths
2221
import java.util.concurrent.TimeUnit
23-
import kotlin.apply
2422
import org.springframework.boot.ApplicationArguments
2523
import org.springframework.boot.ApplicationRunner
2624
import org.springframework.boot.autoconfigure.SpringBootApplication
@@ -61,7 +59,7 @@ class SleepingBarber {
6159

6260
fun main(args: Array<String>) {
6361
val role = System.getenv("ROLE") ?: "customer"
64-
if (role == "waiting_room")
62+
if (role == "waitingRoom")
6563
ActorRuntime.getInstance().registerActor(WaitingRoomActorImpl::class.java)
6664
if (role == "barber") ActorRuntime.getInstance().registerActor(BarberActorImpl::class.java)
6765
if (role == "customer") ActorRuntime.getInstance().registerActor(CustomerActorImpl::class.java)
@@ -77,8 +75,6 @@ class AutoStarter : ApplicationRunner {
7775
if (role == "customer") {
7876
val id = System.getenv("CUSTOMER_ID")
7977
ActorProxyBuilder(CustomerActor::class.java, client).build(ActorId(id)).request()
80-
} else if (role == "barber") {
81-
ActorProxyBuilder(BarberActor::class.java, client).build(ActorId("barber")).sleeping()
8278
}
8379
}
8480
}

sleepingBarber/src/main/kotlin/ac/at/uibk/dps/dapr/barber/barber/BarberActor.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@ import io.dapr.actors.ActorType
55

66
@ActorType(name = "BarberActor")
77
interface BarberActor {
8-
@ActorMethod(name = "sleeping") fun sleeping()
9-
108
@ActorMethod(name = "cutting") fun cutting(data: Map<String, Any>)
119
}

sleepingBarber/src/main/kotlin/ac/at/uibk/dps/dapr/barber/barber/BarberActorImpl.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
package ac.at.uibk.dps.dapr.barber.barber
22

3-
import ac.at.uibk.dps.dapr.barber.SleepingBarber.Companion.metricsRegistry
3+
import ac.at.uibk.dps.dapr.barber.SleepingBarber
44
import io.dapr.actors.ActorId
55
import io.dapr.actors.runtime.AbstractActor
66
import io.dapr.actors.runtime.ActorRuntimeContext
7-
import io.dapr.client.DaprClient
87
import io.dapr.client.DaprClientBuilder
9-
import java.lang.Thread.sleep
108
import java.security.SecureRandom
119
import java.util.concurrent.TimeUnit
1210
import kotlin.random.Random
1311
import kotlin.time.Clock
1412

1513
class BarberActorImpl(runtimeContext: ActorRuntimeContext<BarberActorImpl>, id: ActorId) :
1614
AbstractActor(runtimeContext, id), BarberActor {
17-
val client: DaprClient? = DaprClientBuilder().build()
15+
val client = DaprClientBuilder().build()
1816

1917
private val seedGenerator = SecureRandom()
2018

@@ -25,17 +23,22 @@ class BarberActorImpl(runtimeContext: ActorRuntimeContext<BarberActorImpl>, id:
2523
}
2624
}
2725

28-
override fun sleeping() {
29-
client!!.publishEvent("pubsub", "ready", getMap()).subscribe()
30-
}
26+
private val metricsRegistry = SleepingBarber.metricsRegistry
3127

3228
override fun cutting(data: Map<String, Any>) {
3329
measureEventTime(data)
3430
val customer = data["id"].toString().toInt()
35-
client!!.publishEvent("pubsub", "comeIn", getMap(customer)).subscribe()
36-
sleep(randomAround(10, 2).toLong())
31+
32+
client.publishEvent("pubsub", "comeIn", getMap(customer)).subscribe()
33+
34+
Thread.sleep(randomAround(10, 2).toLong())
35+
36+
`continue`(customer)
37+
}
38+
39+
private fun `continue`(customer: Int) {
3740
client.publishEvent("pubsub", "done", getMap(customer)).subscribe()
38-
sleeping()
41+
client.publishEvent("pubsub", "ready", getMap()).subscribe()
3942
}
4043

4144
fun randomAround(base: Int, delta: Int): Int {
@@ -62,6 +65,6 @@ class BarberActorImpl(runtimeContext: ActorRuntimeContext<BarberActorImpl>, id:
6265

6366
val deltaNanos = (nowNanos - data["time"] as Long).coerceAtLeast(0L)
6467

65-
metricsRegistry.timer("event.latency")!!.update((deltaNanos), TimeUnit.NANOSECONDS)
68+
metricsRegistry.timer("event.latency").update((deltaNanos), TimeUnit.NANOSECONDS)
6669
}
6770
}

sleepingBarber/src/main/kotlin/ac/at/uibk/dps/dapr/barber/barber/BarberPubSub.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import org.springframework.web.bind.annotation.RestController
1313
@RestController
1414
@ConditionalOnProperty("app.role", havingValue = "barber")
1515
class BarberPubSub {
16-
val barberActor: BarberActor? =
16+
val barberActor =
1717
ActorProxyBuilder(BarberActor::class.java, ActorClient()).build(ActorId("barber"))
1818

19-
@Topic(name = "cutting", pubsubName = "pubsub")
20-
@PostMapping("/cutting")
21-
fun cuttingSubscriber(@RequestBody event: CloudEvent<Map<String, Any>>) {
22-
barberActor!!.cutting(event.data)
19+
@Topic(name = "sit", pubsubName = "pubsub")
20+
@PostMapping("/sit")
21+
fun sittingSubscriber(@RequestBody event: CloudEvent<Map<String, Any>>) {
22+
barberActor.cutting(event.data)
2323
}
2424
}

sleepingBarber/src/main/kotlin/ac/at/uibk/dps/dapr/barber/customer/CustomerActorImpl.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import ac.at.uibk.dps.dapr.barber.SleepingBarber
44
import io.dapr.actors.ActorId
55
import io.dapr.actors.runtime.AbstractActor
66
import io.dapr.actors.runtime.ActorRuntimeContext
7-
import io.dapr.client.DaprClient
87
import io.dapr.client.DaprClientBuilder
98
import java.security.SecureRandom
109
import java.util.concurrent.TimeUnit
@@ -13,9 +12,8 @@ import kotlin.time.Clock
1312

1413
class CustomerActorImpl(runtimeContext: ActorRuntimeContext<CustomerActorImpl>, id: ActorId) :
1514
AbstractActor(runtimeContext, id), CustomerActor {
16-
val metricsRegistry = SleepingBarber.metricsRegistry
1715

18-
val client: DaprClient? = DaprClientBuilder().build()
16+
val client = DaprClientBuilder().build()
1917

2018
private val seedGenerator = SecureRandom()
2119
private val threadRng =
@@ -24,11 +22,12 @@ class CustomerActorImpl(runtimeContext: ActorRuntimeContext<CustomerActorImpl>,
2422
return Random(seedGenerator.nextLong())
2523
}
2624
}
25+
private val metricsRegistry = SleepingBarber.metricsRegistry
2726

2827
var count = 0
2928

3029
override fun request() {
31-
client!!.publishEvent("pubsub", "enter", getMap(id.toString().toInt())).subscribe()
30+
client.publishEvent("pubsub", "enter", getMap(id.toString().toInt())).subscribe()
3231
}
3332

3433
override fun full(data: Map<String, Any>) {
@@ -60,9 +59,7 @@ class CustomerActorImpl(runtimeContext: ActorRuntimeContext<CustomerActorImpl>,
6059

6160
val deltaNanos = (nowNanos - data["time"] as Long).coerceAtLeast(0L)
6261

63-
SleepingBarber.Companion.metricsRegistry
64-
.timer("event.latency")!!
65-
.update((deltaNanos), TimeUnit.NANOSECONDS)
62+
metricsRegistry.timer("event.latency").update((deltaNanos), TimeUnit.NANOSECONDS)
6663
}
6764

6865
private fun randomAround(base: Int, delta: Int): Int {

sleepingBarber/src/main/kotlin/ac/at/uibk/dps/dapr/barber/customer/CustomerPubSub.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,30 @@ import org.springframework.web.bind.annotation.RestController
1515
class CustomerPubSub {
1616
val id = System.getenv("CUSTOMER_ID")?.toInt() ?: 0
1717

18-
val customerProxy: CustomerActor? =
18+
val customerProxy =
1919
ActorProxyBuilder(CustomerActor::class.java, ActorClient()).build(ActorId(id.toString()))
2020

2121
@Topic(name = "full", pubsubName = "pubsub")
2222
@PostMapping("/full")
2323
fun fullSubscriber(@RequestBody event: CloudEvent<Map<String, Any>>) {
2424
if (event.data["id"] == id) {
25-
customerProxy!!.full(event.data)
25+
customerProxy.full(event.data)
2626
}
2727
}
2828

2929
@Topic(name = "comeIn", pubsubName = "pubsub")
3030
@PostMapping("/comeIn")
3131
fun comeInSubscriber(@RequestBody event: CloudEvent<Map<String, Any>>) {
3232
if (event.data["id"] == id) {
33-
customerProxy!!.comeIn(event.data)
33+
customerProxy.comeIn(event.data)
3434
}
3535
}
3636

3737
@Topic(name = "done", pubsubName = "pubsub")
3838
@PostMapping("/done")
3939
fun doneSubscriber(@RequestBody event: CloudEvent<Map<String, Any>>) {
4040
if (event.data["id"] == id) {
41-
customerProxy!!.done(event.data)
41+
customerProxy.done(event.data)
4242
}
4343
}
4444
}

sleepingBarber/src/main/kotlin/ac/at/uibk/dps/dapr/barber/waitingroom/WaitingRoomActorImpl.kt

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,50 @@ import ac.at.uibk.dps.dapr.barber.SleepingBarber.Companion.metricsRegistry
44
import io.dapr.actors.ActorId
55
import io.dapr.actors.runtime.AbstractActor
66
import io.dapr.actors.runtime.ActorRuntimeContext
7-
import io.dapr.client.DaprClient
87
import io.dapr.client.DaprClientBuilder
98
import java.util.concurrent.TimeUnit
109
import kotlin.time.Clock
1110

1211
class WaitingRoomActorImpl(runtimeContext: ActorRuntimeContext<WaitingRoomActorImpl>, id: ActorId) :
1312
AbstractActor(runtimeContext, id), WaitingRoomActor {
14-
val client: DaprClient? = DaprClientBuilder().build()
13+
val client = DaprClientBuilder().build()
1514

1615
val waiting: ArrayList<Int> = arrayListOf()
17-
18-
var barber = "sleeping"
16+
var isBarberSleeping = true
1917

2018
override fun enter(data: Map<String, Any>) {
2119
val now = Clock.System.now()
2220
val nowNanos = (now.epochSeconds * 1_000_000_000L) + now.nanosecondsOfSecond
2321
val deltaNanos = (nowNanos - data["time"] as Long).coerceAtLeast(0L)
24-
metricsRegistry.timer("event.latency")!!.update((deltaNanos), TimeUnit.NANOSECONDS)
22+
23+
metricsRegistry.timer("event.latency").update((deltaNanos), TimeUnit.NANOSECONDS)
24+
2525
val customer = data["id"].toString().toInt()
26-
if (waiting.size == 3) {
27-
client!!.publishEvent("pubsub", "full", getMap(customer)).subscribe()
28-
} else if (barber == "sleeping" && waiting.isEmpty()) {
29-
client!!.publishEvent("pubsub", "cutting", getMap(customer)).subscribe()
30-
barber = "busy"
31-
} else if (barber == "sleeping" && !waiting.isEmpty()) {
32-
waiting.add(customer)
33-
client!!.publishEvent("pubsub", "cutting", getMap(waiting[0])).subscribe()
34-
barber = "busy"
35-
waiting.removeFirst()
36-
} else if (barber == "busy" && waiting.size < 3) {
26+
27+
if (waiting.size < 3) {
3728
waiting.add(customer)
29+
if (isBarberSleeping) {
30+
isBarberSleeping = false
31+
client.publishEvent("pubsub", "sit", getMap(waiting[0])).subscribe()
32+
waiting.remove(0)
33+
}
34+
} else {
35+
client.publishEvent("pubsub", "full", getMap(customer)).subscribe()
3836
}
3937
}
4038

4139
override fun ready(data: Map<String, Any>) {
4240
val now = Clock.System.now()
4341
val nowNanos = (now.epochSeconds * 1_000_000_000L) + now.nanosecondsOfSecond
4442
val deltaNanos = (nowNanos - data["time"] as Long).coerceAtLeast(0L)
45-
metricsRegistry.timer("event.latency")!!.update((deltaNanos), TimeUnit.NANOSECONDS)
46-
barber = "sleeping"
43+
44+
metricsRegistry.timer("event.latency").update((deltaNanos), TimeUnit.NANOSECONDS)
45+
46+
isBarberSleeping = true
4747
if (!waiting.isEmpty()) {
48-
barber = "busy"
49-
client!!.publishEvent("pubsub", "cutting", getMap(waiting[0])).subscribe()
50-
waiting.removeFirst()
48+
isBarberSleeping = false
49+
client.publishEvent("pubsub", "sit", getMap(waiting[0])).subscribe()
50+
waiting.remove(0)
5151
}
5252
}
5353

sleepingBarber/src/main/kotlin/ac/at/uibk/dps/dapr/barber/waitingroom/WaitingRoomPubSub.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import io.dapr.actors.ActorId
55
import io.dapr.actors.client.ActorClient
66
import io.dapr.actors.client.ActorProxyBuilder
77
import io.dapr.client.domain.CloudEvent
8-
import kotlin.jvm.java
98
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
109
import org.springframework.web.bind.annotation.PostMapping
1110
import org.springframework.web.bind.annotation.RequestBody
@@ -14,18 +13,18 @@ import org.springframework.web.bind.annotation.RestController
1413
@RestController
1514
@ConditionalOnProperty("app.role", havingValue = "waiting_room")
1615
class WaitingRoomPubSub {
17-
val waitingRoomActor: WaitingRoomActor? =
16+
val waitingRoomActor =
1817
ActorProxyBuilder(WaitingRoomActor::class.java, ActorClient()).build(ActorId("waitingRoom"))
1918

2019
@Topic(name = "enter", pubsubName = "pubsub")
2120
@PostMapping("/enter")
2221
fun enterSubscriber(@RequestBody(required = true) event: CloudEvent<Map<String, Any>>) {
23-
waitingRoomActor!!.enter(event.data)
22+
waitingRoomActor.enter(event.data)
2423
}
2524

2625
@Topic(name = "ready", pubsubName = "pubsub")
2726
@PostMapping("/ready")
2827
fun readySubscriber(@RequestBody(required = true) event: CloudEvent<Map<String, Any>>) {
29-
waitingRoomActor!!.ready(event.data)
28+
waitingRoomActor.ready(event.data)
3029
}
3130
}

0 commit comments

Comments
 (0)