Exemple #1
0
        /// <summary> This app will be for automating on-boarding of new tenant </summary>
        static void Main(string[] args)
        {
            Console.WriteLine($"Starting Shard Registration For Tenant:{args[0]}");
            var tenantKey           = args[0];
            var sqlShardDBServer    = args[1];
            var sqlShardDB          = args[2];
            var shardedDBConnection = args[3];

            ShardMapFactory     shardMapFactory = new ShardMapFactory(shardedDBConnection);
            ListShardMap <Guid> shardMap        = (ListShardMap <Guid>)shardMapFactory.CreateShardMap();


            string appserver   = sqlShardDBServer;
            string appdatabase = sqlShardDB;
            Guid   TenantId    = Guid.Parse(tenantKey);

            var shardLocation = new ShardLocation(appserver, appdatabase);

            if (!shardMap.TryGetShard(shardLocation, out var shard))
            {
                shard = shardMap.CreateShard(new ShardLocation(appserver, appdatabase));
            }
            PointMapping <Guid> mapping;

            if (!shardMap.TryGetMappingForKey(TenantId, out mapping))
            {
                shardMap.CreatePointMapping(TenantId, shard);
            }
        }
        public void CreateIfNotExists(ListShardMap <TKey> shardMap, Shard shard, TKey key)
        {
            var mappingExists = shardMap.TryGetMappingForKey(key, out var pointMapping);

            if (!mappingExists)
            {
                shardMap.CreatePointMapping(key, shard);
            }
        }
        public bool TryGetMappingForKey(TKey value, out IMappingInfoProvider mapping)
        {
            if (_lsm != null)
            {
                bool ret = _lsm.TryGetMappingForKey(value, out var x);
                mapping = x;
                return(ret);
            }

            if (_rsm != null)
            {
                bool ret = _rsm.TryGetMappingForKey(value, out var x);
                mapping = x;
                return(ret);
            }

            throw new InvalidOperationException("Both lsm and rsm are null!");
        }
Exemple #4
0
        public bool TryAddTenant(T tenantId, string shardName)
        {
            var database = $"{_shardingConfig.ShardPrefix}.{shardName}";
            var location = new ShardLocation(_shardingConfig.ShardServer, database);

            if (!_shardMap.TryGetMappingForKey(tenantId, out _) && _shardMap.TryGetShard(location, out _))
            {
                lock (_tenantLock)
                {
                    if (!_shardMap.TryGetMappingForKey(tenantId, out _) && _shardMap.TryGetShard(location, out Shard shard))
                    {
                        _shardMap.CreatePointMapping(new PointMappingCreationInfo <T>(tenantId, shard, MappingStatus.Online));
                        return(true);
                    }
                }
            }
            return(false);
        }
        public void LoadTestAddPointMapping()
        {
            try
            {
                ShardMapManager smm = ShardMapManagerFactory.GetSqlShardMapManager(
                    Globals.ShardMapManagerConnectionString,
                    ShardMapManagerLoadPolicy.Lazy);

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

                Assert.IsNotNull(lsm);
                do
                {
                    // Chose a random shard to add mapping.
                    Shard s = GetRandomOnlineShardFromShardMap((ShardMap)lsm);
                    if (s == null)
                    {
                        continue;
                    }

                    // Create a random integer key for a new mapping and verify that its not already present in this shard map.
                    int key = _r.Next(MinMappingPoint, MaxMappingPoint);
                    PointMapping <int> pExisting = null;

                    // choose different mapping if this one already exists.
                    if (lsm.TryGetMappingForKey(key, out pExisting))
                    {
                        continue;
                    }

                    Debug.WriteLine("Trying to add point mapping for key {0} to shard location {1}", key, s.Location);

                    PointMapping <int> p1 = lsm.CreatePointMapping(key, s);

                    Assert.IsNotNull(p1);

                    // Validate mapping by trying to connect
                    s_retryPolicy.ExecuteAction(
                        () => ValidateImpl(
                            (ShardMap)lsm,
                            key));
                }while (false);
            }
            catch (ShardManagementException sme)
            {
                Debug.WriteLine("Exception caught: {0}", sme.Message);
            }
        }
