Implemented handling of concurrent modification of objects

based on lastModified date
This commit is contained in:
Philipp Czora
2018-08-29 10:39:29 +02:00
parent 19f2c88768
commit 05a7e6c587
13 changed files with 103 additions and 73 deletions

View File

@@ -0,0 +1,15 @@
package sonia.scm.api.rest;
import sonia.scm.ConcurrentModificationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class ConcurrentModificationExceptionMapper implements ExceptionMapper<ConcurrentModificationException> {
@Override
public Response toResponse(ConcurrentModificationException exception) {
return Response.status(Response.Status.CONFLICT).build();
}
}

View File

@@ -4,12 +4,19 @@ import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import sonia.scm.group.Group;
import java.time.Instant;
@Mapper
public abstract class GroupDtoToGroupMapper {
@Mapping(target = "creationDate", ignore = true)
@Mapping(target = "lastModified", ignore = true)
public abstract Group map(GroupDto groupDto);
Long mapDate(Instant value) {
if (value != null) {
return value.toEpochMilli();
}
return null;
}
}

View File

@@ -3,6 +3,7 @@ package sonia.scm.api.v2.resources;
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import com.webcohesion.enunciate.metadata.rs.TypeHint;
import sonia.scm.ConcurrentModificationException;
import sonia.scm.NotFoundException;
import sonia.scm.group.Group;
import sonia.scm.group.GroupManager;
@@ -97,7 +98,7 @@ public class GroupResource {
@ResponseCode(code = 500, condition = "internal server error")
})
@TypeHint(TypeHint.NO_CONTENT.class)
public Response update(@PathParam("id") String name, @Valid GroupDto groupDto) throws NotFoundException {
public Response update(@PathParam("id") String name, @Valid GroupDto groupDto) throws NotFoundException, ConcurrentModificationException {
return adapter.update(name, existing -> dtoToGroupMapper.map(groupDto));
}
}

View File

