internal static void Finish(LuceneIndexingActivity activity) { lock (_waitingSetLock) { // activity is done in the ActivityQueue _waitingSet.Remove(activity); // terminate and release waiting threads if there is any. activity.Finish(); // register activity termination in the log. IndexingActivityHistory.Finish(activity.Id); // register activity termination if the activity was not skipped. if (activity.Executed) { TerminationHistory.FinishActivity(activity); } // execute all activities that are completely freed. foreach (var dependentItem in activity.WaitingForMe.ToArray()) { dependentItem.FinishWaiting(activity); if (dependentItem.WaitingFor.Count == 0) { Task.Run(() => Executor.Execute(dependentItem)); } } } }
public static IndexingActivityHistory GetHistory() { IndexingActivityHistory result; var list = new List <IndexingActivityHistoryItem>(_history.Length); lock (_lock) { for (int i = _position; i < _history.Length; i++) { if (_history[i] != null) { list.Add(_history[i]); } } for (int i = 0; i < _position; i++) { if (_history[i] != null) { list.Add(_history[i]); } } result = new IndexingActivityHistory() { State = IndexingActivityQueue.GetCurrentState(), Recent = list.ToArray() }; } return(result); }
internal static void AttachOrFinish(LuceneIndexingActivity activity) { lock (_waitingSetLock) { var sameActivity = _waitingSet.FirstOrDefault(a => a.Id == activity.Id); if (sameActivity != null) { sameActivity.Attach(activity); SnTrace.IndexQueue.Write("IAQ: A{0} attached to another in the waiting set.", activity.Id); return; } } activity.Finish(); // release blocked thread IndexingActivityHistory.Finish(activity.Id); SnTrace.IndexQueue.Write("IAQ: A{0} ignored: finished but not executed.", activity.Id); }
public static IndexingActivityHistory Reset() { IndexingActivityHistory result; lock (_lock) { for (int i = 0; i < _history.Length; i++) { _history[i] = null; } _position = 0; _unfinished = 0; result = new IndexingActivityHistory() { State = IndexingActivityQueue.GetCurrentState(), Recent = new IndexingActivityHistoryItem[0] }; } return(result); }
public static void Execute(LuceneIndexingActivity activity) { using (var op = SnTrace.Index.StartOperation("IAQ: A{0} EXECUTION.", activity.Id)) { IndexingActivityHistory.Start(activity.Id); try { using (new SenseNet.ContentRepository.Storage.Security.SystemAccount()) activity.ExecuteIndexingActivity(); } catch (Exception e) { SnTrace.Index.WriteError("IAQ: A{0} EXECUTION ERROR: {1}", activity.Id, e); IndexingActivityHistory.Error(activity.Id, e); } finally { DependencyManager.Finish(activity); } op.Successful = true; } }
private static void MakeDependencies(LuceneIndexingActivity newerActivity) { lock (_waitingSetLock) { foreach (var olderActivity in _waitingSet) { if (MustWait(newerActivity, olderActivity)) { newerActivity.WaitFor(olderActivity); SnTrace.IndexQueue.Write("IAQ: A{0} depends from A{1}", newerActivity.Id, olderActivity.Id); IndexingActivityHistory.Wait(newerActivity); } } _waitingSet.Add(newerActivity); if (newerActivity.WaitingFor.Count == 0) { Task.Run(() => Executor.Execute(newerActivity)); } } }
public static void EnqueueActivity(LuceneIndexingActivity activity) { SnTrace.IndexQueue.Write("IAQ: A{0} arrived{1}. {2}, {3}", activity.Id, activity.FromReceiver ? " from another computer" : "", activity.GetType().Name, activity.Path); IndexingActivityHistory.Arrive(activity); lock (_arrivalQueueLock) { if (activity.Id <= _lastQueued) { var sameActivity = _arrivalQueue.FirstOrDefault(a => a.Id == activity.Id); if (sameActivity != null) { sameActivity.Attach(activity); SnTrace.IndexQueue.Write("IAQ: A{0} attached to another one in the queue", activity.Id); return; } DependencyManager.AttachOrFinish(activity); return; } if (activity.Id > _lastQueued + 1) { var from = _lastQueued + 1; var to = activity.Id - 1; var expectedCount = to - from + 1; var loadedActivities = Retrier.Retry <IEnumerable <IIndexingActivity> >( 3, 100, () => LoadActivities(from, to), (r, i, e) => { if (i < 3) { SnTrace.IndexQueue.Write("IAQ: Loading attempt {0}", 4 - i); } if (e != null) { return(false); } return(r.Count() == expectedCount); }); foreach (LuceneIndexingActivity loadedActivity in loadedActivities) { IndexingActivityHistory.Arrive(loadedActivity); _arrivalQueue.Enqueue(loadedActivity); _lastQueued = loadedActivity.Id; SnTrace.IndexQueue.Write("IAQ: A{0} enqueued from db.", loadedActivity.Id); DependencyManager.ActivityEnqueued(); } } _arrivalQueue.Enqueue(activity); _lastQueued = activity.Id; SnTrace.IndexQueue.Write("IAQ: A{0} enqueued.", activity.Id); DependencyManager.ActivityEnqueued(); } }
/// <summary> /// MUST BE SYNCHRON /// GAPS MUST BE ORDERED /// </summary> internal static void Start(int lastDatabaseId, int lastExecutedId, int[] gaps, System.IO.TextWriter consoleOut) { if (consoleOut != null) { consoleOut.WriteLine("Executing unprocessed activities. {0}-{1} {2}", lastExecutedId, lastDatabaseId, CompletionState.GapsToString(gaps, 5, 3)); } SnLog.WriteInformation("Executing unprocessed activities.", EventId.RepositoryRuntime, properties: new Dictionary <string, object> { { "LastDatabaseId", lastDatabaseId }, { "LastExecutedId", lastExecutedId }, { "CountOfGaps", gaps.Length }, { "Gaps", String.Join(", ", gaps) } }); DependencyManager.Start(); var count = 0; if (gaps.Any()) { var loadedActivities = new IndexingActivityLoader(gaps, true); foreach (LuceneIndexingActivity loadedActivity in loadedActivities) { // wait and start processing loaded activities in the meantime WaitIfOverloaded(true); SnTrace.IndexQueue.Write("IAQ: Startup: A{0} enqueued from db.", loadedActivity.Id); IndexingActivityHistory.Arrive(loadedActivity); _arrivalQueue.Enqueue(loadedActivity); _lastQueued = loadedActivity.Id; count++; } } if (lastExecutedId < lastDatabaseId) { var loadedActivities = new IndexingActivityLoader(lastExecutedId + 1, lastDatabaseId, true); foreach (LuceneIndexingActivity loadedActivity in loadedActivities) { // wait and start processing loaded activities in the meantime WaitIfOverloaded(true); SnTrace.IndexQueue.Write("IAQ: Startup: A{0} enqueued from db.", loadedActivity.Id); IndexingActivityHistory.Arrive(loadedActivity); _arrivalQueue.Enqueue(loadedActivity); _lastQueued = loadedActivity.Id; count++; } } if (_lastQueued < lastExecutedId) { _lastQueued = lastExecutedId; } // ensure that the arrival activity queue is not empty at this pont. DependencyManager.ActivityEnqueued(); if (lastDatabaseId != 0 || lastExecutedId != 0 || gaps.Any()) { while (IsWorking()) { Thread.Sleep(200); } } // At this point we know for sure that the original gap is not there anymore. // In case there is a false gap (e.g. because there are missing activity ids // in the db) we have to remove these ids manually from the in-memory gap. if (gaps.Any()) { TerminationHistory.RemoveGaps(gaps); // Commit is necessary because otherwise the gap is removed only in memory, but // the index is not updated in the file system. LuceneManager.Commit(); } SnLog.WriteInformation($"Executing unprocessed activities ({count}) finished.", EventId.RepositoryLifecycle); }