|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.repositories; |
| 10 | + |
| 11 | +import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesResponse; |
| 12 | +import org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryResponse; |
| 13 | +import org.elasticsearch.cluster.metadata.RepositoryMetadata; |
| 14 | +import org.elasticsearch.cluster.service.ClusterService; |
| 15 | +import org.elasticsearch.common.settings.Setting; |
| 16 | +import org.elasticsearch.common.settings.Settings; |
| 17 | +import org.elasticsearch.common.util.BigArrays; |
| 18 | +import org.elasticsearch.env.Environment; |
| 19 | +import org.elasticsearch.indices.recovery.RecoverySettings; |
| 20 | +import org.elasticsearch.plugins.Plugin; |
| 21 | +import org.elasticsearch.plugins.RepositoryPlugin; |
| 22 | +import org.elasticsearch.snapshots.mockstore.MockRepository; |
| 23 | +import org.elasticsearch.test.ESIntegTestCase; |
| 24 | +import org.elasticsearch.xcontent.NamedXContentRegistry; |
| 25 | + |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 33 | +import static org.hamcrest.Matchers.equalTo; |
| 34 | +import static org.hamcrest.Matchers.hasSize; |
| 35 | +import static org.hamcrest.Matchers.isA; |
| 36 | + |
| 37 | +public class InvalidRepositoryIT extends ESIntegTestCase { |
| 38 | + @Override |
| 39 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 40 | + return Collections.singletonList(UnstableRepository.Plugin.class); |
| 41 | + } |
| 42 | + |
| 43 | + public static class UnstableRepository extends MockRepository { |
| 44 | + public static final String TYPE = "unstable"; |
| 45 | + public static final Setting<List<String>> UNSTABLE_NODES = Setting.stringListSetting( |
| 46 | + "repository.unstable_nodes", |
| 47 | + Setting.Property.NodeScope, |
| 48 | + Setting.Property.Dynamic |
| 49 | + ); |
| 50 | + |
| 51 | + public UnstableRepository( |
| 52 | + RepositoryMetadata metadata, |
| 53 | + Environment environment, |
| 54 | + NamedXContentRegistry namedXContentRegistry, |
| 55 | + ClusterService clusterService, |
| 56 | + BigArrays bigArrays, |
| 57 | + RecoverySettings recoverySettings |
| 58 | + ) { |
| 59 | + super(metadata, environment, namedXContentRegistry, clusterService, bigArrays, recoverySettings); |
| 60 | + List<String> unstableNodes = UNSTABLE_NODES.get(metadata.settings()); |
| 61 | + if (unstableNodes.contains(clusterService.getNodeName())) { |
| 62 | + throw new RepositoryException(metadata.name(), "Failed to create repository: current node is not stable"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public static class Plugin extends org.elasticsearch.plugins.Plugin implements RepositoryPlugin { |
| 67 | + @Override |
| 68 | + public Map<String, Factory> getRepositories( |
| 69 | + Environment env, |
| 70 | + NamedXContentRegistry namedXContentRegistry, |
| 71 | + ClusterService clusterService, |
| 72 | + BigArrays bigArrays, |
| 73 | + RecoverySettings recoverySettings |
| 74 | + ) { |
| 75 | + return Collections.singletonMap( |
| 76 | + TYPE, |
| 77 | + (metadata) -> new UnstableRepository(metadata, env, namedXContentRegistry, clusterService, bigArrays, recoverySettings) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public List<Setting<?>> getSettings() { |
| 83 | + return List.of(UNSTABLE_NODES); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public void testCreateInvalidRepository() throws Exception { |
| 89 | + internalCluster().ensureAtLeastNumDataNodes(2); |
| 90 | + final String repositoryName = "test-duplicate-create-repo"; |
| 91 | + |
| 92 | + // put repository for the first time: only let master node create repository successfully |
| 93 | + createRepository( |
| 94 | + repositoryName, |
| 95 | + UnstableRepository.TYPE, |
| 96 | + Settings.builder() |
| 97 | + .put("location", randomRepoPath()) |
| 98 | + .putList( |
| 99 | + UnstableRepository.UNSTABLE_NODES.getKey(), |
| 100 | + Arrays.stream(internalCluster().getNodeNames()) |
| 101 | + .filter(name -> name.equals(internalCluster().getMasterName()) == false) |
| 102 | + .toList() |
| 103 | + ) |
| 104 | + ); |
| 105 | + // verification should fail with some node has InvalidRepository |
| 106 | + final var expectedException = expectThrows( |
| 107 | + RepositoryVerificationException.class, |
| 108 | + () -> client().admin().cluster().prepareVerifyRepository(repositoryName).get() |
| 109 | + ); |
| 110 | + for (Throwable suppressed : expectedException.getSuppressed()) { |
| 111 | + Throwable outerCause = suppressed.getCause(); |
| 112 | + assertThat(outerCause, isA(RepositoryException.class)); |
| 113 | + assertThat( |
| 114 | + outerCause.getMessage(), |
| 115 | + equalTo("[" + repositoryName + "] repository type [" + UnstableRepository.TYPE + "] failed to create on current node") |
| 116 | + ); |
| 117 | + Throwable innerCause = suppressed.getCause().getCause().getCause(); |
| 118 | + assertThat(innerCause, isA(RepositoryException.class)); |
| 119 | + assertThat( |
| 120 | + innerCause.getMessage(), |
| 121 | + equalTo("[" + repositoryName + "] Failed to create repository: current node is not stable") |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + // restart master |
| 126 | + internalCluster().restartNode(internalCluster().getMasterName()); |
| 127 | + ensureGreen(); |
| 128 | + |
| 129 | + // put repository again: let all node can create repository successfully |
| 130 | + createRepository(repositoryName, UnstableRepository.TYPE, Settings.builder().put("location", randomRepoPath())); |
| 131 | + // verification should succeed with all node create repository successfully |
| 132 | + VerifyRepositoryResponse verifyRepositoryResponse = client().admin().cluster().prepareVerifyRepository(repositoryName).get(); |
| 133 | + assertEquals(verifyRepositoryResponse.getNodes().size(), internalCluster().numDataAndMasterNodes()); |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + private void createRepository(String name, String type, Settings.Builder settings) { |
| 138 | + // create |
| 139 | + assertAcked(client().admin().cluster().preparePutRepository(name).setType(type).setVerify(false).setSettings(settings).get()); |
| 140 | + // get |
| 141 | + final GetRepositoriesResponse updatedGetRepositoriesResponse = client().admin().cluster().prepareGetRepositories(name).get(); |
| 142 | + // assert |
| 143 | + assertThat(updatedGetRepositoriesResponse.repositories(), hasSize(1)); |
| 144 | + final RepositoryMetadata updatedRepositoryMetadata = updatedGetRepositoriesResponse.repositories().get(0); |
| 145 | + assertThat(updatedRepositoryMetadata.type(), equalTo(type)); |
| 146 | + } |
| 147 | +} |
0 commit comments