Search in sources :

Example 16 with RepositoriesMetadata

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

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 {
        RepositoriesMetaData oldMetaData = event.previousState().getMetaData().custom(RepositoriesMetaData.TYPE);
        RepositoriesMetaData newMetaData = event.state().getMetaData().custom(RepositoriesMetaData.TYPE);
        // Check if repositories got changed
        if ((oldMetaData == null && newMetaData == null) || (oldMetaData != null && oldMetaData.equals(newMetaData))) {
            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((Supplier<?>) () -> new ParameterizedMessage("failed to change repository [{}]", repositoryMetaData.name()), ex);
                        }
                    }
                } else {
                    try {
                        repository = createRepository(repositoryMetaData);
                    } catch (RepositoryException ex) {
                        logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to create repository [{}]", repositoryMetaData.name()), ex);
                    }
                }
                if (repository != null) {
                    logger.debug("registering repository [{}]", repositoryMetaData.name());
                    builder.put(repositoryMetaData.name(), repository);
                }
            }
        }
        repositories = Collections.unmodifiableMap(builder);
    } catch (Exception ex) {
        logger.warn("failure updating cluster state ", ex);
    }
}
Also used : RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) HashMap(java.util.HashMap) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException)

Example 17 with RepositoriesMetadata

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

the class TransportGetRepositoriesAction method masterOperation.

@Override
protected void masterOperation(final GetRepositoriesRequest request, ClusterState state, final ActionListener<GetRepositoriesResponse> listener) {
    MetaData metaData = state.metaData();
    RepositoriesMetaData repositories = metaData.custom(RepositoriesMetaData.TYPE);
    if (request.repositories().length == 0 || (request.repositories().length == 1 && "_all".equals(request.repositories()[0]))) {
        if (repositories != null) {
            listener.onResponse(new GetRepositoriesResponse(repositories.repositories()));
        } else {
            listener.onResponse(new GetRepositoriesResponse(Collections.<RepositoryMetaData>emptyList()));
        }
    } else {
        if (repositories != null) {
            // to keep insertion order
            Set<String> repositoriesToGet = new LinkedHashSet<>();
            for (String repositoryOrPattern : request.repositories()) {
                if (Regex.isSimpleMatchPattern(repositoryOrPattern) == false) {
                    repositoriesToGet.add(repositoryOrPattern);
                } else {
                    for (RepositoryMetaData repository : repositories.repositories()) {
                        if (Regex.simpleMatch(repositoryOrPattern, repository.name())) {
                            repositoriesToGet.add(repository.name());
                        }
                    }
                }
            }
            List<RepositoryMetaData> repositoryListBuilder = new ArrayList<>();
            for (String repository : repositoriesToGet) {
                RepositoryMetaData repositoryMetaData = repositories.repository(repository);
                if (repositoryMetaData == null) {
                    listener.onFailure(new RepositoryMissingException(repository));
                    return;
                }
                repositoryListBuilder.add(repositoryMetaData);
            }
            listener.onResponse(new GetRepositoriesResponse(Collections.unmodifiableList(repositoryListBuilder)));
        } else {
            listener.onFailure(new RepositoryMissingException(request.repositories()[0]));
        }
    }
}
Also used : RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) LinkedHashSet(java.util.LinkedHashSet) 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) RepositoryMissingException(org.elasticsearch.repositories.RepositoryMissingException)

Example 18 with RepositoriesMetadata

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

the class RepositoriesMetaDataSerializationTests method createTestInstance.

@Override
protected Custom createTestInstance() {
    int numberOfRepositories = randomInt(10);
    List<RepositoryMetaData> entries = new ArrayList<>();
    for (int i = 0; i < numberOfRepositories; i++) {
        entries.add(new RepositoryMetaData(randomAsciiOfLength(10), randomAsciiOfLength(10), randomSettings()));
    }
    entries.sort(Comparator.comparing(RepositoryMetaData::name));
    return new RepositoriesMetaData(entries.toArray(new RepositoryMetaData[entries.size()]));
}
Also used : RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) ArrayList(java.util.ArrayList) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData)

Example 19 with RepositoriesMetadata

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

the class RepositoriesMetaDataSerializationTests method doParseInstance.

@Override
protected Custom doParseInstance(XContentParser parser) throws IOException {
    assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
    RepositoriesMetaData repositoriesMetaData = RepositoriesMetaData.fromXContent(parser);
    assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
    List<RepositoryMetaData> repos = repositoriesMetaData.repositories();
    repos.sort(Comparator.comparing(RepositoryMetaData::name));
    return new RepositoriesMetaData(repos.toArray(new RepositoryMetaData[repos.size()]));
}
Also used : RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData)

Example 20 with RepositoriesMetadata

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

the class SysRepositoriesService method repositoriesChanged.

private boolean repositoriesChanged(ClusterChangedEvent event) {
    RepositoriesMetaData previousRepositoriesMetaData = event.previousState().metaData().custom(RepositoriesMetaData.TYPE);
    RepositoriesMetaData repositoriesMetaData = event.state().metaData().custom(RepositoriesMetaData.TYPE);
    if (previousRepositoriesMetaData == null && repositoriesMetaData == null) {
        return false;
    } else if (previousRepositoriesMetaData == null || repositoriesMetaData == null) {
        return true;
    }
    return !repositoriesMetaData.repositories().equals(previousRepositoriesMetaData.repositories());
}
Also used : RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData)

Aggregations

RepositoriesMetadata (org.elasticsearch.cluster.metadata.RepositoriesMetadata)15 ClusterState (org.elasticsearch.cluster.ClusterState)14 RepositoriesMetaData (org.elasticsearch.cluster.metadata.RepositoriesMetaData)14 RepositoryMetadata (org.elasticsearch.cluster.metadata.RepositoryMetadata)13 ArrayList (java.util.ArrayList)10 RepositoryMetaData (org.elasticsearch.cluster.metadata.RepositoryMetaData)10 ClusterName (org.elasticsearch.cluster.ClusterName)8 Before (org.junit.Before)7 IOException (java.io.IOException)5 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)5 Map (java.util.Map)4 ActionListener (org.elasticsearch.action.ActionListener)4 RepositoryParamValidator (io.crate.analyze.repositories.RepositoryParamValidator)3 TypeSettings (io.crate.analyze.repositories.TypeSettings)3 HashMap (java.util.HashMap)3 DeleteRepositoryRequest (org.elasticsearch.action.admin.cluster.repositories.delete.DeleteRepositoryRequest)3 PutRepositoryRequest (org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest)3 RepositoryException (org.elasticsearch.repositories.RepositoryException)3 Path (java.nio.file.Path)2 List (java.util.List)2