/// <summary> /// Attempts to create an index writer, it will retry on failure 5 times and on the last time will try to forcefully unlock the index files /// </summary> /// <param name="baseLuceneDirectory"></param> /// <param name="analyzer"></param> /// <returns></returns> private Attempt <IndexWriter> TryCreateWriterWithRetry(Directory baseLuceneDirectory, Analyzer analyzer) { var maxTries = 5; var result = DelegateExtensions.RetryUntilSuccessOrMaxAttempts((currentTry) => { //last try... if (currentTry == maxTries) { LogHelper.Info <LocalTempStorageIndexer>("Could not acquire index lock, attempting to force unlock it..."); //unlock it! IndexWriter.Unlock(baseLuceneDirectory); } var writerAttempt = TryCreateWriter(baseLuceneDirectory, analyzer); if (writerAttempt) { return(writerAttempt); } LogHelper.Info <LocalTempStorageIndexer>("Could not create writer on {0}, retrying ....", baseLuceneDirectory.ToString); return(Attempt <IndexWriter> .Fail()); }, 5, TimeSpan.FromSeconds(1)); return(result); }
private bool TryWaitForDirectoryUnlock(Directory dir) { var maxTries = 5; var result = DelegateExtensions.RetryUntilSuccessOrMaxAttempts((currentTry) => { //last try... if (currentTry == maxTries) { LogHelper.Info <LocalTempStorageIndexer>("Could not acquire directory lock, attempting to force unlock it..."); //unlock it! IndexWriter.Unlock(dir); } if (IndexWriter.IsLocked(dir) == false) { return(Attempt.Succeed(true)); } LogHelper.Info <LocalTempStorageIndexer>("Could not acquire directory lock for {0} writer, retrying ....", dir.ToString); return(Attempt <bool> .Fail()); }, 5, TimeSpan.FromSeconds(1)); return(result); }
public void Initialize(NameValueCollection config, string configuredPath, FSDirectory baseLuceneDirectory, Analyzer analyzer, LocalStorageType localStorageType) { var codegenPath = HttpRuntime.CodegenDir; TempPath = Path.Combine(codegenPath, configuredPath.TrimStart('~', '/').Replace("/", "\\")); switch (localStorageType) { case LocalStorageType.Sync: var success = InitializeLocalIndexAndDirectory(baseLuceneDirectory, analyzer, configuredPath); //create the custom lucene directory which will keep the main and temp FS's in sync LuceneDirectory = LocalTempStorageDirectoryTracker.Current.GetDirectory( new DirectoryInfo(TempPath), baseLuceneDirectory, //flag to disable the mirrored folder if not successful (int)success >= 100); //If the master index simply doesn't exist, we don't continue to try to open anything since there will // actually be nothing there. if (success == InitializeDirectoryFlags.SuccessNoIndexExists) { return; } //Try to open the reader, this will fail if the index is corrupt and we'll need to handle that var result = DelegateExtensions.RetryUntilSuccessOrMaxAttempts(i => { try { using (IndexReader.Open( LuceneDirectory, DeletePolicyTracker.Current.GetPolicy(LuceneDirectory), true)) { } return(Attempt.Succeed(true)); } catch (Exception ex) { LogHelper.WarnWithException <LocalTempStorageIndexer>( string.Format("Could not open an index reader, local temp storage index is empty or corrupt... retrying... {0}", configuredPath), ex); } return(Attempt.Fail(false)); }, 5, TimeSpan.FromSeconds(1)); if (result.Success == false) { LogHelper.Warn <LocalTempStorageIndexer>( string.Format("Could not open an index reader, local temp storage index is empty or corrupt... attempting to clear index files in local temp storage, will operate from main storage only {0}", configuredPath)); ClearFilesInPath(TempPath); //create the custom lucene directory which will keep the main and temp FS's in sync LuceneDirectory = LocalTempStorageDirectoryTracker.Current.GetDirectory( new DirectoryInfo(TempPath), baseLuceneDirectory, //Disable mirrored index, we're kind of screwed here only use master index true); } break; case LocalStorageType.LocalOnly: if (System.IO.Directory.Exists(TempPath) == false) { System.IO.Directory.CreateDirectory(TempPath); } LuceneDirectory = DirectoryTracker.Current.GetDirectory(new DirectoryInfo(TempPath)); break; default: throw new ArgumentOutOfRangeException("localStorageType"); } }