Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions changelog/unreleased/SOLR-16398-restore-delete-backup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
title: >
Add v2 API to restore a core from backup and to delete a backup of a core.
type: added
authors:
- name: Jalaz Kumar
nick: jaykay12
links:
- name: SOLR-16398
url: https://issues.apache.org/jira/browse/SOLR-16398
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
Expand All @@ -37,6 +38,7 @@
import org.apache.solr.client.api.model.ReplicationDetailsResponse;
import org.apache.solr.client.api.model.ReplicationEnabledRequestBody;
import org.apache.solr.client.api.model.ReplicationPollingRequestBody;
import org.apache.solr.client.api.model.ReplicationRestoreRequestBody;
import org.apache.solr.client.api.model.RestoreStatusResponse;
import org.apache.solr.client.api.model.SolrJerseyResponse;
import org.apache.solr.client.api.util.CoreApiParameters;
Expand All @@ -57,6 +59,18 @@ ReplicationDetailsResponse fetchReplicationDetails(
@DefaultValue("true")
Boolean follower);

@POST
@CoreApiParameters
@Path("/restore")
@Operation(
summary = "Restore the index of the specified core from a backup.",
description = "Restore runs asynchronously; use the restore status API to track progress",
tags = {"replication"})
SolrJerseyResponse restoreIndex(
@RequestBody(description = "Identifies the backup to restore")
ReplicationRestoreRequestBody requestBody)
throws IOException;

@GET
@CoreApiParameters
@Path("/restore/status")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
package org.apache.solr.client.api.endpoint;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.QueryParam;
import org.apache.solr.client.api.model.ReplicationBackupRequestBody;
import org.apache.solr.client.api.model.ReplicationBackupResponse;
import org.apache.solr.client.api.model.SolrJerseyResponse;
import org.apache.solr.client.api.util.CoreApiParameters;

/**
Expand All @@ -40,4 +45,17 @@ public interface ReplicationBackupApis {
tags = {"replication-backups"})
ReplicationBackupResponse createBackup(
@RequestBody ReplicationBackupRequestBody backupReplicationPayload);

@DELETE
@CoreApiParameters
@Path("/backups/{name}")
@Operation(
summary = "Delete a backup which was taken via Solr's 'Replication Handler'",
tags = {"replication-backups"})
SolrJerseyResponse deleteBackup(
@Parameter(description = "Name of the backup to delete", required = true) @PathParam("name")
String name,
@Parameter(description = "location of the backup") @QueryParam("location") String location,
@Parameter(description = "name of the backup repository") @QueryParam("repository")
String repository);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.client.api.model;

import com.fasterxml.jackson.annotation.JsonProperty;

/** Request body for restoring a core's index from a backup taken via the replication handler */
public class ReplicationRestoreRequestBody {

@JsonProperty public String name;

@JsonProperty public String location;

@JsonProperty public String repository;
}
45 changes: 20 additions & 25 deletions solr/core/src/java/org/apache/solr/handler/ReplicationHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package org.apache.solr.handler;

