[GitHub] rabbah commented on a change in pull request #3240: Add a loadbalancer with local state and horizontal invoker sharding.

2018-02-08 Thread GitBox
rabbah commented on a change in pull request #3240: Add a loadbalancer with 
local state and horizontal invoker sharding.
URL: 
https://github.com/apache/incubator-openwhisk/pull/3240#discussion_r166900059
 
 

 ##
 File path: 
core/controller/src/main/scala/whisk/core/loadBalancer/ShardingContainerPoolBalancer.scala
 ##
 @@ -0,0 +1,439 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package whisk.core.loadBalancer
+
+import java.nio.charset.StandardCharsets
+import java.util.concurrent.atomic.LongAdder
+import java.util.concurrent.ThreadLocalRandom
+
+import akka.actor.{Actor, ActorSystem, Props}
+import akka.cluster.ClusterEvent._
+import akka.cluster.{Cluster, Member, MemberStatus}
+import akka.event.Logging.InfoLevel
+import akka.stream.ActorMaterializer
+import org.apache.kafka.clients.producer.RecordMetadata
+import pureconfig._
+import whisk.common.{ForcableSemaphore, Logging, LoggingMarkers, TransactionId}
+import whisk.core.WhiskConfig._
+import whisk.core.connector._
+import whisk.core.entity._
+import whisk.core.{ConfigKeys, WhiskConfig}
+import whisk.spi.SpiLoader
+
+import scala.annotation.tailrec
+import scala.collection.concurrent.TrieMap
+import scala.concurrent.duration._
+import scala.concurrent.{ExecutionContext, Future, Promise}
+import scala.util.{Failure, Success}
+
+/**
+ * A loadbalancer that uses "horizontal" sharding to not collide with fellow 
loadbalancers.
+ *
+ * Horizontal sharding means, that each invoker's capacity is evenly divided 
between the loadbalancers. If an invoker
+ * has at most 16 slots available, those will be divided to 8 slots for each 
loadbalancer (if there are 2).
+ */
+class ShardingContainerPoolBalancer(config: WhiskConfig, controllerInstance: 
InstanceId)(
+  implicit val actorSystem: ActorSystem,
+  logging: Logging,
+  materializer: ActorMaterializer)
+extends LoadBalancer {
+
+  private implicit val executionContext: ExecutionContext = 
actorSystem.dispatcher
+
+  /** Build a cluster of all loadbalancers */
+  val seedNodesProvider = new 
StaticSeedNodesProvider(config.controllerSeedNodes, actorSystem.name)
+  val cluster = Cluster(actorSystem)
+  cluster.joinSeedNodes(seedNodesProvider.getSeedNodes())
+
+  /** Used to manage an action for testing invoker health */
+  private val entityStore = WhiskEntityStore.datastore(config)
+
+  /** State related to invocations and throttling */
+  private val activations = TrieMap[ActivationId, ActivationEntry]()
+  private val activationsPerNamespace = TrieMap[UUID, LongAdder]()
+  private val totalActivations = new LongAdder()
+
+  /** State needed for scheduling. */
+  private val schedulingState = ShardingContainerPoolBalancerState()()
+
+  /**
+   * Monitors invoker supervision and the cluster to update the state 
sequentially
+   *
+   * All state updates should go through this actor to guarantee, that 
`updateState` and `updateCluster` are called
+   * mutually exclusive and not concurrently.
+   */
+  private val monitor = actorSystem.actorOf(Props(new Actor {
+override def preStart(): Unit = {
+  cluster.subscribe(self, classOf[MemberEvent], classOf[ReachabilityEvent])
+}
+
+// all members of the cluster that are available
+var availableMembers = Set.empty[Member]
+
+override def receive: Receive = {
+  case CurrentInvokerPoolState(newState) =>
+schedulingState.updateInvokers(newState)
+
+  // State of the cluster as it is right now
+  case CurrentClusterState(members, _, _, _, _) =>
+availableMembers = members.filter(_.status == MemberStatus.Up)
+schedulingState.updateCluster(availableMembers.size)
+
+  // General lifecycle events and events concerning the reachability of 
members. Split-brain is not a huge concern
+  // in this case as only the invoker-threshold is adjusted according to 
the perceived cluster-size.
+  // Taking the unreachable member out of the cluster from that 
point-of-view results in a better experience
+  // even under split-brain-conditions, as that (in the worst-case) 
results in premature overloading of invokers vs.
+  // going into overflow mode prematurely.
+  case event: ClusterDomainEvent =>
+

[GitHub] rabbah commented on a change in pull request #3240: Add a loadbalancer with local state and horizontal invoker sharding.

2018-02-08 Thread GitBox
rabbah commented on a change in pull request #3240: Add a loadbalancer with 
local state and horizontal invoker sharding.
URL: 
https://github.com/apache/incubator-openwhisk/pull/3240#discussion_r166899101
 
 

 ##
 File path: common/scala/src/main/scala/whisk/common/ForcableSemaphore.scala
 ##
 @@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package whisk.common
+
+import java.util.concurrent.locks.AbstractQueuedSynchronizer
+
+import scala.annotation.tailrec
+
+/**
+ * A Semaphore, which in addition to the usual features has means to force 
more clients to get permits.
+ *
+ * Like any usual Semaphore, this implementation will give away at most 
`maxAllowed` permits when used the "usual" way.
+ * In addition to that, it also has a `forceAcquire` method which will push 
the Semaphore's remaining permits into a
+ * negative value. Getting permits using `tryAcquire` will only be possible 
once the permits value is in a positive
+ * state again.
+ *
+ * As this is (now) only used for the loadbalancer's scheduling, this does not 
implement the "whole" Java Semaphore's
+ * interface but only the methods needed.
+ *
+ * @param maxAllowed maximum number of permits given away by `tryAcquire`
+ */
+class ForcableSemaphore(maxAllowed: Int) {
+  class Sync extends AbstractQueuedSynchronizer {
 
 Review comment:
   Nesting this class seems unnecessary, but fine. 


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] rabbah commented on a change in pull request #3240: Add a loadbalancer with local state and horizontal invoker sharding.

2018-02-08 Thread GitBox
rabbah commented on a change in pull request #3240: Add a loadbalancer with 
local state and horizontal invoker sharding.
URL: 
https://github.com/apache/incubator-openwhisk/pull/3240#discussion_r166897507
 
 

 ##
 File path: common/scala/src/main/scala/whisk/common/ForcableSemaphore.scala
 ##
 @@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package whisk.common
+
+import java.util.concurrent.locks.AbstractQueuedSynchronizer
+
+import scala.annotation.tailrec
+
+/**
+ * A Semaphore, which in addition to the usual features has means to force 
more clients to get permits.
+ *
+ * Like any usual Semaphore, this implementation will give away at most 
`maxAllowed` permits when used the "usual" way.
+ * In addition to that, it also has a `forceAcquire` method which will push 
the Semaphore's remaining permits into a
+ * negative value. Getting permits using `tryAcquire` will only be possible 
once the permits value is in a positive
+ * state again.
+ *
+ * As this is (now) only used for the loadbalancer's scheduling, this does not 
implement the "whole" Java Semaphore's
+ * interface but only the methods needed.
+ *
+ * @param maxAllowed maximum number of permits given away by `tryAcquire`
+ */
+class ForcableSemaphore(maxAllowed: Int) {
+  class Sync extends AbstractQueuedSynchronizer {
 
 Review comment:
   Why is this class nested) 


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] rabbah commented on a change in pull request #3240: Add a loadbalancer with local state and horizontal invoker sharding.

2018-02-08 Thread GitBox
rabbah commented on a change in pull request #3240: Add a loadbalancer with 
local state and horizontal invoker sharding.
URL: 
https://github.com/apache/incubator-openwhisk/pull/3240#discussion_r166901098
 
 

 ##
 File path: 
core/controller/src/main/scala/whisk/core/loadBalancer/ShardingContainerPoolBalancer.scala
 ##
 @@ -0,0 +1,439 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package whisk.core.loadBalancer
+
+import java.nio.charset.StandardCharsets
+import java.util.concurrent.atomic.LongAdder
+import java.util.concurrent.ThreadLocalRandom
+
+import akka.actor.{Actor, ActorSystem, Props}
+import akka.cluster.ClusterEvent._
+import akka.cluster.{Cluster, Member, MemberStatus}
+import akka.event.Logging.InfoLevel
+import akka.stream.ActorMaterializer
+import org.apache.kafka.clients.producer.RecordMetadata
+import pureconfig._
+import whisk.common.{ForcableSemaphore, Logging, LoggingMarkers, TransactionId}
+import whisk.core.WhiskConfig._
+import whisk.core.connector._
+import whisk.core.entity._
+import whisk.core.{ConfigKeys, WhiskConfig}
+import whisk.spi.SpiLoader
+
+import scala.annotation.tailrec
+import scala.collection.concurrent.TrieMap
+import scala.concurrent.duration._
+import scala.concurrent.{ExecutionContext, Future, Promise}
+import scala.util.{Failure, Success}
+
+/**
+ * A loadbalancer that uses "horizontal" sharding to not collide with fellow 
loadbalancers.
+ *
+ * Horizontal sharding means, that each invoker's capacity is evenly divided 
between the loadbalancers. If an invoker
+ * has at most 16 slots available, those will be divided to 8 slots for each 
loadbalancer (if there are 2).
+ */
+class ShardingContainerPoolBalancer(config: WhiskConfig, controllerInstance: 
InstanceId)(
+  implicit val actorSystem: ActorSystem,
+  logging: Logging,
+  materializer: ActorMaterializer)
+extends LoadBalancer {
+
+  private implicit val executionContext: ExecutionContext = 
actorSystem.dispatcher
+
+  /** Build a cluster of all loadbalancers */
+  val seedNodesProvider = new 
StaticSeedNodesProvider(config.controllerSeedNodes, actorSystem.name)
+  val cluster = Cluster(actorSystem)
+  cluster.joinSeedNodes(seedNodesProvider.getSeedNodes())
+
+  /** Used to manage an action for testing invoker health */
+  private val entityStore = WhiskEntityStore.datastore(config)
+
+  /** State related to invocations and throttling */
+  private val activations = TrieMap[ActivationId, ActivationEntry]()
+  private val activationsPerNamespace = TrieMap[UUID, LongAdder]()
+  private val totalActivations = new LongAdder()
+
+  /** State needed for scheduling. */
+  private val schedulingState = ShardingContainerPoolBalancerState()()
+
+  /**
+   * Monitors invoker supervision and the cluster to update the state 
sequentially
+   *
+   * All state updates should go through this actor to guarantee, that 
`updateState` and `updateCluster` are called
+   * mutually exclusive and not concurrently.
+   */
+  private val monitor = actorSystem.actorOf(Props(new Actor {
+override def preStart(): Unit = {
+  cluster.subscribe(self, classOf[MemberEvent], classOf[ReachabilityEvent])
+}
+
+// all members of the cluster that are available
+var availableMembers = Set.empty[Member]
+
+override def receive: Receive = {
+  case CurrentInvokerPoolState(newState) =>
+schedulingState.updateInvokers(newState)
+
+  // State of the cluster as it is right now
+  case CurrentClusterState(members, _, _, _, _) =>
+availableMembers = members.filter(_.status == MemberStatus.Up)
+schedulingState.updateCluster(availableMembers.size)
+
+  // General lifecycle events and events concerning the reachability of 
members. Split-brain is not a huge concern
+  // in this case as only the invoker-threshold is adjusted according to 
the perceived cluster-size.
+  // Taking the unreachable member out of the cluster from that 
point-of-view results in a better experience
+  // even under split-brain-conditions, as that (in the worst-case) 
results in premature overloading of invokers vs.
+  // going into overflow mode prematurely.
+  case event: ClusterDomainEvent =>
+