Example #1
0
		public void Dispose()
		{
			disposerLock.EnterWriteLock();
			try
			{
				if (disposed)
					return;

				disposed = true;

				var exceptionAggregator = new ExceptionAggregator("Could not properly dispose TransactionalStorage");

				exceptionAggregator.Execute(() => current.Dispose());

				if (tableStorage != null)
					exceptionAggregator.Execute(() => tableStorage.Dispose());

				if (bufferPool != null)
					exceptionAggregator.Execute(() => bufferPool.Dispose());

				exceptionAggregator.ThrowIfNeeded();
			}
			finally
			{
				disposerLock.ExitWriteLock();
			}
		}
Example #2
0
		public void Dispose()
		{
			var aggregator = new ExceptionAggregator("Error during ResourceTimerManager disposal.");

			foreach (var timer in timers)
			{
				var t = timer;
				aggregator.Execute(() =>
				{
					if (t != null)
						t.Dispose();
				});
			}

			aggregator.ThrowIfNeeded();
		}
		public void Close()
		{
			var exceptionAggregator = new ExceptionAggregator(log, "Failed to close response");
			exceptionAggregator.Execute(OutputStream.Flush);
			exceptionAggregator.Execute(OutputStream.Dispose);
			if (StreamsToDispose!= null)
			{
				foreach (var stream in StreamsToDispose)
				{
					exceptionAggregator.Execute(stream.Flush);
					exceptionAggregator.Execute(stream.Dispose);
				}
			}
			exceptionAggregator.Execute(response.Close);

			exceptionAggregator.ThrowIfNeeded();
		}
Example #4
0
        public void Dispose(ExceptionAggregator exceptionAggregator)
        {
            foreach (var pendingTaskAndState in pendingTasks.Select(shouldDispose => shouldDispose.Value))
            {
                exceptionAggregator.Execute(() =>
                {
                    try
                    {
#if DEBUG
                        pendingTaskAndState.Task.Wait(3000);
#else
						pendingTaskAndState.Task.Wait();
#endif
                    }
                    catch (Exception)
                    {
                        // we explictly don't care about this during shutdown
                    }
                });
            }

            pendingTasks.Clear();
        }
Example #5
0
        public void Dispose()
        {
            var exceptionAggregator = new ExceptionAggregator(Log, string.Format("Could not properly close index storage for file system '{0}'", name));

            exceptionAggregator.Execute(() => { if (analyzer != null) analyzer.Close(); });
            exceptionAggregator.Execute(() => { if (currentIndexSearcherHolder != null)  currentIndexSearcherHolder.SetIndexSearcher(null); });
            exceptionAggregator.Execute(() => SafeDispose(crashMarker) );
            exceptionAggregator.Execute(() => SafeDispose(writer) );
            exceptionAggregator.Execute(() => SafeDispose(directory) );

            exceptionAggregator.ThrowIfNeeded();
        }
Example #6
0
		public void Dispose()
		{
			disposerLock.EnterWriteLock();
			try
			{
				TenantDatabaseModified.Occured -= TenantDatabaseRemoved;
				var exceptionAggregator = new ExceptionAggregator(logger, "Could not properly dispose of HttpServer");
				exceptionAggregator.Execute(() =>
				{
					if (databasesCleanupTimer != null)
						databasesCleanupTimer.Dispose();
				});
				exceptionAggregator.Execute(() =>
				{
					if (listener != null && listener.IsListening)
						listener.Stop();
				});
				disposed = true;

				exceptionAggregator.Execute(() =>
				{
					lock (ResourcesStoresCache)
					{
						foreach (var documentDatabase in ResourcesStoresCache)
						{
							var database = documentDatabase.Value;
							exceptionAggregator.Execute(database.Dispose);
						}
						ResourcesStoresCache.Clear();
					}
				});

				exceptionAggregator.Execute(currentConfiguration.Dispose);
				exceptionAggregator.Execute(currentDatabase.Dispose);
				exceptionAggregator.Execute(currentTenantId.Dispose);

				exceptionAggregator.ThrowIfNeeded();
			}
			finally
			{
				disposerLock.ExitWriteLock();
			}
		}