import static org.apache.solr.common.params.CommonParams.NAME;
import static org.apache.solr.common.params.CoreAdminParams.BACKUP_LOCATION;
import static org.apache.solr.common.params.CoreAdminParams.BACKUP_REPOSITORY;
import static org.apache.solr.common.params.CoreAdminParams.COMMIT_NAME;
import static org.apache.solr.handler.admin.api.ReplicationAPIBase.CHECKSUM;
import static org.apache.solr.handler.admin.api.ReplicationAPIBase.COMPRESSION;
import static org.apache.solr.handler.admin.api.ReplicationAPIBase.CONF_FILE_SHORT;
Expand Down Expand Up @@ -90,7 +93,6 @@
import org.apache.solr.client.api.model.SolrJerseyResponse;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.params.CoreAdminParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.ExecutorUtil;
Expand Down Expand Up @@ -314,7 +316,9 @@ public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throw
} else if (command.equalsIgnoreCase(CMD_BACKUP)) {
doSnapShoot(new ModifiableSolrParams(solrParams), rsp, req);
} else if (command.equalsIgnoreCase(CMD_RESTORE)) {
restore(new ModifiableSolrParams(solrParams), rsp, req);
restore(
solrParams.get(NAME), solrParams.get(BACKUP_LOCATION), solrParams.get(BACKUP_REPOSITORY));
rsp.add(STATUS, OK_STATUS);
} else if (command.equalsIgnoreCase(CMD_RESTORE_STATUS)) {
final CoreReplication coreReplicationAPI = new CoreReplication(core, req, rsp);
final RestoreStatusResponse restoreStatus = coreReplicationAPI.fetchRestoreStatus();
Expand All @@ -328,7 +332,13 @@ public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throw
new Exception(restoreStatus.restoreStatus.exception));
}
} else if (command.equalsIgnoreCase(CMD_DELETE_BACKUP)) {
deleteSnapshot(new ModifiableSolrParams(solrParams), rsp);
solrParams.required().get(NAME);
deleteBackup(
solrParams.get(NAME),
solrParams.get(BACKUP_LOCATION),
solrParams.get(BACKUP_REPOSITORY),
solrParams.get(COMMIT_NAME));
rsp.add(STATUS, OK_STATUS);
} else if (command.equalsIgnoreCase(CMD_FETCH_INDEX)) {
fetchIndex(solrParams, rsp);
} else if (command.equalsIgnoreCase(CMD_DISABLE_POLL)) {
Expand Down Expand Up @@ -425,16 +435,7 @@ public boolean abortFetch() {
}

@SuppressWarnings("deprecation")
private void deleteSnapshot(ModifiableSolrParams params, SolrQueryResponse rsp) {
params.required().get(NAME);

String repoName = params.get(CoreAdminParams.BACKUP_REPOSITORY);
String location = params.get(CoreAdminParams.BACKUP_LOCATION);

// commitName is not documented in ref guide for the backup option.
// copying to here, an dit may be that both are just null?
String commitName = params.get(CoreAdminParams.COMMIT_NAME);

public void deleteBackup(String name, String location, String repoName, String commitName) {
CoreContainer cc = core.getCoreContainer();
BackupRepository repo = null;
if (repoName != null) {
Expand All @@ -457,11 +458,9 @@ private void deleteSnapshot(ModifiableSolrParams params, SolrQueryResponse rsp)
}

URI locationUri = repo.createDirectoryURI(location);
SnapShooter snapShooter =
new SnapShooter(repo, core, locationUri, params.get(NAME), commitName);
SnapShooter snapShooter = new SnapShooter(repo, core, locationUri, name, commitName);
snapShooter.validateDeleteSnapshot();
snapShooter.deleteSnapAsync(this);
rsp.add(STATUS, OK_STATUS);
}

private void fetchIndex(SolrParams solrParams, SolrQueryResponse rsp)
Expand Down Expand Up @@ -591,16 +590,13 @@ boolean isReplicating() {
return indexFetchLock.isLocked();
}

private void restore(SolrParams params, SolrQueryResponse rsp, SolrQueryRequest req)
throws IOException {
public void restore(String name, String location, String repoName) throws IOException {
if (restoreFuture != null && !restoreFuture.isDone()) {
throw new SolrException(
ErrorCode.BAD_REQUEST,
"Restore in progress. Cannot run multiple restore operations" + "for the same core");
}
String name = params.get(NAME);
String location = params.get(CoreAdminParams.BACKUP_LOCATION);
String repoName = params.get(CoreAdminParams.BACKUP_REPOSITORY);

CoreContainer cc = core.getCoreContainer();
BackupRepository repo = null;
if (repoName != null) {
Expand Down Expand Up @@ -654,7 +650,6 @@ private void restore(SolrParams params, SolrQueryResponse rsp, SolrQueryRequest
MDC.put("RestoreCore.backupName", name);
restoreFuture = restoreExecutor.submit(restoreCore);
currentRestoreName = name;
rsp.add(STATUS, OK_STATUS);
} finally {
MDC.remove("RestoreCore.core");
MDC.remove("RestoreCore.backupLocation");
Expand Down Expand Up @@ -696,9 +691,9 @@ public CommitsResponse getCommitsResponse() {
private void doSnapShoot(SolrParams params, SolrQueryResponse rsp, SolrQueryRequest req) {
try {
int numberToKeep = params.getInt(NUMBER_BACKUPS_TO_KEEP_REQUEST_PARAM, 0);
String location = params.get(CoreAdminParams.BACKUP_LOCATION);
String repoName = params.get(CoreAdminParams.BACKUP_REPOSITORY);
String commitName = params.get(CoreAdminParams.COMMIT_NAME);
String location = params.get(BACKUP_LOCATION);
String repoName = params.get(BACKUP_REPOSITORY);
String commitName = params.get(COMMIT_NAME);
String name = params.get(NAME);
doSnapShoot(
numberToKeep,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.solr.client.api.model.ReplicationDetailsResponse;
import org.apache.solr.client.api.model.ReplicationEnabledRequestBody;
import org.apache.solr.client.api.model.ReplicationPollingRequestBody;
import org.apache.solr.client.api.model.ReplicationRestoreRequestBody;
import org.apache.solr.client.api.model.RestoreStatusResponse;
import org.apache.solr.client.api.model.SolrJerseyResponse;
import org.apache.solr.common.SolrException;
Expand Down Expand Up @@ -57,6 +58,13 @@ public ReplicationDetailsResponse fetchReplicationDetails(Boolean follower) {
return doFetchReplicationDetails(follower == null || follower);
}

@Override
@PermissionName(CORE_EDIT_PERM)
public SolrJerseyResponse restoreIndex(ReplicationRestoreRequestBody requestBody)
throws IOException {
return doRestoreIndex(requestBody);
}

@Override
@PermissionName(CORE_READ_PERM)
public RestoreStatusResponse fetchRestoreStatus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.apache.solr.client.api.model.ReplicationDetailsResponse;
import org.apache.solr.client.api.model.ReplicationEnabledRequestBody;
import org.apache.solr.client.api.model.ReplicationPollingRequestBody;
import org.apache.solr.client.api.model.ReplicationRestoreRequestBody;
import org.apache.solr.client.api.model.RestoreStatusResponse;
import org.apache.solr.client.api.model.SolrJerseyResponse;
import org.apache.solr.common.SolrException;
Expand Down Expand Up @@ -125,6 +126,16 @@ protected SolrJerseyResponse doSetReplicationPolling(ReplicationPollingRequestBo
return instantiateJerseyResponse(SolrJerseyResponse.class);
}

protected SolrJerseyResponse doRestoreIndex(ReplicationRestoreRequestBody requestBody)
throws IOException {
final ReplicationRestoreRequestBody body =
requestBody == null ? new ReplicationRestoreRequestBody() : requestBody;
ReplicationHandler replicationHandler =
(ReplicationHandler) solrCore.getRequestHandler(ReplicationHandler.PATH);
replicationHandler.restore(body.name, body.location, body.repository);
return instantiateJerseyResponse(SolrJerseyResponse.class);
}

protected RestoreStatusResponse doFetchRestoreStatus() {
ReplicationHandler replicationHandler =
(ReplicationHandler) solrCore.getRequestHandler(ReplicationHandler.PATH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.solr.client.api.endpoint.ReplicationBackupApis;
import org.apache.solr.client.api.model.ReplicationBackupRequestBody;
import org.apache.solr.client.api.model.ReplicationBackupResponse;
import org.apache.solr.client.api.model.SolrJerseyResponse;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.SolrCore;
Expand Down Expand Up @@ -66,6 +67,16 @@ public ReplicationBackupResponse createBackup(
return doBackup(backupReplicationPayload);
}

@Override
@PermissionName(CORE_EDIT_PERM)
public SolrJerseyResponse deleteBackup(String name, String location, String repository) {
ensureRequiredParameterProvided("name", name);
final var replicationHandler =
(ReplicationHandler) solrCore.getRequestHandler(ReplicationHandler.PATH);
replicationHandler.deleteBackup(name, location, repository, null);
return instantiateJerseyResponse(SolrJerseyResponse.class);
}

private ReplicationBackupResponse doBackup(
ReplicationBackupRequestBody backupReplicationPayload) {
ReplicationBackupResponse response = instantiateJerseyResponse(ReplicationBackupResponse.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.lucene.tests.util.TestUtil;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.request.V2Request;
import org.apache.solr.embedded.JettyConfig;
import org.apache.solr.embedded.JettySolrRunner;
import org.junit.After;
Expand Down Expand Up @@ -223,8 +224,17 @@ private void testDeleteNamedBackup(String[] backupNames) throws Exception {
for (int i = 0; i < 2; i++) {
final Path p = Path.of(leader.getDataDir(), "snapshot." + backupNames[i]);
assertTrue("WTF: Backup doesn't exist: " + p, Files.exists(p));
runBackupCommand(
leaderJetty, ReplicationHandler.CMD_DELETE_BACKUP, "&name=" + backupNames[i]);
if (random().nextBoolean()) {
runBackupCommand(
leaderJetty, ReplicationHandler.CMD_DELETE_BACKUP, "&name=" + backupNames[i]);
} else {
leaderClient.request(
new V2Request.Builder(
"/cores/" + DEFAULT_TEST_CORENAME + "/replication/backups/" + backupNames[i])
.DELETE()
.build());
}

backupStatus.waitForBackupDeletionSuccess(backupNames[i], 30);
assertFalse("backup still exists after deletion: " + p, Files.exists(p));
}
Expand Down
20 changes: 17 additions & 3 deletions solr/core/src/test/org/apache/solr/handler/TestRestoreCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.lucene.index.IndexFileNames;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.tests.util.TestUtil;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.request.V2Request;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.embedded.JettyConfig;
import org.apache.solr.embedded.JettySolrRunner;
Expand Down Expand Up @@ -96,7 +99,7 @@ public void testSimpleRestore() throws Exception {
new BackupStatusChecker(leaderClient, "/" + DEFAULT_TEST_CORENAME + "/replication");
final String oldBackupDir = backupStatus.checkBackupSuccess();
String snapshotName = null;
String location;
String location = null;
String params = "";
String baseUrl = leaderJetty.getBaseUrl().toString();

Expand Down Expand Up @@ -153,8 +156,19 @@ public void testSimpleRestore() throws Exception {
}
}

TestReplicationHandlerBackup.runBackupCommand(
leaderJetty, ReplicationHandler.CMD_RESTORE, params);
if (random().nextBoolean()) {
TestReplicationHandlerBackup.runBackupCommand(
leaderJetty, ReplicationHandler.CMD_RESTORE, params);
} else {
final Map<String, Object> requestBody = new HashMap<>();
if (snapshotName != null) requestBody.put("name", snapshotName);
if (location != null) requestBody.put("location", location);
leaderClient.request(
new V2Request.Builder("/cores/" + DEFAULT_TEST_CORENAME + "/replication/restore")
.POST()
.withPayload(requestBody)
.build());
}

while (!TestRestoreCoreUtil.fetchRestoreStatus(baseUrl, DEFAULT_TEST_CORENAME)) {
Thread.sleep(1000);
Expand Down
Loading