Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

limit windowing support snapshoted events #195

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,16 @@ trait BaseJournalDaoWithReadMessages extends JournalDaoWithReadMessages {
toSequenceNr: Long,
batchSize: Int,
refreshInterval: Option[(FiniteDuration, Scheduler)]) = {
val firstSequenceNr: Long = Math.max(1, fromSequenceNr)
Source
.unfoldAsync[(Long, FlowControl), Seq[Try[(PersistentRepr, Long)]]]((Math.max(1, fromSequenceNr), Continue)) {
.unfoldAsync[(Long, FlowControl), Seq[Try[(PersistentRepr, Long)]]]((firstSequenceNr, Continue)) {
case (from, control) =>
def limitWindow(from: Long): Long = {
math.min(from + batchSize, toSequenceNr)
if (from == firstSequenceNr || batchSize <= 0 || (Long.MaxValue - batchSize) < from) {
toSequenceNr
} else {
Math.min(from + batchSize, toSequenceNr)
}
}

def retrieveNextBatch(): Future[Option[((Long, FlowControl), Seq[Try[(PersistentRepr, Long)]])]] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,23 @@ abstract class LimitWindowingStreamTest(configFile: String)
implicit val mat: Materializer = SystemMaterializer(system).materializer

val persistenceId = UUID.randomUUID().toString
val payload = 'a'.toByte
val writerUuid = UUID.randomUUID().toString
val payload = Array.fill(16)('a'.toByte)
val eventsPerBatch = 1000
val numberOfInsertBatches = 16
val totalMessages = numberOfInsertBatches * eventsPerBatch

withDao { dao =>
val lastInsert =
Source
.fromIterator(() => (1 to numberOfInsertBatches).toIterator)
.fromIterator(() => (1 to numberOfInsertBatches).iterator)
.mapAsync(1) { i =>
val end = i * eventsPerBatch
val start = end - (eventsPerBatch - 1)
log.info(s"batch $i (events from $start to $end")
log.info(s"batch $i - events from $start to $end")
val atomicWrites =
(start to end).map { j =>
AtomicWrite(immutable.Seq(PersistentRepr(payload, j, persistenceId)))
AtomicWrite(immutable.Seq(PersistentRepr(payload, j, persistenceId, writerUuid = writerUuid)))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oracle not allow insert null writedUuid

}
dao.asyncWriteMessages(atomicWrites).map(_ => i)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import pekko.persistence.query.Offset
import pekko.persistence.query.{ EventEnvelope, Sequence }
import pekko.testkit.TestProbe

import scala.concurrent.Future

abstract class CurrentEventsByPersistenceIdTest(config: String) extends QueryTestSpec(config) {
import QueryTestSpec.EventEnvelopeProbeOps

Expand Down Expand Up @@ -219,6 +221,42 @@ abstract class CurrentEventsByPersistenceIdTest(config: String) extends QueryTes
}
}
}

it should "return event when has been archived more than batch size" in withActorSystem { implicit system =>
import pekko.pattern.ask
import system.dispatcher
import scala.concurrent.duration._

val journalOps = new JavaDslJdbcReadJournalOperations(system)
val batchSize = readJournalConfig.maxBufferSize
withTestActors(replyToMessages = true) { (actor1, _, _) =>
def sendMessages(numberOfMessages: Int): Future[Done] = {
val futures = for (i <- 1 to numberOfMessages) yield {
actor1 ? i
}
Future.sequence(futures).map(_ => Done)
}

val numberOfEvents = batchSize << 2
val archiveEventSum = numberOfEvents >> 1
val batch = sendMessages(numberOfEvents)

// wait for acknowledgement of the batch
batch.futureValue

// and then archive some of event(delete it).
val deleteBatch = for (i <- 1 to archiveEventSum) yield {
actor1 ? DeleteCmd(i)
}
// blocking until all delete commands are processed
Future.sequence(deleteBatch).futureValue

journalOps.withCurrentEventsByPersistenceId()("my-1", 0, Long.MaxValue) { tp =>
val allEvents = tp.toStrict(atMost = 10.seconds)
allEvents.size shouldBe (numberOfEvents - archiveEventSum)
}
}
}
}

// Note: these tests use the shared-db configs, the test for all (so not only current) events use the regular db config
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 org.apache.pekko.persistence.jdbc.integration

import org.apache.pekko.persistence.jdbc.journal.dao.LimitWindowingStreamTest
import org.apache.pekko.persistence.jdbc.query.{ MysqlCleaner, OracleCleaner, PostgresCleaner, SqlServerCleaner }

class PostgresLimitWindowingStreamTest
extends LimitWindowingStreamTest("postgres-application.conf")
with PostgresCleaner

class MySQLLimitWindowingStreamTest
extends LimitWindowingStreamTest("mysql-application.conf")
with MysqlCleaner

class OracleLimitWindowingStreamTest
extends LimitWindowingStreamTest("oracle-application.conf")
with OracleCleaner

class SqlServerLimitWindowingStreamTest
extends LimitWindowingStreamTest("sqlserver-application.conf")
with SqlServerCleaner