Update to a working version of Scalariform

This commit is contained in:
Björn Antonsson 2016-06-02 14:06:57 +02:00
parent cae070bd93
commit c66ce62d63
616 changed files with 5966 additions and 5436 deletions

View file

@ -32,22 +32,22 @@ class ReplicatedCache extends Actor {
LWWMapKey("cache-" + math.abs(entryKey.hashCode) % 100)
def receive = {
case PutInCache(key, value)
case PutInCache(key, value) =>
replicator ! Update(dataKey(key), LWWMap(), WriteLocal)(_ + (key -> value))
case Evict(key)
case Evict(key) =>
replicator ! Update(dataKey(key), LWWMap(), WriteLocal)(_ - key)
case GetFromCache(key)
case GetFromCache(key) =>
replicator ! Get(dataKey(key), ReadLocal, Some(Request(key, sender())))
case g @ GetSuccess(LWWMapKey(_), Some(Request(key, replyTo)))
case g @ GetSuccess(LWWMapKey(_), Some(Request(key, replyTo))) =>
g.dataValue match {
case data: LWWMap[_] data.get(key) match {
case Some(value) replyTo ! Cached(key, Some(value))
case None replyTo ! Cached(key, None)
case data: LWWMap[_] => data.get(key) match {
case Some(value) => replyTo ! Cached(key, Some(value))
case None => replyTo ! Cached(key, None)
}
}
case NotFound(_, Some(Request(key, replyTo)))
case NotFound(_, Some(Request(key, replyTo))) =>
replyTo ! Cached(key, None)
case _: UpdateResponse[_] // ok
case _: UpdateResponse[_] => // ok
}
}

View file

@ -30,7 +30,7 @@ object ReplicatedMetrics {
case class UsedHeap(percentPerNode: Map[String, Double]) {
override def toString =
percentPerNode.toSeq.sortBy(_._1).map {
case (key, value) key + " --> " + value + " %"
case (key, value) => key + " --> " + value + " %"
}.mkString("\n")
}
@ -71,42 +71,42 @@ class ReplicatedMetrics(measureInterval: FiniteDuration, cleanupInterval: Finite
var nodesInCluster = Set.empty[String]
def receive = {
case Tick
case Tick =>
val heap = memoryMBean.getHeapMemoryUsage
val used = heap.getUsed
val max = heap.getMax
replicator ! Update(UsedHeapKey, LWWMap.empty[Long], WriteLocal)(_ + (node -> used))
replicator ! Update(MaxHeapKey, LWWMap.empty[Long], WriteLocal) { data
replicator ! Update(MaxHeapKey, LWWMap.empty[Long], WriteLocal) { data =>
data.get(node) match {
case Some(`max`) data // unchanged
case _ data + (node -> max)
case Some(`max`) => data // unchanged
case _ => data + (node -> max)
}
}
case c @ Changed(MaxHeapKey)
case c @ Changed(MaxHeapKey) =>
maxHeap = c.get(MaxHeapKey).entries
case c @ Changed(UsedHeapKey)
case c @ Changed(UsedHeapKey) =>
val usedHeapPercent = UsedHeap(c.get(UsedHeapKey).entries.collect {
case (key, value) if maxHeap.contains(key)
case (key, value) if maxHeap.contains(key) =>
(key -> (value.toDouble / maxHeap(key)) * 100.0)
})
log.debug("Node {} observed:\n{}", node, usedHeapPercent)
context.system.eventStream.publish(usedHeapPercent)
case _: UpdateResponse[_] // ok
case _: UpdateResponse[_] => // ok
case MemberUp(m)
case MemberUp(m) =>
nodesInCluster += nodeKey(m.address)
case MemberRemoved(m, _)
case MemberRemoved(m, _) =>
nodesInCluster -= nodeKey(m.address)
if (m.address == cluster.selfAddress)
context.stop(self)
case Cleanup
case Cleanup =>
def cleanupRemoved(data: LWWMap[Long]): LWWMap[Long] =
(data.entries.keySet -- nodesInCluster).foldLeft(data) { case (d, key) d - key }
(data.entries.keySet -- nodesInCluster).foldLeft(data) { case (d, key) => d - key }
replicator ! Update(UsedHeapKey, LWWMap.empty[Long], WriteLocal)(cleanupRemoved)
replicator ! Update(MaxHeapKey, LWWMap.empty[Long], WriteLocal)(cleanupRemoved)

View file

@ -69,7 +69,7 @@ class ServiceRegistry extends Actor with ActorLogging {
}
def receive = {
case Register(name, service)
case Register(name, service) =>
val dKey = serviceKey(name)
// store the service names in a separate GSet to be able to
// get notifications of new names
@ -78,19 +78,19 @@ class ServiceRegistry extends Actor with ActorLogging {
// add the service
replicator ! Update(dKey, ORSet(), WriteLocal)(_ + service)
case Lookup(name)
case Lookup(name) =>
sender() ! Bindings(name, services.getOrElse(name, Set.empty))
case c @ Changed(AllServicesKey)
case c @ Changed(AllServicesKey) =>
val newKeys = c.get(AllServicesKey).elements
log.debug("Services changed, added: {}, all: {}", (newKeys -- keys), newKeys)
(newKeys -- keys).foreach { dKey
(newKeys -- keys).foreach { dKey =>
// subscribe to get notifications of when services with this name are added or removed
replicator ! Subscribe(dKey, self)
}
keys = newKeys
case c @ Changed(ServiceKey(serviceName))
case c @ Changed(ServiceKey(serviceName)) =>
val name = serviceName.split(":").tail.mkString
val newServices = c.get(serviceKey(name)).elements
log.debug("Services changed for name [{}]: {}", name, newServices)
@ -99,7 +99,7 @@ class ServiceRegistry extends Actor with ActorLogging {
if (leader)
newServices.foreach(context.watch) // watch is idempotent
case LeaderChanged(node)
case LeaderChanged(node) =>
// Let one node (the leader) be responsible for removal of terminated services
// to avoid redundant work and too many death watch notifications.
// It is not critical to only do it from one node.
@ -114,14 +114,14 @@ class ServiceRegistry extends Actor with ActorLogging {
for (refs services.valuesIterator; ref refs)
context.unwatch(ref)
case Terminated(ref)
val names = services.collect { case (name, refs) if refs.contains(ref) name }
names.foreach { name
case Terminated(ref) =>
val names = services.collect { case (name, refs) if refs.contains(ref) => name }
names.foreach { name =>
log.debug("Service with name [{}] terminated: {}", name, ref)
replicator ! Update(serviceKey(name), ORSet(), WriteLocal)(_ - ref)
}
case _: UpdateResponse[_] // ok
case _: UpdateResponse[_] => // ok
}
}

View file

@ -45,18 +45,18 @@ class ShoppingCart(userId: String) extends Actor {
//#get-cart
def receiveGetCart: Receive = {
case GetCart
case GetCart =>
replicator ! Get(DataKey, readMajority, Some(sender()))
case g @ GetSuccess(DataKey, Some(replyTo: ActorRef))
case g @ GetSuccess(DataKey, Some(replyTo: ActorRef)) =>
val data = g.get(DataKey)
val cart = Cart(data.entries.values.toSet)
replyTo ! cart
case NotFound(DataKey, Some(replyTo: ActorRef))
case NotFound(DataKey, Some(replyTo: ActorRef)) =>
replyTo ! Cart(Set.empty)
case GetFailure(DataKey, Some(replyTo: ActorRef))
case GetFailure(DataKey, Some(replyTo: ActorRef)) =>
// ReadMajority failure, try again with local read
replicator ! Get(DataKey, ReadLocal, Some(replyTo))
}
@ -64,9 +64,9 @@ class ShoppingCart(userId: String) extends Actor {
//#add-item
def receiveAddItem: Receive = {
case cmd @ AddItem(item)
case cmd @ AddItem(item) =>
val update = Update(DataKey, LWWMap.empty[LineItem], writeMajority, Some(cmd)) {
cart updateCart(cart, item)
cart => updateCart(cart, item)
}
replicator ! update
}
@ -74,38 +74,38 @@ class ShoppingCart(userId: String) extends Actor {
def updateCart(data: LWWMap[LineItem], item: LineItem): LWWMap[LineItem] =
data.get(item.productId) match {
case Some(LineItem(_, _, existingQuantity))
case Some(LineItem(_, _, existingQuantity)) =>
data + (item.productId -> item.copy(quantity = existingQuantity + item.quantity))
case None data + (item.productId -> item)
case None => data + (item.productId -> item)
}
//#remove-item
def receiveRemoveItem: Receive = {
case cmd @ RemoveItem(productId)
case cmd @ RemoveItem(productId) =>
// Try to fetch latest from a majority of nodes first, since ORMap
// remove must have seen the item to be able to remove it.
replicator ! Get(DataKey, readMajority, Some(cmd))
case GetSuccess(DataKey, Some(RemoveItem(productId)))
case GetSuccess(DataKey, Some(RemoveItem(productId))) =>
replicator ! Update(DataKey, LWWMap(), writeMajority, None) {
_ - productId
}
case GetFailure(DataKey, Some(RemoveItem(productId)))
case GetFailure(DataKey, Some(RemoveItem(productId))) =>
// ReadMajority failed, fall back to best effort local value
replicator ! Update(DataKey, LWWMap(), writeMajority, None) {
_ - productId
}
case NotFound(DataKey, Some(RemoveItem(productId)))
case NotFound(DataKey, Some(RemoveItem(productId))) =>
// nothing to remove
}
//#remove-item
def receiveOther: Receive = {
case _: UpdateSuccess[_] | _: UpdateTimeout[_]
case _: UpdateSuccess[_] | _: UpdateTimeout[_] =>
// UpdateTimeout, will eventually be replicated
case e: UpdateFailure[_] throw new IllegalStateException("Unexpected failure: " + e)
case e: UpdateFailure[_] => throw new IllegalStateException("Unexpected failure: " + e)
}
}

View file

@ -35,14 +35,14 @@ class VotingService extends Actor {
replicator ! Subscribe(OpenedKey, self)
def receive = {
case Open
case Open =>
replicator ! Update(OpenedKey, Flag(), WriteAll(5.seconds))(_.switchOn)
becomeOpen()
case c @ Changed(OpenedKey) if c.get(OpenedKey).enabled
case c @ Changed(OpenedKey) if c.get(OpenedKey).enabled =>
becomeOpen()
case GetVotes
case GetVotes =>
sender() ! Votes(Map.empty, open = false)
}
@ -53,36 +53,36 @@ class VotingService extends Actor {
}
def open: Receive = {
case v @ Vote(participant)
case v @ Vote(participant) =>
val update = Update(CountersKey, PNCounterMap(), WriteLocal, request = Some(v)) {
_.increment(participant, 1)
}
replicator ! update
case _: UpdateSuccess[_]
case _: UpdateSuccess[_] =>
case Close
case Close =>
replicator ! Update(ClosedKey, Flag(), WriteAll(5.seconds))(_.switchOn)
context.become(getVotes(open = false))
case c @ Changed(ClosedKey) if c.get(ClosedKey).enabled
case c @ Changed(ClosedKey) if c.get(ClosedKey).enabled =>
context.become(getVotes(open = false))
}
def getVotes(open: Boolean): Receive = {
case GetVotes
case GetVotes =>
replicator ! Get(CountersKey, ReadAll(3.seconds), Some(GetVotesReq(sender())))
case g @ GetSuccess(CountersKey, Some(GetVotesReq(replyTo)))
case g @ GetSuccess(CountersKey, Some(GetVotesReq(replyTo))) =>
val data = g.get(CountersKey)
replyTo ! Votes(data.entries, open)
case NotFound(CountersKey, Some(GetVotesReq(replyTo)))
case NotFound(CountersKey, Some(GetVotesReq(replyTo))) =>
replyTo ! Votes(Map.empty, open)
case _: GetFailure[_]
case _: GetFailure[_] =>
case _: UpdateSuccess[_]
case _: UpdateSuccess[_] =>
}
}

View file

@ -27,7 +27,7 @@ object ServiceRegistrySpec extends MultiNodeConfig {
class Service extends Actor {
def receive = {
case s: String sender() ! self.path.name + ": " + s
case s: String => sender() ! self.path.name + ": " + s
}
}

View file

@ -72,7 +72,7 @@ class VotingServiceSpec extends MultiNodeSpec(VotingServiceSpec) with STMultiNod
val p = TestProbe()
awaitAssert {
votingService.tell(GetVotes, p.ref)
p.expectMsgPF(3.seconds) { case Votes(_, true) true }
p.expectMsgPF(3.seconds) { case Votes(_, true) => true }
}
for (n 1 to N) {
votingService ! Vote("#" + ((n % 20) + 1))
@ -83,7 +83,7 @@ class VotingServiceSpec extends MultiNodeSpec(VotingServiceSpec) with STMultiNod
votingService ! Close
}
val expected = (1 to 20).map(n "#" + n -> BigInt(3L * N / 20)).toMap
val expected = (1 to 20).map(n => "#" + n -> BigInt(3L * N / 20)).toMap
awaitAssert {
votingService ! GetVotes
expectMsg(3.seconds, Votes(expected, false))