-
Notifications
You must be signed in to change notification settings - Fork 527
Expand file tree
/
Copy pathPartitionKeyRangeCacheTests.cs
More file actions
339 lines (274 loc) · 14 KB
/
PartitionKeyRangeCacheTests.cs
File metadata and controls
339 lines (274 loc) · 14 KB
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
// unset
namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Routing;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using static Microsoft.Azure.Cosmos.SDK.EmulatorTests.TransportClientHelper;
[TestClass]
public class PartitionKeyRangeCacheTests
{
private bool loopBackgroundOperaitons = false;
[TestInitialize]
public void TestInitialize()
{
this.loopBackgroundOperaitons = false;
}
[TestCleanup]
public void TestCleanup()
{
this.loopBackgroundOperaitons = false;
}
[TestMethod]
public async Task VerifyPkRangeCacheRefreshOnSplitWithErrorsAsync()
{
int throwOnPkRefreshCount = 3;
int pkRangeCalls = 0;
bool causeSplitExceptionInRntbdCall = false;
HttpClientHandlerHelper httpHandlerHelper = new();
List<string> ifNoneMatchValues = new();
string failedIfNoneMatchValue = null;
httpHandlerHelper.RequestCallBack = (request, cancellationToken) =>
{
if (!request.RequestUri.ToString().EndsWith("pkranges"))
{
return null;
}
ifNoneMatchValues.Add(request.Headers.IfNoneMatch.ToString());
pkRangeCalls++;
if (pkRangeCalls == throwOnPkRefreshCount)
{
failedIfNoneMatchValue = request.Headers.IfNoneMatch.ToString();
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.InternalServerError));
}
return null;
};
int countSplitExceptions = 0;
CosmosClientOptions clientOptions = new CosmosClientOptions()
{
HttpClientFactory = () => new HttpClient(httpHandlerHelper),
TransportClientHandlerFactory = (transportClient) => new TransportClientWrapper(
transportClient,
(uri, resource, dsr) =>
{
if (dsr.OperationType == Documents.OperationType.Read &&
dsr.ResourceType == Documents.ResourceType.Document &&
causeSplitExceptionInRntbdCall)
{
countSplitExceptions++;
causeSplitExceptionInRntbdCall = false;
throw new Documents.Routing.PartitionKeyRangeIsSplittingException("Test");
}
})
};
using CosmosClient resourceClient = TestCommon.CreateCosmosClient(clientOptions);
string dbName = Guid.NewGuid().ToString();
string containerName = nameof(PartitionKeyRangeCacheTests);
Database db = await resourceClient.CreateDatabaseIfNotExistsAsync(dbName);
Container container = await db.CreateContainerIfNotExistsAsync(
containerName,
"/pk",
400);
// Start a background job that loops forever
List<Exception> exceptions = new();
Task backgroundItemOperatios = Task.Factory.StartNew(() => this.CreateAndReadItemBackgroundLoop(container, exceptions));
// Wait for the background job to start
Documents.ValueStopwatch stopwatch = Documents.ValueStopwatch.StartNew();
while (!this.loopBackgroundOperaitons && stopwatch.Elapsed.TotalSeconds < 30)
{
await Task.Delay(TimeSpan.FromSeconds(.5));
}
Assert.IsTrue(this.loopBackgroundOperaitons);
Assert.AreEqual(2, pkRangeCalls);
// Cause direct call to hit a split exception and wait for the background job to hit it
causeSplitExceptionInRntbdCall = true;
stopwatch = Documents.ValueStopwatch.StartNew();
while (causeSplitExceptionInRntbdCall && stopwatch.Elapsed.TotalSeconds < 10)
{
await Task.Delay(TimeSpan.FromSeconds(.5));
}
Assert.IsFalse(causeSplitExceptionInRntbdCall);
Assert.AreEqual(3, pkRangeCalls);
// Cause another direct call split exception
causeSplitExceptionInRntbdCall = true;
stopwatch = Documents.ValueStopwatch.StartNew();
while (causeSplitExceptionInRntbdCall && stopwatch.Elapsed.TotalSeconds < 10)
{
await Task.Delay(TimeSpan.FromSeconds(.5));
}
Assert.IsFalse(causeSplitExceptionInRntbdCall);
Assert.AreEqual(4, pkRangeCalls);
Assert.AreEqual(1, ifNoneMatchValues.Count(x => string.IsNullOrEmpty(x)));
Assert.AreEqual(3, ifNoneMatchValues.Count(x => x == failedIfNoneMatchValue), $"3 request with same if none value. 1 initial, 2 from the split errors. split exception count: {countSplitExceptions}; {string.Join(';', ifNoneMatchValues)}");
HashSet<string> verifyUniqueIfNoneHeaderValues = new HashSet<string>();
foreach (string ifNoneValue in ifNoneMatchValues)
{
if (!verifyUniqueIfNoneHeaderValues.Contains(ifNoneValue))
{
verifyUniqueIfNoneHeaderValues.Add(ifNoneValue);
}
else if (ifNoneValue != failedIfNoneMatchValue)
{
Assert.AreEqual(failedIfNoneMatchValue, ifNoneValue, $"Multiple duplicates; split exception count: {countSplitExceptions}; {string.Join(';', ifNoneMatchValues)}");
}
}
Assert.AreEqual(0, exceptions.Count, $"Unexpected exceptions: {string.Join(';', exceptions)}");
await db.DeleteStreamAsync();
}
private async Task CreateAndReadItemBackgroundLoop(Container container, List<Exception> exceptions)
{
this.loopBackgroundOperaitons = true;
while (this.loopBackgroundOperaitons)
{
ToDoActivity toDoActivity = ToDoActivity.CreateRandomToDoActivity();
try
{
await container.CreateItemAsync<ToDoActivity>(toDoActivity);
await container.ReadItemAsync<ToDoActivity>(toDoActivity.id, new PartitionKey(toDoActivity.pk));
}
catch (CosmosException ce) when (ce.StatusCode == HttpStatusCode.InternalServerError)
{
// Expected exception caused by failure on pk range refresh
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}
}
[TestMethod]
public async Task VerifyPkRangeCacheRefreshOnThrottlesAsync()
{
int pkRangeCalls = 0;
bool causeSplitExceptionInRntbdCall = false;
HttpClientHandlerHelper httpHandlerHelper = new();
List<string> ifNoneMatchValues = new();
httpHandlerHelper.RequestCallBack = (request, cancellationToken) =>
{
if (!request.RequestUri.ToString().EndsWith("pkranges"))
{
return null;
}
ifNoneMatchValues.Add(request.Headers.IfNoneMatch.ToString());
pkRangeCalls++;
// Cause throttle on the init call
if (pkRangeCalls <= 3)
{
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.TooManyRequests));
}
return null;
};
int countSplitExceptions = 0;
CosmosClientOptions clientOptions = new CosmosClientOptions()
{
HttpClientFactory = () => new HttpClient(httpHandlerHelper),
TransportClientHandlerFactory = (transportClient) => new TransportClientWrapper(
transportClient,
(uri, resource, dsr) =>
{
if (dsr.ResourceType == Documents.ResourceType.Document &&
causeSplitExceptionInRntbdCall)
{
countSplitExceptions++;
causeSplitExceptionInRntbdCall = false;
throw new Documents.Routing.PartitionKeyRangeIsSplittingException("Test");
}
})
};
using CosmosClient resourceClient = TestCommon.CreateCosmosClient(clientOptions);
string dbName = Guid.NewGuid().ToString();
string containerName = nameof(PartitionKeyRangeCacheTests);
Database db = await resourceClient.CreateDatabaseIfNotExistsAsync(dbName);
Container container = await db.CreateContainerIfNotExistsAsync(
containerName,
"/pk",
400);
ToDoActivity toDoActivity = ToDoActivity.CreateRandomToDoActivity();
await container.CreateItemAsync<ToDoActivity>(toDoActivity);
Assert.AreEqual(5, pkRangeCalls);
Assert.AreEqual(4, ifNoneMatchValues.Count(x => string.IsNullOrEmpty(x)), "First 3 calls are throttled and 4 succeeds");
string lastIfNoneMatchValue = ifNoneMatchValues.Last();
ifNoneMatchValues.Clear();
pkRangeCalls = 0;
causeSplitExceptionInRntbdCall = true;
await container.ReadItemAsync<ToDoActivity>(toDoActivity.id, new PartitionKey(toDoActivity.pk));
Assert.AreEqual(4, pkRangeCalls);
Assert.AreEqual(0, ifNoneMatchValues.Count(x => string.IsNullOrEmpty(x)), "The cache is already init. It should never re-initialize the cache.");
await db.DeleteStreamAsync();
}
[TestMethod]
public async Task VerifyPkRangeCacheRefreshOnTimeoutsAsync()
{
int pkRangeCalls = 0;
HttpClientHandlerHelper httpHandlerHelper = new();
List<string> ifNoneMatchValues = new();
httpHandlerHelper.RequestCallBack = (request, cancellationToken) =>
{
if (!request.RequestUri.ToString().EndsWith("pkranges"))
{
return null;
}
ifNoneMatchValues.Add(request.Headers.IfNoneMatch.ToString());
pkRangeCalls++;
// Cause timeout on the init
if (pkRangeCalls == 1)
{
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.RequestTimeout));
}
return null;
};
CosmosClientOptions clientOptions = new CosmosClientOptions()
{
HttpClientFactory = () => new HttpClient(httpHandlerHelper),
};
using CosmosClient resourceClient = TestCommon.CreateCosmosClient(clientOptions);
string dbName = Guid.NewGuid().ToString();
string containerName = nameof(PartitionKeyRangeCacheTests);
Database db = await resourceClient.CreateDatabaseIfNotExistsAsync(dbName);
Container container = await db.CreateContainerIfNotExistsAsync(
containerName,
"/pk",
400);
ToDoActivity toDoActivity = ToDoActivity.CreateRandomToDoActivity();
await container.CreateItemAsync<ToDoActivity>(toDoActivity);
Assert.AreEqual(3, pkRangeCalls);
Assert.AreEqual(2, ifNoneMatchValues.Count(x => string.IsNullOrEmpty(x)), "First call is a 408");
await db.DeleteStreamAsync();
}
[TestMethod]
public async Task TestRidRefreshOnNotFoundAsync()
{
using CosmosClient resourceClient = TestCommon.CreateCosmosClient();
string dbName = Guid.NewGuid().ToString();
string containerName = Guid.NewGuid().ToString();
Database db = await resourceClient.CreateDatabaseAsync(dbName);
Container container = await db.CreateContainerAsync(containerName, "/_id");
using CosmosClient testClient = TestCommon.CreateCosmosClient();
ContainerInternal testContainer = (ContainerInlineCore)testClient.GetContainer(dbName, containerName);
// Populate the RID cache.
string cachedRidAsync = await testContainer.GetCachedRIDAsync(forceRefresh: false, trace: NoOpTrace.Singleton, cancellationToken: default);
// Delete the container (using resource client).
await container.DeleteContainerAsync();
// Because the RID is cached, this will now try to resolve the collection routing map.
Assert.AreEqual(cachedRidAsync, await testContainer.GetCachedRIDAsync(forceRefresh: false, trace: NoOpTrace.Singleton, cancellationToken: default));
CosmosException notFoundException = await Assert.ThrowsExceptionAsync<CosmosException>(() => testContainer.GetRoutingMapAsync(cancellationToken: default));
Assert.AreEqual(HttpStatusCode.NotFound, notFoundException.StatusCode);
await db.CreateContainerAsync(containerName, "/_id");
CollectionRoutingMap collectionRoutingMap = await testContainer.GetRoutingMapAsync(cancellationToken: default);
Assert.IsNotNull(collectionRoutingMap);
Assert.AreNotEqual(cachedRidAsync, await testContainer.GetCachedRIDAsync(forceRefresh: false, trace: NoOpTrace.Singleton, cancellationToken: default));
// Delete the container (using resource client).
await container.DeleteContainerAsync();
CollectionRoutingMap collectionRoutingMapFromCache = await testContainer.GetRoutingMapAsync(cancellationToken: default);
Assert.AreEqual(collectionRoutingMap, collectionRoutingMapFromCache);
await db.DeleteStreamAsync();
}
}
}