Example #7
0
		public void Dispose()
		{
			if (disposed)
				return;

			// give it 3 seconds to complete requests
			for (int i = 0; i < 30 && Interlocked.Read(ref metricsCounters.ConcurrentRequestsCount) > 0; i++)
			{
				Thread.Sleep(100);
			}

			AppDomain.CurrentDomain.ProcessExit -= ShouldDispose;
			AppDomain.CurrentDomain.DomainUnload -= ShouldDispose;

			disposed = true;

			var exceptionAggregator = new ExceptionAggregator(Log, "Could not properly dispose RavenFileSystem");

			if (synchronizationTask != null)
				exceptionAggregator.Execute(synchronizationTask.Dispose);

			if (storage != null)
				exceptionAggregator.Execute(storage.Dispose);

			if (search != null)
				exceptionAggregator.Execute(search.Dispose);

			if (sigGenerator != null)
				exceptionAggregator.Execute(sigGenerator.Dispose);

			if (BufferPool != null)
				exceptionAggregator.Execute(BufferPool.Dispose);

			if (metricsCounters != null)
				exceptionAggregator.Execute(metricsCounters.Dispose);

			if (Tasks != null)
				Tasks.Dispose(exceptionAggregator);

			exceptionAggregator.ThrowIfNeeded();
		}
Example #8
0
	    public void Dispose()
        {
            var tryEnterWriteLock = disposerLock.TryEnterWriteLock(TimeSpan.FromMinutes(2));
            try
            {
                if (tryEnterWriteLock == false)
                    log.Warn("After waiting for 2 minutes, could not acquire disposal lock, will force disposal anyway, pending transactions will all error");

                if (disposed)
                    return;

                var exceptionAggregator = new ExceptionAggregator(log, "Could not close database properly");
                disposed = true;
                exceptionAggregator.Execute(current.Dispose);
                if (documentCacher != null)
                    exceptionAggregator.Execute(documentCacher.Dispose);

                if (inFlightTransactionalState != null)
                    exceptionAggregator.Execute(inFlightTransactionalState.Dispose);

                exceptionAggregator.Execute(() =>
                    {
                        try
                        {
                            Api.JetTerm2(instance, TermGrbit.Complete);
                        }
                        catch (Exception e1)
                        {
                            log.ErrorException(
                                "Unexpected error occurred while terminating Esent Storage. Ignoring this error to allow to shutdown RavenDB instance.",
                                e1);

                            try
                            {
                                log.Warn(
                                    "Will now attempt to perform an abrupt shutdown, because asking nicely didn't work. You might need to run defrag on the database to recover potentially lost space (but no data will be lost).");
                                Api.JetTerm2(instance, TermGrbit.Abrupt);
                            }
                            catch (Exception e2)
                            {

                                log.FatalException(
                                    "Couldn't shut down the database server even when using abrupt, something is probably wrong and you'll need to restart the server process to access the database",
                                    e2);
                            }
                        }
                        finally
                        {
                            GC.SuppressFinalize(this);
                        }
                    });

                exceptionAggregator.ThrowIfNeeded();
            }
            catch (Exception e)
            {
                log.FatalException("Could not dispose of the transactional storage for " + path, e);
                throw;
            }
            finally
            {
                if (tryEnterWriteLock)
                    disposerLock.ExitWriteLock();
            }
        }
