Search in sources :

Example 41 with RepositoryMetadata

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

the class SnapshotRestoreAnalyzerTest method prepare.

@Before
public void prepare() throws IOException {
    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).addTable(USER_TABLE_DEFINITION).addTable(TEST_DOC_LOCATIONS_TABLE_DEFINITION).addPartitionedTable(TEST_PARTITIONED_TABLE_DEFINITION, TEST_PARTITIONED_TABLE_PARTITIONS).addBlobTable("create blob table my_blobs").build();
    plannerContext = e.getPlannerContext(clusterService.state());
}
Also used : RepositoriesMetadata(org.elasticsearch.cluster.metadata.RepositoriesMetadata) ClusterState(org.elasticsearch.cluster.ClusterState) RepositoryMetadata(org.elasticsearch.cluster.metadata.RepositoryMetadata) ClusterName(org.elasticsearch.cluster.ClusterName) Before(org.junit.Before)

Example 42 with RepositoryMetadata

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

the class BlobStoreRepository method writeIndexGen.

/**
 * Writing a new index generation is a three step process.
 * First, the {@link RepositoryMetadata} entry for this repository is set into a pending state by incrementing its
 * pending generation {@code P} while its safe generation {@code N} remains unchanged.
 * Second, the updated {@link RepositoryData} is written to generation {@code P + 1}.
 * Lastly, the {@link RepositoryMetadata} entry for this repository is updated to the new generation {@code P + 1} and thus
 * pending and safe generation are set to the same value marking the end of the update of the repository data.
 *
 * @param repositoryData RepositoryData to write
 * @param expectedGen    expected repository generation at the start of the operation
 * @param writeShardGens whether to write {@link ShardGenerations} to the new {@link RepositoryData} blob
 * @param listener       completion listener
 */
