-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathPlacementAttribute.cs
153 lines (140 loc) · 5.63 KB
/
PlacementAttribute.cs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using Orleans.Metadata;
using Orleans.Runtime;
using static Orleans.Placement.ImmovableAttribute;
namespace Orleans.Placement
{
/// <summary>
/// Base for all placement policy marker attributes.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public abstract class PlacementAttribute : Attribute, IGrainPropertiesProviderAttribute
{
public PlacementStrategy PlacementStrategy { get; private set; }
protected PlacementAttribute(PlacementStrategy placement)
{
if (placement == null) throw new ArgumentNullException(nameof(placement));
this.PlacementStrategy = placement;
}
/// <inheritdoc />
public virtual void Populate(IServiceProvider services, Type grainClass, GrainType grainType, Dictionary<string, string> properties)
{
this.PlacementStrategy?.PopulateGrainProperties(services, grainClass, grainType, properties);
}
}
/// <summary>
/// Marks a grain class as using the <see cref="RandomPlacement"/> policy.
/// </summary>
/// <remarks>
/// This is the default placement policy, so this attribute does not need to be used for normal grains.
/// </remarks>
/// <inheritdoc cref="RandomPlacement"/>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class RandomPlacementAttribute : PlacementAttribute
{
public RandomPlacementAttribute() :
base(RandomPlacement.Singleton)
{
}
}
/// <summary>
/// Marks a grain class as using the <see cref="HashBasedPlacement"/> policy.
/// </summary>
/// <inheritdoc cref="HashBasedPlacement"/>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class HashBasedPlacementAttribute : PlacementAttribute
{
public HashBasedPlacementAttribute() :
base(HashBasedPlacement.Singleton)
{ }
}
/// <summary>
/// Marks a grain class as using the <see cref="PreferLocalPlacement"/> policy.
/// </summary>
/// <inheritdoc cref="PreferLocalPlacement"/>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class PreferLocalPlacementAttribute : PlacementAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="PreferLocalPlacementAttribute"/> class.
/// </summary>
public PreferLocalPlacementAttribute()
: base(PreferLocalPlacement.Singleton)
{
}
}
/// <summary>
/// Marks a grain class as using the <see cref="ActivationCountBasedPlacement"/> policy, which attempts to balance
/// grain placement across servers based upon the relative number of recently active grains on each one.
/// </summary>
/// <inheritdoc cref="ActivationCountBasedPlacement"/>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class ActivationCountBasedPlacementAttribute : PlacementAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="ActivationCountBasedPlacementAttribute"/> class.
/// </summary>
public ActivationCountBasedPlacementAttribute()
: base(ActivationCountBasedPlacement.Singleton)
{
}
}
/// <summary>
/// Marks a grain class as using the <see cref="SiloRoleBasedPlacement"/> policy.
/// </summary>
/// <inheritdoc cref="SiloRoleBasedPlacement"/>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class SiloRoleBasedPlacementAttribute : PlacementAttribute
{
public SiloRoleBasedPlacementAttribute() :
base(SiloRoleBasedPlacement.Singleton)
{ }
}
/// <summary>
/// Marks a grain class as using the <see cref="ResourceOptimizedPlacement"/> policy.
/// </summary>
/// <inheritdoc cref="ResourceOptimizedPlacement"/>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class ResourceOptimizedPlacementAttribute : PlacementAttribute
{
public ResourceOptimizedPlacementAttribute() :
base(ResourceOptimizedPlacement.Singleton)
{ }
}
/// <summary>
/// Ensures that activations of this grain type will not be migrated automatically.
/// </summary>
/// <remarks>Activations can still be migrated by user initiated code.</remarks>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class ImmovableAttribute(ImmovableKind kind = ImmovableKind.Any)
: Attribute, IGrainPropertiesProviderAttribute
{
/// <summary>
/// The kind of immovability.
/// </summary>
public ImmovableKind Kind { get; } = kind;
/// <inheritdoc/>
public void Populate(IServiceProvider services, Type grainClass, GrainType grainType, Dictionary<string, string> properties)
=> properties[WellKnownGrainTypeProperties.Immovable] = ((byte)Kind).ToString();
}
/// <summary>
/// Emphasizes that immovability is restricted to certain components.
/// </summary>
[Flags]
public enum ImmovableKind : byte
{
/// <summary>
/// Activations of this grain type will not be migrated by the repartitioner.
/// </summary>
Repartitioner = 1,
/// <summary>
/// Activations of this grain type will not be migrated by the rebalancer.
/// </summary>
Rebalancer = 2,
/// <summary>
/// Activations of this grain type will not be migrated by anything.
/// </summary>
Any = Repartitioner | Rebalancer
}
}