GUACAMOLE-102: Initial addition of connection weight to JDBC authentication extension

This commit is contained in:
Nick Couchman
2017-03-19 11:57:28 -04:00
committed by Nick Couchman
parent 91920d0b2d
commit 025f77d1c4
11 changed files with 180 additions and 8 deletions

View File

@@ -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

View File

@@ -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.

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 for the WRR algorithm.
*/
public static final String CONNECTION_WEIGHT = "connection-weight";
/**
* All attributes related to restricting user accounts, within a logical
* form.
@@ -265,6 +270,9 @@ public class ModeledConnection extends ModeledChildDirectoryObject<ConnectionMod
}
}
// Set connection weight
attributes.put(CONNECTION_WEIGHT, NumericField.format(getModel().getConnectionWeight()));
return attributes;
}
@@ -310,6 +318,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);
}
}
/**
@@ -394,6 +409,25 @@ public class ModeledConnection extends ModeledChildDirectoryObject<ConnectionMod
encryptionMethod != null ? encryptionMethod : defaultConfig.getEncryptionMethod()
);
/**
* Returns the weight of the connection, or the default.
*
* @return
* The weight of the connection.
*
* @throws GuacamoleException
* If an error occurs while parsing the concurrency limit properties
* specified within guacamole.properties.
*/
public int getConnectionWeight() throws GuacamoleException {
// Pull default from environment if weight is unset
Integer value = getModel().getConnectionWeight();
if (value == null)
return environment.getDefaultConnectionWeight();
// Otherwise use defined value
return value;
}
}

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.
*/
@@ -166,20 +173,24 @@ public class RestrictedGuacamoleTunnelService
protected ModeledConnection acquire(RemoteAuthenticatedUser user,
List<ModeledConnection> 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<ModeledConnection>() {
@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();