/// <summary>
        /// Performs the LSM operation on the source shard.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>Result of the operation.</returns>
        public override IStoreResults DoLocalSourceExecute(IStoreTransactionScope ts)
        {
            IStoreResults result;

            if (_updateLocation)
            {
                result = ts.ExecuteOperation(
                    StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                    StoreOperationRequestBuilder.RemoveShardMappingLocal(
                        this.Id,
                        false,
                        _shardMap,
                        _mappingSource));
            }
            else
            {
                result = ts.ExecuteOperation(
                    StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                    StoreOperationRequestBuilder.UpdateShardMappingLocal(
                        this.Id,
                        false,
                        _shardMap,
                        _mappingSource,
                        _mappingTarget));
            }

            // We need to treat the kill connection operation separately, the reason
            // being that we cannot perform kill operations within a transaction.
            if (result.Result == StoreResult.Success && _fromOnlineToOffline)
            {
                this.KillConnectionsOnSourceShard();
            }

            return(result);
        }
        /// <summary>
        /// Performs the undo of LSM operation on the source shard.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>Result of the operation.</returns>
        public override IStoreResults UndoLocalSourceExecute(IStoreTransactionScope ts)
        {
            DefaultStoreMapping dsmSource = new DefaultStoreMapping(
                _mappingSource.Id,
                _shardMap.Id,
                new DefaultStoreShard(
                    _mappingSource.StoreShard.Id,
                    this.OriginalShardVersionRemoves,
                    _shardMap.Id,
                    _mappingSource.StoreShard.Location,
                    _mappingSource.StoreShard.Status),
                _mappingSource.MinValue,
                _mappingSource.MaxValue,
                _mappingSource.Status,
                _lockOwnerId);

            if (_updateLocation)
            {
                return(ts.ExecuteOperation(
                           StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                           StoreOperationRequestBuilder.AddShardMappingLocal(
                               this.Id,
                               true,
                               _shardMap,
                               dsmSource)));
            }
            else
            {
                DefaultStoreMapping dsmTarget = new DefaultStoreMapping(
                    _mappingTarget.Id,
                    _shardMap.Id,
                    new DefaultStoreShard(
                        _mappingTarget.StoreShard.Id,
                        this.OriginalShardVersionRemoves,
                        _shardMap.Id,
                        _mappingTarget.StoreShard.Location,
                        _mappingTarget.StoreShard.Status),
                    _mappingTarget.MinValue,
                    _mappingTarget.MaxValue,
                    _mappingTarget.Status,
                    _lockOwnerId);

                return(ts.ExecuteOperation(
                           StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                           StoreOperationRequestBuilder.UpdateShardMappingLocal(
                               this.Id,
                               true,
                               _shardMap,
                               dsmTarget,
                               dsmSource)));
            }
        }
 /// <summary>
 /// Execute the operation against GSM in the current transaction scope.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>
 /// Results of the operation.
 /// </returns>
 public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
 {
     // If no ranges are specified, blindly mark everything for deletion.
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpGetAllShardMappingsGlobal,
                StoreOperationRequestBuilder.GetAllShardMappingsGlobal(_shardMap, _shard, _range)));
 }
 /// <summary>
 /// Execute the operation against GSM in the current transaction scope.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>
 /// Results of the operation.
 /// </returns>
 public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
 {
     // If no ranges are specified, blindly mark everything for deletion.
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpFindShardMappingByIdGlobal,
         StoreOperationRequestBuilder.FindShardMappingByIdGlobal(_shardMap, _mapping));
 }
Esempio n. 5
0
 /// <summary>
 /// Performs the LSM operation on the source shard.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Result of the operation.</returns>
 public override IStoreResults DoLocalSourceExecute(IStoreTransactionScope ts)
 {
     // Now actually update the shard.
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpUpdateShardLocal,
                StoreOperationRequestBuilder.UpdateShardLocal(this.Id, _shardMap, _shardNew)));
 }
Esempio n. 6
0
 /// <summary>
 /// Performs the LSM operation on the source shard.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Result of the operation.</returns>
 public override IStoreResults DoLocalSourceExecute(IStoreTransactionScope ts)
 {
     // Now actually add the shard entries.
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpRemoveShardLocal,
                StoreOperationRequestBuilder.RemoveShardLocal(this.Id, _shardMap, _shard)));
 }
Esempio n. 7
0
 /// <summary>
 /// Performs the undo of LSM operation on the source shard.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Result of the operation.</returns>
 public override IStoreResults UndoLocalSourceExecute(IStoreTransactionScope ts)
 {
     // Adds back the removed shard entries.
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpUpdateShardLocal,
                StoreOperationRequestBuilder.UpdateShardLocal(this.Id, _shardMap, _shardOld)));
 }
 /// <summary>
 /// Performs the LSM operation on the source shard.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Result of the operation.</returns>
 public override IStoreResults DoLocalSourceExecute(IStoreTransactionScope ts)
 {
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                StoreOperationRequestBuilder.AddShardMappingLocal(
                    this.Id,
                    false,
                    _shardMap,
                    _mapping)));
 }
