Example #1
0
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            AsyncHelpers.RunSync(() => Task.WhenAll(_activeWriteTasks));
            _isDisposed.Raise();
        }
Example #2
0
        public void Dispose()
        {
            if (!_disposed.Raise())
            {
                return;
            }

            GC.SuppressFinalize(this);
            _options.IoMetrics.FileClosed(FileName.FullPath);

            List <Exception> exceptions = null;

            TryExecute(() =>
            {
                _readHandle.Dispose();
                if (_readHandle.FailCode != PalFlags.FailCodes.Success)
                {
                    PalHelper.ThrowLastError(_readHandle.FailCode, _readHandle.ErrorNo,
                                             $"Attempted to close 'read journal handle' - Path: {FileName.FullPath}");
                }
            });

            TryExecute(() =>
            {
                _writeHandle.Dispose();
                if (_writeHandle.FailCode != PalFlags.FailCodes.Success)
                {
                    PalHelper.ThrowLastError(_writeHandle.FailCode, _writeHandle.ErrorNo,
                                             $"Attempted to close 'write journal handle' - Path: {FileName.FullPath}");
                }
            });

            if (exceptions != null)
            {
                throw new AggregateException("Failed to dispose journal writer", exceptions);
            }

            if (DeleteOnClose)
            {
                _options.TryStoreJournalForReuse(FileName);
            }

            void TryExecute(Action a)
            {
                try
                {
                    a();
                }
                catch (Exception e)
                {
                    if (exceptions == null)
                    {
                        exceptions = new List <Exception>();
                    }
                    exceptions.Add(e);
                }
            }
        }
Example #3
0
        public void Dispose()
        {
            if (_disposed.Raise() == false)
            {
                return;
            }

            _writer.Dispose();
            GlobalCache.Free(_cacheItem);
        }
        public override void Dispose()
        {
            if (_disposed.Raise() == false)
            {
                return;
            }

            _writer.Dispose();
            base.Dispose();
        }
 public void Dispose()
 {
     _isDisposed.Raise();
     _cts.Cancel();
     _sendQueue.Enqueue(new ChangeValue
     {
         AllowSkip   = false,
         ValueToSend = null
     });
     _cts.Dispose();
 }
Example #6
0
        public void Initialize(DatabaseRecord record)
        {
            if (_isInitialized) //precaution -> probably not necessary, but still...
            {
                return;
            }

            ConflictSolverConfig = record.ConflictSolverConfig;
            ConflictResolver     = new ResolveConflictOnReplicationConfigurationChange(this, _log);
            ConflictResolver.RunConflictResolversOnce();
            _isInitialized.Raise();
        }
Example #7
0
        /// <summary>
        /// Mark the passage of time and decay the current rate accordingly.
        /// </summary>
        public void Tick()
        {
            var count       = _uncounted.GetAndSet(0);
            var instantRate = count / _interval;

            if (_initialized.Raise())
            {
                _rate.Set(instantRate);
            }
            else
            {
                _rate += _alpha * (instantRate - _rate);
            }
        }
Example #8
0
        public void Tick()
        {
            var count = Interlocked.Exchange(ref _uncounted, 0);

            var instantRate = count / _interval;

            if (_initialized.Raise())
            {
                Volatile.Write(ref _rate, instantRate);
            }
            else
            {
                double doubleRate = Volatile.Read(ref _rate);
                Volatile.Write(ref _rate, doubleRate + _alpha * (instantRate - doubleRate));
            }
        }
