Repository: cloudstack Updated Branches: refs/heads/4.3 21172a2dc -> 717440883
Move mysql ha strategy to a plugin. Activate this plugin using the mysqlha profile or the noredist flag (cherry picked from commit a0f932437c9ac9a0a82cc9b45b1aa6f17e8a478a) Signed-off-by: Animesh Chaturvedi <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/472a387d Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/472a387d Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/472a387d Branch: refs/heads/4.3 Commit: 472a387d2d828e21aff93a750ed3e178837a1879 Parents: 21172a2 Author: Hugo Trippaers <[email protected]> Authored: Thu Feb 27 09:48:55 2014 +0100 Committer: Animesh Chaturvedi <[email protected]> Committed: Thu Mar 6 16:07:32 2014 -0800 ---------------------------------------------------------------------- client/pom.xml | 15 +++ framework/db/pom.xml | 5 - .../src/com/cloud/utils/db/StaticStrategy.java | 130 ------------------- plugins/database/mysql-ha/pom.xml | 28 ++++ .../src/com/cloud/utils/db/StaticStrategy.java | 130 +++++++++++++++++++ plugins/pom.xml | 11 ++ 6 files changed, 184 insertions(+), 135 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/472a387d/client/pom.xml ---------------------------------------------------------------------- diff --git a/client/pom.xml b/client/pom.xml index 745c1d9..94e23d1 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -733,6 +733,21 @@ </dependencies> </profile> <profile> + <id>mysqlha</id> + <activation> + <property> + <name>noredist</name> + </property> + </activation> + <dependencies> + <dependency> + <groupId>org.apache.cloudstack</groupId> + <artifactId>cloud-plugin-database-myqslha</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + </profile> + <profile> <id>quickcloud</id> <activation> <property> http://git-wip-us.apache.org/repos/asf/cloudstack/blob/472a387d/framework/db/pom.xml ---------------------------------------------------------------------- diff --git a/framework/db/pom.xml b/framework/db/pom.xml index 2a8f310..412d497 100644 --- a/framework/db/pom.xml +++ b/framework/db/pom.xml @@ -44,11 +44,6 @@ <artifactId>cloud-utils</artifactId> <version>${project.version}</version> </dependency> - <dependency> - <groupId>mysql</groupId> - <artifactId>mysql-connector-java</artifactId> - <scope>provided</scope> - </dependency> </dependencies> <build> <plugins> http://git-wip-us.apache.org/repos/asf/cloudstack/blob/472a387d/framework/db/src/com/cloud/utils/db/StaticStrategy.java ---------------------------------------------------------------------- diff --git a/framework/db/src/com/cloud/utils/db/StaticStrategy.java b/framework/db/src/com/cloud/utils/db/StaticStrategy.java deleted file mode 100644 index 29e96df..0000000 --- a/framework/db/src/com/cloud/utils/db/StaticStrategy.java +++ /dev/null @@ -1,130 +0,0 @@ -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -package com.cloud.utils.db; - -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; - -import com.mysql.jdbc.BalanceStrategy; -import com.mysql.jdbc.Connection; -import com.mysql.jdbc.ConnectionImpl; -import com.mysql.jdbc.LoadBalancingConnectionProxy; -import com.mysql.jdbc.SQLError; - -public class StaticStrategy implements BalanceStrategy { - - public StaticStrategy() { - } - - public void destroy() { - // we don't have anything to clean up - } - - public void init(Connection conn, Properties props) throws SQLException { - // we don't have anything to initialize - } - - public ConnectionImpl pickConnection(LoadBalancingConnectionProxy proxy, - List<String> configuredHosts, Map<String, ConnectionImpl> liveConnections, long[] responseTimes, - int numRetries) throws SQLException { - int numHosts = configuredHosts.size(); - - SQLException ex = null; - - List<String> whiteList = new ArrayList<String>(numHosts); - whiteList.addAll(configuredHosts); - - Map<String, Long> blackList = proxy.getGlobalBlacklist(); - - whiteList.removeAll(blackList.keySet()); - - Map<String, Integer> whiteListMap = this.getArrayIndexMap(whiteList); - - - for (int attempts = 0; attempts < numRetries;) { - if(whiteList.size() == 0){ - throw SQLError.createSQLException("No hosts configured", null); - } - - String hostPortSpec = whiteList.get(0); //Always take the first host - - ConnectionImpl conn = liveConnections.get(hostPortSpec); - - if (conn == null) { - try { - conn = proxy.createConnectionForHost(hostPortSpec); - } catch (SQLException sqlEx) { - ex = sqlEx; - - if (proxy.shouldExceptionTriggerFailover(sqlEx)) { - - Integer whiteListIndex = whiteListMap.get(hostPortSpec); - - // exclude this host from being picked again - if (whiteListIndex != null) { - whiteList.remove(whiteListIndex.intValue()); - whiteListMap = this.getArrayIndexMap(whiteList); - } - proxy.addToGlobalBlacklist( hostPortSpec ); - - if (whiteList.size() == 0) { - attempts++; - try { - Thread.sleep(250); - } catch (InterruptedException e) { - } - - // start fresh - whiteListMap = new HashMap<String, Integer>(numHosts); - whiteList.addAll(configuredHosts); - blackList = proxy.getGlobalBlacklist(); - - whiteList.removeAll(blackList.keySet()); - whiteListMap = this.getArrayIndexMap(whiteList); - } - - continue; - } - - throw sqlEx; - } - } - - return conn; - } - - if (ex != null) { - throw ex; - } - - return null; // we won't get here, compiler can't tell - } - - private Map<String, Integer> getArrayIndexMap(List<String> l) { - Map<String, Integer> m = new HashMap<String, Integer>(l.size()); - for (int i = 0; i < l.size(); i++) { - m.put(l.get(i), Integer.valueOf(i)); - } - return m; - - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/472a387d/plugins/database/mysql-ha/pom.xml ---------------------------------------------------------------------- diff --git a/plugins/database/mysql-ha/pom.xml b/plugins/database/mysql-ha/pom.xml new file mode 100644 index 0000000..5ce6d6f --- /dev/null +++ b/plugins/database/mysql-ha/pom.xml @@ -0,0 +1,28 @@ +<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor + license agreements. See the NOTICE file distributed with this work for additional + information regarding copyright ownership. The ASF licenses this file to you under + the Apache License, Version 2.0 (the "License"); you may not use this file except + in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software distributed under + the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS + OF ANY KIND, either express or implied. See the License for the specific language + governing permissions and limitations under the License. --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <artifactId>cloud-plugin-database-mysqlha</artifactId> + <name>Apache CloudStack Plugin - MySQL HA Strategy</name> + <parent> + <groupId>org.apache.cloudstack</groupId> + <artifactId>cloudstack-plugins</artifactId> + <version>4.4.0-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + <dependencies> + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <scope>provided</scope> + </dependency> + </dependencies> +</project> http://git-wip-us.apache.org/repos/asf/cloudstack/blob/472a387d/plugins/database/mysql-ha/src/com/cloud/utils/db/StaticStrategy.java ---------------------------------------------------------------------- diff --git a/plugins/database/mysql-ha/src/com/cloud/utils/db/StaticStrategy.java b/plugins/database/mysql-ha/src/com/cloud/utils/db/StaticStrategy.java new file mode 100644 index 0000000..29e96df --- /dev/null +++ b/plugins/database/mysql-ha/src/com/cloud/utils/db/StaticStrategy.java @@ -0,0 +1,130 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.utils.db; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import com.mysql.jdbc.BalanceStrategy; +import com.mysql.jdbc.Connection; +import com.mysql.jdbc.ConnectionImpl; +import com.mysql.jdbc.LoadBalancingConnectionProxy; +import com.mysql.jdbc.SQLError; + +public class StaticStrategy implements BalanceStrategy { + + public StaticStrategy() { + } + + public void destroy() { + // we don't have anything to clean up + } + + public void init(Connection conn, Properties props) throws SQLException { + // we don't have anything to initialize + } + + public ConnectionImpl pickConnection(LoadBalancingConnectionProxy proxy, + List<String> configuredHosts, Map<String, ConnectionImpl> liveConnections, long[] responseTimes, + int numRetries) throws SQLException { + int numHosts = configuredHosts.size(); + + SQLException ex = null; + + List<String> whiteList = new ArrayList<String>(numHosts); + whiteList.addAll(configuredHosts); + + Map<String, Long> blackList = proxy.getGlobalBlacklist(); + + whiteList.removeAll(blackList.keySet()); + + Map<String, Integer> whiteListMap = this.getArrayIndexMap(whiteList); + + + for (int attempts = 0; attempts < numRetries;) { + if(whiteList.size() == 0){ + throw SQLError.createSQLException("No hosts configured", null); + } + + String hostPortSpec = whiteList.get(0); //Always take the first host + + ConnectionImpl conn = liveConnections.get(hostPortSpec); + + if (conn == null) { + try { + conn = proxy.createConnectionForHost(hostPortSpec); + } catch (SQLException sqlEx) { + ex = sqlEx; + + if (proxy.shouldExceptionTriggerFailover(sqlEx)) { + + Integer whiteListIndex = whiteListMap.get(hostPortSpec); + + // exclude this host from being picked again + if (whiteListIndex != null) { + whiteList.remove(whiteListIndex.intValue()); + whiteListMap = this.getArrayIndexMap(whiteList); + } + proxy.addToGlobalBlacklist( hostPortSpec ); + + if (whiteList.size() == 0) { + attempts++; + try { + Thread.sleep(250); + } catch (InterruptedException e) { + } + + // start fresh + whiteListMap = new HashMap<String, Integer>(numHosts); + whiteList.addAll(configuredHosts); + blackList = proxy.getGlobalBlacklist(); + + whiteList.removeAll(blackList.keySet()); + whiteListMap = this.getArrayIndexMap(whiteList); + } + + continue; + } + + throw sqlEx; + } + } + + return conn; + } + + if (ex != null) { + throw ex; + } + + return null; // we won't get here, compiler can't tell + } + + private Map<String, Integer> getArrayIndexMap(List<String> l) { + Map<String, Integer> m = new HashMap<String, Integer>(l.size()); + for (int i = 0; i < l.size(); i++) { + m.put(l.get(i), Integer.valueOf(i)); + } + return m; + + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/472a387d/plugins/pom.xml ---------------------------------------------------------------------- diff --git a/plugins/pom.xml b/plugins/pom.xml index 4021aec..4f7805a 100755 --- a/plugins/pom.xml +++ b/plugins/pom.xml @@ -168,6 +168,17 @@ </modules> </profile> <profile> + <id>mysqlha</id> + <activation> + <property> + <name>noredist</name> + </property> + </activation> + <modules> + <module>database/mysql-ha</module> + </modules> + </profile> + <profile> <id>simulator</id> <activation> <property>
