From 025f77d1c40547abafa3a5ed18f341804883493b Mon Sep 17 00:00:00 2001 From: Nick Couchman Date: Sun, 19 Mar 2017 11:57:28 -0400 Subject: [PATCH] GUACAMOLE-102: Initial addition of connection weight to JDBC authentication extension --- .../guacamole/auth/jdbc/JDBCEnvironment.java | 11 ++++++ .../auth/jdbc/connection/ConnectionModel.java | 30 ++++++++++++++++ .../jdbc/connection/ModeledConnection.java | 34 +++++++++++++++++++ .../RestrictedGuacamoleTunnelService.java | 17 +++++++++- .../schema/001-create-schema.sql | 5 ++- .../schema/upgrade/upgrade-pre-0.9.12.sql | 25 ++++++++++++++ .../auth/mysql/MySQLEnvironment.java | 13 +++++++ .../auth/mysql/MySQLGuacamoleProperties.java | 12 +++++++ .../postgresql/PostgreSQLEnvironment.java | 13 +++++++ .../PostgreSQLGuacamoleProperties.java | 9 +++++ .../auth/jdbc/connection/ConnectionMapper.xml | 19 +++++++---- 11 files changed, 180 insertions(+), 8 deletions(-) create mode 100644 extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/upgrade/upgrade-pre-0.9.12.sql diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/JDBCEnvironment.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/JDBCEnvironment.java index dfbf0e9b5..64f2449fe 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/JDBCEnvironment.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/JDBCEnvironment.java @@ -81,6 +81,17 @@ public abstract class JDBCEnvironment extends LocalEnvironment { * If an error occurs while retrieving the property. */ public abstract int getDefaultMaxConnections() throws GuacamoleException; + + /** + * Returns the connection weight for the purpose of WRR calculation + * + * @return + * The weight of the connection. + * + * @throws GuacamoleException + * If an error occurs while retrieving the property. + */ + public abstract int getConnectionWeight() throws GuacamoleException; /** * Returns the default maximum number of concurrent connections to allow to diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ConnectionModel.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ConnectionModel.java index 44f4b3b69..91dd42c43 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ConnectionModel.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ConnectionModel.java @@ -54,6 +54,13 @@ public class ConnectionModel extends ChildObjectModel { */ private Integer maxConnectionsPerUser; + /** + * The weight of the connection for the purposes of calculating + * WRR algorithm. null indicates nothing has been set, -1 indicates + * the system is unavailable. + */ + private Integer connectionWeight; + /** * The identifiers of all readable sharing profiles associated with this * connection. @@ -164,6 +171,29 @@ public class ConnectionModel extends ChildObjectModel { return maxConnectionsPerUser; } + /** + * Sets the connection weight. + * + * @param connectionWeight + * The weight of the connection. null is acceptable, -1 indicates the + * connection should not be used. + */ + public void setConnectionWeight(Integer connectionWeight) { + this.connectionWeight = connectionWeight; + } + + /** + * Returns the connection weight used in calculating the + * WRR algorithm. + * + * @return + * The connection weight. Null indicates no weight has been set, + * -1 indicates that the system is unavailable. + */ + public Integer getConnectionWeight() { + return connectionWeight; + } + /** * Sets the maximum number of connections that can be established to this * connection concurrently by any one user. diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ModeledConnection.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ModeledConnection.java index bcd7b117e..93a7329ca 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ModeledConnection.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ModeledConnection.java @@ -116,6 +116,11 @@ public class ModeledConnection extends ModeledChildDirectoryObject connections) throws GuacamoleException { + logger.trace("Attempting to acquire a connection..."); // Do not acquire connection unless within overall limits if (!tryIncrement(totalActiveConnections, environment.getAbsoluteMaxConnections())) throw new GuacamoleResourceConflictException("Cannot connect. Overall maximum connections reached."); // Get username String username = user.getIdentifier(); + logger.trace("Username is: {}", username); + logger.trace("Sorting {} connections.", connections.size()); // Sort connections in ascending order of usage ModeledConnection[] sortedConnections = connections.toArray(new ModeledConnection[connections.size()]); Arrays.sort(sortedConnections, new Comparator() { @Override public int compare(ModeledConnection a, ModeledConnection b) { - + + logger.trace("Comparing {} to {}.", a.getName(), b.getName()); return getActiveConnections(a).size() - getActiveConnections(b).size(); @@ -194,15 +205,18 @@ public class RestrictedGuacamoleTunnelService for (ModeledConnection connection : sortedConnections) { // Attempt to aquire connection according to per-user limits + logger.trace("Trying to grab a seat on this train: {}", connection.getName()); Seat seat = new Seat(username, connection.getIdentifier()); if (tryAdd(activeSeats, seat, connection.getMaxConnectionsPerUser())) { + logger.trace("Got a seat, trying to get the connection..."); // Attempt to aquire connection according to overall limits if (tryAdd(activeConnections, connection.getIdentifier(), connection.getMaxConnections())) return connection; + logger.trace("Uh-oh, failed to get the connection..."); // Acquire failed - retry with next connection activeSeats.remove(seat); @@ -213,6 +227,7 @@ public class RestrictedGuacamoleTunnelService } + logger.trace("Well, we failed to get a seat at all..."); // Acquire failed totalActiveConnections.decrementAndGet(); diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/001-create-schema.sql b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/001-create-schema.sql index 519d5ca7b..2f0aa7303 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/001-create-schema.sql +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/001-create-schema.sql @@ -34,7 +34,6 @@ CREATE TABLE `guacamole_connection_group` ( `max_connections_per_user` int(11), `enable_session_affinity` boolean NOT NULL DEFAULT 0, - PRIMARY KEY (`connection_group_id`), UNIQUE KEY `connection_group_name_parent` (`connection_group_name`, `parent_id`), @@ -65,6 +64,10 @@ CREATE TABLE `guacamole_connection` ( -- Concurrency limits `max_connections` int(11), `max_connections_per_user` int(11), + + -- Connection weight + `connection_weight` int(11), + PRIMARY KEY (`connection_id`), UNIQUE KEY `connection_name_parent` (`connection_name`, `parent_id`), diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/upgrade/upgrade-pre-0.9.12.sql b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/upgrade/upgrade-pre-0.9.12.sql new file mode 100644 index 000000000..48e2dd1ae --- /dev/null +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/schema/upgrade/upgrade-pre-0.9.12.sql @@ -0,0 +1,25 @@ +-- +-- 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. +-- + +-- +-- Add per-user password set date +-- + +ALTER TABLE guacamole_connection + ADD COLUMN connection_weight int(11); diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/java/org/apache/guacamole/auth/mysql/MySQLEnvironment.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/java/org/apache/guacamole/auth/mysql/MySQLEnvironment.java index 19a8ef448..63d4ae316 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/java/org/apache/guacamole/auth/mysql/MySQLEnvironment.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/java/org/apache/guacamole/auth/mysql/MySQLEnvironment.java @@ -87,6 +87,11 @@ public class MySQLEnvironment extends JDBCEnvironment { */ private int DEFAULT_MAX_CONNECTIONS = 0; + /** + * The default value for the connection weight for a connection in + * a balancing group. + private int DEFAULT_CONNECTION_WEIGHT = 0; + /** * The default value for the default maximum number of connections to be * allowed to any one connection group. Note that, as long as the legacy @@ -194,6 +199,14 @@ public class MySQLEnvironment extends JDBCEnvironment { ); } + @Override + public int getDefaultConnectionWeight() throws GuacamoleException { + return getProperty( + MySQLGuacamoleProperties.MYSQL_DEFAULT_CONNECTION_WEIGHT, + DEFAULT_CONNECTION_WEIGHT + ); + } + @Override public int getDefaultMaxGroupConnections() throws GuacamoleException { return getProperty( diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/java/org/apache/guacamole/auth/mysql/MySQLGuacamoleProperties.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/java/org/apache/guacamole/auth/mysql/MySQLGuacamoleProperties.java index 9039c029b..79f3e7fd5 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/java/org/apache/guacamole/auth/mysql/MySQLGuacamoleProperties.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/java/org/apache/guacamole/auth/mysql/MySQLGuacamoleProperties.java @@ -174,6 +174,18 @@ public class MySQLGuacamoleProperties { }; + /** + * The connection weight for connections in balancing groups. + */ + public static final IntegerGuacamoleProperty + MYSQL_DEFAULT_CONNECTION_WEIGHT = + new IntegerGuacamoleProperty() { + + @Overide + public String getName() { return "mysql-default-connection-weight"; } + + }; + /** * The maximum number of concurrent connections to allow to any one * connection group by an individual user. Zero denotes diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-postgresql/src/main/java/org/apache/guacamole/auth/postgresql/PostgreSQLEnvironment.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-postgresql/src/main/java/org/apache/guacamole/auth/postgresql/PostgreSQLEnvironment.java index e0ee75ff1..539941773 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-postgresql/src/main/java/org/apache/guacamole/auth/postgresql/PostgreSQLEnvironment.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-postgresql/src/main/java/org/apache/guacamole/auth/postgresql/PostgreSQLEnvironment.java @@ -87,6 +87,11 @@ public class PostgreSQLEnvironment extends JDBCEnvironment { */ private int DEFAULT_MAX_CONNECTIONS = 0; + /** + * The default value for the connection weight for a connection in + * a balancing group. + private int DEFAULT_CONNECTION_WEIGHT = 0; + /** * The default value for the default maximum number of connections to be * allowed to any one connection group. Note that, as long as the legacy @@ -194,6 +199,14 @@ public class PostgreSQLEnvironment extends JDBCEnvironment { ); } + @Override + public int getDefaultConnectionWeight() throws GuacamoleException { + return getProperty( + PostgreSQLGuacamoleProperties.POSTGRESQL_DEFAULT_CONNECTION_WEIGHT, + DEFAULT_CONNECTION_WEIGHT + ); + } + @Override public int getDefaultMaxGroupConnections() throws GuacamoleException { return getProperty( diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-postgresql/src/main/java/org/apache/guacamole/auth/postgresql/PostgreSQLGuacamoleProperties.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-postgresql/src/main/java/org/apache/guacamole/auth/postgresql/PostgreSQLGuacamoleProperties.java index 3da972fe7..3ae83ab21 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-postgresql/src/main/java/org/apache/guacamole/auth/postgresql/PostgreSQLGuacamoleProperties.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-postgresql/src/main/java/org/apache/guacamole/auth/postgresql/PostgreSQLGuacamoleProperties.java @@ -157,6 +157,15 @@ public class PostgreSQLGuacamoleProperties { }; + public static final IntegerGuacamoleProperty + POSTGRESQL_DEFAULT_CONNECTION_WEIGHT = + new IntegerGuacamoleProperty() { + + @Override + public String getName() { return "postgresql-default-connection-weight"; } + + }; + /** * The maximum number of concurrent connections to allow to any one * connection group. Zero denotes unlimited. diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-postgresql/src/main/resources/org/apache/guacamole/auth/jdbc/connection/ConnectionMapper.xml b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-postgresql/src/main/resources/org/apache/guacamole/auth/jdbc/connection/ConnectionMapper.xml index 44828fa29..f53e43948 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-postgresql/src/main/resources/org/apache/guacamole/auth/jdbc/connection/ConnectionMapper.xml +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-postgresql/src/main/resources/org/apache/guacamole/auth/jdbc/connection/ConnectionMapper.xml @@ -37,6 +37,7 @@ + parent_id = #{parentIdentifier,jdbcType=INTEGER}::integer @@ -189,7 +193,8 @@ max_connections_per_user, proxy_hostname, proxy_port, - proxy_encryption_method + proxy_encryption_method, + connection_weight ) VALUES ( #{object.name,jdbcType=VARCHAR}, @@ -199,7 +204,8 @@ #{object.maxConnectionsPerUser,jdbcType=INTEGER}, #{object.proxyHostname,jdbcType=VARCHAR}, #{object.proxyPort,jdbcType=INTEGER}, - #{object.proxyEncryptionMethod,jdbcType=VARCHAR}::guacamole_proxy_encryption_method + #{object.proxyEncryptionMethod,jdbcType=VARCHAR}::guacamole_proxy_encryption_method, + #{object.connectionWeight,jdbcType=INTEGER} ) @@ -214,7 +220,8 @@ max_connections_per_user = #{object.maxConnectionsPerUser,jdbcType=INTEGER}, proxy_hostname = #{object.proxyHostname,jdbcType=VARCHAR}, proxy_port = #{object.proxyPort,jdbcType=INTEGER}, - proxy_encryption_method = #{object.proxyEncryptionMethod,jdbcType=VARCHAR}::guacamole_proxy_encryption_method + proxy_encryption_method = #{object.proxyEncryptionMethod,jdbcType=VARCHAR}::guacamole_proxy_encryption_method, + connection_weight = #{object.connectionWeight,jdbcType=INTEGER} WHERE connection_id = #{object.objectID,jdbcType=INTEGER}::integer