Exemplo n.º 1
0
        public void Cache_should_not_reply_with_expired_but_not_yet_swept_out_entries()
        {
            var localClock = new AtomicReference<long>(0);
            var cache = new SimpleDnsCacheTestDouble(localClock);
            var cacheEntry = Dns.Resolved.Create("test.local", System.Net.Dns.GetHostEntry("127.0.0.1").AddressList);
            cache.Put(cacheEntry, 5000);

            cache.Cached("test.local").ShouldBe(cacheEntry);
            localClock.CompareAndSet(0, 4999);
            cache.Cached("test.local").ShouldBe(cacheEntry);
            localClock.CompareAndSet(4999, 5000);
            cache.Cached("test.local").ShouldBe(null);

        }
Exemplo n.º 2
0
        public void CompareAndSet_when_equal_returns_true()
        {
            var expected = new object();
            var obj = new AtomicReference<object>(expected);

            Assert.IsTrue(obj.CompareAndSet(expected, new object()));
        }
Exemplo n.º 3
0
        public void CompareAndSet_when_not_equal_does_not_set_value()
        {
            var initial = new object();
            var obj = new AtomicReference<object>(initial);

            obj.CompareAndSet(new object(), new object());

            Assert.AreEqual(initial, obj.Value);
        }
Exemplo n.º 4
0
 public override void ExceptionCaught(IChannelHandlerContext ctx, Exception cause)
 {
     if (_exception.CompareAndSet(null, cause))
     {
         _output.WriteLine(cause.ToString());
         _donePromise.TrySetException(new InvalidOperationException("exceptionCaught: " + ctx.Channel, cause));
         ctx.CloseAsync();
     }
 }
Exemplo n.º 5
0
 public override void ExceptionCaught(IChannelHandlerContext ctx, Exception cause)
 {
     if (_exception.CompareAndSet(null, cause))
     {
         _donePromise.TrySetException(new InvalidOperationException("exceptionCaught: " + ctx.Channel, cause));
         ctx.CloseAsync();
     }
     _globalException.CompareAndSet(null, cause);
 }
Exemplo n.º 6
0
        internal CollapsedRequest <RequestResponseType, RequestArgumentType> GetOrAddNullArg(CollapsedRequest <RequestResponseType, RequestArgumentType> collapsedRequest)
        {
            if (_nullArg.CompareAndSet(null, collapsedRequest))
            {
                return(collapsedRequest);
            }

            return(_nullArg.Value);
        }
Exemplo n.º 7
0
        public void Lock()
        {
            var current = Thread.CurrentThread;

            if (Holder.Value == current)
            {
                HoldCounter++;
            }
            else
            {
                Waiters.Enqueue(current);
                Thread first;
                while ((Waiters.TryPeek(out first) && first != current) || !Holder.CompareAndSet(null, current))
                {
                    ResetEvent.WaitOne();
                }
                TakeLock();
            }
        }
Exemplo n.º 8
0
        public void Unsubscribe()
        {
            var s = subscription.Value;

            if (s != null)
            {
                s.Dispose();
                subscription.CompareAndSet(s, null);
            }
        }
Exemplo n.º 9
0
        public void Cache_should_sweep_out_expired_entries_on_cleanup()
        {
            var localClock = new AtomicReference<long>(0);
            var cache = new SimpleDnsCacheTestDouble(localClock);
            var cacheEntry = Dns.Resolved.Create("test.local", System.Net.Dns.GetHostEntry("127.0.0.1").AddressList);
            cache.Put(cacheEntry, 5000);

            cache.Cached("test.local").ShouldBe(cacheEntry);
            localClock.CompareAndSet(0, 5000);
            cache.Cached("test.local").ShouldBe(null);
            localClock.CompareAndSet(5000, 0);
            cache.Cached("test.local").ShouldBe(cacheEntry);
            localClock.CompareAndSet(0, 5000);
            cache.CleanUp();
            cache.Cached("test.local").ShouldBe(null);
            localClock.CompareAndSet(5000, 0);
            cache.Cached("test.local").ShouldBe(null);
           
        }
