-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathActorProxy.cs
455 lines (410 loc) · 18.3 KB
/
ActorProxy.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------
namespace Microsoft.ServiceFabric.Actors.Client
{
using System;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ServiceFabric.Actors.Remoting.V2;
using Microsoft.ServiceFabric.Actors.Runtime;
using Microsoft.ServiceFabric.Services;
using Microsoft.ServiceFabric.Services.Remoting;
using Microsoft.ServiceFabric.Services.Remoting.Builder;
using Microsoft.ServiceFabric.Services.Remoting.V2;
/// <summary>
/// Provides the base implementation for the proxy to the remote actor objects implementing <see cref="IActor"/> interfaces.
/// The proxy object can be used used for client-to-actor and actor-to-actor communication.
/// </summary>
public abstract class ActorProxy : ProxyBase, IActorProxy
{
internal static readonly ActorProxyFactory DefaultProxyFactory = new ActorProxyFactory();
private Remoting.V2.Client.ActorServicePartitionClient servicePartitionClientV2;
private RemotingClientVersion remotingClient;
#if !DotNetCoreClr
[Obsolete(Services.Remoting.DeprecationMessage.RemotingV1)]
private Remoting.V1.Builder.ActorProxyGeneratorWith proxyGeneratorWith;
[Obsolete(Services.Remoting.DeprecationMessage.RemotingV1)]
private Remoting.V1.Client.ActorServicePartitionClient servicePartitionClient;
#endif
/// <summary>
/// Initializes a new instance of the <see cref="ActorProxy"/> class.
/// </summary>
protected ActorProxy()
{
}
/// <summary>
/// Gets <see cref="ServiceFabric.Actors.ActorId"/> associated with the proxy object.
/// </summary>
/// <value><see cref="ServiceFabric.Actors.ActorId"/> associated with the proxy object.</value>
public ActorId ActorId
{
get
{
#if !DotNetCoreClr
if (!(Helper.IsEitherRemotingV2(this.remotingClient)))
{
#pragma warning disable 618
return this.servicePartitionClient.ActorId;
#pragma warning restore 618
}
#endif
return this.servicePartitionClientV2.ActorId;
}
}
#if !DotNetCoreClr
/// <summary>
/// Gets the <see cref="Remoting.V1.Client.IActorServicePartitionClient"/> interface that this proxy is using to communicate with the actor.
/// </summary>
/// <value><see cref="Remoting.V1.Client.IActorServicePartitionClient"/> that this proxy is using to communicate with the actor.</value>
[Obsolete(Services.Remoting.DeprecationMessage.RemotingV1)]
public Remoting.V1.Client.IActorServicePartitionClient ActorServicePartitionClient
{
get { return this.servicePartitionClient; }
}
#endif
/// <summary>
/// Gets the <see cref="Remoting.V2.Client.IActorServicePartitionClient"/> interface that this proxy is using to communicate with the actor.
/// </summary>
/// <value><see cref="Remoting.V2.Client.IActorServicePartitionClient"/> that this proxy is using to communicate with the actor.</value>
public Remoting.V2.Client.IActorServicePartitionClient ActorServicePartitionClientV2
{
get { return this.servicePartitionClientV2; }
}
/// <summary>
/// Creates a proxy to the actor object that implements an actor interface.
/// </summary>
/// <typeparam name="TActorInterface">
/// The actor interface implemented by the remote actor object.
/// The returned proxy object will implement this interface.
/// </typeparam>
/// <param name="actorId">The actor ID of the proxy actor object. Methods called on this proxy will result in requests
/// being sent to the actor with this ID.</param>
/// <param name="applicationName">
/// The name of the Service Fabric application that contains the actor service hosting the actor objects.
/// This parameter can be null if the client is running as part of that same Service Fabric application. For more information, see Remarks.
/// </param>
/// <param name="serviceName">
/// The name of the Service Fabric service as configured by <see cref="ActorServiceAttribute"/> on the actor implementation.
/// By default, the name of the service is derived from the name of the actor interface. However, <see cref="ActorServiceAttribute"/>
/// is required when an actor implements more than one actor interface or an actor interface derives from another actor interface since
/// the service name cannot be determined automatically.
/// </param>
/// <param name="listenerName">
/// By default an actor service has only one listener for clients to connect to and communicate with.
/// However, it is possible to configure an actor service with more than one listener. This parameter specifies the name of the listener to connect to.
/// </param>
/// <returns>An actor proxy object that implements <see cref="IActorProxy"/> and TActorInterface.</returns>
/// <remarks><para>The applicationName parameter can be null if the client is running as part of the same Service Fabric
/// application as the actor service it intends to communicate with. In this case, the application name is determined from
/// <see cref="System.Fabric.CodePackageActivationContext"/>, and is obtained by calling the
/// <see cref="System.Fabric.CodePackageActivationContext.ApplicationName"/> property.</para>
/// </remarks>
public static TActorInterface Create<TActorInterface>(
ActorId actorId,
string applicationName = null,
string serviceName = null,
string listenerName = null)
where TActorInterface : IActor
{
return DefaultProxyFactory.CreateActorProxy<TActorInterface>(
actorId,
applicationName,
serviceName,
listenerName);
}
/// <summary>
/// Creates a proxy to the actor object that implements an actor interface.
/// </summary>
/// <typeparam name="TActorInterface">
/// The actor interface implemented by the remote actor object.
/// The returned proxy object will implement this interface.
/// </typeparam>
/// <param name="actorId">Actor Id of the proxy actor object. Methods called on this proxy will result in requests
/// being sent to the actor with this id.</param>
/// <param name="serviceUri">Uri of the actor service.</param>
/// <param name="listenerName">
/// By default an actor service has only one listener for clients to connect to and communicate with.
/// However it is possible to configure an actor service with more than one listeners, the listenerName parameter specifies the name of the listener to connect to.
/// </param>
/// <returns>An actor proxy object that implements <see cref="IActorProxy"/> and TActorInterface.</returns>
public static TActorInterface Create<TActorInterface>(
ActorId actorId,
Uri serviceUri,
string listenerName = null)
where TActorInterface : IActor
{
return DefaultProxyFactory.CreateActorProxy<TActorInterface>(serviceUri, actorId, listenerName);
}
// V2 Stack Api
internal void Initialize(
Remoting.V2.Client.ActorServicePartitionClient client,
IServiceRemotingMessageBodyFactory serviceRemotingMessageBodyFactory)
{
this.servicePartitionClientV2 = client;
this.InitializeV2(serviceRemotingMessageBodyFactory);
this.remotingClient = RemotingClientVersion.V2;
}
internal override void InvokeImplV2(
int interfaceId,
int methodId,
IServiceRemotingRequestMessageBody requestMsgBodyValue)
{
// no - op as events/one way messages are not supported for services
}
internal override Task<IServiceRemotingResponseMessage> InvokeAsyncImplV2(
int interfaceId,
int methodId,
string methodName,
IServiceRemotingRequestMessageBody requestMsgBodyValue,
CancellationToken cancellationToken)
{
var requestId = Guid.NewGuid();
LogContext.Set(new LogContext
{
RequestId = requestId,
});
var headers = new ActorRemotingMessageHeaders
{
ActorId = this.servicePartitionClientV2.ActorId,
InterfaceId = interfaceId,
MethodId = methodId,
CallContext = Actors.Helper.GetCallContext(),
MethodName = methodName,
RequestId = requestId,
};
ServiceTrace.Source.WriteInfo(
"ActorProxy",
$"Invoking actor proxy - RequestId : {requestId.ToString()}, ActorId : {headers.ActorId}, MethodName : {headers.MethodName}, CallContext : {headers.CallContext}");
return this.servicePartitionClientV2.InvokeAsync(
new ServiceRemotingRequestMessage(
headers,
requestMsgBodyValue),
methodName,
cancellationToken);
}
internal async Task SubscribeAsyncV2(Type eventType, object subscriber, TimeSpan resubscriptionInterval)
{
var actorId = this.servicePartitionClientV2.ActorId;
var info = Remoting.V2.Client.ActorEventSubscriberManager.Instance.RegisterSubscriber(
actorId,
eventType,
subscriber);
Exception error = null;
try
{
await this.servicePartitionClientV2.SubscribeAsync(info.Subscriber.EventId, info.Id);
}
catch (Exception e)
{
error = e;
}
if (error != null)
{
try
{
await this.UnsubscribeAsyncV2(eventType, subscriber);
}
catch
{
// ignore
}
throw error;
}
this.ResubscribeAsyncV2(info, resubscriptionInterval);
}
internal async Task UnsubscribeAsyncV2(Type eventType, object subscriber)
{
var actorId = this.servicePartitionClientV2.ActorId;
if (Remoting.V2.Client.ActorEventSubscriberManager.Instance.TryUnregisterSubscriber(
actorId,
eventType,
subscriber,
out var info))
{
await this.servicePartitionClientV2.UnsubscribeAsync(info.Subscriber.EventId, info.Id);
}
}
#if !DotNetCoreClr
[Obsolete(Services.Remoting.DeprecationMessage.RemotingV1)]
internal override DataContractSerializer GetRequestMessageBodySerializer(int interfaceId)
{
return this.proxyGeneratorWith.GetRequestMessageBodySerializer(interfaceId);
}
[Obsolete(Services.Remoting.DeprecationMessage.RemotingV1)]
internal override DataContractSerializer GetResponseMessageBodySerializer(int interfaceId)
{
return this.proxyGeneratorWith.GetResponseMessageBodySerializer(interfaceId);
}
[Obsolete(Services.Remoting.DeprecationMessage.RemotingV1)]
internal override object GetResponseMessageBodyValue(object responseMessageBody)
{
return ((Remoting.V1.ActorMessageBody)responseMessageBody).Value;
}
[Obsolete(Services.Remoting.DeprecationMessage.RemotingV1)]
internal override object CreateRequestMessageBody(object requestMessageBodyValue)
{
return new Remoting.V1.ActorMessageBody() { Value = requestMessageBodyValue };
}
[Obsolete(Services.Remoting.DeprecationMessage.RemotingV1)]
internal override Task<byte[]> InvokeAsync(
int interfaceId,
int methodId,
byte[] requestMsgBodyBytes,
CancellationToken cancellationToken)
{
var actorMsgHeaders = new Remoting.V1.ActorMessageHeaders()
{
ActorId = this.servicePartitionClient.ActorId,
InterfaceId = interfaceId,
MethodId = methodId,
CallContext = Actors.Helper.GetCallContext(),
};
return this.servicePartitionClient.InvokeAsync(actorMsgHeaders, requestMsgBodyBytes, cancellationToken);
}
[Obsolete(Services.Remoting.DeprecationMessage.RemotingV1)]
internal override void Invoke(
int interfaceId,
int methodId,
byte[] requestMsgBodyBytes)
{
// actor proxy does not support one way messages
// actor events are sent from actor event proxy
throw new NotImplementedException();
}
[Obsolete(Services.Remoting.DeprecationMessage.RemotingV1)]
internal void Initialize(
Remoting.V1.Builder.ActorProxyGeneratorWith actorProxyGeneratorWith,
Remoting.V1.Client.ActorServicePartitionClient actorServicePartitionClient)
{
this.proxyGeneratorWith = actorProxyGeneratorWith;
this.servicePartitionClient = actorServicePartitionClient;
this.remotingClient = RemotingClientVersion.V1;
}
#endif
#region Event Subscription
internal async Task SubscribeAsync(Type eventType, object subscriber, TimeSpan resubscriptionInterval)
{
if (Helper.IsEitherRemotingV2(this.remotingClient))
{
await this.SubscribeAsyncV2(eventType, subscriber, resubscriptionInterval);
return;
}
#if !DotNetCoreClr
#pragma warning disable 618
var actorId = this.servicePartitionClient.ActorId;
var info = Remoting.V1.Client.ActorEventSubscriberManager.Instance.RegisterSubscriber(
actorId,
eventType,
subscriber);
Exception error = null;
try
{
await this.servicePartitionClient.SubscribeAsync(info.Subscriber.EventId, info.Id);
}
catch (Exception e)
{
error = e;
}
if (error != null)
{
try
{
await this.UnsubscribeAsync(eventType, subscriber);
}
catch
{
// ignore
}
throw error;
}
this.ResubscribeAsync(info, resubscriptionInterval);
#pragma warning restore 618
#endif
}
internal async Task UnsubscribeAsync(Type eventType, object subscriber)
{
if (Helper.IsEitherRemotingV2(this.remotingClient))
{
await this.UnsubscribeAsyncV2(eventType, subscriber);
return;
}
#if !DotNetCoreClr
#pragma warning disable 618
var actorId = this.servicePartitionClient.ActorId;
if (Remoting.V1.Client.ActorEventSubscriberManager.Instance.TryUnregisterSubscriber(
actorId,
eventType,
subscriber,
out var info))
{
await this.servicePartitionClient.UnsubscribeAsync(info.Subscriber.EventId, info.Id);
}
#pragma warning restore 618
#endif
}
#if !DotNetCoreClr
private void ResubscribeAsync(SubscriptionInfo info, TimeSpan resubscriptionInterval)
{
#pragma warning disable 4014
#pragma warning disable 618
// ReSharper disable once UnusedVariable
var ignore = Task.Run(
async () =>
#pragma warning restore 4014
{
while (true)
{
await Task.Delay(resubscriptionInterval);
if (!info.IsActive)
{
break;
}
try
{
await
this.servicePartitionClient.SubscribeAsync(info.Subscriber.EventId, info.Id)
.ConfigureAwait(false);
}
catch
{
// ignore
}
}
});
}
#pragma warning restore 618
#endif
private void ResubscribeAsyncV2(SubscriptionInfo info, TimeSpan resubscriptionInterval)
{
#pragma warning disable 4014
// ReSharper disable once UnusedVariable
var ignore = Task.Run(
async () =>
#pragma warning restore 4014
{
while (true)
{
await Task.Delay(resubscriptionInterval);
if (!info.IsActive)
{
break;
}
try
{
await
this.servicePartitionClientV2.SubscribeAsync(info.Subscriber.EventId, info.Id)
.ConfigureAwait(false);
}
catch
{
// ignore
}
}
});
}
#endregion
}
}