コード例 #1
0
        public void LoadTestRemoveShardFromListShardMap()
        {
            try
            {
                ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                    Globals.ShardMapManagerConnectionString,
                    ShardMapManagerLoadPolicy.Lazy);

                ListShardMap <int> lsm = smm.GetListShardMap <int>(ShardMapManagerLoadTests.s_listShardMapName);
                Assert.IsNotNull(lsm);

                List <Shard> existingShards = lsm.GetShards().ToList();

                if (existingShards.Count == 0)
                {
                    return;
                }

                // If there is already a shard marked as offline, chose that one to delete.
                // This can happend if earlier remove operation was terminated for some reason - ex. killing connections.
                Shard offlineShard = existingShards.Find(e => e.Status == ShardStatus.Offline);

                if (offlineShard == null)
                {
                    offlineShard = existingShards[_r.Next(existingShards.Count)];

                    // First mark shard as offline so that other test threads will not add new mappings to it.
                    offlineShard = lsm.UpdateShard(offlineShard,
                                                   new ShardUpdate
                    {
                        Status = ShardStatus.Offline
                    });
                }

                Debug.WriteLine("Trying to remove shard at location {0}", offlineShard.Location);

                PointMappingUpdate pu = new PointMappingUpdate();
                pu.Status = MappingStatus.Offline;

                // Remove all mappings from this shard for given shard map.
                foreach (PointMapping <int> p in lsm.GetMappings(offlineShard))
                {
                    PointMapping <int> mappingToDelete = lsm.UpdateMapping(p, pu);
                    lsm.DeleteMapping(mappingToDelete);
                }

                // Shard object is changed as mappings are removed, get it again.
                Shard deleteShard = lsm.GetShard(offlineShard.Location);

                // now remove shard.
                lsm.DeleteShard(deleteShard);

                Debug.WriteLine("Removed shard at location {0} from shard map {1}", deleteShard.Location, lsm);
            }
            catch (ShardManagementException sme)
            {
                Debug.WriteLine("Exception caught: {0}", sme.Message);
            }
        }
コード例 #2
0
        /// <summary>
        /// Get existing point mapping from a list shard map
        /// </summary>
        /// <param name="lsm">List shard map</param>
        /// <returns>Valid existing point mapping, null if no point mappings found in given shard map</returns>
        private PointMapping <int> GetRandomPointMapping(ListShardMap <int> lsm)
        {
            // Get all point mappings without storing the results in the cache so that OpenConnection will fetch mapping again.
            IReadOnlyList <PointMapping <int> > allMappings = lsm.GetMappings();

            if (allMappings.Count == 0)
            {
                return(null);
            }

            int index = _r.Next(allMappings.Count);

            return(allMappings[index]);
        }
コード例 #3
0
        private Shard FindEmptyShard(ListShardMap <int> shardMap)
        {
            // Get all shards in the shard map
            IEnumerable <Shard> allShards = shardMap.GetShards();

            // Get all mappings in the shard map
            IEnumerable <PointMapping <int> > allMappings = shardMap.GetMappings();

            // Determine which shards have mappings
            HashSet <Shard> shardsWithMappings = new HashSet <Shard>(allMappings.Select(m => m.Shard));

            // Get the first shard (ordered by name) that has no mappings, if it exists
            return(allShards.OrderBy(s => s.Location.Database).FirstOrDefault(s => !shardsWithMappings.Contains(s)));
        }
コード例 #4
0
        public bool DeleteShardListMap()
        {
            try
            {
                if (ShardMapManager == null)
                {
                    return(false);
                }
                Shard tempShard = null;
                PointMapping <long> tempMapping = null;

                if (ShardMapManager.TryGetListShardMap(mapManagerName, out lsm))
                {
                    foreach (Shard shard in lsm.GetShards())
                    {
                        // delete all mappings
                        var allMappings = lsm.GetMappings(shard);
                        for (int i = 0; i < allMappings.Count; i++)
                        {
                            lsm.MarkMappingOffline(allMappings[i]);
                            if (lsm.TryGetMappingForKey(allMappings[i].Value, out tempMapping))
                            {
                                lsm.DeleteMapping(tempMapping);
                            }
                        }
                        // delete shard
                        if (lsm.TryGetShard(shard.Location, out tempShard))
                        {
                            lsm.DeleteShard(tempShard);
                        }
                    }
                }
                // clear shard map manager
                if (ShardMapManager.TryGetListShardMap(mapManagerName, out lsm))
                {
                    ShardMapManager.DeleteShardMap(lsm);
                }

                return(true);
            }
            catch { return(false); }
        }
