LoadBalancerService does not consider concurrent requests when scheduling
Under concurrent requests to controller, LoadBalancerService will not consider concurrent requests when scheduling to an invoker.
For example, consider LoadBalacnerServiceObjectTests.scala, with the following test
it should "consider load immediately and jump to next invoker when overloaded" in {
val invokerCount = 10
val hash = 2
val targetInvoker = hash % invokerCount
val invs = invokers(invokerCount).updated(targetInvoker, (InstanceId(targetInvoker), Healthy, 0))
val step = hashInto(LoadBalancerService.pairwiseCoprimeNumbersUntil(invokerCount), hash)
LoadBalancerService.schedule(invs, 1, hash) shouldBe Some(InstanceId((hash + 0) % invs.size))
LoadBalancerService.schedule(invs, 1, hash) shouldBe Some(InstanceId((hash + 1) % invs.size))
}
We have:
- set the existing load to 0 to start with
- set the invokerBusyThreshold to 1 on
scheduler()invocations - schedule one activation with hash=2
- schedule second activation with hash=2
In this case, but activations are scheduled to InstanceId(2), but the second should be scheduled to next invoker since busy threshold is exceeded.
I believe this is related to the changing of LoadbalancerData.activationCountPerInvoker() from sync to async. Since LoadBalancerService is processed with multiple threads, the updates to the LoadBalancerData are not seen immediately by other threads.
Good catch.
I will try to reproduce it later, but I think you are right, the fact that we read async, doesn't allow us to see the update by the second call of schedule.
Since we only write to the local instances, we could consider changing read to be synchronous, need to measure the performance overhead though.
According to akka doc the use case you are showing should be supported:
You will always read your own writes. For example if you send a Update message followed by a Get of the same key the Get will retrieve the change that was performed by the preceding Update message.
I think "reading your own writes" is only for the replicated data, which is inherently async (by way of messaging with replicator), so I'm not sure that will work (but great if it does!).
I was thinking about the following:
- revert to the LoadBalancerData to sync signatures (e.g.
def activationCountPerInvoker: Map[String, Int] - make DistributedLoadBalancerData keep 2 sets of data - one local, one replicated
- the views that DistributedLoadBalancerData exposes would be a composition of local + replicated data
Note that this requires changing the replicated format to include an indication of the controller instance that contributed the data, so that the local instance can be ignored within the set of replicated data, in favor of the more accurate local data. Basically the localdata would be accurate and the replicated data would be eventually consistent. But at least within the same controller the scheduling would be predictable under load.
This is not necessarily related to the shared state, it is also the case with the local version of the load balancer. Although that used AtomicIntegers, they were simply used as a thread-safe counter in terms of changing operations, but not used as an atomically checked-then-update counter.
Should we implement the Loadbalancer as an Actor to achieve local consistency for each Controller to start? That would solve parallel read consistency at least.
For the write/read races introduced because of state-sharing, we can come up with a smart solution as outlined by @tysonnorris to keep local state synchronous. Maybe there's a mechanism to wait for a write to happen and then implement the respective operations as asks so they "block" scheduling until that returned?
As noted by @vvraskin both of these bits would need to be tested under load but I'm confident that at least the "load balancer as an Actor" should not impose a bottleneck.
"load balancer as an Actor" is in #2932 but MesssageFeed handler method are still synchronous. I think those will need to change as well, since they manipulate the LoadBalancerData