gerlowskija commented on code in PR #1224:
URL: https://github.com/apache/solr/pull/1224#discussion_r1114647260


##########
solr/core/src/java/org/apache/solr/handler/admin/api/BackupCoreAPI.java:
##########
@@ -0,0 +1,255 @@
+/*
+ * 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.handler.admin.api;
+
+import static 
org.apache.solr.client.solrj.impl.BinaryResponseParser.BINARY_CONTENT_TYPE_V2;
+import static 
org.apache.solr.security.PermissionNameProvider.Name.COLL_EDIT_PERM;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import io.swagger.v3.oas.annotations.parameters.RequestBody;
+import java.net.URI;
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import javax.inject.Inject;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import org.apache.solr.api.JerseyResource;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.annotation.JsonProperty;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.core.CoreContainer;
+import org.apache.solr.core.SolrCore;
+import org.apache.solr.core.backup.BackupFilePaths;
+import org.apache.solr.core.backup.ShardBackupId;
+import org.apache.solr.core.backup.repository.BackupRepository;
+import org.apache.solr.handler.IncrementalShardBackup;
+import org.apache.solr.handler.SnapShooter;
+import org.apache.solr.jersey.JacksonReflectMapWriter;
+import org.apache.solr.jersey.PermissionName;
+import org.apache.solr.jersey.SolrJerseyResponse;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.response.SolrQueryResponse;
+
+@Path(("/cores/{coreName}/backup/{name}"))
+public class BackupCoreAPI extends JerseyResource {
+  private final SolrQueryResponse solrQueryResponse;
+  private final SolrQueryRequest solrQueryRequest;
+  private final CoreContainer coreContainer;
+
+  @Inject
+  public BackupCoreAPI(
+      CoreContainer coreContainer,
+      SolrQueryRequest solrQueryRequest,
+      SolrQueryResponse solrQueryResponse) {
+    this.solrQueryResponse = solrQueryResponse;
+    this.solrQueryRequest = solrQueryRequest;
+    this.coreContainer = coreContainer;
+  }
+
+  @POST
+  @Produces({"application/json", "application/xml", BINARY_CONTENT_TYPE_V2})
+  @PermissionName(COLL_EDIT_PERM)
+  public BackupCoreResponse createBackup(
+      @Schema(description = "The name of the core.") @PathParam("coreName") 
String coreName,
+      @Schema(description = "The backup will be created in a directory called 
snapshot.<name>")
+          @PathParam("name")
+          String name,
+      @Schema(description = "The POJO for representing additional backup 
params.") @RequestBody
+          BackupCoreRequestBody backupCoreRequestBody)
+      throws Exception {
+    final BackupCoreResponse backupCoreResponse =
+        instantiateJerseyResponse(BackupCoreResponse.class);
+    try (BackupRepository repository =
+            
coreContainer.newBackupRepository(backupCoreRequestBody.repository);
+        SolrCore core = coreContainer.getCore(coreName)) {
+      String location = 
repository.getBackupLocation(backupCoreRequestBody.location);
+      if (location == null) {
+        throw new SolrException(
+            SolrException.ErrorCode.BAD_REQUEST,
+            "'location' is not specified as a query"
+                + " parameter or as a default repository property");
+      }
+      URI locationUri = repository.createDirectoryURI(location);
+      repository.createDirectory(locationUri);
+
+      SnapShooter snapShooter =
+          new SnapShooter(repository, core, locationUri, name, 
backupCoreRequestBody.commitName);
+      // validateCreateSnapshot will create parent dirs instead of throw; that 
choice is dubious.
+      // But we want to throw. One reason is that this dir really should, in 
fact must, already
+      // exist here if triggered via a collection backup on a shared file 
system. Otherwise,
+      // perhaps the FS location isn't shared -- we want an error.
+      if 
(!snapShooter.getBackupRepository().exists(snapShooter.getLocation())) {
+        throw new SolrException(
+            SolrException.ErrorCode.BAD_REQUEST,
+            "Directory to contain snapshots doesn't exist: "
+                + snapShooter.getLocation()
+                + ". "
+                + "Note that Backup/Restore of a SolrCloud collection "
+                + "requires a shared file system mounted at the same path on 
all nodes!");
+      }
+      snapShooter.validateCreateSnapshot();
+      Map<String, Object> rsp = snapShooter.createSnapshot().toMap(new 
HashMap<>());
+      backupCoreResponse.directoryName = rsp.get("directoryName").toString();
+      backupCoreResponse.endTime = 
Instant.parse(rsp.get("endTime").toString());
+      backupCoreResponse.indexFileCount = 
Integer.parseInt(rsp.get("indexFileCount").toString());
+      backupCoreResponse.snapshotName = rsp.get("snapshotName").toString();
+      backupCoreResponse.status = rsp.get("status").toString();
+    }
+    return backupCoreResponse;
+  }
+
+  @Path("/incremental")
+  @Produces({"application/json", "application/xml", BINARY_CONTENT_TYPE_V2})
+  @PermissionName(COLL_EDIT_PERM)
+  public IncrementalBackupCoreResponse createIncrementBackup(
+      @Schema(description = "The name of the core.") @PathParam("coreName") 
String coreName,
+      @Schema(description = "The backup will be created in a directory called 
snapshot.<name>")
+          @PathParam("name")
+          String name,
+      @Schema(description = "The POJO for representing additional backup 
params.") @RequestBody
+          BackupCoreRequestBody backupCoreRequestBody)
+      throws Exception {
+    final IncrementalBackupCoreResponse backupCoreResponse =
+        instantiateJerseyResponse(IncrementalBackupCoreResponse.class);
+    if (backupCoreRequestBody.shardBackupId == null) {
+      throw new SolrException(
+          SolrException.ErrorCode.BAD_REQUEST, "Missing required parameter: 
shardBackupId");
+    }
+    final ShardBackupId shardBackupId = 
ShardBackupId.from(backupCoreRequestBody.shardBackupId);
+    try (BackupRepository repository =
+            
coreContainer.newBackupRepository(backupCoreRequestBody.repository);
+        SolrCore core = coreContainer.getCore(coreName)) {
+      String location = 
repository.getBackupLocation(backupCoreRequestBody.location);
+      if (location == null) {
+        throw new SolrException(
+            SolrException.ErrorCode.BAD_REQUEST,
+            "'location' is not specified as a query"
+                + " parameter or as a default repository property");
+      }
+      URI locationUri = repository.createDirectoryURI(location);
+      repository.createDirectory(locationUri);
+      final ShardBackupId prevShardBackupId =
+          backupCoreRequestBody.prevShardBackupId != null
+              ? ShardBackupId.from(backupCoreRequestBody.prevShardBackupId)
+              : null;
+      BackupFilePaths incBackupFiles = new BackupFilePaths(repository, 
locationUri);
+      IncrementalShardBackup incSnapShooter =
+          new IncrementalShardBackup(
+              repository,
+              core,
+              incBackupFiles,
+              prevShardBackupId,
+              shardBackupId,
+              Optional.ofNullable(backupCoreRequestBody.commitName));
+      NamedList<Object> rsp = incSnapShooter.backup();
+      backupCoreResponse.endTime = 
Instant.parse(rsp.get("endTime").toString());
+      backupCoreResponse.indexSizeMB = 
Double.parseDouble(rsp.get("indexSizeMB").toString());
+      backupCoreResponse.indexFileCount = 
Integer.parseInt(rsp.get("indexFileCount").toString());
+      backupCoreResponse.startTime = 
Instant.parse(rsp.get("startTime").toString());
+      backupCoreResponse.shardBackupId = rsp.get("shardBackupId").toString();
+      backupCoreResponse.uploadedIndexFileCount =
+          Integer.parseInt(rsp.get("uploadedIndexFileCount").toString());
+      backupCoreResponse.uploadedIndexFileMB =
+          Double.parseDouble(rsp.get("uploadedIndexFileMB").toString());
+      if (rsp.get("shard") != null) backupCoreResponse.shard = 
rsp.get("shard").toString();
+    }
+    return backupCoreResponse;
+  }
+
+  public static class BackupCoreRequestBody implements JacksonReflectMapWriter 
{
+
+    @Schema(description = "The name of the repository to be used for e 
backup.")
+    @JsonProperty("repository")
+    public String repository;
+
+    @Schema(description = "The path where the backup will be created")
+    @JsonProperty("location")
+    public String location;
+
+    @JsonProperty("shardBackupId")
+    public String shardBackupId;
+
+    @JsonProperty("prevShardBackupId")
+    public String prevShardBackupId;
+
+    @Schema(
+        description =
+            "The name of the commit which was used while taking a snapshot 
using the CREATESNAPSHOT command.")
+    @JsonProperty("commitName")
+    public String commitName;
+  }
+
+  public static class IncrementalBackupCoreResponse extends SolrJerseyResponse 
{
+    @Schema(description = "The time at which backup snapshot started at.")

Review Comment:
   [+1] Really appreciate all the work you put into these `@Schema` annotations!



##########
solr/core/src/java/org/apache/solr/handler/admin/api/BackupCoreAPI.java:
##########
@@ -0,0 +1,255 @@
+/*
+ * 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.handler.admin.api;
+
+import static 
org.apache.solr.client.solrj.impl.BinaryResponseParser.BINARY_CONTENT_TYPE_V2;
+import static 
org.apache.solr.security.PermissionNameProvider.Name.COLL_EDIT_PERM;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import io.swagger.v3.oas.annotations.parameters.RequestBody;
+import java.net.URI;
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import javax.inject.Inject;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import org.apache.solr.api.JerseyResource;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.annotation.JsonProperty;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.core.CoreContainer;
+import org.apache.solr.core.SolrCore;
+import org.apache.solr.core.backup.BackupFilePaths;
+import org.apache.solr.core.backup.ShardBackupId;
+import org.apache.solr.core.backup.repository.BackupRepository;
+import org.apache.solr.handler.IncrementalShardBackup;
+import org.apache.solr.handler.SnapShooter;
+import org.apache.solr.jersey.JacksonReflectMapWriter;
+import org.apache.solr.jersey.PermissionName;
+import org.apache.solr.jersey.SolrJerseyResponse;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.response.SolrQueryResponse;
+
+@Path(("/cores/{coreName}/backup/{name}"))
+public class BackupCoreAPI extends JerseyResource {
+  private final SolrQueryResponse solrQueryResponse;
+  private final SolrQueryRequest solrQueryRequest;
+  private final CoreContainer coreContainer;
+
+  @Inject
+  public BackupCoreAPI(
+      CoreContainer coreContainer,
+      SolrQueryRequest solrQueryRequest,
+      SolrQueryResponse solrQueryResponse) {
+    this.solrQueryResponse = solrQueryResponse;
+    this.solrQueryRequest = solrQueryRequest;
+    this.coreContainer = coreContainer;
+  }
+
+  @POST
+  @Produces({"application/json", "application/xml", BINARY_CONTENT_TYPE_V2})
+  @PermissionName(COLL_EDIT_PERM)
+  public BackupCoreResponse createBackup(
+      @Schema(description = "The name of the core.") @PathParam("coreName") 
String coreName,
+      @Schema(description = "The backup will be created in a directory called 
snapshot.<name>")
+          @PathParam("name")
+          String name,
+      @Schema(description = "The POJO for representing additional backup 
params.") @RequestBody
+          BackupCoreRequestBody backupCoreRequestBody)
+      throws Exception {
+    final BackupCoreResponse backupCoreResponse =
+        instantiateJerseyResponse(BackupCoreResponse.class);
+    try (BackupRepository repository =
+            
coreContainer.newBackupRepository(backupCoreRequestBody.repository);
+        SolrCore core = coreContainer.getCore(coreName)) {
+      String location = 
repository.getBackupLocation(backupCoreRequestBody.location);
+      if (location == null) {
+        throw new SolrException(
+            SolrException.ErrorCode.BAD_REQUEST,
+            "'location' is not specified as a query"
+                + " parameter or as a default repository property");
+      }
+      URI locationUri = repository.createDirectoryURI(location);
+      repository.createDirectory(locationUri);
+
+      SnapShooter snapShooter =
+          new SnapShooter(repository, core, locationUri, name, 
backupCoreRequestBody.commitName);
+      // validateCreateSnapshot will create parent dirs instead of throw; that 
choice is dubious.
+      // But we want to throw. One reason is that this dir really should, in 
fact must, already
+      // exist here if triggered via a collection backup on a shared file 
system. Otherwise,
+      // perhaps the FS location isn't shared -- we want an error.
+      if 
(!snapShooter.getBackupRepository().exists(snapShooter.getLocation())) {
+        throw new SolrException(
+            SolrException.ErrorCode.BAD_REQUEST,
+            "Directory to contain snapshots doesn't exist: "
+                + snapShooter.getLocation()
+                + ". "
+                + "Note that Backup/Restore of a SolrCloud collection "
+                + "requires a shared file system mounted at the same path on 
all nodes!");
+      }
+      snapShooter.validateCreateSnapshot();
+      Map<String, Object> rsp = snapShooter.createSnapshot().toMap(new 
HashMap<>());
+      backupCoreResponse.directoryName = rsp.get("directoryName").toString();
+      backupCoreResponse.endTime = 
Instant.parse(rsp.get("endTime").toString());
+      backupCoreResponse.indexFileCount = 
Integer.parseInt(rsp.get("indexFileCount").toString());
+      backupCoreResponse.snapshotName = rsp.get("snapshotName").toString();
+      backupCoreResponse.status = rsp.get("status").toString();
+    }
+    return backupCoreResponse;
+  }
+
+  @Path("/incremental")
+  @Produces({"application/json", "application/xml", BINARY_CONTENT_TYPE_V2})
+  @PermissionName(COLL_EDIT_PERM)
+  public IncrementalBackupCoreResponse createIncrementBackup(
+      @Schema(description = "The name of the core.") @PathParam("coreName") 
String coreName,
+      @Schema(description = "The backup will be created in a directory called 
snapshot.<name>")
+          @PathParam("name")
+          String name,
+      @Schema(description = "The POJO for representing additional backup 
params.") @RequestBody
+          BackupCoreRequestBody backupCoreRequestBody)
+      throws Exception {
+    final IncrementalBackupCoreResponse backupCoreResponse =
+        instantiateJerseyResponse(IncrementalBackupCoreResponse.class);
+    if (backupCoreRequestBody.shardBackupId == null) {
+      throw new SolrException(
+          SolrException.ErrorCode.BAD_REQUEST, "Missing required parameter: 
shardBackupId");
+    }
+    final ShardBackupId shardBackupId = 
ShardBackupId.from(backupCoreRequestBody.shardBackupId);
+    try (BackupRepository repository =
+            
coreContainer.newBackupRepository(backupCoreRequestBody.repository);
+        SolrCore core = coreContainer.getCore(coreName)) {
+      String location = 
repository.getBackupLocation(backupCoreRequestBody.location);
+      if (location == null) {
+        throw new SolrException(
+            SolrException.ErrorCode.BAD_REQUEST,
+            "'location' is not specified as a query"
+                + " parameter or as a default repository property");
+      }
+      URI locationUri = repository.createDirectoryURI(location);
+      repository.createDirectory(locationUri);
+      final ShardBackupId prevShardBackupId =
+          backupCoreRequestBody.prevShardBackupId != null
+              ? ShardBackupId.from(backupCoreRequestBody.prevShardBackupId)
+              : null;
+      BackupFilePaths incBackupFiles = new BackupFilePaths(repository, 
locationUri);
+      IncrementalShardBackup incSnapShooter =
+          new IncrementalShardBackup(
+              repository,
+              core,
+              incBackupFiles,
+              prevShardBackupId,
+              shardBackupId,
+              Optional.ofNullable(backupCoreRequestBody.commitName));
+      NamedList<Object> rsp = incSnapShooter.backup();

Review Comment:
   [-1] Maybe we should just have `IncrementalSnapShooter.backup` return an 
IncrementalBackupCoreResponse rather than having it return a NL that we then 
have to unpack.



##########
solr/core/src/java/org/apache/solr/handler/admin/api/BackupCoreAPI.java:
##########
@@ -0,0 +1,255 @@
+/*
+ * 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.handler.admin.api;
+
+import static 
org.apache.solr.client.solrj.impl.BinaryResponseParser.BINARY_CONTENT_TYPE_V2;
+import static 
org.apache.solr.security.PermissionNameProvider.Name.COLL_EDIT_PERM;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import io.swagger.v3.oas.annotations.parameters.RequestBody;
+import java.net.URI;
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import javax.inject.Inject;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import org.apache.solr.api.JerseyResource;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.annotation.JsonProperty;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.core.CoreContainer;
+import org.apache.solr.core.SolrCore;
+import org.apache.solr.core.backup.BackupFilePaths;
+import org.apache.solr.core.backup.ShardBackupId;
+import org.apache.solr.core.backup.repository.BackupRepository;
+import org.apache.solr.handler.IncrementalShardBackup;
+import org.apache.solr.handler.SnapShooter;
+import org.apache.solr.jersey.JacksonReflectMapWriter;
+import org.apache.solr.jersey.PermissionName;
+import org.apache.solr.jersey.SolrJerseyResponse;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.response.SolrQueryResponse;
+
+@Path(("/cores/{coreName}/backup/{name}"))
+public class BackupCoreAPI extends JerseyResource {
+  private final SolrQueryResponse solrQueryResponse;
+  private final SolrQueryRequest solrQueryRequest;
+  private final CoreContainer coreContainer;
+
+  @Inject
+  public BackupCoreAPI(
+      CoreContainer coreContainer,
+      SolrQueryRequest solrQueryRequest,
+      SolrQueryResponse solrQueryResponse) {
+    this.solrQueryResponse = solrQueryResponse;
+    this.solrQueryRequest = solrQueryRequest;
+    this.coreContainer = coreContainer;
+  }
+
+  @POST
+  @Produces({"application/json", "application/xml", BINARY_CONTENT_TYPE_V2})
+  @PermissionName(COLL_EDIT_PERM)
+  public BackupCoreResponse createBackup(
+      @Schema(description = "The name of the core.") @PathParam("coreName") 
String coreName,
+      @Schema(description = "The backup will be created in a directory called 
snapshot.<name>")
+          @PathParam("name")
+          String name,
+      @Schema(description = "The POJO for representing additional backup 
params.") @RequestBody
+          BackupCoreRequestBody backupCoreRequestBody)
+      throws Exception {
+    final BackupCoreResponse backupCoreResponse =
+        instantiateJerseyResponse(BackupCoreResponse.class);
+    try (BackupRepository repository =
+            
coreContainer.newBackupRepository(backupCoreRequestBody.repository);
+        SolrCore core = coreContainer.getCore(coreName)) {
+      String location = 
repository.getBackupLocation(backupCoreRequestBody.location);
+      if (location == null) {
+        throw new SolrException(
+            SolrException.ErrorCode.BAD_REQUEST,
+            "'location' is not specified as a query"
+                + " parameter or as a default repository property");
+      }
+      URI locationUri = repository.createDirectoryURI(location);
+      repository.createDirectory(locationUri);
+
+      SnapShooter snapShooter =
+          new SnapShooter(repository, core, locationUri, name, 
backupCoreRequestBody.commitName);
+      // validateCreateSnapshot will create parent dirs instead of throw; that 
choice is dubious.
+      // But we want to throw. One reason is that this dir really should, in 
fact must, already
+      // exist here if triggered via a collection backup on a shared file 
system. Otherwise,
+      // perhaps the FS location isn't shared -- we want an error.
+      if 
(!snapShooter.getBackupRepository().exists(snapShooter.getLocation())) {
+        throw new SolrException(
+            SolrException.ErrorCode.BAD_REQUEST,
+            "Directory to contain snapshots doesn't exist: "
+                + snapShooter.getLocation()
+                + ". "
+                + "Note that Backup/Restore of a SolrCloud collection "
+                + "requires a shared file system mounted at the same path on 
all nodes!");
+      }
+      snapShooter.validateCreateSnapshot();
+      Map<String, Object> rsp = snapShooter.createSnapshot().toMap(new 
HashMap<>());
+      backupCoreResponse.directoryName = rsp.get("directoryName").toString();
+      backupCoreResponse.endTime = 
Instant.parse(rsp.get("endTime").toString());
+      backupCoreResponse.indexFileCount = 
Integer.parseInt(rsp.get("indexFileCount").toString());
+      backupCoreResponse.snapshotName = rsp.get("snapshotName").toString();
+      backupCoreResponse.status = rsp.get("status").toString();
+    }
+    return backupCoreResponse;
+  }
+
+  @Path("/incremental")

Review Comment:
   [-1] I appreciate what you were trying to do, but I think we should keep 
traditional and incremental backup creation together in the same endpoint.
   
   It's a great idea but the traditional backup format is already deprecated 
and should be going away for 10.0.  So soon we'll only have 1 backup format, 
and the 'incremental' signifier probably doesn't make much sense.
   
   Keeping the code separate seems like a great idea - I think it'd be awesome 
to keep the 'traditional' and 'incremental' internals in separate private 
methods here, but they should both be called from a single user-facing 
endpoint. 



##########
solr/core/src/java/org/apache/solr/handler/admin/api/BackupCoreAPI.java:
##########
@@ -0,0 +1,255 @@
+/*
+ * 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.handler.admin.api;
+
+import static 
org.apache.solr.client.solrj.impl.BinaryResponseParser.BINARY_CONTENT_TYPE_V2;
+import static 
org.apache.solr.security.PermissionNameProvider.Name.COLL_EDIT_PERM;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import io.swagger.v3.oas.annotations.parameters.RequestBody;
+import java.net.URI;
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import javax.inject.Inject;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import org.apache.solr.api.JerseyResource;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.annotation.JsonProperty;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.core.CoreContainer;
+import org.apache.solr.core.SolrCore;
+import org.apache.solr.core.backup.BackupFilePaths;
+import org.apache.solr.core.backup.ShardBackupId;
+import org.apache.solr.core.backup.repository.BackupRepository;
+import org.apache.solr.handler.IncrementalShardBackup;
+import org.apache.solr.handler.SnapShooter;
+import org.apache.solr.jersey.JacksonReflectMapWriter;
+import org.apache.solr.jersey.PermissionName;
+import org.apache.solr.jersey.SolrJerseyResponse;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.response.SolrQueryResponse;
+
+@Path(("/cores/{coreName}/backup/{name}"))
+public class BackupCoreAPI extends JerseyResource {
+  private final SolrQueryResponse solrQueryResponse;
+  private final SolrQueryRequest solrQueryRequest;
+  private final CoreContainer coreContainer;
+
+  @Inject
+  public BackupCoreAPI(
+      CoreContainer coreContainer,
+      SolrQueryRequest solrQueryRequest,
+      SolrQueryResponse solrQueryResponse) {
+    this.solrQueryResponse = solrQueryResponse;
+    this.solrQueryRequest = solrQueryRequest;
+    this.coreContainer = coreContainer;
+  }
+
+  @POST
+  @Produces({"application/json", "application/xml", BINARY_CONTENT_TYPE_V2})
+  @PermissionName(COLL_EDIT_PERM)
+  public BackupCoreResponse createBackup(
+      @Schema(description = "The name of the core.") @PathParam("coreName") 
String coreName,
+      @Schema(description = "The backup will be created in a directory called 
snapshot.<name>")
+          @PathParam("name")
+          String name,
+      @Schema(description = "The POJO for representing additional backup 
params.") @RequestBody
+          BackupCoreRequestBody backupCoreRequestBody)
+      throws Exception {
+    final BackupCoreResponse backupCoreResponse =
+        instantiateJerseyResponse(BackupCoreResponse.class);
+    try (BackupRepository repository =
+            
coreContainer.newBackupRepository(backupCoreRequestBody.repository);
+        SolrCore core = coreContainer.getCore(coreName)) {
+      String location = 
repository.getBackupLocation(backupCoreRequestBody.location);
+      if (location == null) {
+        throw new SolrException(
+            SolrException.ErrorCode.BAD_REQUEST,
+            "'location' is not specified as a query"
+                + " parameter or as a default repository property");
+      }
+      URI locationUri = repository.createDirectoryURI(location);
+      repository.createDirectory(locationUri);
+
+      SnapShooter snapShooter =
+          new SnapShooter(repository, core, locationUri, name, 
backupCoreRequestBody.commitName);
+      // validateCreateSnapshot will create parent dirs instead of throw; that 
choice is dubious.
+      // But we want to throw. One reason is that this dir really should, in 
fact must, already
+      // exist here if triggered via a collection backup on a shared file 
system. Otherwise,
+      // perhaps the FS location isn't shared -- we want an error.
+      if 
(!snapShooter.getBackupRepository().exists(snapShooter.getLocation())) {
+        throw new SolrException(
+            SolrException.ErrorCode.BAD_REQUEST,
+            "Directory to contain snapshots doesn't exist: "
+                + snapShooter.getLocation()
+                + ". "
+                + "Note that Backup/Restore of a SolrCloud collection "
+                + "requires a shared file system mounted at the same path on 
all nodes!");
+      }
+      snapShooter.validateCreateSnapshot();
+      Map<String, Object> rsp = snapShooter.createSnapshot().toMap(new 
HashMap<>());
+      backupCoreResponse.directoryName = rsp.get("directoryName").toString();
+      backupCoreResponse.endTime = 
Instant.parse(rsp.get("endTime").toString());
+      backupCoreResponse.indexFileCount = 
Integer.parseInt(rsp.get("indexFileCount").toString());
+      backupCoreResponse.snapshotName = rsp.get("snapshotName").toString();
+      backupCoreResponse.status = rsp.get("status").toString();
+    }
+    return backupCoreResponse;
+  }
+
+  @Path("/incremental")
+  @Produces({"application/json", "application/xml", BINARY_CONTENT_TYPE_V2})
+  @PermissionName(COLL_EDIT_PERM)
+  public IncrementalBackupCoreResponse createIncrementBackup(
+      @Schema(description = "The name of the core.") @PathParam("coreName") 
String coreName,
+      @Schema(description = "The backup will be created in a directory called 
snapshot.<name>")
+          @PathParam("name")
+          String name,
+      @Schema(description = "The POJO for representing additional backup 
params.") @RequestBody
+          BackupCoreRequestBody backupCoreRequestBody)
+      throws Exception {
+    final IncrementalBackupCoreResponse backupCoreResponse =
+        instantiateJerseyResponse(IncrementalBackupCoreResponse.class);
+    if (backupCoreRequestBody.shardBackupId == null) {
+      throw new SolrException(
+          SolrException.ErrorCode.BAD_REQUEST, "Missing required parameter: 
shardBackupId");
+    }
+    final ShardBackupId shardBackupId = 
ShardBackupId.from(backupCoreRequestBody.shardBackupId);
+    try (BackupRepository repository =
+            
coreContainer.newBackupRepository(backupCoreRequestBody.repository);
+        SolrCore core = coreContainer.getCore(coreName)) {
+      String location = 
repository.getBackupLocation(backupCoreRequestBody.location);
+      if (location == null) {
+        throw new SolrException(
+            SolrException.ErrorCode.BAD_REQUEST,
+            "'location' is not specified as a query"
+                + " parameter or as a default repository property");
+      }
+      URI locationUri = repository.createDirectoryURI(location);
+      repository.createDirectory(locationUri);
+      final ShardBackupId prevShardBackupId =
+          backupCoreRequestBody.prevShardBackupId != null
+              ? ShardBackupId.from(backupCoreRequestBody.prevShardBackupId)
+              : null;
+      BackupFilePaths incBackupFiles = new BackupFilePaths(repository, 
locationUri);
+      IncrementalShardBackup incSnapShooter =
+          new IncrementalShardBackup(
+              repository,
+              core,
+              incBackupFiles,
+              prevShardBackupId,
+              shardBackupId,
+              Optional.ofNullable(backupCoreRequestBody.commitName));
+      NamedList<Object> rsp = incSnapShooter.backup();
+      backupCoreResponse.endTime = 
Instant.parse(rsp.get("endTime").toString());
+      backupCoreResponse.indexSizeMB = 
Double.parseDouble(rsp.get("indexSizeMB").toString());
+      backupCoreResponse.indexFileCount = 
Integer.parseInt(rsp.get("indexFileCount").toString());
+      backupCoreResponse.startTime = 
Instant.parse(rsp.get("startTime").toString());
+      backupCoreResponse.shardBackupId = rsp.get("shardBackupId").toString();
+      backupCoreResponse.uploadedIndexFileCount =
+          Integer.parseInt(rsp.get("uploadedIndexFileCount").toString());
+      backupCoreResponse.uploadedIndexFileMB =
+          Double.parseDouble(rsp.get("uploadedIndexFileMB").toString());
+      if (rsp.get("shard") != null) backupCoreResponse.shard = 
rsp.get("shard").toString();
+    }
+    return backupCoreResponse;
+  }
+
+  public static class BackupCoreRequestBody implements JacksonReflectMapWriter 
{
+
+    @Schema(description = "The name of the repository to be used for e 
backup.")

Review Comment:
   [0] Typo: " for e backup"



##########
solr/core/src/test/org/apache/solr/handler/admin/api/BackupCoreAPIJerseyTest.java:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.handler.admin.api;
+
+import static org.apache.solr.SolrTestCaseJ4.assumeWorkingMockito;
+import static org.apache.solr.jersey.RequestContextKeys.SOLR_QUERY_RESPONSE;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import javax.inject.Singleton;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.container.ContainerRequestContext;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.core.CoreContainer;
+import org.apache.solr.jersey.CatchAllExceptionMapper;
+import org.apache.solr.jersey.InjectionFactories;
+import org.apache.solr.jersey.SolrJacksonMapper;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.request.SolrQueryRequestBase;
+import org.apache.solr.response.SolrQueryResponse;
+import org.glassfish.hk2.api.Factory;
+import org.glassfish.hk2.utilities.binding.AbstractBinder;
+import org.glassfish.jersey.process.internal.RequestScoped;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.test.JerseyTest;
+import org.junit.BeforeClass;
+
+public class BackupCoreAPIJerseyTest extends JerseyTest {

Review Comment:
   [Q] How would you describe the relationship between this test and 
BackupCoreAPITest below?  Are they both worth keeping longterm, or were you 
trying out different approaches before choosing one to move forward with?
   
   Do they both pass - I notice that there's some commented out code at points 
in this test in particular.  Can e.g. L142 be deleted and some of the `@Test` 
annotations be uncommented, or are those still serving a purpose here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org
For additional commands, e-mail: issues-h...@solr.apache.org


Reply via email to