Beispiel #1
0
        public void TrySet_should_return_true_if_value_changed()
        {
            boolean.Value = true;

            boolean.TrySet(true).Should().BeFalse();
            boolean.TrySet(false).Should().BeTrue();
            boolean.TrySet(false).Should().BeFalse();
        }
Beispiel #2
0
 /// <summary>
 /// Makes sure retrieving items that were postponed,
 /// because they would exceed <see cref="DataflowBlockOptions.BoundedCapacity"/>,
 /// is currently running.
 /// </summary>
 void EnsurePostponedProcessing()
 {
     if (postponedProcessing.TrySet())
     {
         Task.Factory.StartNew(RetrievePostponed, options.CancellationToken,
                               TaskCreationOptions.PreferFairness, options.TaskScheduler);
     }
 }
 /// <summary>
 /// Starts non-greedy creation of tuples, if one doesn't already run.
 /// </summary>
 void EnsureNonGreedyProcessing()
 {
     if (nonGreedyProcessing.TrySet())
     {
         Task.Factory.StartNew(NonGreedyProcess,
                               dataflowBlockOptions.CancellationToken,
                               TaskCreationOptions.PreferFairness,
                               dataflowBlockOptions.TaskScheduler);
     }
 }
Beispiel #4
0
 /// <summary>
 /// Starts non-greedy creation of batches, if one doesn't already run.
 /// </summary>
 /// <param name="manuallyTriggered">Whether the batch was triggered by <see cref="TriggerBatch"/>.</param>
 void EnsureNonGreedyProcessing(bool manuallyTriggered)
 {
     if (nonGreedyProcessing.TrySet())
     {
         Task.Factory.StartNew(() => NonGreedyProcess(manuallyTriggered),
                               dataflowBlockOptions.CancellationToken,
                               TaskCreationOptions.PreferFairness,
                               dataflowBlockOptions.TaskScheduler);
     }
 }
Beispiel #5
0
        internal void ContinueWithCore(Task continuation, TaskContinuationOptions kind,
                                       TaskScheduler scheduler, Func <bool> predicate)
        {
            // Already set the scheduler so that user can call Wait and that sort of stuff
            continuation.taskScheduler = scheduler;
            continuation.scheduler     = ProxifyScheduler(scheduler);
            continuation.schedWait.Set();

            AtomicBoolean launched = new AtomicBoolean();
            EventHandler  action   = delegate(object sender, EventArgs e) {
                if (!launched.Value && launched.TrySet())
                {
                    if (!predicate())
                    {
                        return;
                    }

                    if (!ContinuationStatusCheck(kind))
                    {
                        continuation.CancelReal();
                        continuation.Dispose();

                        return;
                    }

                    CheckAndSchedule(continuation, kind, scheduler, sender == null);
                }
            };

            if (IsCompleted)
            {
                action(null, EventArgs.Empty);
                return;
            }

            completed.Enqueue(action);

            // Retry in case completion was achieved but event adding was too late
            if (IsCompleted)
            {
                action(null, EventArgs.Empty);
            }
        }
        private async Task EnsureNodeExistsAsync()
        {
            if (!await EnvironmentExistsAsync().ConfigureAwait(false))
            {
                return;
            }

            var existsNode = await zooKeeperClient.ExistsAsync(new ExistsRequest(replicaNodePath) { Watcher = nodeWatcher }).ConfigureAwait(false);

            if (!existsNode.IsSuccessful)
            {
                return;
            }

            if (registrationAllowed.TrySet(settings.RegistrationAllowedProvider?.Invoke() ?? true))
            {
                log.Info(registrationAllowed ? "Registration has been allowed." : "Registration has been denied.");
            }

            if (!registrationAllowed)
            {
                if (existsNode.Stat != null)
                {
                    await DeleteNodeAsync().ConfigureAwait(false);
                }
                return;
            }

            if (existsNode.Stat != null)
            {
                if (existsNode.Stat.EphemeralOwner == zooKeeperClient.SessionId)
                {
                    nodeCreatedOnceSignal.Set();
                    return;
                }

                var lastConnected    = new DateTime(Interlocked.Read(ref lastConnectedTimestamp), DateTimeKind.Utc);
                var nodeCreationTime = existsNode.Stat.CreatedTime;
                if (nodeCreationTime > lastConnected)
                {
                    log.Warn(
                        "Node with path '{ReplicaNodePath}' already exists " +
                        "and has owner session id = {NodeEphemeralOwner:x16}, " +
                        "which differs from our id = {ClientSessionId:x16}. " +
                        "But it was created recently (at {NodeCreationTime}) so we won't touch it. " +
                        "This may indicate several beacons with same environment, application and replica exist.",
                        replicaNodePath,
                        existsNode.Stat.EphemeralOwner,
                        zooKeeperClient.SessionId,
                        nodeCreationTime);

                    return;
                }

                log.Warn(
                    "Node with path '{ReplicaNodePath}' already exists, " +
                    "but looks like a stale one from ourselves. " +
                    "It has owner session id = {NodeEphemeralOwner:x16}, " +
                    "which differs from our id = {ClientSessionId:x16}. " +
                    "It was created at {NodeCreationTime}. " +
                    "Will delete it and create a new one.",
                    replicaNodePath,
                    existsNode.Stat.EphemeralOwner,
                    zooKeeperClient.SessionId,
                    nodeCreationTime);

                if (!await DeleteNodeAsync().ConfigureAwait(false))
                {
                    return;
                }
            }

            var createRequest = new CreateRequest(replicaNodePath, CreateMode.Ephemeral)
            {
                Data = replicaNodeData
            };

            var create = await zooKeeperClient.CreateAsync(createRequest).ConfigureAwait(false);

            if (create.IsSuccessful)
            {
                nodeCreatedOnceSignal.Set();
            }

            if (!create.IsSuccessful && create.Status != ZooKeeperStatus.NodeAlreadyExists)
            {
                log.Error("Node creation has failed.");
                return;
            }

            await zooKeeperClient.ExistsAsync(new ExistsRequest(replicaNodePath) { Watcher = nodeWatcher }).ConfigureAwait(false);
        }