// Copyright ?2014, 2020, Oracle and/or its affiliates.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 2.0, as
// published by the Free Software Foundation.
//
// This program is also distributed with certain software (including
// but not limited to OpenSSL) that is licensed under separate terms,
// as designated in a particular file or component or in included license
// documentation. The authors of MySQL hereby grant you an
// additional permission to link the program and your derivative works
// with the separately licensed software that they have included with
// MySQL.
//
// Without limiting anything contained in the foregoing, this file,
// which is part of MySQL Connector/NET, is also subject to the
// Universal FOSS Exception, version 1.0, a copy of which can be found at
// http://oss.oracle.com/licenses/universal-foss-exception.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License, version 2.0, for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using MySql.Data.MySqlClient;
using Sog.Properties;
namespace MySql.Data.MySqlClient.Replication
{
///
/// Manager for Replication and Load Balancing features
///
internal static class ReplicationManager
{
private static List groups = new List();
private static Object thisLock = new Object();
// private static Dictionary selectors = new Dictionary();
///
/// Returns Replication Server Group List
///
internal static IList Groups { get; private set; }
///
/// Adds a Default Server Group to the list
///
/// Group name
/// Time between reconnections for failed servers
/// Replication Server Group added
internal static ReplicationServerGroup AddGroup(string name, int retryTime)
{
return AddGroup(name, null, retryTime);
}
///
/// Adds a Server Group to the list
///
/// Group name
/// ServerGroup type reference
/// Time between reconnections for failed servers
/// Server Group added
internal static ReplicationServerGroup AddGroup(string name, string groupType, int retryTime)
{
if (string.IsNullOrEmpty(groupType))
{
groupType = "MySql.Data.MySqlClient.Replication.ReplicationRoundRobinServerGroup";
}
Type t = Type.GetType(groupType);
ReplicationServerGroup g = (ReplicationServerGroup)Activator.CreateInstance(t, name, retryTime) as ReplicationServerGroup;
groups.Add(g);
return g;
}
///
/// Gets the next server from a replication group
///
/// Group name
/// True if the server to return must be a source
/// Replication Server defined by the Load Balancing plugin
internal static ReplicationServer GetServer(string groupName, bool isSource)
{
ReplicationServerGroup group = GetGroup(groupName);
return group.GetServer(isSource);
}
///
/// Gets a Server Group by name
///
/// Group name
/// Server Group if found, otherwise throws an MySqlException
internal static ReplicationServerGroup GetGroup(string groupName)
{
ReplicationServerGroup group = null;
foreach (ReplicationServerGroup g in groups)
{
if (String.Compare(g.Name, groupName, StringComparison.OrdinalIgnoreCase) != 0)
{
continue;
}
group = g;
break;
}
if (group == null)
{
throw new MySqlException(String.Format(Resources.ReplicationGroupNotFound, groupName));
}
return group;
}
///
/// Validates if the replication group name exists
///
/// Group name to validate
/// true if the replication group name is found; otherwise, false
internal static bool IsReplicationGroup(string groupName)
{
foreach (ReplicationServerGroup g in groups)
{
if (String.Compare(g.Name, groupName, StringComparison.OrdinalIgnoreCase) == 0)
{
return true;
}
}
return false;
}
///
/// Assigns a new server driver to the connection object
///
/// Group name
/// True if the server connection to assign must be a source
/// MySqlConnection object where the new driver will be assigned
internal static void GetNewConnection(string groupName, bool source, MySqlConnection connection)
{
do
{
lock (thisLock)
{
if (!IsReplicationGroup(groupName))
{
return;
}
ReplicationServerGroup group = GetGroup(groupName);
ReplicationServer server = group.GetServer(source, connection.Settings);
if (server == null)
{
throw new MySqlException(Resources.Replication_NoAvailableServer);
}
try
{
bool isNewServer = false;
if (connection.driver == null || !connection.driver.IsOpen)
{
isNewServer = true;
}
else
{
MySqlConnectionStringBuilder msb = new MySqlConnectionStringBuilder(server.ConnectionString);
if (!msb.Equals(connection.driver.Settings))
{
isNewServer = true;
}
}
if (isNewServer)
{
Driver driver = Driver.Create(new MySqlConnectionStringBuilder(server.ConnectionString));
connection.driver = driver;
}
return;
}
catch (MySqlException ex)
{
connection.driver = null;
server.IsAvailable = false;
MySqlTrace.LogError(ex.Number, ex.ToString());
if (ex.Number == 1042)
{
// retry to open a failed connection and update its status
group.HandleFailover(server, ex);
}
else
{
throw;
}
}
}
} while (true);
}
}
}