Exemple #6
0
        public async Task MoveShard(ListShardMap <Guid> shardMap, Guid point, string newDbName, Func <Stream> shardInitializerStript, Action <string> stepOverCallBack, string dbEdition = "Basic")
        {
            var callBack = stepOverCallBack ?? (x => { });

            if (string.IsNullOrWhiteSpace(newDbName))
            {
                callBack("No se pudo determinar el nombre de la location del shard (dbname).");
            }
            await dataBase.Verify(conf.UniverseServerName, newDbName, shardInitializerStript, callBack, dbEdition);

            PointMapping <Guid> mapping;

            if (!shardMap.TryGetMappingForKey(point, out mapping))
            {
                return;
            }

            var shardLocation = new ShardLocation(conf.UniverseServerName, newDbName);
            var shard         = CreateOrGetShard(shardMap, shardLocation);

            shardMap.UpdateMapping(mapping, new PointMappingUpdate {
                Shard = shard
            });
        }
        private bool RefreshShardListMap(List <Tuple <long, string> > ListMappings = null)
        {
            try
            {
                string shardServer1 = WingtipTicketApp.Config.PrimaryDatabaseServer;
                string shardServer2 = WingtipTicketApp.Config.ShardDatabaseServer;
                string ticketsDbName = WingtipTicketApp.Config.TicketsDbName;
                Shard  shard1 = null, shard2 = null;
                PointMapping <long> lmpg;

                // check if shard map manager exists and if not, create it (Idempotent / tolerant of re-execute)
                if (!ShardMapManager.TryGetListShardMap(mapManagerName, out lsm))
                {
                    lsm = ShardMapManager.CreateListShardMap <long>(mapManagerName);
                }

                try
                {
                    // check if shard exists and if not, create it (Idempotent / tolerant of re-execute)
                    if (!lsm.TryGetShard(new ShardLocation(shardServer1, ticketsDbName), out shard1))
                    {
                        shard1 = lsm.CreateShard(new ShardLocation(shardServer1, ticketsDbName));
                    }
                }
                catch // sometimes, it may throw an error stating that a concurrent user recently changed some settings.
                      //This is a retry logic to cover this scenario.
                {
                    Thread.Sleep(500);
                    // check if shard map manager exists and if not, create it (Idempotent / tolerant of re-execute)
                    if (!ShardMapManager.TryGetListShardMap(mapManagerName, out lsm))
                    {
                        lsm = ShardMapManager.CreateListShardMap <long>(mapManagerName);
                    }
                    // check if shard exists and if not, create it (Idempotent / tolerant of re-execute)
                    if (!lsm.TryGetShard(new ShardLocation(shardServer1, ticketsDbName), out shard1))
                    {
                        shard1 = lsm.CreateShard(new ShardLocation(shardServer1, ticketsDbName));
                    }
                }
                if (!lsm.TryGetShard(new ShardLocation(shardServer2, ticketsDbName), out shard2))
                {
                    shard2 = lsm.CreateShard(new ShardLocation(shardServer2, ticketsDbName));
                }

                // Check if mapping exists and if not, create it (Idempotent / tolerant of re-execute)

                if (ListMappings != null)
                {
                    foreach (Tuple <long, string> mapping in ListMappings)
                    {
                        if (!lsm.TryGetMappingForKey(mapping.Item1, out lmpg))
                        {
                            if (mapping.Item2 == shardServer1)
                            {
                                lsm.CreatePointMapping(new PointMappingCreationInfo <long>(mapping.Item1, shard1, MappingStatus.Online));
                            }
                            else if (mapping.Item2 == shardServer2)
                            {
                                lsm.CreatePointMapping(new PointMappingCreationInfo <long>(mapping.Item1, shard2, MappingStatus.Online));
                            }
                        }
                    }
                }
                return(true);
            }
            catch { return(false); }
        }