Skip to content

Commit 4ad3d93

Browse files
authored
Slm set operation mode to RUNNING on first run (#49236)
* SLM set the operation mode to RUNNING on first run Set the SLM operation mode to RUNNING when setting the first SLM lifecycle policy. Historically, SLM was not decoupled from ILM but now they are independent components. Setting the SLM operation mode to what the ILM running mode was when we set the first SLM lifecycle policy was a remain from those times. * SLM update package info * SLM suppress unusued warning * SLM use logger for the correct class * SLM Add integration test for operation mode * Use ESSingleNodeTestCase instead of ESIntegTestCase
1 parent cc89d53 commit 4ad3d93

File tree

5 files changed

+87
-11
lines changed

5 files changed

+87
-11
lines changed

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycle.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
176176
components.add(indexLifecycleInitialisationService.get());
177177
}
178178
if (slmEnabled) {
179+
// the template registry is a cluster state listener
180+
@SuppressWarnings("unused")
179181
SnapshotLifecycleTemplateRegistry templateRegistry = new SnapshotLifecycleTemplateRegistry(settings, clusterService, threadPool,
180182
client, xContentRegistry);
181183
snapshotHistoryStore.set(new SnapshotHistoryStore(settings, new OriginSettingClient(client, INDEX_LIFECYCLE_ORIGIN),

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSnapshotLifecycleAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
public class TransportGetSnapshotLifecycleAction extends
4343
TransportMasterNodeAction<GetSnapshotLifecycleAction.Request, GetSnapshotLifecycleAction.Response> {
4444

45-
private static final Logger logger = LogManager.getLogger(TransportPutSnapshotLifecycleAction.class);
45+
private static final Logger logger = LogManager.getLogger(TransportGetSnapshotLifecycleAction.class);
4646

4747
@Inject
4848
public TransportGetSnapshotLifecycleAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool,

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportPutSnapshotLifecycleAction.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.elasticsearch.threadpool.ThreadPool;
2525
import org.elasticsearch.transport.TransportService;
2626
import org.elasticsearch.xpack.core.ClientHelper;
27-
import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata;
2827
import org.elasticsearch.xpack.core.ilm.LifecyclePolicy;
2928
import org.elasticsearch.xpack.core.ilm.OperationMode;
3029
import org.elasticsearch.xpack.core.slm.SnapshotLifecycleMetadata;
@@ -38,7 +37,6 @@
3837
import java.util.Collections;
3938
import java.util.HashMap;
4039
import java.util.Map;
41-
import java.util.Optional;
4240
import java.util.stream.Collectors;
4341

4442
public class TransportPutSnapshotLifecycleAction extends
@@ -90,12 +88,8 @@ public ClusterState execute(ClusterState currentState) {
9088
.setHeaders(filteredHeaders)
9189
.setModifiedDate(Instant.now().toEpochMilli())
9290
.build();
93-
IndexLifecycleMetadata ilmMeta = currentState.metaData().custom(IndexLifecycleMetadata.TYPE);
94-
OperationMode mode = Optional.ofNullable(ilmMeta)
95-
.map(IndexLifecycleMetadata::getOperationMode)
96-
.orElse(OperationMode.RUNNING);
9791
lifecycleMetadata = new SnapshotLifecycleMetadata(Collections.singletonMap(id, meta),
98-
mode, new SnapshotLifecycleStats());
92+
OperationMode.RUNNING, new SnapshotLifecycleStats());
9993
logger.info("adding new snapshot lifecycle [{}]", id);
10094
} else {
10195
Map<String, SnapshotLifecyclePolicyMetadata> snapLifecycles = new HashMap<>(snapMeta.getSnapshotConfigurations());

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/package-info.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66

77
/**
8-
* This is the Snapshot Lifecycle Management (SLM) main package. SLM is part of the wider ILM feature, reusing quite a bit of the
9-
* functionality for itself in some places, which is why the two features are contained in the same plugin.
8+
* This is the Snapshot Lifecycle Management (SLM) main package. SLM is part of the wider ILM feature, which is why the two features are
9+
* contained in the same plugin. However SLM is enabled, configured and run independently of ILM.
1010
*
1111
* This package contains the {@link org.elasticsearch.xpack.slm.SnapshotLifecycleService} and
1212
* {@link org.elasticsearch.xpack.slm.SnapshotLifecycleTask}, as well as the Rest and Transport actions for the
@@ -18,7 +18,7 @@
1818
* <p>{@link org.elasticsearch.xpack.slm.SnapshotLifecycleService} maintains an internal
1919
* {@link org.elasticsearch.xpack.core.scheduler.SchedulerEngine SchedulerEngine} that handles scheduling snapshots. The service
2020
* executes on the currently elected master node. It listens to the cluster state, detecting new policies to schedule, and unscheduling
21-
* policies when they are deleted or if ILM is stopped. The bulk of this scheduling management is handled within
21+
* policies when they are deleted or if SLM is stopped. The bulk of this scheduling management is handled within
2222
* {@link org.elasticsearch.xpack.slm.SnapshotLifecycleService#maybeScheduleSnapshot(SnapshotLifecyclePolicyMetadata)}
2323
* which is executed on all snapshot policies each update.
2424
*
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.slm;
7+
8+
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryAction;
9+
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest;
10+
import org.elasticsearch.cluster.ClusterState;
11+
import org.elasticsearch.cluster.service.ClusterService;
12+
import org.elasticsearch.common.settings.Settings;
13+
import org.elasticsearch.env.Environment;
14+
import org.elasticsearch.plugins.Plugin;
15+
import org.elasticsearch.test.ESSingleNodeTestCase;
16+
import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin;
17+
import org.elasticsearch.xpack.core.XPackSettings;
18+
import org.elasticsearch.xpack.core.ilm.OperationMode;
19+
import org.elasticsearch.xpack.core.slm.SnapshotLifecycleMetadata;
20+
import org.elasticsearch.xpack.core.slm.SnapshotLifecyclePolicy;
21+
import org.elasticsearch.xpack.core.slm.SnapshotRetentionConfiguration;
22+
import org.elasticsearch.xpack.core.slm.action.PutSnapshotLifecycleAction;
23+
import org.elasticsearch.xpack.core.slm.action.PutSnapshotLifecycleAction.Request;
24+
import org.elasticsearch.xpack.ilm.IndexLifecycle;
25+
import org.junit.BeforeClass;
26+
27+
import java.nio.file.Path;
28+
import java.util.Arrays;
29+
import java.util.Collection;
30+
import java.util.Collections;
31+
import java.util.concurrent.TimeUnit;
32+
33+
import static org.hamcrest.core.Is.is;
34+
35+
public class SnapshotLifecycleInitialisationTests extends ESSingleNodeTestCase {
36+
37+
private static Path repositoryLocation;
38+
39+
@BeforeClass
40+
public static void setupRepositoryPath() {
41+
repositoryLocation = createTempDir();
42+
}
43+
44+
@Override
45+
protected Settings nodeSettings() {
46+
Settings.Builder settings = Settings.builder().put(super.nodeSettings());
47+
settings.put(XPackSettings.INDEX_LIFECYCLE_ENABLED.getKey(), false);
48+
settings.put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), false);
49+
settings.put(XPackSettings.SECURITY_ENABLED.getKey(), false);
50+
settings.put(XPackSettings.WATCHER_ENABLED.getKey(), false);
51+
settings.put(XPackSettings.MONITORING_ENABLED.getKey(), false);
52+
settings.put(XPackSettings.GRAPH_ENABLED.getKey(), false);
53+
settings.put(XPackSettings.LOGSTASH_ENABLED.getKey(), false);
54+
settings.put(Environment.PATH_REPO_SETTING.getKey(), repositoryLocation);
55+
56+
settings.put(XPackSettings.SNAPSHOT_LIFECYCLE_ENABLED.getKey(), true);
57+
return settings.build();
58+
}
59+
60+
@Override
61+
protected Collection<Class<? extends Plugin>> getPlugins() {
62+
return Arrays.asList(LocalStateCompositeXPackPlugin.class, IndexLifecycle.class);
63+
}
64+
65+
public void testSLMIsInRunningModeWhenILMIsDisabled() throws Exception {
66+
client().execute(PutRepositoryAction.INSTANCE,
67+
new PutRepositoryRequest().name("repo").type("fs")
68+
.settings(Settings.builder().put("repositories.fs.location", repositoryLocation).build())
69+
).get(10, TimeUnit.SECONDS);
70+
71+
client().execute(PutSnapshotLifecycleAction.INSTANCE,
72+
new Request("snapshot-policy", new SnapshotLifecyclePolicy("test-policy", "snap",
73+
"*/1 * * * * ?", "repo", Collections.emptyMap(), SnapshotRetentionConfiguration.EMPTY))
74+
).get(10, TimeUnit.SECONDS);
75+
76+
ClusterState state = getInstanceFromNode(ClusterService.class).state();
77+
SnapshotLifecycleMetadata snapMeta = state.metaData().custom(SnapshotLifecycleMetadata.TYPE);
78+
assertThat(snapMeta.getOperationMode(), is(OperationMode.RUNNING));
79+
}
80+
}

0 commit comments

Comments
 (0)