You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
5.9 KiB

4 years ago
  1. #if MYSQL_6_9
  2. // Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
  3. //
  4. // MySQL Connector/NET is licensed under the terms of the GPLv2
  5. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  6. // MySQL Connectors. There are special exceptions to the terms and
  7. // conditions of the GPLv2 as it is applied to this software, see the
  8. // FLOSS License Exception
  9. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  10. //
  11. // This program is free software; you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published
  13. // by the Free Software Foundation; version 2 of the License.
  14. //
  15. // This program is distributed in the hope that it will be useful, but
  16. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. // for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License along
  21. // with this program; if not, write to the Free Software Foundation, Inc.,
  22. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. using Externals.MySql.Data.MySqlClient.Properties;
  24. using System;
  25. using System.Collections.Generic;
  26. using System.ComponentModel;
  27. using System.Text;
  28. namespace Externals.MySql.Data.MySqlClient.Replication
  29. {
  30. /// <summary>
  31. /// Base class used to implement load balancing features
  32. /// </summary>
  33. internal abstract class ReplicationServerGroup
  34. {
  35. protected List<ReplicationServer> servers = new List<ReplicationServer>();
  36. /// <param name="name">Group name</param>
  37. /// <param name="retryTime"></param>
  38. public ReplicationServerGroup(string name, int retryTime)
  39. {
  40. Servers = servers;
  41. Name = name;
  42. RetryTime = retryTime;
  43. }
  44. /// <summary>
  45. /// Group name
  46. /// </summary>
  47. public string Name { get; protected set; }
  48. /// <summary>
  49. /// Retry time between connections to failed servers
  50. /// </summary>
  51. public int RetryTime { get; protected set; }
  52. /// <summary>
  53. /// Servers list in the group
  54. /// </summary>
  55. protected IList<ReplicationServer> Servers { get; private set; }
  56. /// <summary>
  57. /// Adds a server into the group
  58. /// </summary>
  59. /// <param name="name">Server name</param>
  60. /// <param name="isMaster">True if the server to add is master, False for slave server</param>
  61. /// <param name="connectionString">Connection string used by this server</param>
  62. /// <returns></returns>
  63. internal protected ReplicationServer AddServer(string name, bool isMaster, string connectionString)
  64. {
  65. ReplicationServer server = new ReplicationServer(name, isMaster, connectionString);
  66. servers.Add(server);
  67. return server;
  68. }
  69. /// <summary>
  70. /// Removes a server from group
  71. /// </summary>
  72. /// <param name="name">Server name</param>
  73. internal protected void RemoveServer(string name)
  74. {
  75. ReplicationServer serverToRemove = GetServer(name);
  76. if (serverToRemove == null)
  77. throw new MySqlException(String.Format(Resources.ReplicationServerNotFound, name));
  78. servers.Remove(serverToRemove);
  79. }
  80. /// <summary>
  81. /// Gets a server by name
  82. /// </summary>
  83. /// <param name="name">Server name</param>
  84. /// <returns>Replication server</returns>
  85. internal protected ReplicationServer GetServer(string name)
  86. {
  87. foreach (var server in servers)
  88. if (String.Compare(name, server.Name, StringComparison.OrdinalIgnoreCase) == 0) return server;
  89. return null;
  90. }
  91. /// <summary>
  92. /// Must be implemented. Defines the next server for a custom load balancing implementation.
  93. /// </summary>
  94. /// <param name="isMaster">Defines if the server to return is a master or any</param>
  95. /// <returns>Next server based on the load balancing implementation.
  96. /// Null if no available server is found.
  97. /// </returns>
  98. internal protected abstract ReplicationServer GetServer(bool isMaster);
  99. internal protected virtual ReplicationServer GetServer(bool isMaster, MySqlConnectionStringBuilder settings)
  100. {
  101. return GetServer(isMaster);
  102. }
  103. /// <summary>
  104. /// Handles a failed connection to a server.
  105. /// This method can be overrided to implement a custom failover handling
  106. /// </summary>
  107. /// <param name="server">The failed server</param>
  108. internal protected virtual void HandleFailover(ReplicationServer server)
  109. {
  110. BackgroundWorker worker = new BackgroundWorker();
  111. worker.DoWork += delegate(object sender, DoWorkEventArgs e)
  112. {
  113. bool isRunning = false;
  114. ReplicationServer server1 = e.Argument as ReplicationServer;
  115. System.Timers.Timer timer = new System.Timers.Timer(RetryTime * 1000.0);
  116. System.Timers.ElapsedEventHandler elapsedEvent = delegate(object sender1, System.Timers.ElapsedEventArgs e1)
  117. {
  118. if (isRunning) return;
  119. try
  120. {
  121. isRunning = true;
  122. using (MySqlConnection connectionFailed = new MySqlConnection(server.ConnectionString))
  123. {
  124. connectionFailed.Open();
  125. server1.IsAvailable = true;
  126. timer.Stop();
  127. }
  128. }
  129. catch
  130. {
  131. MySqlTrace.LogWarning(0,
  132. string.Format(Properties.Resources.Replication_ConnectionAttemptFailed, server1.Name));
  133. }
  134. finally
  135. {
  136. isRunning = false;
  137. }
  138. };
  139. timer.Elapsed += elapsedEvent;
  140. timer.Start();
  141. elapsedEvent(sender, null);
  142. };
  143. worker.RunWorkerAsync(server);
  144. }
  145. /// <summary>
  146. /// Handles a failed connection to a server.
  147. /// </summary>
  148. /// <param name="server">The failed server</param>
  149. /// <param name="exception">Exception that caused the failover</param>
  150. internal protected virtual void HandleFailover(ReplicationServer server, Exception exception)
  151. {
  152. HandleFailover(server);
  153. }
  154. }
  155. }
  156. #endif