/// <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(Lucene.Net.Store.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); }
public void Quits_On_Success_Count() { var totalTries = 0; DelegateExtensions.RetryUntilSuccessOrMaxAttempts((currentTry) => { totalTries = currentTry; return(totalTries == 2 ? Attempt <string> .Succeed() : Attempt <string> .Fail()); }, 5, TimeSpan.FromMilliseconds(10)); Assert.AreEqual(2, totalTries); }
public void Only_Executes_Specific_Count() { const int maxTries = 5; var totalTries = 0; DelegateExtensions.RetryUntilSuccessOrMaxAttempts((currentTry) => { totalTries = currentTry; return(Attempt <IndexWriter> .Fail()); }, 5, TimeSpan.FromMilliseconds(10)); Assert.AreEqual(maxTries, totalTries); }
private bool TryWaitForDirectoryUnlock(Lucene.Net.Store.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 (Directory.Exists(TempPath) == false) { Directory.CreateDirectory(TempPath); } LuceneDirectory = DirectoryTracker.Current.GetDirectory(new DirectoryInfo(TempPath)); break; default: throw new ArgumentOutOfRangeException("localStorageType"); } }
public void Initialize(NameValueCollection config, string configuredPath, FSDirectory baseLuceneDirectory, Analyzer analyzer, LocalStorageType localStorageType) { //this is the default ILocalStorageDirectory localStorageDir = new CodeGenLocalStorageDirectory(); if (config["tempStorageDirectory"] != null) { //try to get the type var dirType = BuildManager.GetType(config["tempStorageDirectory"], false); if (dirType != null) { try { localStorageDir = (ILocalStorageDirectory)Activator.CreateInstance(dirType); } catch (Exception ex) { LogHelper.Error <LocalTempStorageIndexer>( string.Format("Could not create a temp storage location of type {0}, reverting to use the " + typeof(CodeGenLocalStorageDirectory).FullName, dirType), ex); } } } var tempPath = localStorageDir.GetLocalStorageDirectory(config, configuredPath); if (tempPath == null) { throw new InvalidOperationException("Could not resolve a temp location from the " + localStorageDir.GetType() + " specified"); } TempPath = tempPath.FullName; 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 (Directory.Exists(TempPath) == false) { Directory.CreateDirectory(TempPath); } LuceneDirectory = DirectoryTracker.Current.GetDirectory(new DirectoryInfo(TempPath)); break; default: throw new ArgumentOutOfRangeException("localStorageType"); } }