1 | /* |
---|
2 | Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. |
---|
3 | |
---|
4 | The MySQL Connector/J is licensed under the terms of the GPLv2 |
---|
5 | <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors. |
---|
6 | There are special exceptions to the terms and conditions of the GPLv2 as it is applied to |
---|
7 | this software, see the FLOSS License Exception |
---|
8 | <http://www.mysql.com/about/legal/licensing/foss-exception.html>. |
---|
9 | |
---|
10 | This program is free software; you can redistribute it and/or modify it under the terms |
---|
11 | of the GNU General Public License as published by the Free Software Foundation; version 2 |
---|
12 | of the License. |
---|
13 | |
---|
14 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
---|
15 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
---|
16 | See the GNU General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License along with this |
---|
19 | program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth |
---|
20 | Floor, Boston, MA 02110-1301 USA |
---|
21 | |
---|
22 | */ |
---|
23 | package com.mysql.jdbc; |
---|
24 | |
---|
25 | import java.sql.SQLException; |
---|
26 | import java.util.List; |
---|
27 | import java.util.Map; |
---|
28 | import java.util.Properties; |
---|
29 | |
---|
30 | public class BestResponseTimeBalanceStrategy implements BalanceStrategy { |
---|
31 | |
---|
32 | /** |
---|
33 | * @param loadBalancingConnectionProxy |
---|
34 | */ |
---|
35 | public BestResponseTimeBalanceStrategy() { |
---|
36 | } |
---|
37 | |
---|
38 | public void destroy() { |
---|
39 | // we don't have anything to clean up |
---|
40 | } |
---|
41 | |
---|
42 | public void init(Connection conn, Properties props) throws SQLException { |
---|
43 | // we don't have anything to initialize |
---|
44 | } |
---|
45 | |
---|
46 | public ConnectionImpl pickConnection(LoadBalancingConnectionProxy proxy, |
---|
47 | List<String> configuredHosts, Map<String, ConnectionImpl> liveConnections, long[] responseTimes, |
---|
48 | int numRetries) throws SQLException { |
---|
49 | |
---|
50 | Map<String, Long> blackList = proxy.getGlobalBlacklist(); |
---|
51 | |
---|
52 | SQLException ex = null; |
---|
53 | |
---|
54 | for (int attempts = 0; attempts < numRetries; ) { |
---|
55 | long minResponseTime = Long.MAX_VALUE; |
---|
56 | |
---|
57 | int bestHostIndex = 0; |
---|
58 | |
---|
59 | // safety |
---|
60 | if (blackList.size() == configuredHosts.size()) { |
---|
61 | blackList = proxy.getGlobalBlacklist(); |
---|
62 | } |
---|
63 | |
---|
64 | for (int i = 0; i < responseTimes.length; i++) { |
---|
65 | long candidateResponseTime = responseTimes[i]; |
---|
66 | |
---|
67 | if (candidateResponseTime < minResponseTime |
---|
68 | && !blackList.containsKey(configuredHosts.get(i))) { |
---|
69 | if (candidateResponseTime == 0) { |
---|
70 | bestHostIndex = i; |
---|
71 | |
---|
72 | break; |
---|
73 | } |
---|
74 | |
---|
75 | bestHostIndex = i; |
---|
76 | minResponseTime = candidateResponseTime; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | String bestHost = configuredHosts.get(bestHostIndex); |
---|
81 | |
---|
82 | ConnectionImpl conn = liveConnections.get(bestHost); |
---|
83 | |
---|
84 | if (conn == null) { |
---|
85 | try { |
---|
86 | conn = proxy.createConnectionForHost(bestHost); |
---|
87 | } catch (SQLException sqlEx) { |
---|
88 | ex = sqlEx; |
---|
89 | |
---|
90 | if (proxy.shouldExceptionTriggerFailover(sqlEx)) { |
---|
91 | proxy.addToGlobalBlacklist(bestHost); |
---|
92 | blackList.put(bestHost, null); |
---|
93 | |
---|
94 | |
---|
95 | if (blackList.size() == configuredHosts.size()) { |
---|
96 | attempts++; |
---|
97 | try { |
---|
98 | Thread.sleep(250); |
---|
99 | } catch (InterruptedException e) { |
---|
100 | } |
---|
101 | blackList = proxy.getGlobalBlacklist(); // try again after a little bit |
---|
102 | } |
---|
103 | |
---|
104 | continue; |
---|
105 | } |
---|
106 | |
---|
107 | throw sqlEx; |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | return conn; |
---|
112 | } |
---|
113 | |
---|
114 | if (ex != null) { |
---|
115 | throw ex; |
---|
116 | } |
---|
117 | |
---|
118 | return null; // we won't get here, compiler can't tell |
---|
119 | } |
---|
120 | } |
---|