Skip to content

Commit

Permalink
WFLY-3624 ConsistentHashFactory needs to be configurable via XML
Browse files Browse the repository at this point in the history
Also add capacity-factor.
  • Loading branch information
pferraro committed Aug 1, 2014
1 parent c53e0b8 commit e4b491a
Show file tree
Hide file tree
Showing 19 changed files with 550 additions and 38 deletions.
12 changes: 12 additions & 0 deletions clustering/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-processor</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.clustering.controller.validation;

import org.jboss.as.clustering.logging.ClusteringLogger;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.operations.validation.ModelTypeValidator;
import org.jboss.as.controller.operations.validation.ParameterValidator;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;

/**
* A builder for creating a range validator for {@link ModelType#DOUBLE} parameters.
* @author Paul Ferraro
*/
public class DoubleRangeValidatorBuilder implements ParameterValidatorBuilder {
private Bound upperBound;
private Bound lowerBound;
private boolean allowsUndefined = false;
private boolean allowsExpression = false;

/**
* Sets an inclusive lower bound of this validator.
* @param lowerBound the lower bound
*/
public DoubleRangeValidatorBuilder lowerBound(double value) {
this.lowerBound = new Bound(value, false);
return this;
}

/**
* Sets an exclusive lower bound of this validator.
* @param lowerBound the lower bound
*/
public DoubleRangeValidatorBuilder lowerBoundExclusive(double value) {
this.lowerBound = new Bound(value, true);
return this;
}

/**
* Sets the inclusive upper bound of this validator.
* @param upperBound the upper bound
*/
public DoubleRangeValidatorBuilder upperBound(double value) {
this.upperBound = new Bound(value, false);
return this;
}

/**
* Sets the exclusive upper bound of this validator.
* @param upperBound the upper bound
*/
public DoubleRangeValidatorBuilder upperBoundExclusive(double value) {
this.upperBound = new Bound(value, true);
return this;
}

/**
* Indicates whether {@link ModelType#UNDEFINED} is allowed
* @param allowsUndefined indicates whether {@link ModelType#UNDEFINED} is allowed
*/
public DoubleRangeValidatorBuilder allowUndefined(boolean allowsUndefined) {
this.allowsUndefined = allowsUndefined;
return this;
}

/**
* Indicates whether {@link ModelType#EXPRESSION} is allowed
* @param allowExpressions whether {@link ModelType#EXPRESSION} is allowed
*/
public DoubleRangeValidatorBuilder allowExpression(boolean allowsExpressions) {
this.allowsExpression = allowsExpressions;
return this;
}

/**
* {@inheritDoc}
*/
@Override
public ParameterValidator build() {
return new DoubleRangeValidator(this.lowerBound, this.upperBound, this.allowsUndefined, this.allowsExpression);
}

private static class Bound {
private final double value;
private final boolean exclusive;

Bound(double value, boolean exclusive) {
this.value = value;
this.exclusive = exclusive;
}

double getValue() {
return this.value;
}

boolean isExclusive() {
return this.exclusive;
}
}

private class DoubleRangeValidator extends ModelTypeValidator {
private final Bound lowerBound;
private final Bound upperBound;

/**
* Creates an upper- and lower-bounded validator.
* @param lowerBound the lower bound
* @param lowerBound the upper bound
* @param nullable indicates whether {@link ModelType#UNDEFINED} is allowed
* @param allowExpressions whether {@link ModelType#EXPRESSION} is allowed
*/
DoubleRangeValidator(Bound lowerBound, Bound upperBound, boolean nullable, boolean allowExpressions) {
super(ModelType.DOUBLE, nullable, allowExpressions, false);
this.lowerBound = lowerBound;
this.upperBound = upperBound;
}

/**
* {@inheritDoc}
*/
@Override
public void validateParameter(String parameterName, ModelNode parameterValue) throws OperationFailedException {
super.validateParameter(parameterName, parameterValue);
if (parameterValue.isDefined() && parameterValue.getType() != ModelType.EXPRESSION) {
double value = parameterValue.asDouble();
if (this.lowerBound != null) {
double bound = this.lowerBound.getValue();
boolean exclusive = this.lowerBound.isExclusive();
if ((value < bound) || (exclusive && (value == bound))) {
throw ClusteringLogger.ROOT_LOGGER.parameterValueOutOfBounds(parameterName, value, exclusive ? ">" : ">=", bound);
}
}
if (this.upperBound != null) {
double bound = this.upperBound.getValue();
boolean exclusive = this.upperBound.isExclusive();
if ((value > bound) || (exclusive && (value == bound))) {
throw ClusteringLogger.ROOT_LOGGER.parameterValueOutOfBounds(parameterName, value, exclusive ? "<" : "<=", bound);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.clustering.controller.validation;

import org.jboss.as.controller.operations.validation.ParameterValidator;

public interface ParameterValidatorBuilder {
/**
* Builds the validator.
* @return a parameter validator
*/
ParameterValidator build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.clustering.logging;

import org.jboss.as.controller.OperationFailedException;
import org.jboss.logging.BasicLogger;
import org.jboss.logging.Logger;
import org.jboss.logging.annotations.Message;
import org.jboss.logging.annotations.MessageLogger;

/**
* @author Paul Ferraro
*/
@MessageLogger(projectCode = "WFLYCLCOM", length = 4)
public interface ClusteringLogger extends BasicLogger {

String ROOT_LOGGER_CATEGORY = "org.jboss.as.clustering";

/**
* The root logger.
*/
ClusteringLogger ROOT_LOGGER = Logger.getMessageLogger(ClusteringLogger.class, ROOT_LOGGER_CATEGORY);

@Message(id = 1, value = "%2$g is not a valid value for parameter %1$s. The value must be %3$s %4$g")
OperationFailedException parameterValueOutOfBounds(String name, double value, String relationalOperator, double bound);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.clustering.controller.validation;

import static org.junit.Assert.*;

import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.operations.validation.ParameterValidator;
import org.jboss.dmr.ModelNode;
import org.junit.Test;

/**
* @author Paul Ferraro
*/
public class DoubleRangeValidatorTestCase {

@Test
public void testFloat() {
ParameterValidator validator = new DoubleRangeValidatorBuilder().lowerBound(Float.MIN_VALUE).upperBound(Float.MAX_VALUE).build();
assertFalse(isValid(validator, new ModelNode(Double.MAX_VALUE)));
assertFalse(isValid(validator, new ModelNode(Double.MIN_VALUE)));
assertTrue(isValid(validator, new ModelNode(Float.MAX_VALUE)));
assertTrue(isValid(validator, new ModelNode(Float.MIN_VALUE)));
}

@Test
public void testExclusive() {
int lower = 0;
ParameterValidator validator = new DoubleRangeValidatorBuilder().lowerBoundExclusive(lower).build();
assertFalse(isValid(validator, new ModelNode(0)));
assertTrue(isValid(validator, new ModelNode(0.1)));
assertTrue(isValid(validator, new ModelNode(Double.MAX_VALUE)));

int upper = 1;
validator = new DoubleRangeValidatorBuilder().upperBoundExclusive(upper).build();
assertTrue(isValid(validator, new ModelNode(Double.MIN_VALUE)));
assertTrue(isValid(validator, new ModelNode(0.99)));
assertFalse(isValid(validator, new ModelNode(1)));
}

static boolean isValid(ParameterValidator validator, ModelNode value) {
try {
validator.validateParameter("test", value);
return true;
} catch (OperationFailedException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ public enum Attribute {
BATCH_SIZE(ModelKeys.BATCH_SIZE),
@Deprecated BATCHING(ModelKeys.BATCHING),
CACHE(ModelKeys.CACHE),
CAPACITY_FACTOR(ModelKeys.CAPACITY_FACTOR),
CHUNK_SIZE(ModelKeys.CHUNK_SIZE),
CLASS(ModelKeys.CLASS),
CLUSTER(ModelKeys.CLUSTER),
CONCURRENCY_LEVEL(ModelKeys.CONCURRENCY_LEVEL),
CONSISTENT_HASH_STRATEGY(ModelKeys.CONSISTENT_HASH_STRATEGY),
DATASOURCE(ModelKeys.DATASOURCE),
DEFAULT_CACHE(ModelKeys.DEFAULT_CACHE),
@Deprecated DEFAULT_CACHE_CONTAINER("default-cache-container"),
Expand Down

0 comments on commit e4b491a

Please sign in to comment.