|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.pekko.persistence.journal |
| 19 | + |
| 20 | +import org.apache.pekko.persistence.{ AtomicWrite, JournalProtocol, PersistenceSpec, PersistentRepr } |
| 21 | +import org.apache.pekko.testkit.ImplicitSender |
| 22 | + |
| 23 | +import scala.collection.immutable |
| 24 | +import scala.concurrent.{ ExecutionContext, Future, Promise } |
| 25 | +import scala.util.Try |
| 26 | + |
| 27 | +/** |
| 28 | + * Verifies write response ordering logic for [[AsyncWriteJournal]]. |
| 29 | + * |
| 30 | + * Checkout write-response-global-order config option for more information. |
| 31 | + */ |
| 32 | +class AsyncWriteJournalResponseOrderSpec |
| 33 | + extends PersistenceSpec( |
| 34 | + PersistenceSpec.config( |
| 35 | + plugin = "", // we will provide explicit plugin IDs later |
| 36 | + test = classOf[AsyncWriteJournalResponseOrderSpec].getSimpleName, |
| 37 | + extraConfig = Some( |
| 38 | + s""" |
| 39 | + |pekko.persistence.journal.reverse-plugin { |
| 40 | + | with-global-order { |
| 41 | + | class = "${classOf[AsyncWriteJournalResponseOrderSpec.ReversePlugin].getName}" |
| 42 | + | |
| 43 | + | write-response-global-order = on |
| 44 | + | } |
| 45 | + | no-global-order { |
| 46 | + | class = "${classOf[AsyncWriteJournalResponseOrderSpec.ReversePlugin].getName}" |
| 47 | + | |
| 48 | + | write-response-global-order = off |
| 49 | + | } |
| 50 | + |} |
| 51 | + |""".stripMargin |
| 52 | + ))) with ImplicitSender { |
| 53 | + |
| 54 | + import AsyncWriteJournalResponseOrderSpec._ |
| 55 | + |
| 56 | + "AsyncWriteJournal" must { |
| 57 | + "return write responses in request order if global response order is enabled" in { |
| 58 | + val pluginRef = |
| 59 | + extension.journalFor(journalPluginId = "pekko.persistence.journal.reverse-plugin.with-global-order") |
| 60 | + |
| 61 | + pluginRef ! mkWriteMessages(1) |
| 62 | + pluginRef ! mkWriteMessages(2) |
| 63 | + pluginRef ! mkWriteMessages(3) |
| 64 | + |
| 65 | + pluginRef ! CompleteWriteOps |
| 66 | + |
| 67 | + getMessageNumsFromResponses(receiveN(6)) shouldEqual Vector(1, 2, 3) |
| 68 | + } |
| 69 | + |
| 70 | + "return write responses in completion order if global response order is disabled" in { |
| 71 | + val pluginRef = |
| 72 | + extension.journalFor(journalPluginId = "pekko.persistence.journal.reverse-plugin.no-global-order") |
| 73 | + |
| 74 | + pluginRef ! mkWriteMessages(1) |
| 75 | + pluginRef ! mkWriteMessages(2) |
| 76 | + pluginRef ! mkWriteMessages(3) |
| 77 | + |
| 78 | + pluginRef ! CompleteWriteOps |
| 79 | + |
| 80 | + getMessageNumsFromResponses(receiveN(6)) shouldEqual Vector(3, 2, 1) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private def mkWriteMessages(num: Int): JournalProtocol.WriteMessages = JournalProtocol.WriteMessages( |
| 85 | + messages = Vector(AtomicWrite(PersistentRepr( |
| 86 | + payload = num, |
| 87 | + sequenceNr = 0L, |
| 88 | + persistenceId = num.toString |
| 89 | + ))), |
| 90 | + persistentActor = self, |
| 91 | + actorInstanceId = 1 |
| 92 | + ) |
| 93 | + |
| 94 | + private def getMessageNumsFromResponses(responses: Seq[AnyRef]): Vector[Int] = responses.collect { |
| 95 | + case successResponse: JournalProtocol.WriteMessageSuccess => |
| 96 | + successResponse.persistent.payload.asInstanceOf[Int] |
| 97 | + }.toVector |
| 98 | +} |
| 99 | + |
| 100 | +private object AsyncWriteJournalResponseOrderSpec { |
| 101 | + case object CompleteWriteOps |
| 102 | + |
| 103 | + /** |
| 104 | + * Accumulates asyncWriteMessages requests and completes them in reverse receive order on [[CompleteWriteOps]] command |
| 105 | + */ |
| 106 | + class ReversePlugin extends AsyncWriteJournal { |
| 107 | + |
| 108 | + private implicit val ec: ExecutionContext = context.dispatcher |
| 109 | + |
| 110 | + private var pendingOps: Vector[Promise[Unit]] = Vector.empty |
| 111 | + |
| 112 | + override def receivePluginInternal: Receive = { |
| 113 | + case CompleteWriteOps => |
| 114 | + pendingOps.reverse.foreach(_.success(())) |
| 115 | + pendingOps = Vector.empty |
| 116 | + } |
| 117 | + |
| 118 | + override def asyncWriteMessages(messages: immutable.Seq[AtomicWrite]): Future[immutable.Seq[Try[Unit]]] = { |
| 119 | + val responsePromise = Promise[Unit]() |
| 120 | + pendingOps = pendingOps :+ responsePromise |
| 121 | + responsePromise.future.map(_ => Vector.empty) |
| 122 | + } |
| 123 | + |
| 124 | + override def asyncDeleteMessagesTo(persistenceId: String, toSequenceNr: Long): Future[Unit] = ??? |
| 125 | + |
| 126 | + override def asyncReplayMessages(persistenceId: String, fromSequenceNr: Long, toSequenceNr: Long, max: Long)( |
| 127 | + recoveryCallback: PersistentRepr => Unit): Future[Unit] = ??? |
| 128 | + |
| 129 | + override def asyncReadHighestSequenceNr(persistenceId: String, fromSequenceNr: Long): Future[Long] = ??? |
| 130 | + } |
| 131 | +} |
0 commit comments