Search in sources :

Example 16 with RepositoryMetaData

use of org.elasticsearch.cluster.metadata.RepositoryMetaData in project crate by crate.

the class AzureRepositoryAnalyzerTest method prepare.

@Before
public void prepare() {
    RepositoriesMetadata repositoriesMetadata = new RepositoriesMetadata(Collections.singletonList(new RepositoryMetadata("my_repo", "fs", Settings.builder().put("location", "/tmp/my_repo").build())));
    ClusterState clusterState = ClusterState.builder(new ClusterName("testing")).metadata(Metadata.builder().putCustom(RepositoriesMetadata.TYPE, repositoriesMetadata)).build();
    ClusterServiceUtils.setState(clusterService, clusterState);
    e = SQLExecutor.builder(clusterService).build();
    plannerContext = e.getPlannerContext(clusterService.state());
    repositoryParamValidator = new RepositoryParamValidator(Map.of("azure", new TypeSettings(AzureRepository.mandatorySettings(), AzureRepository.optionalSettings())));
}
Also used : RepositoryParamValidator(io.crate.analyze.repositories.RepositoryParamValidator) RepositoriesMetadata(org.elasticsearch.cluster.metadata.RepositoriesMetadata) ClusterState(org.elasticsearch.cluster.ClusterState) RepositoryMetadata(org.elasticsearch.cluster.metadata.RepositoryMetadata) ClusterName(org.elasticsearch.cluster.ClusterName) TypeSettings(io.crate.analyze.repositories.TypeSettings) Before(org.junit.Before)

Example 17 with RepositoryMetaData

use of org.elasticsearch.cluster.metadata.RepositoryMetaData in project crate by crate.

the class MockRepository method overrideSettings.

private static RepositoryMetadata overrideSettings(RepositoryMetadata metadata, Environment environment) {
    // this is super duper hacky
    if (metadata.settings().getAsBoolean("localize_location", false)) {
        Path location = PathUtils.get(metadata.settings().get("location"));
        location = location.resolve(Integer.toString(environment.hashCode()));
        return new RepositoryMetadata(metadata.name(), metadata.type(), Settings.builder().put(metadata.settings()).put("location", location.toAbsolutePath()).build());
    } else {
        return metadata;
    }
}
Also used : BlobPath(org.elasticsearch.common.blobstore.BlobPath) Path(java.nio.file.Path) RepositoryMetadata(org.elasticsearch.cluster.metadata.RepositoryMetadata)

Example 18 with RepositoryMetaData

use of org.elasticsearch.cluster.metadata.RepositoryMetaData in project crate by crate.

the class MockEventuallyConsistentRepositoryTests method testOverwriteShardSnapBlobFails.

