Skip to content

Commit c8d86e8

Browse files
committed
Validate GrainId Type and Key on reminder creation
1 parent 3414f3d commit c8d86e8

File tree

10 files changed

+53
-49
lines changed

10 files changed

+53
-49
lines changed

Orleans.Providers.MongoDB/Membership/Store/Multiple/MultipleMembershipCollection.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public async Task<bool> UpsertRow(string deploymentId, MembershipEntry entry, st
4949
using var session = await Client.StartSessionAsync();
5050
return await session.WithTransactionAsync(async (sessionHandle, ct) =>
5151
{
52-
var hasUpsertedTable = await tableVersionCollection.UpsertAsync(session, tableVersion, deploymentId);
52+
var hasUpsertedTable = await tableVersionCollection.UpsertAsync(sessionHandle, tableVersion, deploymentId);
5353

5454
if (!hasUpsertedTable)
5555
{
@@ -106,10 +106,10 @@ public async Task<MembershipTableData> ReadAll(string deploymentId)
106106
using var session = await Client.StartSessionAsync();
107107
return await session.WithTransactionAsync(async (sessionHandle, ct) =>
108108
{
109-
var tableVersion = tableVersionCollection.GetTableVersionAsync(deploymentId);
109+
var tableVersion = tableVersionCollection.GetTableVersionAsync(sessionHandle, deploymentId);
110110

111111
var entries =
112-
Collection.Find(x => x.DeploymentId == deploymentId)
112+
Collection.Find(sessionHandle, x => x.DeploymentId == deploymentId)
113113
.ToListAsync(cancellationToken: ct);
114114

115115
await Task.WhenAll(tableVersion, entries);
@@ -123,12 +123,12 @@ public async Task<MembershipTableData> ReadRow(string deploymentId, SiloAddress
123123
using var session = await Client.StartSessionAsync();
124124
return await session.WithTransactionAsync(async (sessionHandle, ct) =>
125125
{
126-
var tableVersion = tableVersionCollection.GetTableVersionAsync(deploymentId);
126+
var tableVersion = tableVersionCollection.GetTableVersionAsync(sessionHandle, deploymentId);
127127

128128
var id = ReturnId(deploymentId, address);
129129

130130
var entries =
131-
Collection.Find(x => x.Id == id)
131+
Collection.Find(sessionHandle, x => x.Id == id)
132132
.ToListAsync(cancellationToken: ct);
133133

134134
await Task.WhenAll(tableVersion, entries);
@@ -151,12 +151,19 @@ public Task CleanupDefunctSiloEntries(string deploymentId, DateTimeOffset before
151151
return Collection.DeleteManyAsync(x => x.DeploymentId == deploymentId && x.Status != (int)SiloStatus.Active && x.Timestamp < beforeUtc);
152152
}
153153

154-
public Task DeleteMembershipTableEntries(string deploymentId)
154+
public async Task DeleteMembershipTableEntries(string deploymentId)
155155
{
156-
return Task.WhenAll(
157-
Collection.DeleteManyAsync(x => x.DeploymentId == deploymentId),
158-
tableVersionCollection.DeleteAsync(deploymentId)
159-
);
156+
using var session = await Client.StartSessionAsync();
157+
158+
await session.WithTransactionAsync(async (sessionHandle, ct) =>
159+
{
160+
await Task.WhenAll(
161+
Collection.DeleteManyAsync(sessionHandle, x => x.DeploymentId == deploymentId),
162+
tableVersionCollection.DeleteAsync(sessionHandle, deploymentId)
163+
);
164+
165+
return true;
166+
});
160167
}
161168

162169
private static MembershipTableData ReturnMembershipTableData(IEnumerable<MongoMembershipDocument> membershipList, TableVersion tableVersion)

Orleans.Providers.MongoDB/Membership/Store/Multiple/TableVersionCollection.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ protected override string CollectionName()
2626
return $"{collectionPrefix}OrleansMembershipV3_TableVersion";
2727
}
2828

29-
public Task DeleteAsync(string deploymentId)
29+
public Task DeleteAsync(IClientSessionHandle session, string deploymentId)
3030
{
31-
return Collection.DeleteOneAsync(x => x.DeploymentId == deploymentId);
31+
return Collection.DeleteOneAsync(session, x => x.DeploymentId == deploymentId);
3232
}
3333

34-
public async Task<TableVersion> GetTableVersionAsync(string deploymentId)
34+
public async Task<TableVersion> GetTableVersionAsync(IClientSessionHandle session, string deploymentId)
3535
{
36-
var deployment = await Collection.Find(x => x.DeploymentId == deploymentId).FirstOrDefaultAsync();
36+
var deployment = await Collection.Find(session, x => x.DeploymentId == deploymentId).FirstOrDefaultAsync();
3737

3838
if (deployment == null)
3939
{
@@ -50,7 +50,7 @@ public async Task<bool> UpsertAsync(IClientSessionHandle session, TableVersion t
5050
try
5151
{
5252
await Collection.ReplaceOneAsync(session,
53-
x => x.DeploymentId == deploymentId && x.VersionEtag == tableVersion.VersionEtag,
53+
x => x.DeploymentId == deploymentId && x.VersionEtag == tableVersion.VersionEtag,
5454
update,
5555
UpsertReplace);
5656

Orleans.Providers.MongoDB/Orleans.Providers.MongoDB.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>net8.0</TargetFrameworks>
55
<Authors>laredoza,sebastianstehle,wassim-k</Authors>
66
<Company>Orleans.Providers.MongoDB</Company>
77
<Copyright>MIT</Copyright>
8-
<Description>A MongoDb implementation of the Orleans Providers. This includes custering (IMembershipTable and IGatewayListProvider), reminders (IReminderTable) and storage providers (IGrainStorage).</Description>
8+
<Description>A MongoDb implementation of the Orleans Providers. This includes clustering (IMembershipTable and IGatewayListProvider), reminders (IReminderTable) and storage providers (IGrainStorage).</Description>
99
<EmbedUntrackedSources>true</EmbedUntrackedSources>
1010
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
1111
<IncludeSymbols>true</IncludeSymbols>
@@ -18,12 +18,12 @@
1818
</PropertyGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="Microsoft.Orleans.Reminders" Version="9.1.2" />
21+
<PackageReference Include="Microsoft.Orleans.Reminders" Version="9.0.1" />
2222
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
2323
<PrivateAssets>all</PrivateAssets>
2424
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2525
</PackageReference>
26-
<PackageReference Include="MongoDB.Driver" Version="3.2.1" />
26+
<PackageReference Include="MongoDB.Driver" Version="3.0.0" />
2727
</ItemGroup>
2828

2929
<ItemGroup>

Orleans.Providers.MongoDB/Reminders/Store/MongoReminderCollection.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ await Collection.ReplaceOneAsync(x => x.Id == id,
200200

201201
private static string ReturnId(string serviceId, GrainId grainId, string reminderName)
202202
{
203+
var grainType = grainId.Type.ToString();
204+
var grainKey = grainId.Key.ToString();
205+
206+
if (grainType is null || grainKey is null)
207+
{
208+
throw new ArgumentNullException(nameof(grainId));
209+
}
210+
203211
return $"{serviceId}_{grainId}_{reminderName}";
204212
}
205213
}

Orleans.Providers.MongoDB/StorageProviders/MongoGrainStorage.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Concurrent;
3-
using System.Threading;
43
using System.Threading.Tasks;
54
using Microsoft.Extensions.Logging;
65
using MongoDB.Driver;
@@ -12,14 +11,13 @@
1211

1312
namespace Orleans.Providers.MongoDB.StorageProviders
1413
{
15-
public class MongoGrainStorage : IGrainStorage, ILifecycleParticipant<ISiloLifecycle>
14+
public class MongoGrainStorage : IGrainStorage
1615
{
1716
private readonly ConcurrentDictionary<string, MongoGrainStorageCollection> collections = new ConcurrentDictionary<string, MongoGrainStorageCollection>();
1817
private readonly MongoDBGrainStorageOptions options;
1918
private readonly IMongoClient mongoClient;
2019
private readonly ILogger<MongoGrainStorage> logger;
2120
private readonly IGrainStateSerializer serializer;
22-
private IMongoDatabase database;
2321

2422
public MongoGrainStorage(
2523
IMongoClientFactory mongoClientFactory,
@@ -32,21 +30,6 @@ public MongoGrainStorage(
3230
this.serializer = options.GrainStateSerializer;
3331
}
3432

35-
public void Participate(ISiloLifecycle lifecycle)
36-
{
37-
lifecycle.Subscribe<MongoGrainStorage>(ServiceLifecycleStage.ApplicationServices, Init);
38-
}
39-
40-
private Task Init(CancellationToken ct)
41-
{
42-
return DoAndLog(nameof(Init), () =>
43-
{
44-
database = mongoClient.GetDatabase(options.DatabaseName);
45-
46-
return Task.CompletedTask;
47-
});
48-
}
49-
5033
public Task ReadStateAsync<T>(string stateName, GrainId grainId, IGrainState<T> grainState)
5134
{
5235
return DoAndLog(nameof(ReadStateAsync), () =>

Test/GrainInterfaces/Orleans.Providers.MongoDB.Test.GrainInterfaces.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.Orleans.Sdk" Version="9.1.2" />
10-
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="9.1.2" />
11-
<PackageReference Include="Microsoft.Orleans.Reminders" Version="9.1.2" />
9+
<PackageReference Include="Microsoft.Orleans.Sdk" Version="9.0.1" />
10+
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="9.0.1" />
11+
<PackageReference Include="Microsoft.Orleans.Reminders" Version="9.0.1" />
1212
</ItemGroup>
1313

1414
</Project>

Test/Grains/Orleans.Providers.MongoDB.Test.Grains.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.Orleans.Sdk" Version="9.1.2" />
10-
<PackageReference Include="Microsoft.Orleans.Core" Version="9.1.2" />
11-
<PackageReference Include="Microsoft.Orleans.Streaming" Version="9.1.2" />
9+
<PackageReference Include="Microsoft.Orleans.Sdk" Version="9.0.1" />
10+
<PackageReference Include="Microsoft.Orleans.Core" Version="9.0.1" />
11+
<PackageReference Include="Microsoft.Orleans.Streaming" Version="9.0.1" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

Test/Host/Orleans.Providers.MongoDB.Test.Host.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
@@ -8,7 +8,10 @@
88
<ItemGroup>
99
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.2" />
1010
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.2" />
11-
<PackageReference Include="MongoSandbox8" Version="1.0.1" />
11+
<PackageReference Include="MongoSandbox.Core" Version="2.0.0" />
12+
<PackageReference Include="MongoSandbox8.runtime.linux-x64" Version="2.0.0" Condition="$([MSBuild]::IsOSPlatform('Linux'))" />
13+
<PackageReference Include="MongoSandbox8.runtime.osx-arm64" Version="2.0.0" Condition="$([MSBuild]::IsOSPlatform('OSX'))" />
14+
<PackageReference Include="MongoSandbox8.runtime.win-x64" Version="2.0.0" Condition="$([MSBuild]::IsOSPlatform('Windows'))" />
1215
</ItemGroup>
1316

1417
<ItemGroup>

Test/Host/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static async Task Main(string[] args)
2525
{
2626
var createShardKey = false;
2727

28-
using var mongoRunner = MongoRunner.Run(new MongoRunnerOptions { KillMongoProcessesWhenCurrentProcessExits = true });
28+
using var mongoRunner = MongoRunner.Run();
2929

3030
Console.WriteLine("MongoDB ConnectionString: {0}", mongoRunner.ConnectionString);
3131

UnitTest/Orleans.Providers.MongoDB.UnitTest.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net8.0</TargetFramework>
44
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
@@ -9,8 +9,11 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
13-
<PackageReference Include="MongoSandbox8" Version="1.0.1" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
13+
<PackageReference Include="MongoSandbox.Core" Version="2.0.0" />
14+
<PackageReference Include="MongoSandbox8.runtime.linux-x64" Version="2.0.0" Condition="$([MSBuild]::IsOSPlatform('Linux'))" />
15+
<PackageReference Include="MongoSandbox8.runtime.osx-arm64" Version="2.0.0" Condition="$([MSBuild]::IsOSPlatform('OSX'))" />
16+
<PackageReference Include="MongoSandbox8.runtime.win-x64" Version="2.0.0" Condition="$([MSBuild]::IsOSPlatform('Windows'))" />
1417
<PackageReference Include="xunit" Version="2.9.3" />
1518
</ItemGroup>
1619

0 commit comments

Comments
 (0)