-
Notifications
You must be signed in to change notification settings - Fork 494
/
ServerStoreModel.cs
133 lines (112 loc) · 4.61 KB
/
ServerStoreModel.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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Documents
{
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
internal sealed class ServerStoreModel : IStoreModelExtension
{
private readonly StoreClient storeClient;
private EventHandler<SendingRequestEventArgs> sendingRequest;
private readonly EventHandler<ReceivedResponseEventArgs> receivedResponse;
public ServerStoreModel(StoreClient storeClient)
{
this.storeClient = storeClient;
}
public ServerStoreModel(StoreClient storeClient, EventHandler<SendingRequestEventArgs> sendingRequest, EventHandler<ReceivedResponseEventArgs> receivedResponse)
: this(storeClient)
{
this.sendingRequest = sendingRequest;
this.receivedResponse = receivedResponse;
}
#region Test hooks
public uint? DefaultReplicaIndex
{
get;
set;
}
public string LastReadAddress
{
get
{
return this.storeClient.LastReadAddress;
}
set
{
this.storeClient.LastReadAddress = value;
}
}
public bool ForceAddressRefresh
{
get
{
return this.storeClient.ForceAddressRefresh;
}
set
{
this.storeClient.ForceAddressRefresh = value;
}
}
#endregion
public Task<DocumentServiceResponse> ProcessMessageAsync(DocumentServiceRequest request, CancellationToken cancellationToken = default(CancellationToken))
{
if (this.DefaultReplicaIndex.HasValue)
{
request.DefaultReplicaIndex = this.DefaultReplicaIndex;
}
string requestConsistencyLevelHeaderValue = request.Headers[HttpConstants.HttpHeaders.ConsistencyLevel];
request.RequestContext.OriginalRequestConsistencyLevel = null;
if (!string.IsNullOrEmpty(requestConsistencyLevelHeaderValue))
{
ConsistencyLevel requestConsistencyLevel;
if (!Enum.TryParse<ConsistencyLevel>(requestConsistencyLevelHeaderValue, out requestConsistencyLevel))
{
throw new BadRequestException(
string.Format(CultureInfo.CurrentUICulture,
RMResources.InvalidHeaderValue,
requestConsistencyLevelHeaderValue,
HttpConstants.HttpHeaders.ConsistencyLevel));
}
request.RequestContext.OriginalRequestConsistencyLevel = requestConsistencyLevel;
}
if (ReplicatedResourceClient.IsMasterResource(request.ResourceType))
{
request.Headers[HttpConstants.HttpHeaders.ConsistencyLevel] = ConsistencyLevel.Strong.ToString();
}
this.sendingRequest?.Invoke(this, new SendingRequestEventArgs(request));
if (this.receivedResponse != null)
{
return this.ProcessMessageWithReceivedResponseDelegateAsync(request, cancellationToken);
}
else
{
return this.storeClient.ProcessMessageAsync(request, cancellationToken);
}
}
/// <inheritdoc/>>
public async Task OpenConnectionsToAllReplicasAsync(
string databaseName,
string containerLinkUri,
CancellationToken cancellationToken = default)
{
await this.storeClient.OpenConnectionsToAllReplicasAsync(
databaseName,
containerLinkUri,
cancellationToken);
}
private async Task<DocumentServiceResponse> ProcessMessageWithReceivedResponseDelegateAsync(
DocumentServiceRequest request,
CancellationToken cancellationToken = default(CancellationToken))
{
DocumentServiceResponse response = await this.storeClient.ProcessMessageAsync(request, cancellationToken);
this.receivedResponse?.Invoke(this, new ReceivedResponseEventArgs(request, response));
return response;
}
public void Dispose()
{
}
}
}