@@ -2,6 +2,7 @@ package sonia.scm.api.v2.resources;
import de.otto.edison.hal.HalRepresentation;
import sonia.scm.AlreadyExistsException;
import sonia.scm.ConcurrentModificationException;
import sonia.scm.Manager;
import sonia.scm.ModelObject;
import sonia.scm.NotFoundException;
@@ -36,7 +37,7 @@ class IdResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
return singleAdapter.get(loadBy(id), mapToDto);
}
public Response update(String id, Function<MODEL_OBJECT, MODEL_OBJECT> applyChanges) throws NotFoundException {
public Response update(String id, Function<MODEL_OBJECT, MODEL_OBJECT> applyChanges) throws NotFoundException, ConcurrentModificationException {
return singleAdapter.update(
loadBy(id),
applyChanges,

View File

@@ -3,6 +3,7 @@ package sonia.scm.api.v2.resources;
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import com.webcohesion.enunciate.metadata.rs.TypeHint;
import sonia.scm.ConcurrentModificationException;
import sonia.scm.NotFoundException;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
@@ -126,7 +127,7 @@ public class RepositoryResource {
@ResponseCode(code = 500, condition = "internal server error")
})
@TypeHint(TypeHint.NO_CONTENT.class)
public Response update(@PathParam("namespace") String namespace, @PathParam("name") String name, @Valid RepositoryDto repositoryDto) throws NotFoundException {
public Response update(@PathParam("namespace") String namespace, @PathParam("name") String name, @Valid RepositoryDto repositoryDto) throws NotFoundException, ConcurrentModificationException {
return adapter.update(
loadBy(namespace, name),
existing -> dtoToRepositoryMapper.map(repositoryDto, existing.getId()),

View File

@@ -1,6 +1,7 @@
package sonia.scm.api.v2.resources;
import de.otto.edison.hal.HalRepresentation;
import sonia.scm.ConcurrentModificationException;
import sonia.scm.Manager;
import sonia.scm.ModelObject;
import sonia.scm.NotFoundException;
@@ -57,15 +58,23 @@ class SingleResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
* Update the model object for the given id according to the given function and returns a corresponding http response.
* This handles all corner cases, eg. no matching object for the id or missing privileges.
*/
public Response update(Supplier<Optional<MODEL_OBJECT>> reader, Function<MODEL_OBJECT, MODEL_OBJECT> applyChanges, Predicate<MODEL_OBJECT> hasSameKey) throws NotFoundException {
public Response update(Supplier<Optional<MODEL_OBJECT>> reader, Function<MODEL_OBJECT, MODEL_OBJECT> applyChanges, Predicate<MODEL_OBJECT> hasSameKey) throws NotFoundException, ConcurrentModificationException {
MODEL_OBJECT existingModelObject = reader.get().orElseThrow(NotFoundException::new);
MODEL_OBJECT changedModelObject = applyChanges.apply(existingModelObject);
if (!hasSameKey.test(changedModelObject)) {
return Response.status(BAD_REQUEST).entity("illegal change of id").build();
}
else if (modelObjectWasModifiedConcurrently(existingModelObject, changedModelObject)) {
throw new ConcurrentModificationException();
}
return update(getId(existingModelObject), changedModelObject);
}
private boolean modelObjectWasModifiedConcurrently(MODEL_OBJECT existing, MODEL_OBJECT updated) {
return (existing.getLastModified() != null && updated.getLastModified() != null)
&& (existing.getLastModified() > updated.getLastModified());
}
public Response delete(Supplier<Optional<MODEL_OBJECT>> reader) {
return reader.get()
.map(MODEL_OBJECT::getId)

View File

@@ -27,7 +27,6 @@ public class UserDto extends HalRepresentation {
@Pattern(regexp = "^[A-z0-9\\.\\-_@]|[^ ]([A-z0-9\\.\\-_@ ]*[A-z0-9\\.\\-_@]|[^ ])?$")
private String name;
private String password;
@NotEmpty
private String type;
private Map<String, String> properties;

View File

@@ -3,6 +3,7 @@ package sonia.scm.api.v2.resources;
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import com.webcohesion.enunciate.metadata.rs.TypeHint;
import sonia.scm.ConcurrentModificationException;
import sonia.scm.NotFoundException;
import sonia.scm.user.User;
import sonia.scm.user.UserManager;
@@ -97,7 +98,7 @@ public class UserResource {
@ResponseCode(code = 500, condition = "internal server error")
})
@TypeHint(TypeHint.NO_CONTENT.class)
public Response update(@PathParam("id") String name, @Valid UserDto userDto) throws NotFoundException {
public Response update(@PathParam("id") String name, @Valid UserDto userDto) throws NotFoundException, ConcurrentModificationException {
return adapter.update(name, existing -> dtoToUserMapper.map(userDto, existing.getPassword()));
}
}

View File

@@ -1,9 +1,9 @@
/**
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
*
* <p>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* <p>
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. 2. Redistributions in
* binary form must reproduce the above copyright notice, this list of
@@ -11,7 +11,7 @@
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
* nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* <p>
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -22,17 +22,12 @@
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* <p>
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository;
//~--- non-JDK imports --------------------------------------------------------
import com.github.sdorra.ssp.PermissionActionCheck;
import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
@@ -43,47 +38,29 @@ import sonia.scm.NotFoundException;
import java.util.Set;
//~--- JDK imports ------------------------------------------------------------
/**
*
* @author Sebastian Sdorra
*/
public final class HealthChecker
{
public final class HealthChecker {
/**
* the logger for HealthChecker
*/
private static final Logger logger =
LoggerFactory.getLogger(HealthChecker.class);
//~--- constructors ---------------------------------------------------------
private final Set<HealthCheck> checks;
private final RepositoryManager repositoryManager;
/**
* Constructs ...
*
*
* @param checks
* @param repositoryManager
*/
@Inject
public HealthChecker(Set<HealthCheck> checks,
RepositoryManager repositoryManager)
{
RepositoryManager repositoryManager) {
this.checks = checks;
this.repositoryManager = repositoryManager;
}
//~--- methods --------------------------------------------------------------
public void check(String id) throws NotFoundException {
RepositoryPermissions.healthCheck(id).check();
Repository repository = repositoryManager.get(id);
if (repository == null)
{
if (repository == null) {
throw new RepositoryNotFoundException(
"could not find repository with id ".concat(id));
}
@@ -98,27 +75,19 @@ public final class HealthChecker
doCheck(repository);
}
public void checkAll()
{
public void checkAll() {
logger.debug("check health of all repositories");
PermissionActionCheck<Repository> check = RepositoryPermissions.healthCheck();
for (Repository repository : repositoryManager.getAll())
{
if (check.isPermitted(repository))
{
try
{
for (Repository repository : repositoryManager.getAll()) {
if (check.isPermitted(repository)) {
try {
check(repository);
}
catch (ConcurrentModificationException | NotFoundException ex)
{
} catch (ConcurrentModificationException | NotFoundException ex) {
logger.error("health check ends with exception", ex);
}
}
else
{
} else {
logger.debug(
"no permissions to execute health check for repository {}",
repository.getId());
@@ -131,25 +100,20 @@ public final class HealthChecker
HealthCheckResult result = HealthCheckResult.healthy();
for (HealthCheck check : checks)
{
for (HealthCheck check : checks) {
logger.trace("execute health check {} for repository {}",
check.getClass(), repository.getName());
result = result.merge(check.check(repository));
}
if (result.isUnhealthy())
{
if (result.isUnhealthy()) {
logger.warn("repository {} is unhealthy: {}", repository.getName(),
result);
}
else
{
} else {
logger.info("repository {} is healthy", repository.getName());
}
if (!(repository.isHealthy() && result.isHealthy()))
{
if (!(repository.isHealthy() && result.isHealthy())) {
logger.trace("store health check results for repository {}",
repository.getName());
repository.setHealthCheckFailures(
@@ -158,11 +122,5 @@ public final class HealthChecker
}
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private final Set<HealthCheck> checks;
/** Field description */
private final RepositoryManager repositoryManager;
}