-
-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathPartitionActivatorManager.cs
More file actions
101 lines (83 loc) · 3.03 KB
/
PartitionActivatorManager.cs
File metadata and controls
101 lines (83 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// -----------------------------------------------------------------------
// <copyright file="PartitionManager.cs" company="Asynkron AB">
// Copyright (C) 2015-2025 Asynkron AB All rights reserved
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Proto.Cluster.PartitionActivator;
//helper to interact with partition actors on this and other members
public class PartitionActivatorManager
{
private const string PartitionActivatorActorName = "$partition-activator";
private readonly Cluster _cluster;
private readonly IRootContext _context;
private readonly bool _isClient;
private readonly ActorSystem _system;
private readonly Func<Props, Props>? _configureProps;
private PID _partitionActivatorActor = null!;
internal PartitionActivatorManager(Cluster cluster, bool isClient, Func<Props, Props>? configureProps = null)
{
_cluster = cluster;
_system = cluster.System;
_context = _system.Root;
_isClient = isClient;
_configureProps = configureProps;
}
internal PartitionActivatorSelector Selector { get; } = new();
public void Setup()
{
if (_isClient)
{
var topologyHash = 0ul;
//make sure selector is updated first
_system.EventStream.Subscribe<ClusterTopology>(e =>
{
if (e.TopologyHash == topologyHash)
{
return;
}
topologyHash = e.TopologyHash;
Selector.Update(e.Members.ToArray());
}
);
}
else
{
var partitionActivatorProps =
Props.FromProducer(() => new PartitionActivatorActor(_cluster, this));
if (_configureProps is not null)
{
partitionActivatorProps = _configureProps(partitionActivatorProps);
}
_partitionActivatorActor = _context.SpawnNamedSystem(partitionActivatorProps, PartitionActivatorActorName);
//synchronous subscribe to keep accurate
var topologyHash = 0ul;
//make sure selector is updated first
_system.EventStream.Subscribe<ClusterTopology>(e =>
{
if (e.TopologyHash == topologyHash)
{
return;
}
topologyHash = e.TopologyHash;
Selector.Update(e.Members.ToArray());
_context.Send(_partitionActivatorActor, e);
}
);
}
}
public async Task ShutdownAsync()
{
if (_isClient)
{
}
else
{
await _context.StopAsync(_partitionActivatorActor).ConfigureAwait(false);
}
}
public static PID RemotePartitionActivatorActor(string address) =>
PID.FromAddress(address, PartitionActivatorActorName);
}