Esempio n. 9
0
 /// <summary>
 /// Execute the operation against GSM in the current transaction scope.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>
 /// Results of the operation.
 /// </returns>
 public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
 {
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpLockOrUnLockShardMappingsGlobal,
                StoreOperationRequestBuilder.LockOrUnLockShardMappingsGlobal(
                    _shardMap,
                    _mapping,
                    _lockOwnerId,
                    _lockOpType)));
 }
Esempio n. 10
0
 /// <summary>
 /// Performs the LSM operation on the source shard.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Result of the operation.</returns>
 public override IStoreResults DoLocalSourceExecute(IStoreTransactionScope ts)
 {
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                StoreOperationRequestBuilder.ReplaceShardMappingsLocal(
                    this.Id,
                    false,
                    _shardMap,
                    _mappingsSource.Select(m => m.Item1).ToArray(),
                    _mappingsTarget.Select(m => m.Item1).ToArray())));
 }
 /// <summary>
 /// Performs the LSM operation on the target shard.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Result of the operation.</returns>
 public override IStoreResults DoLocalTargetExecute(IStoreTransactionScope ts)
 {
     Debug.Assert(_updateLocation);
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                StoreOperationRequestBuilder.AddShardMappingLocal(
                    this.Id,
                    false,
                    _shardMap,
                    _mappingTarget)));
 }
Esempio n. 12
0
        /// <summary>
        /// Execute the operation against GSM in the current transaction scope.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>
        /// Results of the operation.
        /// </returns>
        public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
        {
            IEnumerable <IStoreMapping> mappingsToReplace = this.GetMappingsToPurge(ts);

            return(ts.ExecuteOperation(
                       StoreOperationRequestBuilder.SpReplaceShardMappingsGlobal,
                       StoreOperationRequestBuilder.ReplaceShardMappingsGlobalWithoutLogging(
                           _shardMap,
                           mappingsToReplace.ToArray(),
                           _mappingsToAdd.ToArray())));
        }
 /// <summary>
 /// Performs the initial GSM operation prior to LSM operations.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Pending operations on the target objects if any.</returns>
 public override IStoreResults DoGlobalPreLocalExecute(IStoreTransactionScope ts)
 {
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpBulkOperationShardsGlobalBegin,
         StoreOperationRequestBuilder.AddShardGlobal(
             this.Id,
             this.OperationCode,
             false, // undo
             _shardMap,
             _shard));
 }
 /// <summary>
 /// Performs the initial GSM operation prior to LSM operations.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Pending operations on the target objects if any.</returns>
 public override IStoreResults DoGlobalPreLocalExecute(IStoreTransactionScope ts)
 {
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpBulkOperationShardsGlobalBegin,
                StoreOperationRequestBuilder.AddShardGlobal(
                    this.Id,
                    this.OperationCode,
                    false, // undo
                    _shardMap,
                    _shard)));
 }
Esempio n. 15
0
 /// <summary>
 /// Performs the undo of GSM operation after LSM operations.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Pending operations on the target objects if any.</returns>
 public override IStoreResults UndoGlobalPostLocalExecute(IStoreTransactionScope ts)
 {
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpBulkOperationShardsGlobalEnd,
                StoreOperationRequestBuilder.RemoveShardGlobal(
                    this.Id,
                    this.OperationCode,
                    true, // undo
                    _shardMap,
                    _shard)));
 }
        /// <summary>
        /// Execute the operation against GSM in the current transaction scope.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>
        /// Results of the operation.
        /// </returns>
        public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
        {
            IEnumerable<IStoreMapping> mappingsToReplace = this.GetMappingsToPurge(ts);

            return ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpReplaceShardMappingsGlobal,
                StoreOperationRequestBuilder.ReplaceShardMappingsGlobalWithoutLogging(
                    _shardMap,
                    mappingsToReplace.ToArray(),
                    _mappingsToAdd.ToArray()));
        }
Esempio n. 17
0
 /// <summary>
 /// Performs the final GSM operation after the LSM operations.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Pending operations on the target objects if any.</returns>
 public override IStoreResults DoGlobalPostLocalExecute(IStoreTransactionScope ts)
 {
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpBulkOperationShardsGlobalEnd,
                StoreOperationRequestBuilder.UpdateShardGlobal(
                    this.Id,
                    this.OperationCode,
                    false, // undo
                    _shardMap,
                    _shardOld,
                    _shardNew)));
 }
