public RangeMapping <TKey> MergeMappings(RangeMapping <TKey> left, RangeMapping <TKey> right,
                                                 MappingLockToken leftMappingLockToken, MappingLockToken rightMappingLockToken)
        {
            ExceptionUtils.DisallowNullArgument(left, "left");
            ExceptionUtils.DisallowNullArgument(right, "right");
            ExceptionUtils.DisallowNullArgument(leftMappingLockToken, "leftMappingLockToken");
            ExceptionUtils.DisallowNullArgument(rightMappingLockToken, "rightMappingLockToken");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "SplitMapping", "Start; Left Shard: {0}; Right Shard: {1}",
                                 left.Shard.Location, right.Shard.Location);

                Stopwatch stopwatch = Stopwatch.StartNew();

                RangeMapping <TKey> rangeMapping = this.rsm.Merge(left, right, leftMappingLockToken.LockOwnerId, rightMappingLockToken.LockOwnerId);

                stopwatch.Stop();

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "SplitMapping", "Complete; Duration: {0}", stopwatch.Elapsed);

                return(rangeMapping);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Removes a range mapping.
 /// </summary>
 /// <param name="mapping">Mapping being removed.</param>
 /// <param name="lockOwnerId">Lock owner id of this mapping</param>
 public void Remove(RangeMapping <TKey> mapping, Guid lockOwnerId)
 {
     this.Remove <RangeMapping <TKey> >(
         mapping,
         (smm, sm, ssm) => new RangeMapping <TKey>(smm, sm, ssm),
         lockOwnerId);
 }
        public RangeMapping <TKey> UpdateMapping(RangeMapping <TKey> currentMapping, RangeMappingUpdate update,
                                                 MappingLockToken mappingLockToken)
        {
            ExceptionUtils.DisallowNullArgument(currentMapping, "currentMapping");
            ExceptionUtils.DisallowNullArgument(update, "update");
            ExceptionUtils.DisallowNullArgument(mappingLockToken, "mappingLockToken");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "UpdateMapping", "Start; Current mapping shard: {0}",
                                 currentMapping.Shard.Location);

                Stopwatch stopwatch = Stopwatch.StartNew();

                RangeMapping <TKey> rangeMapping = this.rsm.Update(currentMapping, update, mappingLockToken.LockOwnerId);

                stopwatch.Stop();

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "UpdateMapping", "Complete; Current mapping shard: {0}; Duration: {1}",
                                 currentMapping.Shard.Location, stopwatch.Elapsed);

                return(rangeMapping);
            }
        }
        public void LockMapping(RangeMapping <TKey> mapping, MappingLockToken mappingLockToken)
        {
            ExceptionUtils.DisallowNullArgument(mapping, "mapping");
            ExceptionUtils.DisallowNullArgument(mappingLockToken, "mappingLockToken");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                // Generate a lock owner id
                Guid lockOwnerId = mappingLockToken.LockOwnerId;

                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.RangeShardMap,
                    "Lock", "Start; LockOwnerId: {0}", lockOwnerId);

                Stopwatch stopwatch = Stopwatch.StartNew();

                this.rsm.LockOrUnlockMappings(mapping, lockOwnerId, LockOwnerIdOpType.Lock);

                stopwatch.Stop();

                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.RangeShardMap,
                    "Lock", "Complete; Duration: {0}; StoreLockOwnerId: {1}",
                    stopwatch.Elapsed, lockOwnerId);
            }
        }
        public RangeMapping <TKey> MarkMappingOnline(RangeMapping <TKey> mapping, MappingLockToken mappingLockToken)
        {
            ExceptionUtils.DisallowNullArgument(mapping, "mapping");
            ExceptionUtils.DisallowNullArgument(mappingLockToken, "mappingLockToken");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.RangeShardMap,
                    "MarkMappingOnline", "Start; ");

                Stopwatch stopwatch = Stopwatch.StartNew();

                RangeMapping <TKey> result = this.rsm.MarkMappingOnline(mapping, mappingLockToken.LockOwnerId);

                stopwatch.Stop();

                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.RangeShardMap,
                    "MarkMappingOnline", "Complete; Duration: {0}",
                    stopwatch.Elapsed);

                return(result);
            }
        }
