|
1 | 1 | package lerna.akka.entityreplication.raft.snapshot |
2 | 2 |
|
| 3 | +import akka.actor.testkit.typed.scaladsl.LoggingTestKit |
| 4 | +import akka.actor.typed.scaladsl.adapter.ClassicActorSystemOps |
| 5 | + |
3 | 6 | import java.util.concurrent.atomic.AtomicInteger |
4 | 7 | import akka.actor.{ ActorRef, ActorSystem } |
| 8 | +import akka.persistence.testkit.scaladsl.SnapshotTestKit |
| 9 | +import akka.persistence.testkit.{ |
| 10 | + ProcessingResult, |
| 11 | + ProcessingSuccess, |
| 12 | + SnapshotOperation, |
| 13 | + SnapshotStorage, |
| 14 | + WriteSnapshot, |
| 15 | +} |
5 | 16 | import akka.testkit.TestKit |
6 | | -import com.typesafe.config.{ Config, ConfigFactory } |
7 | 17 | import lerna.akka.entityreplication.model.{ NormalizedEntityId, TypeName } |
8 | 18 | import lerna.akka.entityreplication.raft.model.LogEntryIndex |
9 | 19 | import lerna.akka.entityreplication.raft.routing.MemberIndex |
10 | | -import lerna.akka.entityreplication.raft.snapshot.ShardSnapshotStoreFailureSpecBase._ |
11 | 20 | import lerna.akka.entityreplication.raft.snapshot.SnapshotProtocol._ |
12 | 21 | import lerna.akka.entityreplication.raft.{ ActorSpec, RaftSettings } |
13 | 22 | import lerna.akka.entityreplication.testkit.KryoSerializable |
14 | 23 |
|
15 | | -// snapshot-store のスタブを利用して snapshot の読み込みを失敗させる |
16 | | -class ShardSnapshotStoreLoadingFailureSpec |
17 | | - extends ShardSnapshotStoreFailureSpecBase( |
18 | | - SnapshotPluginStub.brokenLoadingSnapshotConfig.withFallback(ConfigFactory.load()), |
19 | | - ) { |
| 24 | +import scala.concurrent.Promise |
| 25 | + |
| 26 | +object ShardSnapshotStoreFailureSpec { |
| 27 | + final case object DummyState extends KryoSerializable |
| 28 | +} |
| 29 | + |
| 30 | +class ShardSnapshotStoreFailureSpec |
| 31 | + extends TestKit( |
| 32 | + ActorSystem("ShardSnapshotStoreFailureSpec", ShardSnapshotStoreSpecBase.configWithPersistenceTestKits), |
| 33 | + ) |
| 34 | + with ActorSpec { |
| 35 | + import ShardSnapshotStoreFailureSpec._ |
| 36 | + |
| 37 | + private val snapshotTestKit = SnapshotTestKit(system) |
| 38 | + private val typeName = TypeName.from("test") |
| 39 | + private val memberIndex = MemberIndex("test-role") |
| 40 | + |
| 41 | + override def beforeEach(): Unit = { |
| 42 | + super.beforeEach() |
| 43 | + snapshotTestKit.clearAll() |
| 44 | + snapshotTestKit.resetPolicy() |
| 45 | + } |
| 46 | + |
| 47 | + def createShardSnapshotStore(): ActorRef = |
| 48 | + planAutoKill { |
| 49 | + childActorOf( |
| 50 | + ShardSnapshotStore.props( |
| 51 | + typeName, |
| 52 | + RaftSettings(system.settings.config), |
| 53 | + memberIndex, |
| 54 | + ), |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + val entityIdSeq = new AtomicInteger(0) |
| 59 | + |
| 60 | + def generateUniqueEntityId(): NormalizedEntityId = |
| 61 | + NormalizedEntityId.from(s"test-entity-${entityIdSeq.incrementAndGet()}") |
20 | 62 |
|
21 | 63 | "ShardSnapshotStore(読み込みの異常)" should { |
22 | 64 |
|
23 | 65 | "FetchSnapshot に失敗した場合は応答無し(クライアント側でタイムアウトの実装が必要)" in { |
24 | | - val entityId = generateUniqueEntityId() |
25 | | - val shardSnapshotStore = createShardSnapshotStore() |
| 66 | + val entityId = generateUniqueEntityId() |
| 67 | + val shardSnapshotStore = createShardSnapshotStore() |
| 68 | + val snapshotStorePersistenceId = SnapshotStore.persistenceId(typeName, entityId, memberIndex) |
26 | 69 |
|
| 70 | + snapshotTestKit.failNextRead(snapshotStorePersistenceId) |
27 | 71 | shardSnapshotStore ! FetchSnapshot(entityId, replyTo = testActor) |
28 | 72 | expectNoMessage() |
29 | 73 | } |
30 | 74 | } |
31 | | -} |
32 | | - |
33 | | -// snapshot-store のスタブを利用して snapshot の永続化を失敗させる |
34 | | -class ShardSnapshotStoreSavingFailureSpec |
35 | | - extends ShardSnapshotStoreFailureSpecBase( |
36 | | - SnapshotPluginStub.brokenSavingSnapshotConfig.withFallback(ConfigFactory.load()), |
37 | | - ) { |
38 | | - |
39 | | - private[this] val dummyEntityState = EntityState(DummyState) |
40 | 75 |
|
41 | 76 | "ShardSnapshotStore(書き込みの異常)" should { |
42 | 77 |
|
43 | 78 | "SaveSnapshot に失敗した場合は SaveSnapshotFailure が返信される" in { |
44 | | - val entityId = generateUniqueEntityId() |
45 | | - val shardSnapshotStore = createShardSnapshotStore() |
46 | | - val metadata = EntitySnapshotMetadata(entityId, LogEntryIndex.initial()) |
47 | | - val snapshot = EntitySnapshot(metadata, dummyEntityState) |
| 79 | + val entityId = generateUniqueEntityId() |
| 80 | + val shardSnapshotStore = createShardSnapshotStore() |
| 81 | + val snapshotStorePersistenceId = SnapshotStore.persistenceId(typeName, entityId, memberIndex) |
| 82 | + val metadata = EntitySnapshotMetadata(entityId, LogEntryIndex.initial()) |
| 83 | + val dummyEntityState = EntityState(DummyState) |
| 84 | + val snapshot = EntitySnapshot(metadata, dummyEntityState) |
48 | 85 |
|
| 86 | + snapshotTestKit.failNextPersisted(snapshotStorePersistenceId) |
49 | 87 | shardSnapshotStore ! SaveSnapshot(snapshot, replyTo = testActor) |
50 | 88 | expectMsg(SaveSnapshotFailure(metadata)) |
51 | 89 | } |
52 | 90 | } |
53 | | -} |
54 | 91 |
|
55 | | -object ShardSnapshotStoreFailureSpecBase { |
56 | | - final case object DummyState extends KryoSerializable |
57 | | -} |
| 92 | + "ShardSnapshotStore (with time-consuming writes)" should { |
58 | 93 |
|
59 | | -abstract class ShardSnapshotStoreFailureSpecBase(config: Config) |
60 | | - extends TestKit(ActorSystem("ShardSnapshotStoreFailureSpec", config)) |
61 | | - with ActorSpec { |
| 94 | + // Emulates a time-consuming write |
| 95 | + // Note: |
| 96 | + // The promise (`processingResultPromise`) must be fulfilled. |
| 97 | + // The succeeding tests will fail unless the promise is fulfilled. |
| 98 | + class TimeConsumingWriteSnapshotPolicy extends SnapshotStorage.SnapshotPolicies.PolicyType { |
| 99 | + val processingResultPromise = Promise[ProcessingResult]() |
| 100 | + override def tryProcess(persistenceId: String, processingUnit: SnapshotOperation): ProcessingResult = { |
| 101 | + processingUnit match { |
| 102 | + case _: WriteSnapshot => processingResultPromise.future.await |
| 103 | + case _ => ProcessingSuccess |
| 104 | + } |
| 105 | + } |
| 106 | + def trySuccess(): Unit = { |
| 107 | + processingResultPromise.trySuccess(ProcessingSuccess) |
| 108 | + } |
| 109 | + } |
62 | 110 |
|
63 | | - def createShardSnapshotStore(): ActorRef = |
64 | | - planAutoKill { |
65 | | - childActorOf( |
66 | | - ShardSnapshotStore.props( |
67 | | - TypeName.from("test"), |
68 | | - RaftSettings(system.settings.config), |
69 | | - MemberIndex("test-role"), |
70 | | - ), |
71 | | - ) |
| 111 | + "reply with `SnapshotNotFound` to `FetchSnapshot` if it has no EntitySnapshot and is saving an EntitySnapshot" in { |
| 112 | + val entityId = generateUniqueEntityId() |
| 113 | + val shardSnapshotStore = createShardSnapshotStore() |
| 114 | + val metadata = EntitySnapshotMetadata(entityId, LogEntryIndex(1)) |
| 115 | + val snapshot = EntitySnapshot(metadata, EntityState(DummyState)) |
| 116 | + |
| 117 | + val timeConsumingWriteSnapshotPolicy = new TimeConsumingWriteSnapshotPolicy() |
| 118 | + try { |
| 119 | + // Prepare: SnapshotStore is saving the snapshot |
| 120 | + snapshotTestKit.withPolicy(timeConsumingWriteSnapshotPolicy) |
| 121 | + shardSnapshotStore ! SaveSnapshot(snapshot, replyTo = testActor) |
| 122 | + |
| 123 | + // Test: |
| 124 | + shardSnapshotStore ! FetchSnapshot(entityId, replyTo = testActor) |
| 125 | + expectMsg(SnapshotNotFound(entityId)) |
| 126 | + } finally { |
| 127 | + // Cleanup: |
| 128 | + // The succeeding tests will fail unless the promise is fulfilled. |
| 129 | + timeConsumingWriteSnapshotPolicy.trySuccess() |
| 130 | + } |
72 | 131 | } |
73 | 132 |
|
74 | | - val entityIdSeq = new AtomicInteger(0) |
| 133 | + "reply with `SnapshotFound` to `FetchSnapshot` if it has an EntitySnapshot and is saving a new EntitySnapshot" in { |
| 134 | + val entityId = generateUniqueEntityId() |
| 135 | + val shardSnapshotStore = createShardSnapshotStore() |
| 136 | + |
| 137 | + val firstSnapshotMetadata = EntitySnapshotMetadata(entityId, LogEntryIndex(1)) |
| 138 | + val firstSnapshot = |
| 139 | + EntitySnapshot(firstSnapshotMetadata, EntityState(DummyState)) |
| 140 | + shardSnapshotStore ! SaveSnapshot(firstSnapshot, replyTo = testActor) |
| 141 | + expectMsg(SaveSnapshotSuccess(firstSnapshotMetadata)) |
| 142 | + |
| 143 | + val timeConsumingWriteSnapshotPolicy = new TimeConsumingWriteSnapshotPolicy() |
| 144 | + try { |
| 145 | + // Prepare: SnapshotStore is saving the second snapshot |
| 146 | + snapshotTestKit.withPolicy(timeConsumingWriteSnapshotPolicy) |
| 147 | + val secondSnapshot = |
| 148 | + EntitySnapshot(EntitySnapshotMetadata(entityId, LogEntryIndex(5)), EntityState(DummyState)) |
| 149 | + shardSnapshotStore ! SaveSnapshot(secondSnapshot, replyTo = testActor) |
| 150 | + |
| 151 | + // Test: |
| 152 | + shardSnapshotStore ! FetchSnapshot(entityId, replyTo = testActor) |
| 153 | + expectMsg(SnapshotFound(firstSnapshot)) |
| 154 | + } finally { |
| 155 | + // Cleanup: |
| 156 | + // The succeeding tests will fail unless the promise is fulfilled. |
| 157 | + timeConsumingWriteSnapshotPolicy.trySuccess() |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + "reply with nothing to `SaveSnapshot` and log a warning if it is saving an EntitySnapshot" in { |
| 162 | + implicit val typedSystem: akka.actor.typed.ActorSystem[Nothing] = system.toTyped |
| 163 | + |
| 164 | + val entityId = generateUniqueEntityId() |
| 165 | + val shardSnapshotStore = createShardSnapshotStore() |
| 166 | + val metadata = EntitySnapshotMetadata(entityId, LogEntryIndex(1)) |
| 167 | + val snapshot = EntitySnapshot(metadata, EntityState(DummyState)) |
| 168 | + |
| 169 | + val timeConsumingWriteSnapshotPolicy = new TimeConsumingWriteSnapshotPolicy() |
| 170 | + try { |
| 171 | + // Prepare: SnapshotStore is saving the snapshot |
| 172 | + snapshotTestKit.withPolicy(timeConsumingWriteSnapshotPolicy) |
| 173 | + shardSnapshotStore ! SaveSnapshot(snapshot, replyTo = testActor) |
| 174 | + |
| 175 | + // Test: |
| 176 | + LoggingTestKit |
| 177 | + .warn( |
| 178 | + s"Saving snapshot for an entity ($entityId) currently. Consider to increase log-size-threshold or log-size-check-interval.", |
| 179 | + ).expect { |
| 180 | + shardSnapshotStore ! SaveSnapshot(snapshot, replyTo = testActor) |
| 181 | + } |
| 182 | + expectNoMessage() |
| 183 | + } finally { |
| 184 | + // Cleanup: |
| 185 | + // The succeeding tests will fail unless the promise is fulfilled. |
| 186 | + timeConsumingWriteSnapshotPolicy.trySuccess() |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + } |
75 | 191 |
|
76 | | - def generateUniqueEntityId(): NormalizedEntityId = |
77 | | - NormalizedEntityId.from(s"test-entity-${entityIdSeq.incrementAndGet()}") |
78 | 192 | } |
0 commit comments