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