Exemplo n.º 6
0
 internal void LockOrUnlockMappings(RangeMapping <TKey> mapping, Guid lockOwnerId, LockOwnerIdOpType lockOwnerIdOpType)
 {
     this.LockOrUnlockMappings <RangeMapping <TKey> >(
         mapping,
         lockOwnerId,
         lockOwnerIdOpType,
         ShardManagementErrorCategory.RangeShardMap);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Allows for update to a range mapping with the updates provided in
 /// the <paramref name="update"/> parameter.
 /// </summary>
 /// <param name="currentMapping">Mapping being updated.</param>
 /// <param name="update">Updated properties of the Shard.</param>
 /// <param name="lockOwnerId">Lock owner id of this mapping</param>
 /// <returns>New instance of mapping with updated information.</returns>
 internal RangeMapping <TKey> Update(RangeMapping <TKey> currentMapping, RangeMappingUpdate update, Guid lockOwnerId)
 {
     return(this.Update <RangeMapping <TKey>, RangeMappingUpdate, MappingStatus>(
                currentMapping,
                update,
                (smm, sm, ssm) => new RangeMapping <TKey>(smm, sm, ssm),
                rms => (int)rms,
                i => (MappingStatus)i,
                lockOwnerId));
 }
Exemplo n.º 8
0
        /// <summary>
        /// Tries to looks up the key value and returns the corresponding mapping.
        /// </summary>
        /// <param name="key">Input key value.</param>
        /// <param name="useCache">Whether to use cache for lookups.</param>
        /// <param name="mapping">Mapping that contains the key value.</param>
        /// <returns><c>true</c> if mapping is found, <c>false</c> otherwise.</returns>
        public bool TryLookup(TKey key, bool useCache, out RangeMapping <TKey> mapping)
        {
            RangeMapping <TKey> p = this.Lookup <RangeMapping <TKey>, TKey>(
                key,
                useCache,
                (smm, sm, ssm) => new RangeMapping <TKey>(smm, sm, ssm),
                ShardManagementErrorCategory.RangeShardMap);

            mapping = p;

            return(p != null);
        }
Exemplo n.º 9
0
 /// <summary>
 /// Marks the given mapping offline.
 /// </summary>
 /// <param name="mapping">Input range mapping.</param>
 /// <param name="lockOwnerId">Lock owner id of this mapping</param>
 /// <returns>An offline mapping.</returns>
 public RangeMapping <TKey> MarkMappingOffline(RangeMapping <TKey> mapping, Guid lockOwnerId)
 {
     return(BaseShardMapper.SetStatus <RangeMapping <TKey>, RangeMappingUpdate, MappingStatus>(
                mapping,
                mapping.Status,
                s => MappingStatus.Offline,
                s => new RangeMappingUpdate()
     {
         Status = s
     },
                this.Update,
                lockOwnerId));
 }
        /// <summary>
        /// Determines whether the specified object is equal to the current object.
        /// </summary>
        /// <param name="obj">The object to compare with the current object.</param>
        /// <returns>True if the specified object is equal to the current object; otherwise, false.</returns>
        public override bool Equals(object obj)
        {
            RangeMapping <TKey> other = obj as RangeMapping <TKey>;

            if (other == null)
            {
                return(false);
            }

            if (this.Id.Equals(other.Id))
            {
                Debug.Assert(this.Range.Equals(other.Range));
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Looks up the key value and returns the corresponding mapping.
        /// </summary>
        /// <param name="key">Input key value.</param>
        /// <returns>Mapping that contains the key value.</returns>
        public RangeMapping <TKey> GetMappingForKey(TKey key)
        {
            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "GetMapping", "Start; Range Mapping Key Type: {0}", typeof(TKey));

                Stopwatch stopwatch = Stopwatch.StartNew();

                RangeMapping <TKey> rangeMapping = this.rsm.Lookup(key, false);

                stopwatch.Stop();

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "GetMapping", "Complete; Range Mapping Key Type: {0} Duration: {1}",
                                 typeof(TKey), stopwatch.Elapsed);

                return(rangeMapping);
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Looks up the key value and returns the corresponding mapping.
        /// </summary>
        /// <param name="key">Input key value.</param>
        /// <param name="useCache">Whether to use cache for lookups.</param>
        /// <returns>Mapping that contains the key value.</returns>
        public RangeMapping <TKey> Lookup(TKey key, bool useCache)
        {
            RangeMapping <TKey> p = this.Lookup <RangeMapping <TKey>, TKey>(
                key,
                useCache,
                (smm, sm, ssm) => new RangeMapping <TKey>(smm, sm, ssm),
                ShardManagementErrorCategory.RangeShardMap);

            if (p == null)
            {
                throw new ShardManagementException(
                          ShardManagementErrorCategory.RangeShardMap,
                          ShardManagementErrorCode.MappingNotFoundForKey,
                          Errors._Store_ShardMapper_MappingNotFoundForKeyGlobal,
                          this.ShardMap.Name,
                          StoreOperationRequestBuilder.SpFindShardMappingByKeyGlobal,
                          "Lookup");
            }

            return(p);
        }
        /// <summary>
        /// Tries to looks up the key value and place the corresponding mapping in <paramref name="rangeMapping"/>.
        /// </summary>
        /// <param name="key">Input key value.</param>
        /// <param name="rangeMapping">Mapping that contains the key value.</param>
        /// <returns><c>true</c> if mapping is found, <c>false</c> otherwise.</returns>
        public bool TryGetMappingForKey(TKey key, out RangeMapping <TKey> rangeMapping)
        {
            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "TryLookupRangeMapping", "Start; ShardMap name: {0}; Range Mapping Key Type: {1}",
                                 this.Name, typeof(TKey));

                Stopwatch stopwatch = Stopwatch.StartNew();

                bool result = this.rsm.TryLookup(key, false, out rangeMapping);

                stopwatch.Stop();

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "TryLookupRangeMapping", "Complete; ShardMap name: {0}; Range Mapping Key Type: {1}; Duration: {2}",
                                 this.Name, typeof(TKey), stopwatch.Elapsed);

                return(result);
            }
        }
        public void DeleteMapping(RangeMapping <TKey> mapping, MappingLockToken mappingLockToken)
        {
            ExceptionUtils.DisallowNullArgument(mapping, "mapping");
            ExceptionUtils.DisallowNullArgument(mappingLockToken, "mappingLockToken");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "DeleteMapping", "Start; Shard: {0}", mapping.Shard.Location);

                Stopwatch stopwatch = Stopwatch.StartNew();

                this.rsm.Remove(mapping, mappingLockToken.LockOwnerId);

                stopwatch.Stop();

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "DeleteMapping", "Complete; Shard: {0}; Duration: {1}",
                                 mapping.Shard.Location, stopwatch.Elapsed);
            }
        }
        public RangeMapping <TKey> CreateRangeMapping(RangeMappingCreationInfo <TKey> creationInfo)
        {
            ExceptionUtils.DisallowNullArgument(creationInfo, "args");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "CreateRangeMapping", "Start; Shard: {0}", creationInfo.Shard.Location);

                Stopwatch stopwatch = Stopwatch.StartNew();

                RangeMapping <TKey> rangeMapping = this.rsm.Add(new RangeMapping <TKey>(this.Manager, creationInfo));

                stopwatch.Stop();

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "CreateRangeMapping", "Complete; Shard: {0}; Duration: {1}",
                                 creationInfo.Shard.Location, stopwatch.Elapsed);

                return(rangeMapping);
            }
        }
        /// <summary>
        /// Gets the lock owner id of the specified mapping.
        /// </summary>
        /// <param name="mapping">Input range mapping.</param>
        /// <returns>An instance of <see cref="MappingLockToken"/></returns>
        public MappingLockToken GetMappingLockOwner(RangeMapping <TKey> mapping)
        {
            ExceptionUtils.DisallowNullArgument(mapping, "mapping");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.RangeShardMap,
                    "LookupLockOwner", "Start");

                Stopwatch stopwatch = Stopwatch.StartNew();

                Guid storeLockOwnerId = this.rsm.GetLockOwnerForMapping(mapping);

                stopwatch.Stop();

                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.RangeShardMap,
                    "LookupLockOwner", "Complete; Duration: {0}; StoreLockOwnerId: {1}",
                    stopwatch.Elapsed, storeLockOwnerId);

                return(new MappingLockToken(storeLockOwnerId));
            }
        }
        public IReadOnlyList <RangeMapping <TKey> > SplitMapping(RangeMapping <TKey> existingMapping, TKey splitAt,
                                                                 MappingLockToken mappingLockToken)
        {
            ExceptionUtils.DisallowNullArgument(existingMapping, "existingMapping");
            ExceptionUtils.DisallowNullArgument(mappingLockToken, "mappingLockToken");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "SplitMapping", "Start; Shard: {0}", existingMapping.Shard.Location);

                Stopwatch stopwatch = Stopwatch.StartNew();

                IReadOnlyList <RangeMapping <TKey> > rangeMapping = this.rsm.Split(existingMapping, splitAt, mappingLockToken.LockOwnerId);

                stopwatch.Stop();

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "SplitMapping", "Complete; Shard: {0}; Duration: {1}",
                                 existingMapping.Shard.Location, stopwatch.Elapsed);

                return(rangeMapping);
            }
        }
 public RangeMapping <TKey> MergeMappings(RangeMapping <TKey> left, RangeMapping <TKey> right)
 {
     return(this.MergeMappings(left, right, MappingLockToken.NoLock, MappingLockToken.NoLock));
 }
 /// <summary>
 /// Marks the specified mapping online.
 /// </summary>
 /// <param name="mapping">Input range mapping.</param>
 /// <returns>An online mapping.</returns>
 public RangeMapping <TKey> MarkMappingOnline(RangeMapping <TKey> mapping)
 {
     return(this.MarkMappingOnline(mapping, MappingLockToken.NoLock));
 }