Esempio n. 18
0
 /// <summary>
 /// Performs the final GSM operation after the LSM operations.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Pending operations on the target objects if any.</returns>
 public override IStoreResults DoGlobalPostLocalExecute(IStoreTransactionScope ts)
 {
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpBulkOperationShardMappingsGlobalEnd,
                StoreOperationRequestBuilder.ReplaceShardMappingsGlobal(
                    this.Id,
                    this.OperationCode,
                    false, // undo
                    _shardMap,
                    _mappingsSource,
                    _mappingsTarget)));
 }
        /// <summary>
        /// Execute the operation against GSM in the current transaction scope.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>
        /// Results of the operation.
        /// </returns>
        public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
        {
            _loadResults.Clear();

            IStoreResults result = ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpGetAllShardMapsGlobal,
                StoreOperationRequestBuilder.GetAllShardMapsGlobal());

            if (result.Result == StoreResult.Success)
            {
                foreach (IStoreShardMap ssm in result.StoreShardMaps)
                {
                    _ssmCurrent = ssm;

                    result = ts.ExecuteOperation(
                        StoreOperationRequestBuilder.SpGetAllShardMappingsGlobal,
                        StoreOperationRequestBuilder.GetAllShardMappingsGlobal(ssm, null, null));

                    if (result.Result == StoreResult.Success)
                    {
                        _loadResults.Add(
                            new LoadResult
                            {
                                ShardMap = ssm,
                                Mappings = result.StoreMappings
                            });
                    }
                    else
                    if (result.Result != StoreResult.ShardMapDoesNotExist)
                    {
                        // Ignore some possible failures for Load operation and skip failed
                        // shard map caching operations.
                        break;
                    }
                }
            }

            return result;
        }
        /// <summary>
        /// Execute the operation against GSM in the current transaction scope.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>
        /// Results of the operation.
        /// </returns>
        public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
        {
            _loadResults.Clear();

            IStoreResults result = ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpGetAllShardMapsGlobal,
                StoreOperationRequestBuilder.GetAllShardMapsGlobal());

            if (result.Result == StoreResult.Success)
            {
                foreach (IStoreShardMap ssm in result.StoreShardMaps)
                {
                    _ssmCurrent = ssm;

                    result = ts.ExecuteOperation(
                        StoreOperationRequestBuilder.SpGetAllShardMappingsGlobal,
                        StoreOperationRequestBuilder.GetAllShardMappingsGlobal(ssm, null, null));

                    if (result.Result == StoreResult.Success)
                    {
                        _loadResults.Add(
                            new LoadResult
                        {
                            ShardMap = ssm,
                            Mappings = result.StoreMappings
                        });
                    }
                    else
                    if (result.Result != StoreResult.ShardMapDoesNotExist)
                    {
                        // Ignore some possible failures for Load operation and skip failed
                        // shard map caching operations.
                        break;
                    }
                }
            }

            return(result);
        }
Esempio n. 21
0
        /// <summary>
        /// Execute the operation against LSM in the current transaction scope.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>
        /// Results of the operation.
        /// </returns>
        public override IStoreResults DoLocalExecute(IStoreTransactionScope ts)
        {
            IEnumerable <IStoreMapping> mappingsToRemove = this.GetMappingsToPurge(ts);

            return(ts.ExecuteOperation(
                       StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                       StoreOperationRequestBuilder.ReplaceShardMappingsLocal(
                           Guid.NewGuid(), // Create a new Guid so that this operation forces over-writes.
                           false,
                           _shardMap,
                           mappingsToRemove.ToArray(),
                           _mappingsToAdd.ToArray())));
        }
 /// <summary>
 /// Performs the undo of GSM operation after LSM operations.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Pending operations on the target objects if any.</returns>
 public override IStoreResults UndoGlobalPostLocalExecute(IStoreTransactionScope ts)
 {
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpBulkOperationShardMappingsGlobalEnd,
                StoreOperationRequestBuilder.UpdateShardMappingGlobal(
                    this.Id,
                    this.OperationCode,
                    true, // undo
                    _patternForKill,
                    _shardMap,
                    _mappingSource,
                    _mappingTarget,
                    _lockOwnerId)));
 }
        /// <summary>
        /// Performs the LSM operation on the source shard.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>Result of the operation.</returns>
        public override IStoreResults DoLocalSourceExecute(IStoreTransactionScope ts)
        {
            // There should already be some version of LSM at this location as RecoveryMAnager.AttachShard() first reads existing shard maps from this location.

            IStoreResults checkResult = ts.ExecuteCommandSingle(SqlUtils.CheckIfExistsLocalScript.Single());

            Debug.Assert(checkResult.StoreVersion != null);

            // Upgrade local shard map to latest version before attaching.
            ts.ExecuteCommandBatch(SqlUtils.FilterUpgradeCommands(SqlUtils.UpgradeLocalScript, GlobalConstants.LsmVersionClient, checkResult.StoreVersion.Version));

            // Now update the shards table in LSM.
            return(ts.ExecuteOperation(
                       StoreOperationRequestBuilder.SpUpdateShardLocal,
                       StoreOperationRequestBuilder.UpdateShardLocal(this.Id, _shardMap, _shard)));
        }
        /// <summary>
        /// Performs the undo of LSM operation on the source shard.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>Result of the operation.</returns>
        public override IStoreResults UndoLocalSourceExecute(IStoreTransactionScope ts)
        {
            IStoreResults checkResult = ts.ExecuteCommandSingle(SqlUtils.CheckIfExistsLocalScript.Single());

            if (checkResult.StoreVersion != null)
            {
                // Remove the added shard entries.
                return(ts.ExecuteOperation(
                           StoreOperationRequestBuilder.SpRemoveShardLocal,
                           StoreOperationRequestBuilder.RemoveShardLocal(this.Id, _shardMap, _shard)));
            }
            else
            {
                // If version is < 0, then shard never got deployed, consider it a success.
                return(new SqlResults());
            }
        }
