GUACAMOLE-102: Merge support for weighted least-connections (WLC) balancing algorithm.

This commit is contained in:
Michael Jumper
2017-06-06 17:30:47 -07:00
11 changed files with 192 additions and 18 deletions

View File

@@ -81,7 +81,7 @@ public abstract class JDBCEnvironment extends LocalEnvironment {
* If an error occurs while retrieving the property.
*/
public abstract int getDefaultMaxConnections() throws GuacamoleException;
/**
* Returns the default maximum number of concurrent connections to allow to
* any one connection group, unless specified differently on an individual

View File

@@ -54,6 +54,13 @@ public class ConnectionModel extends ChildObjectModel {
*/
private Integer maxConnectionsPerUser;
/**
* The weight of the connection for the purposes of calculating
* WLC algorithm. null indicates nothing has been set, and anything less
* than 1 eliminates the system from being used for connections.
*/
private Integer connectionWeight;
/**
* The identifiers of all readable sharing profiles associated with this
* connection.
@@ -164,6 +171,31 @@ public class ConnectionModel extends ChildObjectModel {
return maxConnectionsPerUser;
}
/**
* Sets the connection weight for load balancing.
*
* @param connectionWeight
* The weight of the connection used in load balancing.
* The value is not required for the connection (null), and
* values less than 1 will prevent the connection from being
* used.
*/
public void setConnectionWeight(Integer connectionWeight) {
this.connectionWeight = connectionWeight;
}
/**
* Returns the connection weight used in applying weighted
* load balancing algorithms.
*
* @return
* The connection weight used in applying weighted
* load balancing aglorithms.
*/
public Integer getConnectionWeight() {
return connectionWeight;
}
/**
* Sets the maximum number of connections that can be established to this
* connection concurrently by any one user.

View File

@@ -116,6 +116,11 @@ public class ModeledConnection extends ModeledChildDirectoryObject<ConnectionMod
*/
public static final String MAX_CONNECTIONS_PER_USER_NAME = "max-connections-per-user";
/**
* The connection weight attribute used for weighted load balancing algorithms.
*/
public static final String CONNECTION_WEIGHT = "weight";
/**
* All attributes related to restricting user accounts, within a logical
* form.
@@ -125,12 +130,20 @@ public class ModeledConnection extends ModeledChildDirectoryObject<ConnectionMod
new NumericField(MAX_CONNECTIONS_PER_USER_NAME)
));
/**
* All attributes related to load balancing in a logical form.
*/
public static final Form LOAD_BALANCING = new Form("load-balancing", Arrays.<Field>asList(
new NumericField(CONNECTION_WEIGHT)
));
/**
* All possible attributes of connection objects organized as individual,
* logical forms.
*/
public static final Collection<Form> ATTRIBUTES = Collections.unmodifiableCollection(Arrays.asList(
CONCURRENCY_LIMITS,
LOAD_BALANCING,
GUACD_PARAMETERS
));
@@ -265,6 +278,9 @@ public class ModeledConnection extends ModeledChildDirectoryObject<ConnectionMod
}
}
// Set connection weight
attributes.put(CONNECTION_WEIGHT, NumericField.format(getModel().getConnectionWeight()));
return attributes;
}
@@ -310,6 +326,13 @@ public class ModeledConnection extends ModeledChildDirectoryObject<ConnectionMod
else
getModel().setProxyEncryptionMethod(null);
// Translate connection weight attribute
try { getModel().setConnectionWeight(NumericField.parse(attributes.get(CONNECTION_WEIGHT))); }
catch (NumberFormatException e) {
logger.warn("Not setting the connection weight: {}", e.getMessage());
logger.debug("Unable to parse numeric attribute.", e);
}
}
/**
@@ -393,6 +416,23 @@ public class ModeledConnection extends ModeledChildDirectoryObject<ConnectionMod
port != null ? port : defaultConfig.getPort(),
encryptionMethod != null ? encryptionMethod : defaultConfig.getEncryptionMethod()
);
}
/**
* Returns the weight of the connection used in applying weighted
* load balancing algorithms, or a default of 1 if the
* attribute is undefined.
*
* @return
* The weight of the connection used in applying weighted
* load balancing algorithms.
*/
public int getConnectionWeight() {
Integer connectionWeight = getModel().getConnectionWeight();
if (connectionWeight == null)
return 1;
return connectionWeight;
}

