/// <summary> /// INTERNAL API. /// /// Shuts down all connections to other members, the cluster daemon and the periodic gossip and cleanup tasks. /// This should not be called directly by the user /// /// The user can issue a <see cref="Leave"/> command which will tell the node /// to go through graceful handoff process <c>LEAVE -> EXITING -> REMOVED -> SHUTDOWN</c>. /// </summary> internal void Shutdown() { if (_isTerminated.CompareAndSet(false, true)) { LogInfo("Shutting down..."); System.Stop(_clusterDaemons); _readView?.Dispose(); LogInfo("Successfully shut down"); } }
internal override void PostInit() { if (_nearCacheInitialized.CompareAndSet(false, true)) { var nearCacheConfig = GetContext().GetClientConfig().GetNearCacheConfig(GetName()); if (nearCacheConfig == null) { return; } _nearCache = new ClientNearCache(GetName(), ClientNearCacheType.Map, GetContext(), nearCacheConfig); } }
public override void Release() { if (_active.CompareAndSet(true, false)) { _pool?.Release(this); } else { Debug.WriteLine($"Attempt to release the same buffer [{Id}:{Tag}] more than once"); } _pool?.Release(this); }
public void AtomicBooleanCompareAndSetReturnsExpectedValuesForFailedMatchesMultiThreaded() { const int expected = 1000; var b = new AtomicBoolean(true); var values = new ConcurrentCountedSet <bool>(); System.Threading.Tasks.Parallel.For(0, 1000, i => values.Add(b.CompareAndSet(i % 2 != 0, false))); Assert.AreEqual(expected, values[true] + values[false]); }
// May be incorrect if there's multiple NodeManagers running on a single host. // knownNodeCount is based on node managers, not hosts. blacklisting is // currently based on hosts. protected internal virtual void ComputeIgnoreBlacklisting() { if (!nodeBlacklistingEnabled) { return; } if (blacklistDisablePercent != -1 && (blacklistedNodeCount != blacklistedNodes.Count || clusterNmCount != lastClusterNmCount)) { blacklistedNodeCount = blacklistedNodes.Count; if (clusterNmCount == 0) { Log.Info("KnownNode Count at 0. Not computing ignoreBlacklisting"); return; } int val = (int)((float)blacklistedNodes.Count / clusterNmCount * 100); if (val >= blacklistDisablePercent) { if (ignoreBlacklisting.CompareAndSet(false, true)) { Log.Info("Ignore blacklisting set to true. Known: " + clusterNmCount + ", Blacklisted: " + blacklistedNodeCount + ", " + val + "%"); // notify RM to ignore all the blacklisted nodes blacklistAdditions.Clear(); Sharpen.Collections.AddAll(blacklistRemovals, blacklistedNodes); } } else { if (ignoreBlacklisting.CompareAndSet(true, false)) { Log.Info("Ignore blacklisting set to false. Known: " + clusterNmCount + ", Blacklisted: " + blacklistedNodeCount + ", " + val + "%"); // notify RM of all the blacklisted nodes Sharpen.Collections.AddAll(blacklistAdditions, blacklistedNodes); blacklistRemovals.Clear(); } } } }
public async Task Recover() { if (_isActive.CompareAndSet(false, true)) { var promise = new TaskCompletionSource <object>(); var recovery = new Recovery(this); var infos = await recovery.ReadEndpointInfoAsync(); var trackers = await recovery.ReadEventLogClocksAsync(); var phase1 = Task.Run <IEnumerable <RecoveryLink> >(async() => { var deleteSnapshots = new List <Task>(); var links = new List <RecoveryLink>(); foreach (var link in recovery.GetRecoveryLinks(infos, trackers)) { deleteSnapshots.Add(recovery.DeleteSnapshotsAsync(link)); links.Add(link); } await Task.WhenAll(deleteSnapshots); return(links); }); var phase2 = Task.Run(async() => { var _ = await phase1; var r = await promise.Task; _isActive.Value = false; return(r); }); phase1.ContinueWith(t => _acceptor.Tell(new Acceptor.Recover(t.Result.ToImmutableHashSet(), promise))); await phase2.ConfigureAwait(false); } else { throw new IllegalStateException("Recovery running or endpoint already activated"); } }
bool IReadBuffer.TryRead(out T result) { if (readyToRead.CompareAndSet(true, false)) { result = value; return(true); } else { result = default; return(false); } }
private void DoShutdown() { lock (_shutdownLock) { if (!_active.CompareAndSet(true, false)) { return; } FireLifecycleEvent(LifecycleEvent.LifecycleState.ShuttingDown); _client.DoShutdown(); FireLifecycleEvent(LifecycleEvent.LifecycleState.Shutdown); } }
public virtual void MarkSuccess() { if (circuitOpen.Value) { if (circuitOpen.CompareAndSet(true, false)) { // win the thread race to reset metrics // Unsubscribe from the current stream to reset the health counts stream. This only affects the health counts view, // and all other metric consumers are unaffected by the reset metrics.ResetStream(); } } }
internal Overrides?Peek() { var retries = 0; while (true) { if (_accessible.CompareAndSet(false, true)) { Overrides?temp = null; if (!IsEmpty) { temp = _overrides[0]; } _accessible.Set(false); return(temp); } if (++retries > 100_000_000) { return(null); } } }
public override void Start() { base.Start(); if (firstStart.CompareAndSet(true, false)) { try { next.Oneway(wireFormat.PreferedWireFormatInfo); } finally { wireInfoSentDownLatch.countDown(); } } }
public void Shutdown() { if (_isTerminated.CompareAndSet(false, true)) { LogInfo("Shutting down..."); System.Stop(_clusterDaemons); if (_readView != null) { _readView.Dispose(); } LogInfo("Successfully shut down"); } }
public void AtomicBooleanCompareAndSetReturnsExpectedValuesForFailedMatches() { const int expected = 1000; var b = new AtomicBoolean(true); var values = new ConcurrentCountedSet <bool>(); for (int i = 0; i < 1000; i++) { values.Add(b.CompareAndSet(i % 2 != 0, false)); } Assert.AreEqual(expected, values[true] + values[false]); }
public void Dispose(bool unregister) { if (_disposed.CompareAndSet(false, true)) { if (unregister) { _logic.Remove(_observer); } _observer.OnCompleted(); } else { throw new ObjectDisposedException("ObservableSink subscription has been already disposed."); } }
public void Subscribe(ISubscriber <int> subscriber) => subscriber.OnSubscribe(new LamdaSubscription(onRequest: n => { Action signalling = () => { for (var i = 0L; i < n; i++) { try { // shutdown cleanly in when the task is shutting down if (_token.IsCancellationRequested) { return; } subscriber.OnNext((int)i); } catch (Exception ex) { // signal others to shut down _source.Cancel(); if (ex is Latch.ExpectedOpenLatchException) { if (!_concurrentAccessCaused.CompareAndSet(false, true)) { throw new Exception("Concurrent access detected", ex); } // error signalled once already, stop more errors from propagating return; } else { throw; } } } }; // must be guarded like this in case a Subscriber triggers request() synchronously from it's onNext() while (_startedSignallingThreads.GetAndIncrement() < MaxSignallingThreads && !_token.IsCancellationRequested) { new Thread(() => signalling()).Start(); } }));
public void Stop() { if (!stopped.CompareAndSet(false, true)) { return; } Sweep(); int retries = 0; while (Count > 1 && ++retries < 10) { try { Thread.Sleep(10); } catch (Exception) { } } scheduler.Close(); }
/// <exception cref="System.IO.IOException"></exception> public void Close() { if (!_live.CompareAndSet(true, false)) { return; } _writeQueue.CompleteAdding(); if (_writeThread != null && _writeThread.IsAlive) { _writeThread.Interrupt(); } if (Logger.IsFinestEnabled()) { Logger.Finest(string.Format("Closing socket, address: {0} id: {1}", GetAddress(), _id)); } _stream.Close(); _clientSocket.Shutdown(SocketShutdown.Both); _clientSocket.Close(); }
private bool GetPartitions() { if (_isLive && _updating.CompareAndSet(false, true)) { try { Logger.Finest("Updating partition list."); var clusterService = _client.GetClientClusterService(); var ownerAddress = clusterService.GetOwnerConnectionAddress(); if (ownerAddress == null) { throw new InvalidOperationException("Owner address was null"); } var connection = _client.GetConnectionManager().GetConnection(ownerAddress); if (connection == null) { throw new InvalidOperationException( "Owner connection is not available, could not get partitions."); } var response = GetPartitionsFrom(connection); var result = ProcessPartitionResponse(response); Logger.Finest("Partition list updated"); return(result); } catch (HazelcastInstanceNotActiveException) { } catch (Exception e) { Logger.Warning("Error when getting list of partitions", e); } finally { _updating.Set(false); } } return(false); }
/// <summary> /// Has to run asyncronously because it invokes methods in main thread /// </summary> /// <param name="repoUrl"></param> async void TryLoadRepository(string repoUrl) { if (!currentlyLoadingRepository.CompareAndSet(false, true)) { return; // do nothing because one repository is already loading } _searchResultsViewModel.Reset(); _repositoryOverviewViewModel.Reset(); _filePreviewViewModel.Reset(); if (repoUrl == null || repoUrl.Length == 0) { if (subversionSearcher != null) { subversionSearcher.Close(); subversionSearcher = null; } return; } repoUrl = repoUrl.TrimEnd('/') + '/'; if (SubversionSearcher.IsReady(subversionSearcher) && repoUrl.StartsWith(subversionSearcher.RepositoryUrl)) { // were just in another directory of the same repository //_headNodePath = subversionSearcher.GetLowestExistingHeadNodePath(repoUrl, subversionSearcher.HeadRevision); App.Current.Dispatcher.Invoke(() => MakeTreeAndUpdateRepoUrl(subversionSearcher, repoUrl)); } else { // load other repository // close the old one if (SubversionSearcher.IsReady(subversionSearcher)) { subversionSearcher.Close(); Progress.Log("Switching Repository..."); } subversionSearcher = null; /// START trying to find credentials by and opening add credentials window if necessary Progress.Log("Finding credentials..."); bool useCredentials = true; SubversionSearcher.AuthenticationInfo info = SubversionSearcher.HasCredentials(repoUrl); for (int i = 0; i < 4; i++) { if (info.Successful) { if (i == 0) { useCredentials = false; } break; } else if ( info.ThrownException != null && ( info.ThrownException is SharpSvn.SvnAuthorizationException || info.ThrownException is SharpSvn.SvnRepositoryIOForbiddenException || info.ThrownException is SharpSvn.SvnAuthenticationException) || info.ThrownException?.InnerException != null && ( info.ThrownException.InnerException is SharpSvn.SvnAuthorizationException || info.ThrownException.InnerException is SharpSvn.SvnRepositoryIOForbiddenException || info.ThrownException.InnerException is SharpSvn.SvnAuthenticationException)) { // trying to make sense of sharpsvn's exceptions // opening credentials window if (i == 0) { info = SubversionSearcher.CheckCredentials(repoUrl, Settings.Instance.Username, Settings.Instance.Password); } else { Progress.Log("Wrong credentials"); var mrse = new ManualResetEvent(false); bool cancelled = false; App.Current.Dispatcher.Invoke(() => { OpenCredentialsWindow(res => { HandleCredentialsWindowResponse(res); cancelled = res.cancelled; mrse.Set(); }); }); mrse.WaitOne(); if (cancelled) { break; } info = SubversionSearcher.CheckCredentials(repoUrl, Settings.Instance.Username, Settings.Instance.Password); } } else { Progress.Log(String.Format("{0}\r\nSee error.log", info.ExceptionMessage)); break; } } // END trying to find credentials if (info.Successful) { Progress.Log("Loading Repository"); if (useCredentials) { // Use credentials from credentials window or file await Task.Run(() => { try { subversionSearcher = new SubversionSearcher(RepositoryUrl, Settings.Instance.Username, Settings.Instance.Password); } catch (Exception ex) { Progress.Log("Failed to initialize. See error.log"); Progress.ErrorLog(ex); } }); } else { // Use credentials from tortoise await Task.Run(() => { try { subversionSearcher = new SubversionSearcher(RepositoryUrl); } catch (Exception ex) { Progress.Log("Failed to initialize. See error.log"); Progress.ErrorLog(ex); } }); } App.Current.Dispatcher.Invoke(() => MakeTreeAndUpdateRepoUrl(subversionSearcher, repoUrl)); } } currentlyLoadingRepository.Set(false); }
public bool Delivering(bool flag) => delivering.CompareAndSet(!flag, flag);
public virtual int Call() { // dispatcher not typed ContainerLaunchContext launchContext = container.GetLaunchContext(); IDictionary <Path, IList <string> > localResources = null; ContainerId containerID = container.GetContainerId(); string containerIdStr = ConverterUtils.ToString(containerID); IList <string> command = launchContext.GetCommands(); int ret = -1; // CONTAINER_KILLED_ON_REQUEST should not be missed if the container // is already at KILLING if (container.GetContainerState() == ContainerState.Killing) { dispatcher.GetEventHandler().Handle(new ContainerExitEvent(containerID, ContainerEventType .ContainerKilledOnRequest, Shell.Windows ? ContainerExecutor.ExitCode.ForceKilled .GetExitCode() : ContainerExecutor.ExitCode.Terminated.GetExitCode(), "Container terminated before launch." )); return(0); } try { localResources = container.GetLocalizedResources(); if (localResources == null) { throw RPCUtil.GetRemoteException("Unable to get local resources when Container " + containerID + " is at " + container.GetContainerState()); } string user = container.GetUser(); // /////////////////////////// Variable expansion // Before the container script gets written out. IList <string> newCmds = new AList <string>(command.Count); string appIdStr = app.GetAppId().ToString(); string relativeContainerLogDir = Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Launcher.ContainerLaunch .GetRelativeContainerLogDir(appIdStr, containerIdStr); Path containerLogDir = dirsHandler.GetLogPathForWrite(relativeContainerLogDir, false ); foreach (string str in command) { // TODO: Should we instead work via symlinks without this grammar? newCmds.AddItem(ExpandEnvironment(str, containerLogDir)); } launchContext.SetCommands(newCmds); IDictionary <string, string> environment = launchContext.GetEnvironment(); // Make a copy of env to iterate & do variable expansion foreach (KeyValuePair <string, string> entry in environment) { string value = entry.Value; value = ExpandEnvironment(value, containerLogDir); entry.SetValue(value); } // /////////////////////////// End of variable expansion FileContext lfs = FileContext.GetLocalFSFileContext(); Path nmPrivateContainerScriptPath = dirsHandler.GetLocalPathForWrite(GetContainerPrivateDir (appIdStr, containerIdStr) + Path.Separator + ContainerScript); Path nmPrivateTokensPath = dirsHandler.GetLocalPathForWrite(GetContainerPrivateDir (appIdStr, containerIdStr) + Path.Separator + string.Format(ContainerLocalizer.TokenFileNameFmt , containerIdStr)); Path nmPrivateClasspathJarDir = dirsHandler.GetLocalPathForWrite(GetContainerPrivateDir (appIdStr, containerIdStr)); DataOutputStream containerScriptOutStream = null; DataOutputStream tokensOutStream = null; // Select the working directory for the container Path containerWorkDir = dirsHandler.GetLocalPathForWrite(ContainerLocalizer.Usercache + Path.Separator + user + Path.Separator + ContainerLocalizer.Appcache + Path.Separator + appIdStr + Path.Separator + containerIdStr, LocalDirAllocator.SizeUnknown, false ); string pidFileSubpath = GetPidFileSubpath(appIdStr, containerIdStr); // pid file should be in nm private dir so that it is not // accessible by users pidFilePath = dirsHandler.GetLocalPathForWrite(pidFileSubpath); IList <string> localDirs = dirsHandler.GetLocalDirs(); IList <string> logDirs = dirsHandler.GetLogDirs(); IList <string> containerLogDirs = new AList <string>(); foreach (string logDir in logDirs) { containerLogDirs.AddItem(logDir + Path.Separator + relativeContainerLogDir); } if (!dirsHandler.AreDisksHealthy()) { ret = ContainerExitStatus.DisksFailed; throw new IOException("Most of the disks failed. " + dirsHandler.GetDisksHealthReport (false)); } try { // /////////// Write out the container-script in the nmPrivate space. IList <Path> appDirs = new AList <Path>(localDirs.Count); foreach (string localDir in localDirs) { Path usersdir = new Path(localDir, ContainerLocalizer.Usercache); Path userdir = new Path(usersdir, user); Path appsdir = new Path(userdir, ContainerLocalizer.Appcache); appDirs.AddItem(new Path(appsdir, appIdStr)); } containerScriptOutStream = lfs.Create(nmPrivateContainerScriptPath, EnumSet.Of(CreateFlag .Create, CreateFlag.Overwrite)); // Set the token location too. environment[ApplicationConstants.ContainerTokenFileEnvName] = new Path(containerWorkDir , FinalContainerTokensFile).ToUri().GetPath(); // Sanitize the container's environment SanitizeEnv(environment, containerWorkDir, appDirs, containerLogDirs, localResources , nmPrivateClasspathJarDir); // Write out the environment exec.WriteLaunchEnv(containerScriptOutStream, environment, localResources, launchContext .GetCommands()); // /////////// End of writing out container-script // /////////// Write out the container-tokens in the nmPrivate space. tokensOutStream = lfs.Create(nmPrivateTokensPath, EnumSet.Of(CreateFlag.Create, CreateFlag .Overwrite)); Credentials creds = container.GetCredentials(); creds.WriteTokenStorageToStream(tokensOutStream); } finally { // /////////// End of writing out container-tokens IOUtils.Cleanup(Log, containerScriptOutStream, tokensOutStream); } // LaunchContainer is a blocking call. We are here almost means the // container is launched, so send out the event. dispatcher.GetEventHandler().Handle(new ContainerEvent(containerID, ContainerEventType .ContainerLaunched)); context.GetNMStateStore().StoreContainerLaunched(containerID); // Check if the container is signalled to be killed. if (!shouldLaunchContainer.CompareAndSet(false, true)) { Log.Info("Container " + containerIdStr + " not launched as " + "cleanup already called" ); ret = ContainerExecutor.ExitCode.Terminated.GetExitCode(); } else { exec.ActivateContainer(containerID, pidFilePath); ret = exec.LaunchContainer(container, nmPrivateContainerScriptPath, nmPrivateTokensPath , user, appIdStr, containerWorkDir, localDirs, logDirs); } } catch (Exception e) { Log.Warn("Failed to launch container.", e); dispatcher.GetEventHandler().Handle(new ContainerExitEvent(containerID, ContainerEventType .ContainerExitedWithFailure, ret, e.Message)); return(ret); } finally { completed.Set(true); exec.DeactivateContainer(containerID); try { context.GetNMStateStore().StoreContainerCompleted(containerID, ret); } catch (IOException) { Log.Error("Unable to set exit code for container " + containerID); } } if (Log.IsDebugEnabled()) { Log.Debug("Container " + containerIdStr + " completed with exit code " + ret); } if (ret == ContainerExecutor.ExitCode.ForceKilled.GetExitCode() || ret == ContainerExecutor.ExitCode .Terminated.GetExitCode()) { // If the process was killed, Send container_cleanedup_after_kill and // just break out of this method. dispatcher.GetEventHandler().Handle(new ContainerExitEvent(containerID, ContainerEventType .ContainerKilledOnRequest, ret, "Container exited with a non-zero exit code " + ret)); return(ret); } if (ret != 0) { Log.Warn("Container exited with a non-zero exit code " + ret); this.dispatcher.GetEventHandler().Handle(new ContainerExitEvent(containerID, ContainerEventType .ContainerExitedWithFailure, ret, "Container exited with a non-zero exit code " + ret)); return(ret); } Log.Info("Container " + containerIdStr + " succeeded "); dispatcher.GetEventHandler().Handle(new ContainerEvent(containerID, ContainerEventType .ContainerExitedWithSuccess)); return(0); }
public void AtomicBoolean_CompareAndSet_ThreadSafe_Test() { var boolean = new AtomicBoolean(false); var casResult = new ConcurrentDictionary <int, bool>(); var tasks = Enumerable.Range(1, 50).Select(key => Task.Factory.StartNew(() => casResult.TryAdd(key, boolean.CompareAndSet(false, true)))); Task.WaitAll(tasks.ToArray()); Assert.IsTrue(casResult.Values.Count(val => val == true) == 1); }
private async Task ProcessRequestAsync( long requestSequenceId, IChannelHandlerContext ctx, ThriftMessage message, TNiftyTransport messageTransport, TProtocol inProtocol, TProtocol outProtocol) { //Task.Run(() => //{ try { AtomicBoolean responseSent = new AtomicBoolean(false); // Use AtomicReference as a generic holder class to be able to mark it final // and pass into inner classes. Since we only use .get() and .set(), we don't // actually do any atomic operations. AtomicReference <ITimeout> expireTimeout = new AtomicReference <ITimeout>(null); try { try { long timeRemaining = 0; long timeElapsed = (DateTime.UtcNow.Ticks - message.ProcessStartTimeTicks) / 10000; if (_queueTimeoutMillis > 0) { if (timeElapsed >= _queueTimeoutMillis) { String error = "Task stayed on the queue for " + timeElapsed + " milliseconds, exceeding configured queue timeout of " + _queueTimeoutMillis + " milliseconds."; _logger.LogWarning(error); TApplicationException taskTimeoutException = new TApplicationException( ExceptionType.InternalError, error); await SendTApplicationExceptionAsync(taskTimeoutException, ctx, message, requestSequenceId, messageTransport, inProtocol, outProtocol); return; } } else if (_taskTimeoutMillis > 0) { if (timeElapsed >= _taskTimeoutMillis) { String error = "Task stayed on the queue for " + timeElapsed + " milliseconds, exceeding configured task timeout of " + _taskTimeoutMillis + " milliseconds."; _logger.LogWarning(error); TApplicationException taskTimeoutException = new TApplicationException( ExceptionType.InternalError, error); await SendTApplicationExceptionAsync(taskTimeoutException, ctx, message, requestSequenceId, messageTransport, inProtocol, outProtocol); return; } else { timeRemaining = _taskTimeoutMillis - timeElapsed; } } if (timeRemaining > 0) { expireTimeout.Value = _taskTimeoutTimer.NewTimeout(timeout => { // The immediateFuture returned by processors isn't cancellable, cancel() and // isCanceled() always return false. Use a flag to detect task expiration. if (responseSent.CompareAndSet(false, true)) { TApplicationException ex = new TApplicationException( ExceptionType.InternalError, "Task timed out while executing." ); // Create a temporary transport to send the exception var duplicateBuffer = message.Buffer.Duplicate(); duplicateBuffer.ResetReaderIndex(); using (TNiftyTransport temporaryTransport = new TNiftyTransport( ctx.Channel, duplicateBuffer, message.TransportType)) { TProtocolPair protocolPair = _duplexProtocolFactory.GetProtocolPair( TTransportPair.FromSingleTransport(temporaryTransport)); SendTApplicationExceptionAsync(ex, ctx, message, requestSequenceId, temporaryTransport, protocolPair.InputProtocol, protocolPair.OutputProtocol).GetAwaiter().GetResult(); } } }, TimeSpan.FromMilliseconds(timeRemaining / 10000)); } //SSL 部分暂时不处理 IConnectionContext connectionContext = ctx.Channel.GetConnectionContext(); IRequestContext requestContext = new NiftyRequestContext(connectionContext, inProtocol, outProtocol, messageTransport); RequestContexts.SetCurrentContext(requestContext); try { //关键部分:交给 Thrift 来处理二进制。 bool result = await _processorFactory.GetProcessor(messageTransport).ProcessAsync(inProtocol, outProtocol, requestContext); DeleteExpirationTimer(expireTimeout.Value); try { // Only write response if the client is still there and the task timeout // hasn't expired. if (ctx.Channel.Active && responseSent.CompareAndSet(false, true)) { ThriftMessage response = message.MessageFactory( messageTransport.OutBuffer); await WriteResponseAsync(ctx, response, requestSequenceId, DispatcherContext.IsResponseOrderingRequired(ctx)); } } catch (Exception t) { this.OnDispatchException(ctx, t); } } catch (Exception ex) { _logger.LogError(0, ex, "write response to client fault."); DeleteExpirationTimer(expireTimeout.Value); OnDispatchException(ctx, ex); } } finally { RequestContexts.ClearCurrentContext(); } } catch (Exception ex) { OnDispatchException(ctx, ex); ex.ThrowIfNecessary(); } } catch (Exception ex) { ex.ThrowIfNecessary(); } //}); }
public void Subscribe(ISubscriber <int> subscriber) => subscriber.OnSubscribe(new LamdaSubscription(onRequest: n => { Action signalling = () => { for (var i = 0L; i <= n; i++) { if (_token.IsCancellationRequested) { break; } // one signal too much try { var signal = i; Task.Run(() => { try { subscriber.OnNext((int)signal); } catch (Exception ex) { if (!_swallowOnNextExceptions) { throw new Exception("onNext threw an exception!", ex); } else { // yes, swallow the exception, we're not asserting and they'd just end up being logged (stdout), // which we do not need in this specific PublisherVerificationTest } } }); } catch (Exception ex) { if (ex is Latch.ExpectedOpenLatchException) { if (_concurrentAccessCaused.CompareAndSet(false, true)) { throw new Exception("Concurrent access detected", ex); } } else { if (!_concurrentAccessCaused.Value) { throw; } } } } }; // must be guarded like this in case a Subscriber triggers request() synchronously from it's onNext() while (_startedSignallingThreads.GetAndAdd(1) < MaxSignallingThreads) { Task.Run(signalling, _token); } }));
public void ExecuteBatchIfNotAlreadyStarted() { /* * - check that we only execute once since there's multiple paths to do so (timer, waiting thread or max batch size hit) * - close the gate so 'offer' can no longer be invoked and we turn those threads away so they create a new batch */ if (batchStarted.CompareAndSet(false, true)) { /* wait for 'offer'/'remove' threads to finish before executing the batch so 'requests' is complete */ batchLock.EnterWriteLock(); List <CollapsedRequest <RequestResponseType, RequestArgumentType> > args = new List <CollapsedRequest <RequestResponseType, RequestArgumentType> >(); try { // Check for cancel foreach (var entry in argumentMap) { if (!entry.Value.IsRequestCanceled()) { args.Add(entry.Value); } } // Handle case of null arg submit if (nullArg.Value != null) { var req = nullArg.Value; if (!req.IsRequestCanceled()) { args.Add(req); } } if (args.Count > 0) { // shard batches ICollection <ICollection <ICollapsedRequest <RequestResponseType, RequestArgumentType> > > shards = commandCollapser.DoShardRequests(args); // for each shard execute its requests foreach (ICollection <ICollapsedRequest <RequestResponseType, RequestArgumentType> > shardRequests in shards) { try { // create a new command to handle this batch of requests HystrixCommand <BatchReturnType> command = commandCollapser.DoCreateObservableCommand(shardRequests); BatchReturnType result = command.Execute(); try { commandCollapser.DoMapResponseToRequests(result, shardRequests); } catch (Exception mapException) { // logger.debug("Exception mapping responses to requests.", e); foreach (CollapsedRequest <RequestResponseType, RequestArgumentType> request in args) { try { request.SetExceptionIfResponseNotReceived(mapException); } catch (InvalidOperationException) { // if we have partial responses set in mapResponseToRequests // then we may get InvalidOperationException as we loop over them // so we'll log but continue to the rest // logger.error("Partial success of 'mapResponseToRequests' resulted in InvalidOperationException while setting Exception. Continuing ... ", e2); } } } // check that all requests had setResponse or setException invoked in case 'mapResponseToRequests' was implemented poorly Exception e = null; foreach (var request in shardRequests.OfType <CollapsedRequest <RequestResponseType, RequestArgumentType> >()) { try { e = request.SetExceptionIfResponseNotReceived(e, "No response set by " + commandCollapser.CollapserKey.Name + " 'mapResponseToRequests' implementation."); } catch (InvalidOperationException) { // logger.debug("Partial success of 'mapResponseToRequests' resulted in InvalidOperationException while setting 'No response set' Exception. Continuing ... ", e2); } } } catch (Exception e) { // logger.error("Exception while creating and queueing command with batch.", e); // if a failure occurs we want to pass that exception to all of the Futures that we've returned foreach (var request in shardRequests.OfType <CollapsedRequest <RequestResponseType, RequestArgumentType> >()) { try { request.Exception = e; } catch (InvalidOperationException) { // logger.debug("Failed trying to setException on CollapsedRequest", e2); } } } } } } catch (Exception e) { // logger.error("Exception while sharding requests.", e); // same error handling as we do around the shards, but this is a wider net in case the shardRequest method fails foreach (ICollapsedRequest <RequestResponseType, RequestArgumentType> request in args) { try { request.Exception = e; } catch (InvalidOperationException) { // logger.debug("Failed trying to setException on CollapsedRequest", e2); } } } finally { batchLock.ExitWriteLock(); } } }
protected override ICompletes <TResult> Apply <TResult>(Entity1State state, string metadataValue, string operation, Func <TResult> andThen) { _runsApply.CompareAndSet(false, true); return(base.Apply(state, metadataValue, operation, andThen)); }
/// <summary> /// Run tasks of all phases including and after the given phase. /// </summary> /// <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 once. /// </remarks> public Task <Done> Run(string fromPhase = null) { if (_runStarted.CompareAndSet(false, true)) { var debugEnabled = Log.IsDebugEnabled; Func <List <string>, Task <Done> > loop = null; loop = remainingPhases => { var phase = remainingPhases.FirstOrDefault(); if (phase == null) { return(TaskEx.Completed); } var remaining = remainingPhases.Skip(1).ToList(); Task <Done> phaseResult = null; ImmutableList <Tuple <string, Func <Task <Done> > > > phaseTasks; if (!_tasks.TryGetValue(phase, out 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) { // too early, i.e. triggered by system termination return(result); } else if (result.IsCompleted) { return(TaskEx.Completed); } else if (recoverEnabled) { Log.Warning("Coordinated shutdown phase [{0}] timed out after {1}", phase, timeout); return(TaskEx.Completed); } else { 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()); }; 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); }
/// <summary> /// {@inheritDoc} /// </summary> public virtual bool Enter() { if (Log.isLoggable(PlatformLogger.Level.FINE)) { Log.fine("enter(): blockingEDT=" + KeepBlockingEDT.Get() + ", blockingCT=" + KeepBlockingCT.Get()); } if (!KeepBlockingEDT.CompareAndSet(false, true)) { Log.fine("The secondary loop is already running, aborting"); return(false); } //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final Runnable run = new Runnable() Runnable run = new RunnableAnonymousInnerClassHelper(this); // We have two mechanisms for blocking: if we're on the // dispatch thread, start a new event pump; if we're // on any other thread, call wait() on the treelock Thread currentThread = Thread.CurrentThread; if (currentThread == DispatchThread) { if (Log.isLoggable(PlatformLogger.Level.FINEST)) { Log.finest("On dispatch thread: " + DispatchThread); } if (Interval != 0) { if (Log.isLoggable(PlatformLogger.Level.FINEST)) { Log.finest("scheduling the timer for " + Interval + " ms"); } Timer.Schedule(TimerTask = new TimerTaskAnonymousInnerClassHelper(this), Interval); } // Dispose SequencedEvent we are dispatching on the the current // AppContext, to prevent us from hang - see 4531693 for details SequencedEvent currentSE = KeyboardFocusManager.CurrentKeyboardFocusManager.CurrentSequencedEvent; if (currentSE != null) { if (Log.isLoggable(PlatformLogger.Level.FINE)) { Log.fine("Dispose current SequencedEvent: " + currentSE); } currentSE.Dispose(); } // In case the exit() method is called before starting // new event pump it will post the waking event to EDT. // The event will be handled after the the new event pump // starts. Thus, the enter() method will not hang. // // Event pump should be privileged. See 6300270. AccessController.doPrivileged(new PrivilegedActionAnonymousInnerClassHelper(this, run)); } else { if (Log.isLoggable(PlatformLogger.Level.FINEST)) { Log.finest("On non-dispatch thread: " + currentThread); } lock (TreeLock) { if (Filter != null) { DispatchThread.AddEventFilter(Filter); } try { EventQueue eq = DispatchThread.EventQueue; eq.PostEvent(new PeerEvent(this, run, PeerEvent.PRIORITY_EVENT)); KeepBlockingCT.Set(true); if (Interval > 0) { long currTime = DateTimeHelperClass.CurrentUnixTimeMillis(); while (KeepBlockingCT.Get() && ((ExtCondition != null) ? ExtCondition.Evaluate() : true) && (currTime + Interval > DateTimeHelperClass.CurrentUnixTimeMillis())) { Monitor.Wait(TreeLock, TimeSpan.FromMilliseconds(Interval)); } } else { while (KeepBlockingCT.Get() && ((ExtCondition != null) ? ExtCondition.Evaluate() : true)) { TreeLock.Wait(); } } if (Log.isLoggable(PlatformLogger.Level.FINE)) { Log.fine("waitDone " + KeepBlockingEDT.Get() + " " + KeepBlockingCT.Get()); } } catch (InterruptedException e) { if (Log.isLoggable(PlatformLogger.Level.FINE)) { Log.fine("Exception caught while waiting: " + e); } } finally { if (Filter != null) { DispatchThread.RemoveEventFilter(Filter); } } // If the waiting process has been stopped because of the // time interval passed or an exception occurred, the state // should be changed KeepBlockingEDT.Set(false); KeepBlockingCT.Set(false); } } return(true); }
/// <summary> /// Runs an asynchronous disaster recovery procedure. This procedure recovers this endpoint in case of total or /// partial event loss. Partial event loss means event loss from a given sequence number upwards (for example, /// after having installed a storage backup). Recovery copies events from directly connected remote endpoints back /// to this endpoint and automatically removes invalid snapshots. A snapshot is invalid if it covers events that /// have been lost. /// /// This procedure requires that event replication between this and directly connected endpoints is bi-directional /// and that these endpoints are available during recovery. After successful recovery the endpoint is automatically /// activated. A failed recovery completes with a <see cref="RecoveryException"/> and must be retried. Activating this endpoint /// without having successfully recovered from partial or total event loss may result in inconsistent replica states. /// /// Running a recovery on an endpoint that didn't loose events has no effect but may still fail due to unavailable /// replication partners, for example. In this case, a recovery retry can be omitted if the `partialUpdate` field /// of <see cref="RecoveryException"/> is set to `false`. /// </summary> public async Task Recover() { if (Connections.IsEmpty) { throw new InvalidOperationException("Recover an endpoint without connections"); } if (!isActive.CompareAndSet(false, true)) { throw new InvalidOperationException("Recovery running or endpoint already activated"); } var recovery = new Recovery(this); var partialUpdate = false; try { // Disaster recovery is executed in 3 steps: // 1. synchronize metadata to // - reset replication progress of remote sites and // - determine after disaster progress of remote sites // 2. Recover events from unfiltered links // 3. Recover events from filtered links // 4. Adjust the sequence numbers of local logs to their version vectors // unfiltered links are recovered first to ensure that no events are recovered from a filtered connection // where the causal predecessor is not yet recovered (from an unfiltered connection) // as causal predecessors cannot be written after their successors to the event log. // The sequence number of an event log needs to be adjusted if not all events could be // recovered as otherwise it could be less then the corresponding entriy in the // log's version vector var localEndpointInfo = await recovery.ReadEndpointInfo(); LogLocalState(localEndpointInfo); var recoveryLinks = await recovery.SynchronizeReplicationProgressesWithRemote(localEndpointInfo); partialUpdate = true; var filteredBuilder = ImmutableHashSet.CreateBuilder <RecoveryLink>(); var unfilteredBuilder = ImmutableHashSet.CreateBuilder <RecoveryLink>(); foreach (var link in recoveryLinks) { if (recovery.IsFilteredLink(link)) { filteredBuilder.Add(link); } else { unfilteredBuilder.Add(link); } } var unfilteredLinks = unfilteredBuilder.ToImmutable(); var filteredLinks = filteredBuilder.ToImmutable(); LogLinksToBeRecovered(unfilteredLinks, "unfiltered"); await recovery.RecoverLinks(unfilteredLinks); LogLinksToBeRecovered(filteredLinks, "filtered"); await recovery.RecoverLinks(filteredLinks); await recovery.AdjustEventLogClocks(); Acceptor.Tell(new Acceptor.RecoveryCompleted()); } catch (Exception cause) { throw new RecoveryException(cause, partialUpdate); } }