gerlowskija commented on code in PR #1119: URL: https://github.com/apache/solr/pull/1119#discussion_r1319865106
########## solr/core/src/java/org/apache/solr/handler/admin/api/SnapshotBackupAPI.java: ########## @@ -0,0 +1,174 @@ +/* + * 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.handler.ReplicationHandler.ERR_STATUS; +import static org.apache.solr.security.PermissionNameProvider.Name.CORE_EDIT_PERM; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.parameters.RequestBody; +import java.lang.invoke.MethodHandles; +import java.util.function.Consumer; +import javax.inject.Inject; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import org.apache.solr.api.JerseyResource; +import org.apache.solr.client.api.model.SolrJerseyResponse; +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.SolrCore; +import org.apache.solr.handler.ReplicationHandler; +import org.apache.solr.handler.ReplicationHandler.ReplicationHandlerConfig; +import org.apache.solr.jersey.JacksonReflectMapWriter; +import org.apache.solr.jersey.PermissionName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** V2 endpoint for Backup API used for User-Managed clusters and Single-Node Installation. */ +@Path("/cores/{coreName}/replication/backups") +public class SnapshotBackupAPI extends JerseyResource { Review Comment: [+1] Thanks for updating the path here to match what we lined up in the spreadsheet! [-0] There are a LOT of different backup or snapshot adjacent APIs: `/admin/collections?action=BACKUP`, `/admin/cores?action=BACKUP`, `/admin/collections?action=CREATESNAPSHOT`, `/admin/cores?action=CREATESNAPSHOT`, `/replication?cmd=backup`, etc. With so many similar APIs, it's really hard to find names to differentiate them all. (Tbh I'm not even sure what the differences are between the ReplicationHandle backup functionality and what's exposed via `/admin/collections` and `/admin/cores`.) But if you've got any sparks of inspiration there and can come up with a name that helps distinguish this API from its close cousins, we should definitely use that here. (As a larger question, I think we should re-evaluate which of these functionalities is worth keeping around in 10.0. So maybe this disambiguation question is only a short-term concern. But still 🤷 ) ########## solr/core/src/test/org/apache/solr/handler/admin/api/SnapshotBackupAPITest.java: ########## @@ -0,0 +1,106 @@ +/* + * 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.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import org.apache.solr.common.SolrException; +import org.apache.solr.core.SolrCore; +import org.apache.solr.handler.ReplicationHandler.ReplicationHandlerConfig; +import org.apache.solr.jersey.InjectionFactories; +import org.apache.solr.jersey.SolrJacksonMapper; +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; +import org.junit.Test; + +/** Unit tests for {@link SnapshotBackupAPI}. */ +public class SnapshotBackupAPITest extends JerseyTest { + + private SolrCore solrCore; + private ReplicationHandlerConfig replicationHandlerConfig; + + @BeforeClass + public static void ensureWorkingMockito() { + assumeWorkingMockito(); + } + + @Override + protected Application configure() { + resetMocks(); + final ResourceConfig config = new ResourceConfig(); + config.register(SnapshotBackupAPI.class); + config.register(SolrJacksonMapper.class); + config.register(SolrExceptionTestMapper.class); + config.register( + new AbstractBinder() { + @Override + protected void configure() { + bindFactory(new InjectionFactories.SingletonFactory<>(solrCore)) + .to(SolrCore.class) + .in(RequestScoped.class); + } + }); + config.register( + new AbstractBinder() { + @Override + protected void configure() { + bindFactory(new InjectionFactories.SingletonFactory<>(replicationHandlerConfig)) + .to(ReplicationHandlerConfig.class) + .in(RequestScoped.class); + } + }); + return config; + } + + @Test + public void testSuccessfulBackupCommand() throws Exception { Review Comment: [-0] Does this test name make sense, if the backup (intentionally and correctly) comes back with a 400? [0] I think I agree with something you said elsewhere, that it'd be worth adding some input-validation unit tests somewhere. But nbd if you disagree on that at this point. ########## solr/core/src/java/org/apache/solr/handler/admin/api/SnapshotBackupAPI.java: ########## @@ -0,0 +1,174 @@ +/* + * 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.handler.ReplicationHandler.ERR_STATUS; +import static org.apache.solr.security.PermissionNameProvider.Name.CORE_EDIT_PERM; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.parameters.RequestBody; +import java.lang.invoke.MethodHandles; +import java.util.function.Consumer; +import javax.inject.Inject; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import org.apache.solr.api.JerseyResource; +import org.apache.solr.client.api.model.SolrJerseyResponse; +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.SolrCore; +import org.apache.solr.handler.ReplicationHandler; +import org.apache.solr.handler.ReplicationHandler.ReplicationHandlerConfig; +import org.apache.solr.jersey.JacksonReflectMapWriter; +import org.apache.solr.jersey.PermissionName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** V2 endpoint for Backup API used for User-Managed clusters and Single-Node Installation. */ +@Path("/cores/{coreName}/replication/backups") +public class SnapshotBackupAPI extends JerseyResource { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + private final SolrCore solrCore; + private final ReplicationHandlerConfig replicationHandlerConfig; + + @Inject + public SnapshotBackupAPI(SolrCore solrCore, ReplicationHandlerConfig replicationHandlerConfig) { + this.solrCore = solrCore; + this.replicationHandlerConfig = replicationHandlerConfig; + } + + /** + * This API (POST /api/cores/coreName/replication/backups {...}) is analogous to the v1 + * /solr/coreName/replication?command=backup + */ + @POST + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, BINARY_CONTENT_TYPE_V2}) + @Operation(summary = "Backup command using ReplicationHandler") + @PermissionName(CORE_EDIT_PERM) + public BackupReplicationResponse createBackup( + @RequestBody BackupReplicationRequestBody backupReplicationPayload) throws Exception { + if (backupReplicationPayload == null) { Review Comment: [0] It comes up often enough that maybe it's worth moving this check-null-and-throw to be a protected method on `JerseyResource` similar to the existing `ensureRequiredParameterProvided`. ########## solr/core/src/java/org/apache/solr/handler/admin/api/SnapshotBackupAPI.java: ########## @@ -0,0 +1,174 @@ +/* + * 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.handler.ReplicationHandler.ERR_STATUS; +import static org.apache.solr.security.PermissionNameProvider.Name.CORE_EDIT_PERM; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.parameters.RequestBody; +import java.lang.invoke.MethodHandles; +import java.util.function.Consumer; +import javax.inject.Inject; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import org.apache.solr.api.JerseyResource; +import org.apache.solr.client.api.model.SolrJerseyResponse; +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.SolrCore; +import org.apache.solr.handler.ReplicationHandler; +import org.apache.solr.handler.ReplicationHandler.ReplicationHandlerConfig; +import org.apache.solr.jersey.JacksonReflectMapWriter; +import org.apache.solr.jersey.PermissionName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** V2 endpoint for Backup API used for User-Managed clusters and Single-Node Installation. */ +@Path("/cores/{coreName}/replication/backups") +public class SnapshotBackupAPI extends JerseyResource { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + private final SolrCore solrCore; + private final ReplicationHandlerConfig replicationHandlerConfig; + + @Inject + public SnapshotBackupAPI(SolrCore solrCore, ReplicationHandlerConfig replicationHandlerConfig) { + this.solrCore = solrCore; + this.replicationHandlerConfig = replicationHandlerConfig; + } + + /** + * This API (POST /api/cores/coreName/replication/backups {...}) is analogous to the v1 + * /solr/coreName/replication?command=backup + */ + @POST + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, BINARY_CONTENT_TYPE_V2}) + @Operation(summary = "Backup command using ReplicationHandler") + @PermissionName(CORE_EDIT_PERM) + public BackupReplicationResponse createBackup( + @RequestBody BackupReplicationRequestBody backupReplicationPayload) throws Exception { + if (backupReplicationPayload == null) { + throw new SolrException( + SolrException.ErrorCode.BAD_REQUEST, "Required request-body is missing"); + } + ReplicationHandler replicationHandler = + (ReplicationHandler) solrCore.getRequestHandler(ReplicationHandler.PATH); + return doBackup(replicationHandler, backupReplicationPayload); + } + + private BackupReplicationResponse doBackup( + ReplicationHandler replicationHandler, + BackupReplicationRequestBody backupReplicationPayload) { + BackupReplicationResponse response = instantiateJerseyResponse(BackupReplicationResponse.class); + int numberToKeep = backupReplicationPayload.numberToKeep; + int numberBackupsToKeep = replicationHandlerConfig.getNumberBackupsToKeep(); + String location = backupReplicationPayload.location; + String repoName = backupReplicationPayload.repository; + String commitName = backupReplicationPayload.commitName; + String name = backupReplicationPayload.name; + Consumer<NamedList<?>> resultConsumer = result -> response.result = result; + try { + ReplicationHandler.doSnapShoot( Review Comment: [0] It'd prob be easier to test this API in `SnapshotBackupAPITest` if the underling functionality lived in some helper class (that I guess was injected into the ctor). Not sure it's worth the hassle and noise of refactoring that all, but figured I'd mention it here in case you liked the idea 🤷 ########## solr/core/src/java/org/apache/solr/handler/admin/api/SnapshotBackupAPI.java: ########## @@ -0,0 +1,174 @@ +/* + * 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.handler.ReplicationHandler.ERR_STATUS; +import static org.apache.solr.security.PermissionNameProvider.Name.CORE_EDIT_PERM; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.parameters.RequestBody; +import java.lang.invoke.MethodHandles; +import java.util.function.Consumer; +import javax.inject.Inject; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import org.apache.solr.api.JerseyResource; +import org.apache.solr.client.api.model.SolrJerseyResponse; +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.SolrCore; +import org.apache.solr.handler.ReplicationHandler; +import org.apache.solr.handler.ReplicationHandler.ReplicationHandlerConfig; +import org.apache.solr.jersey.JacksonReflectMapWriter; +import org.apache.solr.jersey.PermissionName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** V2 endpoint for Backup API used for User-Managed clusters and Single-Node Installation. */ +@Path("/cores/{coreName}/replication/backups") +public class SnapshotBackupAPI extends JerseyResource { Review Comment: [+1] Thanks for updating the path here to match what we lined up in the spreadsheet! [-0] There are a LOT of different backup or snapshot adjacent APIs: `/admin/collections?action=BACKUP`, `/admin/cores?action=BACKUP`, `/admin/collections?action=CREATESNAPSHOT`, `/admin/cores?action=CREATESNAPSHOT`, `/replication?cmd=backup`, etc. With so many similar APIs, it's really hard to find names to differentiate them all. (Tbh I'm not even sure what the differences are between the ReplicationHandle backup functionality and what's exposed via `/admin/collections` and `/admin/cores`.) But if you've got any sparks of inspiration there and can come up with a name that helps distinguish this API from its close cousins, we should definitely use that here. (As a larger question, I think we should re-evaluate which of these functionalities is worth keeping around in 10.0. So maybe this disambiguation question is only a short-term concern. But still 🤷 ) ########## solr/core/src/java/org/apache/solr/handler/admin/api/SnapshotBackupAPI.java: ########## @@ -0,0 +1,174 @@ +/* + * 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.handler.ReplicationHandler.ERR_STATUS; +import static org.apache.solr.security.PermissionNameProvider.Name.CORE_EDIT_PERM; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.parameters.RequestBody; +import java.lang.invoke.MethodHandles; +import java.util.function.Consumer; +import javax.inject.Inject; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import org.apache.solr.api.JerseyResource; +import org.apache.solr.client.api.model.SolrJerseyResponse; +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.SolrCore; +import org.apache.solr.handler.ReplicationHandler; +import org.apache.solr.handler.ReplicationHandler.ReplicationHandlerConfig; +import org.apache.solr.jersey.JacksonReflectMapWriter; +import org.apache.solr.jersey.PermissionName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** V2 endpoint for Backup API used for User-Managed clusters and Single-Node Installation. */ +@Path("/cores/{coreName}/replication/backups") +public class SnapshotBackupAPI extends JerseyResource { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + private final SolrCore solrCore; + private final ReplicationHandlerConfig replicationHandlerConfig; + + @Inject + public SnapshotBackupAPI(SolrCore solrCore, ReplicationHandlerConfig replicationHandlerConfig) { + this.solrCore = solrCore; + this.replicationHandlerConfig = replicationHandlerConfig; + } + + /** + * This API (POST /api/cores/coreName/replication/backups {...}) is analogous to the v1 + * /solr/coreName/replication?command=backup + */ + @POST + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, BINARY_CONTENT_TYPE_V2}) + @Operation(summary = "Backup command using ReplicationHandler") + @PermissionName(CORE_EDIT_PERM) + public BackupReplicationResponse createBackup( + @RequestBody BackupReplicationRequestBody backupReplicationPayload) throws Exception { + if (backupReplicationPayload == null) { + throw new SolrException( + SolrException.ErrorCode.BAD_REQUEST, "Required request-body is missing"); + } + ReplicationHandler replicationHandler = + (ReplicationHandler) solrCore.getRequestHandler(ReplicationHandler.PATH); + return doBackup(replicationHandler, backupReplicationPayload); + } + + private BackupReplicationResponse doBackup( + ReplicationHandler replicationHandler, + BackupReplicationRequestBody backupReplicationPayload) { + BackupReplicationResponse response = instantiateJerseyResponse(BackupReplicationResponse.class); + int numberToKeep = backupReplicationPayload.numberToKeep; + int numberBackupsToKeep = replicationHandlerConfig.getNumberBackupsToKeep(); + String location = backupReplicationPayload.location; + String repoName = backupReplicationPayload.repository; + String commitName = backupReplicationPayload.commitName; + String name = backupReplicationPayload.name; + Consumer<NamedList<?>> resultConsumer = result -> response.result = result; + try { + ReplicationHandler.doSnapShoot( Review Comment: [0] It'd prob be easier to test this API in `SnapshotBackupAPITest` if the underling functionality lived in some helper class (that I guess was injected into the ctor). Not sure it's worth the hassle and noise of refactoring that all, but figured I'd mention it here in case you liked the idea 🤷 -- 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