From de1630b45da18bcf100fff50980ca74b13be2040 Mon Sep 17 00:00:00 2001 From: David Pine Date: Thu, 10 Nov 2022 11:47:35 -0600 Subject: [PATCH 1/4] Upgrade 'Bank Account' sample --- .../AccountTransfer.Grains/AccountGrain.cs | 25 ++++---- .../AccountTransfer.Grains.csproj | 10 +--- .../AccountTransfer.Grains/AtmGrain.cs | 3 +- .../AccountTransfer.Interfaces.csproj | 9 +-- .../IAccountGrain.cs | 8 +-- .../AccountTransfer.Interfaces/IAtmGrain.cs | 4 +- .../BankAccount/BankClient/BankClient.csproj | 9 +-- samples/BankAccount/BankClient/Program.cs | 60 +++++++++++-------- .../BankAccount/BankServer/BankServer.csproj | 11 ++-- samples/BankAccount/BankServer/Program.cs | 2 - 10 files changed, 71 insertions(+), 70 deletions(-) diff --git a/samples/BankAccount/AccountTransfer.Grains/AccountGrain.cs b/samples/BankAccount/AccountTransfer.Grains/AccountGrain.cs index 97472d172b..1ef6f10266 100644 --- a/samples/BankAccount/AccountTransfer.Grains/AccountGrain.cs +++ b/samples/BankAccount/AccountTransfer.Grains/AccountGrain.cs @@ -1,16 +1,18 @@ using AccountTransfer.Interfaces; -using Orleans; +using Orleans.Concurrency; using Orleans.Transactions.Abstractions; namespace AccountTransfer.Grains; -[Serializable] +[GenerateSerializer] public record class Balance { - public uint Value { get; set; } = 1_000; + [Id(0)] + public int Value { get; set; } = 1_000; } -public class AccountGrain : Grain, IAccountGrain +[Reentrant] +public sealed class AccountGrain : Grain, IAccountGrain { private readonly ITransactionalState _balance; @@ -18,24 +20,25 @@ public AccountGrain( [TransactionalState("balance")] ITransactionalState balance) => _balance = balance ?? throw new ArgumentNullException(nameof(balance)); - public Task Deposit(uint amount) => + public Task Deposit(int amount) => _balance.PerformUpdate( balance => balance.Value += amount); - public Task Withdraw(uint amount) => + public Task Withdraw(int amount) => _balance.PerformUpdate(balance => { if (balance.Value < amount) { - throw new InvalidOperationException( - $"Withdrawing {amount} credits from account " + - $"\"{this.GetPrimaryKeyString()}\" would overdraw it." + - $" This account has {balance.Value} credits."); + throw new InvalidOperationException($""" + Withdrawing {amount} credits from account + "{this.GetPrimaryKeyString()}" would overdraw it. + This account has {balance.Value} credits. + """); } balance.Value -= amount; }); - public Task GetBalance() => + public Task GetBalance() => _balance.PerformRead(balance => balance.Value); } diff --git a/samples/BankAccount/AccountTransfer.Grains/AccountTransfer.Grains.csproj b/samples/BankAccount/AccountTransfer.Grains/AccountTransfer.Grains.csproj index ff21b092d4..c03bf517eb 100644 --- a/samples/BankAccount/AccountTransfer.Grains/AccountTransfer.Grains.csproj +++ b/samples/BankAccount/AccountTransfer.Grains/AccountTransfer.Grains.csproj @@ -1,16 +1,12 @@ - net6.0 + net7.0 enable enable - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/samples/BankAccount/AccountTransfer.Grains/AtmGrain.cs b/samples/BankAccount/AccountTransfer.Grains/AtmGrain.cs index 832f77a62f..a65ea3dd02 100644 --- a/samples/BankAccount/AccountTransfer.Grains/AtmGrain.cs +++ b/samples/BankAccount/AccountTransfer.Grains/AtmGrain.cs @@ -1,5 +1,4 @@ using AccountTransfer.Interfaces; -using Orleans; using Orleans.Concurrency; namespace AccountTransfer.Grains; @@ -10,7 +9,7 @@ public class AtmGrain : Grain, IAtmGrain public Task Transfer( IAccountGrain fromAccount, IAccountGrain toAccount, - uint amountToTransfer) => + int amountToTransfer) => Task.WhenAll( fromAccount.Withdraw(amountToTransfer), toAccount.Deposit(amountToTransfer)); diff --git a/samples/BankAccount/AccountTransfer.Interfaces/AccountTransfer.Interfaces.csproj b/samples/BankAccount/AccountTransfer.Interfaces/AccountTransfer.Interfaces.csproj index 98074e538d..8f3145b3d4 100644 --- a/samples/BankAccount/AccountTransfer.Interfaces/AccountTransfer.Interfaces.csproj +++ b/samples/BankAccount/AccountTransfer.Interfaces/AccountTransfer.Interfaces.csproj @@ -1,14 +1,11 @@ - net6.0 + net7.0 enable enable - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - + + \ No newline at end of file diff --git a/samples/BankAccount/AccountTransfer.Interfaces/IAccountGrain.cs b/samples/BankAccount/AccountTransfer.Interfaces/IAccountGrain.cs index b8fd2f675c..fc695f0df7 100644 --- a/samples/BankAccount/AccountTransfer.Interfaces/IAccountGrain.cs +++ b/samples/BankAccount/AccountTransfer.Interfaces/IAccountGrain.cs @@ -1,15 +1,13 @@ -using Orleans; - namespace AccountTransfer.Interfaces; public interface IAccountGrain : IGrainWithStringKey { [Transaction(TransactionOption.Join)] - Task Withdraw(uint amount); + Task Withdraw(int amount); [Transaction(TransactionOption.Join)] - Task Deposit(uint amount); + Task Deposit(int amount); [Transaction(TransactionOption.CreateOrJoin)] - Task GetBalance(); + Task GetBalance(); } diff --git a/samples/BankAccount/AccountTransfer.Interfaces/IAtmGrain.cs b/samples/BankAccount/AccountTransfer.Interfaces/IAtmGrain.cs index 3aa73c9b24..116d98a96d 100644 --- a/samples/BankAccount/AccountTransfer.Interfaces/IAtmGrain.cs +++ b/samples/BankAccount/AccountTransfer.Interfaces/IAtmGrain.cs @@ -1,5 +1,3 @@ -using Orleans; - namespace AccountTransfer.Interfaces; public interface IAtmGrain : IGrainWithIntegerKey @@ -8,5 +6,5 @@ public interface IAtmGrain : IGrainWithIntegerKey Task Transfer( IAccountGrain fromAccount, IAccountGrain toAccount, - uint amountToTransfer); + int amountToTransfer); } diff --git a/samples/BankAccount/BankClient/BankClient.csproj b/samples/BankAccount/BankClient/BankClient.csproj index 9f4f0842c7..1ea755f537 100644 --- a/samples/BankAccount/BankClient/BankClient.csproj +++ b/samples/BankAccount/BankClient/BankClient.csproj @@ -1,15 +1,16 @@ Exe - net6.0 + net7.0 enable enable - - - + + + + diff --git a/samples/BankAccount/BankClient/Program.cs b/samples/BankAccount/BankClient/Program.cs index 528f4fc1d9..e1eaba225b 100644 --- a/samples/BankAccount/BankClient/Program.cs +++ b/samples/BankAccount/BankClient/Program.cs @@ -1,52 +1,64 @@ using AccountTransfer.Interfaces; -using Microsoft.Extensions.Logging; -using Orleans; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; -using var client = new ClientBuilder() - .UseLocalhostClustering() - .ConfigureLogging(logging => logging.AddConsole()) +using IHost host = Host.CreateDefaultBuilder(args) + .UseOrleansClient((_, client) => + { + client.UseLocalhostClustering() + .UseTransactions(); + }) .Build(); -await client.Connect(); +await host.StartAsync(); + +var client = host.Services.GetRequiredService(); +var transactionClient = host.Services.GetRequiredService(); var accountNames = new[] { "Xaawo", "Pasqualino", "Derick", "Ida", "Stacy", "Xiao" }; var random = Random.Shared; while (!Console.KeyAvailable) { - var atm = client.GetGrain(0); - // Choose some random accounts to exchange money - var fromId = random.Next(accountNames.Length); - var toId = random.Next(accountNames.Length); - while (toId == fromId) + var fromIndex = random.Next(accountNames.Length); + var toIndex = random.Next(accountNames.Length); + while (toIndex == fromIndex) { - // Avoid transfering to/from the same account, since it would be meaningless - toId = (toId + 1) % accountNames.Length; + // Avoid transferring to/from the same account, since it would be meaningless + toIndex = (toIndex + 1) % accountNames.Length; } - var fromName = accountNames[fromId]; - var toName = accountNames[toId]; - var from = client.GetGrain(fromName); - var to = client.GetGrain(toName); + var fromKey = accountNames[fromIndex]; + var toKey = accountNames[toIndex]; + var fromAccount = client.GetGrain(fromKey); + var toAccount = client.GetGrain(toKey); // Perform the transfer and query the results try { - await atm.Transfer(from, to, 100); + var transferAmount = random.Next(200); + + await transactionClient.RunTransaction( + TransactionOption.Create, + async () => + { + await fromAccount.Withdraw(transferAmount); + await toAccount.Deposit(transferAmount); + }); - var fromBalance = await from.GetBalance(); - var toBalance = await to.GetBalance(); + var fromBalance = await fromAccount.GetBalance(); + var toBalance = await toAccount.GetBalance(); Console.WriteLine( - $"We transfered 100 credits from {fromName} to " + - $"{toName}.\n{fromName} balance: {fromBalance}\n{toName} balance: {toBalance}\n"); + $"We transferred {transferAmount} credits from {fromKey} to " + + $"{toKey}.\n{fromKey} balance: {fromBalance}\n{toKey} balance: {toBalance}\n"); } catch (Exception exception) { Console.WriteLine( - $"Error transfering 100 credits from "+ - $"{fromName} to {toName}: {exception.Message}"); + $"Error transferring credits from " + + $"{fromKey} to {toKey}: {exception.Message}"); if (exception.InnerException is { } inner) { diff --git a/samples/BankAccount/BankServer/BankServer.csproj b/samples/BankAccount/BankServer/BankServer.csproj index 06df95e241..a27f96ab17 100644 --- a/samples/BankAccount/BankServer/BankServer.csproj +++ b/samples/BankAccount/BankServer/BankServer.csproj @@ -1,17 +1,16 @@ Exe - net6.0 + net7.0 enable enable - - - - - + + + + diff --git a/samples/BankAccount/BankServer/Program.cs b/samples/BankAccount/BankServer/Program.cs index b351108bed..c65fd8af31 100644 --- a/samples/BankAccount/BankServer/Program.cs +++ b/samples/BankAccount/BankServer/Program.cs @@ -1,6 +1,4 @@ using Microsoft.Extensions.Hosting; -using Orleans; -using Orleans.Hosting; await Host.CreateDefaultBuilder() .UseOrleans(siloBuilder => From b0f0b30fa9feff2c24a152dff2be174f0968954a Mon Sep 17 00:00:00 2001 From: David Pine Date: Thu, 10 Nov 2022 11:55:43 -0600 Subject: [PATCH 2/4] Correct error message --- .../BankAccount/AccountTransfer.Grains/AccountGrain.cs | 9 ++++----- samples/BankAccount/BankClient/Program.cs | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/samples/BankAccount/AccountTransfer.Grains/AccountGrain.cs b/samples/BankAccount/AccountTransfer.Grains/AccountGrain.cs index 1ef6f10266..93ba3c663c 100644 --- a/samples/BankAccount/AccountTransfer.Grains/AccountGrain.cs +++ b/samples/BankAccount/AccountTransfer.Grains/AccountGrain.cs @@ -29,11 +29,10 @@ public Task Withdraw(int amount) => { if (balance.Value < amount) { - throw new InvalidOperationException($""" - Withdrawing {amount} credits from account - "{this.GetPrimaryKeyString()}" would overdraw it. - This account has {balance.Value} credits. - """); + throw new InvalidOperationException( + $"Withdrawing {amount} credits from account " + + $"\"{this.GetPrimaryKeyString()}\" would overdraw it." + + $" This account has {balance.Value} credits."); } balance.Value -= amount; diff --git a/samples/BankAccount/BankClient/Program.cs b/samples/BankAccount/BankClient/Program.cs index e1eaba225b..46e639c853 100644 --- a/samples/BankAccount/BankClient/Program.cs +++ b/samples/BankAccount/BankClient/Program.cs @@ -3,7 +3,7 @@ using Microsoft.Extensions.Hosting; using IHost host = Host.CreateDefaultBuilder(args) - .UseOrleansClient((_, client) => + .UseOrleansClient(client => { client.UseLocalhostClustering() .UseTransactions(); From acc49cab82b2fd5729b2773d7cf77872500ee431 Mon Sep 17 00:00:00 2001 From: ReubenBond Date: Thu, 10 Nov 2022 10:22:22 -0800 Subject: [PATCH 3/4] Use console lifetime --- samples/BankAccount/BankClient/Program.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/samples/BankAccount/BankClient/Program.cs b/samples/BankAccount/BankClient/Program.cs index 46e639c853..db9e8ccc25 100644 --- a/samples/BankAccount/BankClient/Program.cs +++ b/samples/BankAccount/BankClient/Program.cs @@ -8,17 +8,19 @@ client.UseLocalhostClustering() .UseTransactions(); }) + .UseConsoleLifetime() .Build(); await host.StartAsync(); var client = host.Services.GetRequiredService(); var transactionClient = host.Services.GetRequiredService(); +var lifetime = host.Services.GetRequiredService(); var accountNames = new[] { "Xaawo", "Pasqualino", "Derick", "Ida", "Stacy", "Xiao" }; var random = Random.Shared; -while (!Console.KeyAvailable) +while (!lifetime.ApplicationStopping.IsCancellationRequested) { // Choose some random accounts to exchange money var fromIndex = random.Next(accountNames.Length); From e2a93fa977cdfb59e8d9a3a25be1d7b1b2d24554 Mon Sep 17 00:00:00 2001 From: ReubenBond Date: Thu, 10 Nov 2022 10:23:56 -0800 Subject: [PATCH 4/4] Args --- samples/BankAccount/BankServer/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/BankAccount/BankServer/Program.cs b/samples/BankAccount/BankServer/Program.cs index c65fd8af31..8d1b976ab3 100644 --- a/samples/BankAccount/BankServer/Program.cs +++ b/samples/BankAccount/BankServer/Program.cs @@ -1,6 +1,6 @@ using Microsoft.Extensions.Hosting; -await Host.CreateDefaultBuilder() +await Host.CreateDefaultBuilder(args) .UseOrleans(siloBuilder => { siloBuilder