Search in sources :

Example 1 with LockRecord

use of com.arjuna.ats.internal.txoj.abstractrecords.LockRecord in project narayana by jbosstm.

the class OptimisticLockManager method setlock.

/*
     * Some of these methods are copied directly from LockManager and do 99% the same thing.
     * The only way they differ is with the types of Locks or LockRecords that they create.
     * Probably should refactor later to reduce the amount of copied code.
     */
/*
     * All of this is here to prevent us grabbing a copy of the state of the object when we lock it.
     * Optimistic and pessimistic locks cannot be used in the same transaction on the same object.
     * 
     * WARNING HERE BE DRAGONS.
     * 
     * (non-Javadoc)
     * @see com.arjuna.ats.txoj.LockManager#setlock(com.arjuna.ats.txoj.Lock, int, int)
     */
public int setlock(Lock toSet, int retry, int sleepTime) {
    if (txojLogger.logger.isTraceEnabled()) {
        txojLogger.logger.trace("OptimisticLockManager::setlock(" + toSet + ", " + retry + ", " + sleepTime + ")");
    }
    int conflict = ConflictType.CONFLICT;
    int returnStatus = LockResult.REFUSED;
    LockRecord newLockR = null;
    boolean modifyRequired = false;
    BasicAction currAct = null;
    if (toSet == null) {
        txojLogger.i18NLogger.warn_LockManager_2();
        return LockResult.REFUSED;
    }
    if (!(toSet instanceof OptimisticLock))
        return LockResult.REFUSED;
    initialise();
    currAct = BasicAction.Current();
    if (currAct != null) {
        ActionHierarchy ah = currAct.getHierarchy();
        if (ah != null)
            toSet.changeHierarchy(ah);
        else {
            txojLogger.i18NLogger.warn_LockManager_3();
            toSet = null;
            return LockResult.REFUSED;
        }
    }
    if (super.loadObjectState())
        super.setupStore();
    while ((conflict == ConflictType.CONFLICT) && ((retry >= 0) || ((retry == LockManager.waitTotalTimeout) && (sleepTime > 0)))) {
        Object syncObject = ((currAct == null) ? getMutex() : currAct);
        synchronized (super.lockStore.getClass()) {
            synchronized (syncObject) {
                synchronized (locksHeldLockObject) {
                    conflict = ConflictType.CONFLICT;
                    if (loadState()) {
                        conflict = lockConflict(toSet);
                    } else {
                        txojLogger.i18NLogger.warn_LockManager_4();
                    }
                    if (conflict != ConflictType.CONFLICT) {
                        /*
                             * When here the conflict was resolved or the retry limit
                             * expired.
                             */
                        /* no conflict so set lock */
                        modifyRequired = toSet.modifiesObject();
                        if (super.activate()) {
                            returnStatus = LockResult.GRANTED;
                            if (conflict == ConflictType.COMPATIBLE) {
                                int lrStatus = AddOutcome.AR_ADDED;
                                if (currAct != null) {
                                    /* add new lock record to action list */
                                    newLockR = new OptimisticLockRecord(this, (modifyRequired ? false : true), currAct, true);
                                    if ((lrStatus = currAct.add(newLockR)) != AddOutcome.AR_ADDED) {
                                        newLockR = null;
                                        if (lrStatus == AddOutcome.AR_REJECTED) {
                                            returnStatus = LockResult.REFUSED;
                                        }
                                    }
                                }
                                if (returnStatus == LockResult.GRANTED) {
                                    locksHeld.insert(toSet);
                                /*
                                                                  * add to local lock
                                                                  * list
                                                                  */
                                }
                            } else {
                                if (modifyRequired)
                                    returnStatus = LockResult.GRANTED;
                            }
                        } else {
                            /* activate failed - refuse request */
                            txojLogger.i18NLogger.warn_LockManager_5();
                            returnStatus = LockResult.REFUSED;
                        }
                    }
                    if ((returnStatus == LockResult.GRANTED) && (conflict == ConflictType.COMPATIBLE)) {
                        if (!unloadState()) {
                            txojLogger.i18NLogger.warn_LockManager_6();
                            returnStatus = LockResult.REFUSED;
                        }
                    } else
                        freeState();
                    if (returnStatus == LockResult.GRANTED) {
                        if (modifyRequired) {
                            if (super.modified()) {
                                hasBeenLocked = true;
                            } else {
                                conflict = ConflictType.CONFLICT;
                                returnStatus = LockResult.REFUSED;
                            }
                        }
                    }
                    if (conflict == ConflictType.CONFLICT)
                        freeState();
                }
            }
        }
        if (conflict == ConflictType.CONFLICT) {
            if (retry != 0) {
                if (sleepTime > 0) {
                    sleepTime -= conflictManager.wait(retry, sleepTime);
                } else
                    retry = 0;
            }
            if (retry != LockManager.waitTotalTimeout)
                retry--;
        }
    }
    return returnStatus;
}
Also used : LockRecord(com.arjuna.ats.internal.txoj.abstractrecords.LockRecord)

