Skip to content

Commit ae967be

Browse files
authored
Convert ccr license object to LicensedFeature (#79215) (#79253)
* Convert ccr license object to LicensedFeature (#79215) This commit moves the ccr license checks to use the new LicensedFeature class. * checkstyle
1 parent 9eea5a0 commit ae967be

File tree

8 files changed

+29
-20
lines changed

8 files changed

+29
-20
lines changed

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrLicenseChecker.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
import org.elasticsearch.cluster.metadata.DataStream;
2828
import org.elasticsearch.cluster.metadata.IndexAbstraction;
2929
import org.elasticsearch.cluster.metadata.IndexMetadata;
30-
import org.elasticsearch.core.CheckedConsumer;
3130
import org.elasticsearch.common.Strings;
32-
import org.elasticsearch.core.Tuple;
3331
import org.elasticsearch.common.settings.Settings;
3432
import org.elasticsearch.common.util.concurrent.ThreadContext;
33+
import org.elasticsearch.core.CheckedConsumer;
34+
import org.elasticsearch.core.Tuple;
3535
import org.elasticsearch.index.IndexNotFoundException;
3636
import org.elasticsearch.index.engine.CommitStats;
3737
import org.elasticsearch.index.engine.Engine;
@@ -43,6 +43,7 @@
4343
import org.elasticsearch.xpack.ccr.action.ShardChangesAction;
4444
import org.elasticsearch.xpack.core.ClientHelper;
4545
import org.elasticsearch.xpack.core.XPackPlugin;
46+
import org.elasticsearch.xpack.core.ccr.CcrConstants;
4647
import org.elasticsearch.xpack.core.security.SecurityContext;
4748
import org.elasticsearch.xpack.core.security.action.user.HasPrivilegesAction;
4849
import org.elasticsearch.xpack.core.security.action.user.HasPrivilegesRequest;
@@ -75,7 +76,7 @@ public class CcrLicenseChecker {
7576
* Constructs a CCR license checker with the default rule based on the license state for checking if CCR is allowed.
7677
*/
7778
CcrLicenseChecker() {
78-
this(() -> XPackPlugin.getSharedLicenseState().checkFeature(XPackLicenseState.Feature.CCR),
79+
this(() -> CcrConstants.CCR_FEATURE.check(XPackPlugin.getSharedLicenseState()),
7980
XPackPlugin.getSharedLicenseState()::isSecurityEnabled);
8081
}
8182

x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CCRFeatureSetTests.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
import org.elasticsearch.cluster.metadata.Metadata;
1515
import org.elasticsearch.cluster.service.ClusterService;
1616
import org.elasticsearch.common.settings.Settings;
17-
import org.elasticsearch.license.XPackLicenseState;
17+
import org.elasticsearch.license.MockLicenseState;
1818
import org.elasticsearch.test.ESTestCase;
1919
import org.elasticsearch.xpack.core.XPackFeatureSet;
2020
import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata;
21-
import org.elasticsearch.xpack.core.ccr.CcrConstants;
2221
import org.elasticsearch.xpack.core.ccr.CCRFeatureSet;
22+
import org.elasticsearch.xpack.core.ccr.CcrConstants;
2323
import org.junit.Before;
2424
import org.mockito.Mockito;
2525

@@ -35,22 +35,22 @@
3535

3636
public class CCRFeatureSetTests extends ESTestCase {
3737

38-
private XPackLicenseState licenseState;
38+
private MockLicenseState licenseState;
3939
private ClusterService clusterService;
4040

4141
@Before
4242
public void init() {
43-
licenseState = mock(XPackLicenseState.class);
43+
licenseState = mock(MockLicenseState.class);
4444
clusterService = mock(ClusterService.class);
4545
}
4646

4747
public void testAvailable() {
4848
CCRFeatureSet featureSet = new CCRFeatureSet(Settings.EMPTY, licenseState, clusterService);
4949

50-
when(licenseState.isAllowed(XPackLicenseState.Feature.CCR)).thenReturn(false);
50+
when(licenseState.isAllowed(CcrConstants.CCR_FEATURE)).thenReturn(false);
5151
assertThat(featureSet.available(), equalTo(false));
5252

53-
when(licenseState.isAllowed(XPackLicenseState.Feature.CCR)).thenReturn(true);
53+
when(licenseState.isAllowed(CcrConstants.CCR_FEATURE)).thenReturn(true);
5454
assertThat(featureSet.available(), equalTo(true));
5555

5656
featureSet = new CCRFeatureSet(Settings.EMPTY, null, clusterService);

x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollectorTests.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
import org.elasticsearch.client.Client;
1111
import org.elasticsearch.cluster.service.ClusterService;
1212
import org.elasticsearch.common.settings.Settings;
13-
import org.elasticsearch.core.TimeValue;
1413
import org.elasticsearch.common.util.concurrent.ThreadContext;
14+
import org.elasticsearch.core.TimeValue;
1515
import org.elasticsearch.license.XPackLicenseState;
1616
import org.elasticsearch.xpack.core.XPackSettings;
1717
import org.elasticsearch.xpack.core.ccr.AutoFollowStats;
18+
import org.elasticsearch.xpack.core.ccr.CcrConstants;
1819
import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus;
1920
import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction;
2021
import org.elasticsearch.xpack.core.ccr.action.CcrStatsAction;
@@ -44,7 +45,7 @@ public void testShouldCollectReturnsFalseIfNotMaster() {
4445
// regardless of CCR being enabled
4546
final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings());
4647

47-
when(licenseState.checkFeature(XPackLicenseState.Feature.CCR)).thenReturn(randomBoolean());
48+
when(licenseState.isAllowed(CcrConstants.CCR_FEATURE)).thenReturn(randomBoolean());
4849
// this controls the blockage
4950
final boolean isElectedMaster = false;
5051

@@ -57,7 +58,7 @@ public void testShouldCollectReturnsFalseIfCCRIsDisabled() {
5758
// this is controls the blockage
5859
final Settings settings = ccrDisabledSettings();
5960

60-
when(licenseState.checkFeature(XPackLicenseState.Feature.CCR)).thenReturn(randomBoolean());
61+
when(licenseState.isAllowed(CcrConstants.CCR_FEATURE)).thenReturn(randomBoolean());
6162

6263
final boolean isElectedMaster = randomBoolean();
6364
whenLocalNodeElectedMaster(isElectedMaster);
@@ -71,7 +72,7 @@ public void testShouldCollectReturnsFalseIfCCRIsNotAllowed() {
7172
final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings());
7273

7374
// this is controls the blockage
74-
when(licenseState.checkFeature(XPackLicenseState.Feature.CCR)).thenReturn(false);
75+
when(licenseState.isAllowed(CcrConstants.CCR_FEATURE)).thenReturn(false);
7576
final boolean isElectedMaster = randomBoolean();
7677
whenLocalNodeElectedMaster(isElectedMaster);
7778

@@ -83,7 +84,7 @@ public void testShouldCollectReturnsFalseIfCCRIsNotAllowed() {
8384
public void testShouldCollectReturnsTrue() {
8485
final Settings settings = ccrEnabledSettings();
8586

86-
when(licenseState.checkFeature(XPackLicenseState.Feature.CCR)).thenReturn(true);
87+
when(licenseState.isAllowed(CcrConstants.CCR_FEATURE)).thenReturn(true);
8788
final boolean isElectedMaster = true;
8889

8990
final StatsCollector collector = createCollector(settings, clusterService, licenseState, client);

x-pack/plugin/core/src/main/java/org/elasticsearch/license/XPackLicenseState.java

-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ public enum Feature {
4949

5050
MONITORING_CLUSTER_ALERTS(OperationMode.STANDARD, true),
5151

52-
CCR(OperationMode.PLATINUM, true),
53-
5452
MACHINE_LEARNING(OperationMode.PLATINUM, true),
5553

5654
OPERATOR_PRIVILEGES(OperationMode.ENTERPRISE, true);

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/CCRFeatureSet.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public String name() {
4747

4848
@Override
4949
public boolean available() {
50-
return licenseState != null && licenseState.isAllowed(XPackLicenseState.Feature.CCR);
50+
return licenseState != null && CcrConstants.CCR_FEATURE.checkWithoutTracking(licenseState);
5151
}
5252

5353
@Override

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/CcrConstants.java

+7
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
77

88
package org.elasticsearch.xpack.core.ccr;
99

10+
import org.elasticsearch.license.License;
11+
import org.elasticsearch.license.LicensedFeature;
12+
1013
public class CcrConstants {
1114
public static final String CCR_CUSTOM_METADATA_KEY = "ccr";
1215
public static final String CCR_CUSTOM_METADATA_LEADER_INDEX_UUID_KEY = "leader_index_uuid";
1316
public static final String CCR_CUSTOM_METADATA_LEADER_INDEX_SHARD_HISTORY_UUIDS = "leader_index_shard_history_uuids";
1417
public static final String CCR_CUSTOM_METADATA_LEADER_INDEX_NAME_KEY = "leader_index_name";
1518
public static final String CCR_CUSTOM_METADATA_REMOTE_CLUSTER_NAME_KEY = "remote_cluster_name";
1619

20+
public static final LicensedFeature.Momentary CCR_FEATURE =
21+
LicensedFeature.momentary(null, "ccr", License.OperationMode.PLATINUM);
22+
23+
// no construction
1724
private CcrConstants() {}
1825
}

x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollector.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.license.XPackLicenseState;
1818
import org.elasticsearch.xpack.core.XPackClient;
1919
import org.elasticsearch.xpack.core.XPackSettings;
20+
import org.elasticsearch.xpack.core.ccr.CcrConstants;
2021
import org.elasticsearch.xpack.core.ccr.action.CcrStatsAction;
2122
import org.elasticsearch.xpack.core.ccr.client.CcrClient;
2223
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc;
@@ -66,7 +67,7 @@ protected boolean shouldCollect(final boolean isElectedMaster) {
6667
return isElectedMaster
6768
&& super.shouldCollect(isElectedMaster)
6869
&& XPackSettings.CCR_ENABLED_SETTING.get(settings)
69-
&& licenseState.checkFeature(XPackLicenseState.Feature.CCR);
70+
&& CcrConstants.CCR_FEATURE.checkWithoutTracking(licenseState);
7071
}
7172

7273

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/BaseCollectorTestCase.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.core.TimeValue;
2222
import org.elasticsearch.common.util.concurrent.ThreadContext;
2323
import org.elasticsearch.common.util.set.Sets;
24+
import org.elasticsearch.license.MockLicenseState;
2425
import org.elasticsearch.license.XPackLicenseState;
2526
import org.elasticsearch.test.ESTestCase;
2627
import org.elasticsearch.threadpool.ThreadPool;
@@ -38,7 +39,7 @@ public abstract class BaseCollectorTestCase extends ESTestCase {
3839
protected ClusterState clusterState;
3940
protected DiscoveryNodes nodes;
4041
protected Metadata metadata;
41-
protected XPackLicenseState licenseState;
42+
protected MockLicenseState licenseState;
4243
protected Client client;
4344
protected Settings settings;
4445

@@ -50,7 +51,7 @@ public void setUp() throws Exception {
5051
clusterState = mock(ClusterState.class);
5152
nodes = mock(DiscoveryNodes.class);
5253
metadata = mock(Metadata.class);
53-
licenseState = mock(XPackLicenseState.class);
54+
licenseState = mock(MockLicenseState.class);
5455
client = mock(Client.class);
5556
ThreadPool threadPool = mock(ThreadPool.class);
5657
when(client.threadPool()).thenReturn(threadPool);

0 commit comments

Comments
 (0)