forked from ReactiveDesignPatterns/CodeSamples
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathActivePassive.scala
More file actions
803 lines (668 loc) · 28 KB
/
ActivePassive.scala
File metadata and controls
803 lines (668 loc) · 28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
/*
* Copyright (c) 2018 https://www.reactivedesignpatterns.com/
*
* Copyright (c) 2018 https://rdp.reactiveplatform.xyz/
*
*/
package chapter13
import akka.actor._
import akka.cluster.Cluster
import akka.cluster.singleton.{
ClusterSingletonManager,
ClusterSingletonManagerSettings,
ClusterSingletonProxy,
ClusterSingletonProxySettings
}
import com.typesafe.config.{ Config, ConfigFactory }
import play.api.libs.json._
import scala.collection.immutable.TreeMap
import scala.collection.{ immutable, mutable }
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.io.StdIn
import scala.util.Random
object ActivePassive {
import Persistence._
import ReplicationProtocol._
private final case class Replicate(seq: Int, key: String, value: JsValue, replyTo: ActorRef)
private final case class Replicated(seq: Int)
private case object Tick
private final case class TakeOver(replyTo: ActorRef)
private final case class InitialState(map: Map[String, JsValue], seq: Int)
object Snip13_1 {
// #snip_13-1
class Active(localReplica: ActorRef, replicationFactor: Int, maxQueueSize: Int)
extends Actor
with Stash
with ActorLogging {
private var theStore: Map[String, JsValue] = _
private var seqNr: Iterator[Int] = _
log.info("taking over from local replica")
localReplica ! TakeOver(self)
def receive: Receive = {
case InitialState(m, s) =>
log.info("took over at sequence {}", s)
theStore = m
seqNr = Iterator.from(s)
context.become(running)
unstashAll()
case _ => stash()
}
val running: Receive = ??? //...
}
// #snip_13-1
}
object Snip13_2 {
// #snip_13-2
class Active(localReplica: ActorRef, replicationFactor: Int, maxQueueSize: Int)
extends Actor
with Stash
with ActorLogging {
// ...
private val MaxOutstanding = maxQueueSize / 2
private var theStore: Map[String, JsValue] = _
private var seqNr: Iterator[Int] = _
private val toReplicate = mutable.Queue.empty[Replicate]
private var replicating = TreeMap.empty[Int, (Replicate, Int)]
private var rejected = 0
val timer: Cancellable =
context.system.scheduler.schedule(1.second, 1.second, self, Tick)(context.dispatcher)
override def postStop(): Unit = timer.cancel()
log.info("taking over from local replica")
localReplica ! TakeOver(self)
def receive: Receive = {
case InitialState(m, s) =>
log.info("took over at sequence {}", s)
theStore = m
seqNr = Iterator.from(s)
context.become(running)
unstashAll()
case _ => stash()
}
val running: Receive = {
case p @ Put(key, value, replyTo) =>
if (toReplicate.size < MaxOutstanding) {
toReplicate.enqueue(Replicate(seqNr.next, key, value, replyTo))
replicate()
} else {
rejected += 1
replyTo ! PutRejected(key, value)
}
case Get(key, replyTo) =>
replyTo ! GetResult(key, theStore.get(key))
case Tick =>
replicating.valuesIterator.foreach {
case (replicate, _) => disseminate(replicate)
}
if (rejected > 0) {
log.info("rejected {} PUT requests", rejected)
rejected = 0
}
case Replicated(confirm) =>
replicating.get(confirm) match {
case None => // already removed
case Some((rep, 1)) =>
replicating -= confirm
theStore += rep.key -> rep.value
rep.replyTo ! PutConfirmed(rep.key, rep.value)
case Some((rep, n)) =>
replicating += confirm -> (rep, n - 1)
}
replicate()
}
private def replicate(): Unit =
if (replicating.size < MaxOutstanding && toReplicate.nonEmpty) {
val r = toReplicate.dequeue()
replicating += r.seq -> (r, replicationFactor)
disseminate(r)
}
private def disseminate(r: Replicate): Unit = {
val req = r.copy(replyTo = self)
val members = Cluster(context.system).state.members
members.foreach(m => replicaOn(m.address) ! req)
}
private def replicaOn(addr: Address): ActorSelection =
context.actorSelection(localReplica.path.toStringWithAddress(addr))
}
// #snip_13-2
}
private final case class GetSingle(seq: Int, replyTo: ActorRef)
private final case class GetFull(replyTo: ActorRef)
private case object DoConsolidate
object Snip13_4 {
// #snip_13-4
class Passive(askAroundCount: Int, askAroundInterval: FiniteDuration, maxLag: Int) extends Actor with ActorLogging {
private val applied = mutable.Queue.empty[Replicate]
private val name: String =
Cluster(context.system).selfAddress.toString.replaceAll("[:/]", "_")
def receive: Receive = readPersisted(name) match {
case Database(s, kv) =>
log.info("started at sequence {}", s)
upToDate(kv, s + 1)
}
def upToDate(theStore: Map[String, JsValue], expectedSeq: Int): Receive = {
case TakeOver(active) =>
log.info("active replica starting at sequence {}", expectedSeq)
active ! InitialState(theStore, expectedSeq)
case Replicate(s, _, _, replyTo) if s - expectedSeq < 0 =>
replyTo ! Replicated(s)
case r: Replicate if r.seq == expectedSeq =>
val nextStore = theStore + (r.key -> r.value)
persist(name, expectedSeq, nextStore)
r.replyTo ! Replicated(r.seq)
applied.enqueue(r)
context.become(upToDate(nextStore, expectedSeq + 1))
case r: Replicate =>
if (r.seq - expectedSeq > maxLag)
fallBehind(expectedSeq, TreeMap(r.seq -> r))
else
missingSomeUpdates(theStore, expectedSeq, Set.empty, TreeMap(r.seq -> r))
case GetSingle(s, replyTo) =>
log.info("GetSingle from {}", replyTo)
if (applied.nonEmpty && applied.head.seq <= s && applied.last.seq >= s)
replyTo ! applied.find(_.seq == s).get
else if (s < expectedSeq) replyTo ! InitialState(theStore, expectedSeq)
case GetFull(replyTo) =>
log.info("sending full info to {}", replyTo)
replyTo ! InitialState(theStore, expectedSeq)
}
def fallBehind(expectedSeq: Int, _waiting: TreeMap[Int, Replicate]): Unit = ???
def missingSomeUpdates(
theStore: Map[String, JsValue],
expectedSeq: Int,
prevOutstanding: Set[Int],
waiting: TreeMap[Int, Replicate]): Unit = ???
}
// #snip_13-4
}
object Snip13_5 {
// #snip_13-5
class Passive(askAroundCount: Int, askAroundInterval: FiniteDuration, maxLag: Int) extends Actor with ActorLogging {
private val applied = mutable.Queue.empty[Replicate]
private var awaitingInitialState = Option.empty[ActorRef]
private val name: String =
Cluster(context.system).selfAddress.toString.replaceAll("[:/]", "_")
private val cluster = Cluster(context.system)
private val random = new Random
private var tickTask = Option.empty[Cancellable]
def scheduleTick(): Unit = {
tickTask.foreach(_.cancel())
tickTask = Some(
context.system.scheduler.scheduleOnce(askAroundInterval, self, DoConsolidate)(context.dispatcher))
}
def receive: Receive = readPersisted(name) match {
case Database(s, kv) =>
log.info("started at sequence {}", s)
upToDate(kv, s + 1)
}
def caughtUp(theStore: Map[String, JsValue], expectedSeq: Int): Unit = {
awaitingInitialState.foreach(_ ! InitialState(theStore, expectedSeq))
awaitingInitialState = None
context.become(upToDate(theStore, expectedSeq))
}
def upToDate(theStore: Map[String, JsValue], expectedSeq: Int): Receive = {
// Cases shown previously elided
case TakeOver(active) => ??? //...
case Replicate(s, _, _, replyTo) if s - expectedSeq < 0 => ??? //...
case r: Replicate if r.seq == expectedSeq => ??? //...
case r: Replicate => ??? //...
case GetSingle(s, replyTo) => ??? //...
case GetFull(replyTo) =>
log.info("sending full info to {}", replyTo)
replyTo ! InitialState(theStore, expectedSeq)
}
def fallBehind(expectedSeq: Int, _waiting: TreeMap[Int, Replicate]): Unit = {
askAroundFullState()
scheduleTick()
var waiting = _waiting
context.become {
case Replicate(s, _, _, replyTo) if s < expectedSeq =>
replyTo ! Replicated(s)
case r: Replicate =>
waiting += (r.seq -> r)
case TakeOver(active) =>
log.info("delaying active replica takeOver, at seq {} while highest is {}", expectedSeq, waiting.lastKey)
awaitingInitialState = Some(active)
case InitialState(m, s) if s > expectedSeq =>
log.info("received newer state at sequence {} (was at {})", s, expectedSeq)
persist(name, s, m)
waiting.to(s).valuesIterator.foreach(r => r.replyTo ! Replicated(r.seq))
val nextWaiting = waiting.from(expectedSeq)
consolidate(m, s + 1, Set.empty, nextWaiting)
case DoConsolidate =>
askAroundFullState()
scheduleTick()
}
}
private def consolidate(
theStore: Map[String, JsValue],
expectedSeq: Int,
askedFor: Set[Int],
waiting: TreeMap[Int, Replicate]): Unit = ??? //...
private def getMembers(n: Int): Seq[Address] = {
// using .iterator to avoid one intermediate collection to be created
random.shuffle(cluster.state.members.iterator.map(_.address).toSeq).take(n)
}
private def askAroundFullState(): Unit = {
log.info("asking for full data")
getMembers(1).foreach(addr => replicaOn(addr) ! GetFull(self))
}
private def replicaOn(addr: Address): ActorSelection =
context.actorSelection(self.path.toStringWithAddress(addr))
}
// #snip_13-5
}
object Snip13_6 {
class Passive(askAroundCount: Int, askAroundInterval: FiniteDuration, maxLag: Int) extends Actor with ActorLogging {
private val applied = mutable.Queue.empty[Replicate]
private var awaitingInitialState = Option.empty[ActorRef]
private val name = Cluster(context.system).selfAddress.toString.replaceAll("[:/]", "_")
private val cluster = Cluster(context.system)
private val random = new Random
private var tickTask = Option.empty[Cancellable]
def scheduleTick(): Unit = {
tickTask.foreach(_.cancel())
tickTask = Some(
context.system.scheduler.scheduleOnce(askAroundInterval, self, DoConsolidate)(context.dispatcher))
}
def receive: Receive = ??? //...
// #snip_13-6
private val matches = (p: (Int, Int)) => p._1 == p._2
private def consolidate(
theStore: Map[String, JsValue],
expectedSeq: Int,
askedFor: Set[Int],
waiting: TreeMap[Int, Replicate]): Unit = {
// calculate applicable prefix length
val prefix = waiting.keysIterator.zip(Iterator.from(expectedSeq)).takeWhile(matches).size
val nextStore = waiting.valuesIterator.take(prefix).foldLeft(theStore) { (store, replicate) =>
persist(name, replicate.seq, theStore)
replicate.replyTo ! Replicated(replicate.seq)
applied.enqueue(replicate)
store + (replicate.key -> replicate.value)
}
val nextWaiting = waiting.drop(prefix)
val nextExpectedSeq = expectedSeq + prefix
// cap the size of the applied buffer
applied.drop(Math.max(0, applied.size - maxLag))
if (nextWaiting.nonEmpty) {
// check if we fell behind by too much
if (nextWaiting.lastKey - nextExpectedSeq > maxLag)
fallBehind(nextExpectedSeq, nextWaiting)
else missingSomeUpdates(nextStore, nextExpectedSeq, askedFor, nextWaiting)
} else caughtUp(nextStore, nextExpectedSeq)
}
// #snip_13-6
def caughtUp(theStore: Map[String, JsValue], expectedSeq: Int): Unit = ???
def fallBehind(expectedSeq: Int, _waiting: TreeMap[Int, Replicate]): Unit = ???
def missingSomeUpdates(
theStore: Map[String, JsValue],
expectedSeq: Int,
prevOutstanding: Set[Int],
waiting: TreeMap[Int, Replicate]): Unit = ???
}
}
object Snip13_7 {
// #snip_13-7
class Passive(askAroundCount: Int, askAroundInterval: FiniteDuration, maxLag: Int) extends Actor with ActorLogging {
private val applied = mutable.Queue.empty[Replicate]
private var awaitingInitialState = Option.empty[ActorRef]
// ... Initialization elided
def receive: Receive = ??? //...
def upToDate(theStore: Map[String, JsValue], expectedSeq: Int): Receive = {
case TakeOver(active) => ??? //...
case Replicate(s, _, _, replyTo) if s - expectedSeq < 0 => ??? //...
case r: Replicate if r.seq == expectedSeq => ??? //...
case r: Replicate => ??? //...
case GetFull(replyTo) => ??? //...
case GetSingle(s, replyTo) =>
log.info("GetSingle from {}", replyTo)
if (applied.nonEmpty &&
applied.head.seq <= s && applied.last.seq >= s) {
replyTo ! applied.find(_.seq == s).get
} else if (s < expectedSeq) {
replyTo ! InitialState(theStore, expectedSeq)
}
}
def missingSomeUpdates(
theStore: Map[String, JsValue],
expectedSeq: Int,
prevOutstanding: Set[Int],
waiting: TreeMap[Int, Replicate]): Unit = {
val askFor = (expectedSeq to waiting.lastKey).iterator
.filterNot(seq =>
waiting.contains(seq) ||
prevOutstanding.contains(seq))
.toList
askFor.foreach(askAround)
if (prevOutstanding.isEmpty) {
scheduleTick()
}
val outstanding = prevOutstanding ++ askFor
context.become {
case Replicate(s, _, _, replyTo) if s < expectedSeq =>
replyTo ! Replicated(s)
case r: Replicate =>
consolidate(theStore, expectedSeq, outstanding - r.seq, waiting + (r.seq -> r))
case TakeOver(active) =>
log.info("delaying active replica takeOver, at seq {} while highest is {}", expectedSeq, waiting.lastKey)
awaitingInitialState = Some(active)
case GetSingle(s, replyTo) =>
log.info("GetSingle from {}", replyTo)
if (applied.nonEmpty &&
applied.head.seq <= s &&
applied.last.seq >= s) {
replyTo ! applied.find(_.seq == s).get
} else if (s < expectedSeq) {
replyTo ! InitialState(theStore, expectedSeq)
}
case GetFull(replyTo) =>
log.info("sending full info to {}", replyTo)
replyTo ! InitialState(theStore, expectedSeq)
case DoConsolidate =>
outstanding.foreach(askAround)
scheduleTick()
}
}
private def askAround(seq: Int): Unit = {
log.info("asking around for sequence number {}", seq)
getMembers(askAroundCount).foreach(addr => replicaOn(addr) ! GetSingle(seq, self))
}
// ... Other helpers elided
private def consolidate(
theStore: Map[String, JsValue],
expectedSeq: Int,
askedFor: Set[Int],
waiting: TreeMap[Int, Replicate]): Unit = ???
private def getMembers(n: Int): Seq[Address] = ???
private def replicaOn(addr: Address): ActorSelection = ???
def scheduleTick(): Unit = ???
}
// #snip_13-7
}
class Passive(askAroundCount: Int, askAroundInterval: FiniteDuration, maxLag: Int) extends Actor with ActorLogging {
private val applied = mutable.Queue.empty[Replicate]
private var awaitingInitialState = Option.empty[ActorRef]
private val name = Cluster(context.system).selfAddress.toString.replaceAll("[:/]", "_")
private val cluster = Cluster(context.system)
private val random = new Random
private var tickTask = Option.empty[Cancellable]
def scheduleTick(): Unit = {
tickTask.foreach(_.cancel())
tickTask = Some(context.system.scheduler.scheduleOnce(askAroundInterval, self, DoConsolidate)(context.dispatcher))
}
def receive: Receive = readPersisted(name) match {
case Database(s, kv) =>
log.info("started at sequence {}", s)
upToDate(kv, s + 1)
}
override def postStop(): Unit = {
log.info("stopped")
}
def caughtUp(theStore: Map[String, JsValue], expectedSeq: Int): Unit = {
awaitingInitialState.foreach(_ ! InitialState(theStore, expectedSeq))
awaitingInitialState = None
context.become(upToDate(theStore, expectedSeq))
}
def upToDate(theStore: Map[String, JsValue], expectedSeq: Int): Receive = {
case TakeOver(active) =>
log.info("active replica starting at sequence {}", expectedSeq)
active ! InitialState(theStore, expectedSeq)
case Replicate(s, _, _, replyTo) if s - expectedSeq < 0 =>
replyTo ! Replicated(s)
case r: Replicate if r.seq == expectedSeq =>
val nextStore = theStore + (r.key -> r.value)
persist(name, expectedSeq, nextStore)
r.replyTo ! Replicated(r.seq)
applied.enqueue(r)
context.become(upToDate(nextStore, expectedSeq + 1))
case r: Replicate =>
if (r.seq - expectedSeq > maxLag)
fallBehind(expectedSeq, TreeMap(r.seq -> r))
else
missingSomeUpdates(theStore, expectedSeq, Set.empty, TreeMap(r.seq -> r))
case GetSingle(s, replyTo) =>
log.info("GetSingle from {}", replyTo)
if (applied.nonEmpty && applied.head.seq <= s && applied.last.seq >= s)
replyTo ! applied.find(_.seq == s).get
else if (s < expectedSeq) replyTo ! InitialState(theStore, expectedSeq)
case GetFull(replyTo) =>
log.info("sending full info to {}", replyTo)
replyTo ! InitialState(theStore, expectedSeq)
}
def missingSomeUpdates(
theStore: Map[String, JsValue],
expectedSeq: Int,
prevOutstanding: Set[Int],
waiting: TreeMap[Int, Replicate]): Unit = {
val askFor = (expectedSeq to waiting.lastKey).iterator
.filterNot(seq =>
waiting.contains(seq) ||
prevOutstanding.contains(seq))
.toList
askFor.foreach(askAround)
if (prevOutstanding.isEmpty) scheduleTick()
val outstanding = prevOutstanding ++ askFor
context.become {
case Replicate(s, _, _, replyTo) if s < expectedSeq =>
replyTo ! Replicated(s)
case r: Replicate =>
consolidate(theStore, expectedSeq, outstanding - r.seq, waiting + (r.seq -> r))
case TakeOver(active) =>
log.info("delaying active replica takeOver, at seq {} while highest is {}", expectedSeq, waiting.lastKey)
awaitingInitialState = Some(active)
case GetSingle(s, replyTo) =>
log.info("GetSingle from {}", replyTo)
if (applied.nonEmpty && applied.head.seq <= s && applied.last.seq >= s)
replyTo ! applied.find(_.seq == s).get
else if (s < expectedSeq) replyTo ! InitialState(theStore, expectedSeq)
case GetFull(replyTo) =>
log.info("sending full info to {}", replyTo)
replyTo ! InitialState(theStore, expectedSeq)
case DoConsolidate =>
outstanding.foreach(askAround)
scheduleTick()
}
}
def fallBehind(expectedSeq: Int, _waiting: TreeMap[Int, Replicate]): Unit = {
askAroundFullState()
scheduleTick()
var waiting = _waiting
context.become {
case Replicate(s, _, _, replyTo) if s < expectedSeq =>
replyTo ! Replicated(s)
case r: Replicate =>
waiting += (r.seq -> r)
case TakeOver(active) =>
log.info("delaying active replica takeOver, at seq {} while highest is {}", expectedSeq, waiting.lastKey)
awaitingInitialState = Some(active)
case InitialState(m, s) if s > expectedSeq =>
log.info("received newer state at sequence {} (was at {})", s, expectedSeq)
persist(name, s, m)
waiting.to(s).valuesIterator.foreach(r => r.replyTo ! Replicated(r.seq))
val nextWaiting = waiting.from(s + 1)
consolidate(m, s + 1, Set.empty, nextWaiting)
case DoConsolidate =>
askAroundFullState()
scheduleTick()
}
}
private val matches = (p: (Int, Int)) => p._1 == p._2
private def consolidate(
theStore: Map[String, JsValue],
expectedSeq: Int,
askedFor: Set[Int],
waiting: TreeMap[Int, Replicate]): Unit = {
// calculate applicable prefix length
val prefix = waiting.keysIterator.zip(Iterator.from(expectedSeq)).takeWhile(matches).size
val nextStore = waiting.valuesIterator.take(prefix).foldLeft(theStore) { (store, replicate) =>
persist(name, replicate.seq, theStore)
replicate.replyTo ! Replicated(replicate.seq)
applied.enqueue(replicate)
store + (replicate.key -> replicate.value)
}
val nextWaiting = waiting.drop(prefix)
val nextExpectedSeq = expectedSeq + prefix
// cap the size of the applied buffer
applied.drop(Math.max(0, applied.size - maxLag))
if (nextWaiting.nonEmpty) {
// check if we fell behind by too much
if (nextWaiting.lastKey - nextExpectedSeq > maxLag)
fallBehind(nextExpectedSeq, nextWaiting)
else missingSomeUpdates(nextStore, nextExpectedSeq, askedFor, nextWaiting)
} else caughtUp(nextStore, nextExpectedSeq)
}
private def getMembers(n: Int): Seq[Address] = {
// using .iterator to avoid one intermediate collection to be created
random.shuffle(cluster.state.members.iterator.map(_.address).toSeq).take(n)
}
private def askAround(seq: Int): Unit = {
log.info("asking around for sequence number {}", seq)
getMembers(askAroundCount).foreach(addr => replicaOn(addr) ! GetSingle(seq, self))
}
private def askAroundFullState(): Unit = {
log.info("asking for full data")
getMembers(1).foreach(addr => replicaOn(addr) ! GetFull(self))
}
private def replicaOn(addr: Address): ActorSelection =
context.actorSelection(self.path.toStringWithAddress(addr))
}
val commonConfig: Config = ConfigFactory.parseString("""
akka.actor.provider = akka.cluster.ClusterActorRefProvider
akka.remote.netty.tcp {
host = "127.0.0.1"
port = 0
}
akka.cluster {
gossip-interval = 100ms
failure-detector {
heartbeat-interval = 100ms
acceptable-heartbeat-pause = 500ms
}
}
""")
def roleConfig(name: String, port: Option[Int]): Config = {
val roles = ConfigFactory.parseString(s"""akka.cluster.roles = ["$name"]""")
port match {
case None => roles
case Some(p) =>
ConfigFactory.parseString(s"""akka.remote.netty.tcp.port = $p""").withFallback(roles)
}
}
def start(port: Option[Int]): ActorSystem = {
val system = ActorSystem("ActivePassive", roleConfig("backend", port).withFallback(commonConfig))
val localReplica = system.actorOf(Props(new Passive(3, 3.seconds, 100)), "passive")
val settings = ClusterSingletonManagerSettings(system)
.withSingletonName("active")
.withRole("backend")
.withHandOverRetryInterval(150.millis)
val managerProps =
ClusterSingletonManager.props(Props(new Snip13_2.Active(localReplica, 2, 120)), PoisonPill, settings)
val manager = system.actorOf(managerProps, "activeManager")
system
}
def main(args: Array[String]): Unit = {
val systems = Array.fill(5)(start(None))
val seedNode = Cluster(systems(0)).selfAddress
systems.foreach(Cluster(_).join(seedNode))
val sys = ActorSystem("ActivePassive", ConfigFactory.parseString("akka.loglevel=INFO").withFallback(commonConfig))
Cluster(sys).join(seedNode)
awaitMembers(sys, systems.length + 1)
val proxySettings = ClusterSingletonProxySettings(sys).withRole("backend").withSingletonName("active")
val proxy = sys.actorOf(ClusterSingletonProxy.props("/user/activeManager", proxySettings), "proxy")
val useStorage = sys.actorOf(Props(new UseStorage(proxy)), "useStorage")
useStorage ! Run(0)
sys.actorOf(Props(new Actor {
def receive: Receive = {
case Run =>
StdIn.readLine()
useStorage ! Stop
}
})) ! Run
Thread.sleep(10000)
val rnd = new Random
while (!terminate) {
Thread.sleep(5000)
val sysIdx = rnd.nextInt(systems.length)
val oldSys = systems(sysIdx)
val port = Cluster(oldSys).selfAddress.port
Await.ready(oldSys.terminate(), Duration.Inf)
val newSys = start(port)
val seed = Cluster(if (sysIdx == 0) systems(1) else systems(0)).selfAddress
Cluster(newSys).join(seed)
systems(sysIdx) = newSys
awaitMembers(sys, systems.length + 1)
}
Thread.sleep(3000)
sys.terminate()
systems.foreach(_.terminate())
}
private def awaitMembers(sys: ActorSystem, count: Int): Unit = {
while (Cluster(sys).state.members.size < count) {
Thread.sleep(500)
print('.')
Console.flush()
}
println("cluster started")
}
private final case class Run(round: Int)
private case object Stop
@volatile private var terminate = false
private class UseStorage(db: ActorRef) extends Actor with ActorLogging {
private val N = 200
private var theStore = Map.empty[String, JsValue]
private val keys: immutable.IndexedSeq[String] = (1 to N).map(i => f"$i%03d")
private var outstanding = Set.empty[String]
private val rnd = new Random
private var lastOutstandingCount = 0
def receive: Receive = {
case Run(0) =>
db ! Get("initial", self)
case GetResult("initial", _) =>
self ! Run(1)
case Run(round) =>
if (round % 100 == 0) log.info("round {}", round)
val nowOutstanding = outstanding.size
if (nowOutstanding != lastOutstandingCount) {
lastOutstandingCount = nowOutstanding
log.info("{} outstanding", nowOutstanding)
}
for (k <- keys) {
db ! Get(k, self)
if (!outstanding.contains(k) && rnd.nextBoolean()) {
db ! Put(k, JsNumber(round), self)
outstanding += k
}
}
context.system.scheduler.scheduleOnce(100.millis, self, Run(round + 1))(context.dispatcher)
case GetResult(key, value) =>
if (outstanding.contains(key)) {
outstanding -= key
value.foreach(theStore += key -> _)
} else if (value != theStore.get(key)) {
log.warning("returned wrong value for key {}: {} (expected {})", key, value, theStore.get(key))
context.stop(self)
}
case PutConfirmed(key, value) =>
outstanding -= key
theStore += key -> value
case PutRejected(key, value) =>
outstanding -= key
case Stop => context.stop(self)
}
override def postStop(): Unit = terminate = true
}
/*
* Problems:
* - when after TakeOver the localReplica gets a full map that has newer info, we potentially
* accepted conflicting writes
* - after TakeOver we should probably do a reconciliation phase that brings all known replicas
* up to speed
* - but if a replica with newer data joins later then we’ll need to wipe its contents and roll
* it back because a different universe has won
* - in general there is the question of when to wipe a local copy that gets kicked out: we
* want that for cases where it was in a losing partition but NOT when the whole cluster goes
* down
* - I guess that this can only be reconciled later when joining things back together.
*/
}