source: Ballon/out/artifacts/geisa_artifact/WEB-INF/lib/mysql-connector-java-5.1.21/src/com/mysql/jdbc/SequentialBalanceStrategy.java

Last change on this file was 766, checked in by npipsl, 11 years ago
File size: 4.5 KB
Line 
1/*
2 Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4
5  The MySQL Connector/J is licensed under the terms of the GPLv2
6  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
7  There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
8  this software, see the 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 it under the terms
12  of the GNU General Public License as published by the Free Software Foundation; version 2
13  of the License.
14
15  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
16  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17  See the GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License along with this
20  program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
21  Floor, Boston, MA 02110-1301  USA
22 
23 */
24package com.mysql.jdbc;
25
26import java.sql.SQLException;
27import java.util.List;
28import java.util.Map;
29import java.util.Properties;
30
31/**
32 * A balancing strategy that starts at a random point, and then advances
33 * in the list (wrapping around) for each new pickConnection() call.
34 *
35 * The initial point selection, and subsequent point selections are
36 * blacklist-aware.
37 *
38 */
39public class SequentialBalanceStrategy implements BalanceStrategy {
40        private int currentHostIndex = -1;
41       
42        public SequentialBalanceStrategy() {
43        }
44
45        public void destroy() {
46                // we don't have anything to clean up
47        }
48
49        public void init(Connection conn, Properties props) throws SQLException {
50                // we don't have anything to initialize
51        }
52
53        public ConnectionImpl pickConnection(LoadBalancingConnectionProxy proxy,
54                        List<String> configuredHosts, Map<String, ConnectionImpl> liveConnections, long[] responseTimes,
55                        int numRetries) throws SQLException {
56                int numHosts = configuredHosts.size();
57
58                SQLException ex = null;
59
60                Map<String, Long> blackList = proxy.getGlobalBlacklist();
61
62                for (int attempts = 0; attempts < numRetries;) {
63                        if (numHosts == 1) {
64                                currentHostIndex = 0; // pathological case
65                        } else  if (currentHostIndex == -1) {
66                                int random = (int) Math.floor((Math.random() * numHosts));
67                               
68                                for (int i = random; i < numHosts; i++) {
69                                        if (!blackList.containsKey(configuredHosts.get(i))) {
70                                                currentHostIndex = i; 
71                                                break;
72                                        }
73                                }
74                               
75                                if (currentHostIndex == -1) {
76                                        for (int i = 0; i < random; i++) {
77                                                if (!blackList.containsKey(configuredHosts.get(i))) {
78                                                        currentHostIndex = i; 
79                                                        break;
80                                                }
81                                        }
82                                }
83                               
84                                if (currentHostIndex == -1) {
85                                        blackList = proxy.getGlobalBlacklist(); // it may have changed
86                                                                // and the proxy returns a copy
87                                       
88                                        try {
89                                                Thread.sleep(250);
90                                        } catch (InterruptedException e) {
91                                        }
92
93                                        continue; // retry
94                                }
95                        } else {
96
97                               
98                                int i = currentHostIndex + 1;
99                                boolean foundGoodHost = false;
100                               
101                                for (; i < numHosts; i++) {
102                                        if (!blackList.containsKey(configuredHosts.get(i))) {
103                                                currentHostIndex = i;
104                                                foundGoodHost = true;
105                                                break;
106                                        }
107                                }
108
109                                if (!foundGoodHost) {
110                                        for (i = 0; i < currentHostIndex; i++) {
111                                                if (!blackList.containsKey(configuredHosts.get(i))) {
112                                                        currentHostIndex = i;
113                                                        foundGoodHost = true;
114                                                        break;
115                                                }
116                                        }
117                                }
118                       
119                                if (!foundGoodHost) {
120                                        blackList = proxy.getGlobalBlacklist(); // it may have changed
121                                        // and the proxy returns a copy
122               
123                                        try {
124                                                Thread.sleep(250);
125                                        } catch (InterruptedException e) {
126                                        }
127                       
128                                        continue; // retry
129                                }
130                        }
131
132                        String hostPortSpec = configuredHosts.get(currentHostIndex);
133
134                        ConnectionImpl conn = liveConnections.get(hostPortSpec);
135
136                        if (conn == null) {
137                                try {
138                                        conn = proxy.createConnectionForHost(hostPortSpec);
139                                } catch (SQLException sqlEx) {
140                                        ex = sqlEx;
141
142                                        if (sqlEx instanceof CommunicationsException
143                                                        || "08S01".equals(sqlEx.getSQLState())) {
144
145                                                proxy.addToGlobalBlacklist( hostPortSpec );
146
147                                                try {
148                                                        Thread.sleep(250);
149                                                } catch (InterruptedException e) {
150                                                }
151                                               
152                                                continue;
153                                        }
154                                        throw sqlEx;
155                                }
156                        }
157                       
158                        return conn;
159                }
160
161                if (ex != null) {
162                        throw ex;
163                }
164
165                return null; // we won't get here, compiler can't tell
166        }
167
168}
Note: See TracBrowser for help on using the repository browser.