protected void writeIndexGen(RepositoryData repositoryData, long expectedGen, boolean writeShardGens, ActionListener<Void> listener) {
    // can not write to a read only repository
    assert isReadOnly() == false;
    final long currentGen = repositoryData.getGenId();
    if (currentGen != expectedGen) {
        // the index file was updated by a concurrent operation, so we were operating on stale
        // repository data
        listener.onFailure(new RepositoryException(metadata.name(), "concurrent modification of the index-N file, expected current generation [" + expectedGen + "], actual current generation [" + currentGen + "]"));
        return;
    }
    // Step 1: Set repository generation state to the next possible pending generation
    final StepListener<Long> setPendingStep = new StepListener<>();
    clusterService.submitStateUpdateTask("set pending repository generation [" + metadata.name() + "][" + expectedGen + "]", new ClusterStateUpdateTask() {

        private long newGen;

        @Override
        public ClusterState execute(ClusterState currentState) {
            final RepositoryMetadata meta = getRepoMetadata(currentState);
            final String repoName = metadata.name();
            final long genInState = meta.generation();
            final boolean uninitializedMeta = meta.generation() == RepositoryData.UNKNOWN_REPO_GEN || bestEffortConsistency;
            if (uninitializedMeta == false && meta.pendingGeneration() != genInState) {
                LOGGER.info("Trying to write new repository data over unfinished write, repo [{}] is at " + "safe generation [{}] and pending generation [{}]", meta.name(), genInState, meta.pendingGeneration());
            }
            assert expectedGen == RepositoryData.EMPTY_REPO_GEN || uninitializedMeta || expectedGen == meta.generation() : "Expected non-empty generation [" + expectedGen + "] does not match generation tracked in [" + meta + "]";
            // If we run into the empty repo generation for the expected gen, the repo is assumed to have been cleared of
            // all contents by an external process so we reset the safe generation to the empty generation.
            final long safeGeneration = expectedGen == RepositoryData.EMPTY_REPO_GEN ? RepositoryData.EMPTY_REPO_GEN : (uninitializedMeta ? expectedGen : genInState);
            // Regardless of whether or not the safe generation has been reset, the pending generation always increments so that
            // even if a repository has been manually cleared of all contents we will never reuse the same repository generation.
            // This is motivated by the consistency behavior the S3 based blob repository implementation has to support which does
            // not offer any consistency guarantees when it comes to overwriting the same blob name with different content.
            final long nextPendingGen = metadata.pendingGeneration() + 1;
            newGen = uninitializedMeta ? Math.max(expectedGen + 1, nextPendingGen) : nextPendingGen;
            assert newGen > latestKnownRepoGen.get() : "Attempted new generation [" + newGen + "] must be larger than latest known generation [" + latestKnownRepoGen.get() + "]";
            return ClusterState.builder(currentState).metadata(Metadata.builder(currentState.getMetadata()).putCustom(RepositoriesMetadata.TYPE, currentState.metadata().<RepositoriesMetadata>custom(RepositoriesMetadata.TYPE).withUpdatedGeneration(repoName, safeGeneration, newGen)).build()).build();
        }

        @Override
        public void onFailure(String source, Exception e) {
            listener.onFailure(new RepositoryException(metadata.name(), "Failed to execute cluster state update [" + source + "]", e));
        }

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            setPendingStep.onResponse(newGen);
        }
    });
    final StepListener<RepositoryData> filterRepositoryDataStep = new StepListener<>();
    // Step 2: Write new index-N blob to repository and update index.latest
    setPendingStep.whenComplete(newGen -> threadPool().executor(ThreadPool.Names.SNAPSHOT).execute(ActionRunnable.wrap(listener, l -> {
        // BwC logic: Load snapshot version information if any snapshot is missing a version in RepositoryData so that the new
        // RepositoryData contains a version for every snapshot
        final List<SnapshotId> snapshotIdsWithoutVersion = repositoryData.getSnapshotIds().stream().filter(snapshotId -> repositoryData.getVersion(snapshotId) == null).collect(Collectors.toList());
        if (snapshotIdsWithoutVersion.isEmpty() == false) {
            final Map<SnapshotId, Version> updatedVersionMap = new ConcurrentHashMap<>();
            final GroupedActionListener<Void> loadAllVersionsListener = new GroupedActionListener<>(ActionListener.runAfter(new ActionListener<Collection<Void>>() {

                @Override
                public void onResponse(Collection<Void> voids) {
                    LOGGER.info("Successfully loaded all snapshot's version information for {} from snapshot metadata", AllocationService.firstListElementsToCommaDelimitedString(snapshotIdsWithoutVersion, SnapshotId::toString, LOGGER.isDebugEnabled()));
                }

                @Override
                public void onFailure(Exception e) {
                    LOGGER.warn("Failure when trying to load missing version information from snapshot metadata", e);
                }
            }, () -> filterRepositoryDataStep.onResponse(repositoryData.withVersions(updatedVersionMap))), snapshotIdsWithoutVersion.size());
            for (SnapshotId snapshotId : snapshotIdsWithoutVersion) {
                threadPool().executor(ThreadPool.Names.SNAPSHOT).execute(ActionRunnable.run(loadAllVersionsListener, () -> {
                    ActionListener<SnapshotInfo> snapshotInfoListener = ActionListener.delegateFailure(loadAllVersionsListener, (delegate, snapshotInfo) -> {
                        updatedVersionMap.put(snapshotId, snapshotInfo.version());
                        delegate.onResponse(null);
                    });
                    getSnapshotInfo(snapshotId, snapshotInfoListener);
                }));
            }
        } else {
            filterRepositoryDataStep.onResponse(repositoryData);
        }
    })), listener::onFailure);
    filterRepositoryDataStep.whenComplete(filteredRepositoryData -> {
        final long newGen = setPendingStep.result();
        if (latestKnownRepoGen.get() >= newGen) {
            throw new IllegalArgumentException("Tried writing generation [" + newGen + "] but repository is at least at generation [" + latestKnownRepoGen.get() + "] already");
        }
        // write the index file
        final String indexBlob = INDEX_FILE_PREFIX + Long.toString(newGen);
        LOGGER.debug("Repository [{}] writing new index generational blob [{}]", metadata.name(), indexBlob);
        writeAtomic(indexBlob, BytesReference.bytes(filteredRepositoryData.snapshotsToXContent(XContentFactory.jsonBuilder(), writeShardGens)), true);
        // write the current generation to the index-latest file
        final BytesReference genBytes;
        try (BytesStreamOutput bStream = new BytesStreamOutput()) {
            bStream.writeLong(newGen);
            genBytes = bStream.bytes();
        }
        LOGGER.debug("Repository [{}] updating index.latest with generation [{}]", metadata.name(), newGen);
        writeAtomic(INDEX_LATEST_BLOB, genBytes, false);
        // Step 3: Update CS to reflect new repository generation.
        clusterService.submitStateUpdateTask("set safe repository generation [" + metadata.name() + "][" + newGen + "]", new ClusterStateUpdateTask() {

            @Override
            public ClusterState execute(ClusterState currentState) {
                final RepositoryMetadata meta = getRepoMetadata(currentState);
                if (meta.generation() != expectedGen) {
                    throw new IllegalStateException("Tried to update repo generation to [" + newGen + "] but saw unexpected generation in state [" + meta + "]");
                }
                if (meta.pendingGeneration() != newGen) {
                    throw new IllegalStateException("Tried to update from unexpected pending repo generation [" + meta.pendingGeneration() + "] after write to generation [" + newGen + "]");
                }
                return ClusterState.builder(currentState).metadata(Metadata.builder(currentState.getMetadata()).putCustom(RepositoriesMetadata.TYPE, currentState.metadata().<RepositoriesMetadata>custom(RepositoriesMetadata.TYPE).withUpdatedGeneration(metadata.name(), newGen, newGen)).build()).build();
            }

            @Override
            public void onFailure(String source, Exception e) {
                listener.onFailure(new RepositoryException(metadata.name(), "Failed to execute cluster state update [" + source + "]", e));
            }

            @Override
            public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                threadPool.executor(ThreadPool.Names.SNAPSHOT).execute(ActionRunnable.run(listener, () -> {
                    // Delete all now outdated index files up to 1000 blobs back from the new generation.
                    // If there are more than 1000 dangling index-N cleanup functionality on repo delete will take care of them.
                    // Deleting one older than the current expectedGen is done for BwC reasons as older versions used to keep
                    // two index-N blobs around.
                    final List<String> oldIndexN = LongStream.range(Math.max(Math.max(expectedGen - 1, 0), newGen - 1000), newGen).mapToObj(gen -> INDEX_FILE_PREFIX + gen).collect(Collectors.toList());
                    try {
                        blobContainer().deleteBlobsIgnoringIfNotExists(oldIndexN);
                    } catch (IOException e) {
                        LOGGER.warn("Failed to clean up old index blobs {}", oldIndexN);
                    }
                }));
            }
        });
    }, listener::onFailure);
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) SnapshotFiles(org.elasticsearch.index.snapshots.blobstore.SnapshotFiles) IndexShardSnapshotFailedException(org.elasticsearch.index.snapshots.IndexShardSnapshotFailedException) ByteSizeUnit(org.elasticsearch.common.unit.ByteSizeUnit) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) Map(java.util.Map) BlobContainer(org.elasticsearch.common.blobstore.BlobContainer) RateLimitingInputStream(org.elasticsearch.index.snapshots.blobstore.RateLimitingInputStream) IOContext(org.apache.lucene.store.IOContext) InvalidArgumentException(io.crate.exceptions.InvalidArgumentException) SnapshotDeletionsInProgress(org.elasticsearch.cluster.SnapshotDeletionsInProgress) UUIDs(org.elasticsearch.common.UUIDs) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) StandardCharsets(java.nio.charset.StandardCharsets) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) InputStreamIndexInput(org.elasticsearch.common.lucene.store.InputStreamIndexInput) BlobStore(org.elasticsearch.common.blobstore.BlobStore) SnapshotException(org.elasticsearch.snapshots.SnapshotException) FileInfo.canonicalName(org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot.FileInfo.canonicalName) IndexCommit(org.apache.lucene.index.IndexCommit) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) SnapshotId(org.elasticsearch.snapshots.SnapshotId) Tuple(io.crate.common.collections.Tuple) ShardGenerations(org.elasticsearch.repositories.ShardGenerations) ClusterService(org.elasticsearch.cluster.service.ClusterService) SnapshotShardFailure(org.elasticsearch.snapshots.SnapshotShardFailure) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) LoggingDeprecationHandler(org.elasticsearch.common.xcontent.LoggingDeprecationHandler) ArrayList(java.util.ArrayList) BytesArray(org.elasticsearch.common.bytes.BytesArray) Metadata(org.elasticsearch.cluster.metadata.Metadata) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Store(org.elasticsearch.index.store.Store) Nullable(javax.annotation.Nullable) LongStream(java.util.stream.LongStream) IndexInput(org.apache.lucene.store.IndexInput) SetOnce(org.apache.lucene.util.SetOnce) Executor(java.util.concurrent.Executor) IOException(java.io.IOException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) AtomicLong(java.util.concurrent.atomic.AtomicLong) CounterMetric(org.elasticsearch.common.metrics.CounterMetric) ActionListener(org.elasticsearch.action.ActionListener) FsBlobContainer(org.elasticsearch.common.blobstore.fs.FsBlobContainer) SnapshotMissingException(org.elasticsearch.snapshots.SnapshotMissingException) NoSuchFileException(java.nio.file.NoSuchFileException) ConcurrentSnapshotExecutionException(org.elasticsearch.snapshots.ConcurrentSnapshotExecutionException) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) StoreFileMetadata(org.elasticsearch.index.store.StoreFileMetadata) RepositoryMetadata(org.elasticsearch.cluster.metadata.RepositoryMetadata) Settings(org.elasticsearch.common.settings.Settings) Locale(java.util.Locale) Streams(org.elasticsearch.common.io.Streams) ThreadPool(org.elasticsearch.threadpool.ThreadPool) IndexShardRestoreFailedException(org.elasticsearch.index.snapshots.IndexShardRestoreFailedException) ActionRunnable(org.elasticsearch.action.ActionRunnable) StepListener(org.elasticsearch.action.StepListener) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) RepositoryException(org.elasticsearch.repositories.RepositoryException) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) NotXContentException(org.elasticsearch.common.compress.NotXContentException) Setting(org.elasticsearch.common.settings.Setting) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BlobMetadata(org.elasticsearch.common.blobstore.BlobMetadata) BytesReference(org.elasticsearch.common.bytes.BytesReference) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) IndexShardSnapshotException(org.elasticsearch.index.snapshots.IndexShardSnapshotException) MapperService(org.elasticsearch.index.mapper.MapperService) List(java.util.List) BlobStoreIndexShardSnapshot(org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot) Version(org.elasticsearch.Version) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState) RepositoryData(org.elasticsearch.repositories.RepositoryData) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) XContentType(org.elasticsearch.common.xcontent.XContentType) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) Index(org.elasticsearch.index.Index) Lucene(org.elasticsearch.common.lucene.Lucene) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) IndexId(org.elasticsearch.repositories.IndexId) FilterInputStream(java.io.FilterInputStream) RepositoriesMetadata(org.elasticsearch.cluster.metadata.RepositoriesMetadata) RepositoryVerificationException(org.elasticsearch.repositories.RepositoryVerificationException) BlobPath(org.elasticsearch.common.blobstore.BlobPath) IndexOutput(org.apache.lucene.store.IndexOutput) Numbers(org.elasticsearch.common.Numbers) Repository(org.elasticsearch.repositories.Repository) SnapshotsService(org.elasticsearch.snapshots.SnapshotsService) GroupedActionListener(org.elasticsearch.action.support.GroupedActionListener) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) AbstractLifecycleComponent(org.elasticsearch.common.component.AbstractLifecycleComponent) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ExceptionsHelper(org.elasticsearch.ExceptionsHelper) SlicedInputStream(org.elasticsearch.index.snapshots.blobstore.SlicedInputStream) SnapshotsInProgress(org.elasticsearch.cluster.SnapshotsInProgress) BlobStoreIndexShardSnapshots(org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshots) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) RepositoryOperation(org.elasticsearch.repositories.RepositoryOperation) Snapshot(org.elasticsearch.snapshots.Snapshot) RateLimiter(org.apache.lucene.store.RateLimiter) InputStream(java.io.InputStream) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) RepositoriesMetadata(org.elasticsearch.cluster.metadata.RepositoriesMetadata) GroupedActionListener(org.elasticsearch.action.support.GroupedActionListener) Version(org.elasticsearch.Version) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BytesReference(org.elasticsearch.common.bytes.BytesReference) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) RepositoryException(org.elasticsearch.repositories.RepositoryException) IOException(java.io.IOException) IndexShardSnapshotFailedException(org.elasticsearch.index.snapshots.IndexShardSnapshotFailedException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) InvalidArgumentException(io.crate.exceptions.InvalidArgumentException) SnapshotException(org.elasticsearch.snapshots.SnapshotException) IOException(java.io.IOException) SnapshotMissingException(org.elasticsearch.snapshots.SnapshotMissingException) NoSuchFileException(java.nio.file.NoSuchFileException) ConcurrentSnapshotExecutionException(org.elasticsearch.snapshots.ConcurrentSnapshotExecutionException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) IndexShardRestoreFailedException(org.elasticsearch.index.snapshots.IndexShardRestoreFailedException) RepositoryException(org.elasticsearch.repositories.RepositoryException) NotXContentException(org.elasticsearch.common.compress.NotXContentException) IndexShardSnapshotException(org.elasticsearch.index.snapshots.IndexShardSnapshotException) RepositoryVerificationException(org.elasticsearch.repositories.RepositoryVerificationException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) RepositoryData(org.elasticsearch.repositories.RepositoryData) SnapshotId(org.elasticsearch.snapshots.SnapshotId) ActionListener(org.elasticsearch.action.ActionListener) GroupedActionListener(org.elasticsearch.action.support.GroupedActionListener) RepositoryMetadata(org.elasticsearch.cluster.metadata.RepositoryMetadata) AtomicLong(java.util.concurrent.atomic.AtomicLong) Collection(java.util.Collection) StepListener(org.elasticsearch.action.StepListener)