View File

@@ -33,6 +33,8 @@ import org.apache.guacamole.GuacamoleResourceConflictException;
import org.apache.guacamole.auth.jdbc.JDBCEnvironment;
import org.apache.guacamole.auth.jdbc.connectiongroup.ModeledConnectionGroup;
import org.apache.guacamole.auth.jdbc.user.RemoteAuthenticatedUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
@@ -44,6 +46,11 @@ import org.apache.guacamole.auth.jdbc.user.RemoteAuthenticatedUser;
public class RestrictedGuacamoleTunnelService
extends AbstractGuacamoleTunnelService {
/**
* Logger for this class.
*/
private static final Logger logger = LoggerFactory.getLogger(RestrictedGuacamoleTunnelService.class);
/**
* The environment of the Guacamole server.
*/
@@ -180,8 +187,24 @@ public class RestrictedGuacamoleTunnelService
@Override
public int compare(ModeledConnection a, ModeledConnection b) {
return getActiveConnections(a).size()
- getActiveConnections(b).size();
// Active connections
int connA = getActiveConnections(a).size();
int connB = getActiveConnections(b).size();
// Assigned weight
int weightA = a.getConnectionWeight();
int weightB = b.getConnectionWeight();
// Calculated weight of connections
int calcWeightA = connA * weightB;
int calcWeightB = connB * weightA;
// If calculated weights are equal, return difference in assigned weight
if (calcWeightA == calcWeightB)
return (weightA - weightB);
// Return different in calculated weights
return (calcWeightA - calcWeightB);
}
@@ -193,6 +216,12 @@ public class RestrictedGuacamoleTunnelService
// Return the first unreserved connection
for (ModeledConnection connection : sortedConnections) {
// If connection weight is less than 1 this host is disabled and should not be used.
if (connection.getConnectionWeight() < 1) {
logger.debug("Weight for {} is < 1, connection will be skipped.", connection.getName());
continue;
}
// Attempt to aquire connection according to per-user limits
Seat seat = new Seat(username, connection.getIdentifier());
if (tryAdd(activeSeats, seat,

View File

@@ -20,6 +20,8 @@
"FIELD_HEADER_MAX_CONNECTIONS" : "Maximum number of connections:",
"FIELD_HEADER_MAX_CONNECTIONS_PER_USER" : "Maximum number of connections per user:",
"FIELD_HEADER_WEIGHT" : "Connection weight:",
"FIELD_HEADER_GUACD_HOSTNAME" : "Hostname:",
"FIELD_HEADER_GUACD_ENCRYPTION" : "Encryption:",
"FIELD_HEADER_GUACD_PORT" : "Port:",
@@ -28,8 +30,9 @@
"FIELD_OPTION_GUACD_ENCRYPTION_NONE" : "None (unencrypted)",
"FIELD_OPTION_GUACD_ENCRYPTION_SSL" : "SSL / TLS",
"SECTION_HEADER_CONCURRENCY" : "Concurrency Limits",
"SECTION_HEADER_GUACD" : "Guacamole Proxy Parameters (guacd)"
"SECTION_HEADER_CONCURRENCY" : "Concurrency Limits",
"SECTION_HEADER_LOAD_BALANCING" : "Load Balancing",
"SECTION_HEADER_GUACD" : "Guacamole Proxy Parameters (guacd)"
},

View File

@@ -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`),

View File

@@ -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-connection weight
--
ALTER TABLE guacamole_connection
ADD COLUMN connection_weight int(11);

View File

@@ -37,6 +37,7 @@
<result column="proxy_port" property="proxyPort" jdbcType="INTEGER"/>
<result column="proxy_encryption_method" property="proxyEncryptionMethod" jdbcType="VARCHAR"
javaType="org.apache.guacamole.net.auth.GuacamoleProxyConfiguration$EncryptionMethod"/>
<result column="connection_weight" property="connectionWeight" jdbcType="INTEGER"/>
<!-- Associated sharing profiles -->
<collection property="sharingProfileIdentifiers" resultSet="sharingProfiles" ofType="java.lang.String"
@@ -95,7 +96,8 @@
max_connections_per_user,
proxy_hostname,
proxy_port,
proxy_encryption_method
proxy_encryption_method,
connection_weight
FROM guacamole_connection
WHERE connection_id IN
<foreach collection="identifiers" item="identifier"
@@ -126,7 +128,8 @@
max_connections_per_user,
proxy_hostname,
proxy_port,
proxy_encryption_method
proxy_encryption_method,
connection_weight
FROM guacamole_connection
JOIN guacamole_connection_permission ON guacamole_connection_permission.connection_id = guacamole_connection.connection_id
WHERE guacamole_connection.connection_id IN
@@ -162,7 +165,8 @@
max_connections_per_user,
proxy_hostname,
proxy_port,
proxy_encryption_method
proxy_encryption_method,
connection_weight
FROM guacamole_connection
WHERE
<if test="parentIdentifier != null">parent_id = #{parentIdentifier,jdbcType=VARCHAR}</if>
@@ -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}
#{object.proxyEncryptionMethod,jdbcType=VARCHAR},
#{object.connectionWeight,jdbcType=INTEGER}
)
</insert>
@@ -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}
proxy_encryption_method = #{object.proxyEncryptionMethod,jdbcType=VARCHAR},
connection_weight = #{object.connectionWeight,jdbcType=INTEGER}
WHERE connection_id = #{object.objectID,jdbcType=INTEGER}
</update>

View File

@@ -106,6 +106,9 @@ CREATE TABLE guacamole_connection (
max_connections integer,
max_connections_per_user integer,
-- Connection Weight
connection_weight integer,
-- Guacamole proxy (guacd) overrides
proxy_port integer,
proxy_hostname varchar(512),

View File

@@ -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-connection weight
--
ALTER TABLE guacamole_connection
ADD COLUMN connection_weight int;

View File

@@ -37,6 +37,7 @@
<result column="proxy_port" property="proxyPort" jdbcType="INTEGER"/>
<result column="proxy_encryption_method" property="proxyEncryptionMethod" jdbcType="VARCHAR"
javaType="org.apache.guacamole.net.auth.GuacamoleProxyConfiguration$EncryptionMethod"/>
<result column="connection_weight" property="connectionWeight" jdbcType="INTEGER"/>
<!-- Associated sharing profiles -->
<collection property="sharingProfileIdentifiers" resultSet="sharingProfiles" ofType="java.lang.String"
@@ -95,7 +96,8 @@
max_connections_per_user,
proxy_hostname,
proxy_port,
proxy_encryption_method
proxy_encryption_method,
connection_weight
FROM guacamole_connection
WHERE connection_id IN
<foreach collection="identifiers" item="identifier"
@@ -126,7 +128,8 @@
max_connections_per_user,
proxy_hostname,
proxy_port,
proxy_encryption_method
proxy_encryption_method,
connection_weight
FROM guacamole_connection
JOIN guacamole_connection_permission ON guacamole_connection_permission.connection_id = guacamole_connection.connection_id
WHERE guacamole_connection.connection_id IN
@@ -162,7 +165,8 @@
max_connections_per_user,
proxy_hostname,
proxy_port,
proxy_encryption_method
proxy_encryption_method,
connection_weight
FROM guacamole_connection
WHERE
<if test="parentIdentifier != null">parent_id = #{parentIdentifier,jdbcType=INTEGER}::integer</if>
@@ -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}
)
</insert>
@@ -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
</update>