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 transport actions instead of guice for xpack info #43449

Merged
merged 13 commits into from
Jun 24, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.IndexScopedSettings;
Expand Down Expand Up @@ -87,6 +86,7 @@
import org.elasticsearch.xpack.ccr.rest.RestResumeFollowAction;
import org.elasticsearch.xpack.ccr.rest.RestUnfollowAction;
import org.elasticsearch.xpack.core.XPackPlugin;
import org.elasticsearch.xpack.core.action.XPackInfoFeatureAction;
import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction;
import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata;
import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus;
Expand All @@ -112,7 +112,6 @@
import java.util.function.Supplier;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.elasticsearch.xpack.ccr.CcrSettings.CCR_FOLLOWING_INDEX_SETTING;
import static org.elasticsearch.xpack.core.XPackSettings.CCR_ENABLED_SETTING;

Expand Down Expand Up @@ -201,9 +200,10 @@ public List<PersistentTasksExecutor<?>> getPersistentTasksExecutor(ClusterServic
}

public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() {
var usageAction = new ActionHandler<>(XPackUsageFeatureAction.CCR, CCRFeatureSet.UsageTransportAction.class);
var usageAction = new ActionHandler<>(XPackUsageFeatureAction.CCR, CCRUsageTransportAction.class);
var infoAction = new ActionHandler<>(XPackInfoFeatureAction.CCR, CCRInfoTransportAction.class);
if (enabled == false) {
return singletonList(usageAction);
return Arrays.asList(usageAction, infoAction);
}

return Arrays.asList(
Expand Down Expand Up @@ -235,7 +235,8 @@ public List<PersistentTasksExecutor<?>> getPersistentTasksExecutor(ClusterServic
new ActionHandler<>(GetAutoFollowPatternAction.INSTANCE, TransportGetAutoFollowPatternAction.class),
// forget follower action
new ActionHandler<>(ForgetFollowerAction.INSTANCE, TransportForgetFollowerAction.class),
usageAction);
usageAction,
infoAction);
}

public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings,
Expand Down Expand Up @@ -341,11 +342,6 @@ public void onIndexModule(IndexModule indexModule) {
}
}

@Override
public Collection<Module> createGuiceModules() {
return Collections.singleton(b -> XPackPlugin.bindFeatureSet(b, CCRFeatureSet.class));
}

protected XPackLicenseState getLicenseState() { return XPackPlugin.getSharedLicenseState(); }

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.AbstractWireSerializingTestCase;

