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

chore: Rounding of epoch seconds #650

Merged
merged 1 commit into from
Jan 18, 2025
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 @@ -235,7 +235,8 @@ import org.slf4j.Logger
protected def appendEmptyBucketIfLastIsMissing(
buckets: IndexedSeq[Bucket],
toTimestamp: Instant): IndexedSeq[Bucket] = {
val startTimeOfLastBucket = (toTimestamp.getEpochSecond / 10) * 10
// db epoch seconds is rounding, but Instant is not
Copy link
Member Author

Choose a reason for hiding this comment

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

example with postgres:

SELECT db_timestamp, extract(EPOCH from db_timestamp)::BIGINT / 10 AS bucket from test order by db_timestamp;
         db_timestamp          |  bucket
-------------------------------+-----------
 2025-01-17 01:24:49.431768+00 | 173707708
 2025-01-17 01:24:49.631768+00 | 173707709
 2025-01-17 01:24:59.491768+00 | 173707709
 2025-01-17 01:24:59.531768+00 | 173707710
 2025-01-17 01:24:59.631768+00 | 173707710

val startTimeOfLastBucket = (toTimestamp.plusMillis(500).getEpochSecond / 10) * 10
if (buckets.last.startTime != startTimeOfLastBucket)
buckets :+ Bucket(startTimeOfLastBucket, 0)
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package akka.persistence.r2dbc.query

import java.time.Instant

import org.scalatest.wordspec.AnyWordSpecLike

import akka.actor.testkit.typed.scaladsl.LogCapturing
Expand Down Expand Up @@ -38,7 +40,8 @@ class BucketCountSpec
val slice2 = persistenceExt.sliceForPersistenceId(pid2)

val startTime = InstantFactory.now().minusSeconds(3600)
val bucketStartTime = (startTime.getEpochSecond / 10) * 10
// db epoch seconds is rounding, but Instant is not
val bucketStartTime = (startTime.plusMillis(500).getEpochSecond / 10) * 10

(0 until 10).foreach { i =>
writeEvent(slice1, pid1, 1 + i, startTime.plusSeconds(Buckets.BucketDurationSeconds * i), s"e1-$i")
Expand Down Expand Up @@ -69,7 +72,8 @@ class BucketCountSpec

val limit = 100
val startTime = InstantFactory.now().minusSeconds(3600)
val bucketStartTime = (startTime.getEpochSecond / 10) * 10
// db epoch seconds is rounding, but Instant is not
val bucketStartTime = (startTime.plusMillis(500).getEpochSecond / 10) * 10

(0 until 10).foreach { i =>
writeEvent(slice1, pid1, 1 + i, startTime.plusSeconds(Buckets.BucketDurationSeconds * i), s"e1-$i")
Expand Down