/
Service.cs
283 lines (235 loc) · 13.3 KB
/
Service.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
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------
namespace HealthMetrics.BandCreationService
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Fabric;
using System.Fabric.Description;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using HealthMetrics.BandActor.Interfaces;
using HealthMetrics.Common;
using HealthMetrics.DoctorActor.Interfaces;
using Microsoft.ServiceFabric.Actors;
using Microsoft.ServiceFabric.Actors.Client;
using Microsoft.ServiceFabric.Services.Communication.Client;
using Microsoft.ServiceFabric.Services.Runtime;
public class Service : StatelessService
{
// This is the name of the ServiceType that is registered with FabricRuntime.
// This name must match the name defined in the ServiceManifest. If you change
// this name, please change the name of the ServiceType in the ServiceManifest.
public const string ServiceTypeName = "HealthMetrics.BandCreationServiceType";
private static FabricClient fabricClient = new FabricClient();
private Uri ActorServiceUri;
private Uri DoctorServiceUri;
private int NumberOfCreationThreads;
private int MaxBandsToCreatePerServiceInstance;
private bool GenerateKnownPeople;
private ConcurrentDictionary<int, ServicePartitionClient<HttpCommunicationClient>> communicationClientDictionary =
new ConcurrentDictionary<int, ServicePartitionClient<HttpCommunicationClient>>();
public Service(StatelessServiceContext serviceContext) : base(serviceContext)
{
}
/// <summary>
/// This is the main entry point for your service instance.
/// </summary>
/// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service instance.</param>
protected override async Task RunAsync(CancellationToken cancellationToken)
{
ConfigurationSettings configSettings = FabricRuntime.GetActivationContext().GetConfigurationPackageObject("Config").Settings;
KeyedCollection<string, ConfigurationProperty> serviceParameters = configSettings.Sections["HealthMetrics.BandCreationService.Settings"].Parameters;
this.NumberOfCreationThreads = int.Parse(serviceParameters["NumberOfCreationThreads"].Value); // 4
this.MaxBandsToCreatePerServiceInstance = int.Parse(serviceParameters["MaxBandsToCreatePerServiceInstance"].Value); // 4000
/// the uri is this format: fabric:/applicationName/serviceName
this.ActorServiceUri = new ServiceUriBuilder(serviceParameters["BandActorServiceName"].Value).ToUri();
this.DoctorServiceUri = new ServiceUriBuilder(serviceParameters["DoctorActorServiceName"].Value).ToUri();
this.GenerateKnownPeople = bool.Parse(serviceParameters["GenerateKnownPeople"].Value); // false
string dataPath = FabricRuntime.GetActivationContext().GetDataPackageObject("Data").Path;
BandActorGenerator bag = new BandActorGenerator(configSettings, dataPath);
bag.Prepare();
ServicePrimer primer = new ServicePrimer();
await primer.WaitForStatefulService(this.ActorServiceUri, cancellationToken);
List<Task> tasks = new List<Task>();
if (this.GenerateKnownPeople)
{
tasks.Add(Task.Run(() => this.CreateKnownActors(bag, configSettings, cancellationToken)));
}
for (int i = 0; i < this.NumberOfCreationThreads; i++)
{
tasks.Add(Task.Run(() => this.CreateBandActorTask(bag, cancellationToken), cancellationToken));
}
ServiceEventSource.Current.ServiceMessage(this, "Band Creation has begun.");
await Task.WhenAll(tasks);
ServiceEventSource.Current.ServiceMessage(this, "Band Creation has completed.");
}
private async Task CreateBandActorTask(BandActorGenerator bag, CancellationToken cancellationToken)
{
CryptoRandom random = new CryptoRandom();
while (!cancellationToken.IsCancellationRequested && this.MaxBandsToCreatePerServiceInstance > 0)
{
bool created = false;
while (!created && !cancellationToken.IsCancellationRequested)
{
ActorId bandActorId;
ActorId doctorActorId;
int randomCountyId = -1;
string doctorName = null;
randomCountyId = random.Next(0, bag.doctorsPerCounty.Keys.Count);
doctorName = bag.GetRandomName(random);
CountyRecord randomCountyRecord = bag.doctorsPerCounty.Keys.ElementAt(randomCountyId);
BandInfo bandActorInfo = bag.GetRandomHealthStatus(randomCountyRecord, random); // each band is associated with one doctor
try
{
bandActorId = new ActorId(Guid.NewGuid()); // band actor id is guid
doctorActorId = new ActorId(bandActorInfo.DoctorId); // the relationship between band and doctor is multiple to 1.
IDoctorActor docActor = ActorProxy.Create<IDoctorActor>(doctorActorId, this.DoctorServiceUri);
await docActor.NewAsync(doctorName, randomCountyRecord);
IBandActor bandActor = ActorProxy.Create<IBandActor>(bandActorId, this.ActorServiceUri);
await bandActor.NewAsync(bandActorInfo);
ServiceEventSource.Current.Message("Actor created {0} verifying...", bandActorId);
await VerifyActors(
new HealthIndexCalculator(this.Context),
bandActorId,
doctorName,
randomCountyRecord,
bandActorInfo,
docActor,
bandActor,
cancellationToken);
}
catch (Exception e)
{
ServiceEventSource.Current.ServiceMessage(this, "Failed to iniitalize band or doctor. {0}", e.ToString());
}
created = true;
}
this.MaxBandsToCreatePerServiceInstance--;
ServiceEventSource.Current.ServiceMessage(this, "Created Actors, {0} remaining", this.MaxBandsToCreatePerServiceInstance);
await Task.Delay(100, cancellationToken);
}
}
private async Task CreateKnownActors(BandActorGenerator bag, ConfigurationSettings settings, CancellationToken cancellationToken)
{
ServiceEventSource.Current.ServiceMessage(this, "Creating known actors");
CryptoRandom random = new CryptoRandom();
FabricClient fc = new FabricClient();
HealthIndexCalculator hic = new HealthIndexCalculator(this.Context);
KeyedCollection<string, ConfigurationProperty> serviceParameters = settings.Sections["HealthMetrics.BandCreationService.Settings"].Parameters;
while (!cancellationToken.IsCancellationRequested)
{
ActorId bandActorId;
ActorId doctorActorId;
int randomCountyId = -1;
string doctorName = null;
randomCountyId = int.Parse(serviceParameters["KnownCountyIdIndex"].Value);
//(2968 is King, WA) || (2231 is Multnomah, OR) || (1870 is St. Lawrence, NY)
doctorName = serviceParameters["KnownDoctorName"].Value;
CountyRecord randomCountyRecord = bag.doctorsPerCounty.Keys.ElementAt(randomCountyId);
BandInfo bandActorInfo = bag.GetRandomHealthStatus(randomCountyRecord, random);
try
{
bandActorInfo.PersonName = serviceParameters["KnownPatientName"].Value;
bandActorId = new ActorId(new Guid(serviceParameters["KnownPatientId"].Value));
bandActorInfo.DoctorId = new Guid(serviceParameters["KnownDoctorId"].Value);
doctorActorId = new ActorId(bandActorInfo.DoctorId);
bag.doctorsPerCounty[bag.doctorsPerCounty.Keys.ElementAt(randomCountyId)].Add(bandActorInfo.DoctorId);
IDoctorActor docActor = ActorProxy.Create<IDoctorActor>(doctorActorId, this.DoctorServiceUri);
await docActor.NewAsync(doctorName, randomCountyRecord);
IBandActor bandActor = ActorProxy.Create<IBandActor>(bandActorId, this.ActorServiceUri);
await bandActor.NewAsync(bandActorInfo);
ServiceEventSource.Current.ServiceMessage(this, "Known actors news sent, verifying");
await VerifyActors(hic, bandActorId, doctorName, randomCountyRecord, bandActorInfo, docActor, bandActor, cancellationToken);
break;
}
catch (Exception e)
{
ServiceEventSource.Current.ServiceMessage(this, "Exception when creating actor {0}", e.ToString());
}
}
}
private static async Task VerifyActors(
HealthIndexCalculator hic, ActorId bandActorId, string doctorName, CountyRecord randomCountyRecord, BandInfo bandActorInfo, IDoctorActor docActor,
IBandActor bandActor, CancellationToken ct)
{
ServiceEventSource.Current.Message("Verifying Actor {0}", bandActorId);
bool bandVerified = false;
bool doctorVerified = false;
int bandErrorCount = 0;
int doctorErrorCount = 0;
while (!ct.IsCancellationRequested && !bandVerified && !doctorVerified)
{
await Task.Delay(100);
if (!bandVerified)
{
try
{
BandDataViewModel view = await bandActor.GetBandDataAsync();
if (view.PersonName == bandActorInfo.PersonName)
{
if (view.CountyInfo == bandActorInfo.CountyInfo)
{
if (view.DoctorId == bandActorInfo.DoctorId)
{
if (view.PersonId == bandActorId.GetGuidId())
{
if (view.HealthIndexValue == bandActorInfo.HealthIndex)
{
bandVerified = true;
ServiceEventSource.Current.Message("Band actor verified.");
}
else
{
await bandActor.NewAsync(bandActorInfo);
}
}
}
}
}
}
catch (Exception e)
{
bandErrorCount++;
ServiceEventSource.Current.Message("Exception Count {0} verifying band actor, retrying. {1}", bandErrorCount, e);
}
}
else
{
ServiceEventSource.Current.Message("band already verified, skipping");
}
if (!doctorVerified)
{
try
{
Tuple<CountyRecord, string> info = await docActor.GetInfoAndNameAsync();
if (info.Item2 == String.Format("Dr. {0}", doctorName)
&& info.Item1 == randomCountyRecord)
{
doctorVerified = true;
ServiceEventSource.Current.Message("Doctor actor verified.");
}
else
{
await docActor.NewAsync(doctorName, randomCountyRecord);
}
}
catch (Exception e)
{
doctorErrorCount++;
ServiceEventSource.Current.Message("Exception Count {0} verifying doctor actor, retrying. {1}", doctorErrorCount, e);
}
}
else
{
ServiceEventSource.Current.Message("doctor already verified, skipping");
}
}
}
}
}