David
David

Reputation: 11

How should I use a storage provider with Orleans

I'm a newbie in Orleans. I'd like to know how I can use the grain storage feature in Orleans. Should I use it like a message queue? Does it store my state temporary and keep the data available even it throw exceptions or the server crashed.

Thanks!

Upvotes: 1

Views: 1902

Answers (2)

Gigi
Gigi

Reputation: 29501

I had written a couple of articles to guide you step-by-step into getting used to the Storage Provider API and setting up your persistence store:

Basically, Orleans gives you a very simple API (image taken from the first article above):

enter image description here

Your grain will inherit from Grain<T>, where T is your own class containing the state that you want to persist. The State property from Grain<T> lets you access it and read/modify state. The remaining async methods let you save changes to the persistence store, read them back, or clear the state. You typically don't need to read the state; it is done automatically when the grain gets activated.

There are no message queues involved. When you call one of these three methods, they will use the underlying storage provider to talk to whatever database you are using. This may fail due to store-specific errors (e.g. deadlocks), or due to an InconsistentStateException that is the result of a failed optimistic concurrency control check.

Whatever storage provided you decide to use (e.g. SQL Server, Azure Table Storage, in-memory, etc) must be configured via either XML config or code, and given a name. This name is then used in a [StorageProvider] attribute that goes over the grain class; in this way, the grain knows what storage provider to use when doing its persistence work (you could have various in your system).

The details of how all this is done are a bit lengthy to include here (which is why I wrote articles on the subject). You can find more information about this either in my articles linked above, or the Grain Persistence documentation.

Upvotes: 1

Cronan
Cronan

Reputation: 193

Grains that extend the Grain<T> class and are annotated with a [StorageProvider] attribute, will write their current state to the specified provider when you call base.WriteStateAsync().

If the grain is deactivated for any reason (including server crashed) then upon reactivation the grain will be initialized with the state that was last saved down.

I like to think of it as a cache, rather than a queue. Hope that helps, and, like the previous poster said, read the documentation, it's useful.

Upvotes: 1

Related Questions