1+ // Licensed to the Apache Software Foundation (ASF) under one
2+ // or more contributor license agreements. See the NOTICE file
3+ // distributed with this work for additional information
4+ // regarding copyright ownership. The ASF licenses this file
5+ // to you under the Apache License, Version 2.0 (the
6+ // "License"); you may not use this file except in compliance
7+ // the License. You may obtain a copy of the License at
8+ //
9+ // http://www.apache.org/licenses/LICENSE-2.0
10+ //
11+ // Unless required by applicable law or agreed to in writing,
12+ // software distributed under the License is distributed on an
13+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+ // KIND, either express or implied. See the License for the
15+ // specific language governing permissions and limitations
16+ // under the License.
17+ package com .cloud .utils .db ;
18+
19+ import java .sql .SQLException ;
20+ import java .util .ArrayList ;
21+ import java .util .HashMap ;
22+ import java .util .List ;
23+ import java .util .Map ;
24+ import java .util .Properties ;
25+
26+ import com .mysql .jdbc .BalanceStrategy ;
27+ import com .mysql .jdbc .Connection ;
28+ import com .mysql .jdbc .ConnectionImpl ;
29+ import com .mysql .jdbc .LoadBalancingConnectionProxy ;
30+ import com .mysql .jdbc .SQLError ;
31+
32+ public class StaticStrategy implements BalanceStrategy {
33+
34+ public StaticStrategy () {
35+ }
36+
37+ public void destroy () {
38+ // we don't have anything to clean up
39+ }
40+
41+ public void init (Connection conn , Properties props ) throws SQLException {
42+ // we don't have anything to initialize
43+ }
44+
45+ public ConnectionImpl pickConnection (LoadBalancingConnectionProxy proxy ,
46+ List <String > configuredHosts , Map <String , ConnectionImpl > liveConnections , long [] responseTimes ,
47+ int numRetries ) throws SQLException {
48+ int numHosts = configuredHosts .size ();
49+
50+ SQLException ex = null ;
51+
52+ List <String > whiteList = new ArrayList <String >(numHosts );
53+ whiteList .addAll (configuredHosts );
54+
55+ Map <String , Long > blackList = proxy .getGlobalBlacklist ();
56+
57+ whiteList .removeAll (blackList .keySet ());
58+
59+ Map <String , Integer > whiteListMap = this .getArrayIndexMap (whiteList );
60+
61+
62+ for (int attempts = 0 ; attempts < numRetries ;) {
63+ if (whiteList .size () == 0 ){
64+ throw SQLError .createSQLException ("No hosts configured" , null );
65+ }
66+
67+ String hostPortSpec = whiteList .get (0 ); //Always take the first host
68+
69+ ConnectionImpl conn = liveConnections .get (hostPortSpec );
70+
71+ if (conn == null ) {
72+ try {
73+ conn = proxy .createConnectionForHost (hostPortSpec );
74+ } catch (SQLException sqlEx ) {
75+ ex = sqlEx ;
76+
77+ if (proxy .shouldExceptionTriggerFailover (sqlEx )) {
78+
79+ Integer whiteListIndex = whiteListMap .get (hostPortSpec );
80+
81+ // exclude this host from being picked again
82+ if (whiteListIndex != null ) {
83+ whiteList .remove (whiteListIndex .intValue ());
84+ whiteListMap = this .getArrayIndexMap (whiteList );
85+ }
86+ proxy .addToGlobalBlacklist ( hostPortSpec );
87+
88+ if (whiteList .size () == 0 ) {
89+ attempts ++;
90+ try {
91+ Thread .sleep (250 );
92+ } catch (InterruptedException e ) {
93+ }
94+
95+ // start fresh
96+ whiteListMap = new HashMap <String , Integer >(numHosts );
97+ whiteList .addAll (configuredHosts );
98+ blackList = proxy .getGlobalBlacklist ();
99+
100+ whiteList .removeAll (blackList .keySet ());
101+ whiteListMap = this .getArrayIndexMap (whiteList );
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+
121+ private Map <String , Integer > getArrayIndexMap (List <String > l ) {
122+ Map <String , Integer > m = new HashMap <String , Integer >(l .size ());
123+ for (int i = 0 ; i < l .size (); i ++) {
124+ m .put (l .get (i ), Integer .valueOf (i ));
125+ }
126+ return m ;
127+
128+ }
129+
130+ }
0 commit comments