Search in sources :

Example 6 with Lock

use of org.jumpmind.symmetric.model.Lock in project symmetric-ds by JumpMind.

the class ClusterService method lockShared.

protected boolean lockShared(final String action) {
    final Date timeout = DateUtils.addMilliseconds(new Date(), (int) -parameterService.getLong(ParameterConstants.LOCK_TIMEOUT_MS));
    if (isClusteringEnabled()) {
        return sqlTemplate.update(getSql("acquireSharedLockSql"), new Object[] { TYPE_SHARED, getServerId(), new Date(), action, TYPE_SHARED, timeout }) == 1;
    } else {
        Lock lock = lockCache.get(action);
        if (lock != null) {
            synchronized (lock) {
                if ((lock.getLockType().equals(TYPE_SHARED) || lock.getLockTime() == null || lock.getLockTime().before(timeout)) && (lock.isSharedEnable() || lock.getSharedCount() == 0)) {
                    lock.setLockType(TYPE_SHARED);
                    lock.setLockingServerId(getServerId());
                    lock.setLockTime(new Date());
                    if (lock.getSharedCount() == 0) {
                        lock.setSharedEnable(true);
                    }
                    lock.setSharedCount(lock.getSharedCount() + 1);
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : Date(java.util.Date) Lock(org.jumpmind.symmetric.model.Lock)

Example 7 with Lock

use of org.jumpmind.symmetric.model.Lock in project symmetric-ds by JumpMind.

the class ClusterService method unlockCluster.

protected boolean unlockCluster(String action, String serverId) {
    if (isClusteringEnabled()) {
        return sqlTemplate.update(getSql("releaseClusterLockSql"), new Object[] { action, TYPE_CLUSTER, serverId }) > 0;
    } else {
        Lock lock = lockCache.get(action);
        if (lock != null) {
            synchronized (lock) {
                if (lock.getLockType().equals(TYPE_CLUSTER) && serverId.equals(lock.getLockingServerId())) {
                    lock.setLastLockingServerId(lock.getLockingServerId());
                    lock.setLockingServerId(null);
                    lock.setLastLockTime(lock.getLockTime());
                    lock.setLockTime(null);
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : Lock(org.jumpmind.symmetric.model.Lock)

Example 8 with Lock

use of org.jumpmind.symmetric.model.Lock in project symmetric-ds by JumpMind.

the class ClusterService method unlockShared.

protected boolean unlockShared(final String action) {
    if (isClusteringEnabled()) {
        return sqlTemplate.update(getSql("releaseSharedLockSql"), new Object[] { action, TYPE_SHARED }) == 1;
    } else {
        Lock lock = lockCache.get(action);
        if (lock != null) {
            synchronized (lock) {
                if (lock.getLockType().equals(TYPE_SHARED)) {
                    lock.setLastLockTime(lock.getLockTime());
                    lock.setLastLockingServerId(lock.getLockingServerId());
                    if (lock.getSharedCount() == 1) {
                        lock.setSharedEnable(false);
                        lock.setLockingServerId(null);
                        lock.setLockTime(null);
                    }
                    if (lock.getSharedCount() > 1) {
                        lock.setSharedCount(lock.getSharedCount() - 1);
                    } else {
                        lock.setSharedCount(0);
                    }
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : Lock(org.jumpmind.symmetric.model.Lock)

Example 9 with Lock

use of org.jumpmind.symmetric.model.Lock in project symmetric-ds by JumpMind.

the class ClusterService method isInfiniteLocked.

public boolean isInfiniteLocked(String action) {
    Map<String, Lock> locks = findLocks();
    Lock lock = locks.get(action);
    if (lock != null && lock.getLockTime() != null && new Date().before(lock.getLockTime()) && Lock.STOPPED.equals(lock.getLockingServerId())) {
        return true;
    } else {
        return false;
    }
}
Also used : Date(java.util.Date) Lock(org.jumpmind.symmetric.model.Lock)

Example 10 with Lock

use of org.jumpmind.symmetric.model.Lock in project symmetric-ds by JumpMind.

the class AbstractClusterServiceTest method checkLock.

private Lock checkLock(String action, String lockType, int expectedSharedCount, boolean expectedSharedEnable) {
    Lock lock = getClusterService().findLocks().get(action);
    Assert.assertEquals(lockType, lock.getLockType());
    Assert.assertNotNull(lock.getLockingServerId());
    Assert.assertNotNull(lock.getLockTime());
    Assert.assertEquals(expectedSharedCount, lock.getSharedCount());
    if (expectedSharedCount > 0) {
        Assert.assertEquals(expectedSharedEnable, lock.isSharedEnable());
    }
    return lock;
}
Also used : Lock(org.jumpmind.symmetric.model.Lock)

Aggregations

Lock (org.jumpmind.symmetric.model.Lock)18 Date (java.util.Date)4 IOException (java.io.IOException)2 IoException (org.jumpmind.exception.IoException)2 File (java.io.File)1 FileWriter (java.io.FileWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 FileAlterationObserver (org.apache.commons.io.monitor.FileAlterationObserver)1 ConcurrencySqlException (org.jumpmind.db.sql.ConcurrencySqlException)1 Row (org.jumpmind.db.sql.Row)1 SymmetricException (org.jumpmind.symmetric.SymmetricException)1 DirectorySnapshot (org.jumpmind.symmetric.file.DirectorySnapshot)1 FileConflictException (org.jumpmind.symmetric.file.FileConflictException)1 FileTriggerFileModifiedListener (org.jumpmind.symmetric.file.FileTriggerFileModifiedListener)1 FileModifiedCallback (org.jumpmind.symmetric.file.FileTriggerFileModifiedListener.FileModifiedCallback)1 IJob (org.jumpmind.symmetric.job.IJob)1 IJobManager (org.jumpmind.symmetric.job.IJobManager)1 FileTriggerRouter (org.jumpmind.symmetric.model.FileTriggerRouter)1 Monitor (org.jumpmind.symmetric.model.Monitor)1