forked from apache/lucenenet
/
NativeFSLockFactory.cs
753 lines (691 loc) · 28.4 KB
/
NativeFSLockFactory.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
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
using Lucene.Net.Support.IO;
using Lucene.Net.Util;
using System;
using System.IO;
using System.Collections.Generic;
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.
*/
/// <summary>
/// <para>Implements <see cref="LockFactory"/> using native OS file
/// locks. For NFS based access to an index, it's
/// recommended that you try <see cref="SimpleFSLockFactory"/>
/// first and work around the one limitation that a lock file
/// could be left when the runtime exits abnormally.</para>
///
/// <para>The primary benefit of <see cref="NativeFSLockFactory"/> is
/// that locks (not the lock file itsself) will be properly
/// removed (by the OS) if the runtime has an abnormal exit.</para>
///
/// <para>Note that, unlike <see cref="SimpleFSLockFactory"/>, the existence of
/// leftover lock files in the filesystem is fine because the OS
/// will free the locks held against these files even though the
/// files still remain. Lucene will never actively remove the lock
/// files, so although you see them, the index may not be locked.</para>
///
/// <para>Special care needs to be taken if you change the locking
/// implementation: First be certain that no writer is in fact
/// writing to the index otherwise you can easily corrupt
/// your index. Be sure to do the <see cref="LockFactory"/> change on all Lucene
/// instances and clean up all leftover lock files before starting
/// the new configuration for the first time. Different implementations
/// can not work together!</para>
///
/// <para>If you suspect that this or any other <see cref="LockFactory"/> is
/// not working properly in your environment, you can easily
/// test it by using <see cref="VerifyingLockFactory"/>,
/// <see cref="LockVerifyServer"/> and <see cref="LockStressTest"/>.</para>
/// </summary>
/// <seealso cref="LockFactory"/>
// LUCENENET specific - this class has been refactored significantly from its Java counterpart
// to take advantage of .NET FileShare locking in the Windows and Linux environments.
public class NativeFSLockFactory : FSLockFactory
{
private enum FSLockingStrategy
{
FileStreamLockViolation,
FileSharingViolation,
Fallback
}
// LUCENENET: This controls the locking strategy used for the current operating system and framework
private static FSLockingStrategy LockingStrategy
{
get
{
if (IS_FILESTREAM_LOCKING_PLATFORM && HRESULT_FILE_LOCK_VIOLATION.HasValue)
return FSLockingStrategy.FileStreamLockViolation;
else if (HRESULT_FILE_SHARE_VIOLATION.HasValue)
return FSLockingStrategy.FileSharingViolation;
else
// Fallback implementation for unknown platforms that don't rely on HResult
return FSLockingStrategy.Fallback;
}
}
// LUCNENENET NOTE: Lookup the HResult value we are interested in for the current OS
// by provoking the exception during initialization and caching its HResult value for later.
// We optimize for Windows because those HResult values are known and documented, but for
// other platforms, this is the only way we can reliably determine the HResult values
// we are interested in.
//
// Reference: https://stackoverflow.com/q/46380483
private static bool IS_FILESTREAM_LOCKING_PLATFORM = LoadIsFileStreamLockingPlatform();
private const int WIN_HRESULT_FILE_LOCK_VIOLATION = unchecked((int)0x80070021);
private const int WIN_HRESULT_FILE_SHARE_VIOLATION = unchecked((int)0x80070020);
internal static readonly int? HRESULT_FILE_LOCK_VIOLATION = LoadFileLockViolationHResult();
internal static readonly int? HRESULT_FILE_SHARE_VIOLATION = LoadFileShareViolationHResult();
private static bool LoadIsFileStreamLockingPlatform()
{
#if FEATURE_FILESTREAM_LOCK
return Constants.WINDOWS; // LUCENENET: See: https://github.com/dotnet/corefx/issues/5964
#else
return false;
#endif
}
private static int? LoadFileLockViolationHResult()
{
if (Constants.WINDOWS)
return WIN_HRESULT_FILE_LOCK_VIOLATION;
// Skip provoking the exception unless we know we will use the value
if (IS_FILESTREAM_LOCKING_PLATFORM)
{
return FileSupport.GetFileIOExceptionHResult(provokeException: (fileName) =>
{
using (var lockStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
{
lockStream.Lock(0, 1); // Create an exclusive lock
using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
// try to find out if the file is locked by writing a byte. Note that we need to flush the stream to find out.
stream.WriteByte(0);
stream.Flush(); // this *may* throw an IOException if the file is locked, but...
// ... closing the stream is the real test
}
}
});
}
return null;
}
private static int? LoadFileShareViolationHResult()
{
if (Constants.WINDOWS)
return WIN_HRESULT_FILE_SHARE_VIOLATION;
return FileSupport.GetFileIOExceptionHResult(provokeException: (fileName) =>
{
using (var lockStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 1, FileOptions.None))
// Try to get an exclusive lock on the file - this should throw an IOException with the current platform's HResult value for FileShare violation
using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Write, FileShare.None, 1, FileOptions.None))
{
}
});
}
/// <summary>
/// Create a <see cref="NativeFSLockFactory"/> instance, with <c>null</c> (unset)
/// lock directory. When you pass this factory to a <see cref="FSDirectory"/>
/// subclass, the lock directory is automatically set to the
/// directory itself. Be sure to create one instance for each directory
/// your create!
/// </summary>
public NativeFSLockFactory()
: this((DirectoryInfo)null)
{
}
/// <summary>
/// Create a <see cref="NativeFSLockFactory"/> instance, storing lock
/// files into the specified <paramref name="lockDirName"/>
/// </summary>
/// <param name="lockDirName"> where lock files are created. </param>
public NativeFSLockFactory(string lockDirName)
: this(new DirectoryInfo(lockDirName))
{
}
/// <summary>
/// Create a <see cref="NativeFSLockFactory"/> instance, storing lock
/// files into the specified <paramref name="lockDir"/>
/// </summary>
/// <param name="lockDir"> where lock files are created. </param>
public NativeFSLockFactory(DirectoryInfo lockDir)
{
SetLockDir(lockDir);
}
// LUCENENET: NativeFSLocks in Java are infact singletons; this is how we mimick that to track instances and make sure
// IW.Unlock and IW.IsLocked works correctly
internal static readonly Dictionary<string, Lock> _locks = new Dictionary<string, Lock>();
/// <summary>
/// Given a lock name, return the full prefixed path of the actual lock file.
/// </summary>
/// <param name="lockName"></param>
/// <returns></returns>
private string GetCanonicalPathOfLockFile(string lockName)
{
if (m_lockPrefix != null)
{
lockName = m_lockPrefix + "-" + lockName;
}
return new FileInfo(Path.Combine(m_lockDir.FullName, lockName)).GetCanonicalPath();
}
public override Lock MakeLock(string lockName)
{
var path = GetCanonicalPathOfLockFile(lockName);
Lock l;
lock (_locks)
if (!_locks.TryGetValue(path, out l))
_locks.Add(path, l = NewLock(path));
return l;
}
// Internal for testing
internal virtual Lock NewLock(string path)
{
switch (LockingStrategy)
{
case FSLockingStrategy.FileStreamLockViolation:
return new NativeFSLock(m_lockDir, path);
case FSLockingStrategy.FileSharingViolation:
return new SharingNativeFSLock(m_lockDir, path);
default:
// Fallback implementation for unknown platforms that don't rely on HResult
return new FallbackNativeFSLock(m_lockDir, path);
}
}
public override void ClearLock(string lockName)
{
var path = GetCanonicalPathOfLockFile(lockName);
// this is the reason why we can't use ConcurrentDictionary: we need the removal and disposal of the lock to be atomic
// otherwise it may clash with MakeLock making a lock and ClearLock disposing of it in another thread.
lock (_locks)
if (_locks.TryGetValue(path, out Lock l))
{
_locks.Remove(path);
l.Dispose();
}
}
}
// LUCENENET NOTE: We use this implementation as a fallback for platforms that we don't
// know the HResult numbers for lock and file sharing errors.
//
// Note that using NativeFSLock would be ideal for all platforms. However, there is a
// small chance that provoking lock/share exceptions will fail. In that rare case, we
// fallback to this substandard implementation.
//
// Reference: https://stackoverflow.com/q/46380483
internal class FallbackNativeFSLock : Lock
{
private FileStream channel;
private readonly string path;
private readonly DirectoryInfo lockDir;
public FallbackNativeFSLock(DirectoryInfo lockDir, string path)
{
this.lockDir = lockDir;
this.path = path;
}
public override bool Obtain()
{
lock (this)
{
FailureReason = null;
if (channel != null)
{
// Our instance is already locked:
return false;
}
if (!System.IO.Directory.Exists(lockDir.FullName))
{
try
{
System.IO.Directory.CreateDirectory(lockDir.FullName);
}
catch
{
throw new IOException("Cannot create directory: " + lockDir.FullName);
}
}
else if (File.Exists(lockDir.FullName))
{
throw new IOException("Found regular file where directory expected: " + lockDir.FullName);
}
var success = false;
try
{
channel = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
success = true;
}
catch (IOException e)
{
FailureReason = e;
}
// LUCENENET: UnauthorizedAccessException does not derive from IOException like in java
catch (UnauthorizedAccessException e)
{
// At least on OS X, we will sometimes get an
// intermittent "Permission Denied" Exception,
// which seems to simply mean "you failed to get
// the lock". But other IOExceptions could be
// "permanent" (eg, locking is not supported via
// the filesystem). So, we record the failure
// reason here; the timeout obtain (usually the
// one calling us) will use this as "root cause"
// if it fails to get the lock.
FailureReason = e;
}
finally
{
if (!success)
{
IOUtils.DisposeWhileHandlingException(channel);
channel = null;
}
}
return channel != null;
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
lock (this)
{
// whether or not we have created a file, we need to remove
// the lock instance from the dictionary that tracks them.
try
{
lock (NativeFSLockFactory._locks)
NativeFSLockFactory._locks.Remove(path);
}
finally
{
if (channel != null)
{
IOUtils.DisposeWhileHandlingException(channel);
channel = null;
bool tmpBool;
if (File.Exists(path))
{
File.Delete(path);
tmpBool = true;
}
else if (System.IO.Directory.Exists(path))
{
System.IO.Directory.Delete(path);
tmpBool = true;
}
else
{
tmpBool = false;
}
if (!tmpBool)
{
throw new LockReleaseFailedException("failed to delete " + path);
}
}
}
}
}
}
public override bool IsLocked()
{
lock (this)
{
// The test for is isLocked is not directly possible with native file locks:
// First a shortcut, if a lock reference in this instance is available
if (channel != null)
{
return true;
}
// Look if lock file is present; if not, there can definitely be no lock!
bool tmpBool;
if (System.IO.File.Exists(path))
tmpBool = true;
else
tmpBool = System.IO.Directory.Exists(path);
if (!tmpBool)
return false;
// Try to obtain and release (if was locked) the lock
try
{
bool obtained = Obtain();
if (obtained)
{
Dispose();
}
return !obtained;
}
catch (IOException)
{
return false;
}
}
}
public override string ToString()
{
return "{nameof(FallbackNativeFSLock)}@{path}";
}
}
// Locks the entire file. macOS requires this approach.
internal class SharingNativeFSLock : Lock
{
private FileStream channel;
private readonly string path;
private readonly DirectoryInfo lockDir;
public SharingNativeFSLock(DirectoryInfo lockDir, string path)
{
this.lockDir = lockDir;
this.path = path;
}
/// <summary>
/// Return true if the <see cref="IOException"/> is the result of a share violation
/// </summary>
private bool IsShareViolation(IOException e)
{
return e.HResult == NativeFSLockFactory.HRESULT_FILE_SHARE_VIOLATION;
}
private FileStream GetLockFileStream(FileMode mode)
{
if (!System.IO.Directory.Exists(lockDir.FullName))
{
try
{
System.IO.Directory.CreateDirectory(lockDir.FullName);
}
catch (Exception e)
{
// note that several processes might have been trying to create the same directory at the same time.
// if one succeeded, the directory will exist and the exception can be ignored. In all other cases we should report it.
if (!System.IO.Directory.Exists(lockDir.FullName))
throw new IOException("Cannot create directory: " + lockDir.FullName, e);
}
}
else if (File.Exists(lockDir.FullName))
{
throw new IOException("Found regular file where directory expected: " + lockDir.FullName);
}
return new FileStream(path, mode, FileAccess.Write, FileShare.None, 1, mode == FileMode.Open ? FileOptions.None : FileOptions.DeleteOnClose);
}
public override bool Obtain()
{
lock (this)
{
FailureReason = null;
if (channel != null)
{
// Our instance is already locked:
return false;
}
try
{
channel = GetLockFileStream(FileMode.OpenOrCreate);
}
catch (IOException e) when (IsShareViolation(e))
{
// no failure reason to be recorded, since this is the expected error if a lock exists
}
catch (IOException e)
{
FailureReason = e;
}
// LUCENENET: UnauthorizedAccessException does not derive from IOException like in java
catch (UnauthorizedAccessException e)
{
// At least on OS X, we will sometimes get an
// intermittent "Permission Denied" Exception,
// which seems to simply mean "you failed to get
// the lock". But other IOExceptions could be
// "permanent" (eg, locking is not supported via
// the filesystem). So, we record the failure
// reason here; the timeout obtain (usually the
// one calling us) will use this as "root cause"
// if it fails to get the lock.
FailureReason = e;
}
return channel != null;
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
lock (this)
{
// whether or not we have created a file, we need to remove
// the lock instance from the dictionary that tracks them.
try
{
lock (NativeFSLockFactory._locks)
NativeFSLockFactory._locks.Remove(path);
}
finally
{
if (channel != null)
{
try
{
IOUtils.DisposeWhileHandlingException(channel);
}
finally
{
channel = null;
}
}
}
}
}
}
public override bool IsLocked()
{
lock (this)
{
// First a shortcut, if a lock reference in this instance is available
if (channel != null)
{
return true;
}
try
{
using (var stream = GetLockFileStream(FileMode.Open))
{
}
return false;
}
catch (IOException e) when (IsShareViolation(e))
{
return true;
}
catch (FileNotFoundException)
{
// if the file doesn't exists, there can be no lock active
return false;
}
}
}
public override string ToString()
{
return $"{nameof(SharingNativeFSLock)}@{path}";
}
}
// Uses FileStream locking of file pages.
internal class NativeFSLock : Lock
{
private FileStream channel;
private readonly string path;
private readonly DirectoryInfo lockDir;
public NativeFSLock(DirectoryInfo lockDir, string path)
{
this.lockDir = lockDir;
this.path = path;
}
/// <summary>
/// Return true if the <see cref="IOException"/> is the result of a lock violation
/// </summary>
private bool IsLockViolation(IOException e)
{
return e.HResult == NativeFSLockFactory.HRESULT_FILE_LOCK_VIOLATION;
}
private FileStream GetLockFileStream(FileMode mode)
{
if (!System.IO.Directory.Exists(lockDir.FullName))
{
try
{
System.IO.Directory.CreateDirectory(lockDir.FullName);
}
catch (Exception e)
{
// note that several processes might have been trying to create the same directory at the same time.
// if one succeeded, the directory will exist and the exception can be ignored. In all other cases we should report it.
if (!System.IO.Directory.Exists(lockDir.FullName))
throw new IOException("Cannot create directory: " + lockDir.FullName, e);
}
}
else if (File.Exists(lockDir.FullName))
{
throw new IOException("Found regular file where directory expected: " + lockDir.FullName);
}
return new FileStream(path, mode, FileAccess.Write, FileShare.ReadWrite);
}
public override bool Obtain()
{
lock (this)
{
FailureReason = null;
if (channel != null)
{
// Our instance is already locked:
return false;
}
FileStream stream = null;
try
{
stream = GetLockFileStream(FileMode.OpenOrCreate);
}
catch (IOException e)
{
FailureReason = e;
}
// LUCENENET: UnauthorizedAccessException does not derive from IOException like in java
catch (UnauthorizedAccessException e)
{
// At least on OS X, we will sometimes get an
// intermittent "Permission Denied" Exception,
// which seems to simply mean "you failed to get
// the lock". But other IOExceptions could be
// "permanent" (eg, locking is not supported via
// the filesystem). So, we record the failure
// reason here; the timeout obtain (usually the
// one calling us) will use this as "root cause"
// if it fails to get the lock.
FailureReason = e;
}
if (stream != null)
{
try
{
stream.Lock(0, 1);
// only assign the channel if the lock succeeds
channel = stream;
}
catch (Exception e)
{
FailureReason = e;
IOUtils.DisposeWhileHandlingException(stream);
}
}
return channel != null;
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
lock (this)
{
// whether or not we have created a file, we need to remove
// the lock instance from the dictionary that tracks them.
try
{
lock (NativeFSLockFactory._locks)
NativeFSLockFactory._locks.Remove(path);
}
finally
{
if (channel != null)
{
try
{
IOUtils.DisposeWhileHandlingException(channel);
}
finally
{
channel = null;
}
// try to delete the file if we created it, but it's not an error if we can't.
try
{
File.Delete(path);
}
catch
{
}
}
}
}
}
}
public override bool IsLocked()
{
lock (this)
{
// First a shortcut, if a lock reference in this instance is available
if (channel != null)
{
return true;
}
try
{
using (var stream = GetLockFileStream(FileMode.Open))
{
// try to find out if the file is locked by writing a byte. Note that we need to flush the stream to find out.
stream.WriteByte(0);
stream.Flush(); // this *may* throw an IOException if the file is locked, but...
// ... closing the stream is the real test
}
return false;
}
catch (IOException e) when (IsLockViolation(e))
{
return true;
}
catch (FileNotFoundException)
{
// if the file doesn't exists, there can be no lock active
return false;
}
}
}
public override string ToString()
{
return $"{nameof(NativeFSLock)}@{path}";
}
}
#if !FEATURE_FILESTREAM_LOCK
internal static class FileStreamExtensions
{
// Dummy lock method to ensure we can compile even if the feature is unavailable
public static void Lock(this FileStream stream, long position, long length)
{
}
}
#endif
}