Exemplo n.º 10
0
        public void Cache_should_sweep_out_expired_entries_on_cleanup()
        {
            var localClock = new AtomicReference <long>(0);
            var cache      = new SimpleDnsCacheTestDouble(localClock);
            var cacheEntry = Dns.Resolved.Create("test.local", System.Net.Dns.GetHostEntry("127.0.0.1").AddressList);

            cache.Put(cacheEntry, 5000);

            cache.Cached("test.local").ShouldBe(cacheEntry);
            localClock.CompareAndSet(0, 5000);
            cache.Cached("test.local").ShouldBe(null);
            localClock.CompareAndSet(5000, 0);
            cache.Cached("test.local").ShouldBe(cacheEntry);
            localClock.CompareAndSet(0, 5000);
            cache.CleanUp();
            cache.Cached("test.local").ShouldBe(null);
            localClock.CompareAndSet(5000, 0);
            cache.Cached("test.local").ShouldBe(null);
        }
Exemplo n.º 11
0
 internal void CreateNewBatchAndExecutePreviousIfNeeded(RequestBatch <BatchReturnType, RequestResponseType, RequestArgumentType> previousBatch)
 {
     if (previousBatch == null)
     {
         throw new InvalidOperationException("Trying to start null batch which means it was shutdown already.");
     }
     if (batch.CompareAndSet(previousBatch, new RequestBatch <BatchReturnType, RequestResponseType, RequestArgumentType>(properties, commandCollapser, properties.MaxRequestsInBatch)))
     {
         previousBatch.ExecuteBatchIfNotAlreadyStarted();
     }
 }
 internal virtual void Start()
 {
     Preconditions.CheckState(state.CompareAndSet(TestAppendSnapshotTruncate.Worker.State
                                                  .Idle, TestAppendSnapshotTruncate.Worker.State.Running));
     if (thread.Get() == null)
     {
         Sharpen.Thread t = new Sharpen.Thread(null, new _Runnable_446(this), name);
         Preconditions.CheckState(thread.CompareAndSet(null, t));
         t.Start();
     }
 }
Exemplo n.º 13
0
        private Task Stop()
        {
            var p = new TaskCompletionSource <bool>();

            if (_stopped.CompareAndSet(null, p))
            {
                // Let remaining work that is already being processed finished. The termination task will complete afterwards
                return(p.Task);
            }
            return(Completed);
        }
Exemplo n.º 14
0
        private static Type RegisterBaseClass(Type superType, Type baseType)
        {
            IPersistentMap map = _baseClassMapRef.Get();

            while (!map.containsKey(superType))
            {
                IPersistentMap newMap = map.assoc(superType, baseType);
                _baseClassMapRef.CompareAndSet(map, newMap);
                map = _baseClassMapRef.Get();
            }

            return(LookupBaseClass(superType));  // may not be the one we defined -- race condition
        }
Exemplo n.º 15
0
 public void Clear()
 {
     while (true)
     {
         /*
          * it should be very hard to not succeed the first pass thru since this is typically is only called from
          * a single thread protected by a tryLock, but there is at least 1 other place (at time of writing this comment)
          * where reset can be called from (CircuitBreaker.markSuccess after circuit was tripped) so it can
          * in an edge-case conflict.
          * Instead of trying to determine if someone already successfully called clear() and we should skip
          * we will have both calls reset the circuit, even if that means losing data added in between the two
          * depending on thread scheduling.
          * The rare scenario in which that would occur, we'll accept the possible data loss while clearing it
          * since the code has stated its desire to clear() anyways.
          */
         var current = _state.Value;
         var newState = current.Clear();
         if (_state.CompareAndSet(current, newState))
         {
             return;
         }
     }
 }
