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")));
}
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();
}
});
}
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();
}
}
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));
}
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;
}
Aggregations