Search in sources :

Example 1 with PutRepositoryRequest

use of org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest in project crate by crate.

the class AzureRepositoryAnalyzerTest method testCreateAzureRepositoryWithAllSettings.

@Test
public void testCreateAzureRepositoryWithAllSettings() {
    PutRepositoryRequest request = analyze(e, "CREATE REPOSITORY foo TYPE azure WITH (" + "   container='test_container'," + "   base_path='test_path'," + "   chunk_size='12mb'," + "   compress=true," + "   readonly=true," + "   location_mode='primary_only'," + "   account='test_account'," + "   key='test_key'," + "   max_retries=3," + "   endpoint_suffix='.com'," + "   timeout='30s'," + "   proxy_port=0," + "   proxy_type='socks'," + "   proxy_host='localhost')");
    assertThat(request.name(), is("foo"));
    assertThat(request.type(), is("azure"));
    assertThat(request.settings().getAsStructuredMap(), Matchers.allOf(Matchers.hasEntry("container", "test_container"), Matchers.hasEntry("base_path", "test_path"), Matchers.hasEntry("chunk_size", "12mb"), Matchers.hasEntry("compress", "true"), Matchers.hasEntry("readonly", "true"), Matchers.hasEntry("account", "test_account"), Matchers.hasEntry("key", "test_key"), Matchers.hasEntry("max_retries", "3"), Matchers.hasEntry("endpoint_suffix", ".com"), Matchers.hasEntry("timeout", "30s"), Matchers.hasEntry("proxy_port", "0"), Matchers.hasEntry("proxy_type", "socks"), Matchers.hasEntry("proxy_host", "localhost")));
}
Also used : PutRepositoryRequest(org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 2 with PutRepositoryRequest

use of org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest in project crate by crate.

the class RepositoriesService method registerRepository.

/**
 * Registers new repository in the cluster
 * <p>
 * This method can be only called on the master node. It tries to create a new repository on the master
 * and if it was successful it adds new repository to cluster metadata.
 *
 * @param request  register repository request
 * @param listener register repository listener
 */
public void registerRepository(final PutRepositoryRequest request, final ActionListener<ClusterStateUpdateResponse> listener) {
    final RepositoryMetadata newRepositoryMetadata = new RepositoryMetadata(request.name(), request.type(), request.settings());
    final ActionListener<ClusterStateUpdateResponse> registrationListener;
    if (request.verify()) {
        registrationListener = ActionListener.delegateFailure(listener, (delegatedListener, clusterStateUpdateResponse) -> {
            if (clusterStateUpdateResponse.isAcknowledged()) {
                // The response was acknowledged - all nodes should know about the new repository, let's verify them
                verifyRepository(request.name(), ActionListener.delegateFailure(delegatedListener, (innerDelegatedListener, discoveryNodes) -> innerDelegatedListener.onResponse(clusterStateUpdateResponse)));
            } else {
                delegatedListener.onResponse(clusterStateUpdateResponse);
            }
        });
    } else {
        registrationListener = listener;
    }
    clusterService.submitStateUpdateTask("put_repository [" + request.name() + "]", new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(request, registrationListener) {

        @Override
        protected ClusterStateUpdateResponse newResponse(boolean acknowledged) {
            return new ClusterStateUpdateResponse(acknowledged);
        }

        @Override
        public ClusterState execute(ClusterState currentState) throws IOException {
            ensureRepositoryNotInUse(currentState, request.name());
            // Trying to create the new repository on master to make sure it works
            if (!registerRepository(newRepositoryMetadata)) {
                // The new repository has the same settings as the old one - ignore
                return currentState;
            }
            Metadata metadata = currentState.metadata();
            Metadata.Builder mdBuilder = Metadata.builder(currentState.metadata());
            RepositoriesMetadata repositories = metadata.custom(RepositoriesMetadata.TYPE);
            if (repositories == null) {
                LOGGER.info("put repository [{}]", request.name());
                repositories = new RepositoriesMetadata(Collections.singletonList(new RepositoryMetadata(request.name(), request.type(), request.settings())));
            } else {
                boolean found = false;
                List<RepositoryMetadata> repositoriesMetadata = new ArrayList<>(repositories.repositories().size() + 1);
                for (RepositoryMetadata repositoryMetadata : repositories.repositories()) {
                    if (repositoryMetadata.name().equals(newRepositoryMetadata.name())) {
                        if (newRepositoryMetadata.equalsIgnoreGenerations(repositoryMetadata)) {
                            // Previous version is the same as this one no update is needed.
                            return currentState;
                        }
                        found = true;
                        repositoriesMetadata.add(newRepositoryMetadata);
                    } else {
                        repositoriesMetadata.add(repositoryMetadata);
                    }
                }
                if (!found) {
                    LOGGER.info("put repository [{}]", request.name());
                    repositoriesMetadata.add(new RepositoryMetadata(request.name(), request.type(), request.settings()));
                } else {
                    LOGGER.info("update repository [{}]", request.name());
                }
                repositories = new RepositoriesMetadata(repositoriesMetadata);
            }
            mdBuilder.putCustom(RepositoriesMetadata.TYPE, repositories);
            return ClusterState.builder(currentState).metadata(mdBuilder).build();
        }

        @Override
        public void onFailure(String source, Exception e) {
            LOGGER.warn(() -> new ParameterizedMessage("failed to create repository [{}]", request.name()), e);
            super.onFailure(source, e);
        }

        @Override
        public boolean mustAck(DiscoveryNode discoveryNode) {
            // repository is created on both master and data nodes
            return discoveryNode.isMasterEligibleNode() || discoveryNode.isDataNode();
        }
    });
}
Also used : DeleteRepositoryRequest(org.elasticsearch.action.admin.cluster.repositories.delete.DeleteRepositoryRequest) AckedClusterStateUpdateTask(org.elasticsearch.cluster.AckedClusterStateUpdateTask) ClusterService(org.elasticsearch.cluster.service.ClusterService) HashMap(java.util.HashMap) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ArrayList(java.util.ArrayList) RepositoryMetadata(org.elasticsearch.cluster.metadata.RepositoryMetadata) PutRepositoryRequest(org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest) ClusterState(org.elasticsearch.cluster.ClusterState) Metadata(org.elasticsearch.cluster.metadata.Metadata) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) RepositoriesMetadata(org.elasticsearch.cluster.metadata.RepositoriesMetadata) Settings(org.elasticsearch.common.settings.Settings) Map(java.util.Map) Regex(org.elasticsearch.common.regex.Regex) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportService(org.elasticsearch.transport.TransportService) ActionRunnable(org.elasticsearch.action.ActionRunnable) ClusterStateApplier(org.elasticsearch.cluster.ClusterStateApplier) SnapshotsService(org.elasticsearch.snapshots.SnapshotsService) Collection(java.util.Collection) IOUtils(io.crate.common.io.IOUtils) IOException(java.io.IOException) RestoreService(org.elasticsearch.snapshots.RestoreService) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) ActionListener(org.elasticsearch.action.ActionListener) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) RepositoryMetadata(org.elasticsearch.cluster.metadata.RepositoryMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) RepositoriesMetadata(org.elasticsearch.cluster.metadata.RepositoriesMetadata) IOException(java.io.IOException) IOException(java.io.IOException) RepositoriesMetadata(org.elasticsearch.cluster.metadata.RepositoriesMetadata) RepositoryMetadata(org.elasticsearch.cluster.metadata.RepositoryMetadata) ArrayList(java.util.ArrayList) List(java.util.List) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse)