Esempio n. 25
0
        /// <summary>
        /// Execute the operation against LSM in the current transaction scope.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>
        /// Results of the operation.
        /// </returns>
        public override IStoreResults DoLocalExecute(IStoreTransactionScope ts)
        {
            IStoreResults result = ts.ExecuteCommandSingle(SqlUtils.CheckIfExistsLocalScript.Single());

            if (result.StoreVersion == null)
            {
                // Shard not deployed, which is an error condition.
                throw new ShardManagementException(
                          ShardManagementErrorCategory.Recovery,
                          ShardManagementErrorCode.ShardNotValid,
                          Errors._Recovery_ShardNotValid,
                          this.Location,
                          this.OperationName);
            }

            return(ts.ExecuteOperation(
                       StoreOperationRequestBuilder.SpGetAllShardsLocal,
                       StoreOperationRequestBuilder.GetAllShardsLocal()));
        }
        /// <summary>
        /// Execute the operation against LSM in the current transaction scope.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>
        /// Results of the operation.
        /// </returns>
        public override IStoreResults DoLocalExecute(IStoreTransactionScope ts)
        {
            IStoreResults result = ts.ExecuteCommandSingle(SqlUtils.CheckIfExistsLocalScript.Single());

            if (result.StoreVersion == null)
            {
                // Shard not deployed, which is an error condition.
                throw new ShardManagementException(
                    ShardManagementErrorCategory.Recovery,
                    ShardManagementErrorCode.ShardNotValid,
                    Errors._Recovery_ShardNotValid,
                    this.Location,
                    this.OperationName);
            }

            return ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpGetAllShardsLocal,
                StoreOperationRequestBuilder.GetAllShardsLocal());
        }
        /// <summary>
        /// Performs the LSM operation on the source shard.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>Result of the operation.</returns>
        public override IStoreResults DoLocalSourceExecute(IStoreTransactionScope ts)
        {
            IStoreResults checkResult = ts.ExecuteCommandSingle(SqlUtils.CheckIfExistsLocalScript.Single());

            // Shard not already deployed, just need to add the proper entries.
            if (checkResult.StoreVersion == null)
            {
                // create initial version of LSM
                ts.ExecuteCommandBatch(SqlUtils.CreateLocalScript);

                // now upgrade LSM to latest version
                ts.ExecuteCommandBatch(SqlUtils.FilterUpgradeCommands(SqlUtils.UpgradeLocalScript, GlobalConstants.LsmVersionClient));
            }

            // Now actually add the shard entries.
            return(ts.ExecuteOperation(
                       StoreOperationRequestBuilder.SpAddShardLocal,
                       StoreOperationRequestBuilder.AddShardLocal(this.Id, false, _shardMap, _shard)));
        }
        /// <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);
                }
            });
        }