Exemplo n.º 20
0
 /// <summary>
 /// Gets the lock owner of a mapping.
 /// </summary>
 /// <param name="mapping">The mapping</param>
 /// <returns>Lock owner for the mapping.</returns>
 internal Guid GetLockOwnerForMapping(RangeMapping <TKey> mapping)
 {
     return(this.GetLockOwnerForMapping <RangeMapping <TKey> >(mapping, ShardManagementErrorCategory.RangeShardMap));
 }
Exemplo n.º 21
0
        internal RangeMapping <TKey> Merge(RangeMapping <TKey> left, RangeMapping <TKey> right, Guid leftLockOwnerId, Guid rightLockOwnerId)
        {
            this.EnsureMappingBelongsToShardMap <RangeMapping <TKey> >(left, "Merge", "left");
            this.EnsureMappingBelongsToShardMap <RangeMapping <TKey> >(right, "Merge", "right");

            if (!left.Shard.Location.Equals(right.Shard.Location))
            {
                throw new ArgumentException(
                          StringUtils.FormatInvariant(
                              Errors._ShardMapping_MergeDifferentShards,
                              this.ShardMap.Name,
                              left.Shard.Location,
                              right.Shard.Location),
                          "left");
            }

            if (left.Range.Intersects(right.Range) || left.Range.High != right.Range.Low)
            {
                throw new ArgumentOutOfRangeException(
                          "left",
                          Errors._ShardMapping_MergeNotAdjacent);
            }

            if (left.Status != right.Status)
            {
                throw new ArgumentException(
                          StringUtils.FormatInvariant(
                              Errors._ShardMapping_DifferentStatus,
                              this.ShardMap.Name),
                          "left");
            }

            IStoreShard newShard = new DefaultStoreShard(
                left.Shard.StoreShard.Id,
                Guid.NewGuid(),
                left.Shard.StoreShard.ShardMapId,
                left.Shard.StoreShard.Location,
                left.Shard.StoreShard.Status);

            IStoreMapping mappingToRemoveLeft = new DefaultStoreMapping(
                left.StoreMapping.Id,
                left.StoreMapping.ShardMapId,
                newShard,
                left.StoreMapping.MinValue,
                left.StoreMapping.MaxValue,
                left.StoreMapping.Status,
                left.StoreMapping.LockOwnerId);

            IStoreMapping mappingToRemoveRight = new DefaultStoreMapping(
                right.StoreMapping.Id,
                right.StoreMapping.ShardMapId,
                newShard,
                right.StoreMapping.MinValue,
                right.StoreMapping.MaxValue,
                right.StoreMapping.Status,
                right.StoreMapping.LockOwnerId);

            IStoreMapping mappingToAdd = new DefaultStoreMapping(
                Guid.NewGuid(),
                newShard.ShardMapId,
                newShard,
                left.Range.Low.RawValue,
                right.Range.High.RawValue,
                (int)left.Status,
                leftLockOwnerId);

            using (IStoreOperation op = this.Manager.StoreOperationFactory.CreateReplaceMappingsOperation(
                       this.Manager,
                       StoreOperationCode.MergeMappings,
                       this.ShardMap.StoreShardMap,
                       new[]
            {
                new Tuple <IStoreMapping, Guid> (mappingToRemoveLeft, leftLockOwnerId),
                new Tuple <IStoreMapping, Guid> (mappingToRemoveRight, rightLockOwnerId)
            },
                       new[]
            {
                new Tuple <IStoreMapping, Guid>(mappingToAdd, leftLockOwnerId)
            }))
            {
                op.Do();
            }

            return(new RangeMapping <TKey>(this.Manager, this.ShardMap, mappingToAdd));
        }
