Use namedObject to parse AllocationCommands (#22489)

This removes `AllocationCommandRegistry` entirely and replaces
it with `XContentParser#namedObject`, removing another class from
guice.
This commit is contained in:
Nik Everett 2017-01-09 12:26:57 -05:00 committed by GitHub
parent fc1f7c2147
commit f75ef7adfd
7 changed files with 39 additions and 92 deletions

View File

@ -1,31 +0,0 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.
*/
package org.elasticsearch.cluster.routing.allocation.command;
import org.elasticsearch.common.xcontent.ParseFieldRegistry;
/**
* Registry of allocation commands. This is it's own class just to make Guice happy.
*/
public class AllocationCommandRegistry extends ParseFieldRegistry<AllocationCommand.Parser<?>> {
public AllocationCommandRegistry() {
super("allocation_command");
}
}

View File

@ -125,11 +125,10 @@ public class AllocationCommands extends ToXContentToBytes {
* }
* </pre>
* @param parser {@link XContentParser} to read the commands from
* @param registry of allocation command parsers
* @return {@link AllocationCommands} read
* @throws IOException if something bad happens while reading the stream
*/
public static AllocationCommands fromXContent(XContentParser parser, AllocationCommandRegistry registry) throws IOException {
public static AllocationCommands fromXContent(XContentParser parser) throws IOException {
AllocationCommands commands = new AllocationCommands();
XContentParser.Token token = parser.currentToken();
@ -158,7 +157,7 @@ public class AllocationCommands extends ToXContentToBytes {
token = parser.nextToken();
String commandName = parser.currentName();
token = parser.nextToken();
commands.add(registry.lookup(commandName, parser.getTokenLocation()).fromXContent(parser));
commands.add(parser.namedObject(AllocationCommand.class, commandName, null));
// move to the end object one
if (parser.nextToken() != XContentParser.Token.END_OBJECT) {
throw new ElasticsearchParseException("allocation command is malformed, done parsing a command, but didn't get END_OBJECT, got [{}] instead", token);

View File

@ -24,18 +24,17 @@ import org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimary
import org.elasticsearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand;
import org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand;
import org.elasticsearch.cluster.routing.allocation.command.AllocationCommand;
import org.elasticsearch.cluster.routing.allocation.command.AllocationCommandRegistry;
import org.elasticsearch.cluster.routing.allocation.command.CancelAllocationCommand;
import org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry.Entry;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Setting.Property;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.NamedXContentRegistry.FromXContent;
import org.elasticsearch.http.HttpServerTransport;
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.plugins.NetworkPlugin;
@ -76,12 +75,8 @@ public final class NetworkModule {
private final Settings settings;
private final boolean transportClient;
private static final AllocationCommandRegistry allocationCommandRegistry = new AllocationCommandRegistry();
private static final List<NamedWriteableRegistry.Entry> namedWriteables = new ArrayList<>();
private final Map<String, Supplier<Transport>> transportFactories = new HashMap<>();
private final Map<String, Supplier<HttpServerTransport>> transportHttpFactories = new HashMap<>();
private final List<TransportInterceptor> transportIntercetors = new ArrayList<>();
private static final List<NamedXContentRegistry.Entry> namedXContents = new ArrayList<>();
static {
registerAllocationCommand(CancelAllocationCommand::new, CancelAllocationCommand::fromXContent,
@ -99,6 +94,11 @@ public final class NetworkModule {
namedWriteables.add(
new NamedWriteableRegistry.Entry(Task.Status.class, RawTaskStatus.NAME, RawTaskStatus::new));
}
private final Map<String, Supplier<Transport>> transportFactories = new HashMap<>();
private final Map<String, Supplier<HttpServerTransport>> transportHttpFactories = new HashMap<>();
private final List<TransportInterceptor> transportIntercetors = new ArrayList<>();
/**
* Creates a network module that custom networking classes can be plugged into.
* @param settings The settings for the node
@ -165,22 +165,19 @@ public final class NetworkModule {
* it is the name under which the command's reader is registered.
*/
private static <T extends AllocationCommand> void registerAllocationCommand(Writeable.Reader<T> reader,
AllocationCommand.Parser<T> parser, ParseField commandName) {
allocationCommandRegistry.register(parser, commandName);
namedWriteables.add(new Entry(AllocationCommand.class, commandName.getPreferredName(), reader));
FromXContent<T> parser, ParseField commandName) {
namedXContents.add(new NamedXContentRegistry.Entry(AllocationCommand.class, commandName, parser));
namedWriteables.add(new NamedWriteableRegistry.Entry(AllocationCommand.class, commandName.getPreferredName(), reader));
}
/**
* The registry of allocation command parsers.
*/
public static AllocationCommandRegistry getAllocationCommandRegistry() {
return allocationCommandRegistry;
}
public static List<Entry> getNamedWriteables() {
public static List<NamedWriteableRegistry.Entry> getNamedWriteables() {
return Collections.unmodifiableList(namedWriteables);
}
public static List<NamedXContentRegistry.Entry> getNamedXContents() {
return Collections.unmodifiableList(namedXContents);
}
public Supplier<HttpServerTransport> getHttpServerTransportSupplier() {
final String name;
if (HTTP_TYPE_SETTING.exists(settings)) {

View File

@ -47,7 +47,6 @@ import org.elasticsearch.cluster.metadata.MetaDataIndexUpgradeService;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.routing.RoutingService;
import org.elasticsearch.cluster.routing.allocation.AllocationService;
import org.elasticsearch.cluster.routing.allocation.command.AllocationCommandRegistry;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.StopWatch;
import org.elasticsearch.common.component.Lifecycle;
@ -361,6 +360,7 @@ public class Node implements Closeable {
.flatMap(Function.identity()).collect(Collectors.toList());
final NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(namedWriteables);
NamedXContentRegistry xContentRegistry = new NamedXContentRegistry(Stream.of(
NetworkModule.getNamedXContents().stream(),
searchModule.getNamedXContents().stream(),
pluginsService.filterPlugins(Plugin.class).stream()
.flatMap(p -> p.getNamedXContent().stream()),
@ -437,7 +437,6 @@ public class Node implements Closeable {
b.bind(Transport.class).toInstance(transport);
b.bind(TransportService.class).toInstance(transportService);
b.bind(NetworkService.class).toInstance(networkService);
b.bind(AllocationCommandRegistry.class).toInstance(NetworkModule.getAllocationCommandRegistry());
b.bind(UpdateHelper.class).toInstance(new UpdateHelper(settings, scriptModule.getScriptService()));
b.bind(MetaDataIndexUpgradeService.class).toInstance(new MetaDataIndexUpgradeService(settings,
xContentRegistry, indicesModule.getMapperRegistry(), settingsModule.getIndexScopedSettings()));

View File

@ -24,11 +24,8 @@ import org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.routing.allocation.command.AllocationCommandRegistry;
import org.elasticsearch.cluster.routing.allocation.command.AllocationCommands;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.ParseFieldMatcherSupplier;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
@ -49,10 +46,10 @@ import java.util.HashSet;
import java.util.Set;
public class RestClusterRerouteAction extends BaseRestHandler {
private static final ObjectParser<ClusterRerouteRequest, ParseContext> PARSER = new ObjectParser<>("cluster_reroute");
private static final ObjectParser<ClusterRerouteRequest, Void> PARSER = new ObjectParser<>("cluster_reroute");
static {
PARSER.declareField((p, v, c) -> v.commands(AllocationCommands.fromXContent(p, c.registry)),
new ParseField("commands"), ValueType.OBJECT_ARRAY);
PARSER.declareField((p, v, c) -> v.commands(AllocationCommands.fromXContent(p)), new ParseField("commands"),
ValueType.OBJECT_ARRAY);
PARSER.declareBoolean(ClusterRerouteRequest::dryRun, new ParseField("dry_run"));
}
@ -60,20 +57,17 @@ public class RestClusterRerouteAction extends BaseRestHandler {
.arrayToCommaDelimitedString(EnumSet.complementOf(EnumSet.of(ClusterState.Metric.METADATA)).toArray());
private final SettingsFilter settingsFilter;
private final AllocationCommandRegistry registry;
@Inject
public RestClusterRerouteAction(Settings settings, RestController controller, SettingsFilter settingsFilter,
AllocationCommandRegistry registry) {
public RestClusterRerouteAction(Settings settings, RestController controller, SettingsFilter settingsFilter) {
super(settings);
this.settingsFilter = settingsFilter;
this.registry = registry;
controller.registerHandler(RestRequest.Method.POST, "/_cluster/reroute", this);
}
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
ClusterRerouteRequest clusterRerouteRequest = createRequest(request, registry, parseFieldMatcher);
ClusterRerouteRequest clusterRerouteRequest = createRequest(request);
// by default, return everything but metadata
final String metric = request.param("metric");
@ -111,31 +105,14 @@ public class RestClusterRerouteAction extends BaseRestHandler {
return RESPONSE_PARAMS;
}
public static ClusterRerouteRequest createRequest(RestRequest request, AllocationCommandRegistry registry,
ParseFieldMatcher parseFieldMatcher) throws IOException {
public static ClusterRerouteRequest createRequest(RestRequest request) throws IOException {
ClusterRerouteRequest clusterRerouteRequest = Requests.clusterRerouteRequest();
clusterRerouteRequest.dryRun(request.paramAsBoolean("dry_run", clusterRerouteRequest.dryRun()));
clusterRerouteRequest.explain(request.paramAsBoolean("explain", clusterRerouteRequest.explain()));
clusterRerouteRequest.timeout(request.paramAsTime("timeout", clusterRerouteRequest.timeout()));
clusterRerouteRequest.setRetryFailed(request.paramAsBoolean("retry_failed", clusterRerouteRequest.isRetryFailed()));
clusterRerouteRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterRerouteRequest.masterNodeTimeout()));
request.applyContentParser(parser -> PARSER.parse(parser, clusterRerouteRequest, new ParseContext(registry, parseFieldMatcher)));
request.applyContentParser(parser -> PARSER.parse(parser, clusterRerouteRequest, null));
return clusterRerouteRequest;
}
private static class ParseContext implements ParseFieldMatcherSupplier {
private final AllocationCommandRegistry registry;
private final ParseFieldMatcher parseFieldMatcher;
private ParseContext(AllocationCommandRegistry registry, ParseFieldMatcher parseFieldMatcher) {
this.registry = registry;
this.parseFieldMatcher = parseFieldMatcher;
}
@Override
public ParseFieldMatcher getParseFieldMatcher() {
return parseFieldMatcher;
}
}
}

View File

@ -25,15 +25,14 @@ import org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimary
import org.elasticsearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand;
import org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand;
import org.elasticsearch.cluster.routing.allocation.command.AllocationCommand;
import org.elasticsearch.cluster.routing.allocation.command.AllocationCommandRegistry;
import org.elasticsearch.cluster.routing.allocation.command.CancelAllocationCommand;
import org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
@ -71,10 +70,8 @@ public class ClusterRerouteRequestTests extends ESTestCase {
() -> new MoveAllocationCommand(randomAsciiOfLengthBetween(2, 10), between(0, 1000),
randomAsciiOfLengthBetween(2, 10), randomAsciiOfLengthBetween(2, 10))));
private final NamedWriteableRegistry namedWriteableRegistry;
private final AllocationCommandRegistry allocationCommandRegistry;
public ClusterRerouteRequestTests() {
allocationCommandRegistry = NetworkModule.getAllocationCommandRegistry();
namedWriteableRegistry = new NamedWriteableRegistry(NetworkModule.getNamedWriteables());
}
@ -176,7 +173,7 @@ public class ClusterRerouteRequestTests extends ESTestCase {
private ClusterRerouteRequest roundTripThroughRestRequest(ClusterRerouteRequest original) throws IOException {
RestRequest restRequest = toRestRequest(original);
return RestClusterRerouteAction.createRequest(restRequest, allocationCommandRegistry, ParseFieldMatcher.STRICT);
return RestClusterRerouteAction.createRequest(restRequest);
}
private RestRequest toRestRequest(ClusterRerouteRequest original) throws IOException {
@ -216,4 +213,9 @@ public class ClusterRerouteRequestTests extends ESTestCase {
}
return requestBuilder.build();
}
@Override
protected NamedXContentRegistry xContentRegistry() {
return new NamedXContentRegistry(NetworkModule.getNamedXContents());
}
}

View File

@ -33,7 +33,6 @@ import org.elasticsearch.cluster.routing.allocation.command.AbstractAllocateAllo
import org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand;
import org.elasticsearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand;
import org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand;
import org.elasticsearch.cluster.routing.allocation.command.AllocationCommandRegistry;
import org.elasticsearch.cluster.routing.allocation.command.AllocationCommands;
import org.elasticsearch.cluster.routing.allocation.command.CancelAllocationCommand;
import org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand;
@ -45,6 +44,7 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.IndexNotFoundException;
@ -488,8 +488,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase {
// move two tokens, parser expected to be "on" `commands` field
parser.nextToken();
parser.nextToken();
AllocationCommandRegistry registry = NetworkModule.getAllocationCommandRegistry();
AllocationCommands sCommands = AllocationCommands.fromXContent(parser, registry);
AllocationCommands sCommands = AllocationCommands.fromXContent(parser);
assertThat(sCommands.commands().size(), equalTo(5));
assertThat(((AllocateEmptyPrimaryAllocationCommand) (sCommands.commands().get(0))).shardId(), equalTo(1));
@ -516,4 +515,9 @@ public class AllocationCommandsTests extends ESAllocationTestCase {
assertThat(((CancelAllocationCommand) (sCommands.commands().get(4))).node(), equalTo("node5"));
assertThat(((CancelAllocationCommand) (sCommands.commands().get(4))).allowPrimary(), equalTo(true));
}
@Override
protected NamedXContentRegistry xContentRegistry() {
return new NamedXContentRegistry(NetworkModule.getNamedXContents());
}
}