Skip to content

Commit

Permalink
Merge pull request #1298 from b2ihealthcare/issue/lock-api-timeout-pa…
Browse files Browse the repository at this point in the history
…rameter

feat(locks): support timeout api parameter with a default value of 3s
  • Loading branch information
cmark authored Jun 10, 2024
2 parents 719d89b + 1ca067c commit d5c04d6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.util.List;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;

import com.b2international.commons.StringUtils;
Expand All @@ -38,8 +40,6 @@ final class LockChangeRequest implements Request<ServiceProvider, Boolean> {

private static final long serialVersionUID = 414737555546463276L;

private static final long LOCK_TIMEOUT_MILLIS = 3000L;

private final boolean lock;

@NotEmpty
Expand All @@ -50,6 +50,10 @@ final class LockChangeRequest implements Request<ServiceProvider, Boolean> {

@NotEmpty
private final List<Lockable> targets;

@Min(0)
@Max(60_000) // maximum of 1 minute wait time is allowed, retry if timeout happens
private final Long timeout;

// Nullable
private String userId;
Expand All @@ -59,12 +63,14 @@ final class LockChangeRequest implements Request<ServiceProvider, Boolean> {
final String description,
final String parentDescription,
final String userId,
final Long timeout,
final List<Lockable> targets) {

this.lock = lock;
this.description = description;
this.parentDescription = parentDescription;
this.userId = userId;
this.timeout = timeout;
this.targets = targets;
}

Expand All @@ -78,7 +84,7 @@ public Boolean execute(final ServiceProvider context) throws LockedException, Il
final DatastoreLockContext lockContext = new DatastoreLockContext(userId, description, parentDescription);

if (lock) {
lockManager.lock(lockContext, LOCK_TIMEOUT_MILLIS, targets);
lockManager.lock(lockContext, timeout, targets);
} else {
lockManager.unlock(lockContext, targets);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2023 B2i Healthcare, https://b2ihealthcare.com
* Copyright 2022-2024 B2i Healthcare, https://b2ihealthcare.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,6 +37,7 @@ public class LockChangeRequestBuilder
private String description;
private String parentDescription = DatastoreLockContextDescriptions.ROOT;
private String userId;
private Long timeout = 3000L; // to keep backward compatibility in this API, 3 seconds of lock timeout is applied here
private List<Lockable> targets = List.of();


Expand All @@ -59,13 +60,18 @@ public LockChangeRequestBuilder setUserId(final String userId) {
return getSelf();
}

public LockChangeRequestBuilder setTimeout(Long timeout) {
this.timeout = timeout;
return getSelf();
}

public LockChangeRequestBuilder setTargets(final Lockable... targets) {
this.targets = Collections3.toImmutableList(targets);
return getSelf();
}

@Override
protected Request<ServiceProvider, Boolean> doBuild() {
return new LockChangeRequest(lock, description, parentDescription, userId, targets);
return new LockChangeRequest(lock, description, parentDescription, userId, timeout, targets);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,30 @@ final class ResourceLockChangeRequest implements Request<BranchContext, Boolean>
@NotEmpty
private final String parentDescription;

private final Long timeout;

// Nullable
private String userId;

/*package*/ ResourceLockChangeRequest(
final boolean lock,
final String description,
final String parentDescription,
final String parentDescription,
final Long timeout,
final String userId
) {
this.lock = lock;
this.description = description;
this.parentDescription = parentDescription;
this.timeout = timeout;
this.userId = userId;
}

@Override
public Boolean execute(final BranchContext context) throws LockedException, IllegalArgumentException {
final String repositoryId = context.service(Repository.class).id();
final List<Lockable> targets = List.of(new Lockable(repositoryId, context.path()));
final LockChangeRequest delegateLockRequest = new LockChangeRequest(lock, description, parentDescription, userId, targets);
final LockChangeRequest delegateLockRequest = new LockChangeRequest(lock, description, parentDescription, userId, timeout, targets);
return delegateLockRequest.execute(context);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 B2i Healthcare, https://b2ihealthcare.com
* Copyright 2023-2024 B2i Healthcare, https://b2ihealthcare.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,7 @@ public class ResourceLockChangeRequestBuilder
private String description;
private String parentDescription = DatastoreLockContextDescriptions.ROOT;
private String userId;
private Long timeout = 3000L; // to keep backward compatibility in this API, 3 seconds of lock timeout is applied here

/*package*/ ResourceLockChangeRequestBuilder(final boolean lock) {
this.lock = lock;
Expand All @@ -52,9 +53,14 @@ public ResourceLockChangeRequestBuilder setUserId(final String userId) {
this.userId = userId;
return getSelf();
}

public ResourceLockChangeRequestBuilder setTimeout(final Long timeout) {
this.timeout = timeout;
return getSelf();
}

@Override
protected Request<BranchContext, Boolean> doBuild() {
return new ResourceLockChangeRequest(lock, description, parentDescription, userId);
return new ResourceLockChangeRequest(lock, description, parentDescription, timeout, userId);
}
}

0 comments on commit d5c04d6

Please sign in to comment.