Search in sources :

Example 1 with RepositoryMetaData

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

the class S3RepositoryTests method testBasePathSetting.

public void testBasePathSetting() throws IOException {
    RepositoryMetaData metadata = new RepositoryMetaData("dummy-repo", "mock", Settings.builder().put(Repository.BASE_PATH_SETTING.getKey(), "/foo/bar").build());
    S3Repository s3repo = new S3Repository(metadata, Settings.EMPTY, NamedXContentRegistry.EMPTY, new DummyS3Service());
    // make sure leading `/` is removed and trailing is added
    assertEquals("foo/bar/", s3repo.basePath().buildAsString());
    assertWarnings("S3 repository base_path" + " trimming the leading `/`, and leading `/` will not be supported for the S3 repository in future releases");
    metadata = new RepositoryMetaData("dummy-repo", "mock", Settings.EMPTY);
    Settings settings = Settings.builder().put(Repositories.BASE_PATH_SETTING.getKey(), "/foo/bar").build();
    s3repo = new S3Repository(metadata, settings, NamedXContentRegistry.EMPTY, new DummyS3Service());
    // make sure leading `/` is removed and trailing is added
    assertEquals("foo/bar/", s3repo.basePath().buildAsString());
    assertWarnings("S3 repository base_path" + " trimming the leading `/`, and leading `/` will not be supported for the S3 repository in future releases");
}
Also used : RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData) Settings(org.elasticsearch.common.settings.Settings)

Example 2 with RepositoryMetaData

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

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 RegisterRepositoryRequest request, final ActionListener<ClusterStateUpdateResponse> listener) {
    final RepositoryMetaData newRepositoryMetaData = new RepositoryMetaData(request.name, request.type, request.settings);
    final ActionListener<ClusterStateUpdateResponse> registrationListener;
    if (request.verify) {
        registrationListener = new VerifyingRegisterRepositoryListener(request.name, listener);
    } else {
        registrationListener = listener;
    }
    clusterService.submitStateUpdateTask(request.cause, 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(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())) {
                        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.toArray(new RepositoryMetaData[repositoriesMetaData.size()]));
            }
            mdBuilder.putCustom(RepositoriesMetaData.TYPE, repositories);
            return ClusterState.builder(currentState).metaData(mdBuilder).build();
        }

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

        @Override
        public boolean mustAck(DiscoveryNode discoveryNode) {
            return discoveryNode.isMasterNode();
        }
    });
}
Also used : RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) IOException(java.io.IOException) IOException(java.io.IOException) MetaData(org.elasticsearch.cluster.metadata.MetaData) RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData) ArrayList(java.util.ArrayList) List(java.util.List) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse)

Example 3 with RepositoryMetaData

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

the class RepositoriesService method registerRepository.

/**
     * Creates a new repository and adds it to the list of registered repositories.
     * <p>
     * If a repository with the same name but different types or settings already exists, it will be closed and
     * replaced with the new repository. If a repository with the same name exists but it has the same type and settings
     * the new repository is ignored.
     *
     * @param repositoryMetaData new repository metadata
     * @return {@code true} if new repository was added or {@code false} if it was ignored
     */
private boolean registerRepository(RepositoryMetaData repositoryMetaData) throws IOException {
    Repository previous = repositories.get(repositoryMetaData.name());
    if (previous != null) {
        RepositoryMetaData previousMetadata = previous.getMetadata();
        if (!previousMetadata.type().equals(repositoryMetaData.type()) && previousMetadata.settings().equals(repositoryMetaData.settings())) {
            // Previous version is the same as this one - ignore it
            return false;
        }
    }
    Repository newRepo = createRepository(repositoryMetaData);
    if (previous != null) {
        closeRepository(previous);
    }
    Map<String, Repository> newRepositories = new HashMap<>(repositories);
    newRepositories.put(repositoryMetaData.name(), newRepo);
    repositories = newRepositories;
    return true;
}
Also used : HashMap(java.util.HashMap) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData)

Example 4 with RepositoryMetaData

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

the class RestRepositoriesAction method buildTable.

private Table buildTable(RestRequest req, GetRepositoriesResponse getRepositoriesResponse) {
    Table table = getTableWithHeader(req);
    for (RepositoryMetaData repositoryMetaData : getRepositoriesResponse.repositories()) {
        table.startRow();
        table.addCell(repositoryMetaData.name());
        table.addCell(repositoryMetaData.type());
        table.endRow();
    }
    return table;
}
Also used : Table(org.elasticsearch.common.Table) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData)

Example 5 with RepositoryMetaData

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

the class RepositoriesMetaDataSerializationTests method makeTestChanges.

@Override
protected Custom makeTestChanges(Custom testInstance) {
    RepositoriesMetaData repositoriesMetaData = (RepositoriesMetaData) testInstance;
    List<RepositoryMetaData> repos = new ArrayList<>(repositoriesMetaData.repositories());
    if (randomBoolean() && repos.size() > 1) {
        // remove some elements
        int leaveElements = randomIntBetween(0, repositoriesMetaData.repositories().size() - 1);
        repos = randomSubsetOf(leaveElements, repos.toArray(new RepositoryMetaData[leaveElements]));
    }
    if (randomBoolean()) {
        // add some elements
        int addElements = randomInt(10);
        for (int i = 0; i < addElements; i++) {
            repos.add(new RepositoryMetaData(randomAsciiOfLength(10), randomAsciiOfLength(10), randomSettings()));
        }
    }
    return new RepositoriesMetaData(repos.toArray(new RepositoryMetaData[repos.size()]));
}
Also used : RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) ArrayList(java.util.ArrayList) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData)

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