dotnet / orleans

Cloud Native application framework for .NET
https://docs.microsoft.com/dotnet/orleans
MIT License
10.01k stars 2.02k forks source link

Can I use generated codes by protobuf for state of the grain storage? #9072

Closed scalalang2 closed 1 week ago

scalalang2 commented 1 month ago

Motivation

Lets assume that we declare a protobuf message as blow.

message ChatRoom {
    bool isActive = 1;
    repeated string members = 2;
}

Then, we need to store the ChatRoom as state in the ChatGrain.

class ChatGrain : IChatGrain {
    private IPersistentState<ChatRoom> state; // or use TransactionalState depends on your needs.
}

Even though the ChatRoom class is generated by protobuf. I've not been met any issue.

Can I keep using this pattern without any issues?

jsteinich commented 1 month ago

Yes. We do something very similar.

By default the profobuf serialization won't actually be used for storage, but you can configure it a couple different ways.

In Orleans 8.x (possible, but different in earlier versions) you can create a custom IGrainStorageSerializer implementation that delegates to protobuf serialization or you can use OrleansGrainStorageSerializer and set the Orleans serializer to use protobuf serialization. The latter approach will also add a small header to the stored state and will also impact cross-silo communication.

scalalang2 commented 1 month ago

@jsteinich thanks for a reply 👍

We've used Orleans 7.x and without configuring Serializer, and the class generated by protobuf works fine. I'm not sure whether it is intended or not.

Can I assume that we can use this without any concerns?

jsteinich commented 1 month ago

I recommend double checking the format that your data is actually saved in. Without additional configuration, I believe it defaults to JSON (possibly binary JSON depending on the storage provider used). As long as it matches what you want it to be, then there shouldn't be any issue.