Example #9
0
		public void Dispose()
		{
			disposerLock.EnterWriteLock();
			try
			{
				TenantDatabaseModified.Occured -= TenantDatabaseRemoved;
				var exceptionAggregator = new ExceptionAggregator(logger, "Could not properly dispose of HttpServer");
                exceptionAggregator.Execute(() =>
                {
                    foreach (var databaseTransportState in databaseTransportStates)
                    {
                        databaseTransportState.Value.Dispose();
                    }
                });
				exceptionAggregator.Execute(() =>
				{
					if (serverTimer != null)
						serverTimer.Dispose();
				});
				exceptionAggregator.Execute(() =>
				{
					if (listener != null && listener.IsListening)
						listener.Close();
				});
				disposed = true;

				if (requestAuthorizer != null)
					exceptionAggregator.Execute(requestAuthorizer.Dispose);

				exceptionAggregator.Execute(() =>
				{
					using (ResourcesStoresCache.WithAllLocks())
					{
						// shut down all databases in parallel, avoid having to wait for each one
						Parallel.ForEach(ResourcesStoresCache.Values, dbTask =>
						{
							if (dbTask.IsCompleted == false)
							{
								dbTask.ContinueWith(task =>
								{
									if (task.Status != TaskStatus.RanToCompletion)
										return;

									try
									{
										task.Result.Dispose();
									}
									catch (Exception e)
									{
										logger.WarnException("Failure in deferred disposal of a database", e);
									}
								});
							}
							else if (dbTask.Status == TaskStatus.RanToCompletion)
							{
								exceptionAggregator.Execute(dbTask.Result.Dispose);
							}
							// there is no else, the db is probably faulted
						});
						ResourcesStoresCache.Clear();
					}
				});

				exceptionAggregator.Execute(currentConfiguration.Dispose);
				exceptionAggregator.Execute(currentDatabase.Dispose);
				exceptionAggregator.Execute(currentTenantId.Dispose);
				exceptionAggregator.Execute(bufferPool.Dispose);
				exceptionAggregator.ThrowIfNeeded();
			}
			finally
			{
				disposerLock.ExitWriteLock();
			}
		}
Example #10
0
		public void Dispose()
		{
			if (disposed)
				return;

			var onDisposing = Disposing;
			if(onDisposing!=null)
				onDisposing(this, EventArgs.Empty);

			var exceptionAggregator = new ExceptionAggregator(log, "Could not properly dispose of DatabaseDocument");

			exceptionAggregator.Execute(() =>
			{
				AppDomain.CurrentDomain.DomainUnload -= DomainUnloadOrProcessExit;
				AppDomain.CurrentDomain.ProcessExit -= DomainUnloadOrProcessExit;
				disposed = true;
				workContext.StopWork();
			});
			
			exceptionAggregator.Execute(() =>
			{
				foreach (var value in ExtensionsState.Values.OfType<IDisposable>())
				{
					exceptionAggregator.Execute(value.Dispose);
				}
			});
			
			exceptionAggregator.Execute(() =>
			{
				foreach (var shouldDispose in toDispose)
				{
					exceptionAggregator.Execute(shouldDispose.Dispose);
				}
			});

			exceptionAggregator.Execute(() =>
			{
				if (indexingBackgroundTask != null)
					indexingBackgroundTask.Wait();
			});
			exceptionAggregator.Execute(() =>
			{
				if (reducingBackgroundTask != null)
					reducingBackgroundTask.Wait();
			});

			exceptionAggregator.Execute(() =>
			{
				var disposable = backgroundTaskScheduler as IDisposable;
				if (disposable != null)
					disposable.Dispose();
			});

			if (TransactionalStorage != null)
				exceptionAggregator.Execute(TransactionalStorage.Dispose);
			if (IndexStorage != null)
				exceptionAggregator.Execute(IndexStorage.Dispose);

			if (Configuration != null)
				exceptionAggregator.Execute(Configuration.Dispose);

			exceptionAggregator.Execute(disableAllTriggers.Dispose);

			if (workContext != null)
				exceptionAggregator.Execute(workContext.Dispose);



			exceptionAggregator.ThrowIfNeeded();
		}
Example #11
0
		public void Dispose()
		{
			disposerLock.EnterWriteLock();
			try
			{
				if (disposed)
					return;

				var exceptionAggregator = new ExceptionAggregator(log, "Could not close database properly");
				disposed = true;
				exceptionAggregator.Execute(current.Dispose);
				if (documentCacher != null)
					exceptionAggregator.Execute(documentCacher.Dispose);
				exceptionAggregator.Execute(() =>
					{
						Api.JetTerm2(instance, TermGrbit.Complete);
						GC.SuppressFinalize(this);
					});

				exceptionAggregator.ThrowIfNeeded();
			}
			catch (Exception e)
			{
				log.FatalException("Could not dispose of the transactional storage for " + path, e);
				throw;
			}
			finally
			{
				disposerLock.ExitWriteLock();
			}
		}
