Search code examples
scalaakkaakka-persistence

How to trigger a PersistentView update externally


According to the docs, triggering a view update externally is supposed to work like this:

view ! Update(await = true|false)

sending and waiting seems not to work though:

view ! Update(await = true) // repeat x times does not help either
Thread.sleep(2000)
val getState = view ? GetState

the view state is not being updated.

asking does not work either - there is no answer to Update and the Await times out:

val getUpdate = v ? Update(await = true)
val updated = Await.result(getUpdate, 10 seconds)
val getState = view ? GetState

IIUC, processing of Update messages is performed by PersistentView#State#stateReceive. Its reception is not logged and I dont know how I can verify the reception and processing. Update msgs dont reach the receive overridden in my PersistentView.

what does work is setting a very short refresh interval in a .conf:

persistence.view.auto-update-interval = 100ms

The debug log shows that the Journal is updating the view:

... a.p.i.e.InMemoryJournalStorage akka://entityViewSpec/user/JournalStorage - received handled message GetJournalEntriesExceptDeleted(ea-ZleUNl1a3N,1,1,9223372036854775807) from Actor[akka://entityViewSpec/temp/$o]

I dont see this log entry when sending Update and waiting

after a short sleep, the view is updated, as expected.

Thread.sleep(200)
val getState = view ? GetState

So how do I trigger a view update externally without running autoupdates all the time?

Akka 2.4.8

Thank you

EDIT

My mistake was quite simple - not giving the initial msg enough time to be persisted - the Update call has been received at a time when the relevant events were not available from the store yet. The autoupdate setting waited for 100ms before updating. On my local machine with in-mem persistence I need to allow about 50ms before dispatching the Update. Then give the system some time to fetch and apply the events:

not working:

actor ! MyCommand
view ! Update(await = true)
Thread.sleep(500)
val getState = view ? GetState

working:

actor ! MyCommand
Thread.sleep(50)
view ! Update(await = true)
Thread.sleep(500)
val getState = view ? GetState

Solution

  • I have tried to recreate your issue but mine view is refreshed :/ Check my example: https://github.com/kpbochenek/akka-playground

    Run MyPersistentView.scala

    auto-update is off

    I can see in logs(View actor receives messages persisted by persistent actor):

    00:11:35.325 [pw-akka.actor.default-dispatcher-5] INFO  com.kpbochenek.MyActor - persisted! AAAAAAAA
    00:11:35.326 [pw-akka.actor.default-dispatcher-9] INFO  com.kpbochenek.MyActor - persisted! 11111111
    00:11:37.254 [pw-akka.actor.default-dispatcher-4] INFO  com.kpbochenek.MyView - VIEW READ AAAAAAAA
    00:11:37.255 [pw-akka.actor.default-dispatcher-4] INFO  com.kpbochenek.MyView - VIEW READ 11111111