private async Task BeaconTask() { var observer = new AdHocConnectionStateObserver(OnConnectionStateChanged, OnCompleted); using (zooKeeperClient.OnConnectionStateChanged.Subscribe(observer)) { log.Info( "Registering an instance of application '{Application}' in environment '{Environment}' with id = '{Instance}'.", replicaInfo.Application, replicaInfo.Environment, replicaInfo.Replica); while (isRunning) { var budget = TimeBudget.StartNew(settings.MinimumTimeBetweenIterations); await BeaconTaskIteration().ConfigureAwait(false); if (!budget.HasExpired) { await Task.Delay(budget.Remaining, stopCancellationToken.Token).SilentlyContinue().ConfigureAwait(false); } } } }
public static async Task KillSession(long sessionId, byte[] sessionPassword, IObservable <ConnectionState> onConnectionStateChanged, string connectionString, TimeSpan timeout) { var zooKeeper = new ZooKeeperNetExClient(connectionString, 5000, null, sessionId, sessionPassword); try { var budged = TimeBudget.StartNew(timeout); var observer = new WaitStateObserver(ConnectionState.Expired); onConnectionStateChanged.Subscribe(observer); while (!budged.HasExpired) { if (zooKeeper.getState().Equals(ZooKeeperNetExClient.States.CONNECTED)) { break; } Thread.Sleep(100); } await zooKeeper.closeAsync().ConfigureAwait(false); if (await observer.Signal.Task.TryWaitAsync(budged.Remaining).ConfigureAwait(false)) { return; } throw new TimeoutException($"Expected to kill session within {timeout}, but failed to do so."); } finally { await zooKeeper.closeAsync().ConfigureAwait(false); } }
public async Task <ReplicaResult> SendToReplicaAsync( ITransport transport, IReplicaOrdering replicaOrdering, Uri replica, Request request, int connectionAttempts, TimeSpan?connectionTimeout, TimeSpan timeout, CancellationToken cancellationToken) { if (configuration.Logging.LogReplicaRequests) { LogRequest(replica, timeout); } var timeBudget = TimeBudget.StartNew(timeout, TimeSpan.FromMilliseconds(1)); var absoluteRequest = requestConverter.TryConvertToAbsolute(request, replica); var response = await SendRequestAsync(transport, absoluteRequest, timeBudget, connectionAttempts, connectionTimeout, cancellationToken).ConfigureAwait(false); var responseVerdict = responseClassifier.Decide(response, configuration.ResponseCriteria); var result = new ReplicaResult(replica, response, responseVerdict, timeBudget.Elapsed); if (configuration.Logging.LogReplicaResults) { LogResult(result); } replicaOrdering.Learn(result, storageProvider); return(result); }
public async Task <ZooKeeperNetExClient> GetConnectedClient() { var budget = TimeBudget.StartNew(settings.Timeout); while (!budget.HasExpired) { if (!ResetClientIfNeeded(state)) { return(null); } var currentState = state; if (currentState == null) { return(null); } if (currentState.IsConnected) { return(currentState.Client); } if (!await currentState.NextState.Task.WaitAsync(budget.Remaining).ConfigureAwait(false)) { return(null); } } return(null); }
public RequestInfo(TimeSpan requestTimeout, RequestPriority requestPriority, string clientApplicationIdentity, IPAddress clientIpAddress) { Timeout = requestTimeout; Budget = TimeBudget.StartNew(Timeout.Cut(100.Milliseconds(), 0.05)); Priority = requestPriority; ClientApplicationIdentity = clientApplicationIdentity; ClientIpAddress = clientIpAddress; }
public void StartNew_should_produce_a_budget_that_will_eventually_expire() { var budget = TimeBudget.StartNew(250.Milliseconds()); budget.HasExpired.Should().BeFalse(); new Action(() => budget.HasExpired.Should().BeFalse()) .ShouldPassIn(10.Seconds()); }
public ClientHolderState WithConnectionState(ConnectionState newConnectionState, ZooKeeperClientSettings settings) => new ClientHolderState( IsSuspended, newConnectionState.IsConnected(settings.CanBeReadOnly), lazyClient, ConnectionWatcher, newConnectionState, ConnectionString, TimeBudget.StartNew(settings.Timeout));
public void StartNew_should_produce_a_running_budget() { var budget = TimeBudget.StartNew(10.Seconds()); var remainingBefore = budget.Remaining; Thread.Sleep(1); budget.Remaining.Should().BeLessThan(remainingBefore); }
private Task ShutdownBeaconAsync(TimeSpan timeout) { var budget = TimeBudget.StartNew(timeout); // (iloktionov): No reason to wait for deregistration if serviceBeacon.Stop() call has failed. if (!StopServiceBeacon(budget)) { return(Task.CompletedTask); } return(beaconWaitEnabled ? WaitForBeaconShutdownAsync(budget) : Task.CompletedTask); }
public void Should_work_with_suspended_state() { var state = ClientHolderState.CreateSuspended(TimeBudget.StartNew(9.Seconds())); state.ConnectionState.Should().Be(ConnectionState.Disconnected); state.ConnectionWatcher.Should().BeNull(); state.TimeBeforeReset.Total.Should().Be(9.Seconds()); state.IsSuspended.Should().BeTrue(); state.IsConnected.Should().BeFalse(); state.ConnectionString.Should().BeNull(); state.Dispose(); }
public void Initiate(TimeSpan remainingTotalBudget) { log.Info("Application shutdown has been initiated. Timeout = {ApplicationShutdownTimeout}.", remainingTotalBudget.ToPrettyString()); // (iloktionov): Start the shutdown budget so that ShutdownTimeout property will return actual remaining time from here on. Interlocked.Exchange(ref budget, TimeBudget.StartNew(remainingTotalBudget)); // (iloktionov): Complete the task to notify VostokHost without relying on ShutdownToken callbacks. taskSource.TrySetResult(true); // (iloktionov): Protect against synchronous execution of arbitrary user callbacks. Task.Run(() => tokenSource.Cancel()); }
public TimeBudget GetNextDelay() { if (backoffDepth < 0 || periodCap == TimeSpan.Zero) { return(null); } var delayMs = Math.Min(periodCap.TotalMilliseconds, period.TotalMilliseconds * Math.Pow(2, backoffDepth)); delayMs *= ThreadSafeRandom.NextDouble(); return(TimeBudget.StartNew(delayMs.Milliseconds())); }
public static ClientHolderState CreateActive( Lazy <ZooKeeperNetExClient> client, IConnectionWatcher connectionWatcher, ConnectionState connectionState, string connectionString, ZooKeeperClientSettings settings) => new ClientHolderState( false, connectionState.IsConnected(settings.CanBeReadOnly), client, connectionWatcher, connectionState, connectionString, TimeBudget.StartNew(settings.Timeout));
public async Task <Response> SendAsync(Request request, TimeSpan?connectionTimeout, TimeSpan timeout, CancellationToken cancellationToken) { var timeBudget = TimeBudget.StartNew(timeout, TimeSpan.FromMilliseconds(1)); for (var attempt = 1; attempt <= connectionAttempts; ++attempt) { var connectionAttemptTimeout = connectionTimeout == null || timeBudget.Remaining < connectionTimeout ? (TimeSpan?)null : connectionTimeout.Value; var response = await transport.SendAsync(request, connectionAttemptTimeout, timeBudget.Remaining, cancellationToken).ConfigureAwait(false); if (response.Code == ResponseCode.ConnectFailure) { continue; } return(response); } return(new Response(ResponseCode.ConnectFailure)); }
public async Task <IReadOnlyList <T> > LoadAllAsync(TimeSpan?timeout = null) { var timeBudget = timeout == null ? null : TimeBudget.StartNew(timeout.Value); var data = new List <T>(); var pageIndex = 0L; while (true) { var page = await LoadPageAsync(pageIndex, timeBudget?.Remaining).ConfigureAwait(false); if (!page.IsEmpty) { pageIndex++; data.AddRange(page); } if (page.IsEmpty || page.IsLast) { break; } } return(data); }
public async Task <IReadOnlyList <T> > LoadAllAsync(TimeSpan?timeout = null) { var timeBudget = timeout == null ? null : TimeBudget.StartNew(timeout.Value); var data = new List <T>(); var skip = 0L; while (true) { var slice = await LoadSliceAsync(skip, sliceSize, timeBudget?.Remaining).ConfigureAwait(false); if (slice.Items.Count > 0) { skip += sliceSize; data.AddRange(slice.Items); } if (!slice.HasNextSlice) { break; } } return(data); }
public WebRequestState(TimeSpan timeout) { budget = TimeBudget.StartNew(timeout, TimeSpan.FromMilliseconds(5)); }
private async Task ExecutePayloadAsync(DateTimeOffset executionTime, IScheduler scheduler, CancellationToken token) { if (token.IsCancellationRequested) { return; } var nextExecution = GetNextExecutionTime(executionTime).time; var timeBudget = nextExecution.HasValue ? TimeBudget.StartNew(TimeSpanArithmetics.Max(TimeSpan.Zero, nextExecution.Value - executionTime)) : TimeBudget.Infinite; var context = new ScheduledActionContext(executionTime, timeBudget, scheduler, token); if (!(scheduler is PeriodicalWithConstantPauseScheduler)) { log.Info("Executing with time budget = {TimeBudget}.", timeBudget.Total.ToPrettyString()); } else { log.Info("Executing.."); } async Task ExecutePayload() { monitor.OnIterationStarted(); try { var watch = Stopwatch.StartNew(); await action.Payload(context); watch.Stop(); log.Info( "Executed in {ExecutionTime}.", new { ExecutionTime = watch.Elapsed.ToPrettyString(), ExecutionTimeMs = watch.Elapsed.TotalMilliseconds }); if (watch.Elapsed > timeBudget.Total && !(scheduler is PeriodicalWithConstantPauseScheduler)) { log.Warn("Execution did not fit into the time budget before the next planned execution."); } action.Scheduler.OnSuccessfulIteration(scheduler); monitor.OnIterationSucceeded(); } catch (Exception error) { action.Scheduler.OnFailedIteration(scheduler, error); monitor.OnIterationFailed(error); if (action.Options.CrashOnPayloadException || error is OperationCanceledException) { throw; } log.Error(error, "Scheduled action threw an exception."); } finally { monitor.OnIterationCompleted(); } } var payloadTask = action.Options.PreferSeparateThread ? Task.Factory.StartNew(ExecutePayload, TaskCreationOptions.LongRunning) : Task.Run(ExecutePayload); if (action.Options.AllowOverlappingExecution) { return; } await payloadTask; }
public void TryAcquire_should_be_limited_by_remaining_time() { var budget = TimeBudget.StartNew(250.Milliseconds()); budget.TryAcquire(1.Seconds()).Should().BeLessOrEqualTo(250.Milliseconds()); }