Skip to content

Commit

Permalink
SNAPSHOT: Repo Creation out of ClusterStateTask (#36157)
Browse files Browse the repository at this point in the history
* Move `createRepository` call out of cluster state tasks
    * Now only `RepositoriesService#applyClusterState` manipulates `this.repositories`
* Closes #9488
  • Loading branch information
original-brownbear authored Dec 4, 2018
1 parent c007a42 commit 3c54b41
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -100,20 +99,23 @@ public void registerRepository(final RegisterRepositoryRequest request, final Ac
registrationListener = listener;
}

// Trying to create the new repository on master to make sure it works
try {
closeRepository(createRepository(newRepositoryMetaData));
} catch (Exception e) {
registrationListener.onFailure(e);
return;
}

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 {
public ClusterState execute(ClusterState currentState) {
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);
Expand All @@ -127,6 +129,10 @@ public ClusterState execute(ClusterState currentState) throws IOException {

for (RepositoryMetaData repositoryMetaData : repositories.repositories()) {
if (repositoryMetaData.name().equals(newRepositoryMetaData.name())) {
if (newRepositoryMetaData.equals(repositoryMetaData)) {
// Previous version is the same as this one no update is needed.
return currentState;
}
found = true;
repositoriesMetaData.add(newRepositoryMetaData);
} else {
Expand Down Expand Up @@ -352,37 +358,8 @@ public Repository repository(String repositoryName) {
throw new RepositoryMissingException(repositoryName);
}

/**
* 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.equals(repositoryMetaData)) {
// 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;
}

/** Closes the given repository. */
private void closeRepository(Repository repository) throws IOException {
private void closeRepository(Repository repository) {
logger.debug("closing repository [{}][{}]", repository.getMetadata().type(), repository.getMetadata().name());
repository.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ public void testRepositoryCreation() throws Exception {
assertThat(findRepository(repositoriesResponse.repositories(), "test-repo-1"), notNullValue());
assertThat(findRepository(repositoriesResponse.repositories(), "test-repo-2"), notNullValue());

logger.info("--> check that trying to create a repository with the same settings repeatedly does not update cluster state");
String beforeStateUuid = clusterStateResponse.getState().stateUUID();
assertThat(
client.admin().cluster().preparePutRepository("test-repo-1")
.setType("fs").setSettings(Settings.builder()
.put("location", location)
).get().isAcknowledged(),
equalTo(true));
assertEquals(beforeStateUuid, client.admin().cluster().prepareState().clear().get().getState().stateUUID());

logger.info("--> delete repository test-repo-1");
client.admin().cluster().prepareDeleteRepository("test-repo-1").get();
repositoriesResponse = client.admin().cluster().prepareGetRepositories().get();
Expand Down

0 comments on commit 3c54b41

Please sign in to comment.