[New] Added pruning of old Locator locks
This commit is contained in:
parent
a93f3fc221
commit
941641f357
|
@ -16,14 +16,17 @@
|
||||||
package li.strolch.agent.impl;
|
package li.strolch.agent.impl;
|
||||||
|
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
import li.strolch.agent.api.LockHandler;
|
import li.strolch.agent.api.LockHandler;
|
||||||
|
import li.strolch.agent.api.StrolchAgent;
|
||||||
import li.strolch.agent.api.StrolchLockException;
|
import li.strolch.agent.api.StrolchLockException;
|
||||||
import li.strolch.model.Locator;
|
import li.strolch.model.Locator;
|
||||||
|
import li.strolch.utils.collections.TypedTuple;
|
||||||
import li.strolch.utils.dbc.DBC;
|
import li.strolch.utils.dbc.DBC;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -38,10 +41,11 @@ public class DefaultLockHandler implements LockHandler {
|
||||||
private String realm;
|
private String realm;
|
||||||
private TimeUnit tryLockTimeUnit;
|
private TimeUnit tryLockTimeUnit;
|
||||||
private long tryLockTime;
|
private long tryLockTime;
|
||||||
private Map<Locator, ReentrantLock> lockMap;
|
private Map<Locator, TypedTuple<ReentrantLock, Long>> lockMap;
|
||||||
|
|
||||||
public DefaultLockHandler(String realm, TimeUnit tryLockTimeUnit, long tryLockTime) {
|
public DefaultLockHandler(StrolchAgent agent, String realm, TimeUnit tryLockTimeUnit, long tryLockTime) {
|
||||||
|
|
||||||
|
DBC.PRE.assertNotNull("agent must be set!", agent); //$NON-NLS-1$
|
||||||
DBC.PRE.assertNotEmpty("Realm must be set!", realm); //$NON-NLS-1$
|
DBC.PRE.assertNotEmpty("Realm must be set!", realm); //$NON-NLS-1$
|
||||||
DBC.PRE.assertNotNull("TimeUnit must be set!", tryLockTimeUnit); //$NON-NLS-1$
|
DBC.PRE.assertNotNull("TimeUnit must be set!", tryLockTimeUnit); //$NON-NLS-1$
|
||||||
DBC.PRE.assertNotEquals("try lock time must not be 0", 0, tryLockTime); //$NON-NLS-1$
|
DBC.PRE.assertNotEquals("try lock time must not be 0", 0, tryLockTime); //$NON-NLS-1$
|
||||||
|
@ -50,6 +54,27 @@ public class DefaultLockHandler implements LockHandler {
|
||||||
this.tryLockTimeUnit = tryLockTimeUnit;
|
this.tryLockTimeUnit = tryLockTimeUnit;
|
||||||
this.tryLockTime = tryLockTime;
|
this.tryLockTime = tryLockTime;
|
||||||
this.lockMap = new ConcurrentHashMap<>();
|
this.lockMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
agent.getScheduledExecutor().scheduleAtFixedRate(this::cleanupOldLocks, 1, 1, TimeUnit.HOURS);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cleanupOldLocks() {
|
||||||
|
|
||||||
|
Map<Locator, TypedTuple<ReentrantLock, Long>> lockMap;
|
||||||
|
synchronized (this.lockMap) {
|
||||||
|
lockMap = new HashMap<>(this.lockMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
long maxAge = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1);
|
||||||
|
long count = 0;
|
||||||
|
for (Map.Entry<Locator, TypedTuple<ReentrantLock, Long>> entry : lockMap.entrySet()) {
|
||||||
|
if (!entry.getValue().getFirst().isLocked() && entry.getValue().getSecond() <= maxAge) {
|
||||||
|
this.lockMap.remove(entry.getKey());
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("Pruned " + count + " Locator locks.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRealm() {
|
public String getRealm() {
|
||||||
|
@ -58,13 +83,15 @@ public class DefaultLockHandler implements LockHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void lock(Locator locator) throws StrolchLockException {
|
public void lock(Locator locator) throws StrolchLockException {
|
||||||
ReentrantLock lock = this.lockMap.computeIfAbsent(locator, l -> new ReentrantLock(true));
|
TypedTuple<ReentrantLock, Long> tuple = this.lockMap
|
||||||
lock(this.tryLockTimeUnit, this.tryLockTime, lock, locator);
|
.computeIfAbsent(locator, l -> new TypedTuple<>(new ReentrantLock(true), System.currentTimeMillis()));
|
||||||
|
lock(this.tryLockTimeUnit, this.tryLockTime, tuple, locator);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void unlock(Locator locator) throws StrolchLockException {
|
public void unlock(Locator locator) throws StrolchLockException {
|
||||||
ReentrantLock lock = this.lockMap.get(locator);
|
TypedTuple<ReentrantLock, Long> tuple = this.lockMap.get(locator);
|
||||||
|
ReentrantLock lock = tuple.getFirst();
|
||||||
if (lock == null || !lock.isHeldByCurrentThread()) {
|
if (lock == null || !lock.isHeldByCurrentThread()) {
|
||||||
logger.error(MessageFormat.format("Trying to unlock not locked element {0}", locator)); //$NON-NLS-1$
|
logger.error(MessageFormat.format("Trying to unlock not locked element {0}", locator)); //$NON-NLS-1$
|
||||||
} else {
|
} else {
|
||||||
|
@ -75,7 +102,8 @@ public class DefaultLockHandler implements LockHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void releaseLock(Locator locator) throws StrolchLockException {
|
public void releaseLock(Locator locator) throws StrolchLockException {
|
||||||
ReentrantLock lock = this.lockMap.get(locator);
|
TypedTuple<ReentrantLock, Long> tuple = this.lockMap.get(locator);
|
||||||
|
ReentrantLock lock = tuple.getFirst();
|
||||||
if (lock == null) {
|
if (lock == null) {
|
||||||
logger.error(MessageFormat.format("Trying to unlock not locked element {0}", locator));
|
logger.error(MessageFormat.format("Trying to unlock not locked element {0}", locator));
|
||||||
} else if (!lock.isHeldByCurrentThread()) {
|
} else if (!lock.isHeldByCurrentThread()) {
|
||||||
|
@ -92,16 +120,18 @@ public class DefaultLockHandler implements LockHandler {
|
||||||
/**
|
/**
|
||||||
* @see java.util.concurrent.locks.ReentrantLock#tryLock(long, TimeUnit)
|
* @see java.util.concurrent.locks.ReentrantLock#tryLock(long, TimeUnit)
|
||||||
*/
|
*/
|
||||||
private void lock(TimeUnit timeUnit, long tryLockTime, ReentrantLock lock, Locator locator)
|
private void lock(TimeUnit timeUnit, long tryLockTime, TypedTuple<ReentrantLock, Long> tuple, Locator locator)
|
||||||
throws StrolchLockException {
|
throws StrolchLockException {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
if (!lock.tryLock(tryLockTime, timeUnit)) {
|
if (!tuple.getFirst().tryLock(tryLockTime, timeUnit)) {
|
||||||
String msg = "Failed to acquire lock after {0}s for {1}"; //$NON-NLS-1$
|
String msg = "Failed to acquire lock after {0}s for {1}"; //$NON-NLS-1$
|
||||||
msg = MessageFormat.format(msg, timeUnit.toSeconds(tryLockTime), locator);
|
msg = MessageFormat.format(msg, timeUnit.toSeconds(tryLockTime), locator);
|
||||||
throw new StrolchLockException(msg);
|
throw new StrolchLockException(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tuple.setSecond(System.currentTimeMillis());
|
||||||
|
|
||||||
// logger.debug("locked " + locator); //$NON-NLS-1$
|
// logger.debug("locked " + locator); //$NON-NLS-1$
|
||||||
|
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|
|
@ -75,7 +75,7 @@ public class DefaultRealmHandler extends StrolchComponent implements RealmHandle
|
||||||
@Override
|
@Override
|
||||||
public void setup(ComponentConfiguration configuration) {
|
public void setup(ComponentConfiguration configuration) {
|
||||||
|
|
||||||
this.realms = new HashMap<>();
|
this.realms = new HashMap<>(1);
|
||||||
String[] realms = configuration.getStringArray(PROP_REALMS, StrolchConstants.DEFAULT_REALM);
|
String[] realms = configuration.getStringArray(PROP_REALMS, StrolchConstants.DEFAULT_REALM);
|
||||||
for (String realmName : realms) {
|
for (String realmName : realms) {
|
||||||
|
|
||||||
|
|
|
@ -15,31 +15,19 @@
|
||||||
*/
|
*/
|
||||||
package li.strolch.agent.impl;
|
package li.strolch.agent.impl;
|
||||||
|
|
||||||
import static li.strolch.agent.impl.DefaultRealmHandler.PROP_ENABLED_DELAYED_OBSERVER_UPDATES;
|
import static li.strolch.agent.impl.DefaultRealmHandler.*;
|
||||||
import static li.strolch.agent.impl.DefaultRealmHandler.PROP_ENABLE_AUDIT_TRAIL;
|
|
||||||
import static li.strolch.agent.impl.DefaultRealmHandler.PROP_ENABLE_AUDIT_TRAIL_FOR_READ;
|
|
||||||
import static li.strolch.agent.impl.DefaultRealmHandler.PROP_ENABLE_OBSERVER_UPDATES;
|
|
||||||
import static li.strolch.agent.impl.DefaultRealmHandler.PROP_ENABLE_VERSIONING;
|
|
||||||
import static li.strolch.runtime.StrolchConstants.makeRealmKey;
|
import static li.strolch.runtime.StrolchConstants.makeRealmKey;
|
||||||
|
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import li.strolch.agent.api.*;
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import li.strolch.agent.api.ActivityMap;
|
|
||||||
import li.strolch.agent.api.AuditTrail;
|
|
||||||
import li.strolch.agent.api.ComponentContainer;
|
|
||||||
import li.strolch.agent.api.LockHandler;
|
|
||||||
import li.strolch.agent.api.ObserverHandler;
|
|
||||||
import li.strolch.agent.api.OrderMap;
|
|
||||||
import li.strolch.agent.api.ResourceMap;
|
|
||||||
import li.strolch.agent.api.StrolchRealm;
|
|
||||||
import li.strolch.model.Locator;
|
import li.strolch.model.Locator;
|
||||||
import li.strolch.privilege.model.PrivilegeContext;
|
import li.strolch.privilege.model.PrivilegeContext;
|
||||||
import li.strolch.runtime.configuration.ComponentConfiguration;
|
import li.strolch.runtime.configuration.ComponentConfiguration;
|
||||||
import li.strolch.utils.dbc.DBC;
|
import li.strolch.utils.dbc.DBC;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
|
@ -115,7 +103,7 @@ public abstract class InternalStrolchRealm implements StrolchRealm {
|
||||||
String propTryLockTime = makeRealmKey(this.realm, PROP_TRY_LOCK_TIME);
|
String propTryLockTime = makeRealmKey(this.realm, PROP_TRY_LOCK_TIME);
|
||||||
TimeUnit timeUnit = TimeUnit.valueOf(configuration.getString(propTryLockTimeUnit, TimeUnit.SECONDS.name()));
|
TimeUnit timeUnit = TimeUnit.valueOf(configuration.getString(propTryLockTimeUnit, TimeUnit.SECONDS.name()));
|
||||||
long time = configuration.getLong(propTryLockTime, 10L);
|
long time = configuration.getLong(propTryLockTime, 10L);
|
||||||
this.lockHandler = new DefaultLockHandler(this.realm, timeUnit, time);
|
this.lockHandler = new DefaultLockHandler(this.container.getAgent(), this.realm, timeUnit, time);
|
||||||
|
|
||||||
// versioning
|
// versioning
|
||||||
String enableVersioningKey = makeRealmKey(getRealm(), PROP_ENABLE_VERSIONING);
|
String enableVersioningKey = makeRealmKey(getRealm(), PROP_ENABLE_VERSIONING);
|
||||||
|
|
Loading…
Reference in New Issue