Exemplo n.º 22
0
        /// <summary>
        /// Splits the given mapping into 2 at the given key. The new mappings point to the same shard
        /// as the existing mapping.
        /// </summary>
        /// <param name="existingMapping">Given existing mapping.</param>
        /// <param name="splitAt">Split point.</param>
        /// <param name="lockOwnerId">Lock owner id of this mapping</param>
        /// <returns>Read-only collection of 2 new mappings thus created.</returns>
        internal IReadOnlyList <RangeMapping <TKey> > Split(RangeMapping <TKey> existingMapping, TKey splitAt, Guid lockOwnerId)
        {
            this.EnsureMappingBelongsToShardMap <RangeMapping <TKey> >(
                existingMapping,
                "Split",
                "existingMapping");

            ShardKey shardKey = new ShardKey(ShardKey.ShardKeyTypeFromType(typeof(TKey)), splitAt);

            if (!existingMapping.Range.Contains(shardKey) ||
                existingMapping.Range.Low == shardKey ||
                existingMapping.Range.High == shardKey)
            {
                throw new ArgumentOutOfRangeException(
                          "splitAt",
                          Errors._ShardMapping_SplitPointOutOfRange);
            }

            IStoreShard newShard = new DefaultStoreShard(
                existingMapping.Shard.StoreShard.Id,
                Guid.NewGuid(),
                existingMapping.ShardMapId,
                existingMapping.Shard.StoreShard.Location,
                existingMapping.Shard.StoreShard.Status);

            IStoreMapping mappingToRemove = new DefaultStoreMapping(
                existingMapping.StoreMapping.Id,
                existingMapping.StoreMapping.ShardMapId,
                newShard,
                existingMapping.StoreMapping.MinValue,
                existingMapping.StoreMapping.MaxValue,
                existingMapping.StoreMapping.Status,
                existingMapping.StoreMapping.LockOwnerId);

            IStoreMapping[] mappingsToAdd = new IStoreMapping[2]
            {
                new DefaultStoreMapping(
                    Guid.NewGuid(),
                    newShard.ShardMapId,
                    newShard,
                    existingMapping.Range.Low.RawValue,
                    shardKey.RawValue,
                    (int)existingMapping.Status,
                    lockOwnerId),
                new DefaultStoreMapping(
                    Guid.NewGuid(),
                    newShard.ShardMapId,
                    newShard,
                    shardKey.RawValue,
                    existingMapping.Range.High.RawValue,
                    (int)existingMapping.Status,
                    lockOwnerId)
            };

            using (IStoreOperation op = this.Manager.StoreOperationFactory.CreateReplaceMappingsOperation(
                       this.Manager,
                       StoreOperationCode.SplitMapping,
                       this.ShardMap.StoreShardMap,
                       new[] { new Tuple <IStoreMapping, Guid>(mappingToRemove, lockOwnerId) },
                       mappingsToAdd.Select(mappingToAdd => new Tuple <IStoreMapping, Guid>(mappingToAdd, lockOwnerId)).ToArray()))
            {
                op.Do();
            }

            return(mappingsToAdd
                   .Select(m => new RangeMapping <TKey>(this.Manager, this.ShardMap, m))
                   .ToList()
                   .AsReadOnly());
        }
 public RangeMapping <TKey> UpdateMapping(RangeMapping <TKey> currentMapping, RangeMappingUpdate update)
 {
     return(this.UpdateMapping(currentMapping, update, MappingLockToken.NoLock));
 }
 public IReadOnlyList <RangeMapping <TKey> > SplitMapping(RangeMapping <TKey> existingMapping, TKey splitAt)
 {
     return(this.SplitMapping(existingMapping, splitAt, MappingLockToken.NoLock));
 }
Exemplo n.º 25
0
 /// <summary>
 /// Adds a range mapping.
 /// </summary>
 /// <param name="mapping">Mapping being added.</param>
 /// <returns>The added mapping object.</returns>
 public RangeMapping <TKey> Add(RangeMapping <TKey> mapping)
 {
     return(this.Add <RangeMapping <TKey> >(
                mapping,
                (smm, sm, ssm) => new RangeMapping <TKey>(smm, sm, ssm)));
 }
 public void DeleteMapping(RangeMapping <TKey> mapping)
 {
     this.DeleteMapping(mapping, MappingLockToken.NoLock);
 }