Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WiP: Cluster Singletons - Keep alive #1115

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Proto.Cluster/Cluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -15,6 +14,7 @@
using Proto.Cluster.Identity;
using Proto.Cluster.Metrics;
using Proto.Cluster.PubSub;
using Proto.Cluster.Singleton;
using Proto.Extensions;
using Proto.Remote;

Expand Down Expand Up @@ -43,11 +43,14 @@ public Cluster(ActorSystem system, ClusterConfig config)
Gossip = new Gossiper(this);
PidCache = new PidCache();
PubSub = new PubSubManager(this);
Singleton = new SingletonManager(this);

SubscribeToTopologyEvents();
}

public PubSubManager PubSub { get; }

public SingletonManager Singleton { get; }

public static ILogger Logger { get; } = Log.CreateLogger<Cluster>();

Expand Down
2 changes: 2 additions & 0 deletions src/Proto.Cluster/Gossip/GossipActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ private Task OnGossipRequest(IContext context, GossipRequest gossipRequest)
{
context.System.EventStream.Publish(update);
}

context.System.EventStream.Publish(new Gossip(newState));

_state = newState;
CheckConsensus(context);
Expand Down
10 changes: 0 additions & 10 deletions src/Proto.Cluster/Gossip/Gossiper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@

namespace Proto.Cluster.Gossip
{
public record GossipUpdate(string MemberId, string Key, Any Value, long SequenceNumber);
public record GetGossipStateRequest(string Key);

public record GetGossipStateResponse(ImmutableDictionary<string,Any> State);

public record SetGossipStateKey(string Key, IMessage Value);

public record SendGossipStateRequest;
public record SendGossipStateResponse;

public class Gossiper
{
public const string GossipActorName = "gossip";
Expand Down
24 changes: 24 additions & 0 deletions src/Proto.Cluster/Gossip/Messages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// -----------------------------------------------------------------------
// <copyright file="Messages.cs" company="Asynkron AB">
// Copyright (C) 2015-2021 Asynkron AB All rights reserved
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Collections.Immutable;
using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;

namespace Proto.Cluster.Gossip
{
public record GossipUpdate(string MemberId, string Key, Any Value, long SequenceNumber);

public record Gossip(GossipState State);
public record GetGossipStateRequest(string Key);

public record GetGossipStateResponse(ImmutableDictionary<string,Any> State);

public record SetGossipStateKey(string Key, IMessage Value);

public record SendGossipStateRequest;
public record SendGossipStateResponse;
}
65 changes: 65 additions & 0 deletions src/Proto.Cluster/Singleton/SingletonManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// -----------------------------------------------------------------------
// <copyright file="SingletonManager.cs" company="Asynkron AB">
// Copyright (C) 2015-2021 Asynkron AB All rights reserved
// </copyright>
// -----------------------------------------------------------------------
using System.Collections.Concurrent;
using System.Collections.Immutable;
using System.Linq;
using Grpc.Core;
using Proto.Cluster.Gossip;
using Proto.Utils;

namespace Proto.Cluster.Singleton
{
public class SingletonManager
{
private readonly Cluster _cluster;
private ImmutableHashSet<string> _tracked = ImmutableHashSet<string>.Empty;
private readonly object _lock = new();

public SingletonManager(Cluster cluster)
{
_cluster = cluster;

cluster.System.EventStream.Subscribe<Gossip.Gossip>(g => {
var tracked = _tracked;
var existingKeys = (
from member in g.State.Members
from entry in member.Value.Values
where entry.Key.StartsWith("Singleton-")
select entry.Key)
//.Distinct()
.ToImmutableHashSet();

var missing = tracked.Except(existingKeys);

//iterate over all missing actors
//send a touch message to them to activate
foreach (var m in missing)
{
_ = cluster.RequestAsync<Touched>("", "", new Touch(), CancellationTokens.FromSeconds(5));
}
}
);
}

public void Track(ClusterIdentity identity)
{
lock (_lock)
{
_tracked = _tracked.Add(Key(identity));
}
}

public void Untrack(ClusterIdentity identity)
{
lock (_lock)
{
_tracked = _tracked.Remove(Key(identity));
}
}

private static string Key(ClusterIdentity identity) => "Singleton-" + identity.ToDiagnosticString();
}
}