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);
}
}
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]));
}
}
}
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()]));
}
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()]));
}
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());
}
Aggregations