Skip to content

Commit

Permalink
MongoDb seed node discovery (#2033)
Browse files Browse the repository at this point in the history
  • Loading branch information
dankrajka authored Aug 22, 2023
1 parent be5ce7b commit 2a0fe6f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ProtoActor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Proto.Cluster.SeedNode.Redi
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KubernetesDiagnostics", "benchmarks\KubernetesDiagnostics\KubernetesDiagnostics.csproj", "{5FECD1A8-A873-4927-81C3-E5C5A37D80C5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Proto.Cluster.SeedNode.MongoDb", "src\Proto.Cluster.SeedNode.MongoDb\Proto.Cluster.SeedNode.MongoDb.csproj", "{6611DA4A-6471-45CE-A288-45BC7BF00B52}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1388,6 +1390,18 @@ Global
{5FECD1A8-A873-4927-81C3-E5C5A37D80C5}.Release|x64.Build.0 = Release|Any CPU
{5FECD1A8-A873-4927-81C3-E5C5A37D80C5}.Release|x86.ActiveCfg = Release|Any CPU
{5FECD1A8-A873-4927-81C3-E5C5A37D80C5}.Release|x86.Build.0 = Release|Any CPU
{6611DA4A-6471-45CE-A288-45BC7BF00B52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6611DA4A-6471-45CE-A288-45BC7BF00B52}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6611DA4A-6471-45CE-A288-45BC7BF00B52}.Debug|x64.ActiveCfg = Debug|Any CPU
{6611DA4A-6471-45CE-A288-45BC7BF00B52}.Debug|x64.Build.0 = Debug|Any CPU
{6611DA4A-6471-45CE-A288-45BC7BF00B52}.Debug|x86.ActiveCfg = Debug|Any CPU
{6611DA4A-6471-45CE-A288-45BC7BF00B52}.Debug|x86.Build.0 = Debug|Any CPU
{6611DA4A-6471-45CE-A288-45BC7BF00B52}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6611DA4A-6471-45CE-A288-45BC7BF00B52}.Release|Any CPU.Build.0 = Release|Any CPU
{6611DA4A-6471-45CE-A288-45BC7BF00B52}.Release|x64.ActiveCfg = Release|Any CPU
{6611DA4A-6471-45CE-A288-45BC7BF00B52}.Release|x64.Build.0 = Release|Any CPU
{6611DA4A-6471-45CE-A288-45BC7BF00B52}.Release|x86.ActiveCfg = Release|Any CPU
{6611DA4A-6471-45CE-A288-45BC7BF00B52}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1515,6 +1529,7 @@ Global
{4DF9BBFF-C480-4550-B2BA-6603DAE6BC6F} = {3D12F5E5-9774-4D7E-8A5B-B1F64544925B}
{BDB67DAB-12F8-4D9F-BF7E-9F5D9E723816} = {3D12F5E5-9774-4D7E-8A5B-B1F64544925B}
{5FECD1A8-A873-4927-81C3-E5C5A37D80C5} = {0F3AB331-C042-4371-A2F0-0AFDFA13DC9F}
{6611DA4A-6471-45CE-A288-45BC7BF00B52} = {3D12F5E5-9774-4D7E-8A5B-B1F64544925B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CD0D1E44-8118-4682-8793-6B20ABFA824C}
Expand Down
42 changes: 42 additions & 0 deletions src/Proto.Cluster.SeedNode.MongoDb/MongoDbSeedNodeDiscovery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using MongoDB.Driver;
using Proto.Cluster.Seed;

namespace Proto.Cluster.SeedNode.MongoDb;

public class MongoDbSeedNodeDiscovery : ISeedNodeDiscovery
{
private readonly IMongoCollection<ProtoActorMember> _collection;

public MongoDbSeedNodeDiscovery(IMongoDatabase mongoDatabase, string storageKey = "ProtoActorMembers")
{
_collection = mongoDatabase.GetCollection<ProtoActorMember>(storageKey);
}

public async Task Register(string memberId, string host, int port)
{
await _collection.InsertOneAsync(new ProtoActorMember
{
MemberId = memberId,
Host = host,
Port = port
});
}

public async Task Remove(string memberId)
{
await _collection.DeleteOneAsync(x => x.MemberId == memberId);
}

public async Task<(string memberId, string host, int port)[]> GetAll()
{
var mongoResult = await _collection
.Find(_ => true)
.ToListAsync();

var result = mongoResult
.Select(x => (x.MemberId, x.Host, x.Port))
.ToArray();

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsPackable>true</IsPackable>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.20.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Proto.Cluster\Proto.Cluster.csproj" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions src/Proto.Cluster.SeedNode.MongoDb/ProtoActorMember.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using MongoDB.Bson.Serialization.Attributes;

namespace Proto.Cluster.SeedNode.MongoDb;

public class ProtoActorMember
{
[BsonId]
private string? Id { get; set; }

public string MemberId { get; set; } = "";
public string Host { get; set; } = "";
public int Port { get; set; } = int.MinValue;
}

0 comments on commit 2a0fe6f

Please sign in to comment.