Hi Hakkers,
I came across some surprising behaviour with the LocalSnapshotStore - I was
trying to implement what I imagine would be a common pattern: remove older
snapshots each time a new snapshot is created. I did this with
`deleteSnapshots(SnapshotSelectionCriteria(meta.sequenceNr, meta.timestamp
- 1))`, only to find it would delete all snapshots for the given
persistence ID and sequenceNr, regardless of timestamp given.
After staring at my own code for a very long time, I looked at the source
for LocalSnapshotStore and found that it seems to ignore timestamps
altogether:
https://github.com/akka/akka/blob/master/akka-persistence/src/main/scala/akka/persistence/snapshot/local/LocalSnapshotStore.scala#L161
I initially thought this to be a bug, but looking at the history it appears
to have been intentionally removed:
https://github.com/akka/akka/commit/63baaf1b2b3d0fc6c1971307fd1e66d74106c968
I was able to fix this with a minor change in the attached diff. Is there
any interest in this fix? Or am I naive to some history around this?
I guess if it's the intended behaviour, I can easily enough subclass it and
override the relevant bits to take timestamps into account - does that
sound reasonable?
Thanks for Akka, it's amazing!
--
Stephen McDonald
http://jupo.org
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.
diff --git
a/akka-persistence/src/main/scala/akka/persistence/snapshot/local/LocalSnapshotStore.scala
b/akka-persistence/src/main/scala/akka/persistence/snapshot/local/LocalSnapshotStore.scala
index 4dc1ff6..b513816 100644
---
a/akka-persistence/src/main/scala/akka/persistence/snapshot/local/LocalSnapshotStore.scala
+++
b/akka-persistence/src/main/scala/akka/persistence/snapshot/local/LocalSnapshotStore.scala
@@ -25,6 +25,7 @@ import scala.util._
* Local filesystem backed snapshot store.
*/
private[persistence] class LocalSnapshotStore extends SnapshotStore with
ActorLogging {
+
private val FilenamePattern = """^snapshot-(.+)-(\d+)-(\d+)""".r
import akka.util.Helpers._
@@ -82,7 +83,7 @@ private[persistence] class LocalSnapshotStore extends
SnapshotStore with ActorLo
private def snapshotFiles(metadata: SnapshotMetadata): immutable.Seq[File] =
{
// pick all files for this persistenceId and sequenceNr, old journals
could have created multiple entries with appended timestamps
- snapshotDir.listFiles(new
SnapshotSeqNrFilenameFilter(metadata.persistenceId,
metadata.sequenceNr)).toVector
+ snapshotDir.listFiles(new
SnapshotSeqNrFilenameFilter(metadata.persistenceId, metadata.sequenceNr,
metadata.timestamp)).toVector
}
@scala.annotation.tailrec
@@ -156,13 +157,14 @@ private[persistence] class LocalSnapshotStore extends
SnapshotStore with ActorLo
}
}
- private final class SnapshotSeqNrFilenameFilter(persistenceId: String,
sequenceNr: Long) extends FilenameFilter {
- private final def matches(pid: String, snr: String): Boolean =
- pid.equals(URLEncoder.encode(persistenceId)) && Try(snr.toLong ==
sequenceNr).getOrElse(false)
+ private final class SnapshotSeqNrFilenameFilter(persistenceId: String,
sequenceNr: Long, timestamp: Long) extends FilenameFilter {
+ private final def matches(pid: String, snr: String, tms: String): Boolean
= {
+ pid.equals(URLEncoder.encode(persistenceId)) && Try(snr.toLong ==
sequenceNr).getOrElse(false) && Try(tms.toLong <= timestamp).getOrElse(false)
+ }
def accept(dir: File, name: String): Boolean =
name match {
- case FilenamePattern(pid, snr, tms) ⇒ matches(pid, snr)
+ case FilenamePattern(pid, snr, tms) ⇒ matches(pid, snr, tms)
case _ ⇒ false
}