Renamed ConfigManager#isReady() to ConfigManager#start()

Add ConfigManager#stop()
Simplified config loading
smpt alert action settings are now also updated

Original commit: elastic/x-pack-elasticsearch@acb180f88c
This commit is contained in:
Martijn van Groningen 2014-12-05 15:14:24 +01:00
parent 920f7ea2a9
commit 1258a4c327
6 changed files with 32 additions and 38 deletions

View File

@ -251,6 +251,7 @@ public class AlertManager extends AbstractComponent {
actionManager.stop();
scheduler.stop();
alertsStore.stop();
configurationManager.stop();
state.set(State.STOPPED);
logger.info("Alert manager has stopped");
}
@ -260,8 +261,8 @@ public class AlertManager extends AbstractComponent {
if (state.compareAndSet(State.STOPPED, State.STARTING)) {
ClusterState clusterState = initialState;
while(true) {
if (configurationManager.isReady(initialState)) {
while (true) {
if (configurationManager.start(initialState)) {
break;
}
clusterState = newClusterState(clusterState);

View File

@ -152,7 +152,7 @@ public class AlertsStore extends AbstractComponent {
return true;
}
if (!configurationManager.isReady(state)) {
if (!configurationManager.start(state)) {
return false;
}

View File

@ -15,27 +15,23 @@ import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.indices.IndexMissingException;
import java.util.Map;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;
/**
*/
public class ConfigurationManager extends AbstractComponent {
private final Client client;
public static final String CONFIG_TYPE = "config";
public static final String CONFIG_INDEX = AlertsStore.ALERT_INDEX;
public static final String GLOBAL_CONFIG_NAME = "global";
private final Client client;
private volatile boolean started = false;
private final CopyOnWriteArrayList<ConfigurableComponentListener> registeredComponents;
private volatile boolean readyToRead = false;
@Inject
public ConfigurationManager(Settings settings, Client client) {
super(settings);
@ -48,36 +44,28 @@ public class ConfigurationManager extends AbstractComponent {
* @return The immutable settings loaded from the index
*/
public Settings getGlobalConfig() {
ensureReady();
ensureStarted();
try {
client.admin().indices().prepareRefresh(CONFIG_INDEX).get();
} catch (IndexMissingException ime) {
logger.info("No index [" + CONFIG_INDEX + "] found");
logger.error("No index [" + CONFIG_INDEX + "] found");
return null;
}
GetResponse response = client.prepareGet(CONFIG_INDEX, CONFIG_TYPE, GLOBAL_CONFIG_NAME).get();
if (!response.isExists()) {
if (response.isExists()) {
return ImmutableSettings.settingsBuilder().loadFromSource(response.getSourceAsString()).build();
} else {
return null;
}
Map<String, Object> sourceMap = response.getSourceAsMap();
ImmutableSettings.Builder settingsBuilder = ImmutableSettings.builder();
for (Map.Entry<String, Object> configEntry : sourceMap.entrySet() ) {
settingsBuilder.put(configEntry.getKey(), configEntry.getValue());
}
return settingsBuilder.build();
}
/**
* Notify the listeners of a new config
*
* @param settingsSource
*/
public void newConfig(BytesReference settingsSource) {
Map<String, Object> settingsMap = XContentHelper.convertToMap(settingsSource, true).v2();
ImmutableSettings.Builder settingsBuilder = ImmutableSettings.builder();
for (Map.Entry<String, Object> configEntry : settingsMap.entrySet() ) {
settingsBuilder.put(configEntry.getKey(), configEntry.getValue());
}
Settings settings = settingsBuilder.build();
public void newConfig(BytesReference settingsSource) throws IOException {
Settings settings = ImmutableSettings.settingsBuilder().loadFromSource(settingsSource.toUtf8()).build();
for (ConfigurableComponentListener componentListener : registeredComponents) {
componentListener.receiveConfigurationUpdate(settings);
}
@ -89,15 +77,21 @@ public class ConfigurationManager extends AbstractComponent {
* @param clusterState
* @return true if ready to read or false if not
*/
public boolean isReady(ClusterState clusterState) {
if (readyToRead) {
public boolean start(ClusterState clusterState) {
if (started) {
return true;
} else {
readyToRead = checkIndexState(clusterState);
return readyToRead;
started = checkIndexState(clusterState);
return started;
}
}
public void stop() {
// Even though we just check if the config index is started, we need to do it again if alert manager is restarted,
// the index may not be available
started = false;
}
/**
* Registers an component to receive config updates
*/
@ -107,8 +101,8 @@ public class ConfigurationManager extends AbstractComponent {
}
}
private void ensureReady() {
if (!readyToRead) {
private void ensureStarted() {
if (!started) {
throw new ElasticsearchException("Config index [" + CONFIG_INDEX + "] is not known to be started");
}
}
@ -121,7 +115,6 @@ public class ConfigurationManager extends AbstractComponent {
} else {
if (clusterState.routingTable().index(CONFIG_INDEX).allPrimaryShardsActive()) {
logger.info("Index [" + CONFIG_INDEX + "] is started.");
return true;
} else {
return false;

View File

@ -109,7 +109,7 @@ public class AlertActionManager extends AbstractComponent {
if (started.get()) {
return true;
}
if (!configurationManager.isReady(state)) {
if (!configurationManager.start(state)) {
return false;
}

View File

@ -35,7 +35,7 @@ public class SmtpAlertActionFactory implements AlertActionFactory, ConfigurableC
private static final String PASSWD_SETTING = "alerts.action.email.from.passwd";
private final ConfigurationManager configurationManager;
private Settings settings;
private volatile Settings settings;
public SmtpAlertActionFactory(ConfigurationManager configurationManager) {
this.configurationManager = configurationManager;
@ -158,6 +158,6 @@ public class SmtpAlertActionFactory implements AlertActionFactory, ConfigurableC
@Override
public void receiveConfigurationUpdate(Settings settings) {
this.settings = settings;
}
}

View File

@ -41,7 +41,7 @@ public class ConfigTest extends ElasticsearchIntegrationTest {
ConfigurationManager configurationManager = new ConfigurationManager(oldSettings, client());
boolean isReady = configurationManager.isReady(ClusterState.builder(new ClusterName("foobar")).build());
boolean isReady = configurationManager.start(ClusterState.builder(new ClusterName("foobar")).build());
assertTrue(isReady); //Should always be ready on a clean start
SettingsListener settingsListener = new SettingsListener();
@ -85,7 +85,7 @@ public class ConfigTest extends ElasticsearchIntegrationTest {
.get();
assertTrue(indexResponse.isCreated());
ConfigurationManager configurationManager = new ConfigurationManager(oldSettings, client());
assertTrue(configurationManager.isReady(ClusterState.builder(new ClusterName("foobar")).build()));
assertTrue(configurationManager.start(ClusterState.builder(new ClusterName("foobar")).build()));
Settings loadedSettings = configurationManager.getGlobalConfig();
assertThat(loadedSettings.get("foo"), equalTo(newSettings.get("foo")));
assertThat(loadedSettings.get("bar"), equalTo(newSettings.get("bar")));