Esempio n. 29
0
        /// <summary>
        /// Performs the undo of LSM operation on the source shard.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>Result of the operation.</returns>
        public override IStoreResults UndoLocalSourceExecute(IStoreTransactionScope ts)
        {
            DefaultStoreShard dssOriginal = new DefaultStoreShard(
                _mappingsSource[0].Item1.StoreShard.Id,
                this.OriginalShardVersionAdds,
                _mappingsSource[0].Item1.ShardMapId,
                _mappingsSource[0].Item1.StoreShard.Location,
                _mappingsSource[0].Item1.StoreShard.Status);

            DefaultStoreMapping dsmSource = new DefaultStoreMapping(
                _mappingsSource[0].Item1.Id,
                _mappingsSource[0].Item1.ShardMapId,
                dssOriginal,
                _mappingsSource[0].Item1.MinValue,
                _mappingsSource[0].Item1.MaxValue,
                _mappingsSource[0].Item1.Status,
                _mappingsSource[0].Item2);

            DefaultStoreMapping dsmTarget = new DefaultStoreMapping(
                _mappingsTarget[0].Item1.Id,
                _mappingsTarget[0].Item1.ShardMapId,
                dssOriginal,
                _mappingsTarget[0].Item1.MinValue,
                _mappingsTarget[0].Item1.MaxValue,
                _mappingsTarget[0].Item1.Status,
                _mappingsTarget[0].Item2);

            IStoreMapping[] ms = new[] { dsmSource };
            IStoreMapping[] mt = new[] { dsmTarget };

            return(ts.ExecuteOperation(
                       StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                       StoreOperationRequestBuilder.ReplaceShardMappingsLocal(
                           this.Id,
                           true,
                           _shardMap,
                           mt.Concat(_mappingsTarget.Skip(1).Select(m => m.Item1)).ToArray(),
                           ms.Concat(_mappingsSource.Skip(1).Select(m => m.Item1)).ToArray())));
        }
        /// <summary>
        /// Performs undo of LSM operation on the target shard.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>Result of the operation.</returns>
        public override IStoreResults UndoLocalTargetExecute(IStoreTransactionScope ts)
        {
            DefaultStoreMapping dsmTarget = new DefaultStoreMapping(
                _mappingTarget.Id,
                _shardMap.Id,
                new DefaultStoreShard(
                    _mappingTarget.StoreShard.Id,
                    this.OriginalShardVersionAdds,
                    _shardMap.Id,
                    _mappingTarget.StoreShard.Location,
                    _mappingTarget.StoreShard.Status),
                _mappingTarget.MinValue,
                _mappingTarget.MaxValue,
                _mappingTarget.Status,
                _lockOwnerId);

            return(ts.ExecuteOperation(
                       StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                       StoreOperationRequestBuilder.RemoveShardMappingLocal(
                           this.Id,
                           true,
                           _shardMap,
                           dsmTarget)));
        }
 /// <summary>
 /// Performs the LSM operation on the target shard.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Result of the operation.</returns>
 public override IStoreResults DoLocalTargetExecute(IStoreTransactionScope ts)
 {
     Debug.Assert(_updateLocation);
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
         StoreOperationRequestBuilder.AddShardMappingLocal(
             this.Id,
             false,
             _shardMap,
             _mappingTarget));
 }
        /// <summary>
        /// Performs the undo of LSM operation on the source shard.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>Result of the operation.</returns>
        public override IStoreResults UndoLocalSourceExecute(IStoreTransactionScope ts)
        {
            DefaultStoreMapping dsmSource = new DefaultStoreMapping(
                _mappingSource.Id,
                _shardMap.Id,
                new DefaultStoreShard(
                    _mappingSource.StoreShard.Id,
                    this.OriginalShardVersionRemoves,
                    _shardMap.Id,
                    _mappingSource.StoreShard.Location,
                    _mappingSource.StoreShard.Status),
                _mappingSource.MinValue,
                _mappingSource.MaxValue,
                _mappingSource.Status,
                _lockOwnerId);

            if (_updateLocation)
            {
                return ts.ExecuteOperation(
                    StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                    StoreOperationRequestBuilder.AddShardMappingLocal(
                        this.Id,
                        true,
                        _shardMap,
                        dsmSource));
            }
            else
            {
                DefaultStoreMapping dsmTarget = new DefaultStoreMapping(
                    _mappingTarget.Id,
                    _shardMap.Id,
                    new DefaultStoreShard(
                        _mappingTarget.StoreShard.Id,
                        this.OriginalShardVersionRemoves,
                        _shardMap.Id,
                        _mappingTarget.StoreShard.Location,
                        _mappingTarget.StoreShard.Status),
                    _mappingTarget.MinValue,
                    _mappingTarget.MaxValue,
                    _mappingTarget.Status,
                    _lockOwnerId);

                return ts.ExecuteOperation(
                    StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                    StoreOperationRequestBuilder.UpdateShardMappingLocal(
                        this.Id,
                        true,
                        _shardMap,
                        dsmTarget,
                        dsmSource));
            }
        }
 /// <summary>
 /// Performs the undo of GSM operation after LSM operations.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Pending operations on the target objects if any.</returns>
 public override IStoreResults UndoGlobalPostLocalExecute(IStoreTransactionScope ts)
 {
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpBulkOperationShardMappingsGlobalEnd,
         StoreOperationRequestBuilder.AddShardMappingGlobal(
             this.Id,
             this.OperationCode,
             true, // undo
             _shardMap,
             _mapping));
 }
 /// <summary>
 /// Performs the LSM operation on the source shard.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Result of the operation.</returns>
 public override IStoreResults DoLocalSourceExecute(IStoreTransactionScope ts)
 {
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
         StoreOperationRequestBuilder.AddShardMappingLocal(
             this.Id,
             false,
             _shardMap,
             _mapping));
 }
