/// <summary>
        /// Returns the proper ShardManagementException corresponding to given error code in
        /// <paramref name="result"/> for ShardMap operations.
        /// </summary>
        /// <param name="result">Operation result object.</param>
        /// <param name="shardMap">Shard map object.</param>
        /// <param name="location">Location of operation.</param>
        /// <param name="errorCategory">Error category to use for raised errors.</param>
        /// <param name="operationName">Operation being performed.</param>
        /// <param name="storedProcName">Stored procedure being executed.</param>
        /// <returns>ShardManagementException to be raised.</returns>
        internal static ShardManagementException OnRecoveryErrorLocal(
            IStoreResults result,
            IStoreShardMap shardMap,
            ShardLocation location,
            ShardManagementErrorCategory errorCategory,
            string operationName,
            string storedProcName)
        {
            switch (result.Result)
            {
            case StoreResult.ShardMapDoesNotExist:
                return(new ShardManagementException(
                           errorCategory,
                           ShardManagementErrorCode.ShardMapDoesNotExist,
                           Errors._Store_ShardMap_DoesNotExistLocal,
                           shardMap.Name,
                           location,
                           storedProcName,
                           operationName));

            case StoreResult.StoreVersionMismatch:
            case StoreResult.MissingParametersForStoredProcedure:
            default:
                return(StoreOperationErrorHandler.OnCommonErrorLocal(
                           result,
                           location,
                           operationName,
                           storedProcName));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Handles errors from the GSM operation after the LSM operations.
        /// </summary>
        /// <param name="result">Operation result.</param>
        public override void HandleDoGlobalExecuteError(IStoreResults result)
        {
            if (result.Result == StoreResult.ShardMapDoesNotExist)
            {
                // Remove shard map from cache.
                _shardMapManager.Cache.DeleteShardMap(_shardMap);
            }

            if (result.Result == StoreResult.MappingDoesNotExist)
            {
                Debug.Assert(_mapping != null);

                // Remove mapping from cache.
                _shardMapManager.Cache.DeleteMapping(_mapping);
            }

            // Possible errors are:
            // StoreResult.ShardMapDoesNotExist
            // StoreResult.MappingDoesNotExist
            // StoreResult.MappingAlreadyLocked
            // StoreResult.MappingLockOwnerIdMismatch
            // StoreResult.StoreVersionMismatch
            // StoreResult.MissingParametersForStoredProcedure
            throw StoreOperationErrorHandler.OnShardMapperErrorGlobal(
                      result,
                      _shardMap,
                      _mapping == null ? null : _mapping.StoreShard,
                      _errorCategory,
                      this.OperationName,
                      StoreOperationRequestBuilder.SpLockOrUnLockShardMappingsGlobal);
        }
        /// <summary>
        /// Returns the proper ShardManagementException corresponding to given error code in
        /// <paramref name="result"/> for ShardMapper operations.
        /// </summary>
        /// <param name="result">Operation result object.</param>
        /// <param name="shardMapName">Name of shard map.</param>
        /// <param name="operationName">Operation being performed.</param>
        /// <param name="storedProcName">Stored procedure being executed.</param>
        /// <returns></returns>
        internal static ShardManagementException OnShardSchemaInfoErrorGlobal(
            IStoreResults result,
            string shardMapName,
            string operationName,
            string storedProcName)
        {
            switch (result.Result)
            {
            case StoreResult.SchemaInfoNameConflict:
                throw new SchemaInfoException(
                          SchemaInfoErrorCode.SchemaInfoNameConflict,
                          Errors._Store_SchemaInfo_NameConflict,
                          shardMapName);

            case StoreResult.SchemaInfoNameDoesNotExist:
                throw new SchemaInfoException(
                          SchemaInfoErrorCode.SchemaInfoNameDoesNotExist,
                          Errors._Store_SchemaInfo_NameDoesNotExist,
                          operationName,
                          shardMapName);

            case StoreResult.StoreVersionMismatch:
            case StoreResult.MissingParametersForStoredProcedure:
            default:
                return(StoreOperationErrorHandler.OnCommonErrorGlobal(
                           result,
                           operationName,
                           storedProcName));
            }
        }
        /// <summary>
        /// Handles errors from the initial GSM operation prior to LSM operations.
        /// </summary>
        /// <param name="result">Operation result.</param>
        public override void HandleDoGlobalPreLocalExecuteError(IStoreResults result)
        {
            if (result.Result == StoreResult.ShardMapDoesNotExist)
            {
                // Remove shard map from cache.
                this.Manager.Cache.DeleteShardMap(_shardMap);
            }

            if (result.Result == StoreResult.MappingDoesNotExist)
            {
                // Remove mapping from cache.
                this.Manager.Cache.DeleteMapping(_mappingSource);
            }

            // Possible errors are:
            // StoreResult.ShardMapDoesNotExist
            // StoreResult.ShardDoesNotExist
            // StoreResult.MappingRangeAlreadyMapped
            // StoreResult.MappingDoesNotExist
            // StoreResult.MappingLockOwnerIdDoesNotMatch
            // StoreResult.MappingIsNotOffline
            // StoreResult.StoreVersionMismatch
            // StoreResult.MissingParametersForStoredProcedure
            throw StoreOperationErrorHandler.OnShardMapperErrorGlobal(
                      result,
                      _shardMap,
                      result.Result == StoreResult.MappingRangeAlreadyMapped ?
                      _mappingTarget.StoreShard :
                      _mappingSource.StoreShard,
                      _errorCategory,
                      StoreOperationErrorHandler.OperationNameFromStoreOperationCode(this.OperationCode),
                      StoreOperationRequestBuilder.SpBulkOperationShardMappingsGlobalBegin);
        }
 /// <summary>
 /// Handles errors from the GSM operation after the LSM operations.
 /// </summary>
 /// <param name="result">Operation result.</param>
 public override void HandleDoGlobalExecuteError(IStoreResults result)
 {
     if (_ssmCurrent == null)
     {
         // Possible errors are:
         // StoreResult.StoreVersionMismatch
         // StoreResult.MissingParametersForStoredProcedure
         throw StoreOperationErrorHandler.OnShardMapManagerErrorGlobal(
                   result,
                   null,
                   this.OperationName,
                   StoreOperationRequestBuilder.SpGetAllShardMapsGlobal);
     }
     else
     {
         if (result.Result != StoreResult.ShardMapDoesNotExist)
         {
             // Possible errors are:
             // StoreResult.StoreVersionMismatch
             // StoreResult.MissingParametersForStoredProcedure
             throw StoreOperationErrorHandler.OnShardMapperErrorGlobal(
                       result,
                       _ssmCurrent,
                       null, // shard
                       ShardManagementErrorCategory.ShardMapManager,
                       this.OperationName,
                       StoreOperationRequestBuilder.SpGetAllShardMappingsGlobal);
         }
     }
 }
        /// <summary>
        /// Handles errors from the the LSM operation on the source shard.
        /// </summary>
        /// <param name="result">Operation result.</param>
        public override void HandleDoLocalSourceExecuteError(IStoreResults result)
        {
            // Possible errors from spUpdateShardLocal:
            // StoreResult.StoreVersionMismatch
            // StoreResult.MissingParametersForStoredProcedure
            // StoreResult.ShardDoesNotExist
            switch (result.Result)
            {
            case StoreResult.StoreVersionMismatch:
            case StoreResult.MissingParametersForStoredProcedure:
            case StoreResult.ShardDoesNotExist:
                throw StoreOperationErrorHandler.OnShardMapErrorLocal(
                          result,
                          _shardMap,
                          _shard.Location,
                          ShardManagementErrorCategory.ShardMap,
                          StoreOperationErrorHandler.OperationNameFromStoreOperationCode(this.OperationCode),
                          StoreOperationRequestBuilder.SpUpdateShardLocal);

            default:
                throw new ShardManagementException(
                          ShardManagementErrorCategory.ShardMapManager,
                          ShardManagementErrorCode.StorageOperationFailure,
                          Errors._Store_SqlExceptionLocal,
                          OperationName
                          );
            }
        }
Esempio n. 7
0
 /// <summary>
 /// Handles errors from the undo of GSM operation prior to LSM operations.
 /// </summary>
 /// <param name="result">Operation result.</param>
 public virtual void HandleUndoGlobalPreLocalExecuteError(IStoreResults result)
 {
     // Possible errors are:
     // StoreResult.StoreVersionMismatch
     // StoreResult.MissingParametersForStoredProcedure
     throw StoreOperationErrorHandler.OnCommonErrorGlobal(
               result,
               StoreOperationErrorHandler.OperationNameFromStoreOperationCode(this.OperationCode),
               StoreOperationRequestBuilder.SpFindAndUpdateOperationLogEntryByIdGlobal);
 }
 /// <summary>
 /// Handles errors from the GSM operation after the LSM operations.
 /// </summary>
 /// <param name="result">Operation result.</param>
 public override void HandleDoGlobalExecuteError(IStoreResults result)
 {
     // Expected errors are:
     // StoreResult.MissingParametersForStoredProcedure:
     // StoreResult.StoreVersionMismatch:
     throw StoreOperationErrorHandler.OnShardSchemaInfoErrorGlobal(
               result,
               "*",
               this.OperationName,
               StoreOperationRequestBuilder.SpGetAllShardingSchemaInfosGlobal);
 }
 /// <summary>
 /// Performs undo of LSM operation on the target shard.
 /// </summary>
 /// <param name="result">Operation result.</param>
 public override void HandleUndoLocalTargetExecuteError(IStoreResults result)
 {
     // Possible errors are:
     // StoreResult.StoreVersionMismatch
     // StoreResult.MissingParametersForStoredProcedure
     throw StoreOperationErrorHandler.OnShardMapperErrorLocal(
               result,
               _mappingSource.StoreShard.Location,
               StoreOperationErrorHandler.OperationNameFromStoreOperationCode(this.OperationCode),
               StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal);
 }
 /// <summary>
 /// Handles errors from the GSM operation after the LSM operations.
 /// </summary>
 /// <param name="result">Operation result.</param>
 public override void HandleDoGlobalExecuteError(IStoreResults result)
 {
     // Possible errors are:
     // StoreResult.StoreVersionMismatch
     // StoreResult.MissingParametersForStoredProcedure
     throw StoreOperationErrorHandler.OnShardMapManagerErrorGlobal(
               result,
               null,
               this.OperationName,
               StoreOperationRequestBuilder.SpFindShardMapByNameGlobal);
 }
Esempio n. 11
0
 /// <summary>
 /// Handles errors from the undo of LSM operation on the source shard.
 /// </summary>
 /// <param name="result">Operation result.</param>
 public override void HandleUndoLocalSourceExecuteError(IStoreResults result)
 {
     // Possible errors are:
     // StoreResult.StoreVersionMismatch
     // StoreResult.MissingParametersForStoredProcedure
     throw StoreOperationErrorHandler.OnShardMapErrorLocal(
               result,
               _shardMap,
               _shard.Location,
               ShardManagementErrorCategory.ShardMap,
               StoreOperationErrorHandler.OperationNameFromStoreOperationCode(this.OperationCode),
               StoreOperationRequestBuilder.SpAddShardLocal);
 }
 /// <summary>
 /// Handles errors from the GSM operation after the LSM operations.
 /// </summary>
 /// <param name="result">Operation result.</param>
 public override void HandleDoGlobalExecuteError(IStoreResults result)
 {
     // Possible errors are:
     // StoreResult.StoreVersionMismatch
     // StoreResult.MissingParametersForStoredProcedure
     throw StoreOperationErrorHandler.OnRecoveryErrorGlobal(
               result,
               null,
               null,
               ShardManagementErrorCategory.Recovery,
               this.OperationName,
               StoreOperationRequestBuilder.SpDetachShardGlobal);
 }
Esempio n. 13
0
 /// <summary>
 /// Handles errors from the LSM operation.
 /// </summary>
 /// <param name="result">Operation result.</param>
 public override void HandleDoLocalExecuteError(IStoreResults result)
 {
     // Possible errors are:
     // StoreResult.StoreVersionMismatch
     // StoreResult.MissingParametersForStoredProcedure
     throw StoreOperationErrorHandler.OnRecoveryErrorLocal(
               result,
               _shardMap,
               this.Location,
               ShardManagementErrorCategory.Recovery,
               this.OperationName,
               StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal);
 }
Esempio n. 14
0
 /// <summary>
 /// Handles errors from the undo of LSM operation on the source shard.
 /// </summary>
 /// <param name="result">Operation result.</param>
 public override void HandleUndoLocalSourceExecuteError(IStoreResults result)
 {
     // Possible errors are:
     // StoreResult.StoreVersionMismatch
     // StoreResult.MissingParametersForStoredProcedure
     // Stored procedure can also return StoreResult.ShardDoesNotExist, but only for AttachShard operations
     throw StoreOperationErrorHandler.OnShardMapErrorLocal(
               result,
               _shardMap,
               _shardNew.Location,
               ShardManagementErrorCategory.ShardMap,
               StoreOperationErrorHandler.OperationNameFromStoreOperationCode(this.OperationCode),
               StoreOperationRequestBuilder.SpUpdateShardLocal);
 }
 /// <summary>
 /// Handles errors from the GSM operation after the LSM operations.
 /// </summary>
 /// <param name="result">Operation result.</param>
 public override void HandleDoGlobalExecuteError(IStoreResults result)
 {
     // SchemaInfoNameDoesNotExist is handled by the callers i.e. Get vs TryGet.
     if (result.Result != StoreResult.SchemaInfoNameDoesNotExist)
     {
         // Expected errors are:
         // StoreResult.MissingParametersForStoredProcedure:
         // StoreResult.StoreVersionMismatch:
         throw StoreOperationErrorHandler.OnShardSchemaInfoErrorGlobal(
                   result,
                   _schemaInfoName,
                   this.OperationName,
                   StoreOperationRequestBuilder.SpFindShardingSchemaInfoByNameGlobal);
     }
 }
 /// <summary>
 /// Handles errors from the GSM operation after the LSM operations.
 /// </summary>
 /// <param name="result">Operation result.</param>
 public override void HandleDoGlobalExecuteError(IStoreResults result)
 {
     if (result.Result != StoreResult.ShardMapDoesNotExist)
     {
         // Possible errors are:
         // StoreResult.ShardMapHasShards
         // StoreResult.StoreVersionMismatch
         // StoreResult.MissingParametersForStoredProcedure
         throw StoreOperationErrorHandler.OnShardMapManagerErrorGlobal(
                   result,
                   _shardMap,
                   this.OperationName,
                   StoreOperationRequestBuilder.SpRemoveShardMapGlobal);
     }
 }
Esempio n. 17
0
 /// <summary>
 /// Handles errors from the GSM operation after the LSM operations.
 /// </summary>
 /// <param name="result">Operation result.</param>
 public override void HandleDoGlobalExecuteError(IStoreResults result)
 {
     // Possible errors are:
     // StoreResult.ShardMapDoesNotExist
     // StoreResult.MappingDoesNotExist
     // StoreResult.StoreVersionMismatch
     // StoreResult.MissingParametersForStoredProcedure
     throw StoreOperationErrorHandler.OnShardMapperErrorGlobal(
               result,
               _shardMap,
               _mapping.StoreShard, // shard
               _errorCategory,
               this.OperationName,
               StoreOperationRequestBuilder.SpFindShardMappingByIdGlobal);
 }
 /// <summary>
 /// Returns the proper ShardManagementException corresponding to given error code in
 /// <paramref name="result"/> for ShardMapper operations.
 /// </summary>
 /// <param name="result">Operation result object.</param>
 /// <param name="location">Location of LSM operation.</param>
 /// <param name="operationName">Operation being performed.</param>
 /// <param name="storedProcName">Stored procedure being executed.</param>
 /// <returns>ShardManagementException to be raised.</returns>
 internal static ShardManagementException OnShardMapperErrorLocal(
     IStoreResults result,
     ShardLocation location,
     string operationName,
     string storedProcName)
 {
     switch (result.Result)
     {
     case StoreResult.StoreVersionMismatch:
     case StoreResult.MissingParametersForStoredProcedure:
     default:
         return(StoreOperationErrorHandler.OnCommonErrorLocal(
                    result,
                    location,
                    operationName,
                    storedProcName));
     }
 }
        /// <summary>
        /// Returns the proper ShardManagementException corresponding to given error code in
        /// <paramref name="result"/> for ShardMap operations.
        /// </summary>
        /// <param name="result">Operation result object.</param>
        /// <param name="shardMap">Shard map object.</param>
        /// <param name="location">Location of LSM operation.</param>
        /// <param name="errorCategory">Error category to use for raised errors.</param>
        /// <param name="operationName">Operation being performed.</param>
        /// <param name="storedProcName">Stored procedure being executed.</param>
        /// <returns>ShardManagementException to be raised.</returns>
        internal static ShardManagementException OnShardMapErrorLocal(
            IStoreResults result,
            IStoreShardMap shardMap,
            ShardLocation location,
            ShardManagementErrorCategory errorCategory,
            string operationName,
            string storedProcName)
        {
            switch (result.Result)
            {
            case StoreResult.UnableToKillSessions:
                return(new ShardManagementException(
                           errorCategory,
                           ShardManagementErrorCode.MappingsKillConnectionFailure,
                           Errors._Store_ShardMapper_UnableToKillSessions,
                           location,
                           shardMap.Name,
                           operationName,
                           storedProcName,
                           location));

            case StoreResult.StoreVersionMismatch:
            case StoreResult.MissingParametersForStoredProcedure:
            case StoreResult.ShardDoesNotExist:
                // ShardDoesNotExist on local shard map can only occur in Recovery scenario.
                // For normal UpdateShard operation, we will get this error from GSM operation first.
                return(new ShardManagementException(
                           ShardManagementErrorCategory.Recovery,
                           ShardManagementErrorCode.ShardDoesNotExist,
                           Errors._Store_Validate_ShardDoesNotExist,
                           location,
                           shardMap.Name,
                           operationName,
                           storedProcName));

            default:
                return(StoreOperationErrorHandler.OnCommonErrorLocal(
                           result,
                           location,
                           operationName,
                           storedProcName));
            }
        }
        /// <summary>
        /// Handles errors from the undo of GSM operation after LSM operations.
        /// </summary>
        /// <param name="result">Operation result.</param>
        public override void HandleUndoGlobalPostLocalExecuteError(IStoreResults result)
        {
            if (result.Result == StoreResult.ShardMapDoesNotExist)
            {
                // Remove shard map from cache.
                this.Manager.Cache.DeleteShardMap(_shardMap);
            }

            // Possible errors are:
            // StoreResult.ShardMapDoesNotExist
            // StoreResult.StoreVersionMismatch
            // StoreResult.MissingParametersForStoredProcedure
            throw StoreOperationErrorHandler.OnShardMapperErrorGlobal(
                      result,
                      _shardMap,
                      _mappingSource.StoreShard,
                      _errorCategory,
                      StoreOperationErrorHandler.OperationNameFromStoreOperationCode(this.OperationCode),
                      StoreOperationRequestBuilder.SpBulkOperationShardMappingsGlobalEnd);
        }
 /// <summary>
 /// Handles errors from the GSM operation after the LSM operations.
 /// </summary>
 /// <param name="result">Operation result.</param>
 public override void HandleDoGlobalExecuteError(IStoreResults result)
 {
     // Recovery manager handles the ShardMapDoesNotExist error properly, so we don't interfere.
     if (!_ignoreFailure || result.Result != StoreResult.ShardMapDoesNotExist)
     {
         // Possible errors are:
         // StoreResult.ShardMapDoesNotExist
         // StoreResult.ShardDoesNotExist
         // StoreResult.ShardVersionMismatch
         // StoreResult.StoreVersionMismatch
         // StoreResult.MissingParametersForStoredProcedure
         throw StoreOperationErrorHandler.OnShardMapperErrorGlobal(
                   result,
                   _shardMap,
                   _shard,
                   _errorCategory,
                   this.OperationName,
                   StoreOperationRequestBuilder.SpGetAllShardMappingsGlobal);
     }
 }
        /// <summary>
        /// Handles errors from the GSM operation after the LSM operations.
        /// </summary>
        /// <param name="result">Operation result.</param>
        public override void HandleDoGlobalExecuteError(IStoreResults result)
        {
            if (result.Result == StoreResult.ShardMapDoesNotExist)
            {
                // Remove shard map from cache.
                _shardMapManager.Cache.DeleteShardMap(_shardMap);
            }

            // Possible errors are:
            // StoreResult.ShardMapDoesNotExist
            // StoreResult.StoreVersionMismatch
            // StoreResult.MissingParametersForStoredProcedure
            throw StoreOperationErrorHandler.OnShardMapErrorGlobal(
                      result,
                      _shardMap,
                      null, // shard
                      ShardManagementErrorCategory.ShardMap,
                      this.OperationName,
                      StoreOperationRequestBuilder.SpGetAllShardsGlobal);
        }
        /// <summary>
        /// Terminates connection on the source shard object.
        /// </summary>
        private void KillConnectionsOnSourceShard()
        {
            SqlUtils.WithSqlExceptionHandling(() =>
            {
                string sourceShardConnectionString        = this.GetConnectionStringForShardLocation(_mappingSource.StoreShard.Location);
                SqlCredential sourceShardSecureCredential = this.GetSecureCredentialForShardLocation(_mappingSource.StoreShard.Location);

                IStoreResults result;

                using (IStoreConnection connectionForKill = this.Manager.StoreConnectionFactory.GetConnection(
                           StoreConnectionKind.LocalSource,
                           sourceShardConnectionString,
                           sourceShardSecureCredential))
                {
                    connectionForKill.Open();

                    using (IStoreTransactionScope ts = connectionForKill.GetTransactionScope(StoreTransactionScopeKind.NonTransactional))
                    {
                        result = ts.ExecuteOperation(
                            StoreOperationRequestBuilder.SpKillSessionsForShardMappingLocal,
                            StoreOperationRequestBuilder.KillSessionsForShardMappingLocal(_patternForKill));
                    }
                }

                if (result.Result != StoreResult.Success)
                {
                    // Possible errors are:
                    // StoreResult.UnableToKillSessions
                    // StoreResult.StoreVersionMismatch
                    // StoreResult.MissingParametersForStoredProcedure
                    throw StoreOperationErrorHandler.OnShardMapErrorLocal(
                        result,
                        _shardMap,
                        _mappingSource.StoreShard.Location,
                        _errorCategory,
                        StoreOperationErrorHandler.OperationNameFromStoreOperationCode(this.OperationCode),
                        StoreOperationRequestBuilder.SpKillSessionsForShardMappingLocal);
                }
            });
        }
        /// <summary>
        /// Returns the proper ShardManagementException corresponding to given error code in
        /// <paramref name="result"/> for ShardMap operations.
        /// </summary>
        /// <param name="result">Operation result object.</param>
        /// <param name="shardMap">Shard map object.</param>
        /// <param name="shard">Shard object.</param>
        /// <param name="errorCategory">Error category to use for raised errors.</param>
        /// <param name="operationName">Operation being performed.</param>
        /// <param name="storedProcName">Stored procedure being executed.</param>
        /// <returns>ShardManagementException to be raised.</returns>
        internal static ShardManagementException OnRecoveryErrorGlobal(
            IStoreResults result,
            IStoreShardMap shardMap,
            IStoreShard shard,
            ShardManagementErrorCategory errorCategory,
            string operationName,
            string storedProcName)
        {
            switch (result.Result)
            {
            case StoreResult.ShardLocationExists:
                return(new ShardManagementException(
                           errorCategory,
                           ShardManagementErrorCode.ShardLocationAlreadyExists,
                           Errors._Store_Shard_LocationAlreadyExistsGlobal,
                           shard.Location,
                           shardMap.Name,
                           storedProcName,
                           operationName));

            case StoreResult.ShardMapExists:
                Debug.Assert(shardMap != null);
                return(new ShardManagementException(
                           errorCategory,
                           ShardManagementErrorCode.ShardMapAlreadyExists,
                           Errors._Store_ShardMap_AlreadyExistsGlobal,
                           shardMap.Name,
                           storedProcName,
                           operationName));

            case StoreResult.StoreVersionMismatch:
            case StoreResult.MissingParametersForStoredProcedure:
            default:
                return(StoreOperationErrorHandler.OnCommonErrorGlobal(
                           result,
                           operationName,
                           storedProcName));
            }
        }
Esempio n. 25
0
 /// <summary>
 /// Handles errors from the GSM operation after the LSM operations.
 /// </summary>
 /// <param name="result">Operation result.</param>
 public override void HandleDoGlobalExecuteError(IStoreResults result)
 {
     // MappingNotFound for key is supposed to be handled in the calling layers
     // so that TryLookup vs Lookup have proper behavior.
     if (result.Result != StoreResult.MappingNotFoundForKey)
     {
         // Recovery manager handles the ShardMapDoesNotExist error properly, so we don't interfere.
         if (!_ignoreFailure || result.Result != StoreResult.ShardMapDoesNotExist)
         {
             // Possible errors are:
             // StoreResult.ShardMapDoesNotExist
             // StoreResult.StoreVersionMismatch
             // StoreResult.MissingParametersForStoredProcedure
             throw StoreOperationErrorHandler.OnShardMapperErrorGlobal(
                       result,
                       _shardMap,
                       null, // shard
                       _errorCategory,
                       this.OperationName,
                       StoreOperationRequestBuilder.SpFindShardMappingByKeyGlobal);
         }
     }
 }
        /// <summary>
        /// Returns the proper ShardManagementException corresponding to given error code in
        /// <paramref name="result"/> for ShardMap operations.
        /// </summary>
        /// <param name="result">Operation result object.</param>
        /// <param name="shardMap">Shard map object.</param>
        /// <param name="location">Location of LSM operation.</param>
        /// <param name="operationName">Operation being performed.</param>
        /// <param name="storedProcName">Stored procedure being executed.</param>
        /// <returns>ShardManagementException to be raised.</returns>
        internal static ShardManagementException OnValidationErrorLocal(
            IStoreResults result,
            IStoreShardMap shardMap,
            ShardLocation location,
            string operationName,
            string storedProcName)
        {
            switch (result.Result)
            {
            case StoreResult.ShardMapDoesNotExist:
                return(new ShardManagementException(
                           ShardManagementErrorCategory.Validation,
                           ShardManagementErrorCode.ShardMapDoesNotExist,
                           Errors._Store_Validate_ShardMapDoesNotExist,
                           shardMap.Name,
                           location,
                           operationName,
                           storedProcName));

            case StoreResult.ShardDoesNotExist:
                return(new ShardManagementException(
                           ShardManagementErrorCategory.Validation,
                           ShardManagementErrorCode.ShardDoesNotExist,
                           Errors._Store_Validate_ShardDoesNotExist,
                           location,
                           shardMap.Name,
                           operationName,
                           storedProcName));

            case StoreResult.ShardVersionMismatch:
                return(new ShardManagementException(
                           ShardManagementErrorCategory.Validation,
                           ShardManagementErrorCode.ShardVersionMismatch,
                           Errors._Store_Validate_ShardVersionMismatch,
                           location,
                           shardMap.Name,
                           operationName,
                           storedProcName));

            case StoreResult.MappingDoesNotExist:
                return(new ShardManagementException(
                           ShardManagementErrorCategory.Validation,
                           ShardManagementErrorCode.MappingDoesNotExist,
                           Errors._Store_Validate_MappingDoesNotExist,
                           location,
                           shardMap.Name,
                           operationName,
                           storedProcName));

            case StoreResult.MappingIsOffline:
                return(new ShardManagementException(
                           ShardManagementErrorCategory.Validation,
                           ShardManagementErrorCode.MappingIsOffline,
                           Errors._Store_Validate_MappingIsOffline,
                           location,
                           shardMap.Name,
                           operationName,
                           storedProcName));

            case StoreResult.StoreVersionMismatch:
            case StoreResult.MissingParametersForStoredProcedure:
            default:
                return(StoreOperationErrorHandler.OnCommonErrorLocal(
                           result,
                           location,
                           operationName,
                           storedProcName));
            }
        }
Esempio n. 27
0
        /// <summary>
        /// Finds all mappings to be purged based on the given input ranges.
        /// </summary>
        /// <param name="ts">LSM transaction scope.</param>
        /// <returns>Mappings which are to be removed.</returns>
        private IEnumerable <IStoreMapping> GetMappingsToPurge(IStoreTransactionScope ts)
        {
            IEnumerable <IStoreMapping> lsmMappings = null;

            IStoreResults result;

            if (_rangesToRemove == null)
            {
                // If no ranges are specified, get all the mappings for the shard.
                result = ts.ExecuteOperation(
                    StoreOperationRequestBuilder.SpGetAllShardMappingsLocal,
                    StoreOperationRequestBuilder.GetAllShardMappingsLocal(_shardMap, _shard, null));

                if (result.Result != StoreResult.Success)
                {
                    // Possible errors are:
                    // StoreResult.ShardMapDoesNotExist
                    // StoreResult.StoreVersionMismatch
                    // StoreResult.MissingParametersForStoredProcedure
                    throw StoreOperationErrorHandler.OnRecoveryErrorLocal(
                              result,
                              _shardMap,
                              this.Location,
                              ShardManagementErrorCategory.Recovery,
                              this.OperationName,
                              StoreOperationRequestBuilder.SpGetAllShardMappingsLocal);
                }

                lsmMappings = result.StoreMappings;
            }
            else
            {
                // If any ranges are specified, only delete intersected ranges.
                IDictionary <ShardRange, IStoreMapping> mappingsToPurge = new Dictionary <ShardRange, IStoreMapping>();

                foreach (ShardRange range in _rangesToRemove)
                {
                    switch (_shardMap.MapType)
                    {
                    case ShardMapType.Range:
                        result = ts.ExecuteOperation(
                            StoreOperationRequestBuilder.SpGetAllShardMappingsLocal,
                            StoreOperationRequestBuilder.GetAllShardMappingsLocal(
                                _shardMap,
                                _shard,
                                range));
                        break;

                    default:
                        Debug.Assert(_shardMap.MapType == ShardMapType.List);
                        result = ts.ExecuteOperation(
                            StoreOperationRequestBuilder.SpFindShardMappingByKeyLocal,
                            StoreOperationRequestBuilder.FindShardMappingByKeyLocal(
                                _shardMap,
                                ShardKey.FromRawValue(_shardMap.KeyType, range.Low.RawValue)));
                        break;
                    }

                    if (result.Result != StoreResult.Success)
                    {
                        if (result.Result != StoreResult.MappingNotFoundForKey)
                        {
                            // Possible errors are:
                            // StoreResult.ShardMapDoesNotExist
                            // StoreResult.StoreVersionMismatch
                            // StoreResult.MissingParametersForStoredProcedure
                            throw StoreOperationErrorHandler.OnRecoveryErrorLocal(
                                      result,
                                      _shardMap,
                                      this.Location,
                                      ShardManagementErrorCategory.Recovery,
                                      this.OperationName,
                                      _shardMap.MapType == ShardMapType.Range ?
                                      StoreOperationRequestBuilder.SpGetAllShardMappingsLocal :
                                      StoreOperationRequestBuilder.SpFindShardMappingByKeyLocal);
                        }
                        else
                        {
                            // No intersections being found is fine. Skip to the next mapping.
                            Debug.Assert(_shardMap.MapType == ShardMapType.List);
                        }
                    }
                    else
                    {
                        foreach (IStoreMapping mapping in result.StoreMappings)
                        {
                            ShardRange intersectedRange = new ShardRange(
                                ShardKey.FromRawValue(_shardMap.KeyType, mapping.MinValue),
                                ShardKey.FromRawValue(_shardMap.KeyType, mapping.MaxValue));

                            mappingsToPurge[intersectedRange] = mapping;
                        }
                    }
                }

                lsmMappings = mappingsToPurge.Values;
            }

            return(lsmMappings);
        }
        /// <summary>
        /// Performs validation that the local representation is as up-to-date
        /// as the representation on the backing data store.
        /// </summary>
        /// <param name="conn">Connection used for validation.</param>
        /// <param name="manager">ShardMapManager reference.</param>
        /// <param name="shardMap">Shard map for the mapping.</param>
        /// <param name="storeMapping">Mapping to validate.</param>
        internal static void ValidateMapping(
            SqlConnection conn,
            ShardMapManager manager,
            IStoreShardMap shardMap,
            IStoreMapping storeMapping)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            SqlResults lsmResult = new SqlResults();

            XElement xeValidate = StoreOperationRequestBuilder.ValidateShardMappingLocal(shardMap.Id, storeMapping.Id);

            using (SqlCommand cmd = conn.CreateCommand())
                using (XmlReader input = xeValidate.CreateReader())
                {
                    cmd.CommandText = StoreOperationRequestBuilder.SpValidateShardMappingLocal;
                    cmd.CommandType = CommandType.StoredProcedure;

                    SqlUtils.AddCommandParameter(
                        cmd,
                        "@input",
                        SqlDbType.Xml,
                        ParameterDirection.Input,
                        0,
                        new SqlXml(input));

                    SqlParameter resultParam = SqlUtils.AddCommandParameter(
                        cmd,
                        "@result",
                        SqlDbType.Int,
                        ParameterDirection.Output,
                        0,
                        0);

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        lsmResult.Fetch(reader);
                    }

                    // Output parameter will be used to specify the outcome.
                    lsmResult.Result = (StoreResult)resultParam.Value;
                }

            stopwatch.Stop();

            Tracer.TraceInfo(
                TraceSourceConstants.ComponentNames.Shard,
                "ValidateMapping",
                "Complete; Shard: {0}; Connection: {1}; Result: {2}; Duration: {3}",
                storeMapping.StoreShard.Location,
                conn.ConnectionString,
                lsmResult.Result,
                stopwatch.Elapsed);

            if (lsmResult.Result != StoreResult.Success)
            {
                if (lsmResult.Result == StoreResult.ShardMapDoesNotExist)
                {
                    manager.Cache.DeleteShardMap(shardMap);
                }
                else
                if (lsmResult.Result == StoreResult.MappingDoesNotExist)
                {
                    // Only evict from cache is mapping is no longer present,
                    // for Offline mappings, we don't even retry, so same request
                    // will continue to go to the LSM.
                    manager.Cache.DeleteMapping(storeMapping);
                }

                // Possible errors are:
                // StoreResult.ShardMapDoesNotExist
                // StoreResult.MappingDoesNotExist
                // StoreResult.MappingIsOffline
                // StoreResult.ShardVersionMismatch
                // StoreResult.StoreVersionMismatch
                // StoreResult.MissingParametersForStoredProcedure
                throw StoreOperationErrorHandler.OnValidationErrorLocal(
                          lsmResult,
                          shardMap,
                          storeMapping.StoreShard.Location,
                          "ValidateMapping",
                          StoreOperationRequestBuilder.SpValidateShardLocal);
            }

            Debug.Assert(lsmResult.Result == StoreResult.Success);
        }
        /// <summary>
        /// Asynchronously performs validation that the local representation is as
        /// up-to-date as the representation on the backing data store.
        /// </summary>
        /// <param name="conn">Connection used for validation.</param>
        /// <param name="manager">ShardMapManager reference.</param>
        /// <param name="shardMap">Shard map for the shard.</param>
        /// <param name="shard">Shard to validate.</param>
        /// <returns>A task to await validation completion</returns>
        internal static async Task ValidateShardAsync(
            SqlConnection conn,
            ShardMapManager manager,
            IStoreShardMap shardMap,
            IStoreShard shard
            )
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            SqlResults lsmResult = new SqlResults();

            XElement xeValidate = StoreOperationRequestBuilder.ValidateShardLocal(shardMap.Id, shard.Id, shard.Version);

            using (SqlCommand cmd = conn.CreateCommand())
                using (XmlReader input = xeValidate.CreateReader())
                {
                    cmd.CommandText = StoreOperationRequestBuilder.SpValidateShardLocal;
                    cmd.CommandType = CommandType.StoredProcedure;

                    SqlUtils.AddCommandParameter(
                        cmd,
                        "@input",
                        SqlDbType.Xml,
                        ParameterDirection.Input,
                        0,
                        new SqlXml(input));

                    SqlParameter resultParam = SqlUtils.AddCommandParameter(
                        cmd,
                        "@result",
                        SqlDbType.Int,
                        ParameterDirection.Output,
                        0,
                        0);

                    using (SqlDataReader reader = await cmd.ExecuteReaderAsync())
                    {
                        await lsmResult.FetchAsync(reader);
                    }

                    // Output parameter will be used to specify the outcome.
                    lsmResult.Result = (StoreResult)resultParam.Value;
                }

            stopwatch.Stop();

            Tracer.TraceInfo(
                TraceSourceConstants.ComponentNames.Shard,
                "ValidateShardAsync",
                "Complete; Shard: {0}; Connection: {1}; Result: {2}; Duration: {3}",
                shard.Location,
                conn.ConnectionString,
                lsmResult.Result,
                stopwatch.Elapsed);

            if (lsmResult.Result != StoreResult.Success)
            {
                if (lsmResult.Result == StoreResult.ShardMapDoesNotExist)
                {
                    manager.Cache.DeleteShardMap(shardMap);
                }

                // Possible errors are:
                // StoreResult.ShardMapDoesNotExist
                // StoreResult.ShardDoesNotExist
                // StoreResult.ShardVersionMismatch
                // StoreResult.StoreVersionMismatch
                // StoreResult.MissingParametersForStoredProcedure
                throw StoreOperationErrorHandler.OnValidationErrorLocal(
                          lsmResult,
                          shardMap,
                          shard.Location,
                          "ValidateShardAsync",
                          StoreOperationRequestBuilder.SpValidateShardLocal);
            }
        }
        /// <summary>
        /// Returns the proper ShardManagementException corresponding to given error code in
        /// <paramref name="result"/> for ShardMap operations.
        /// </summary>
        /// <param name="result">Operation result object.</param>
        /// <param name="shardMap">Shard map object.</param>
        /// <param name="shard">Shard object.</param>
        /// <param name="errorCategory">Error category to use for raised errors.</param>
        /// <param name="operationName">Operation being performed.</param>
        /// <param name="storedProcName">Stored procedure being executed.</param>
        /// <returns>ShardManagementException to be raised.</returns>
        internal static ShardManagementException OnShardMapErrorGlobal(
            IStoreResults result,
            IStoreShardMap shardMap,
            IStoreShard shard,
            ShardManagementErrorCategory errorCategory,
            string operationName,
            string storedProcName)
        {
            switch (result.Result)
            {
            case StoreResult.ShardMapDoesNotExist:
                return(new ShardManagementException(
                           errorCategory,
                           ShardManagementErrorCode.ShardMapDoesNotExist,
                           Errors._Store_ShardMap_DoesNotExistGlobal,
                           shardMap.Name,
                           storedProcName,
                           operationName,
                           shard != null ? shard.Location.ToString() : "*"));

            case StoreResult.ShardExists:
                return(new ShardManagementException(
                           errorCategory,
                           ShardManagementErrorCode.ShardAlreadyExists,
                           Errors._Store_Shard_AlreadyExistsGlobal,
                           shard.Location,
                           shardMap.Name,
                           storedProcName,
                           operationName));

            case StoreResult.ShardLocationExists:
                return(new ShardManagementException(
                           errorCategory,
                           ShardManagementErrorCode.ShardLocationAlreadyExists,
                           Errors._Store_Shard_LocationAlreadyExistsGlobal,
                           shard.Location,
                           shardMap.Name,
                           storedProcName,
                           operationName));

            case StoreResult.ShardDoesNotExist:
                return(new ShardManagementException(
                           errorCategory,
                           ShardManagementErrorCode.ShardDoesNotExist,
                           Errors._Store_Shard_DoesNotExistGlobal,
                           shard.Location,
                           shardMap.Name,
                           storedProcName,
                           operationName));

            case StoreResult.ShardVersionMismatch:
                return(new ShardManagementException(
                           errorCategory,
                           ShardManagementErrorCode.ShardVersionMismatch,
                           Errors._Store_Shard_VersionMismatchGlobal,
                           shard.Location,
                           shardMap.Name,
                           storedProcName,
                           operationName));

            case StoreResult.ShardHasMappings:
                return(new ShardManagementException(
                           errorCategory,
                           ShardManagementErrorCode.ShardHasMappings,
                           Errors._Store_Shard_HasMappingsGlobal,
                           shard.Location,
                           shardMap.Name,
                           storedProcName,
                           operationName));

            case StoreResult.StoreVersionMismatch:
            case StoreResult.MissingParametersForStoredProcedure:
            default:
                return(StoreOperationErrorHandler.OnCommonErrorGlobal(
                           result,
                           operationName,
                           storedProcName));
            }
        }