Exemplo n.º 16
0
 /// <summary>Dynamically add new volumes to the existing volumes that this DN manages.
 ///     </summary>
 /// <param name="ref">a reference to the new FsVolumeImpl instance.</param>
 internal virtual void AddVolume(FsVolumeReference @ref)
 {
     while (true)
     {
         FsVolumeImpl[]       curVolumes = volumes.Get();
         IList <FsVolumeImpl> volumeList = Lists.NewArrayList(curVolumes);
         volumeList.AddItem((FsVolumeImpl)@ref.GetVolume());
         if (volumes.CompareAndSet(curVolumes, Sharpen.Collections.ToArray(volumeList, new
                                                                           FsVolumeImpl[volumeList.Count])))
         {
             break;
         }
         else
         {
             if (FsDatasetImpl.Log.IsDebugEnabled())
             {
                 FsDatasetImpl.Log.Debug("The volume list has been changed concurrently, " + "retry to remove volume: "
                                         + @ref.GetVolume().GetStorageID());
             }
         }
     }
     if (blockScanner != null)
     {
         blockScanner.AddVolumeScanner(@ref);
     }
     else
     {
         // If the volume is not put into a volume scanner, it does not need to
         // hold the reference.
         IOUtils.Cleanup(FsDatasetImpl.Log, @ref);
     }
     // If the volume is used to replace a failed volume, it needs to reset the
     // volume failure info for this volume.
     RemoveVolumeFailureInfo(new FilePath(@ref.GetVolume().GetBasePath()));
     FsDatasetImpl.Log.Info("Added new volume: " + @ref.GetVolume().GetStorageID());
 }
Exemplo n.º 17
0
        /// <summary>
        /// Adds a continuation to the current task being performed.
        /// </summary>
        /// <param name="code">The method to run as part of the continuation</param>
        /// <exception cref="InvalidOperationException">This exception is thrown if the actor system has been terminated.</exception>
        public void Add(Action code)
        {
            var previous = _atomicRef.Value;

            if (_atomicRef.Value == null)
            {
                throw new InvalidOperationException("ActorSystem already terminated.");
            }

            var t = new Task(code);

            if (_atomicRef.CompareAndSet(previous, t))
            {
                t.ContinueWith(_ => previous.Start());
                return;
            }

            Add(code);
        }
Exemplo n.º 18
0
        private async Task TestSoLingerZeroCausesOnlyRstOnClose0(ServerBootstrap sb, Bootstrap cb)
        {
            AtomicReference <IChannel>  serverChannelRef = new AtomicReference <IChannel>();
            AtomicReference <Exception> throwableRef     = new AtomicReference <Exception>();
            CountdownEvent latch  = new CountdownEvent(1);
            CountdownEvent latch2 = new CountdownEvent(1);

            // SO_LINGER=0 means that we must send ONLY a RST when closing (not a FIN + RST).
            sb.ChildOption(ChannelOption.SoLinger, 0);
            sb.ChildHandler(new ActionChannelInitializer <IChannel>(ch =>
            {
                serverChannelRef.CompareAndSet(null, ch);
                latch.SafeSignal();
            }));
            cb.Handler(new ActionChannelInitializer <IChannel>(ch =>
            {
                ch.Pipeline.AddLast(new ChannelInboundHandlerAdapter0(throwableRef, latch2));
            }));
            IChannel sc = await sb.BindAsync();

            IChannel cc = await cb.ConnectAsync(sc.LocalAddress);

            // Wait for the server to get setup.
            latch.Wait();

            // The server has SO_LINGER=0 and so it must send a RST when close is called.
            serverChannelRef.Value.CloseAsync().Ignore();

            // Wait for the client to get channelInactive.
            latch2.Wait();

            // Verify the client received a RST.
            var cause = throwableRef.Value;

            if (cause is object)
            {
                Assert.True(cause is SocketException, "actual [type, message]: [" + cause.GetType() + ", " + cause.Message + "]");

                //AssertRstOnCloseException((SocketException)cause, cc);
                Assert.True(cause.Message.Contains("reset") || cause.Message.Contains("closed"), "actual message: " + cause.Message);
            }
        }
 public override void ChannelInactive(IChannelHandlerContext ctx)
 {
     if (_clientReceived.CompareAndSet(null, _aggregator))
     {
         try
         {
             Assert.Equal(_expectedContent.ReadableBytes, _aggregator.ReadableBytes);
         }
         catch (Exception cause)
         {
             _aggregator.Release();
             _aggregator           = null;
             _clientReceived.Value = cause;
         }
         finally
         {
             _latch.SafeSignal();
         }
     }
 }
