-
Notifications
You must be signed in to change notification settings - Fork 2k
/
RedisGrainDirectory.cs
227 lines (192 loc) · 8.48 KB
/
RedisGrainDirectory.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#nullable enable
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Orleans.Configuration;
using Orleans.Runtime;
using StackExchange.Redis;
namespace Orleans.GrainDirectory.Redis
{
public class RedisGrainDirectory : IGrainDirectory, ILifecycleParticipant<ISiloLifecycle>
{
private readonly RedisGrainDirectoryOptions _directoryOptions;
private readonly ClusterOptions _clusterOptions;
private readonly ILogger<RedisGrainDirectory> _logger;
private readonly RedisKey _keyPrefix;
private readonly string _ttl;
// Both are initialized in the Initialize method.
private IConnectionMultiplexer _redis = null!;
private IDatabase _database = null!;
public RedisGrainDirectory(
RedisGrainDirectoryOptions directoryOptions,
IOptions<ClusterOptions> clusterOptions,
ILogger<RedisGrainDirectory> logger)
{
_directoryOptions = directoryOptions;
_logger = logger;
_clusterOptions = clusterOptions.Value;
_keyPrefix = Encoding.UTF8.GetBytes($"{_clusterOptions.ClusterId}/directory/");
_ttl = directoryOptions.EntryExpiry is { } ts ? ts.TotalSeconds.ToString(CultureInfo.InvariantCulture) : "-1";
}
public async Task<GrainAddress?> Lookup(GrainId grainId)
{
try
{
var result = (string?)await _database.StringGetAsync(GetKey(grainId));
if (_logger.IsEnabled(LogLevel.Debug))
_logger.LogDebug("Lookup {GrainId}: {Result}", grainId, string.IsNullOrWhiteSpace(result) ? "null" : result);
if (string.IsNullOrWhiteSpace(result))
return default;
return JsonSerializer.Deserialize<GrainAddress>(result);
}
catch (Exception ex)
{
_logger.LogError(ex, "Lookup failed for {GrainId}", grainId);
if (IsRedisException(ex))
throw new OrleansException($"Lookup failed for {grainId} : {ex}");
else
throw;
}
}
public Task<GrainAddress?> Register(GrainAddress address) => Register(address, null);
public async Task<GrainAddress?> Register(GrainAddress address, GrainAddress? previousAddress)
{
const string RegisterScript =
"""
local cur = redis.call('GET', KEYS[1])
local success = true
if cur ~= false then
local typedCur = cjson.decode(cur)
if typedCur.ActivationId ~= ARGV[2] then
success = false
end
end
if (success) then
redis.call('SET', KEYS[1], ARGV[1])
if ARGV[3] ~= '-1' then
redis.call('EXPIRE', KEYS[1], ARGV[3])
end
return nil
end
return cur
""";
var value = JsonSerializer.Serialize(address);
try
{
var previousActivationId = previousAddress is { } ? previousAddress.ActivationId.ToString() : "";
var key = GetKey(address.GrainId);
var entryString = (string?)await _database.ScriptEvaluateAsync(
RegisterScript,
keys: new RedisKey[] { key },
values: new RedisValue[] { value, previousActivationId, _ttl })!;
if (entryString is null)
{
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("Registered {GrainId} ({Address})", address.GrainId, value);
}
return address;
}
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("Failed to register {GrainId} ({Address}) in directory: Conflicted with existing value, {Result}", address.GrainId, value, entryString);
}
return JsonSerializer.Deserialize<GrainAddress>(entryString);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to register {GrainId} ({Address}) in directory", address.GrainId, value);
if (IsRedisException(ex))
{
throw new OrleansException($"Register failed for {address.GrainId} ({value}) : {ex}");
}
else
{
throw;
}
}
}
public async Task Unregister(GrainAddress address)
{
const string DeleteScript =
"""
local cur = redis.call('GET', KEYS[1])
if cur ~= false then
local typedCur = cjson.decode(cur)
if typedCur.ActivationId == ARGV[1] then
return redis.call('DEL', KEYS[1])
end
end
return 0
""";
try
{
var value = JsonSerializer.Serialize(address);
var result = (int) await _database.ScriptEvaluateAsync(
DeleteScript,
keys: new RedisKey[] { GetKey(address.GrainId) },
values: new RedisValue[] { address.ActivationId.ToString() });
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("Unregister {GrainId} ({Address}): {Result}", address.GrainId, JsonSerializer.Serialize(address), (result != 0) ? "OK" : "Conflict");
}
}
catch (Exception ex)
{
var value = JsonSerializer.Serialize(address);
_logger.LogError(ex, "Unregister failed for {GrainId} ({Address})", address.GrainId, value);
if (IsRedisException(ex))
throw new OrleansException($"Unregister failed for {address.GrainId} ({value}) : {ex}");
else
throw;
}
}
public Task UnregisterSilos(List<SiloAddress> siloAddresses)
{
return Task.CompletedTask;
}
public void Participate(ISiloLifecycle lifecycle)
{
lifecycle.Subscribe(nameof(RedisGrainDirectory), ServiceLifecycleStage.RuntimeInitialize, Initialize, Uninitialize);
}
public async Task Initialize(CancellationToken ct = default)
{
_redis = await _directoryOptions.CreateMultiplexer(_directoryOptions);
// Configure logging
_redis.ConnectionRestored += LogConnectionRestored;
_redis.ConnectionFailed += LogConnectionFailed;
_redis.ErrorMessage += LogErrorMessage;
_redis.InternalError += LogInternalError;
_database = _redis.GetDatabase();
}
private async Task Uninitialize(CancellationToken arg)
{
if (_redis != null && _redis.IsConnected)
{
await _redis.CloseAsync();
_redis.Dispose();
_redis = null!;
_database = null!;
}
}
private RedisKey GetKey(GrainId grainId) => _keyPrefix.Append(grainId.ToString());
#region Logging
private void LogConnectionRestored(object? sender, ConnectionFailedEventArgs e)
=> _logger.LogInformation(e.Exception, "Connection to {EndPoint} failed: {FailureType}", e.EndPoint, e.FailureType);
private void LogConnectionFailed(object? sender, ConnectionFailedEventArgs e)
=> _logger.LogError(e.Exception, "Connection to {EndPoint} failed: {FailureType}", e.EndPoint, e.FailureType);
private void LogErrorMessage(object? sender, RedisErrorEventArgs e)
=> _logger.LogError(e.Message);
private void LogInternalError(object? sender, InternalErrorEventArgs e)
=> _logger.LogError(e.Exception, "Internal error");
#endregion
// These exceptions are not serializable by the client
private static bool IsRedisException(Exception ex) => ex is RedisException || ex is RedisTimeoutException || ex is RedisCommandException;
}
}