Example 3 with PutRepositoryRequest

use of org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest in project crate by crate.

the class RepositoryServiceTest method testRepositoryIsDroppedOnFailure.

@Test
public void testRepositoryIsDroppedOnFailure() throws Throwable {
    expectedException.expect(RepositoryException.class);
    // add repo to cluster service so that it exists..
    RepositoriesMetaData repos = new RepositoriesMetaData(new RepositoryMetaData("repo1", "fs", Settings.EMPTY));
    ClusterState state = ClusterState.builder(new ClusterName("dummy")).metaData(MetaData.builder().putCustom(RepositoriesMetaData.TYPE, repos)).build();
    ClusterService clusterService = new NoopClusterService(state);
    final ActionFilters actionFilters = mock(ActionFilters.class, Answers.RETURNS_MOCKS.get());
    IndexNameExpressionResolver indexNameExpressionResolver = new IndexNameExpressionResolver(Settings.EMPTY);
    final AtomicBoolean deleteRepoCalled = new AtomicBoolean(false);
    TransportDeleteRepositoryAction deleteRepositoryAction = new TransportDeleteRepositoryAction(Settings.EMPTY, mock(TransportService.class, Answers.RETURNS_MOCKS.get()), clusterService, mock(RepositoriesService.class), threadPool, actionFilters, indexNameExpressionResolver) {

        @Override
        protected void doExecute(Task task, DeleteRepositoryRequest request, ActionListener<DeleteRepositoryResponse> listener) {
            deleteRepoCalled.set(true);
            listener.onResponse(mock(DeleteRepositoryResponse.class));
        }
    };
    TransportPutRepositoryAction putRepo = new TransportPutRepositoryAction(Settings.EMPTY, mock(TransportService.class, Answers.RETURNS_MOCKS.get()), clusterService, mock(RepositoriesService.class), threadPool, actionFilters, indexNameExpressionResolver) {

        @Override
        protected void doExecute(Task task, PutRepositoryRequest request, ActionListener<PutRepositoryResponse> listener) {
            listener.onFailure(new RepositoryException(request.name(), "failure"));
        }
    };
    RepositoryService repositoryService = new RepositoryService(clusterService, deleteRepositoryAction, putRepo);
    try {
        repositoryService.execute(new CreateRepositoryAnalyzedStatement("repo1", "fs", Settings.EMPTY)).get(10, TimeUnit.SECONDS);
    } catch (ExecutionException e) {
        assertThat(deleteRepoCalled.get(), is(true));
        throw e.getCause();
    }
}
Also used : RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) ClusterState(org.elasticsearch.cluster.ClusterState) Task(org.elasticsearch.tasks.Task) TransportDeleteRepositoryAction(org.elasticsearch.action.admin.cluster.repositories.delete.TransportDeleteRepositoryAction) DeleteRepositoryRequest(org.elasticsearch.action.admin.cluster.repositories.delete.DeleteRepositoryRequest) RepositoryException(org.elasticsearch.repositories.RepositoryException) ActionFilters(org.elasticsearch.action.support.ActionFilters) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CreateRepositoryAnalyzedStatement(io.crate.analyze.CreateRepositoryAnalyzedStatement) NoopClusterService(org.elasticsearch.test.cluster.NoopClusterService) ClusterService(org.elasticsearch.cluster.ClusterService) DeleteRepositoryResponse(org.elasticsearch.action.admin.cluster.repositories.delete.DeleteRepositoryResponse) ActionListener(org.elasticsearch.action.ActionListener) TransportService(org.elasticsearch.transport.TransportService) RepositoriesService(org.elasticsearch.repositories.RepositoriesService) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData) ClusterName(org.elasticsearch.cluster.ClusterName) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) TransportPutRepositoryAction(org.elasticsearch.action.admin.cluster.repositories.put.TransportPutRepositoryAction) ExecutionException(java.util.concurrent.ExecutionException) NoopClusterService(org.elasticsearch.test.cluster.NoopClusterService) PutRepositoryRequest(org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 4 with PutRepositoryRequest

use of org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest in project elasticsearch by elastic.

the class RestPutRepositoryAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    PutRepositoryRequest putRepositoryRequest = putRepositoryRequest(request.param("repository"));
    try (XContentParser parser = request.contentParser()) {
        putRepositoryRequest.source(parser.mapOrdered());
    }
    putRepositoryRequest.verify(request.paramAsBoolean("verify", true));
    putRepositoryRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putRepositoryRequest.masterNodeTimeout()));
    putRepositoryRequest.timeout(request.paramAsTime("timeout", putRepositoryRequest.timeout()));
    return channel -> client.admin().cluster().putRepository(putRepositoryRequest, new AcknowledgedRestListener<>(channel));
}
Also used : XContentParser(org.elasticsearch.common.xcontent.XContentParser) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) PutRepositoryRequest(org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) Requests.putRepositoryRequest(org.elasticsearch.client.Requests.putRepositoryRequest) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) PUT(org.elasticsearch.rest.RestRequest.Method.PUT) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) PutRepositoryRequest(org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 5 with PutRepositoryRequest

use of org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest in project crate by crate.

the class RepositoryService method execute.

public CompletableFuture<Long> execute(CreateRepositoryAnalyzedStatement statement) {
    final CompletableFuture<Long> result = new CompletableFuture<>();
    final String repoName = statement.repositoryName();
    PutRepositoryRequest request = new PutRepositoryRequest(repoName);
    request.type(statement.repositoryType());
    request.settings(statement.settings());
    putRepositoryAction.execute(request, new ActionListener<PutRepositoryResponse>() {

        @Override
        public void onResponse(PutRepositoryResponse putRepositoryResponse) {
            result.complete(1L);
        }

        @Override
        public void onFailure(Throwable e) {
            final Throwable t = convertRepositoryException(e);
            // in case the put repo action fails in the verificationPhase the repository got already created
            // but an exception is raised anyway.
            // --> remove the repo and then return the exception to the user
            dropIfExists(repoName, new Runnable() {

                @Override
                public void run() {
                    result.completeExceptionally(t);
                }
            });
        }
    });
    return result;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PutRepositoryResponse(org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse) PutRepositoryRequest(org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest)

Aggregations

PutRepositoryRequest (org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest)10 Test (org.junit.Test)5 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)4 ActionListener (org.elasticsearch.action.ActionListener)3 DeleteRepositoryRequest (org.elasticsearch.action.admin.cluster.repositories.delete.DeleteRepositoryRequest)3 ClusterState (org.elasticsearch.cluster.ClusterState)3 AnalyzedCreateRepository (io.crate.analyze.AnalyzedCreateRepository)2 GenericPropertiesConverter.genericPropertiesToSettings (io.crate.analyze.GenericPropertiesConverter.genericPropertiesToSettings)2 SymbolEvaluator (io.crate.analyze.SymbolEvaluator)2 RepositoryParamValidator (io.crate.analyze.repositories.RepositoryParamValidator)2 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)2 Row (io.crate.data.Row)2 Row1 (io.crate.data.Row1)2 RowConsumer (io.crate.data.RowConsumer)2 OneRowActionListener (io.crate.execution.support.OneRowActionListener)2 Symbol (io.crate.expression.symbol.Symbol)2 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)2 NodeContext (io.crate.metadata.NodeContext)2 DependencyCarrier (io.crate.planner.DependencyCarrier)2 Plan (io.crate.planner.Plan)2