Exemplo n.º 20
0
        public List <ServiceMetadata> Lookup(string groupPath)
        {
            if (string.IsNullOrEmpty(groupPath))
            {
                throw new ArgumentNullException("groupPath", "groupPath is null or empty");
            }

            var metadatas = new List <ServiceMetadata>();

            if (_notified.TryGetValue(groupPath, out metadatas) == false || metadatas.Any() == false)
            {
                var reference = new AtomicReference <List <ServiceMetadata> >(null);
                var listener  = new NotifyListener();
                listener.OnMetadataChanged += (metas) => reference.CompareAndSet(null, metas);
                this.Subscribe(groupPath, listener, true); // 订阅逻辑需要保证 Notify 后再返回
                metadatas = reference.Value;
            }

            return(metadatas);
        }
Exemplo n.º 21
0
        /// <summary>
        /// TBD
        /// </summary>
        public override void HeartBeat()
        {
            var timestamp = _clock();
            var oldState  = state;
            HeartbeatHistory newHistory;

            if (!oldState.TimeStamp.HasValue)
            {
                //this is a heartbeat for a new resource
                //add starter records for this new resource
                newHistory = FirstHeartBeat;
            }
            else
            {
                //this is a known connection
                var interval = timestamp - oldState.TimeStamp.Value;
                //don't use the first heartbeat after failure for the history, since a long pause will skew the stats
                if (IsTimeStampAvailable(timestamp))
                {
                    if (interval >= (AcceptableHeartbeatPauseMillis / 3 * 2) && EventStream.HasValue)
                    {
                        EventStream.Value.Publish(new Warning(ToString(), GetType(),
                                                              $"heartbeat interval is growing too large for address {Address}: {interval} millis"));
                    }
                    newHistory = (oldState.History + interval);
                }
                else
                {
                    newHistory = oldState.History;
                }
            }

            var newState = new State(newHistory, timestamp);

            //if we won the race then update else try again
            if (!_state.CompareAndSet(oldState, newState))
            {
                HeartBeat();
            }
        }
Exemplo n.º 22
0
        public void CompareAndSet_when_equal_sets_value()
        {
            var oldValue = new object();
            var obj = new AtomicReference<object>(oldValue);
            var newValue = new object();

            obj.CompareAndSet(oldValue, newValue);

            Assert.AreEqual(newValue, obj.Value);
        }
Exemplo n.º 23
0
 private bool UpdateState(object oldState, object newState)
 {
     return(_stateDoNotCallMeDirectly.CompareAndSet(oldState, newState));
 }
Exemplo n.º 24
0
 private bool UpdateWatchedBy(ImmutableHashSet <IActorRef> oldWatchedBy, ImmutableHashSet <IActorRef> newWatchedBy)
 {
     return(_watchedByDoNotCallMeDirectly.CompareAndSet(oldWatchedBy, newWatchedBy));
 }
