/
IOrleansProvider.cs
189 lines (168 loc) · 6.9 KB
/
IOrleansProvider.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using Orleans.Runtime;
namespace Orleans.Providers
{
#pragma warning disable 1574
/// <summary>
/// Base interface for all type-specific provider interfaces in Orleans
/// </summary>
/// <seealso cref="Orleans.Storage.IStorageProvider"/>
/// <seealso cref="Orleans.LogConsistency.ILogConsistencyProvider"/>
public interface IProvider
{
/// <summary>The name of this provider instance, as given to it in the config.</summary>
string Name { get; }
/// <summary>
/// Initialization function called by Orleans Provider Manager when a new provider class instance is created
/// </summary>
/// <param name="name">Name assigned for this provider</param>
/// <param name="providerRuntime">Callback for accessing system functions in the Provider Runtime</param>
/// <param name="config">Configuration metadata to be used for this provider instance</param>
/// <returns>Completion promise Task for the initialization work for this provider</returns>
Task Init(string name, IProviderRuntime providerRuntime, IProviderConfiguration config);
/// <summary>Close function for this provider instance.</summary>
/// <returns>Completion promise for the Close operation on this provider.</returns>
Task Close();
}
#pragma warning restore 1574
/// <summary>
/// Configuration information that a provider receives
/// </summary>
public interface IProviderConfiguration
{
/// <summary>
/// Full type name of this provider.
/// </summary>
string Type { get; }
/// <summary>
/// Name of this provider.
/// </summary>
string Name { get; }
/// <summary>
/// Configuration properties for this provider instance, as name-value pairs.
/// </summary>
ReadOnlyDictionary<string, string> Properties { get; }
/// <summary>
/// Set a property in this provider configuration.
/// If the property with this key already exists, it is been overwritten with the new value, otherwise it is just added.
/// </summary>
/// <param name="key">The key of the property</param>
/// <param name="val">The value of the property</param>
/// <returns>Provider instance with the given name</returns>
void SetProperty(string key, string val);
/// <summary>
/// Removes a property in this provider configuration.
/// </summary>
/// <param name="key">The key of the property.</param>
/// <returns>True if the property was found and removed, false otherwise.</returns>
bool RemoveProperty(string key);
}
public static class ProviderConfigurationExtensions
{
public static int GetIntProperty(this IProviderConfiguration config, string key, int settingDefault)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
string s;
return config.Properties.TryGetValue(key, out s) ? int.Parse(s) : settingDefault;
}
public static bool TryGetDoubleProperty(this IProviderConfiguration config, string key, out double setting)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
string s;
setting = 0;
return config.Properties.TryGetValue(key, out s) ? double.TryParse(s, out setting) : false;
}
public static string GetProperty(this IProviderConfiguration config, string key, string settingDefault)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
string s;
return config.Properties.TryGetValue(key, out s) ? s : settingDefault;
}
public static Guid GetGuidProperty(this IProviderConfiguration config, string key, Guid settingDefault)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
string s;
return config.Properties.TryGetValue(key, out s) ? Guid.Parse(s) : settingDefault;
}
public static T GetEnumProperty<T>(this IProviderConfiguration config, string key, T settingDefault)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
string s;
return config.Properties.TryGetValue(key, out s) ? (T)Enum.Parse(typeof(T),s) : settingDefault;
}
public static Type GetTypeProperty(this IProviderConfiguration config, string key, Type settingDefault)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
string s;
return config.Properties.TryGetValue(key, out s) ? Type.GetType(s) : settingDefault;
}
public static bool GetBoolProperty(this IProviderConfiguration config, string key, bool settingDefault)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
string s;
return config.Properties.TryGetValue(key, out s) ? bool.Parse(s) : settingDefault;
}
public static TimeSpan GetTimeSpanProperty(this IProviderConfiguration config, string key, TimeSpan settingDefault)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
string s;
return config.Properties.TryGetValue(key, out s) ? TimeSpan.Parse(s) : settingDefault;
}
public static bool TryGetTimeSpanProperty(this IProviderConfiguration config, string key, out TimeSpan setting)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
string s;
setting = TimeSpan.Zero;
return config.Properties.TryGetValue(key, out s) ? TimeSpan.TryParse(s, out setting) : false;
}
}
/// <summary>
/// Exception thrown whenever a provider has failed to be initialized.
/// </summary>
[Serializable]
public class ProviderInitializationException : OrleansException
{
public ProviderInitializationException()
{ }
public ProviderInitializationException(string msg)
: base(msg)
{ }
public ProviderInitializationException(string msg, Exception exc)
: base(msg, exc)
{ }
protected ProviderInitializationException(SerializationInfo info, StreamingContext context)
: base(info, context)
{ }
}
}