Skip to content

Commit

Permalink
Merge pull request #1 from Kritner/grainReuse
Browse files Browse the repository at this point in the history
Grain reuse/Stateful grains
  • Loading branch information
Kritner authored Oct 17, 2018
2 parents 4cbd049 + e6b3705 commit b56b582
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 10 deletions.
59 changes: 50 additions & 9 deletions src/Kritner.OrleansGettingStarted.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ private static async Task<int> RunMainAsync()
using (var client = await StartClientWithRetries())
{
await DoClientWork(client);
await DoStatefulWork(client);
Console.ReadKey();
}

Expand Down Expand Up @@ -78,19 +79,59 @@ private static async Task<bool> RetryFilter(Exception exception)

private static async Task DoClientWork(IClusterClient client)
{
Console.WriteLine("Hello, what should I call you?");
var name = Console.ReadLine();
// example of calling grains from the initialized client
var grain = client.GetGrain<IHelloWorld>(Guid.NewGuid());
var grain2 = client.GetGrain<IHelloWorld>(Guid.NewGuid());

Console.WriteLine($"{await grain.SayHello("1")}");
Console.WriteLine($"{await grain2.SayHello("2")}");
Console.WriteLine($"{await grain.SayHello("3")}");

PrintSeparatorThing();
}

private static async Task DoStatefulWork(IClusterClient client)
{
var kritnerGrain = client.GetGrain<IVisitTracker>("[email protected]");
var notKritnerGrain = client.GetGrain<IVisitTracker>("[email protected]");

await PrettyPrintGrainVisits(kritnerGrain);
await PrettyPrintGrainVisits(notKritnerGrain);

PrintSeparatorThing();
Console.WriteLine("Ayyy some people are visiting!");

await kritnerGrain.Visit();
await kritnerGrain.Visit();
await notKritnerGrain.Visit();

PrintSeparatorThing();

if (string.IsNullOrEmpty(name))
await PrettyPrintGrainVisits(kritnerGrain);
await PrettyPrintGrainVisits(notKritnerGrain);

PrintSeparatorThing();
Console.Write("ayyy kritner's visiting even more!");

for (int i = 0; i < 5; i++)
{
name = "anon";
await kritnerGrain.Visit();
}

// example of calling grains from the initialized client
var grain = client.GetGrain<IHelloWorld>(Guid.NewGuid());

var response = await grain.SayHello(name);
Console.WriteLine($"\n\n{response}\n\n");
PrintSeparatorThing();

await PrettyPrintGrainVisits(kritnerGrain);
await PrettyPrintGrainVisits(notKritnerGrain);
}

private static async Task PrettyPrintGrainVisits(IVisitTracker grain)
{
Console.WriteLine($"{grain.GetPrimaryKeyString()} has visited {await grain.GetNumberOfVisits()} times");
}

private static void PrintSeparatorThing()
{
Console.WriteLine($"{Environment.NewLine}-----{Environment.NewLine}");
}
}
}
11 changes: 11 additions & 0 deletions src/Kritner.OrleansGettingStarted.GrainInterfaces/IVisitTracker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Orleans;
using System.Threading.Tasks;

namespace Kritner.OrleansGettingStarted.GrainInterfaces
{
public interface IVisitTracker : IGrainWithStringKey
{
Task<int> GetNumberOfVisits();
Task Visit();
}
}
11 changes: 11 additions & 0 deletions src/Kritner.OrleansGettingStarted.Grains/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Kritner.OrleansGettingStarted.Grains
{
public class Constants
{
public const string OrleansMemoryProvider = "OrleansMemoryProvider";
}
}
2 changes: 1 addition & 1 deletion src/Kritner.OrleansGettingStarted.Grains/HelloWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class HelloWorld : Grain, IHelloWorld
{
public Task<string> SayHello(string name)
{
return Task.FromResult($"Hello World! Orleans is neato torpedo, eh {name}?");
return Task.FromResult($"Hello from grain {this.GetGrainIdentity()}, {name}!");
}
}
}
39 changes: 39 additions & 0 deletions src/Kritner.OrleansGettingStarted.Grains/VisitTracker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Kritner.OrleansGettingStarted.GrainInterfaces;
using Orleans;
using Orleans.Providers;
using System;
using System.Threading.Tasks;

namespace Kritner.OrleansGettingStarted.Grains
{
[StorageProvider(ProviderName = Constants.OrleansMemoryProvider)]
public class VisitTracker : Grain<VisitTrackerState>, IVisitTracker
{
public Task<int> GetNumberOfVisits()
{
return Task.FromResult(State.NumberOfVisits);
}

public async Task Visit()
{
var now = DateTime.Now;

if (!State.FirstVisit.HasValue)
{
State.FirstVisit = now;
}

State.NumberOfVisits++;
State.LastVisit = now;

await WriteStateAsync();
}
}

public class VisitTrackerState
{
public DateTime? FirstVisit { get; set; }
public DateTime? LastVisit { get; set; }
public int NumberOfVisits { get; set; }
}
}
1 change: 1 addition & 0 deletions src/Kritner.OrleansGettingStarted.SiloHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private static async Task<ISiloHost> StartSilo()
options.ServiceId = "HelloWorldApp";
})
.Configure<EndpointOptions>(options => options.AdvertisedIPAddress = IPAddress.Loopback)
.AddMemoryGrainStorage(Constants.OrleansMemoryProvider)
//.ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(HelloWorld).Assembly).WithReferences())
.ConfigureLogging(logging => logging.AddConsole());

Expand Down

0 comments on commit b56b582

Please sign in to comment.