public class CCRFeatureSetUsageTests extends AbstractWireSerializingTestCase<CCRFeatureSet.Usage> {
public class CCRFeatureSetUsageTests extends AbstractWireSerializingTestCase<CCRInfoTransportAction.Usage> {

@Override
protected CCRFeatureSet.Usage createTestInstance() {
return new CCRFeatureSet.Usage(randomBoolean(), randomBoolean(), randomIntBetween(0, Integer.MAX_VALUE),
protected CCRInfoTransportAction.Usage createTestInstance() {
return new CCRInfoTransportAction.Usage(randomBoolean(), randomBoolean(), randomIntBetween(0, Integer.MAX_VALUE),
randomIntBetween(0, Integer.MAX_VALUE), randomNonNegativeLong());
}

@Override
protected Writeable.Reader<CCRFeatureSet.Usage> instanceReader() {
return CCRFeatureSet.Usage::new;
protected Writeable.Reader<CCRInfoTransportAction.Usage> instanceReader() {
return CCRInfoTransportAction.Usage::new;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class CCRFeatureSetTests extends ESTestCase {
public class CCRInfoTransportActionTests extends ESTestCase {

private XPackLicenseState licenseState;
private ClusterService clusterService;
Expand All @@ -44,30 +44,34 @@ public void init() {
}

public void testAvailable() {
CCRFeatureSet featureSet = new CCRFeatureSet(Settings.EMPTY, licenseState);
CCRInfoTransportAction featureSet = new CCRInfoTransportAction(
mock(TransportService.class), mock(ActionFilters.class), Settings.EMPTY, licenseState);

when(licenseState.isCcrAllowed()).thenReturn(false);
assertThat(featureSet.available(), equalTo(false));

when(licenseState.isCcrAllowed()).thenReturn(true);
assertThat(featureSet.available(), equalTo(true));

featureSet = new CCRFeatureSet(Settings.EMPTY, null);
featureSet = new CCRInfoTransportAction(
mock(TransportService.class), mock(ActionFilters.class), Settings.EMPTY, null);
assertThat(featureSet.available(), equalTo(false));
}

public void testEnabled() {
Settings.Builder settings = Settings.builder().put("xpack.ccr.enabled", false);
CCRFeatureSet featureSet = new CCRFeatureSet(settings.build(), licenseState);
CCRInfoTransportAction featureSet = new CCRInfoTransportAction(
mock(TransportService.class), mock(ActionFilters.class), settings.build(), licenseState);
assertThat(featureSet.enabled(), equalTo(false));

settings = Settings.builder().put("xpack.ccr.enabled", true);
featureSet = new CCRFeatureSet(settings.build(), licenseState);
featureSet = new CCRInfoTransportAction(mock(TransportService.class), null, settings.build(), licenseState);
assertThat(featureSet.enabled(), equalTo(true));
}

public void testName() {
CCRFeatureSet featureSet = new CCRFeatureSet(Settings.EMPTY, licenseState);
CCRInfoTransportAction featureSet = new CCRInfoTransportAction(
mock(TransportService.class), mock(ActionFilters.class), Settings.EMPTY, licenseState);
assertThat(featureSet.name(), equalTo("ccr"));
}

Expand Down Expand Up @@ -105,11 +109,11 @@ public void testUsageStats() throws Exception {
ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).metaData(metaData).build();
Mockito.when(clusterService.state()).thenReturn(clusterState);

var usageAction = new CCRFeatureSet.UsageTransportAction(mock(TransportService.class), null, null,
var usageAction = new CCRUsageTransportAction(mock(TransportService.class), null, null,
mock(ActionFilters.class), null, Settings.EMPTY, licenseState);
PlainActionFuture<XPackUsageFeatureResponse> future = new PlainActionFuture<>();
usageAction.masterOperation(null, clusterState, future);
CCRFeatureSet.Usage ccrUsage = (CCRFeatureSet.Usage) future.get().getUsage();
CCRInfoTransportAction.Usage ccrUsage = (CCRInfoTransportAction.Usage) future.get().getUsage();
assertThat(ccrUsage.enabled(), equalTo(true));
assertThat(ccrUsage.available(), equalTo(false));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,32 @@
*/
package org.elasticsearch.xpack.ccr;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.license.XPackLicenseState;
import org.elasticsearch.protocol.xpack.XPackUsageRequest;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.xpack.core.XPackFeatureSet;
import org.elasticsearch.xpack.core.XPackField;
import org.elasticsearch.xpack.core.XPackSettings;
import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction;
import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse;
import org.elasticsearch.xpack.core.action.XPackUsageFeatureTransportAction;
import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata;
import org.elasticsearch.xpack.core.action.XPackInfoFeatureAction;
import org.elasticsearch.xpack.core.action.XPackInfoFeatureTransportAction;

import java.io.IOException;
import java.time.Instant;
import java.util.Objects;

public class CCRFeatureSet implements XPackFeatureSet {
public class CCRInfoTransportAction extends XPackInfoFeatureTransportAction {

private final boolean enabled;
private final XPackLicenseState licenseState;

@Inject
public CCRFeatureSet(Settings settings, XPackLicenseState licenseState) {
public CCRInfoTransportAction(TransportService transportService, ActionFilters actionFilters,
Settings settings, XPackLicenseState licenseState) {
super(XPackInfoFeatureAction.CCR.name(), transportService, actionFilters);
this.enabled = XPackSettings.CCR_ENABLED_SETTING.get(settings);
this.licenseState = licenseState;
}
Expand All @@ -51,60 +42,14 @@ public String name() {

@Override
public boolean available() {
return licenseState != null && licenseState.isCcrAllowed();
return licenseState.isCcrAllowed();
}

@Override
public boolean enabled() {
return enabled;
}

public static class UsageTransportAction extends XPackUsageFeatureTransportAction {

private final Settings settings;
private final XPackLicenseState licenseState;

@Inject
public UsageTransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool,
ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver,
Settings settings, XPackLicenseState licenseState) {
super(XPackUsageFeatureAction.CCR.name(), transportService, clusterService,
threadPool, actionFilters, indexNameExpressionResolver);
this.settings = settings;
this.licenseState = licenseState;
}

@Override
protected void masterOperation(XPackUsageRequest request, ClusterState state, ActionListener<XPackUsageFeatureResponse> listener) {
MetaData metaData = state.metaData();

int numberOfFollowerIndices = 0;
long lastFollowerIndexCreationDate = 0L;
for (IndexMetaData imd : metaData) {
if (imd.getCustomData("ccr") != null) {
numberOfFollowerIndices++;
if (lastFollowerIndexCreationDate < imd.getCreationDate()) {
lastFollowerIndexCreationDate = imd.getCreationDate();
}
}
}
AutoFollowMetadata autoFollowMetadata = metaData.custom(AutoFollowMetadata.TYPE);
int numberOfAutoFollowPatterns = autoFollowMetadata != null ? autoFollowMetadata.getPatterns().size() : 0;

Long lastFollowTimeInMillis;
if (numberOfFollowerIndices == 0) {
// Otherwise we would return a value that makes no sense.
lastFollowTimeInMillis = null;
} else {
lastFollowTimeInMillis = Math.max(0, Instant.now().toEpochMilli() - lastFollowerIndexCreationDate);
}

Usage usage = new Usage(licenseState.isCcrAllowed(), XPackSettings.CCR_ENABLED_SETTING.get(settings),
numberOfFollowerIndices, numberOfAutoFollowPatterns, lastFollowTimeInMillis);
listener.onResponse(new XPackUsageFeatureResponse(usage));
}
}

public static class Usage extends XPackFeatureSet.Usage {

private final int numberOfFollowerIndices;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.ccr;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.XPackLicenseState;
import org.elasticsearch.protocol.xpack.XPackUsageRequest;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.xpack.core.XPackSettings;
import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction;
import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse;
import org.elasticsearch.xpack.core.action.XPackUsageFeatureTransportAction;
import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata;

import java.time.Instant;

public class CCRUsageTransportAction extends XPackUsageFeatureTransportAction {

private final Settings settings;
private final XPackLicenseState licenseState;

@Inject
public CCRUsageTransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool,
ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver,
Settings settings, XPackLicenseState licenseState) {
super(XPackUsageFeatureAction.CCR.name(), transportService, clusterService,
threadPool, actionFilters, indexNameExpressionResolver);
this.settings = settings;
this.licenseState = licenseState;
}

@Override
protected void masterOperation(XPackUsageRequest request, ClusterState state, ActionListener<XPackUsageFeatureResponse> listener) {
MetaData metaData = state.metaData();

int numberOfFollowerIndices = 0;
long lastFollowerIndexCreationDate = 0L;
for (IndexMetaData imd : metaData) {
if (imd.getCustomData("ccr") != null) {
numberOfFollowerIndices++;
if (lastFollowerIndexCreationDate < imd.getCreationDate()) {
lastFollowerIndexCreationDate = imd.getCreationDate();
}
}
}
AutoFollowMetadata autoFollowMetadata = metaData.custom(AutoFollowMetadata.TYPE);
int numberOfAutoFollowPatterns = autoFollowMetadata != null ? autoFollowMetadata.getPatterns().size() : 0;

Long lastFollowTimeInMillis;
if (numberOfFollowerIndices == 0) {
// Otherwise we would return a value that makes no sense.
lastFollowTimeInMillis = null;
} else {
lastFollowTimeInMillis = Math.max(0, Instant.now().toEpochMilli() - lastFollowerIndexCreationDate);
}

CCRInfoTransportAction.Usage usage = new CCRInfoTransportAction.Usage(licenseState.isCcrAllowed(),
XPackSettings.CCR_ENABLED_SETTING.get(settings), numberOfFollowerIndices, numberOfAutoFollowPatterns, lastFollowTimeInMillis);
listener.onResponse(new XPackUsageFeatureResponse(usage));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.elasticsearch.xpack.core.action.XPackUsageAction;
import org.elasticsearch.xpack.core.beats.BeatsFeatureSetUsage;
import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata;
import org.elasticsearch.xpack.ccr.CCRFeatureSet;
import org.elasticsearch.xpack.ccr.CCRInfoTransportAction;
import org.elasticsearch.xpack.core.dataframe.DataFrameFeatureSetUsage;
import org.elasticsearch.xpack.core.dataframe.DataFrameField;
import org.elasticsearch.xpack.core.dataframe.action.DeleteDataFrameTransformAction;
Expand Down Expand Up @@ -413,7 +413,7 @@ public List<NamedWriteableRegistry.Entry> getNamedWriteables() {
new NamedWriteableRegistry.Entry(MetaData.Custom.class, AutoFollowMetadata.TYPE, AutoFollowMetadata::new),
new NamedWriteableRegistry.Entry(NamedDiff.class, AutoFollowMetadata.TYPE,
in -> AutoFollowMetadata.readDiffFrom(MetaData.Custom.class, AutoFollowMetadata.TYPE, in)),
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.CCR, CCRFeatureSet.Usage::new),
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.CCR, CCRInfoTransportAction.Usage::new),
// ILM
new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.INDEX_LIFECYCLE,
IndexLifecycleFeatureSetUsage::new),
Expand Down
Loading