Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use environment settings instead of state settings for Watcher config #41087

Merged
merged 5 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
throw new UncheckedIOException(e);
}

new WatcherIndexTemplateRegistry(clusterService, threadPool, client, xContentRegistry);
new WatcherIndexTemplateRegistry(environment.settings(), clusterService, threadPool, client, xContentRegistry);

// http client
httpClient = new HttpClient(settings, getSslService(), cryptoService, clusterService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.elasticsearch.cluster.ClusterStateListener;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentType;
Expand Down Expand Up @@ -63,14 +64,17 @@ public class WatcherIndexTemplateRegistry implements ClusterStateListener {

private static final Logger logger = LogManager.getLogger(WatcherIndexTemplateRegistry.class);

private final Settings settings;
private final Client client;
private final ThreadPool threadPool;
private final NamedXContentRegistry xContentRegistry;
private final ConcurrentMap<String, AtomicBoolean> templateCreationsInProgress = new ConcurrentHashMap<>();
private final AtomicBoolean historyPolicyCreationInProgress = new AtomicBoolean();

public WatcherIndexTemplateRegistry(ClusterService clusterService, ThreadPool threadPool, Client client,
public WatcherIndexTemplateRegistry(Settings clusterSettings, ClusterService clusterService,
ThreadPool threadPool, Client client,
NamedXContentRegistry xContentRegistry) {
this.settings = clusterSettings;
this.client = client;
this.threadPool = threadPool;
this.xContentRegistry = xContentRegistry;
Expand Down Expand Up @@ -104,7 +108,7 @@ public void clusterChanged(ClusterChangedEvent event) {
}

private void addTemplatesIfMissing(ClusterState state) {
boolean ilmSupported = XPackSettings.INDEX_LIFECYCLE_ENABLED.get(state.metaData().settings());
boolean ilmSupported = XPackSettings.INDEX_LIFECYCLE_ENABLED.get(this.settings);
final TemplateConfig[] indexTemplates = ilmSupported ? TEMPLATE_CONFIGS : TEMPLATE_CONFIGS_NO_ILM;
for (TemplateConfig template : indexTemplates) {
final String templateName = template.getTemplateName();
Expand Down Expand Up @@ -153,7 +157,7 @@ LifecyclePolicy loadWatcherHistoryPolicy() {
}

private void addIndexLifecyclePolicyIfMissing(ClusterState state) {
boolean ilmSupported = XPackSettings.INDEX_LIFECYCLE_ENABLED.get(state.metaData().settings());
boolean ilmSupported = XPackSettings.INDEX_LIFECYCLE_ENABLED.get(this.settings);
if (ilmSupported && historyPolicyCreationInProgress.compareAndSet(false, true)) {
final LifecyclePolicy policyOnDisk = loadWatcherHistoryPolicy();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ public class WatcherIndexTemplateRegistryTests extends ESTestCase {

private WatcherIndexTemplateRegistry registry;
private NamedXContentRegistry xContentRegistry;
private ClusterService clusterService;
private ThreadPool threadPool;
private Client client;

@Before
public void createRegistryAndClient() {
ThreadPool threadPool = mock(ThreadPool.class);
threadPool = mock(ThreadPool.class);
when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY));
when(threadPool.generic()).thenReturn(EsExecutors.newDirectExecutorService());

Expand All @@ -94,14 +96,14 @@ public void createRegistryAndClient() {
return null;
}).when(indicesAdminClient).putTemplate(any(PutIndexTemplateRequest.class), any(ActionListener.class));

ClusterService clusterService = mock(ClusterService.class);
clusterService = mock(ClusterService.class);
List<NamedXContentRegistry.Entry> entries = new ArrayList<>(ClusterModule.getNamedXWriteables());
entries.addAll(Arrays.asList(
new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TimeseriesLifecycleType.TYPE),
(p) -> TimeseriesLifecycleType.INSTANCE),
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse)));
xContentRegistry = new NamedXContentRegistry(entries);
registry = new WatcherIndexTemplateRegistry(clusterService, threadPool, client, xContentRegistry);
registry = new WatcherIndexTemplateRegistry(Settings.EMPTY, clusterService, threadPool, client, xContentRegistry);
}

public void testThatNonExistingTemplatesAreAddedImmediately() {
Expand Down Expand Up @@ -130,9 +132,10 @@ public void testThatNonExistingTemplatesAreAddedEvenWithILMDisabled() {
DiscoveryNode node = new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), Version.CURRENT);
DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build();

ClusterChangedEvent event = createClusterChangedEvent(Settings.builder()
registry = new WatcherIndexTemplateRegistry(Settings.builder()
.put(XPackSettings.INDEX_LIFECYCLE_ENABLED.getKey(), false).build(),
Collections.emptyList(), Collections.emptyMap(), nodes);
clusterService, threadPool, client, xContentRegistry);
ClusterChangedEvent event = createClusterChangedEvent(Settings.EMPTY, Collections.emptyList(), Collections.emptyMap(), nodes);
registry.clusterChanged(event);
ArgumentCaptor<PutIndexTemplateRequest> argumentCaptor = ArgumentCaptor.forClass(PutIndexTemplateRequest.class);
verify(client.admin().indices(), times(3)).putTemplate(argumentCaptor.capture(), anyObject());
Expand All @@ -142,8 +145,9 @@ public void testThatNonExistingTemplatesAreAddedEvenWithILMDisabled() {
WatcherIndexTemplateRegistryField.TRIGGERED_TEMPLATE_NAME), nodes);
registry.clusterChanged(newEvent);
ArgumentCaptor<PutIndexTemplateRequest> captor = ArgumentCaptor.forClass(PutIndexTemplateRequest.class);
verify(client.admin().indices(), times(4)).putTemplate(captor.capture(), anyObject());
verify(client.admin().indices(), times(5)).putTemplate(captor.capture(), anyObject());
captor.getAllValues().forEach(req -> assertNull(req.settings().get("index.lifecycle.name")));
verify(client, times(0)).execute(eq(PutLifecycleAction.INSTANCE), anyObject(), anyObject());
}

public void testThatNonExistingPoliciesAreAddedImmediately() {
Expand Down Expand Up @@ -171,9 +175,10 @@ public void testNoPolicyButILMDisabled() {
DiscoveryNode node = new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), Version.CURRENT);
DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build();

ClusterChangedEvent event = createClusterChangedEvent(Settings.builder()
.put(XPackSettings.INDEX_LIFECYCLE_ENABLED.getKey(), false).build(),
Collections.emptyList(), Collections.emptyMap(), nodes);
registry = new WatcherIndexTemplateRegistry(Settings.builder()
.put(XPackSettings.INDEX_LIFECYCLE_ENABLED.getKey(), false).build(),
clusterService, threadPool, client, xContentRegistry);
ClusterChangedEvent event = createClusterChangedEvent(Settings.EMPTY, Collections.emptyList(), Collections.emptyMap(), nodes);
registry.clusterChanged(event);
verify(client, times(0)).execute(eq(PutLifecycleAction.INSTANCE), anyObject(), anyObject());
}
Expand Down