コード例 #5
0
        /// <summary>
        /// Writes the shard map's state to the console.
        /// </summary>
        private static void PrintShardMapState()
        {
            Console.WriteLine("Current Shard Map state:");
            ListShardMap <int> shardMap = TryGetShardMap();

            if (shardMap == null)
            {
                return;
            }

            // Get all shards
            IEnumerable <Shard> allShards = shardMap.GetShards();

            // Get all mappings, grouped by the shard that they are on. We do this all in one go to minimise round trips.
            //ILookup<Shard, RangeMapping<int>> mappingsGroupedByShard = shardMap.GetMappings().ToLookup(m => m.Shard);
            var mappingsGroupedByShard = shardMap.GetMappings().ToLookup(m => m.Shard);

            if (allShards.Any())
            {
                // The shard map contains some shards, so for each shard (sorted by database name)
                // write out the mappings for that shard
                foreach (Shard shard in shardMap.GetShards().OrderBy(s => s.Location.Database))
                {
                    var mappingsOnThisShard = mappingsGroupedByShard[shard];

                    if (mappingsOnThisShard.Any())
                    {
                        string mappingsString = string.Join(", ", mappingsOnThisShard.Select(m => m.Value));
                        Console.WriteLine("\t{0} contains key range {1}", shard.Location.Database, mappingsString);
                    }
                    else
                    {
                        Console.WriteLine("\t{0} contains no key ranges.", shard.Location.Database);
                    }
                }
            }
            else
            {
                Console.WriteLine("\tShard Map contains no shards");
            }
        }
コード例 #6
0
        public void DateGetPointMappingsForRange()
        {
            ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                Globals.ShardMapManagerConnectionString,
                ShardMapManagerLoadPolicy.Lazy);

            ListShardMap <DateTime> lsm = smm.GetListShardMap <DateTime>(DateTimeShardMapperTests.s_listShardMapName);

            Assert.IsNotNull(lsm);

            Shard s1 = lsm.CreateShard(new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, DateTimeShardMapperTests.s_shardedDBs[0]));

            Assert.IsNotNull(s1);

            Shard s2 = lsm.CreateShard(new ShardLocation(Globals.ShardMapManagerTestsDatasourceName, DateTimeShardMapperTests.s_shardedDBs[1]));

            Assert.IsNotNull(s2);

            DateTime val1 = DateTime.Now.Subtract(TimeSpan.FromMinutes(10));
            PointMapping <DateTime> p1 = lsm.CreatePointMapping(val1, s1);

            Assert.IsNotNull(p1);

            DateTime val2 = DateTime.Now.Subtract(TimeSpan.FromMinutes(20));
            PointMapping <DateTime> p2 = lsm.CreatePointMapping(val2, s1);

            Assert.IsNotNull(p2);

            DateTime val3 = DateTime.Now.Subtract(TimeSpan.FromMinutes(30));
            PointMapping <DateTime> p3 = lsm.CreatePointMapping(val3, s2);

            Assert.IsNotNull(p2);

            // Get all mappings in shard map.
            int count = 0;
            IEnumerable <PointMapping <DateTime> > allMappings = lsm.GetMappings();

            using (IEnumerator <PointMapping <DateTime> > mEnum = allMappings.GetEnumerator())
            {
                while (mEnum.MoveNext())
                {
                    count++;
                }
            }
            Assert.AreEqual(3, count);

            // Get all mappings in specified range.
            Range <DateTime> wantedRange = new Range <DateTime>(val3.AddMinutes(-5), val3.AddMinutes(15));

            count = 0;
            IEnumerable <PointMapping <DateTime> > mappingsInRange = lsm.GetMappings(wantedRange);

            using (IEnumerator <PointMapping <DateTime> > mEnum = mappingsInRange.GetEnumerator())
            {
                while (mEnum.MoveNext())
                {
                    count++;
                }
            }
            Assert.AreEqual(2, count);

            // Get all mappings for a shard.
            count = 0;
            IEnumerable <PointMapping <DateTime> > mappingsForShard = lsm.GetMappings(s1);

            using (IEnumerator <PointMapping <DateTime> > mEnum = mappingsForShard.GetEnumerator())
            {
                while (mEnum.MoveNext())
                {
                    count++;
                }
            }
            Assert.AreEqual(2, count);

            // Get all mappings in specified range for a particular shard.
            count = 0;
            IEnumerable <PointMapping <DateTime> > mappingsInRangeForShard = lsm.GetMappings(wantedRange, s1);

            using (IEnumerator <PointMapping <DateTime> > mEnum = mappingsInRangeForShard.GetEnumerator())
            {
                while (mEnum.MoveNext())
                {
                    count++;
                }
            }
            Assert.AreEqual(1, count);
        }
コード例 #7
0
        /// <summary>
        /// Get existing point mapping from a list shard map
        /// </summary>
        /// <param name="lsm">List shard map</param>
        /// <returns>Valid existing point mapping, null if no point mappings found in given shard map</returns>
        private PointMapping<int> GetRandomPointMapping(ListShardMap<int> lsm)
        {
            // Get all point mappings without storing the results in the cache so that OpenConnection will fetch mapping again.
            IReadOnlyList<PointMapping<int>> allMappings = lsm.GetMappings();

            if (allMappings.Count == 0)
                return null;

            int index = _r.Next(allMappings.Count);
            return allMappings[index];
        }