Example 2 with LockRecord

use of com.arjuna.ats.internal.txoj.abstractrecords.LockRecord in project narayana by jbosstm.

the class LockManager method setlock.

/*
     * This is the main user visible operation. Attempts to set the given lock
     * on the current object. If lock cannot be set, then the lock attempt is
     * retried retry times before giving up and returning an error. This gives a
     * simple handle on deadlock.
     * 
     * sleepTime is milliseconds.
     * 
     * @return <code>LockResult</code> indicating outcome.
     */
public int setlock(Lock toSet, int retry, int sleepTime) {
    if (txojLogger.logger.isTraceEnabled()) {
        txojLogger.logger.trace("LockManager::setlock(" + toSet + ", " + retry + ", " + sleepTime + ")");
    }
    int returnStatus = LockResult.REFUSED;
    // JBTM-2098, we need to have the action locked in case a simultaneous abort
    // is issued which would try to lock the mutex before we can call modified
    Object toLock = BasicAction.Current();
    if (toLock == null) {
        toLock = new Object();
    }
    synchronized (toLock) {
        if (!lockMutex())
            return LockResult.REFUSED;
        try {
            int conflict = ConflictType.CONFLICT;
            LockRecord newLockR = null;
            boolean modifyRequired = false;
            BasicAction currAct = null;
            if (toSet == null) {
                txojLogger.i18NLogger.warn_LockManager_2();
                return LockResult.REFUSED;
            }
            currAct = BasicAction.Current();
            if (currAct != null) {
                ActionHierarchy ah = currAct.getHierarchy();
                if (ah != null)
                    toSet.changeHierarchy(ah);
                else {
                    txojLogger.i18NLogger.warn_LockManager_3();
                    toSet = null;
                    return LockResult.REFUSED;
                }
            }
            if (super.loadObjectState())
                super.setupStore();
            while ((conflict == ConflictType.CONFLICT) && ((retry >= 0) || ((retry == LockManager.waitTotalTimeout) && (sleepTime > 0)))) {
                synchronized (locksHeldLockObject) {
                    conflict = ConflictType.CONFLICT;
                    if (loadState()) {
                        conflict = lockConflict(toSet);
                    } else {
                        txojLogger.i18NLogger.warn_LockManager_4();
                    }
                    if (conflict != ConflictType.CONFLICT) {
                        /*
                         * When here the conflict was resolved or the retry limit
                         * expired.
                         */
                        /* no conflict so set lock */
                        modifyRequired = toSet.modifiesObject();
                        if (super.activate()) {
                            returnStatus = LockResult.GRANTED;
                            if (conflict == ConflictType.COMPATIBLE) {
                                int lrStatus = AddOutcome.AR_ADDED;
                                if (currAct != null) {
                                    /* add new lock record to action list */
                                    newLockR = new LockRecord(this, (modifyRequired ? false : true), currAct);
                                    if ((lrStatus = currAct.add(newLockR)) != AddOutcome.AR_ADDED) {
                                        newLockR = null;
                                        if (lrStatus == AddOutcome.AR_REJECTED)
                                            returnStatus = LockResult.REFUSED;
                                    }
                                }
                                if (returnStatus == LockResult.GRANTED) {
                                    locksHeld.insert(toSet);
                                /*
                                     * add to local lock
                                     * list
                                     */
                                }
                            }
                        } else {
                            /* activate failed - refuse request */
                            txojLogger.i18NLogger.warn_LockManager_5();
                            returnStatus = LockResult.REFUSED;
                        }
                    }
                    if ((returnStatus == LockResult.GRANTED) && (conflict == ConflictType.COMPATIBLE)) {
                        if (!unloadState()) {
                            txojLogger.i18NLogger.warn_LockManager_6();
                            returnStatus = LockResult.REFUSED;
                        }
                    } else
                        freeState();
                    if (returnStatus == LockResult.GRANTED) {
                        if (modifyRequired) {
                            if (super.modified())
                                hasBeenLocked = true;
                            else {
                                conflict = ConflictType.CONFLICT;
                                returnStatus = LockResult.REFUSED;
                            }
                        }
                    }
                    if (conflict == ConflictType.CONFLICT)
                        freeState();
                }
                if (conflict == ConflictType.CONFLICT) {
                    if (retry != 0) {
                        if (sleepTime > 0) {
                            sleepTime -= conflictManager.wait(retry, sleepTime);
                        } else
                            retry = 0;
                    }
                    if (retry != LockManager.waitTotalTimeout)
                        retry--;
                }
            }
        } finally {
            unlockMutex();
        }
    }
    return returnStatus;
}
Also used : BasicAction(com.arjuna.ats.arjuna.coordinator.BasicAction) CadaverLockRecord(com.arjuna.ats.internal.txoj.abstractrecords.CadaverLockRecord) LockRecord(com.arjuna.ats.internal.txoj.abstractrecords.LockRecord) ActionHierarchy(com.arjuna.ats.arjuna.coordinator.ActionHierarchy)

