rapture.lock.mongodb.MongoLockHandler Maven / Gradle / Ivy
/**
* The MIT License (MIT)
*
* Copyright (c) 2011-2016 Incapture Technologies LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package rapture.lock.mongodb;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.bson.BsonInt32;
import org.bson.Document;
import com.mongodb.BasicDBList;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.UpdateOptions;
import rapture.common.LockHandle;
import rapture.lock.ILockingHandler;
import rapture.mongodb.MongoDBFactory;
/**
* An implementation of a lock strategy on Mongo. The idea is that we need to
* acquire a named lock, and then we release it later. If we fail to release it,
* it will automatically become available after secondsToHold.
*
* The lock in mongo is implemented by attempting to create a document that has
* the following characteristics
*
* { name = lockName, locked=true, freeTime=(some long time) }
*
* @author amkimian
*
*/
public class MongoLockHandler implements ILockingHandler {
private static final String RELEASETIME = "rt";
private static final String DOLLARPUSH = "$push";
private static final String DOLLARPULL = "$pull";
private static final String NAME = "name";
private static final String CTX = "ctx";
private static final String LOCKS = "locks";
private static final String RANDOM = "rnd";
private static Logger log = Logger.getLogger(MongoLockHandler.class);
private static final String tableName = "raplock";
private String instanceName = "default";
@Override
public void setInstanceName(String instanceName) {
this.instanceName = instanceName;
}
private boolean waitAndShouldBail(long bailTime) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
//
}
if (System.currentTimeMillis() > bailTime) {
return true;
}
return false;
}
@Override
public LockHandle acquireLock(String lockHolder, String lockName, long secondsToWait, long secondsToHold) {
log.trace("Mongo acquire lock");
// create a random marker for each acquisition -- that way double-grabs
// from the same session are rejected
String random = makeRandom();
Document query = getLockQuery(lockName);
Document val = createLockVal(lockHolder, secondsToHold, random);
Document update = createAddValObject(val);
// First see if this exists
MongoCollection coll = getLockCollection();
long bailTime = System.currentTimeMillis() + secondsToWait * 1000;
boolean gotLock = false;
coll.updateOne(query, update, new UpdateOptions().upsert(true));
while (!gotLock) {
Document obj = coll.find(query).first();
// Look for the locks field, and see if we are top
if (obj != null) {
log.trace("Locks are present");
List © 2015 - 2025 Weber Informatics LLC | Privacy Policy