forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTestMultiMMap.cs
More file actions
551 lines (515 loc) · 22.1 KB
/
Copy pathTestMultiMMap.cs
File metadata and controls
551 lines (515 loc) · 22.1 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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
using Lucene.Net.Attributes;
using Lucene.Net.Documents;
using Lucene.Net.Index.Extensions;
using NUnit.Framework;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using Assert = Lucene.Net.TestFramework.Assert;
namespace Lucene.Net.Store
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using BytesRef = Lucene.Net.Util.BytesRef;
using Document = Documents.Document;
using Field = Field;
using IndexInputSlicer = Lucene.Net.Store.Directory.IndexInputSlicer;
using IndexReader = Lucene.Net.Index.IndexReader;
using LuceneTestCase = Lucene.Net.Util.LuceneTestCase;
using MockAnalyzer = Lucene.Net.Analysis.MockAnalyzer;
using RandomIndexWriter = Lucene.Net.Index.RandomIndexWriter;
using TestUtil = Lucene.Net.Util.TestUtil;
/// <summary>
/// Tests MMapDirectory's MultiMMapIndexInput
/// <p>
/// Because Java's ByteBuffer uses an int to address the
/// values, it's necessary to access a file >
/// Integer.MAX_VALUE in size using multiple byte buffers.
/// </summary>
[TestFixture]
public class TestMultiMMap : LuceneTestCase
{
[SetUp]
public override void SetUp()
{
base.SetUp();
// LUCENENET NOTE:
// Java seems to have issues releasing memory mapped resources when calling close()
// http://stackoverflow.com/a/2973059/181087
// However, according to MSDN, the Dispose() method of the MemoryMappedFile class will "release all resources".
// https://msdn.microsoft.com/en-us/library/system.io.memorymappedfiles.memorymappedfile(v=vs.110).aspx
// Therefore, I am assuming removing the below line is the correct choice for .NET.
//AssumeTrue("test requires a jre that supports unmapping", MMapDirectory.UNMAP_SUPPORTED);
}
[Test]
public virtual void TestCloneSafety()
{
MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testCloneSafety"));
IndexOutput io = mmapDir.CreateOutput("bytes", NewIOContext(Random));
io.WriteVInt32(5);
io.Dispose();
IndexInput one = mmapDir.OpenInput("bytes", IOContext.DEFAULT);
IndexInput two = (IndexInput)one.Clone();
IndexInput three = (IndexInput)two.Clone(); // clone of clone
one.Dispose();
try
{
one.ReadVInt32();
Assert.Fail("Must throw ObjectDisposedException");
}
catch (Exception ignore) when (ignore.IsAlreadyClosedException())
{
// pass
}
try
{
two.ReadVInt32();
Assert.Fail("Must throw ObjectDisposedException");
}
catch (Exception ignore) when (ignore.IsAlreadyClosedException())
{
// pass
}
try
{
three.ReadVInt32();
Assert.Fail("Must throw ObjectDisposedException");
}
catch (Exception ignore) when (ignore.IsAlreadyClosedException())
{
// pass
}
two.Dispose();
three.Dispose();
// test double close of master:
one.Dispose();
mmapDir.Dispose();
}
[Test]
public virtual void TestCloneClose()
{
MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testCloneClose"));
IndexOutput io = mmapDir.CreateOutput("bytes", NewIOContext(Random));
io.WriteVInt32(5);
io.Dispose();
IndexInput one = mmapDir.OpenInput("bytes", IOContext.DEFAULT);
IndexInput two = (IndexInput)one.Clone();
IndexInput three = (IndexInput)two.Clone(); // clone of clone
two.Dispose();
Assert.AreEqual(5, one.ReadVInt32());
try
{
two.ReadVInt32();
Assert.Fail("Must throw ObjectDisposedException");
}
catch (Exception ignore) when (ignore.IsAlreadyClosedException())
{
// pass
}
Assert.AreEqual(5, three.ReadVInt32());
one.Dispose();
three.Dispose();
mmapDir.Dispose();
}
[Test]
public virtual void TestCloneSliceSafety()
{
MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testCloneSliceSafety"));
IndexOutput io = mmapDir.CreateOutput("bytes", NewIOContext(Random));
io.WriteInt32(1);
io.WriteInt32(2);
io.Dispose();
IndexInputSlicer slicer = mmapDir.CreateSlicer("bytes", NewIOContext(Random));
IndexInput one = slicer.OpenSlice("first int", 0, 4);
IndexInput two = slicer.OpenSlice("second int", 4, 4);
IndexInput three = (IndexInput)one.Clone(); // clone of clone
IndexInput four = (IndexInput)two.Clone(); // clone of clone
slicer.Dispose();
try
{
one.ReadInt32();
Assert.Fail("Must throw ObjectDisposedException");
}
catch (Exception ignore) when (ignore.IsAlreadyClosedException())
{
// pass
}
try
{
two.ReadInt32();
Assert.Fail("Must throw ObjectDisposedException");
}
catch (Exception ignore) when (ignore.IsAlreadyClosedException())
{
// pass
}
try
{
three.ReadInt32();
Assert.Fail("Must throw ObjectDisposedException");
}
catch (Exception ignore) when (ignore.IsAlreadyClosedException())
{
// pass
}
try
{
four.ReadInt32();
Assert.Fail("Must throw ObjectDisposedException");
}
catch (Exception ignore) when (ignore.IsAlreadyClosedException())
{
// pass
}
one.Dispose();
two.Dispose();
three.Dispose();
four.Dispose();
// test double-close of slicer:
slicer.Dispose();
mmapDir.Dispose();
}
[Test]
public virtual void TestCloneSliceClose()
{
MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testCloneSliceClose"));
IndexOutput io = mmapDir.CreateOutput("bytes", NewIOContext(Random));
io.WriteInt32(1);
io.WriteInt32(2);
io.Dispose();
IndexInputSlicer slicer = mmapDir.CreateSlicer("bytes", NewIOContext(Random));
IndexInput one = slicer.OpenSlice("first int", 0, 4);
IndexInput two = slicer.OpenSlice("second int", 4, 4);
one.Dispose();
try
{
one.ReadInt32();
Assert.Fail("Must throw ObjectDisposedException");
}
catch (Exception ignore) when (ignore.IsAlreadyClosedException())
{
// pass
}
Assert.AreEqual(2, two.ReadInt32());
// reopen a new slice "one":
one = slicer.OpenSlice("first int", 0, 4);
Assert.AreEqual(1, one.ReadInt32());
one.Dispose();
two.Dispose();
slicer.Dispose();
mmapDir.Dispose();
}
[Test]
public virtual void TestSeekZero()
{
for (int i = 0; i < 31; i++)
{
MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testSeekZero"), null, 1 << i);
IndexOutput io = mmapDir.CreateOutput("zeroBytes", NewIOContext(Random));
io.Dispose();
IndexInput ii = mmapDir.OpenInput("zeroBytes", NewIOContext(Random));
ii.Seek(0L);
ii.Dispose();
mmapDir.Dispose();
}
}
[Test]
public virtual void TestSeekSliceZero()
{
for (int i = 0; i < 31; i++)
{
MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testSeekSliceZero"), null, 1 << i);
IndexOutput io = mmapDir.CreateOutput("zeroBytes", NewIOContext(Random));
io.Dispose();
IndexInputSlicer slicer = mmapDir.CreateSlicer("zeroBytes", NewIOContext(Random));
IndexInput ii = slicer.OpenSlice("zero-length slice", 0, 0);
ii.Seek(0L);
ii.Dispose();
slicer.Dispose();
mmapDir.Dispose();
}
}
[Test]
public virtual void TestSeekEnd()
{
for (int i = 0; i < 17; i++)
{
MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testSeekEnd"), null, 1 << i);
IndexOutput io = mmapDir.CreateOutput("bytes", NewIOContext(Random));
var bytes = new byte[1 << i];
Random.NextBytes(bytes);
io.WriteBytes(bytes, bytes.Length);
io.Dispose();
IndexInput ii = mmapDir.OpenInput("bytes", NewIOContext(Random));
var actual = new byte[1 << i];
ii.ReadBytes(actual, 0, actual.Length);
Assert.AreEqual(new BytesRef(bytes), new BytesRef(actual));
ii.Seek(1 << i);
ii.Dispose();
mmapDir.Dispose();
}
}
[Test]
public virtual void TestSeekSliceEnd()
{
for (int i = 0; i < 17; i++)
{
MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testSeekSliceEnd"), null, 1 << i);
IndexOutput io = mmapDir.CreateOutput("bytes", NewIOContext(Random));
var bytes = new byte[1 << i];
Random.NextBytes(bytes);
io.WriteBytes(bytes, bytes.Length);
io.Dispose();
IndexInputSlicer slicer = mmapDir.CreateSlicer("bytes", NewIOContext(Random));
IndexInput ii = slicer.OpenSlice("full slice", 0, bytes.Length);
var actual = new byte[1 << i];
ii.ReadBytes(actual, 0, actual.Length);
Assert.AreEqual(new BytesRef(bytes), new BytesRef(actual));
ii.Seek(1 << i);
ii.Dispose();
slicer.Dispose();
mmapDir.Dispose();
}
}
[Test]
[Slow]
public virtual void TestSeeking()
{
for (int i = 0; i < 10; i++)
{
MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testSeeking"), null, 1 << i);
IndexOutput io = mmapDir.CreateOutput("bytes", NewIOContext(Random));
var bytes = new byte[1 << (i + 1)]; // make sure we switch buffers
Random.NextBytes(bytes);
io.WriteBytes(bytes, bytes.Length);
io.Dispose();
IndexInput ii = mmapDir.OpenInput("bytes", NewIOContext(Random));
var actual = new byte[1 << (i + 1)]; // first read all bytes
ii.ReadBytes(actual, 0, actual.Length);
Assert.AreEqual(new BytesRef(bytes), new BytesRef(actual));
for (int sliceStart = 0; sliceStart < bytes.Length; sliceStart++)
{
for (int sliceLength = 0; sliceLength < bytes.Length - sliceStart; sliceLength++)
{
var slice = new byte[sliceLength];
ii.Seek(sliceStart);
ii.ReadBytes(slice, 0, slice.Length);
Assert.AreEqual(new BytesRef(bytes, sliceStart, sliceLength), new BytesRef(slice));
}
}
ii.Dispose();
mmapDir.Dispose();
}
}
// note instead of seeking to offset and reading length, this opens slices at the
// the various offset+length and just does readBytes.
[Test]
[Slow]
public virtual void TestSlicedSeeking()
{
for (int i = 0; i < 10; i++)
{
MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testSlicedSeeking"), null, 1 << i);
IndexOutput io = mmapDir.CreateOutput("bytes", NewIOContext(Random));
var bytes = new byte[1 << (i + 1)]; // make sure we switch buffers
Random.NextBytes(bytes);
io.WriteBytes(bytes, bytes.Length);
io.Dispose();
IndexInput ii = mmapDir.OpenInput("bytes", NewIOContext(Random));
var actual = new byte[1 << (i + 1)]; // first read all bytes
ii.ReadBytes(actual, 0, actual.Length);
ii.Dispose();
Assert.AreEqual(new BytesRef(bytes), new BytesRef(actual));
IndexInputSlicer slicer = mmapDir.CreateSlicer("bytes", NewIOContext(Random));
for (int sliceStart = 0; sliceStart < bytes.Length; sliceStart++)
{
for (int sliceLength = 0; sliceLength < bytes.Length - sliceStart; sliceLength++)
{
var slice = new byte[sliceLength];
IndexInput input = slicer.OpenSlice("bytesSlice", sliceStart, slice.Length);
input.ReadBytes(slice, 0, slice.Length);
input.Dispose();
Assert.AreEqual(new BytesRef(bytes, sliceStart, sliceLength), new BytesRef(slice));
}
}
slicer.Dispose();
mmapDir.Dispose();
}
}
[Test]
public virtual void TestRandomChunkSizes()
{
int num = AtLeast(10);
for (int i = 0; i < num; i++)
{
AssertChunking(Random, TestUtil.NextInt32(Random, 20, 100));
}
}
private void AssertChunking(Random random, int chunkSize)
{
DirectoryInfo path = CreateTempDir("mmap" + chunkSize);
MMapDirectory mmapDir = new MMapDirectory(path, null, chunkSize);
// LUCENENET specific - unmap hack not needed
//// we will map a lot, try to turn on the unmap hack
//if (MMapDirectory.UNMAP_SUPPORTED)
//{
// mmapDir.UseUnmap = true;
//}
MockDirectoryWrapper dir = new MockDirectoryWrapper(random, mmapDir);
RandomIndexWriter writer = new RandomIndexWriter(random, dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).SetMergePolicy(NewLogMergePolicy()));
Document doc = new Document();
Field docid = NewStringField("docid", "0", Field.Store.YES);
Field junk = NewStringField("junk", "", Field.Store.YES);
doc.Add(docid);
doc.Add(junk);
int numDocs = 100;
for (int i = 0; i < numDocs; i++)
{
docid.SetStringValue("" + i);
junk.SetStringValue(TestUtil.RandomUnicodeString(random));
writer.AddDocument(doc);
}
IndexReader reader = writer.GetReader();
writer.Dispose();
int numAsserts = AtLeast(100);
for (int i = 0; i < numAsserts; i++)
{
int docID = random.Next(numDocs);
Assert.AreEqual("" + docID, reader.Document(docID).Get("docid"));
}
reader.Dispose();
dir.Dispose();
}
// LUCENENET: Regression test for GitHub #1090. A background thread
// extends a file on disk while the foreground thread repeatedly
// opens it with MMapDirectory.OpenInput. Before the fix, the
// file's length could grow between the caller capturing fc.Length
// and MemoryMappedFile.CreateFromFile performing its internal
// stat, causing ArgumentOutOfRangeException (paramName="capacity")
// with the message "The capacity may not be smaller than the
// file size."
// NonParallelizable: the retry-path assertion reads static counters on
// MMapDirectory, so any other test exercising MMapDirectory in parallel
// could skew the observed retry count.
[Test, LuceneNetSpecific, Slow, NonParallelizable]
public void TestOpenInputConcurrentFileExtension_Issue1090()
{
var dir = CreateTempDir("testOpenInputConcurrentFileExtension");
const string name = "data.bin";
string filePath = Path.Combine(dir.FullName, name);
// Seed with a small initial payload.
File.WriteAllBytes(filePath, new byte[64]);
using var mmapDir = new MMapDirectory(dir);
const long maxFileSize = 1L * 1024 * 1024; // 1 MiB cap
var stop = new ManualResetEventSlim(false);
Exception writerError = null;
var writer = new Thread(() =>
{
var chunk = new byte[64];
try
{
while (!stop.IsSet)
{
using var fs = new FileStream(filePath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
fs.Seek(0, SeekOrigin.End);
if (fs.Length < maxFileSize)
{
fs.Write(chunk, 0, chunk.Length);
}
else
{
// Keep the file bounded: truncate back and grow again.
fs.SetLength(64);
}
}
}
catch (Exception e)
{
writerError = e;
}
})
{ IsBackground = true, Name = "mmap-issue1090-extender" };
writer.Start();
// Snapshot counters so this test's assertion is not affected by
// any earlier test's activity on MMapDirectory.
long baselineRetries = Interlocked.Read(ref MMapDirectory.s_capacityRetryCount);
try
{
var sw = Stopwatch.StartNew();
int iterations = 0;
// Keep stretching the window until either the race fires or we
// hit a hard deadline. On most machines this takes < 1 second.
const int maxSeconds = 15;
while (sw.Elapsed < TimeSpan.FromSeconds(maxSeconds))
{
using (var _ = mmapDir.OpenInput(name, NewIOContext(Random)))
{
// Just open and dispose; the race occurs during construction.
}
iterations++;
if (Interlocked.Read(ref MMapDirectory.s_capacityRetryCount) > baselineRetries)
{
break; // race reproduced and handled by the retry loop
}
}
long retries = Interlocked.Read(ref MMapDirectory.s_capacityRetryCount) - baselineRetries;
int maxAttempts = Volatile.Read(ref MMapDirectory.s_maxCapacityAttemptsObserved);
// Surface what was observed for diagnostics when run with -v normal.
TestContext.Progress.WriteLine(
$"TestOpenInputConcurrentFileExtension: iterations={iterations}, retries={retries}, maxAttemptsObserved={maxAttempts}");
// The real check: the race must have fired and our retry loop
// must have swallowed it. Without the fix, the exception
// escapes OpenInput and the test fails with ArgumentOutOfRangeException
// (as seen in #1090). If the race never fires during this run
// (timing-dependent), mark the test inconclusive rather than
// silently passing — we haven't actually exercised the fix.
if (retries == 0)
{
NUnit.Framework.Assert.Inconclusive(
$"The concurrent-extension race was not reproduced within {maxSeconds}s " +
$"({iterations} OpenInput iterations). The fix was therefore not exercised on this run.");
}
}
finally
{
stop.Set();
writer.Join();
}
if (writerError != null)
{
throw new Exception("Writer thread failed", writerError);
}
}
[Test, LuceneNetSpecific]
public void TestDisposeIndexInput()
{
string name = "foobar";
var dir = CreateTempDir("testDisposeIndexInput");
string fileName = Path.Combine(dir.FullName, name);
// Create a zero byte file, and close it immediately
File.WriteAllText(fileName, string.Empty, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false) /* No BOM */);
MMapDirectory mmapDir = new MMapDirectory(dir);
using (var _ = mmapDir.OpenInput(name, NewIOContext(Random)))
{
} // Dispose
// Now it should be possible to delete the file. This is the condition we are testing for.
File.Delete(fileName);
}
}
}