public void testOverwriteShardSnapBlobFails() throws IOException {
    MockEventuallyConsistentRepository.Context blobStoreContext = new MockEventuallyConsistentRepository.Context();
    try (BlobStoreRepository repository = new MockEventuallyConsistentRepository(new RepositoryMetadata("testRepo", "mockEventuallyConsistent", Settings.EMPTY), xContentRegistry(), BlobStoreTestUtil.mockClusterService(), blobStoreContext, random())) {
        repository.start();
        final BlobContainer container = repository.blobStore().blobContainer(repository.basePath().add("indices").add("someindex").add("0"));
        final String blobName = BlobStoreRepository.SNAPSHOT_PREFIX + UUIDs.randomBase64UUID();
        final int lengthWritten = randomIntBetween(1, 100);
        final byte[] blobData = randomByteArrayOfLength(lengthWritten);
        container.writeBlob(blobName, new ByteArrayInputStream(blobData), lengthWritten, false);
        final AssertionError assertionError = expectThrows(AssertionError.class, () -> container.writeBlob(blobName, new ByteArrayInputStream(blobData), lengthWritten, false));
        assertThat(assertionError.getMessage(), equalTo("Shard level snap-{uuid} blobs should never be overwritten"));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BlobStoreRepository(org.elasticsearch.repositories.blobstore.BlobStoreRepository) RepositoryMetadata(org.elasticsearch.cluster.metadata.RepositoryMetadata) BlobContainer(org.elasticsearch.common.blobstore.BlobContainer)

Example 19 with RepositoryMetaData

use of org.elasticsearch.cluster.metadata.RepositoryMetaData in project crate by crate.

the class MockEventuallyConsistentRepositoryTests method testOverwriteSnapshotInfoBlob.

public void testOverwriteSnapshotInfoBlob() {
    MockEventuallyConsistentRepository.Context blobStoreContext = new MockEventuallyConsistentRepository.Context();
    final RepositoryMetadata metaData = new RepositoryMetadata("testRepo", "mockEventuallyConsistent", Settings.EMPTY);
    final ClusterService clusterService = BlobStoreTestUtil.mockClusterService(metaData);
    try (BlobStoreRepository repository = new MockEventuallyConsistentRepository(metaData, xContentRegistry(), clusterService, blobStoreContext, random())) {
        clusterService.addStateApplier(event -> repository.updateState(event.state()));
        // Apply state once to initialize repo properly like RepositoriesService would
        repository.updateState(clusterService.state());
        repository.start();
        // We create a snap- blob for snapshot "foo" in the first generation
        final PlainActionFuture<SnapshotInfo> future = PlainActionFuture.newFuture();
        final SnapshotId snapshotId = new SnapshotId("foo", UUIDs.randomBase64UUID());
        // We try to write another snap- blob for "foo" in the next generation. It fails because the content differs.
        repository.finalizeSnapshot(snapshotId, ShardGenerations.EMPTY, 1L, null, 5, Collections.emptyList(), -1L, false, Metadata.EMPTY_METADATA, true, future);
        future.actionGet();
        // We try to write another snap- blob for "foo" in the next generation. It fails because the content differs.
        final AssertionError assertionError = expectThrows(AssertionError.class, () -> {
            final PlainActionFuture<SnapshotInfo> fut = PlainActionFuture.newFuture();
            repository.finalizeSnapshot(snapshotId, ShardGenerations.EMPTY, 1L, null, 6, Collections.emptyList(), 0, false, Metadata.EMPTY_METADATA, true, fut);
            fut.actionGet();
        });
        assertThat(assertionError.getMessage(), equalTo("\nExpected: <6>\n     but: was <5>"));
        // We try to write yet another snap- blob for "foo" in the next generation.
        // It passes cleanly because the content of the blob except for the timestamps.
        final PlainActionFuture<SnapshotInfo> future2 = PlainActionFuture.newFuture();
        repository.finalizeSnapshot(snapshotId, ShardGenerations.EMPTY, 1L, null, 5, Collections.emptyList(), 0, false, Metadata.EMPTY_METADATA, true, future2);
        future2.actionGet();
    }
}
Also used : SnapshotId(org.elasticsearch.snapshots.SnapshotId) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) ClusterService(org.elasticsearch.cluster.service.ClusterService) RepositoryMetadata(org.elasticsearch.cluster.metadata.RepositoryMetadata) BlobStoreRepository(org.elasticsearch.repositories.blobstore.BlobStoreRepository)

Example 20 with RepositoryMetaData

use of org.elasticsearch.cluster.metadata.RepositoryMetaData 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)

Aggregations

RepositoryMetadata (org.elasticsearch.cluster.metadata.RepositoryMetadata)29 RepositoryMetaData (org.elasticsearch.cluster.metadata.RepositoryMetaData)24 ClusterState (org.elasticsearch.cluster.ClusterState)13 RepositoriesMetadata (org.elasticsearch.cluster.metadata.RepositoriesMetadata)13 ArrayList (java.util.ArrayList)11 Settings (org.elasticsearch.common.settings.Settings)11 RepositoriesMetaData (org.elasticsearch.cluster.metadata.RepositoriesMetaData)10 IOException (java.io.IOException)8 ClusterName (org.elasticsearch.cluster.ClusterName)8 Before (org.junit.Before)7 HashMap (java.util.HashMap)6 BlobStoreRepository (org.elasticsearch.repositories.blobstore.BlobStoreRepository)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)5 ActionListener (org.elasticsearch.action.ActionListener)5 BlobContainer (org.elasticsearch.common.blobstore.BlobContainer)5 RepositoryException (org.elasticsearch.repositories.RepositoryException)5 Test (org.junit.Test)5 List (java.util.List)4 Map (java.util.Map)4