Example #12
0
        public void Dispose()
        {
            var toDispose = new[]
                            {
                                documents, 
                                queue, 
                                lists, 
                                directories, 
                                files, 
                                indexesStats, 
                                indexesStatsReduce, 
                                indexesEtags, 
                                scheduledReductions, 
                                mappedResults, 
                                reducedResults, 
                                tasks, 
                                identity, 
                                details, 
                                reduceKeysCounts, 
                                reduceKeysStatus, 
                                indexedDocumentsReferences
                            };

            var aggregator = new ExceptionAggregator("DocumentStorageActions disposal error.");

            foreach (var dispose in toDispose)
            {
                if (dispose == null)
                    continue;

                aggregator.Execute(() => dispose.Dispose());
            }

            aggregator.Execute(() =>
            {
            if (Equals(dbid, JET_DBID.Nil) == false && session != null)
                Api.JetCloseDatabase(session.JetSesid, dbid, CloseDatabaseGrbit.None);
            });

            aggregator.Execute(() =>
            {
            if (sessionAndTransactionDisposer != null)
                sessionAndTransactionDisposer();
            });

            aggregator.ThrowIfNeeded();
        }
Example #13
0
        public void Dispose()
        {
            var ea = new ExceptionAggregator("Cannot dispose server");
            ea.Execute(owinEmbeddedHost.Dispose);
            if (server != null)
                ea.Execute(server.Dispose);

            ea.ThrowIfNeeded();
        }
Example #14
0
        public void Dispose()
        {
            var tryEnterWriteLock = disposerLock.TryEnterWriteLock(TimeSpan.FromMinutes(2));
            try
            {
                if (tryEnterWriteLock == false)
                    log.Warn( "After waiting for 2 minutes, could not aqcuire disposal lock, will force disposal anyway, pending transactions will all error");

                if (disposed)
                    return;

                var exceptionAggregator = new ExceptionAggregator(log, "Could not close database properly");
                disposed = true;
                exceptionAggregator.Execute(current.Dispose);
                if (documentCacher != null)
                    exceptionAggregator.Execute(documentCacher.Dispose);

				if(inFlightTransactionalState != null)
					exceptionAggregator.Execute(inFlightTransactionalState.Dispose);

                exceptionAggregator.Execute(() =>
                    {
                        Api.JetTerm2(instance, TermGrbit.Complete);
                        GC.SuppressFinalize(this);
                    });

                exceptionAggregator.ThrowIfNeeded();
            }
            catch (Exception e)
            {
                log.FatalException("Could not dispose of the transactional storage for " + path, e);
                throw;
            }
            finally
            {
                if (tryEnterWriteLock)
                    disposerLock.ExitWriteLock();
            }
        }
Example #15
0
		protected override void Dispose()
		{
			var exceptionAggregator = new ExceptionAggregator(Log, "Could not dispose of IndexingExecuter");
			foreach (var pendingTask in pendingTasks)
			{
				exceptionAggregator.Execute(pendingTask.Wait);
			}
			pendingTasks.Clear();

			exceptionAggregator.Execute(prefetchingBehavior.Dispose);

			if (indexingCompletedEvent != null)
				exceptionAggregator.Execute(indexingCompletedEvent.Dispose);
			if (indexingSemaphore != null)
				exceptionAggregator.Execute(indexingSemaphore.Dispose);
			exceptionAggregator.ThrowIfNeeded();

			indexingCompletedEvent = null;
			indexingSemaphore = null;
		}