Exemplo n.º 25
0
        /// <summary>
        /// Run tasks of all phases including and after the given phase.
        /// </summary>
        /// <param name="reason">Reason of the shutdown</param>
        /// <param name="fromPhase">Optional. The phase to start the run from.</param>
        /// <returns>A task that is completed when all such tasks have been completed, or
        /// there is failure when <see cref="Phase.Recover"/> is disabled.</returns>
        /// <remarks>
        /// It is safe to call this method multiple times. It will only run the shutdown sequence once.
        /// </remarks>
        public Task <Done> Run(Reason reason, string fromPhase = null)
        {
            if (_runStarted.CompareAndSet(null, reason))
            {
                var debugEnabled = Log.IsDebugEnabled;

                Task <Done> Loop(List <string> remainingPhases)
                {
                    var phase = remainingPhases.FirstOrDefault();

                    if (phase == null)
                    {
                        return(TaskEx.Completed);
                    }
                    var         remaining   = remainingPhases.Skip(1).ToList();
                    Task <Done> phaseResult = null;

                    if (!_tasks.TryGetValue(phase, out var phaseTasks))
                    {
                        if (debugEnabled)
                        {
                            Log.Debug("Performing phase [{0}] with [0] tasks.", phase);
                        }
                        phaseResult = TaskEx.Completed;
                    }
                    else
                    {
                        if (debugEnabled)
                        {
                            Log.Debug("Performing phase [{0}] with [{1}] tasks: [{2}]", phase, phaseTasks.Count, string.Join(",", phaseTasks.Select(x => x.Item1)));
                        }

                        // note that tasks within same phase are performed in parallel
                        var recoverEnabled = Phases[phase].Recover;
                        var result         = Task.WhenAll <Done>(phaseTasks.Select(x =>
                        {
                            var taskName = x.Item1;
                            var task     = x.Item2;
                            try
                            {
                                // need to begin execution of task
                                var r = task();

                                if (recoverEnabled)
                                {
                                    return(r.ContinueWith(tr =>
                                    {
                                        if (tr.IsCanceled || tr.IsFaulted)
                                        {
                                            Log.Warning("Task [{0}] failed in phase [{1}]: {2}", taskName, phase, tr.Exception?.Flatten().Message);
                                        }
                                        return Done.Instance;
                                    }));
                                }

                                return(r);
                            }
                            catch (Exception ex)
                            {
                                // in case task.Start() throws
                                if (recoverEnabled)
                                {
                                    Log.Warning("Task [{0}] failed in phase [{1}]: {2}", taskName, phase, ex.Message);
                                    return(TaskEx.Completed);
                                }

                                return(TaskEx.FromException <Done>(ex));
                            }
                        }))
                                             .ContinueWith(tr =>
                        {
                            // forces downstream error propagation if recover is disabled
                            var force = tr.Result;
                            return(Done.Instance);
                        });
                        var         timeout         = Phases[phase].Timeout;
                        var         deadLine        = MonotonicClock.Elapsed + timeout;
                        Task <Done> timeoutFunction = null;
                        try
                        {
                            timeoutFunction = After(timeout, System.Scheduler, () =>
                            {
                                if (phase == CoordinatedShutdown.PhaseActorSystemTerminate && MonotonicClock.ElapsedHighRes < deadLine)
                                {
                                    return(result); // too early, i.e. triggered by system termination
                                }
                                if (result.IsCompleted)
                                {
                                    return(TaskEx.Completed);
                                }

                                if (recoverEnabled)
                                {
                                    Log.Warning("Coordinated shutdown phase [{0}] timed out after {1}", phase, timeout);
                                    return(TaskEx.Completed);
                                }

                                return(TaskEx.FromException <Done>(new TimeoutException($"Coordinated shutdown phase[{phase}] timed out after {timeout}")));
                            });
                        }
                        catch (SchedulerException)
                        {
                            // The call to `after` threw SchedulerException, triggered by system termination
                            timeoutFunction = result;
                        }
                        catch (InvalidOperationException)
                        {
                            // The call to `after` threw SchedulerException, triggered by Scheduler being in unset state
                            timeoutFunction = result;
                        }

                        phaseResult = Task.WhenAny <Done>(result, timeoutFunction).Unwrap();
                    }

                    if (!remaining.Any())
                    {
                        return(phaseResult);
                    }
                    return(phaseResult.ContinueWith(tr =>
                    {
                        // force any exceptions to be rethrown so next phase stops
                        // and so failure gets propagated back to caller
                        var r = tr.Result;
                        return Loop(remaining);
                    })
                           .Unwrap <Done>());
                }

                var runningPhases = (fromPhase == null
                    ? OrderedPhases // all
                    : OrderedPhases.From(fromPhase)).ToList();

                var done = Loop(runningPhases);
                done.ContinueWith(tr =>
                {
                    if (!tr.IsFaulted && !tr.IsCanceled)
                    {
                        _runPromise.SetResult(tr.Result);
                    }
                    else
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        _runPromise.SetException(tr.Exception.Flatten());
                    }
                });
            }
            return(_runPromise.Task);
        }
