always pass setting name to TimeValue.parseTimeValue; make units required

This commit is contained in:
Michael McCandless 2015-05-25 05:33:58 -04:00 committed by mikemccand
parent dfd7122a53
commit 6a6df5f8ce
32 changed files with 58 additions and 45 deletions

View File

@ -83,7 +83,7 @@ public class ClusterHealthRequest extends MasterNodeReadOperationRequest<Cluster
}
public ClusterHealthRequest timeout(String timeout) {
return this.timeout(TimeValue.parseTimeValue(timeout, null));
return this.timeout(TimeValue.parseTimeValue(timeout, null, "ClusterHealthRequest.timeout"));
}
public ClusterHealthStatus waitForStatus() {

View File

@ -120,7 +120,7 @@ public class DeleteIndexRequest extends MasterNodeOperationRequest<DeleteIndexRe
* to <tt>10s</tt>.
*/
public DeleteIndexRequest timeout(String timeout) {
return timeout(TimeValue.parseTimeValue(timeout, null));
return timeout(TimeValue.parseTimeValue(timeout, null, "DeleteIndexRequest.timeout"));
}
@Override

View File

@ -308,7 +308,7 @@ public class BulkRequest extends ActionRequest<BulkRequest> implements Composite
timestamp = parser.text();
} else if ("_ttl".equals(currentFieldName) || "ttl".equals(currentFieldName)) {
if (parser.currentToken() == XContentParser.Token.VALUE_STRING) {
ttl = TimeValue.parseTimeValue(parser.text(), null).millis();
ttl = TimeValue.parseTimeValue(parser.text(), null, currentFieldName).millis();
} else {
ttl = parser.longValue();
}
@ -417,7 +417,7 @@ public class BulkRequest extends ActionRequest<BulkRequest> implements Composite
* A timeout to wait if the index operation can't be performed immediately. Defaults to <tt>1m</tt>.
*/
public final BulkRequest timeout(String timeout) {
return timeout(TimeValue.parseTimeValue(timeout, null));
return timeout(TimeValue.parseTimeValue(timeout, null, "BulkRequest.timeout"));
}
public TimeValue timeout() {

View File

@ -476,7 +476,7 @@ public class SearchRequest extends ActionRequest<SearchRequest> implements Indic
* If set, will enable scrolling of the search request for the specified timeout.
*/
public SearchRequest scroll(String keepAlive) {
return scroll(new Scroll(TimeValue.parseTimeValue(keepAlive, null)));
return scroll(new Scroll(TimeValue.parseTimeValue(keepAlive, null, "SearchRequest.Scroll.keepAlive")));
}
/**

View File

@ -93,7 +93,7 @@ public class SearchScrollRequest extends ActionRequest<SearchScrollRequest> {
* If set, will enable scrolling of the search request for the specified timeout.
*/
public SearchScrollRequest scroll(String keepAlive) {
return scroll(new Scroll(TimeValue.parseTimeValue(keepAlive, null)));
return scroll(new Scroll(TimeValue.parseTimeValue(keepAlive, null, "SearchScrollRequest.keepAlive")));
}
@Override

View File

@ -51,7 +51,7 @@ public abstract class AdapterActionFuture<T, L> extends BaseFuture<T> implements
@Override
public T actionGet(String timeout) {
return actionGet(TimeValue.parseTimeValue(timeout, null));
return actionGet(TimeValue.parseTimeValue(timeout, null, "AdapterActionFuture.actionGet.timeout"));
}
@Override

View File

@ -53,7 +53,7 @@ public abstract class AcknowledgedRequest<T extends MasterNodeOperationRequest>
*/
@SuppressWarnings("unchecked")
public final T timeout(String timeout) {
this.timeout = TimeValue.parseTimeValue(timeout, this.timeout);
this.timeout = TimeValue.parseTimeValue(timeout, this.timeout, "AcknowledgedRequest.timeout");
return (T)this;
}

View File

@ -56,7 +56,7 @@ public abstract class MasterNodeOperationRequest<T extends MasterNodeOperationRe
* A timeout value in case the master has not been discovered yet or disconnected.
*/
public final T masterNodeTimeout(String timeout) {
return masterNodeTimeout(TimeValue.parseTimeValue(timeout, null));
return masterNodeTimeout(TimeValue.parseTimeValue(timeout, null, "MasterNodeOperationRequest.masterNodeTimeout"));
}
public final TimeValue masterNodeTimeout() {

View File

@ -74,7 +74,7 @@ public abstract class NodesOperationRequest<T extends NodesOperationRequest> ext
@SuppressWarnings("unchecked")
public final T timeout(String timeout) {
this.timeout = TimeValue.parseTimeValue(timeout, null);
this.timeout = TimeValue.parseTimeValue(timeout, null, "NodesOperationRequest.timeout");
return (T) this;
}

View File

@ -121,7 +121,7 @@ public abstract class ShardReplicationOperationRequest<T extends ShardReplicatio
* A timeout to wait if the index operation can't be performed immediately. Defaults to <tt>1m</tt>.
*/
public final T timeout(String timeout) {
return timeout(TimeValue.parseTimeValue(timeout, null));
return timeout(TimeValue.parseTimeValue(timeout, null, "ShardReplicatoinOperationRequest.timeout"));
}
public TimeValue timeout() {

View File

@ -97,7 +97,7 @@ public abstract class InstanceShardOperationRequest<T extends InstanceShardOpera
* A timeout to wait if the index operation can't be performed immediately. Defaults to <tt>1m</tt>.
*/
public final T timeout(String timeout) {
return timeout(TimeValue.parseTimeValue(timeout, null));
return timeout(TimeValue.parseTimeValue(timeout, null, "InstanceShardOperationRequest.timeout"));
}
@Override

View File

@ -259,7 +259,7 @@ public class UpdateHelper extends AbstractComponent {
if (fetchedTTL instanceof Number) {
ttl = ((Number) fetchedTTL).longValue();
} else {
ttl = TimeValue.parseTimeValue((String) fetchedTTL, null).millis();
ttl = TimeValue.parseTimeValue((String) fetchedTTL, null, "_ttl").millis();
}
}
return ttl;

View File

@ -45,7 +45,7 @@ public interface Validator {
@Override
public String validate(String setting, String value) {
try {
if (TimeValue.parseTimeValue(value, null) == null) {
if (TimeValue.parseTimeValue(value, null, setting) == null) {
return "cannot parse value [" + value + "] as time";
}
} catch (ElasticsearchParseException ex) {
@ -59,7 +59,7 @@ public interface Validator {
@Override
public String validate(String setting, String value) {
try {
TimeValue timeValue = TimeValue.parseTimeValue(value, null);
TimeValue timeValue = TimeValue.parseTimeValue(value, null, setting);
if (timeValue == null) {
return "cannot parse value [" + value + "] as time";
}

View File

@ -357,12 +357,19 @@ public class ImmutableSettings implements Settings {
@Override
public TimeValue getAsTime(String setting, TimeValue defaultValue) {
return parseTimeValue(get(setting), defaultValue);
return parseTimeValue(get(setting), defaultValue, setting);
}
@Override
public TimeValue getAsTime(String[] settings, TimeValue defaultValue) {
return parseTimeValue(get(settings), defaultValue);
// NOTE: duplicated from get(String[]) so we can pass which setting name was actually used to parseTimeValue:
for (String setting : settings) {
String retVal = get(setting);
if (retVal != null) {
parseTimeValue(get(settings), defaultValue, setting);
}
}
return defaultValue;
}
@Override

View File

@ -152,7 +152,7 @@ public final class Fuzziness implements ToXContent {
if (this == AUTO) {
return TimeValue.timeValueMillis(1);
} else {
return TimeValue.parseTimeValue(fuzziness.toString(), null);
return TimeValue.parseTimeValue(fuzziness.toString(), null, "fuzziness");
}
}

View File

@ -31,11 +31,9 @@ import org.joda.time.format.PeriodFormatter;
import java.io.IOException;
import java.io.Serializable;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
*
*/
public class TimeValue implements Serializable, Streamable {
/** How many nano-seconds in one milli-second */
@ -228,7 +226,8 @@ public class TimeValue implements Serializable, Streamable {
return Strings.format1Decimals(value, suffix);
}
public static TimeValue parseTimeValue(String sValue, TimeValue defaultValue) {
public static TimeValue parseTimeValue(String sValue, TimeValue defaultValue, String settingName) {
settingName = Objects.requireNonNull(settingName);
if (sValue == null) {
return defaultValue;
}
@ -248,8 +247,15 @@ public class TimeValue implements Serializable, Streamable {
millis = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 1)) * 24 * 60 * 60 * 1000);
} else if (sValue.endsWith("w")) {
millis = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 1)) * 7 * 24 * 60 * 60 * 1000);
} else if (sValue.equals("-1")) {
// Allow this special value to be unit-less:
millis = -1;
} else if (sValue.equals("0")) {
// Allow this special value to be unit-less:
millis = 0;
} else {
millis = Long.parseLong(sValue);
// Missing units:
throw new ElasticsearchParseException("Failed to parse setting [" + settingName + "] with value [" + sValue + "] as a time value: unit is missing or unrecognized");
}
return new TimeValue(millis, TimeUnit.MILLISECONDS);
} catch (NumberFormatException e) {

View File

@ -377,7 +377,7 @@ public class XContentMapValues {
if (node instanceof Number) {
return TimeValue.timeValueMillis(((Number) node).longValue());
}
return TimeValue.parseTimeValue(node.toString(), null);
return TimeValue.parseTimeValue(node.toString(), null, "XContentMapValues.nodeTimeValue");
}
public static Map<String, Object> nodeMapValue(Object node, String desc) {

View File

@ -178,7 +178,7 @@ public class TTLFieldMapper extends LongFieldMapper implements RootMapper {
if (context.sourceToParse().ttl() < 0) { // no ttl has been provided externally
long ttl;
if (context.parser().currentToken() == XContentParser.Token.VALUE_STRING) {
ttl = TimeValue.parseTimeValue(context.parser().text(), null).millis();
ttl = TimeValue.parseTimeValue(context.parser().text(), null, "ttl").millis();
} else {
ttl = context.parser().longValue(coerce.value());
}

View File

@ -269,9 +269,9 @@ public abstract class DecayFunctionParser implements ScoreFunctionParser {
if (scaleString == null) {
throw new ElasticsearchParseException(DecayFunctionBuilder.SCALE + " must be set for date fields.");
}
TimeValue val = TimeValue.parseTimeValue(scaleString, TimeValue.timeValueHours(24));
TimeValue val = TimeValue.parseTimeValue(scaleString, TimeValue.timeValueHours(24), "DecayFunctionParser.scale");
double scale = val.getMillis();
val = TimeValue.parseTimeValue(offsetString, TimeValue.timeValueHours(24));
val = TimeValue.parseTimeValue(offsetString, TimeValue.timeValueHours(24), "DecayFunctionParser.offset");
double offset = val.getMillis();
IndexNumericFieldData numericFieldData = parseContext.getForField(dateFieldMapper);
return new NumericFieldDataScoreFunction(origin, scale, decay, offset, getDecayFunction(), numericFieldData, mode);

View File

@ -408,7 +408,7 @@ public class PluginManager {
case "timeout":
case "-timeout":
String timeoutValue = getCommandValue(args, ++c, "--timeout");
timeout = TimeValue.parseTimeValue(timeoutValue, DEFAULT_TIMEOUT);
timeout = TimeValue.parseTimeValue(timeoutValue, DEFAULT_TIMEOUT, command);
break;
case "-l":
case "--list":

View File

@ -140,7 +140,7 @@ public abstract class RestRequest extends ContextAndHeaderHolder implements ToXC
}
public TimeValue paramAsTime(String key, TimeValue defaultValue) {
return parseTimeValue(param(key), defaultValue);
return parseTimeValue(param(key), defaultValue, key);
}
public ByteSizeValue paramAsSize(String key, ByteSizeValue defaultValue) {

View File

@ -56,7 +56,7 @@ public class RestNodesHotThreadsAction extends BaseRestHandler {
nodesHotThreadsRequest.threads(request.paramAsInt("threads", nodesHotThreadsRequest.threads()));
nodesHotThreadsRequest.ignoreIdleThreads(request.paramAsBoolean("ignore_idle_threads", nodesHotThreadsRequest.ignoreIdleThreads()));
nodesHotThreadsRequest.type(request.param("type", nodesHotThreadsRequest.type()));
nodesHotThreadsRequest.interval(TimeValue.parseTimeValue(request.param("interval"), nodesHotThreadsRequest.interval()));
nodesHotThreadsRequest.interval(TimeValue.parseTimeValue(request.param("interval"), nodesHotThreadsRequest.interval(), "interval"));
nodesHotThreadsRequest.snapshots(request.paramAsInt("snapshots", nodesHotThreadsRequest.snapshots()));
client.admin().cluster().nodesHotThreads(nodesHotThreadsRequest, new RestResponseListener<NodesHotThreadsResponse>(channel) {
@Override

View File

@ -113,7 +113,7 @@ public class RestSearchAction extends BaseRestHandler {
String scroll = request.param("scroll");
if (scroll != null) {
searchRequest.scroll(new Scroll(parseTimeValue(scroll, null)));
searchRequest.scroll(new Scroll(parseTimeValue(scroll, null, "scroll")));
}
searchRequest.types(Strings.splitStringByCommaToArray(request.param("type")));

View File

@ -63,7 +63,7 @@ public class RestSearchScrollAction extends BaseRestHandler {
searchScrollRequest.scrollId(scrollId);
String scroll = request.param("scroll");
if (scroll != null) {
searchScrollRequest.scroll(new Scroll(parseTimeValue(scroll, null)));
searchScrollRequest.scroll(new Scroll(parseTimeValue(scroll, null, "scroll")));
}
if (RestActions.hasBodyContent(request)) {
@ -94,7 +94,7 @@ public class RestSearchScrollAction extends BaseRestHandler {
} else if ("scroll_id".equals(currentFieldName) && token == XContentParser.Token.VALUE_STRING) {
searchScrollRequest.scrollId(parser.text());
} else if ("scroll".equals(currentFieldName) && token == XContentParser.Token.VALUE_STRING) {
searchScrollRequest.scroll(new Scroll(TimeValue.parseTimeValue(parser.text(), null)));
searchScrollRequest.scroll(new Scroll(TimeValue.parseTimeValue(parser.text(), null, "scroll")));
} else {
throw new IllegalArgumentException("Unknown parameter [" + currentFieldName + "] in request body or parameter is of the wrong type[" + token + "] ");
}

View File

@ -189,7 +189,7 @@ public class DateHistogramParser implements Aggregator.Parser {
tzRoundingBuilder = TimeZoneRounding.builder(dateTimeUnit);
} else {
// the interval is a time value?
tzRoundingBuilder = TimeZoneRounding.builder(TimeValue.parseTimeValue(interval, null));
tzRoundingBuilder = TimeZoneRounding.builder(TimeValue.parseTimeValue(interval, null, "DateHistogramParser.interval"));
}
Rounding rounding = tzRoundingBuilder
@ -217,9 +217,9 @@ public class DateHistogramParser implements Aggregator.Parser {
private long parseOffset(String offset) throws IOException {
if (offset.charAt(0) == '-') {
return -TimeValue.parseTimeValue(offset.substring(1), null).millis();
return -TimeValue.parseTimeValue(offset.substring(1), null, "DateHistogramParser.parseOffset").millis();
}
int beginIndex = offset.charAt(0) == '+' ? 1 : 0;
return TimeValue.parseTimeValue(offset.substring(beginIndex), null).millis();
return TimeValue.parseTimeValue(offset.substring(beginIndex), null, "DateHistogramParser.parseOffset").millis();
}
}

View File

@ -106,7 +106,7 @@ public class DerivativeParser implements PipelineAggregator.Parser {
if (dateTimeUnit != null) {
xAxisUnits = dateTimeUnit.field().getDurationField().getUnitMillis();
} else {
TimeValue timeValue = TimeValue.parseTimeValue(units, null);
TimeValue timeValue = TimeValue.parseTimeValue(units, null, "DerivativeParser.units");
if (timeValue != null) {
xAxisUnits = timeValue.getMillis();
}

View File

@ -315,7 +315,7 @@ public class SearchSourceBuilder extends ToXContentToBytes {
* An optional timeout to control how long search is allowed to take.
*/
public SearchSourceBuilder timeout(String timeout) {
this.timeoutInMillis = TimeValue.parseTimeValue(timeout, null).millis();
this.timeoutInMillis = TimeValue.parseTimeValue(timeout, null, "SearchSourceBuilder.timeout").millis();
return this;
}

View File

@ -34,7 +34,7 @@ public class TimeoutParseElement implements SearchParseElement {
if (token == XContentParser.Token.VALUE_NUMBER) {
context.timeoutInMillis(parser.longValue());
} else {
context.timeoutInMillis(TimeValue.parseTimeValue(parser.text(), null).millis());
context.timeoutInMillis(TimeValue.parseTimeValue(parser.text(), null, "timeout").millis());
}
}
}

View File

@ -132,7 +132,7 @@ public class FuzzinessTests extends ElasticsearchTestCase {
assertThat(parser.nextToken(), equalTo(XContentParser.Token.FIELD_NAME));
assertThat(parser.nextToken(), equalTo(XContentParser.Token.VALUE_STRING));
Fuzziness parse = Fuzziness.parse(parser);
assertThat(parse.asTimeValue(), equalTo(TimeValue.parseTimeValue(actual, null)));
assertThat(parse.asTimeValue(), equalTo(TimeValue.parseTimeValue(actual, null, "fuzziness")));
assertThat(parser.nextToken(), equalTo(XContentParser.Token.END_OBJECT));
}
}
@ -159,7 +159,7 @@ public class FuzzinessTests extends ElasticsearchTestCase {
assertThat(Fuzziness.AUTO.asDouble(), equalTo(1d));
assertThat(Fuzziness.AUTO.asLong(), equalTo(1l));
assertThat(Fuzziness.AUTO.asShort(), equalTo((short) 1));
assertThat(Fuzziness.AUTO.asTimeValue(), equalTo(TimeValue.parseTimeValue("1", TimeValue.timeValueMillis(1))));
assertThat(Fuzziness.AUTO.asTimeValue(), equalTo(TimeValue.parseTimeValue("1", TimeValue.timeValueMillis(1), "fuzziness")));
}

View File

@ -499,7 +499,7 @@ public class SearchScrollTests extends ElasticsearchIntegrationTest {
RestSearchScrollAction.buildFromContent(content, searchScrollRequest);
assertThat(searchScrollRequest.scrollId(), equalTo("SCROLL_ID"));
assertThat(searchScrollRequest.scroll().keepAlive(), equalTo(TimeValue.parseTimeValue("1m", null)));
assertThat(searchScrollRequest.scroll().keepAlive(), equalTo(TimeValue.parseTimeValue("1m", null, "scroll")));
}
@Test

View File

@ -40,7 +40,7 @@ public class GetStressTest {
final int NUMBER_OF_NODES = 2;
final int NUMBER_OF_THREADS = 50;
final TimeValue TEST_TIME = TimeValue.parseTimeValue("10m", null);
final TimeValue TEST_TIME = TimeValue.parseTimeValue("10m", null, "TEST_TIME");
Node[] nodes = new Node[NUMBER_OF_NODES];
for (int i = 0; i < nodes.length; i++) {
@ -94,4 +94,4 @@ public class GetStressTest {
System.out.println("test done.");
done.set(true);
}
}
}

View File

@ -491,7 +491,7 @@ public abstract class AbstractSimpleTransportTests extends ElasticsearchTestCase
serviceA.registerRequestHandler("sayHelloTimeoutDelayedResponse", StringMessageRequest.class, ThreadPool.Names.GENERIC, new TransportRequestHandler<StringMessageRequest>() {
@Override
public void messageReceived(StringMessageRequest request, TransportChannel channel) {
TimeValue sleep = TimeValue.parseTimeValue(request.message, null);
TimeValue sleep = TimeValue.parseTimeValue(request.message, null, "sleep");
try {
Thread.sleep(sleep.millis());
} catch (InterruptedException e) {