Esempio n. 35
0
 /// <summary>
 /// Performs the undo of GSM operation prior to LSM operations.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Result of the operation.</returns>
 public virtual IStoreResults UndoGlobalPreLocalExecute(IStoreTransactionScope ts)
 {
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpFindAndUpdateOperationLogEntryByIdGlobal,
         StoreOperationRequestBuilder.FindAndUpdateOperationLogEntryByIdGlobal(this.Id, this.UndoStartState));
 }
 /// <summary>
 /// Performs the LSM operation on the source shard.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Result of the operation.</returns>
 public override IStoreResults DoLocalSourceExecute(IStoreTransactionScope ts)
 {
     // Now actually update the shard.
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpUpdateShardLocal,
         StoreOperationRequestBuilder.UpdateShardLocal(this.Id, _shardMap, _shardNew));
 }
 /// <summary>
 /// Execute the operation against GSM in the current transaction scope.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>
 /// Results of the operation.
 /// </returns>
 public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
 {
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpRemoveShardMapGlobal,
                StoreOperationRequestBuilder.RemoveShardMapGlobal(_shardMap)));
 }
 /// <summary>
 /// Performs the undo of LSM operation on the source shard.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Result of the operation.</returns>
 public override IStoreResults UndoLocalSourceExecute(IStoreTransactionScope ts)
 {
     // Adds back the removed shard entries.
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpAddShardLocal,
         StoreOperationRequestBuilder.AddShardLocal(this.Id, true, _shardMap, _shard));
 }
        /// <summary>
        /// Performs the LSM operation on the source shard.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>Result of the operation.</returns>
        public override IStoreResults DoLocalSourceExecute(IStoreTransactionScope ts)
        {
            IStoreResults result;

            if (_updateLocation)
            {
                result = ts.ExecuteOperation(
                    StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                    StoreOperationRequestBuilder.RemoveShardMappingLocal(
                        this.Id,
                        false,
                        _shardMap,
                        _mappingSource));
            }
            else
            {
                result = ts.ExecuteOperation(
                    StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                    StoreOperationRequestBuilder.UpdateShardMappingLocal(
                        this.Id,
                        false,
                        _shardMap,
                        _mappingSource,
                        _mappingTarget));
            }

            // We need to treat the kill connection operation separately, the reason
            // being that we cannot perform kill operations within a transaction.
            if (result.Result == StoreResult.Success && _fromOnlineToOffline)
            {
                this.KillConnectionsOnSourceShard();
            }

            return result;
        }
 /// <summary>
 /// Execute the operation against GSM in the current transaction scope.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>
 /// Results of the operation.
 /// </returns>
 public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
 {
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpGetAllDistinctShardLocationsGlobal,
         StoreOperationRequestBuilder.GetAllDistinctShardLocationsGlobal());
 }
        /// <summary>
        /// Finds all mappings to be purged based on the given input ranges.
        /// </summary>
        /// <param name="ts">GSM transaction scope.</param>
        /// <returns>Mappings which are to be removed.</returns>
        private IEnumerable<IStoreMapping> GetMappingsToPurge(IStoreTransactionScope ts)
        {
            // Find all the mappings in GSM belonging to the shard
            IStoreResults gsmMappingsByShard = ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpGetAllShardMappingsGlobal,
                StoreOperationRequestBuilder.GetAllShardMappingsGlobal(_shardMap, _shard, null));

            if (gsmMappingsByShard.Result != StoreResult.Success)
            {
                // Possible errors are:
                // StoreResult.ShardMapDoesNotExist
                // StoreResult.StoreVersionMismatch
                // StoreResult.MissingParametersForStoredProcedure
                throw StoreOperationErrorHandler.OnRecoveryErrorGlobal(
                    gsmMappingsByShard,
                    _shardMap,
                    _shard,
                    ShardManagementErrorCategory.Recovery,
                    this.OperationName,
                    StoreOperationRequestBuilder.SpGetAllShardMappingsGlobal);
            }

            IDictionary<ShardRange, IStoreMapping> intersectingMappings = new Dictionary<ShardRange, IStoreMapping>();

            foreach (IStoreMapping gsmMappingByShard in gsmMappingsByShard.StoreMappings)
            {
                ShardKey min = ShardKey.FromRawValue(
                    _shardMap.KeyType,
                    gsmMappingByShard.MinValue);

                ShardKey max = null;

                switch (_shardMap.MapType)
                {
                    case ShardMapType.Range:
                        max = ShardKey.FromRawValue(
                            _shardMap.KeyType,
                            gsmMappingByShard.MaxValue);
                        break;
                    default:
                        Debug.Assert(_shardMap.MapType == ShardMapType.List);
                        max = ShardKey.FromRawValue(
                            _shardMap.KeyType,
                            gsmMappingByShard.MinValue).GetNextKey();
                        break;
                }

                intersectingMappings.Add(new ShardRange(min, max), gsmMappingByShard);
            }

            // We need to discover, also, the range of intersecting mappings, so we can transitively detect
            // inconsistencies with other shards.
            foreach (IStoreMapping lsmMapping in _mappingsToRemove)
            {
                ShardKey min = ShardKey.FromRawValue(_shardMap.KeyType, lsmMapping.MinValue);

                IStoreResults gsmMappingsByRange;

                switch (_shardMap.MapType)
                {
                    case ShardMapType.Range:
                        gsmMappingsByRange = ts.ExecuteOperation(
                            StoreOperationRequestBuilder.SpGetAllShardMappingsGlobal,
                            StoreOperationRequestBuilder.GetAllShardMappingsGlobal(
                                _shardMap,
                                null,
                                new ShardRange(
                                min,
                                ShardKey.FromRawValue(_shardMap.KeyType, lsmMapping.MaxValue))));
                        break;

                    default:
                        Debug.Assert(_shardMap.MapType == ShardMapType.List);
                        gsmMappingsByRange = ts.ExecuteOperation(
                            StoreOperationRequestBuilder.SpFindShardMappingByKeyGlobal,
                            StoreOperationRequestBuilder.FindShardMappingByKeyGlobal(
                                _shardMap,
                                min));
                        break;
                }

                if (gsmMappingsByRange.Result != StoreResult.Success)
                {
                    if (gsmMappingsByRange.Result != StoreResult.MappingNotFoundForKey)
                    {
                        // Possible errors are:
                        // StoreResult.ShardMapDoesNotExist
                        // StoreResult.StoreVersionMismatch
                        // StoreResult.MissingParametersForStoredProcedure
                        throw StoreOperationErrorHandler.OnRecoveryErrorGlobal(
                            gsmMappingsByRange,
                            _shardMap,
                            _shard,
                            ShardManagementErrorCategory.Recovery,
                            this.OperationName,
                            _shardMap.MapType == ShardMapType.Range ?
                            StoreOperationRequestBuilder.SpGetAllShardMappingsGlobal :
                            StoreOperationRequestBuilder.SpFindShardMappingByKeyGlobal);
                    }
                    else
                    {
                        // No intersections being found is fine. Skip to the next mapping.
                        Debug.Assert(_shardMap.MapType == ShardMapType.List);
                    }
                }
                else
                {
                    foreach (IStoreMapping gsmMappingByRange in gsmMappingsByRange.StoreMappings)
                    {
                        ShardKey minGlobal = ShardKey.FromRawValue(_shardMap.KeyType, gsmMappingByRange.MinValue);
                        ShardKey maxGlobal = null;

                        switch (_shardMap.MapType)
                        {
                            case ShardMapType.Range:
                                maxGlobal = ShardKey.FromRawValue(_shardMap.KeyType, gsmMappingByRange.MaxValue);
                                break;
                            default:
                                Debug.Assert(_shardMap.MapType == ShardMapType.List);
                                maxGlobal = ShardKey.FromRawValue(_shardMap.KeyType, gsmMappingByRange.MinValue).GetNextKey();
                                break;
                        }

                        intersectingMappings[new ShardRange(minGlobal, maxGlobal)] = gsmMappingByRange;
                    }
                }
            }

            return intersectingMappings.Values;
        }
        /// <summary>
        /// Performs the undo of LSM operation on the source shard.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>Result of the operation.</returns>
        public override IStoreResults UndoLocalSourceExecute(IStoreTransactionScope ts)
        {
            IStoreResults checkResult = ts.ExecuteCommandSingle(SqlUtils.CheckIfExistsLocalScript.Single());

            if (checkResult.StoreVersion != null)
            {
                // Remove the added shard entries.
                return ts.ExecuteOperation(
                    StoreOperationRequestBuilder.SpRemoveShardLocal,
                    StoreOperationRequestBuilder.RemoveShardLocal(this.Id, _shardMap, _shard));
            }
            else
            {
                // If version is < 0, then shard never got deployed, consider it a success.
                return new SqlResults();
            }
        }
 /// <summary>
 /// Performs the LSM operation on the source shard.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Result of the operation.</returns>
 public override IStoreResults DoLocalSourceExecute(IStoreTransactionScope ts)
 {
     // Now actually add the shard entries.
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpRemoveShardLocal,
         StoreOperationRequestBuilder.RemoveShardLocal(this.Id, _shardMap, _shard));
 }
 /// <summary>
 /// Execute the operation against GSM in the current transaction scope.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>
 /// Results of the operation.
 /// </returns>
 public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
 {
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpLockOrUnLockShardMappingsGlobal,
         StoreOperationRequestBuilder.LockOrUnLockShardMappingsGlobal(
             _shardMap,
             _mapping,
             _lockOwnerId,
             _lockOpType));
 }
 /// <summary>
 /// Execute the operation against GSM in the current transaction scope.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>
 /// Results of the operation.
 /// </returns>
 public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
 {
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpFindShardMapByNameGlobal,
         StoreOperationRequestBuilder.FindShardMapByNameGlobal(_shardMapName));
 }
