Hello,

following the improvement suggestions in the latest cluster sharding 
activator example I have added a persistent view and a replicated journal 
using
https://github.com/ddevore/akka-persistence-mongo/.
As usual everything works as announced and I think persistence/cluster 
sharding is really great and we'll be using it a lot in the future.
Now, I am not too sure how to best use persistent views:
I have added an AuthorListingView (pls. see code at the end of this post). 
The view is also sharded and I have deactivated auto updates as I think in 
most cases I will not find the correct update interval - either the update 
is tried too often without any changes happened  or the delay between a 
change happening and updating the view would be too long.
So, I create the AuthorListing with the view's shardRegion:

ClusterSharding(system).start(
        typeName = AuthorListing.shardName,
        entryProps = 
Some(AuthorListing.props(ClusterSharding(system).shardRegion(AuthorListingView.shardName)))

and in AuthorListing when it gets a PostSummary msg I update the 
corresponding view "manually" via the shardRegion:

def receive = {
    case Persistent(s: PostSummary, _) =>
      posts :+= s
      log.info("Post added to {}'s list: {}", s.author, s.title)
      view ! ListingChanged(s.author)

Forgetting about the duplicate sending of DoUpdate msgs when replaying ( I 
think Views will be mostly used with EventSourced processors anyway) this 
works
but it still feels not correct somehow - I just could send the view the 
persisted msg (command or event) directly without the Update, saving a 
journal query.
Also, it is probably not a good solution when the same event has to be sent 
to more than one view (using auto update would of course fix this 
problem...).
Would it be a good idea to update a view's state by just sending "normal" 
events(and never use auto-update or Update msgs), but of course use the 
replay and snapshot functionality of a view for starting/restarting?
When updating Views by accessing the journal and not using "messaging" will 
the journal not become a bottleneck at a certain point?
In the moment (we are using eventsourced) we do it the classical way - 
command side persisting to the event store and updating the view side by 
emitting events and the view persists the view model again. But I really 
like the idea to also keep the view in memory when needed, update it, shut 
it down when not used and bring it back easily by replaying the 
corresponding processor or snapshotting the view.

I think I forgot all other things I additionally wanted to ask by now...

Thanks in advance for any suggestions,

michael 



object AuthorListingView {

  case class GetPosts(author: String)
  case class Posts(list: immutable.IndexedSeq[PostSummary])
  case class ListingChanged(author: String)

  val idExtractor: ShardRegion.IdExtractor = {
    case p @ Persistent(s: PostSummary, _) => (s.author, p)
    case ListingChanged(author)                  => (author, Update(await = 
true))
    case m: GetPosts                                     => (m.author, m)
  }

  val shardResolver: ShardRegion.ShardResolver = msg => msg match {
    case Persistent(s: PostSummary, _) => (math.abs(s.author.hashCode) % 
100).toString     
    case ListingChanged(author)           => (math.abs(author.hashCode) % 
100).toString    
    case GetPosts(author)                      => (math.abs(author.hashCode) 
% 100).toString  }

  val shardName: String = "AuthorListingView"
}


class AuthorListingView extends View with ActorLogging {
  import AuthorListingView._

  log.info(s"viewId: $viewId and processorId: $processorId")

  override def processorId: String = this.viewId.replace(shardName, 
AuthorListing.shardName)

  override def autoUpdate = false

  var posts = Vector.empty[PostSummary]

  override def receive: Receive = {
    case Persistent(s: PostSummary, _) =>
      log.info(s"View for ${self.path.name} received summary: $s")
      posts :+= s
    case GetPosts(_) => sender ! Posts(posts)
  }



-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: http://akka.io/faq/
>>>>>>>>>>      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/groups/opt_out.

Reply via email to