Exemplo n.º 26
0
        public override Routee Select(object message, Routee[] routees)
        {
            if (message == null || routees == null || routees.Length == 0)
            {
                return(Routee.NoRoutee);
            }

            Func <ConsistentHash <ConsistentRoutee> > updateConsistentHash = () =>
            {
                // update consistentHash when routees are changed
                // changes to routees are rare when no changes this is a quick operation
                var oldConsistHashTuple = _consistentHashRef.Value;
                var oldRoutees          = oldConsistHashTuple.Item1;
                var oldConsistentHash   = oldConsistHashTuple.Item2;

                if (oldRoutees == null || !routees.SequenceEqual(oldRoutees))
                {
                    // when other instance, same content, no need to re-hash, but try to set routees
                    var consistentHash = routees == oldRoutees
                        ? oldConsistentHash
                        : ConsistentHash.Create(routees.Select(x => new ConsistentRoutee(x, _selfAddress)), _vnodes);
                    //ignore, don't update, in case of CAS failure
                    _consistentHashRef.CompareAndSet(oldConsistHashTuple, Tuple.Create(routees, consistentHash));
                    return(consistentHash);
                }
                return(oldConsistentHash);
            };

            Func <object, Routee> target = hashData =>
            {
                try
                {
                    var currentConsistentHash = updateConsistentHash();
                    if (currentConsistentHash.IsEmpty)
                    {
                        return(Routee.NoRoutee);
                    }
                    else
                    {
                        if (hashData is byte[])
                        {
                            return(currentConsistentHash.NodeFor(hashData as byte[]).Routee);
                        }
                        if (hashData is string)
                        {
                            return(currentConsistentHash.NodeFor(hashData as string).Routee);
                        }
                        return
                            (currentConsistentHash.NodeFor(
                                 _system.Serialization.FindSerializerFor(hashData).ToBinary(hashData)).Routee);
                    }
                }
                catch (Exception ex)
                {
                    //serialization failed
                    _log.Value.Warning("Couldn't route message with consistent hash key [{0}] due to [{1}]", hashData,
                                       ex.Message);
                    return(Routee.NoRoutee);
                }
            };

            if (_hashMapping(message) != null)
            {
                return(target(ConsistentHash.ToBytesOrObject(_hashMapping(message))));
            }
            else if (message is IConsistentHashable)
            {
                var hashable = (IConsistentHashable)message;
                return(target(ConsistentHash.ToBytesOrObject(hashable.ConsistentHashKey)));
            }
            else
            {
                _log.Value.Warning(
                    "Message [{0}] must be handled by hashMapping, or implement [{1}] or be wrapped in [{2}]",
                    message.GetType().Name, typeof(IConsistentHashable).Name, typeof(ConsistentHashableEnvelope).Name);
                return(Routee.NoRoutee);
            }
        }
Exemplo n.º 27
0
 public override void ExceptionCaught(IChannelHandlerContext ctx, Exception cause)
 {
     _throwableRef.CompareAndSet(null, cause);
 }
Exemplo n.º 28
0
 public override void HeartBeat()
 {
     _status.CompareAndSet(Status.Unknown, Status.Up);
 }
Exemplo n.º 29
0
        public void CompareAndSet_when_not_equal_returns_false()
        {
            var obj = new AtomicReference<object>();

            Assert.IsFalse(obj.CompareAndSet(new object(), new object()));
        }