Esempio n. 46
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 the undo of LSM operation on the source shard.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>Result of the operation.</returns>
        public override IStoreResults UndoLocalSourceExecute(IStoreTransactionScope ts)
        {
            DefaultStoreShard dssOriginal = new DefaultStoreShard(
                _mappingsSource[0].Item1.StoreShard.Id,
                this.OriginalShardVersionAdds,
                _mappingsSource[0].Item1.ShardMapId,
                _mappingsSource[0].Item1.StoreShard.Location,
                _mappingsSource[0].Item1.StoreShard.Status);

            DefaultStoreMapping dsmSource = new DefaultStoreMapping(
                _mappingsSource[0].Item1.Id,
                _mappingsSource[0].Item1.ShardMapId,
                dssOriginal,
                _mappingsSource[0].Item1.MinValue,
                _mappingsSource[0].Item1.MaxValue,
                _mappingsSource[0].Item1.Status,
                _mappingsSource[0].Item2);

            DefaultStoreMapping dsmTarget = new DefaultStoreMapping(
                _mappingsTarget[0].Item1.Id,
                _mappingsTarget[0].Item1.ShardMapId,
                dssOriginal,
                _mappingsTarget[0].Item1.MinValue,
                _mappingsTarget[0].Item1.MaxValue,
                _mappingsTarget[0].Item1.Status,
                _mappingsTarget[0].Item2);

            IStoreMapping[] ms = new[] { dsmSource };
            IStoreMapping[] mt = new[] { dsmTarget };

            return ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                StoreOperationRequestBuilder.ReplaceShardMappingsLocal(
                    this.Id,
                    true,
                    _shardMap,
                    mt.Concat(_mappingsTarget.Skip(1).Select(m => m.Item1)).ToArray(),
                    ms.Concat(_mappingsSource.Skip(1).Select(m => m.Item1)).ToArray()));
        }
 /// <summary>
 /// Execute the operation against GSM in the current transaction scope.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>
 /// Results of the operation.
 /// </returns>
 public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
 {
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpRemoveShardingSchemaInfoGlobal,
         StoreOperationRequestBuilder.RemoveShardingSchemaInfoGlobal(_schemaInfoName));
 }
 /// <summary>
 /// Execute the operation against GSM in the current transaction scope.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>
 /// Results of the operation.
 /// </returns>
 public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
 {
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpFindShardMapByNameGlobal,
                StoreOperationRequestBuilder.FindShardMapByNameGlobal(_shardMapName)));
 }
