Skip to content

Commit c99169e

Browse files
authored
Merge pull request #69 from Simon-Initiative/AUTHORING-2268-lock-timeout
take a more passive approach to lock management
2 parents 1a1feaa + 80fb6b4 commit c99169e

3 files changed

Lines changed: 18 additions & 40 deletions

File tree

src/main/java/edu/cmu/oli/content/configuration/Configurations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class Configurations {
2222
private String webContentVolume;
2323
private String themesRepository;
2424
private int transactionRetrys = 3;
25-
private int editLockMaxDuration = 300000;
25+
private int editLockMaxDuration = 1000 * 60 * 5; // 5 minutes
2626
private JsonObject developerAdmin;
2727
private JsonObject namespaces;
2828
private JsonArray themes;

src/main/java/edu/cmu/oli/content/controllers/LockControllerImpl.java

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import javax.enterprise.inject.Instance;
1717
import javax.inject.Inject;
1818
import java.util.Map;
19-
import java.util.Timer;
20-
import java.util.TimerTask;
2119
import java.util.concurrent.ConcurrentHashMap;
2220

2321
import static javax.ws.rs.core.Response.Status.FORBIDDEN;
@@ -36,37 +34,31 @@ public class LockControllerImpl implements LockController {
3634
@ConfigurationCache
3735
Instance<Configurations> configuration;
3836

39-
// :FIXME: avoid use of global state, not good for horizontal scaling. Switch to using a distributed cache
4037
private Map<String, ResourceEditLock> resourceLocks;
4138

4239
@PostConstruct
4340
void init() {
4441
resourceLocks = new ConcurrentHashMap<>();
45-
long sleepTimeInMilli = 1000L * 60 * 5; // 5 minutes
46-
Timer timer = new Timer(true);
47-
TimerTask timerTask = new TimerTask() {
48-
@Override
49-
public void run() {
50-
lockListCleanup();
51-
}
52-
};
53-
timer.scheduleAtFixedRate(timerTask, sleepTimeInMilli, sleepTimeInMilli);
5442
}
5543

5644
@Override
5745
public ResourceEditLock aquire(AppSecurityContext session, String resourceId) {
46+
5847
ResourceEditLock resourceEditLock = resourceLocks.get(resourceId);
59-
// if resourceEditLock has expired create new one
60-
if (resourceEditLock != null) {
61-
if (System.currentTimeMillis() - resourceEditLock.getLockedAt() > configuration.get().getEditLockMaxDuration()) {
62-
//Old ResourceEditLock has expired
63-
resourceEditLock = new ResourceEditLock(configuration.get().getEditLockMaxDuration(), resourceId, session.getPreferredUsername(), System.currentTimeMillis());
64-
resourceLocks.put(resourceId, resourceEditLock);
65-
}
66-
} else {
67-
resourceEditLock = new ResourceEditLock(configuration.get().getEditLockMaxDuration(), resourceId, session.getPreferredUsername(), System.currentTimeMillis());
48+
49+
// Update the lock if either:
50+
//
51+
// 1. One does not exist for this resource
52+
// 2. The user name of an existing lock matches the current user name
53+
// 3. The lock has expired
54+
if (resourceEditLock == null
55+
|| resourceEditLock.getLockedBy().equalsIgnoreCase(session.getPreferredUsername())
56+
|| System.currentTimeMillis() - resourceEditLock.getLockedAt() > configuration.get().getEditLockMaxDuration()) {
57+
58+
resourceEditLock = new ResourceEditLock(resourceId, session.getPreferredUsername(), System.currentTimeMillis());
6859
resourceLocks.put(resourceId, resourceEditLock);
6960
}
61+
7062
return resourceEditLock;
7163
}
7264

@@ -103,7 +95,7 @@ public ResourceEditLock getLockForResource(AppSecurityContext session, String re
10395
ResourceEditLock resourceEditLock = resourceLocks.get(resourceId);
10496
// if resourceEditLock has expired create new one
10597
if (resourceEditLock == null || doCreate) {
106-
resourceEditLock = new ResourceEditLock(configuration.get().getEditLockMaxDuration(), resourceId, session.getPreferredUsername(), System.currentTimeMillis());
98+
resourceEditLock = new ResourceEditLock(resourceId, session.getPreferredUsername(), System.currentTimeMillis());
10799
resourceLocks.put(resourceId, resourceEditLock);
108100
}
109101
return resourceEditLock;
@@ -133,13 +125,4 @@ public void checkLockPermission(AppSecurityContext session, String resourceId, b
133125
}
134126
}
135127

136-
private void lockListCleanup() {
137-
resourceLocks.forEach((key, value) -> {
138-
long timeDiff = System.currentTimeMillis() - value.getLockedAt();
139-
if (timeDiff > configuration.get().getEditLockMaxDuration()) {
140-
log.debug("lock cleanup time diff " + timeDiff + " lock" + value.toString());
141-
resourceLocks.remove(key, value);
142-
}
143-
});
144-
}
145128
}

src/main/java/edu/cmu/oli/content/models/ResourceEditLock.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
*/
99
public final class ResourceEditLock {
1010

11-
@Expose
12-
long lockMaxDuration = 1000L * 60L * 5L; // 5 minutes
13-
1411
@Expose
1512
String lockId;
1613

@@ -24,24 +21,22 @@ public final class ResourceEditLock {
2421
@Expose
2522
long lockedAt;
2623

27-
public ResourceEditLock(long lockMaxDuration, String resourceId, String lockedBy, long lockedAt) {
24+
public ResourceEditLock(String resourceId, String lockedBy, long lockedAt) {
2825
this.lockId = AppUtils.generateGUID();
29-
this.lockMaxDuration = lockMaxDuration;
3026
this.resourceId = resourceId;
3127
this.lockedBy = lockedBy;
3228
this.lockedAt = lockedAt;
3329
}
3430

35-
public ResourceEditLock(String lockId, long lockMaxDuration, String resourceId, String lockedBy, long lockedAt) {
31+
public ResourceEditLock(String lockId, String resourceId, String lockedBy, long lockedAt) {
3632
this.lockId = lockId;
37-
this.lockMaxDuration = lockMaxDuration;
3833
this.resourceId = resourceId;
3934
this.lockedBy = lockedBy;
4035
this.lockedAt = lockedAt;
4136
}
4237

4338
public ResourceEditLock withUpdatedLockedAt(long lockedAt) {
44-
return new ResourceEditLock(this.lockId, this.lockMaxDuration, this.resourceId, this.lockedBy, lockedAt);
39+
return new ResourceEditLock(this.lockId, this.resourceId, this.lockedBy, lockedAt);
4540
}
4641

4742
public String getLockId() {

0 commit comments

Comments
 (0)