Example #9
0
        public void Dispose()
        {
            // There are multiple invocations of dispose, this happens sometimes during tests, causing failures.
            if (!_disposed.Raise())
            {
                return;
            }

            var timeout = _parent._server.Engine.TcpConnectionTimeout;

            if (_log.IsInfoEnabled)
            {
                _log.Info($"Disposing OutgoingReplicationHandler ({FromToString}) [Timeout:{timeout}]");
            }

            _database.Changes.OnDocumentChange -= OnDocumentChange;
            _database.Changes.OnCounterChange  -= OnCounterChange;

            _cts.Cancel();

            DisposeTcpClient();

            _connectionDisposed.Set();

            if (_longRunningSendingWork != null && _longRunningSendingWork != PoolOfThreads.LongRunningWork.Current)
            {
                while (_longRunningSendingWork.Join((int)timeout.TotalMilliseconds) == false)
                {
                    if (_log.IsInfoEnabled)
                    {
                        _log.Info($"Waited {timeout} for timeout to occur, but still this thread is keep on running. Will wait another {timeout} ");
                    }
                    DisposeTcpClient();
                }
            }

            try
            {
                _cts.Dispose();
            }
            catch (ObjectDisposedException)
            {
                //was already disposed? we don't care, we are disposing
            }
        }
Example #10
0
        public void Dispose()
        {
            // There are multiple invocations of dispose, this happens sometimes during tests, causing failures.
            if (!_disposed.Raise())
            {
                return;
            }
            if (_log.IsInfoEnabled)
            {
                _log.Info($"Disposing OutgoingReplicationHandler ({FromToString})");
            }

            _database.Changes.OnDocumentChange -= OnDocumentChange;

            _cts.Cancel();

            try
            {
                _tcpClient?.Dispose();
            }
            catch (Exception)
            {
                // nothing we can do here
            }

            _connectionDisposed.Set();

            if (_sendingThread != Thread.CurrentThread)
            {
                _sendingThread?.Join();
            }

            try
            {
                _cts.Dispose();
            }
            catch (ObjectDisposedException)
            {
                //was already disposed? we don't care, we are disposing
            }
        }
Example #11
0
        public void Dispose()
        {
            if (!_disposed.Raise())
            {
                return;
            }

            GC.SuppressFinalize(this);
            _options.IoMetrics.FileClosed(_filename.FullPath);
            _readHandle?.Dispose();
            _readHandle = null;
            _handle?.Dispose();
            _handle = null;
            if (_nativeOverlapped != null)
            {
                NativeMemory.Free((byte *)_nativeOverlapped, sizeof(NativeOverlapped));
                _nativeOverlapped = null;
            }

            if (DeleteOnClose)
            {
                _options.TryStoreJournalForReuse(_filename);
            }
        }
Example #12
0
        public void RingBuffer_WhenEmptyAndSimultaneouslyTryPushAndTryAcquired_ShouldAcquiredLegal()
        {
            var rb = new SingleConsumerRingBuffer <TestItem>(4);

            var acquiredEnd  = new SingleUseFlag();
            var finished     = new Barrier(2);
            var cancellation = new CancellationTokenSource();
            var timeout      = TimeSpan.FromSeconds(1);

            var exceptions = new List <Exception>();
            var pushTask   = Task.Run(() =>
            {
                try
                {
                    while (acquiredEnd.IsRaised() == false)
                    {
                        if (finished.SignalAndWait(timeout, cancellation.Token) == false)
                        {
                            break;
                        }

                        var testItem = new TestItem
                        {
                            TestProperty = finished.CurrentPhaseNumber
                        };
                        rb.TryPush(ref testItem);
                    }
                }
                catch (OperationCanceledException)
                {
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                }
            }, cancellation.Token);

            try
            {
                for (var i = 0; i < 1000; i++)
                {
                    if (finished.SignalAndWait(timeout) == false)
                    {
                        break;
                    }

                    for (int j = 0; j < 100; j++)
                    {
                        if (rb.TryAcquireSingle(out var ringItem))
                        {
                            Assert.NotNull(ringItem.Item);
                            Assert.True(ringItem.IsReady == 1, $"Acquired {ringItem.GetType()} should be set to ready");
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                exceptions.Add(e);
            }
            finally
            {
                cancellation.Cancel();
                acquiredEnd.Raise();
                try
                {
                    pushTask.Wait(timeout);
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                }
                cancellation.Dispose();

                if (exceptions.Any())
                {
                    throw new AggregateException(exceptions);
                }
            }
        }
Example #13
0
 public void Dispose()
 {
     _disposed.Raise();
     GC.SuppressFinalize(this);
 }
 public void MarkForDisposal()
 {
     ShouldDispose.Raise();
 }