Refactor injection mechanism

Original commit: elastic/x-pack-elasticsearch@a85d5213bf
This commit is contained in:
Igor Motov 2014-10-11 21:52:33 -04:00 committed by Areek Zillur
parent 07ec8ef93f
commit a8f5f0151d
10 changed files with 65 additions and 90 deletions

View File

@ -6,6 +6,7 @@
package org.elasticsearch.license.plugin;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Scopes;
import org.elasticsearch.license.plugin.core.LicensesManagerService;
import org.elasticsearch.license.plugin.core.LicensesService;
@ -13,7 +14,6 @@ public class LicenseModule extends AbstractModule {
@Override
protected void configure() {
//TODO: bind LicensesManagementService and LicensesValidationService to LicensesServices instead
bind(LicensesService.class).asEagerSingleton();
bind(LicensesManagerService.class).to(LicensesService.class).asEagerSingleton();
bind(LicensesService.class).in(Scopes.SINGLETON);
}
}

View File

@ -8,9 +8,13 @@ package org.elasticsearch.license.plugin;
import org.elasticsearch.action.ActionModule;
import org.elasticsearch.action.admin.cluster.ClusterAction;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.collect.ImmutableSet;
import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseAction;
import org.elasticsearch.license.plugin.action.delete.TransportDeleteLicenseAction;
import org.elasticsearch.license.plugin.action.get.GetLicenseAction;
@ -29,10 +33,17 @@ import java.util.Collection;
public class LicensePlugin extends AbstractPlugin {
private final boolean isClient;
static {
MetaData.registerFactory(LicensesMetaData.TYPE, LicensesMetaData.FACTORY);
}
@Inject
public LicensePlugin(Settings settings) {
this.isClient = DiscoveryNode.clientNode(settings);
}
@Override
public String name() {
return "license";
@ -58,12 +69,19 @@ public class LicensePlugin extends AbstractPlugin {
@Override
public Collection<Class<? extends LifecycleComponent>> services() {
return ImmutableSet.<Class<? extends LifecycleComponent>>of(LicensesService.class);
Collection<Class<? extends LifecycleComponent>> services = Lists.newArrayList();
if (!isClient) {
services.add(LicensesService.class);
}
return services;
}
@Override
public Collection<Class<? extends Module>> modules() {
if (isClient) {
return ImmutableSet.of();
}
return ImmutableSet.<Class<? extends Module>>of(LicenseModule.class);
}
}

View File

@ -17,6 +17,9 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.plugin.core.LicensesManagerService;
import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.internal.InternalNode;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;

View File

@ -17,22 +17,20 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.plugin.core.LicensesManagerService;
import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.node.Node;
import static org.elasticsearch.license.plugin.core.LicensesService.PutLicenseRequestHolder;
public class TransportPutLicenseAction extends TransportMasterNodeOperationAction<PutLicenseRequest, PutLicenseResponse> {
private final LicensesManagerService LicensesManagerService;
private final LicensesManagerService licensesManagerService;
@Inject
public TransportPutLicenseAction(Settings settings, TransportService transportService, ClusterService clusterService,
LicensesManagerService LicensesManagerService, ThreadPool threadPool, ActionFilters actionFilters) {
LicensesManagerService licensesManagerService, ThreadPool threadPool, ActionFilters actionFilters) {
super(settings, PutLicenseAction.NAME, transportService, clusterService, threadPool, actionFilters);
this.LicensesManagerService = LicensesManagerService;
this.licensesManagerService = licensesManagerService;
}
@ -59,7 +57,7 @@ public class TransportPutLicenseAction extends TransportMasterNodeOperationActio
@Override
protected void masterOperation(final PutLicenseRequest request, ClusterState state, final ActionListener<PutLicenseResponse> listener) throws ElasticsearchException {
final PutLicenseRequestHolder requestHolder = new PutLicenseRequestHolder(request, "put licenses []");
LicensesManagerService.registerLicenses(requestHolder, new ActionListener<ClusterStateUpdateResponse>() {
licensesManagerService.registerLicenses(requestHolder, new ActionListener<ClusterStateUpdateResponse>() {
@Override
public void onResponse(ClusterStateUpdateResponse clusterStateUpdateResponse) {
listener.onResponse(new PutLicenseResponse(clusterStateUpdateResponse.isAcknowledged()));

View File

@ -7,10 +7,13 @@ package org.elasticsearch.license.plugin.core;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.common.inject.ImplementedBy;
import org.elasticsearch.common.inject.Singleton;
import static org.elasticsearch.license.plugin.core.LicensesService.DeleteLicenseRequestHolder;
import static org.elasticsearch.license.plugin.core.LicensesService.PutLicenseRequestHolder;
@ImplementedBy(LicensesService.class)
public interface LicensesManagerService {
public void registerLicenses(final PutLicenseRequestHolder requestHolder, final ActionListener<ClusterStateUpdateResponse> listener);

View File

@ -7,8 +7,6 @@ package org.elasticsearch.license.plugin.core;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.cluster.*;
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.metadata.MetaData;
@ -16,19 +14,15 @@ import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.inject.Provides;
import org.elasticsearch.common.inject.Singleton;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicenseBuilders;
import org.elasticsearch.license.manager.ESLicenseManager;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseAction;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseRequest;
import org.elasticsearch.license.plugin.action.put.PutLicenseRequest;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.internal.InternalNode;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
@ -43,20 +37,19 @@ import static org.elasticsearch.license.plugin.core.TrialLicensesBuilder.EMPTY;
* - implement logic in clusterChanged
* - interface with LicenseManager
*/
@Singleton
public class LicensesService extends AbstractLifecycleComponent<LicensesService> implements ClusterStateListener, LicensesManagerService, LicensesValidatorService {
private ESLicenseManager esLicenseManager;
private InternalNode node;
private ClusterService clusterService;
private volatile TrialLicenses trialLicenses = EMPTY;
@Inject
public LicensesService(Settings settings, Node node) {
public LicensesService(Settings settings, ClusterService clusterService) {
super(settings);
this.node = (InternalNode) node;
this.clusterService = clusterService;
}
/**
@ -167,10 +160,8 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
@Override
protected void doStart() throws ElasticsearchException {
clusterService = node.injector().getInstance(ClusterService.class);
esLicenseManager = ESLicenseManager.createClusterStateBasedInstance(clusterService);
if (DiscoveryNode.dataNode(settings) || DiscoveryNode.masterNode(settings)) {
if ( DiscoveryNode.dataNode(settings) || DiscoveryNode.masterNode(settings)) {
clusterService.add(this);
}
}

View File

@ -10,14 +10,9 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseAction;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseRequest;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseResponse;
import org.elasticsearch.license.plugin.action.delete.TransportDeleteLicenseAction;
import org.elasticsearch.license.plugin.action.put.PutLicenseRequest;
import org.elasticsearch.license.plugin.action.put.PutLicenseResponse;
import org.elasticsearch.license.plugin.action.put.TransportPutLicenseAction;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
@ -29,18 +24,13 @@ import java.util.Set;
import static org.elasticsearch.license.core.ESLicenses.FeatureType;
import static org.elasticsearch.rest.RestRequest.Method.DELETE;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;
public class RestDeleteLicenseAction extends BaseRestHandler {
private final TransportDeleteLicenseAction transportDeleteLicenseAction;
@Inject
public RestDeleteLicenseAction(Settings settings, RestController controller, Client client, TransportDeleteLicenseAction transportDeleteLicenseAction) {
public RestDeleteLicenseAction(Settings settings, RestController controller, Client client) {
super(settings, controller, client);
controller.registerHandler(DELETE, "/_cluster/license/{features}", this);
this.transportDeleteLicenseAction = transportDeleteLicenseAction;
}
@ -52,7 +42,7 @@ public class RestDeleteLicenseAction extends BaseRestHandler {
}
DeleteLicenseRequest deleteLicenseRequest = new DeleteLicenseRequest(getFeaturesToDelete(features));
deleteLicenseRequest.listenerThreaded(false);
transportDeleteLicenseAction.execute(deleteLicenseRequest, new AcknowledgedRestListener<DeleteLicenseResponse>(channel));
client.admin().cluster().execute(DeleteLicenseAction.INSTANCE, deleteLicenseRequest, new AcknowledgedRestListener<DeleteLicenseResponse>(channel));
}
private static String[] getFeaturesToDelete(String[] features) {

View File

@ -6,9 +6,6 @@
package org.elasticsearch.license.plugin.rest;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.RepositoriesMetaData;
import org.elasticsearch.cluster.metadata.RepositoryMetaData;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -16,30 +13,24 @@ import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.plugin.action.get.GetLicenseAction;
import org.elasticsearch.license.plugin.action.get.GetLicenseRequest;
import org.elasticsearch.license.plugin.action.get.GetLicenseResponse;
import org.elasticsearch.license.plugin.action.get.TransportGetLicenseAction;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.AcknowledgedRestListener;
import org.elasticsearch.rest.action.support.RestBuilderListener;
import static org.elasticsearch.client.Requests.getRepositoryRequest;
import static org.elasticsearch.license.plugin.action.Utils.licenseAsMap;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.OK;
public class RestGetLicenseAction extends BaseRestHandler {
private final TransportGetLicenseAction transportGetLicenseAction;
@Inject
public RestGetLicenseAction(Settings settings, RestController controller, Client client, TransportGetLicenseAction transportGetLicenseAction) {
public RestGetLicenseAction(Settings settings, RestController controller, Client client) {
super(settings, controller, client);
controller.registerHandler(GET, "/_cluster/license", this);
this.transportGetLicenseAction = transportGetLicenseAction;
}
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
transportGetLicenseAction.execute(new GetLicenseRequest(), new RestBuilderListener<GetLicenseResponse>(channel) {
client.admin().cluster().execute(GetLicenseAction.INSTANCE, new GetLicenseRequest(), new RestBuilderListener<GetLicenseResponse>(channel) {
@Override
public RestResponse buildResponse(GetLicenseResponse getLicenseResponse, XContentBuilder builder) throws Exception {
builder.startObject();

View File

@ -11,7 +11,6 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.plugin.action.put.PutLicenseAction;
import org.elasticsearch.license.plugin.action.put.PutLicenseRequest;
import org.elasticsearch.license.plugin.action.put.PutLicenseResponse;
import org.elasticsearch.license.plugin.action.put.TransportPutLicenseAction;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
@ -23,14 +22,11 @@ import static org.elasticsearch.rest.RestRequest.Method.PUT;
public class RestPutLicenseAction extends BaseRestHandler {
private final TransportPutLicenseAction transportPutLicensesAction;
@Inject
public RestPutLicenseAction(Settings settings, RestController controller, Client client, TransportPutLicenseAction transportPutLicenseAction) {
public RestPutLicenseAction(Settings settings, RestController controller, Client client) {
super(settings, controller, client);
controller.registerHandler(PUT, "/_cluster/license", this);
controller.registerHandler(POST, "/_cluster/license", this);
this.transportPutLicensesAction = transportPutLicenseAction;
}
@ -39,6 +35,6 @@ public class RestPutLicenseAction extends BaseRestHandler {
PutLicenseRequest putLicenseRequest = new PutLicenseRequest();
putLicenseRequest.listenerThreaded(false);
putLicenseRequest.license(request.content().toUtf8());
transportPutLicensesAction.execute(putLicenseRequest, new AcknowledgedRestListener<PutLicenseResponse>(channel));
client.admin().cluster().execute(PutLicenseAction.INSTANCE, putLicenseRequest, new AcknowledgedRestListener<PutLicenseResponse>(channel));
}
}

View File

@ -6,25 +6,20 @@
package org.elasticsearch.license.plugin;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.common.collect.ImmutableSet;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.TestUtils;
import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicenseBuilders;
import org.elasticsearch.license.core.LicenseUtils;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseAction;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseRequest;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseRequestBuilder;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseResponse;
import org.elasticsearch.license.plugin.action.delete.TransportDeleteLicenseAction;
import org.elasticsearch.license.plugin.action.get.GetLicenseRequest;
import org.elasticsearch.license.plugin.action.get.GetLicenseRequestBuilder;
import org.elasticsearch.license.plugin.action.get.GetLicenseResponse;
import org.elasticsearch.license.plugin.action.get.TransportGetLicenseAction;
import org.elasticsearch.license.plugin.action.put.PutLicenseRequest;
import org.elasticsearch.license.plugin.action.put.PutLicenseRequestBuilder;
import org.elasticsearch.license.plugin.action.put.PutLicenseResponse;
import org.elasticsearch.license.plugin.action.put.TransportPutLicenseAction;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.elasticsearch.test.InternalTestCluster;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
@ -36,12 +31,10 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import static org.elasticsearch.test.ElasticsearchIntegrationTest.*;
import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope.*;
import static org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope.SUITE;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
@ClusterScope(scope = SUITE, numDataNodes = 10)
public class LicenseTransportTests extends ElasticsearchIntegrationTest {
@ -57,6 +50,12 @@ public class LicenseTransportTests extends ElasticsearchIntegrationTest {
.build();
}
@Override
protected Settings transportClientSettings() {
// Plugin should be loaded on the transport client as well
return nodeSettings(0);
}
@BeforeClass
public static void setup() throws IOException, URISyntaxException {
priKeyPath = Paths.get(LicenseTransportTests.class.getResource("/org.elasticsearch.license.plugin/test_pri.key").toURI()).toAbsolutePath().toString();
@ -72,11 +71,12 @@ public class LicenseTransportTests extends ElasticsearchIntegrationTest {
@Test
public void testEmptyGetLicense() throws Exception {
final ActionFuture<DeleteLicenseResponse> deleteFuture = licenseDeleteAction().execute(new DeleteLicenseRequest("marvel", "shield"));
DeleteLicenseRequestBuilder deleteLicenseRequestBuilder = new DeleteLicenseRequestBuilder(client().admin().cluster()).setFeatures(ImmutableSet.of("marvel", "shield"));
final ActionFuture<DeleteLicenseResponse> deleteFuture = deleteLicenseRequestBuilder.execute();
final DeleteLicenseResponse deleteLicenseResponse = deleteFuture.get();
assertTrue(deleteLicenseResponse.isAcknowledged());
final ActionFuture<GetLicenseResponse> getLicenseFuture = licenseGetAction().execute(new GetLicenseRequest());
final ActionFuture<GetLicenseResponse> getLicenseFuture = new GetLicenseRequestBuilder(client().admin().cluster()).execute();
final GetLicenseResponse getLicenseResponse = getLicenseFuture.get();
@ -93,20 +93,20 @@ public class LicenseTransportTests extends ElasticsearchIntegrationTest {
String licenseString = TestUtils.generateESLicenses(map);
String licenseOutput = TestUtils.runLicenseGenerationTool(licenseString, pubKeyPath, priKeyPath);
PutLicenseRequest putLicenseRequest = new PutLicenseRequest();
PutLicenseRequestBuilder putLicenseRequestBuilder = new PutLicenseRequestBuilder(client().admin().cluster());
//putLicenseRequest.license(licenseString);
final ESLicenses putLicenses = LicenseUtils.readLicensesFromString(licenseOutput);
putLicenseRequest.license(putLicenses);
putLicenseRequestBuilder.setLicense(putLicenses);
//LicenseUtils.printLicense(putLicenses);
ensureGreen();
final ActionFuture<PutLicenseResponse> putLicenseFuture = licensePutAction().execute(putLicenseRequest);
final ActionFuture<PutLicenseResponse> putLicenseFuture = putLicenseRequestBuilder.execute();
final PutLicenseResponse putLicenseResponse = putLicenseFuture.get();
assertThat(putLicenseResponse.isAcknowledged(), equalTo(true));
ActionFuture<GetLicenseResponse> getLicenseFuture = licenseGetAction().execute(new GetLicenseRequest());
ActionFuture<GetLicenseResponse> getLicenseFuture = new GetLicenseRequestBuilder(client().admin().cluster()).execute();
GetLicenseResponse getLicenseResponse = getLicenseFuture.get();
@ -116,30 +116,15 @@ public class LicenseTransportTests extends ElasticsearchIntegrationTest {
assertTrue(isSame(putLicenses, getLicenseResponse.licenses()));
final ActionFuture<DeleteLicenseResponse> deleteFuture = licenseDeleteAction().execute(new DeleteLicenseRequest("marvel", "shield"));
final ActionFuture<DeleteLicenseResponse> deleteFuture = new DeleteLicenseRequestBuilder(client().admin().cluster())
.setFeatures(ImmutableSet.of("marvel", "shield")).execute();
final DeleteLicenseResponse deleteLicenseResponse = deleteFuture.get();
assertTrue(deleteLicenseResponse.isAcknowledged());
getLicenseResponse = licenseGetAction().execute(new GetLicenseRequest()).get();
getLicenseResponse = new GetLicenseRequestBuilder(client().admin().cluster()).execute().get();
assertTrue(isSame(getLicenseResponse.licenses(), LicenseBuilders.licensesBuilder().build()));
}
public TransportGetLicenseAction licenseGetAction() {
final InternalTestCluster clients = internalCluster();
return clients.getInstance(TransportGetLicenseAction.class);
}
public TransportPutLicenseAction licensePutAction() {
final InternalTestCluster clients = internalCluster();
return clients.getInstance(TransportPutLicenseAction.class);
}
public TransportDeleteLicenseAction licenseDeleteAction() {
final InternalTestCluster clients = internalCluster();
return clients.getInstance(TransportDeleteLicenseAction.class);
}
//TODO: convert to asserts
public static boolean isSame(ESLicenses firstLicenses, ESLicenses secondLicenses) {