public async Task <UserAndMetaData> GetUserAsync(string username, GetUserOptions?options = null) { options ??= GetUserOptions.Default; var uri = GetUsersUri(options.DomainNameValue, username); _logger.LogInformation("Attempting to get user with username {username} - {uri}", _redactor.UserData(username), _redactor.SystemData(uri)); try { // check user exists before trying to read content var result = await _client.GetAsync(uri, options.TokenValue).ConfigureAwait(false); if (result.StatusCode == HttpStatusCode.NotFound) { throw new UserNotFoundException(username); } result.EnsureSuccessStatusCode(); // get user from result var json = JObject.Parse(await result.Content.ReadAsStringAsync().ConfigureAwait(false)); return(UserAndMetaData.FromJson(json)); } catch (Exception exception) { _logger.LogError(exception, "Error trying to get user with username {username} - {uri}", _redactor.UserData(username), _redactor.SystemData(uri)); throw; } }
async Task IBootstrappable.BootStrapAsync() { try { await _context.BootstrapGlobalAsync().ConfigureAwait(false); //if we succeeded set the state of the cluster to bootstrapped _hasBootStrapped = _context.GlobalConfig != null; _deferredExceptions.Clear(); UpdateClusterCapabilities(); } catch (AuthenticationFailureException e) { _deferredExceptions.Add(e); //auth failed so bubble up exception and clean up resources _logger.LogError(e, "Could not authenticate user {username}", _redactor.UserData(_context.ClusterOptions.UserName ?? string.Empty)); _context.RemoveAllNodes(); throw; } catch (Exception e) { _logger.LogDebug("Error encountered bootstrapping cluster; if the cluster is 6.5 or earlier, this can be ignored. {exception}.", e); } }
protected virtual void CreateBackingStore() { if (BackingStoreChecked) { return; } try { Collection.InsertAsync(Key, new List <TValue>()).GetAwaiter().GetResult(); BackingStoreChecked = true; } catch (DocumentExistsException e) { //ignore - the doc already exists for this collection Logger?.LogTrace(e, "The PersistentList backing document already exists for ID {key}. Not an error.", Redactor?.UserData(Key)); } }
protected virtual async ValueTask CreateBackingStoreAsync() { if (BackingStoreChecked) { return; } try { // Typecast to IList<TValue> to provide a consistent type which can be registered // by the consumer on their JsonSerializerContext await Collection.InsertAsync(Key, (IList <TValue>) new List <TValue>()).ConfigureAwait(false); BackingStoreChecked = true; } catch (DocumentExistsException e) { //ignore - the doc already exists for this collection Logger?.LogTrace(e, "The PersistentList backing document already exists for ID {key}. Not an error.", Redactor?.UserData(Key)); } }
public async Task <T> RetryAsync <T>(Func <Task <T> > send, IRequest request) where T : IServiceResult { var token = request.Token; if (token == CancellationToken.None) { var cts = new CancellationTokenSource(request.Timeout); token = cts.Token; } //for measuring the capped duration var stopwatch = new Stopwatch(); stopwatch.Start(); var backoff = ControlledBackoff.Create(); do { if (token.IsCancellationRequested) { if (request.Idempotent) { UnambiguousTimeoutException.ThrowWithRetryReasons(request); } AmbiguousTimeoutException.ThrowWithRetryReasons(request); } try { var result = await send().ConfigureAwait(false); var reason = result.RetryReason; if (reason == RetryReason.NoRetry) { return(result); } if (reason.AlwaysRetry()) { _logger.LogDebug( "Retrying query {clientContextId}/{statement} because {reason}.", request.ClientContextId, _redactor.UserData(request.Statement), reason); await backoff.Delay(request).ConfigureAwait(false); request.IncrementAttempts(reason); continue; } var strategy = request.RetryStrategy; var action = strategy.RetryAfter(request, reason); if (action.Retry) { _logger.LogDebug(LoggingEvents.QueryEvent, "Retrying query {clientContextId}/{statement} because {reason}.", request.ClientContextId, request.Statement, reason); var duration = action.DurationValue; if (duration.HasValue) { _logger.LogDebug( "Timeout for {clientContextId} is {timeout} and duration is {duration} and elapsed is {elapsed}", request.ClientContextId, request.Timeout.TotalMilliseconds, duration.Value.TotalMilliseconds, stopwatch.ElapsedMilliseconds); var cappedDuration = request.Timeout.CappedDuration(duration.Value, stopwatch.Elapsed); _logger.LogDebug( "Timeout for {clientContextId} capped duration is {duration} and elapsed is {elapsed}", request.ClientContextId, cappedDuration.TotalMilliseconds, stopwatch.ElapsedMilliseconds); await Task.Delay(cappedDuration, CancellationTokenSource.CreateLinkedTokenSource(token).Token).ConfigureAwait(false); request.IncrementAttempts(reason); //temp fix for query unit tests if (request.Attempts > 9) { if (request.Idempotent) { UnambiguousTimeoutException.ThrowWithRetryReasons(request, new InvalidOperationException($"Too many retries: {request.Attempts}.")); } AmbiguousTimeoutException.ThrowWithRetryReasons(request, new InvalidOperationException($"Too many retries: {request.Attempts}.")); } } else { if (request.Idempotent) { UnambiguousTimeoutException.ThrowWithRetryReasons(request); } AmbiguousTimeoutException.ThrowWithRetryReasons(request); } } } catch (TaskCanceledException _) { _logger.LogDebug("Request was canceled after {elapsed}.", stopwatch.ElapsedMilliseconds); //timed out while waiting if (request.Idempotent) { UnambiguousTimeoutException.ThrowWithRetryReasons(request, _); } AmbiguousTimeoutException.ThrowWithRetryReasons(request, _); } } while (true); }
public string Baseline() { return(_redactor.UserData("test").ToString()); }
public object Baseline() { return(_redactor.UserData("test")); }