-
-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathPartitionManager.cs
More file actions
115 lines (95 loc) · 3.81 KB
/
PartitionManager.cs
File metadata and controls
115 lines (95 loc) · 3.81 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// -----------------------------------------------------------------------
// <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.Partition;
//helper to interact with partition actors on this and other members
internal class PartitionManager
{
private const string PartitionIdentityActorName = "$partition-identity";
private const string PartitionPlacementActorName = "$partition-activator";
private readonly Cluster _cluster;
private readonly PartitionConfig _config;
private readonly IRootContext _context;
private readonly bool _isClient;
private readonly ActorSystem _system;
private readonly Func<Props, Props>? _configurePlacementProps;
private PID _partitionIdentityActor = null!;
private PID _partitionPlacementActor = null!;
internal PartitionManager(Cluster cluster, bool isClient, PartitionConfig config, Func<Props, Props>? configurePlacementProps = null)
{
_cluster = cluster;
_system = cluster.System;
_context = _system.Root;
_isClient = isClient;
_config = config;
_configurePlacementProps = configurePlacementProps;
}
internal PartitionMemberSelector Selector { get; } = new();
public void Setup()
{
if (_isClient)
{
var eventId = 0ul;
//make sure selector is updated first
_system.EventStream.Subscribe<ClusterTopology>(e =>
{
if (e.TopologyHash == eventId)
{
return;
}
eventId = e.TopologyHash;
Selector.Update(e.Members.ToArray(), e.TopologyHash);
}
);
}
else
{
var partitionActorProps = Props
.FromProducer(() => new PartitionIdentityActor(_cluster, _config));
_partitionIdentityActor = _context.SpawnNamedSystem(partitionActorProps, PartitionIdentityActorName);
var partitionActivatorProps = Props.FromProducer(() => new PartitionPlacementActor(_cluster, _config));
if (_configurePlacementProps is not null)
{
partitionActivatorProps = _configurePlacementProps(partitionActivatorProps);
}
_partitionPlacementActor = _context.SpawnNamedSystem(partitionActivatorProps, PartitionPlacementActorName);
//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(), e.TopologyHash);
_context.Send(_partitionIdentityActor, e);
_context.Send(_partitionPlacementActor, e);
}
);
}
}
public async Task ShutdownAsync()
{
if (_isClient)
{
}
else
{
await Task.WhenAll(
_context.StopAsync(_partitionPlacementActor),
_context.StopAsync(_partitionIdentityActor)
).ConfigureAwait(false);
}
}
public static PID RemotePartitionIdentityActor(string address) =>
PID.FromAddress(address, PartitionIdentityActorName);
public static PID RemotePartitionPlacementActor(string address) =>
PID.FromAddress(address, PartitionPlacementActorName);
}