orleans icon indicating copy to clipboard operation
orleans copied to clipboard

Grain services documentation

Open DixonDs opened this issue 9 years ago • 12 comments

Unless I am missing it, grain services feature is not mentioned in documentation. If that's right, it would be really great, if someone who knows it very well could add a relevant documentation page.

DixonDs avatar Jun 13 '17 07:06 DixonDs

@DixonDs You are correct - there is still no documentation for grain services still. I guess I need to write it, unless @jamescarter-le (who implemented the feature) already did that and is keeping it to himself. :-)

sergeybykov avatar Jun 13 '17 23:06 sergeybykov

Eagerly awaiting ... ))

yevhen avatar Sep 01 '17 19:09 yevhen

Any updates?

yevhen avatar Jun 14 '18 06:06 yevhen

Here is content from the GrainService document linked in issue #4675. Please let me know if this is an idea about what is needed and what needs to be changed.

Grain Services Documentation

posted by @bboyle1234

A GrainService is a special grain, one that has no identity, and runs in every single silo, from startup to shutdown. TODO 1: Add examples of what GrainServices might be used for. Here are the steps for creating a GrainService. Step 1. Create the interface The interface of a GrainService is built using exactly the same principles you would use for building the interface of any other grain.

public interface IDataService : IGrainService { Task MyMethod(); }

Step 2. Create the GataService grain itself
//TODO 2: Answer this question: should that be "DataService" and not "GataService"? Make the GrainService re-entrant if possible for better performance. Note the necessary base constructor call. It’s good to know that you can also inject an IGrainFactory so you can make grain calls from your GrainService. // TODO 3: Appears this paragraph below might be wrong A note about streams: A GrainService cannot write to Orleans streams, because it doesn’t work within a grain task scheduler. If you need the GrainService to write to streams for you, then you will have to send the object to another kind of grain for writing to the stream.

[Reentrant]
public class LightstreamerDataService : GrainService, IDataService {

    readonly IGrainFactory GrainFactory;

    public LightstreamerDataService(IServiceProvider services, IGrainIdentity id, Silo silo, ILoggerFactory loggerFactory, IGrainFactory grainFactory) : base(id, silo, loggerFactory) {
        GrainFactory = grainFactory;
    }

    public override Task Init(IServiceProvider serviceProvider) {
        return base.Init(serviceProvider);
    }

    public override async Task Start() {
        await base.Start();
    }

    public override Task Stop() {
        return base.Stop();
    }

  public Task MyMethod() { 
 }
}

Step 3. Create an interface for the GrainServiceClient, which used by other grains to connect to the GrainService.

public interface IDataServiceClient : IGrainServiceClient<IDataService>, IDataService {
}

Step 4. Create the actual grain service client It pretty much just acts as a proxy for the data service. Unfortunately, you have to manually type in all the method mappings, which are just simple one-liners.

public class DataServiceClient : GrainServiceClient<IDataService>, IDataServiceClient {

    public DataServiceClient(IServiceProvider serviceProvider) : base(serviceProvider) {
    }

    public Task MyMethod()  => GrainService.MyMethod();
}

Step 5. Inject the grain service client into the other grains that need it Note that the GrainServiceClient does not guarantee accessing the GrainService on the local silo. Your command could potentially be sent to the GrainService on any silo in the cluster.

public class MyNormalGrain: Grain<NormalGrainState>, INormalGrain {

    readonly IDataServiceClient DataServiceClient;

    public MyNormalGrain(IGrainActivationContext grainActivationContext, IDataServiceClient dataServiceClient) {
                    DataServiceClient = dataServiceClient;
    }
}

Step 6. Inject the grain service into the silo itself You need to do this so that the silo will start the GrainService. (ISiloHostBuilder builder) => builder .ConfigureServices(services => { services.AddSingleton<IDataService, DataService>(); });

JillHeaden avatar Jun 15 '18 21:06 JillHeaden

There's an extension method on ISiloHostBuilder: AddGrainService<SomeGrainService>(). Type constraint is: where T : GrainService. It ends up calling this bit: https://github.com/dotnet/orleans/blob/2f3a5863677963f05ad301659509baefa24bb5d7/src/Orleans.Runtime/Services/GrainServicesSiloBuilderExtensions.cs#L48

Basically the silo fetches IGrainService types from the service provider when starting: https://github.com/dotnet/orleans/blob/2f3a5863677963f05ad301659509baefa24bb5d7/src/Orleans.Runtime/Silo/Silo.cs#L554

martinothamar avatar Jun 30 '18 12:06 martinothamar

I've tried to create a GrainService and things said here are not totally complete. In order for this to work you have to register both the Service and its Client. The code looks something like this

    var builder = new SiloHostBuilder()
        .AddGrainService<DataService>()  // Register GrainService
        .ConfigureServices(s =>
         {
            // Register Client of GrainService
            s.AddSingleton<IDataServiceClient, DataServiceClient>(); 
        })

ghost avatar Jul 17 '18 11:07 ghost

Hi, guys.

Thanks for such a great feature. Just small question to You - can GrainService read from streams?

Thanks.

AlexanderButs avatar Aug 10 '18 22:08 AlexanderButs

Thanks for such a great feature. Just small question to You - can GrainService read from streams?

No, they cannot subscribe to streams because grain services aren't virtual - their addresses are tied to physical silos underneath.

sergeybykov avatar Aug 11 '18 00:08 sergeybykov

Thanks for so fast response.

Maybe it's incorrect place to ask but anyway - is there a way to have a grain per silo and call all those grains as a broadcast? I need to update inmemory state on each silo inside whole cluster.

Thanks once again.

AlexanderButs avatar Aug 11 '18 00:08 AlexanderButs

Is that GrainService similar with [StatelessWorker] Grain which has constant identity(like 0)? I'm using [StatelessWorker] QueryGrain with id 0 for client to query data, can GrainService do this?

Zonciu avatar Aug 16 '18 05:08 Zonciu

In the narrow sense of having one instance per silo, they are similar. But they are different in several other aspects - invocation API, auto-activation, repartitioning, etc.

sergeybykov avatar Aug 28 '18 21:08 sergeybykov

Resolved via #4946.

sergeybykov avatar Oct 08 '18 21:10 sergeybykov