expose default settings on `/_cluster/settings?defaults=true`

This commit is contained in:
Simon Willnauer 2015-12-08 15:22:19 +01:00
parent 44df6814d4
commit dfb8bf658b
3 changed files with 104 additions and 14 deletions

View File

@ -109,6 +109,20 @@ public final class ClusterSettings {
return defaults;
}
/**
* Returns a settings object that contains all clustersettings that are not
* already set in the given source.
*/
public Settings diff(Settings source) {
Settings.Builder builder = Settings.builder();
for (Setting<?> setting : keySettings.values()) {
if (setting.exists(source) == false) {
builder.put(setting.getKey(), setting.getRaw(source));
}
}
return builder.build();
}
public static Set<Setting<?>> BUILT_IN_CLUSTER_SETTINGS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING,
AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_FORCE_GROUP_SETTING,
BalancedShardsAllocator.INDEX_BALANCE_FACTOR_SETTING,
@ -167,4 +181,5 @@ public final class ClusterSettings {
TransportService.TRACE_LOG_INCLUDE_SETTING,
TransportCloseIndexAction.CLUSTER_INDICES_CLOSE_ENABLE_SETTING,
ShardsLimitAllocationDecider.CLUSTER_TOTAL_SHARDS_PER_NODE_SETTING)));
}

View File

@ -23,19 +23,27 @@ import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestBuilderListener;
import java.io.IOException;
/**
*/
public class RestClusterGetSettingsAction extends BaseRestHandler {
private final ClusterSettings clusterSettings;
@Inject
public RestClusterGetSettingsAction(Settings settings, RestController controller, Client client) {
public RestClusterGetSettingsAction(Settings settings, RestController controller, Client client, ClusterSettings clusterSettings) {
super(settings, controller, client);
this.clusterSettings = clusterSettings;
controller.registerHandler(RestRequest.Method.GET, "/_cluster/settings", this);
}
@ -44,24 +52,34 @@ public class RestClusterGetSettingsAction extends BaseRestHandler {
ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest()
.routingTable(false)
.nodes(false);
final boolean renderDefaults = request.paramAsBoolean("defaults", false);
clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
client.admin().cluster().state(clusterStateRequest, new RestBuilderListener<ClusterStateResponse>(channel) {
@Override
public RestResponse buildResponse(ClusterStateResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
builder.startObject("persistent");
response.getState().metaData().persistentSettings().toXContent(builder, request);
builder.endObject();
builder.startObject("transient");
response.getState().metaData().transientSettings().toXContent(builder, request);
builder.endObject();
builder.endObject();
return new BytesRestResponse(RestStatus.OK, builder);
return new BytesRestResponse(RestStatus.OK, renderResponse(clusterSettings, response.getState(), renderDefaults, builder, request));
}
});
}
private static XContentBuilder renderResponse(ClusterSettings settings, ClusterState state, boolean renderDefaults, XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject();
builder.startObject("persistent");
state.metaData().persistentSettings().toXContent(builder, params);
builder.endObject();
builder.startObject("transient");
state.metaData().transientSettings().toXContent(builder, params);
builder.endObject();
if (renderDefaults) {
builder.startObject("defaults");
settings.diff(state.metaData().settings()).toXContent(builder, params);
builder.endObject();
}
builder.endObject();
return builder;
}
}

View File

@ -0,0 +1,57 @@
/*
* 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.common.settings;
import org.elasticsearch.cluster.routing.allocation.decider.FilterAllocationDecider;
import org.elasticsearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
/**
*/
public class ClusterSettingsTests extends ESTestCase {
public void testGet() {
ClusterSettings settings = new ClusterSettings();
Setting setting = settings.get("cluster.routing.allocation.require.value");
assertEquals(setting, FilterAllocationDecider.CLUSTER_ROUTING_REQUIRE_GROUP_SETTING);
setting = settings.get("cluster.routing.allocation.total_shards_per_node");
assertEquals(setting, ShardsLimitAllocationDecider.CLUSTER_TOTAL_SHARDS_PER_NODE_SETTING);
}
public void testIsDynamic(){
ClusterSettings settings = new ClusterSettings(new HashSet<>(Arrays.asList(Setting.intSetting("foo.bar", 1, true, Setting.Scope.Cluster), Setting.intSetting("foo.bar.baz", 1, false, Setting.Scope.Cluster))));
assertFalse(settings.hasDynamicSetting("foo.bar.baz"));
assertTrue(settings.hasDynamicSetting("foo.bar"));
assertNotNull(settings.get("foo.bar.baz"));
}
public void testDiff() throws IOException {
Setting<Integer> foobarbaz = Setting.intSetting("foo.bar.baz", 1, false, Setting.Scope.Cluster);
Setting<Integer> foobar = Setting.intSetting("foo.bar", 1, true, Setting.Scope.Cluster);
ClusterSettings settings = new ClusterSettings(new HashSet<>(Arrays.asList(foobar, foobarbaz)));
Settings diff = settings.diff(Settings.builder().put("foo.bar", 5).build());
assertEquals(diff.getAsMap().size(), 1);
assertEquals(diff.getAsInt("foo.bar.baz", null), Integer.valueOf(1));
}
}