Esempio n. 50
0
 /// <summary>
 /// Execute the operation against GSM in the current transaction scope.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>
 /// Results of the operation.
 /// </returns>
 public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
 {
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpGetAllShardMapsGlobal,
                StoreOperationRequestBuilder.GetAllShardMapsGlobal()));
 }
        /// <summary>
        /// Performs the LSM operation on the source shard.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>Result of the operation.</returns>
        public override IStoreResults DoLocalSourceExecute(IStoreTransactionScope ts)
        {
            IStoreResults checkResult = ts.ExecuteCommandSingle(SqlUtils.CheckIfExistsLocalScript.Single());

            // Shard not already deployed, just need to add the proper entries.
            if (checkResult.StoreVersion == null)
            {
                // create initial version of LSM
                ts.ExecuteCommandBatch(SqlUtils.CreateLocalScript);

                // now upgrade LSM to latest version
                ts.ExecuteCommandBatch(SqlUtils.FilterUpgradeCommands(SqlUtils.UpgradeLocalScript, GlobalConstants.LsmVersionClient));
            }

            // Now actually add the shard entries.
            return ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpAddShardLocal,
                StoreOperationRequestBuilder.AddShardLocal(this.Id, false, _shardMap, _shard));
        }
        /// <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>
        /// Execute the operation against LSM in the current transaction scope.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>
        /// Results of the operation.
        /// </returns>
        public override IStoreResults DoLocalExecute(IStoreTransactionScope ts)
        {
            IEnumerable<IStoreMapping> mappingsToRemove = this.GetMappingsToPurge(ts);

            return ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                StoreOperationRequestBuilder.ReplaceShardMappingsLocal(
                    Guid.NewGuid(), // Create a new Guid so that this operation forces over-writes.
                    false,
                    _shardMap,
                    mappingsToRemove.ToArray(),
                    _mappingsToAdd.ToArray()));
        }
 /// <summary>
 /// Execute the operation against LSM in the current transaction scope.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>
 /// Results of the operation.
 /// </returns>
 public override IStoreResults DoLocalExecute(IStoreTransactionScope ts)
 {
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpGetAllShardMappingsLocal,
         StoreOperationRequestBuilder.GetAllShardMappingsLocal(_shardMap, _shard, _range));
 }
 /// <summary>
 /// Execute the operation against LSM in the current transaction scope.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>
 /// Results of the operation.
 /// </returns>
 public override IStoreResults DoLocalExecute(IStoreTransactionScope ts)
 {
     return(ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpGetAllShardMappingsLocal,
                StoreOperationRequestBuilder.GetAllShardMappingsLocal(_shardMap, _shard, _range)));
 }
 /// <summary>
 /// Execute the operation against GSM in the current transaction scope.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>
 /// Results of the operation.
 /// </returns>
 public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
 {
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpRemoveShardMapGlobal,
         StoreOperationRequestBuilder.RemoveShardMapGlobal(_shardMap));
 }
        /// <summary>
        /// Performs the undo of LSM operation on the source shard.
        /// </summary>
        /// <param name="ts">Transaction scope.</param>
        /// <returns>Result of the operation.</returns>
        public override IStoreResults UndoLocalSourceExecute(IStoreTransactionScope ts)
        {
            DefaultStoreMapping dsm = new DefaultStoreMapping(
                _mapping.Id,
                _shardMap.Id,
                new DefaultStoreShard(
                    _mapping.StoreShard.Id,
                    this.OriginalShardVersionAdds,
                    _shardMap.Id,
                    _mapping.StoreShard.Location,
                    _mapping.StoreShard.Status),
                _mapping.MinValue,
                _mapping.MaxValue,
                _mapping.Status,
                default(Guid));

            return ts.ExecuteOperation(
                StoreOperationRequestBuilder.SpBulkOperationShardMappingsLocal,
                StoreOperationRequestBuilder.RemoveShardMappingLocal(
                    this.Id,
                    true,
                    _shardMap,
                    dsm));
        }
 /// <summary>
 /// Performs the undo of GSM operation after LSM operations.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Pending operations on the target objects if any.</returns>
 public override IStoreResults UndoGlobalPostLocalExecute(IStoreTransactionScope ts)
 {
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpBulkOperationShardMappingsGlobalEnd,
         StoreOperationRequestBuilder.UpdateShardMappingGlobal(
             this.Id,
             this.OperationCode,
             true, // undo
             _patternForKill,
             _shardMap,
             _mappingSource,
             _mappingTarget,
             _lockOwnerId));
 }
 /// <summary>
 /// Execute the operation against GSM in the current transaction scope.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>
 /// Results of the operation.
 /// </returns>
 public override IStoreResults DoGlobalExecute(IStoreTransactionScope ts)
 {
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpGetAllShardingSchemaInfosGlobal,
         StoreOperationRequestBuilder.GetAllShardingSchemaInfosGlobal());
 }
 /// <summary>
 /// Performs the final GSM operation after the LSM operations.
 /// </summary>
 /// <param name="ts">Transaction scope.</param>
 /// <returns>Pending operations on the target objects if any.</returns>
 public override IStoreResults DoGlobalPostLocalExecute(IStoreTransactionScope ts)
 {
     return ts.ExecuteOperation(
         StoreOperationRequestBuilder.SpBulkOperationShardsGlobalEnd,
         StoreOperationRequestBuilder.UpdateShardGlobal(
             this.Id,
             this.OperationCode,
             false, // undo
             _shardMap,
             _shardOld,
             _shardNew));
 }