Example 3 with LockRecord

use of com.arjuna.ats.internal.txoj.abstractrecords.LockRecord in project narayana by jbosstm.

the class LockRecordUnitTest method test.

@Test
public void test() {
    LockRecord lr = new LockRecord();
    assertEquals(lr.lockType(), null);
    assertTrue(lr.save_state(new OutputObjectState(), ObjectType.ANDPERSISTENT));
    assertFalse(lr.restore_state(new InputObjectState(), ObjectType.ANDPERSISTENT));
    assertEquals(lr.value(), null);
    lr.setValue(null);
    assertEquals(lr.nestedAbort(), TwoPhaseOutcome.FINISH_ERROR);
    assertEquals(lr.nestedCommit(), TwoPhaseOutcome.FINISH_ERROR);
    assertEquals(lr.topLevelAbort(), TwoPhaseOutcome.FINISH_ERROR);
    assertEquals(lr.topLevelCommit(), TwoPhaseOutcome.FINISH_ERROR);
    lr = new LockRecord(new AtomicObject(), new AtomicAction());
    assertTrue(lr.toString() != null);
    lr.print(new PrintWriter(new ByteArrayOutputStream()));
    assertTrue(lr.type() != null);
    lr.merge(null);
    lr.alter(null);
}
Also used : InputObjectState(com.arjuna.ats.arjuna.state.InputObjectState) AtomicAction(com.arjuna.ats.arjuna.AtomicAction) LockRecord(com.arjuna.ats.internal.txoj.abstractrecords.LockRecord) OutputObjectState(com.arjuna.ats.arjuna.state.OutputObjectState) AtomicObject(com.hp.mwtests.ts.txoj.common.resources.AtomicObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 4 with LockRecord

use of com.arjuna.ats.internal.txoj.abstractrecords.LockRecord in project narayana by jbosstm.

the class CadaverUnitTest method testBasic.

@Test
public void testBasic() throws Exception {
    AtomicAction A = new AtomicAction();
    AtomicObject B = new AtomicObject();
    A.begin();
    CadaverLockRecord clr = new CadaverLockRecord(null, B, A);
    LockRecord lr = new LockRecord(B, A);
    assertTrue(clr.type() != null);
    clr.print(new PrintWriter(new ByteArrayOutputStream()));
    clr.replace(lr);
    A.abort();
}
Also used : AtomicAction(com.arjuna.ats.arjuna.AtomicAction) CadaverLockRecord(com.arjuna.ats.internal.txoj.abstractrecords.CadaverLockRecord) CadaverLockRecord(com.arjuna.ats.internal.txoj.abstractrecords.CadaverLockRecord) LockRecord(com.arjuna.ats.internal.txoj.abstractrecords.LockRecord) AtomicObject(com.hp.mwtests.ts.txoj.common.resources.AtomicObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

LockRecord (com.arjuna.ats.internal.txoj.abstractrecords.LockRecord)4 AtomicAction (com.arjuna.ats.arjuna.AtomicAction)2 CadaverLockRecord (com.arjuna.ats.internal.txoj.abstractrecords.CadaverLockRecord)2 AtomicObject (com.hp.mwtests.ts.txoj.common.resources.AtomicObject)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintWriter (java.io.PrintWriter)2 Test (org.junit.Test)2 ActionHierarchy (com.arjuna.ats.arjuna.coordinator.ActionHierarchy)1 BasicAction (com.arjuna.ats.arjuna.coordinator.BasicAction)1 InputObjectState (com.arjuna.ats.arjuna.state.InputObjectState)1 OutputObjectState (com.arjuna.ats.arjuna.state.OutputObjectState)1