Example #16
0
		public void Dispose()
		{
			if (disposed)
				return;
			var onDisposing = Disposing;
			if (onDisposing != null)
			{
				try
				{
					onDisposing(this, EventArgs.Empty);
				}
				catch (Exception e)
				{
					log.WarnException("Error when notifying about db disposal, ignoring error and continuing with disposal", e);
				}
			}

			var exceptionAggregator = new ExceptionAggregator(log, "Could not properly dispose of DatabaseDocument");

			exceptionAggregator.Execute(() =>
			{
				AppDomain.CurrentDomain.DomainUnload -= DomainUnloadOrProcessExit;
				AppDomain.CurrentDomain.ProcessExit -= DomainUnloadOrProcessExit;
				disposed = true;

				if (workContext != null)
					workContext.StopWorkRude();
			});

			if (validateLicense != null)
				exceptionAggregator.Execute(validateLicense.Dispose);

			exceptionAggregator.Execute(() =>
			{
				if (ExtensionsState == null)
					return;

				foreach (var value in ExtensionsState.Values.OfType<IDisposable>())
				{
					exceptionAggregator.Execute(value.Dispose);
				}
			});

			exceptionAggregator.Execute(() =>
			{
				if (toDispose == null)
					return;
				foreach (var shouldDispose in toDispose)
				{
					exceptionAggregator.Execute(shouldDispose.Dispose);
				}
			});


			exceptionAggregator.Execute(() =>
			{
				foreach (var shouldDispose in pendingTasks)
				{
					exceptionAggregator.Execute(shouldDispose.Value.Task.Wait);
				}
				pendingTasks.Clear();
			});

			exceptionAggregator.Execute(() =>
			{
				if (indexingBackgroundTask != null)
					indexingBackgroundTask.Wait();
			});
			exceptionAggregator.Execute(() =>
			{
				if (reducingBackgroundTask != null)
					reducingBackgroundTask.Wait();
			});

			exceptionAggregator.Execute(() =>
			{
				var disposable = backgroundTaskScheduler as IDisposable;
				if (disposable != null)
					disposable.Dispose();
			});

			if (TransactionalStorage != null)
				exceptionAggregator.Execute(TransactionalStorage.Dispose);
			if (IndexStorage != null)
				exceptionAggregator.Execute(IndexStorage.Dispose);

			if (Configuration != null)
				exceptionAggregator.Execute(Configuration.Dispose);

			exceptionAggregator.Execute(disableAllTriggers.Dispose);

			if (workContext != null)
				exceptionAggregator.Execute(workContext.Dispose);

			exceptionAggregator.ThrowIfNeeded();
		}
Example #17
0
		public IStorageActionsAccessor CreateAccessor()
		{
		    var snapshotReference = new Reference<SnapshotReader> { Value = tableStorage.CreateSnapshot() };
			var writeBatchReference = new Reference<WriteBatch> { Value = new WriteBatch() };
			
			var accessor = new StorageActionsAccessor(uuidGenerator, _documentCodecs,
                    documentCacher, writeBatchReference, snapshotReference, tableStorage, this, bufferPool);
			accessor.OnDispose += () =>
			{
				var exceptionAggregator = new ExceptionAggregator("Could not properly dispose StorageActionsAccessor");

				exceptionAggregator.Execute(() => snapshotReference.Value.Dispose());
				exceptionAggregator.Execute(() => writeBatchReference.Value.Dispose());

				exceptionAggregator.ThrowIfNeeded();
			};

			return accessor;
		}
		protected override void Dispose()
		{
			var exceptionAggregator = new ExceptionAggregator(Log, "Could not dispose of IndexingExecuter");

			exceptionAggregator.Execute(prefetchingBehavior.Dispose);

			exceptionAggregator.ThrowIfNeeded();
		}
Example #19
0
		public void Dispose()
		{
			FlushMapIndexes();
			FlushReduceIndexes();

			var exceptionAggregator = new ExceptionAggregator(log, "Could not properly close index storage");

			exceptionAggregator.Execute(() => Parallel.ForEach(indexes.Values, index => exceptionAggregator.Execute(index.Dispose)));

			exceptionAggregator.Execute(() => dummyAnalyzer.Close());

			exceptionAggregator.Execute(() =>
			{
				if (crashMarker != null)
					crashMarker.Dispose();
			});

			exceptionAggregator.ThrowIfNeeded();
		}