Example 43 with RepositoryMetadata

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

the class RepositoriesService method applyClusterState.

/**
 * Checks if new repositories appeared in or disappeared from cluster metadata and updates current list of
 * repositories accordingly.
 *
 * @param event cluster changed event
 */
@Override
public void applyClusterState(ClusterChangedEvent event) {
    try {
        final ClusterState state = event.state();
        RepositoriesMetadata oldMetadata = event.previousState().getMetadata().custom(RepositoriesMetadata.TYPE);
        RepositoriesMetadata newMetadata = state.getMetadata().custom(RepositoriesMetadata.TYPE);
        // Check if repositories got changed
        if ((oldMetadata == null && newMetadata == null) || (oldMetadata != null && oldMetadata.equalsIgnoreGenerations(newMetadata))) {
            for (Repository repo : repositories.values()) {
                repo.updateState(state);
            }
            return;
        }
        LOGGER.trace("processing new index repositories for state version [{}]", event.state().version());
        Map<String, Repository> survivors = new HashMap<>();
        // First, remove repositories that are no longer there
        for (Map.Entry<String, Repository> entry : repositories.entrySet()) {
            if (newMetadata == null || newMetadata.repository(entry.getKey()) == null) {
                LOGGER.debug("unregistering repository [{}]", entry.getKey());
                closeRepository(entry.getValue());
            } else {
                survivors.put(entry.getKey(), entry.getValue());
            }
        }
        Map<String, Repository> builder = new HashMap<>();
        if (newMetadata != null) {
            // Now go through all repositories and update existing or create missing
            for (RepositoryMetadata repositoryMetadata : newMetadata.repositories()) {
                Repository repository = survivors.get(repositoryMetadata.name());
                if (repository != null) {
                    // Found previous version of this repository
                    RepositoryMetadata previousMetadata = repository.getMetadata();
                    if (previousMetadata.type().equals(repositoryMetadata.type()) == false || previousMetadata.settings().equals(repositoryMetadata.settings()) == false) {
                        // Previous version is different from the version in settings
                        LOGGER.debug("updating repository [{}]", repositoryMetadata.name());
                        closeRepository(repository);
                        repository = null;
                        try {
                            repository = createRepository(repositoryMetadata);
                        } catch (RepositoryException ex) {
                            // TODO: this catch is bogus, it means the old repo is already closed,
                            // but we have nothing to replace it
                            LOGGER.warn(() -> new ParameterizedMessage("failed to change repository [{}]", repositoryMetadata.name()), ex);
                        }
                    }
                } else {
                    try {
                        repository = createRepository(repositoryMetadata);
                    } catch (RepositoryException ex) {
                        LOGGER.warn(() -> new ParameterizedMessage("failed to create repository [{}]", repositoryMetadata.name()), ex);
                    }
                }
                if (repository != null) {
                    LOGGER.debug("registering repository [{}]", repositoryMetadata.name());
                    builder.put(repositoryMetadata.name(), repository);
                }
            }
        }
        for (Repository repo : builder.values()) {
            repo.updateState(state);
        }
        repositories = Collections.unmodifiableMap(builder);
    } catch (Exception ex) {
        LOGGER.warn("failure updating cluster state ", ex);
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RepositoriesMetadata(org.elasticsearch.cluster.metadata.RepositoriesMetadata) HashMap(java.util.HashMap) RepositoryMetadata(org.elasticsearch.cluster.metadata.RepositoryMetadata) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException)

Example 44 with RepositoryMetadata

use of org.elasticsearch.cluster.metadata.RepositoryMetadata 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(Collections.singletonList(new RepositoryMetadata("repo1", "fs", Settings.EMPTY)));
    ClusterState state = ClusterState.builder(new ClusterName("dummy")).metadata(Metadata.builder().putCustom(RepositoriesMetadata.TYPE, repos)).build();
    ClusterServiceUtils.setState(clusterService, state);
    IndexNameExpressionResolver indexNameExpressionResolver = new IndexNameExpressionResolver();
    final AtomicBoolean deleteRepoCalled = new AtomicBoolean(false);
    MockTransportService transportService = MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, THREAD_POOL, clusterService.getClusterSettings());
    TransportDeleteRepositoryAction deleteRepositoryAction = new TransportDeleteRepositoryAction(transportService, clusterService, mock(RepositoriesService.class), THREAD_POOL, indexNameExpressionResolver) {

        @Override
        protected void doExecute(DeleteRepositoryRequest request, ActionListener<AcknowledgedResponse> listener) {
            deleteRepoCalled.set(true);
            listener.onResponse(mock(AcknowledgedResponse.class));
        }
    };
    TransportPutRepositoryAction putRepo = new TransportPutRepositoryAction(transportService, clusterService, mock(RepositoriesService.class), THREAD_POOL, indexNameExpressionResolver) {

        @Override
        protected void doExecute(PutRepositoryRequest request, ActionListener<AcknowledgedResponse> listener) {
            listener.onFailure(new RepositoryException(request.name(), "failure"));
        }
    };
    RepositoryService repositoryService = new RepositoryService(clusterService, deleteRepositoryAction, putRepo);
    try {
        PutRepositoryRequest request = new PutRepositoryRequest("repo1");
        request.type("fs");
        repositoryService.execute(request).get(10, TimeUnit.SECONDS);
    } catch (ExecutionException e) {
        assertThat(deleteRepoCalled.get(), is(true));
        throw e.getCause();
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportDeleteRepositoryAction(org.elasticsearch.action.admin.cluster.repositories.delete.TransportDeleteRepositoryAction) DeleteRepositoryRequest(org.elasticsearch.action.admin.cluster.repositories.delete.DeleteRepositoryRequest) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) RepositoryException(org.elasticsearch.repositories.RepositoryException) RepositoriesMetadata(org.elasticsearch.cluster.metadata.RepositoriesMetadata) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ActionListener(org.elasticsearch.action.ActionListener) RepositoryMetadata(org.elasticsearch.cluster.metadata.RepositoryMetadata) RepositoriesService(org.elasticsearch.repositories.RepositoriesService) 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) PutRepositoryRequest(org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 45 with RepositoryMetadata

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

the class AccessControlMayExecuteTest method setUpSQLExecutor.

@Before
public void setUpSQLExecutor() throws Exception {
    validationCallArguments = new ArrayList<>();
    RepositoriesMetadata repositoriesMetadata = new RepositoriesMetadata(singletonList(new RepositoryMetadata("my_repo", "fs", Settings.builder().put("location", "/tmp/my_repo").build())));
    ClusterState clusterState = ClusterState.builder(clusterService.state()).metadata(Metadata.builder(clusterService.state().metadata()).putCustom(RepositoriesMetadata.TYPE, repositoriesMetadata)).build();
    ClusterServiceUtils.setState(clusterService, clusterState);
    user = new User("normal", Set.of(), Set.of(), null) {

        @Override
        public boolean hasPrivilege(Privilege.Type type, Privilege.Clazz clazz, String ident, String defaultSchema) {
            validationCallArguments.add(CollectionUtils.arrayAsArrayList(type, clazz, ident, user.name()));
            return true;
        }
    };
    superUser = new User("crate", EnumSet.of(User.Role.SUPERUSER), Set.of(), null) {

        @Override
        public boolean hasPrivilege(Privilege.Type type, Privilege.Clazz clazz, @Nullable String ident, String defaultSchema) {
            validationCallArguments.add(CollectionUtils.arrayAsArrayList(type, clazz, ident, superUser.name()));
            return true;
        }
    };
    UserLookupService userLookupService = new UserLookupService(clusterService) {

        @Nullable
        @Override
        public User findUser(String userName) {
            if ("crate".equals(userName)) {
                return superUser;
            }
            return super.findUser(userName);
        }
    };
    userManager = new UserManagerService(null, null, null, null, mock(SysTableRegistry.class), clusterService, userLookupService, new DDLClusterStateService());
    e = SQLExecutor.builder(clusterService).addBlobTable("create blob table blobs").enableDefaultTables().setUser(superUser).addView(new RelationName("doc", "v1"), "select * from users").setUserManager(userManager).build();
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) UserLookupService(io.crate.user.UserLookupService) User(io.crate.user.User) RepositoriesMetadata(org.elasticsearch.cluster.metadata.RepositoriesMetadata) UserManagerService(io.crate.user.UserManagerService) DDLClusterStateService(io.crate.metadata.cluster.DDLClusterStateService) RepositoryMetadata(org.elasticsearch.cluster.metadata.RepositoryMetadata) RelationName(io.crate.metadata.RelationName) Privilege(io.crate.user.Privilege) Before(org.junit.Before)

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