Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare Flags for Overhaul #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public SettingsProvider(final String[] args,

@Override
public Settings get() {
log.info("Providing settings.");
log.finest("Providing settings.");

final Collection<Field> annotated = findAnnotatedFields();
final Collection<String> args4jWhitelist = makeArgs4jWhitelist(annotated);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static void write(final String methodName, final Object... values) {
final StringBuilder output = new StringBuilder();
output.append("ExperimentDb ").append(methodName).append(" :");
spaceSeparator.appendTo(output, values);
log.info(output.toString());
log.finest(output.toString());
}

/** The logger is being made available so tests can add a handler */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ enum DataSize {
*/
NONE(""),

/**
* The units are in bytes.
*/
BYTE("b"),

/**
* The units are in kilobytes.
*/
Expand Down Expand Up @@ -94,7 +89,7 @@ public String unitFamilyAsRegexpString() {
for (final DataSize dataSize : values()) {
builder.append(dataSize.suffixToRegexpString());
}
builder.append("]");
builder.append("]?");

DATASIZE_SUFFIX_REGEXP = builder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package org.arbeitspferde.groningen.experimentdb.jvmflags;

import com.google.common.collect.ImmutableList;

/**
* Provide standardized representation and validation of flags.
*
Expand All @@ -30,33 +32,13 @@ interface Formatter {
* @param value The value that the flag shall be set to.
* @return The emitted formatted argument string.
*/
String asArgumentString(final JvmFlag cla, final long value);
ImmutableList<String> asArgumentString(final JvmFlag cla, final long value);

/**
* Provide a representation of the flag with possible values as a reg. exp.
*
* @param cla The argument for which this formatter is used.
* @return The regular expression representation.
*/
String asRegularExpressionString(final JvmFlag cla);

/**
* Provide a representation of the acceptable values the flag may be set to.
*
* @param cla The argument for which this formatter is used.
* @return The valid value representation.
*/
String asAcceptableValuesString(final JvmFlag cla);

/**
* Verify that the proposed value is acceptable.
*
* Implementors should include information in the thrown exception about why
* proposedValue is unacceptable.
*
* @param cla The argument for which this formatter is used.
* @param proposedValue The value to verify.
* @throws IllegalArgumentException if proposedValue is unacceptable.
*/
void validate(final JvmFlag cla, final long proposedValue) throws IllegalArgumentException;
ImmutableList<String> asRegularExpressionString(final JvmFlag cla);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.arbeitspferde.groningen.experimentdb.jvmflags;

import com.google.common.base.Preconditions;
import com.google.common.collect.Range;
import com.google.common.collect.ImmutableList;

/**
* A repository of {@link Formatter} implementations.
Expand All @@ -31,94 +31,77 @@ class Formatters {
/**
* Utility class; no instantiation allowed!
*/
private Formatters() {
}
private Formatters() {}

/**
* Provide a representation and validations for integer-based JVM Flags.
*/
static final Formatter INTEGER_FORMATTER = new Formatter() {
/**
* Validate that numeric values are in the acceptable range for {@link Integer}.
*/
private final Range<Long> inherentAcceptableValues = Range.closed((long) Integer.MIN_VALUE,
(long) Integer.MAX_VALUE);

static final Formatter INTEGER = new Formatter() {
@Override
public String asArgumentString(final JvmFlag cla, final long value) {
public ImmutableList<String> asArgumentString(final JvmFlag cla, final long value) {
Preconditions.checkNotNull(cla, "cla may not be null.");
Preconditions.checkState(cla.hasName(), "cla must have name");

return String.format("%s%s%s%s%s", cla.getHotSpotFlagType().getPrefix(), cla.getName(),
cla.getValueSeparator().getInfix(), value, cla.getDataSize().getSuffix());
return ImmutableList.of(String.format("%s%s%s%s%s", cla.getHotSpotFlagType().getPrefix(), cla.getName(),
cla.getValueSeparator().getInfix(), value, cla.getDataSize().getSuffix()));
}

@Override
public String asRegularExpressionString(final JvmFlag cla) {
public ImmutableList<String> asRegularExpressionString(final JvmFlag cla) {
Preconditions.checkNotNull(cla, "cla may not be null.");
Preconditions.checkState(cla.hasName(), "cla must have name");

return String.format("%s%s%s\\d+%s\\b", cla.getHotSpotFlagType().getPrefix(), cla.getName(),
cla.getValueSeparator().getInfix(), cla.getDataSize().unitFamilyAsRegexpString());
}

@Override
public String asAcceptableValuesString(final JvmFlag cla) {
Preconditions.checkNotNull(cla, "cla may not be null.");

return cla.getAcceptableValueRange().toString();
}

@Override
public void validate(final JvmFlag cla, final long proposedValue)
throws IllegalArgumentException {
Preconditions.checkNotNull(cla, "cla may not be null.");
Preconditions.checkArgument(inherentAcceptableValues.contains(proposedValue));
Preconditions.checkArgument(
cla.getAcceptableValueRange().contains(proposedValue),
"The flag %s with range %s cannot contain proposed value %s.",
cla.getName(), cla.getAcceptableValueRange(),
proposedValue);
return ImmutableList.of(String.format("%s%s%s\\d+%s\\b", cla.getHotSpotFlagType().getPrefix(), cla.getName(),
cla.getValueSeparator().getInfix(), cla.getDataSize().unitFamilyAsRegexpString()));
}
};

/**
* Provide a representation and validations for boolean-based JVM Flags.
*/
static final Formatter BOOLEAN_FORMATTER = new Formatter() {
static final Formatter BOOLEAN = new Formatter() {
private final long TRUE_AS_LONG = 1;

private final Range<Long> inherentAcceptableValues = Range.closed(0L, 1L);

@Override
public String asArgumentString(final JvmFlag cla,
public ImmutableList<String> asArgumentString(final JvmFlag cla,
final long value) {
Preconditions.checkNotNull(cla, "cla may not be null.");
Preconditions.checkState(cla.hasName(), "cla must have name");

final String plusOrMinus = value == TRUE_AS_LONG ? "+" : "-";

return String.format("%s%s%s", cla.getHotSpotFlagType().getPrefix(), plusOrMinus,
cla.getName());
}

@Override
public String asRegularExpressionString(final JvmFlag cla) {
Preconditions.checkNotNull(cla, "cla may not be null.");

return String.format("%s[+-]%s", cla.getHotSpotFlagType().getPrefix(), cla.getName());
return ImmutableList.of(String.format("%s%s%s", cla.getHotSpotFlagType().getPrefix(), plusOrMinus,
cla.getName()));
}

@Override
public String asAcceptableValuesString(final JvmFlag cla) {
public ImmutableList<String> asRegularExpressionString(final JvmFlag cla) {
Preconditions.checkNotNull(cla, "cla may not be null.");
Preconditions.checkState(cla.hasName(), "cla must have name");

return "{0 (false), 1 (true)}";
}

@Override
public void validate(final JvmFlag cla, final long proposedValue)
throws IllegalArgumentException {
Preconditions.checkNotNull(cla, "cla may not be null.");
Preconditions.checkArgument(inherentAcceptableValues.contains(proposedValue));
return ImmutableList.of(String.format("%s[+-]%s", cla.getHotSpotFlagType().getPrefix(), cla.getName()));
}
};

static Formatter pinnedIntervalInteger(final String minimumName, final String maximumName) {
return new Formatter() {
@Override
public ImmutableList<String> asArgumentString(JvmFlag cla, long value) {
return ImmutableList.of(
String.format("%s%s%s%s%s", cla.getHotSpotFlagType().getPrefix(), minimumName,
cla.getValueSeparator().getInfix(), value, cla.getDataSize().getSuffix()),
String.format("%s%s%s%s%s", cla.getHotSpotFlagType().getPrefix(), maximumName,
cla.getValueSeparator().getInfix(), value, cla.getDataSize().getSuffix()));
}

@Override
public ImmutableList<String> asRegularExpressionString(JvmFlag cla) {
return ImmutableList.of(
String.format("%s%s%s\\d+%s\\b", cla.getHotSpotFlagType().getPrefix(), minimumName,
cla.getValueSeparator().getInfix(), cla.getDataSize().unitFamilyAsRegexpString()),
String.format("%s%s%s\\d+%s\\b", cla.getHotSpotFlagType().getPrefix(), maximumName,
cla.getValueSeparator().getInfix(), cla.getDataSize().unitFamilyAsRegexpString()));
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

package org.arbeitspferde.groningen.experimentdb.jvmflags;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Range;

Expand All @@ -34,10 +36,8 @@ public enum JvmFlag {
// Hypothesizer adds one to
// this value and generates a random value from 0 inclusive to
// Integer.MAX_VALUE exclusive.

// TODO(team): Look at better ways of implementing this.
HEAP_SIZE("<special-never-should-be-exposed>", HotSpotFlagType.NON_STANDARD, 1L, 64 * 1024L, 1L,
DataSize.MEGA, ValueSeparator.NONE),
HEAP_SIZE(HotSpotFlagType.NON_STANDARD, 1L, 64 * 1024L, 1L,
DataSize.MEGA, ValueSeparator.NONE, Formatters.pinnedIntervalInteger("ms", "mx")),

ADAPTIVE_SIZE_DECREMENT_SCALE_FACTOR("AdaptiveSizeDecrementScaleFactor",
HotSpotFlagType.UNSTABLE, 0L, 100L, 1L, DataSize.NONE, ValueSeparator.EQUAL),
Expand Down Expand Up @@ -114,7 +114,7 @@ public enum JvmFlag {
* The human-readable flag name that excludes the flag-specific argument
* prefix, infix, suffix, and respective assignment value.
*/
private final String name;
private final Optional<String> name;

/**
* The type of flag that the flag is.
Expand All @@ -126,6 +126,11 @@ public enum JvmFlag {
*/
private final Formatter formatter;

/**
* The {@link Validator} flyweight.
*/
private final Validator validator;

/**
* The minimum acceptable value for the flag.
*/
Expand Down Expand Up @@ -170,10 +175,11 @@ public enum JvmFlag {
* @param hotSpotFlagType The type of the flag.
*/
private JvmFlag(final String name, final HotSpotFlagType hotSpotFlagType) {
this.name = Preconditions.checkNotNull(name, "name may not be null.");
this.name = Optional.of(name);
this.hotSpotFlagType = Preconditions.checkNotNull(hotSpotFlagType, "hotSpotFlagType may not be null.");

formatter = Formatters.BOOLEAN_FORMATTER;
formatter = Formatters.BOOLEAN;
validator = Validators.BOOLEAN;
floorValue = 0L;
ceilingValue = 1L;
stepSize = 1L;
Expand All @@ -194,28 +200,52 @@ private JvmFlag(final String name, final HotSpotFlagType hotSpotFlagType) {
JvmFlag(final String name, final HotSpotFlagType hotSpotFlagType, final Long minimum,
final Long maximum, final Long stepSize, final DataSize dataSize,
final ValueSeparator valueSeparator) {
this.name = Preconditions.checkNotNull(name, "name may not be null.");
this.name = Optional.of(name);
this.hotSpotFlagType = Preconditions.checkNotNull(hotSpotFlagType, "hotSpotFlagType may not be null.");
floorValue = Preconditions.checkNotNull(minimum, "minimum may not be null.");
ceilingValue = Preconditions.checkNotNull(maximum, "maximum may not be null.");
this.stepSize = Preconditions.checkNotNull(stepSize, "stepSize may not be null.");
this.dataSize = Preconditions.checkNotNull(dataSize, "dataSize may not be null.");
this.valueSeparator = Preconditions.checkNotNull(valueSeparator, "valueSeparator may not be null.");

formatter = Formatters.INTEGER_FORMATTER;
formatter = Formatters.INTEGER;
validator = Validators.INTEGER;
acceptableValueRange = Range.closed(minimum, maximum);
}

String getName() {
return name;
/**
* Construct a new integer flag.
*
* @param hotSpotFlagType The type of the flag.
* @param minimum The minimum value.
* @param maximum The maximum value.
* @param stepSize The increment between values.
*/
JvmFlag(final HotSpotFlagType hotSpotFlagType, final Long minimum,
final Long maximum, final Long stepSize, final DataSize dataSize,
final ValueSeparator valueSeparator, final Formatter formatter) {
name = Optional.absent();
this.hotSpotFlagType = Preconditions.checkNotNull(hotSpotFlagType, "hotSpotFlagType may not be null.");
floorValue = Preconditions.checkNotNull(minimum, "minimum may not be null.");
ceilingValue = Preconditions.checkNotNull(maximum, "maximum may not be null.");
this.stepSize = Preconditions.checkNotNull(stepSize, "stepSize may not be null.");
this.dataSize = Preconditions.checkNotNull(dataSize, "dataSize may not be null.");
this.valueSeparator = Preconditions.checkNotNull(valueSeparator, "valueSeparator may not be null.");
this.formatter = Preconditions.checkNotNull(formatter, "formatter may not be null.");
validator = Validators.INTEGER;
acceptableValueRange = Range.closed(minimum, maximum);
}

HotSpotFlagType getHotSpotFlagType() {
return hotSpotFlagType;
boolean hasName() {
return name.isPresent();
}

Formatter getFormatter() {
return formatter;
String getName() {
return name.get();
}

HotSpotFlagType getHotSpotFlagType() {
return hotSpotFlagType;
}

public long getMinimum() {
Expand Down Expand Up @@ -248,26 +278,17 @@ Range<Long> getAcceptableValueRange() {
* @param value The value that should be represented in string form.
* @return The String representation.
*/
public String asArgumentString(final Long value) {
return getFormatter().asArgumentString(this, value);
public ImmutableList<String> asArgumentString(final Long value) {
return formatter.asArgumentString(this, value);
}

/**
* Provide a representation of along with argument values as String.
*
* @return The String representation.
*/
public String asRegularExpressionString() {
return getFormatter().asRegularExpressionString(this);
}

/**
* Provide a representation of the allowed values as String.
*
* @return The String representation.
*/
public String asAcceptableValuesString() {
return getFormatter().asAcceptableValuesString(this);
public ImmutableList<String> asRegularExpressionString() {
return formatter.asRegularExpressionString(this);
}

/**
Expand All @@ -277,7 +298,7 @@ public String asAcceptableValuesString() {
* @throws IllegalArgumentException In case of a bad argument.
*/
public void validate(final Long proposedValue) throws IllegalArgumentException {
getFormatter().validate(this, proposedValue);
validator.validate(this, proposedValue);
}

/**
Expand Down
Loading