Example #20
0
		public void Dispose()
		{
			if (disposed)
				return;

			Log.Debug("Start shutdown the following database: {0}", Name ?? Constants.SystemDatabase);

			EventHandler onDisposing = Disposing;
			if (onDisposing != null)
			{
				try
				{
					onDisposing(this, EventArgs.Empty);
				}
				catch (Exception e)
				{
					Log.WarnException("Error when notifying about db disposal, ignoring error and continuing with disposal", e);
				}
			}

			var exceptionAggregator = new ExceptionAggregator(Log, "Could not properly dispose of DatabaseDocument");

			exceptionAggregator.Execute(() =>
							{
								if (prefetcher != null)
									prefetcher.Dispose();
							});

			exceptionAggregator.Execute(() =>
							{
								initializer.UnsubscribeToDomainUnloadOrProcessExit();
								disposed = true;

								if (workContext != null)
									workContext.StopWorkRude();
							});

			if (initializer != null)
			{
				exceptionAggregator.Execute(initializer.Dispose);
			}

			exceptionAggregator.Execute(() =>
			{
				if (ExtensionsState == null)
					return;

				foreach (IDisposable value in ExtensionsState.Values.OfType<IDisposable>())
					exceptionAggregator.Execute(value.Dispose);
			});

			exceptionAggregator.Execute(() =>
			{
				if (toDispose == null)
					return;

				foreach (IDisposable shouldDispose in toDispose)
					exceptionAggregator.Execute(shouldDispose.Dispose);
			});

			exceptionAggregator.Execute(() =>
			{
				if (Tasks != null)
					Tasks.Dispose(exceptionAggregator);
			});

			exceptionAggregator.Execute(() =>
			{
				if (indexingBackgroundTask != null)
					indexingBackgroundTask.Wait();
			});
			exceptionAggregator.Execute(() =>
			{
				if (reducingBackgroundTask != null)
					reducingBackgroundTask.Wait();
			});

			exceptionAggregator.Execute(() =>
			{
				var disposable = backgroundTaskScheduler as IDisposable;
				if (disposable != null)
					disposable.Dispose();
			});


			if (IndexStorage != null)
				exceptionAggregator.Execute(IndexStorage.Dispose);
			
			if (TransactionalStorage != null)
				exceptionAggregator.Execute(TransactionalStorage.Dispose);
			
			if (Configuration != null)
				exceptionAggregator.Execute(Configuration.Dispose);

			exceptionAggregator.Execute(disableAllTriggers.Dispose);

			if (workContext != null)
				exceptionAggregator.Execute(workContext.Dispose);

			try
			{
				exceptionAggregator.ThrowIfNeeded();
			}
			finally
			{
				var onDisposingEnded = DisposingEnded;
				if (onDisposingEnded != null)
				{
					try
					{
						onDisposingEnded(this, EventArgs.Empty);
					}
					catch (Exception e)
					{
						Log.WarnException("Error when notifying about db disposal ending, ignoring error and continuing with disposal", e);
					}
				}
			}

			Log.Debug("Finished shutdown the following database: {0}", Name ?? Constants.SystemDatabase);
		}
Example #21
0
		public void Dispose()
		{

			cancellationTokenSource.Cancel();
			// we give it a second to actually complete all requests, but then we go ahead 
			// and dispose anyway
			for (int i = 0; i < 10 && Interlocked.Read(ref concurrentRequests) > 0; i++)
			{
				Thread.Sleep(100);
			}

			var exceptionAggregator = new ExceptionAggregator(Logger, "Could not properly dispose of HttpServer");

			exceptionAggregator.Execute(() =>
			{
				if (serverTimer != null)
					serverTimer.Dispose();
			});
		}
Example #22
0
		public void Dispose()
		{
			if (disposed)
				return;

			AppDomain.CurrentDomain.ProcessExit -= ShouldDispose;
			AppDomain.CurrentDomain.DomainUnload -= ShouldDispose;

			disposed = true;

			var exceptionAggregator = new ExceptionAggregator(Log, "Could not properly dispose RavenFileSystem");

			if (synchronizationTask != null)
				exceptionAggregator.Execute(synchronizationTask.Dispose);

			if (storage != null)
				exceptionAggregator.Execute(storage.Dispose);

			if (search != null)
				exceptionAggregator.Execute(search.Dispose);

			if (sigGenerator != null)
				exceptionAggregator.Execute(sigGenerator.Dispose);

			if (BufferPool != null)
				exceptionAggregator.Execute(BufferPool.Dispose);

			if (metricsCounters != null)
				exceptionAggregator.Execute(metricsCounters.Dispose);

			if (Tasks != null)
				Tasks.Dispose(exceptionAggregator);

			exceptionAggregator.ThrowIfNeeded();
		}