-
Notifications
You must be signed in to change notification settings - Fork 16
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
perf: avoid large offset query via limit windowing #180
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
03e17a4
perf: avoid large offset query via limit windowing
Roiocam 3223785
add unit tests, simplify legacy
Roiocam 964e292
make legacy it works, new dao it
Roiocam 15a9df4
fix config
Roiocam a44ac5c
use single unit test
Roiocam fb403b3
optimized it
Roiocam 10ff3c8
fix
Roiocam dacba85
scala 2.12 build, oracle npe
Roiocam 554d78f
fix scala 2.12 build, oracle npe
Roiocam daf8bb8
optimized imports
Roiocam cf021eb
optimized import
Roiocam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...c/test/scala/org/apache/pekko/persistence/jdbc/journal/dao/LimitWindowingStreamTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* 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.journal.dao | ||
|
||
import org.apache.pekko | ||
Roiocam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import pekko.persistence.jdbc.journal.dao.LimitWindowingStreamTest.fetchSize | ||
import pekko.persistence.jdbc.query.{ H2Cleaner, QueryTestSpec } | ||
import pekko.persistence.{ AtomicWrite, PersistentRepr } | ||
import pekko.stream.scaladsl.{ Keep, Sink, Source } | ||
import pekko.stream.{ Materializer, SystemMaterializer } | ||
import com.typesafe.config.{ ConfigValue, ConfigValueFactory } | ||
import org.scalatest.concurrent.PatienceConfiguration.Timeout | ||
import org.slf4j.LoggerFactory | ||
|
||
import java.util.UUID | ||
import scala.collection.immutable | ||
import scala.concurrent.duration._ | ||
import scala.concurrent.{ Await, ExecutionContext, Future } | ||
|
||
object LimitWindowingStreamTest { | ||
val fetchSize = 100 | ||
val configOverrides: Map[String, ConfigValue] = | ||
Map("jdbc-journal.fetch-size" -> ConfigValueFactory.fromAnyRef(fetchSize)) | ||
} | ||
|
||
abstract class LimitWindowingStreamTest(configFile: String) | ||
extends QueryTestSpec(configFile, LimitWindowingStreamTest.configOverrides) { | ||
|
||
private val log = LoggerFactory.getLogger(this.getClass) | ||
|
||
it should "stream events with limit windowing" in withActorSystem { implicit system => | ||
implicit val ec: ExecutionContext = system.dispatcher | ||
implicit val mat: Materializer = SystemMaterializer(system).materializer | ||
|
||
val persistenceId = UUID.randomUUID().toString | ||
val payload = 'a'.toByte | ||
val eventsPerBatch = 1000 | ||
val numberOfInsertBatches = 16 | ||
val totalMessages = numberOfInsertBatches * eventsPerBatch | ||
|
||
withDao { dao => | ||
val lastInsert = | ||
Source | ||
.fromIterator(() => (1 to numberOfInsertBatches).toIterator) | ||
.mapAsync(1) { i => | ||
val end = i * eventsPerBatch | ||
val start = end - (eventsPerBatch - 1) | ||
log.info(s"batch $i (events from $start to $end") | ||
val atomicWrites = | ||
(start to end).map { j => | ||
AtomicWrite(immutable.Seq(PersistentRepr(payload, j, persistenceId))) | ||
} | ||
dao.asyncWriteMessages(atomicWrites).map(_ => i) | ||
} | ||
.runWith(Sink.last) | ||
|
||
lastInsert.futureValue(Timeout(totalMessages.seconds)) | ||
val readMessagesDao = dao.asInstanceOf[BaseJournalDaoWithReadMessages] | ||
val messagesSrc = | ||
readMessagesDao.internalBatchStream(persistenceId, 0, totalMessages, batchSize = fetchSize, None) | ||
|
||
val eventualSum: Future[(Int, Int)] = messagesSrc.toMat(Sink.fold((0, 0)) { case ((accBatch, accTotal), seq) => | ||
(accBatch + 1, accTotal + seq.size) | ||
})(Keep.right).run() | ||
|
||
val (batchCount, totalCount) = Await.result(eventualSum, Duration.Inf) | ||
val totalBatch = totalMessages / fetchSize | ||
batchCount shouldBe totalBatch | ||
totalCount shouldBe totalMessages | ||
} | ||
} | ||
} | ||
|
||
class H2LimitWindowingStreamTest extends LimitWindowingStreamTest("h2-application.conf") with H2Cleaner |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should add some check for
fromSequenceNr
andbatchSize
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and add checking for where
fromSequenceNr > toSequenceNr
tooThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does that mean? batch size didn't allow
zero
value, and thefromSequenceNr
was generate from acotr it self.fromSequenceNr > toSequenceNr
is enforced down below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is generally good practice to do input validation to catch failures early and make constraints explicit