private ReplicationServer GetServerByGroup(FabricServerModeEnum mode, string groupId) { if (!FabricGroups.ContainsKey(groupId)) { throw new MySqlFabricException(string.Format(Properties.Resources.errorGroupNotFound, groupId)); } var serversInGroup = FabricGroups[groupId].Servers.Where(i => (i.Mode & mode) != 0 && (i.Status == FabricServerStatusEnum.Primary || i.Status == FabricServerStatusEnum.Secondary)).ToList(); if (serversInGroup.Count == 0) { return(null); } double random_weight = new Random().NextDouble() * serversInGroup.Sum(i => i.Weight); double sum_weight = 0.0; foreach (FabricServer server in serversInGroup) { sum_weight += server.Weight; if (sum_weight > random_weight) { return(server.ReplicationServerInstance); } } return(serversInGroup.Last().ReplicationServerInstance); }
public FabricServer(Guid serverUuid, string groupId, string host, int port, FabricServerModeEnum mode, FabricServerStatusEnum status, float weight, string user, string passowrd) { ServerUuid = serverUuid; GroupId = groupId; Host = host; Port = port; Mode = mode; Status = status; Weight = weight; ReplicationServerInstance = new ReplicationServer( serverUuid.ToString(), mode == FabricServerModeEnum.Read_Write || mode == FabricServerModeEnum.Write_only, string.Format("server={0};port={1};uid={2};password={3};", host, port, user, passowrd) ); }
private ReplicationServer GetServerByShard(FabricServerModeEnum mode) { FabricShardTable shardTable = FabricShardTablesPerTable[tableProperty]; object key = keyProperty; if (shardTable.TypeShard == FabricShardIndexType.Hash) { key = GetMd5Hash(key.ToString()); } foreach (FabricShardIndex idx in shardTable.Indexes) { if (ShardKeyCompare(key, idx.LowerBound, idx.Type) >= 0) { return(GetServerByGroup(mode, idx.GroupId)); } } if (shardTable.TypeShard == FabricShardIndexType.Hash) { return(GetServerByGroup(mode, shardTable.Indexes.First().GroupId)); } return(null); }
public static void SetFabricProperties(this MySqlConnection connection, string groupId, string table, string key, FabricServerModeEnum? mode, FabricScopeEnum? scope) #endif { if (!string.IsNullOrEmpty(groupId) && !string.IsNullOrEmpty(table)) throw new MySqlFabricException(Properties.Resources.errorGroupAndTable); if (!string.IsNullOrEmpty(groupId)) { connection.Settings.FabricGroup = groupId; connection.Settings.ShardingTable = null; connection.Settings.ShardingKey = null; } if (!string.IsNullOrEmpty(table)) { connection.Settings.ShardingTable = table; connection.Settings.FabricGroup = null; } /*if (!string.IsNullOrEmpty(key)) */ connection.Settings.ShardingKey = key; connection.Settings.FabricServerMode = (int?)mode; connection.Settings.FabricScope = (int?)scope; }
internal protected override ReplicationServer GetServer(bool isMaster) { if (string.IsNullOrEmpty(groupIdProperty) && string.IsNullOrEmpty(tableProperty)) { throw new MySqlFabricException(Properties.Resources.errorNotGroupNorTable); } FabricServerModeEnum mode = modeProperty.HasValue ? modeProperty.Value : (isMaster ? FabricServerModeEnum.Read_Write : FabricServerModeEnum.Read_only); lock (_lockObject) { /* * Pick a server using this algorithm. * * */ if (string.IsNullOrEmpty(tableProperty)) { return(GetServerByGroup(mode, groupIdProperty)); } else { ReplicationServer server = GetServerByShard(mode); // Ensure the database is the current shard db. string[] db = tableProperty.Split('.'); string conStr = server.ConnectionString; if (conStr.IndexOf("database=" + db[0] + ";") == -1) { MySqlConnectionStringBuilder msb = new MySqlConnectionStringBuilder(conStr); msb.Database = db[0]; server.ConnectionString = msb.ToString(); } // return server return(server); } } }
private ReplicationServer GetServerByGroup(FabricServerModeEnum mode, string groupId) { var serversInGroup = FabricGroups[groupId].Servers.Where(i => (i.Mode & mode) != 0).ToList(); if (serversInGroup.Count == 0) return null; double random_weight = new Random().NextDouble() * serversInGroup.Sum(i => i.Weight); double sum_weight = 0.0; foreach (FabricServer server in serversInGroup) { sum_weight += server.Weight; if (sum_weight > random_weight) return server.ReplicationServerInstance; } return serversInGroup.Last().ReplicationServerInstance; }
private ReplicationServer GetServerByShard(FabricServerModeEnum mode) { FabricShardTable shardTable = FabricShardTablesPerTable[tableProperty]; object key = keyProperty; if (shardTable.TypeShard == FabricShardIndexType.Hash) key = GetMd5Hash(key.ToString()); foreach( FabricShardIndex idx in shardTable.Indexes ) { if ( ShardKeyCompare( key, idx.LowerBound, idx.Type ) >= 0 ) { return GetServerByGroup(mode, idx.GroupId); } } if (shardTable.TypeShard == FabricShardIndexType.Hash) { return GetServerByGroup(mode, shardTable.Indexes.First().GroupId); } return null; }
public static void SetFabricProperties(this MySqlConnection connection, string groupId = null, string table = null, string key = null, FabricServerModeEnum? mode = null, FabricScopeEnum? scope = null)