add integration test for adding and listening to a dynamic index setting

This commit is contained in:
Simon Willnauer 2015-10-23 12:04:54 +02:00
parent c1b1f395a7
commit 82050e8d30
2 changed files with 150 additions and 18 deletions

View File

@ -48,16 +48,17 @@ public final class IndexSettings {
private final Settings nodeSettings;
private final int numberOfShards;
private final boolean isShadowReplicaIndex;
// updated via #updateIndexMetaData(IndexMetaData)
// volatile fields are updated via #updateIndexMetaData(IndexMetaData) under lock
private volatile Settings settings;
private volatile IndexMetaData indexMetaData;
/**
* Creates a new {@link IndexSettings} instance
* @param indexMetaData the index this settings object is associated with
* @param nodeSettings the actual settings including the node level settings
* Creates a new {@link IndexSettings} instance. The given node settings will be merged with the settings in the metadata
* while index level settings will overwrite node settings.
*
* @param indexMetaData the index metadata this settings object is associated with
* @param nodeSettings the nodes settings this index is allocated on.
* @param updateListeners a collection of listeners / consumers that should be notified if one or more settings are updated
*/
public IndexSettings(final IndexMetaData indexMetaData, final Settings nodeSettings, final Collection<Consumer<Settings>> updateListeners) {
@ -116,13 +117,6 @@ public final class IndexSettings {
return nodeName;
}
/**
* Returns all settings update consumers
*/
List<Consumer<Settings>> getUpdateListeners() { // for testing
return updateListeners;
}
/**
* Returns the current IndexMetaData for this index
*/
@ -138,9 +132,7 @@ public final class IndexSettings {
/**
* Returns the number of replicas this index has.
*/
public int getNumberOfReplicas() {
return settings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, null);
}
public int getNumberOfReplicas() { return settings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, null); }
/**
* Returns <code>true</code> iff this index uses shadow replicas.
@ -159,7 +151,7 @@ public final class IndexSettings {
}
/**
* Notifies all registered settings consumers with the new settings iff at least one setting has changed.
* Updates the settings and index metadata and notifies all registered settings consumers with the new settings iff at least one setting has changed.
*
* @return <code>true</code> iff any setting has been updated otherwise <code>false</code>.
*/
@ -178,8 +170,7 @@ public final class IndexSettings {
// nothing to update, same settings
return false;
}
this.settings = Settings.builder().put(nodeSettings).put(newSettings).build();
final Settings mergedSettings = this.settings;
final Settings mergedSettings = this.settings = Settings.builder().put(nodeSettings).put(newSettings).build();
for (final Consumer<Settings> consumer : updateListeners) {
try {
consumer.accept(mergedSettings);
@ -189,4 +180,11 @@ public final class IndexSettings {
}
return true;
}
/**
* Returns all settings update consumers
*/
List<Consumer<Settings>> getUpdateListeners() { // for testing
return updateListeners;
}
}

View File

@ -0,0 +1,134 @@
/*
* 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.index;
import org.elasticsearch.cluster.ClusterModule;
import org.elasticsearch.cluster.settings.Validator;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Binder;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Consumer;
import static org.elasticsearch.test.ESIntegTestCase.Scope.SUITE;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.nullValue;
@ClusterScope(scope = SUITE, numDataNodes = 1, transportClientRatio = 0.0)
public class SettingsListenerIT extends ESIntegTestCase {
@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return pluginList(SettingsListenerPlugin.class);
}
public static class SettingsListenerPlugin extends Plugin {
private final SettingsTestingService service = new SettingsTestingService();
/**
* The name of the plugin.
*/
@Override
public String name() {
return "settings-listener";
}
/**
* The description of the plugin.
*/
@Override
public String description() {
return "Settings Listenern Plugin";
}
public void onModule(ClusterModule clusterModule) {
clusterModule.registerIndexDynamicSetting("index.test.new.setting", Validator.INTEGER);
}
public void onModule(IndexModule module) {
if (module.getIndex().getName().equals("test")) { // only for the test index
module.addIndexSettingsListener(service);
service.accept(module.getSettings());
}
}
@Override
public Collection<Module> nodeModules() {
return Collections.<Module>singletonList(new SettingsListenerModule(service));
}
}
public static class SettingsListenerModule extends AbstractModule {
private final SettingsTestingService service;
public SettingsListenerModule(SettingsTestingService service) {
this.service = service;
}
@Override
protected void configure() {
bind(SettingsTestingService.class).toInstance(service);
}
}
public static class SettingsTestingService implements Consumer<Settings> {
public volatile int value;
@Override
public void accept(Settings settings) {
value = settings.getAsInt("index.test.new.setting", -1);
}
}
public void testListener() {
assertAcked(client().admin().indices().prepareCreate("test").setSettings(Settings.builder()
.put("index.test.new.setting", 21)
.build()).get());
for (SettingsTestingService instance : internalCluster().getInstances(SettingsTestingService.class)) {
assertEquals(21, instance.value);
}
client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder()
.put("index.test.new.setting", 42)).get();
for (SettingsTestingService instance : internalCluster().getInstances(SettingsTestingService.class)) {
assertEquals(42, instance.value);
}
assertAcked(client().admin().indices().prepareCreate("other").setSettings(Settings.builder()
.put("index.test.new.setting", 21)
.build()).get());
for (SettingsTestingService instance : internalCluster().getInstances(SettingsTestingService.class)) {
assertEquals(42, instance.value);
}
client().admin().indices().prepareUpdateSettings("other").setSettings(Settings.builder()
.put("index.test.new.setting", 84)).get();
for (SettingsTestingService instance : internalCluster().getInstances(SettingsTestingService.class)) {
assertEquals(42, instance.value);
}
}
}