示例#1
0
        private bool RepairManagerServiceExists()
        {
            var serviceList = retryPolicyFactory.Create().Execute(
                () =>
            {
                ServiceList list = this.fabricClient.QueryManager
                                   .GetServiceListAsync(FabricSystemApplicationUri, FabricRepairManagerServiceUri)
                                   .GetAwaiter()
                                   .GetResult();
                return(list);
            },
                "GetServiceListAsync");

            this.environment.DefaultTraceType.WriteInfo(
                "GetServiceList('{0}','{1}'): result count = {2}",
                FabricSystemApplicationUri,
                FabricRepairManagerServiceUri,
                serviceList.Count);

            return(serviceList.Count > 0);
        }
示例#2
0
        /// <inheritdoc />
        public IRetryPolicy RequestPolicy()
        {
            return(UseTrackingSyncLock(
                       () =>
            {
                _callStackDepth++;

                IRetryPolicy retryPolicy;
                if (_callStackDepth == 1)
                {
                    var exceptionPredicates = GetExceptionPredicates();
                    retryPolicy = _retryPolicyFactory.Create(exceptionPredicates);
                }
                else
                {
                    retryPolicy = _noOpFactory();
                }

                return new ObservableRetryPolicyDecorator(retryPolicy, afterExecute: DecrementCallStackDepth);
            }));
        }
        public async Task UpdateValueAsync(Guid activityId, string keyName, string versionKeyName, string value)
        {
            keyName.Validate("keyName");
            versionKeyName.Validate("versionKeyName");
            value.Validate("value");

            traceType.WriteInfo(
                "Starting VersionedPropertyStore.UpdateValueAsync for store name '{0}'. Key: {1}, VersionKey: {2}, Value: {3}, ActivityId: {4}",
                storeName, keyName, versionKeyName, value, activityId);

            Int64 oldVersion, newVersion;

            lock (locker)
            {
                oldVersion = cachedProperties.ContainsKey(keyName) ? cachedProperties[keyName].Version : -1;
                newVersion = oldVersion + 1;
            }

            // Publish the new values to the naming service, including retrieving the new values
            // to update the local cache
            var propertyOperations = new List <PropertyBatchOperation>
            {
                // operation index
                new CheckValuePropertyOperation(versionKeyName, oldVersion), // 0
                new PutPropertyOperation(versionKeyName, newVersion),        // 1
                new GetPropertyOperation(versionKeyName, true),              // 2
                new PutPropertyOperation(keyName, value),                    // 3
                new GetPropertyOperation(keyName, true)                      // 4
            };

            try
            {
                IPropertyBatchResultWrapper result = await retryPolicyFactory.Create().ExecuteAsync(
                    async token => await propertyManager.SubmitPropertyBatchAsync(
                        storeName,
                        propertyOperations).ConfigureAwait(false),
                    "SubmitPropertyBatchAsync",
                    CancellationToken.None).ConfigureAwait(false);

                traceType.WriteInfo(
                    "SubmitPropertyBatchAsync returned result.FailedOperationIndex: {0}, result.FailedOperationException: {1}, ActivityId: {2}",
                    result.FailedOperationIndex,
                    result.FailedOperationException != null ? result.FailedOperationException.ToString() : "<null>",
                    activityId);

                switch (result.FailedOperationIndex)
                {
                case -1:
                    lock (locker)
                    {
                        // Note; these indexes are tied to the batch operation array above. If you adjust
                        // the set of operations, need to update these offsets accordingly
                        Int64  version  = result.GetProperty(2).GetValue <Int64>();
                        string newValue = result.GetProperty(4).GetValue <string>();

                        var updatedVkv = UpdateCache(activityId, new VersionedKeyValue(keyName, versionKeyName, newValue, version));

                        string message =
                            "UpdateValueAsync successful for store name '{0}'. New cached value: {1}, ActivityId: {2}"
                            .ToString(storeName, updatedVkv, activityId);

                        traceType.WriteInfo(message);
                    }
                    break;

                case 0:
                {
                    // optimistic concurrency check failed (someone else has updated the version in the remote store)
                    string message =
                        ("UpdateValueAsync policy failed for store name '{0}' due to version mismatch. This is because of a possible write conflict. Please retry. " +
                         "Key: {1}, VersionKey: {2}, ActivityId: {3}").ToString(
                            storeName,
                            keyName,
                            versionKeyName,
                            activityId);
                    traceType.WriteInfo(message);

                    // get the latest version so that the cache is updated
                    await GetValueAsync(activityId, keyName, versionKeyName, false).ConfigureAwait(false);

                    throw new FabricException(message, FabricErrorCode.WriteConflict);
                }

                default:
                {
                    string message =
                        "UpdateValueAsync policy failed for store name '{0}'. " +
                        "Key: {1}, VersionKey: {2}, ActivityId: {3}".ToString(
                            storeName,
                            keyName,
                            versionKeyName,
                            activityId);
                    var ex = result.FailedOperationException ?? new FabricException(message, FabricErrorCode.Unknown);
                    throw ex;
                }
                }
            }
            catch (Exception ex)
            {
                string message =
                    "Error while updating key for store name '{0}'. Key: {1}, VersionKey: {2}, Value: {3}, ActivityId: {4}, Error: {5}"
                    .ToString(storeName, keyName, versionKeyName, value, activityId, ex);
                traceType.WriteInfo(message);
                throw;
            }
        }
        private async Task <ScannerInformation> CreateScannerAsyncInternal(string tableName, Scanner scannerSettings, string alternativeEndpointBase = null)
        {
            tableName.ArgumentNotNullNorEmpty("tableName");
            scannerSettings.ArgumentNotNull("scannerSettings");

            while (true)
            {
                IRetryPolicy retryPolicy = _retryPolicyFactory.Create();
                try
                {
                    using (HttpWebResponse response = await PostRequestAsync(tableName + "/scanner", scannerSettings, alternativeEndpointBase ?? Constants.RestEndpointBaseZero))
                    {
                        if (response.StatusCode != HttpStatusCode.Created)
                        {
                            using (var output = new StreamReader(response.GetResponseStream()))
                            {
                                string message = output.ReadToEnd();
                                throw new WebException(
                                          string.Format(
                                              "Couldn't create a scanner for table {0}! Response code was: {1}, expected 201! Response body was: {2}",
                                              tableName,
                                              response.StatusCode,
                                              message));
                            }
                        }
                        string location = response.Headers.Get("Location");
                        if (location == null)
                        {
                            throw new ArgumentException("Couldn't find header 'Location' in the response!");
                        }
                        return(new ScannerInformation(new Uri(location), tableName));
                    }
                }
                catch (Exception e)
                {
                    if (!retryPolicy.ShouldRetryAttempt(e))
                    {
                        throw;
                    }
                }
            }
        }