Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(locks): support timeout api parameter with a default value of 3s #1298

Merged
merged 1 commit into from
Jun 10, 2024
Merged
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
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 @@
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

Check warning on line 40 in core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/locks/request/LockChangeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/locks/request/LockChangeRequestBuilder.java#L40

Added line #L40 was not covered by tests
private List<Lockable> targets = List.of();


Expand All @@ -59,13 +60,18 @@
return getSelf();
}

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

Check warning on line 65 in core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/locks/request/LockChangeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/locks/request/LockChangeRequestBuilder.java#L64-L65

Added lines #L64 - L65 were not covered by tests
}

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);

Check warning on line 75 in core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/locks/request/LockChangeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/locks/request/LockChangeRequestBuilder.java#L75

Added line #L75 was not covered by tests
}
}
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 @@
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 @@
this.userId = userId;
return getSelf();
}

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

Check warning on line 59 in core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/locks/request/ResourceLockChangeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/locks/request/ResourceLockChangeRequestBuilder.java#L58-L59

Added lines #L58 - L59 were not covered by tests
}

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