Search in sources :

Example 31 with AtomicAction

use of com.arjuna.ats.arjuna.AtomicAction in project narayana by jbosstm.

the class OptimisticLockUnitTest method testConflict.

public void testConflict() throws Exception {
    Container<Atomic> theContainer1 = new Container<Atomic>();
    Container<Atomic> theContainer2 = new Container<Atomic>();
    final Atomic obj1 = theContainer1.create(new ExampleSTM());
    AtomicAction act = new AtomicAction();
    /*
         * Make sure there's state on "disk" before we try to do anything
         * with a shared instance.
         * 
         * https://issues.jboss.org/browse/JBTM-1732
         */
    act.begin();
    obj1.set(10);
    act.commit();
    final Atomic obj2 = theContainer2.clone(new ExampleSTM(), obj1);
    AtomicAction a = new AtomicAction();
    a.begin();
    obj1.set(1234);
    AtomicAction.suspend();
    AtomicAction b = new AtomicAction();
    b.begin();
    obj2.set(12345);
    b.commit();
    AtomicAction.resume(a);
    assertEquals(a.commit(), ActionStatus.ABORTED);
    AtomicAction c = new AtomicAction();
    c.begin();
    assertEquals(obj1.get(), 12345);
    assertEquals(obj2.get(), 12345);
    c.commit();
    System.out.println("done");
}
Also used : AtomicAction(com.arjuna.ats.arjuna.AtomicAction)

Example 32 with AtomicAction

use of com.arjuna.ats.arjuna.AtomicAction in project narayana by jbosstm.

the class InvocationHandler method invoke.

public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws Throwable {
    if (_txObject == null)
        throw new LockException("Transactional object is null!");
    AtomicAction currentTx = null;
    synchronized (_txObject) {
        synchronized (_theObject) {
            AtomicAction act = null;
            if (_nestedTransactions) {
                act = new AtomicAction();
                act.begin();
            } else {
                if (_nestedTopLevel) {
                    act = new TopLevelAction();
                    act.begin();
                }
            }
            try {
                LockInformation cachedLock = _cachedMethods.get(method);
                if (BasicAction.Current() != null) {
                    Method theMethod = null;
                    /*
                         * Look for the corresponding method in the original object and
                         * check the annotations applied there.
                         * 
                         * Check to see if we've cached this before.
                         */
                    int lockType = -1;
                    boolean lockFree = false;
                    boolean transactionFree = false;
                    if (cachedLock == null) {
                        for (Method mt : _methods) {
                            if (mt.getName().equals(method.getName())) {
                                if (mt.getReturnType().equals(method.getReturnType())) {
                                    if (Arrays.equals(mt.getParameterTypes(), method.getParameterTypes()))
                                        theMethod = mt;
                                }
                            }
                        }
                        if (theMethod == null)
                            throw new LockException("Could not locate method " + method);
                        if (theMethod.isAnnotationPresent(ReadLock.class))
                            lockType = LockMode.READ;
                        else {
                            if (theMethod.isAnnotationPresent(WriteLock.class))
                                lockType = LockMode.WRITE;
                            else {
                                if (theMethod.isAnnotationPresent(TransactionFree.class))
                                    transactionFree = true;
                                else {
                                    if (theMethod.isAnnotationPresent(LockFree.class))
                                        lockFree = true;
                                }
                            }
                        }
                        if (!lockFree && !transactionFree) {
                            int timeout = LockManager.defaultSleepTime;
                            int retry = LockManager.defaultRetry;
                            if (theMethod.isAnnotationPresent(Timeout.class))
                                timeout = theMethod.getAnnotation(Timeout.class).period();
                            if (theMethod.isAnnotationPresent(Retry.class))
                                retry = theMethod.getAnnotation(Retry.class).count();
                            if (// default to WRITE
                            lockType == -1)
                                lockType = LockMode.WRITE;
                            cachedLock = new LockInformation(lockType, timeout, retry);
                            _cachedMethods.put(method, cachedLock);
                        } else {
                            if (transactionFree)
                                currentTx = AtomicAction.suspend();
                        }
                    }
                    if (!lockFree && !transactionFree) {
                        int result = _txObject.setlock((_optimistic ? new OptimisticLock(cachedLock._lockType) : new Lock(cachedLock._lockType)), cachedLock._retry, cachedLock._timeout);
                        if (result != GRANTED) {
                            throw new LockException(Thread.currentThread() + " could not set " + LockMode.stringForm(cachedLock._lockType) + " lock. Got: " + LockResult.stringForm(result));
                        }
                    }
                }
                try {
                    return method.invoke(_theObject, args);
                } catch (InvocationTargetException e) {
                    if (txojLogger.logger.isTraceEnabled()) {
                        Throwable ae = e.getCause() != null ? e.getCause() : e;
                        txojLogger.logger.tracef("STM InvocationHandler::invoke application method %s threw exception %s", method.getName(), ae.getMessage());
                    }
                    throw e.getCause() != null ? e.getCause() : e;
                }
            } finally {
                if (act != null) {
                    int status = act.commit();
                    if ((status != ActionStatus.COMMITTED) && (status != ActionStatus.COMMITTING)) {
                        if (currentTx != null)
                            AtomicAction.resume(currentTx);
                        throw new TransactionException("Failed to commit container transaction!", status);
                    }
                }
                if (currentTx != null)
                    AtomicAction.resume(currentTx);
            }
        }
    }
}
Also used : TopLevelAction(com.arjuna.ats.arjuna.TopLevelAction) Timeout(org.jboss.stm.annotations.Timeout) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) OptimisticLock(org.jboss.stm.internal.optimistic.OptimisticLock) Lock(com.arjuna.ats.txoj.Lock) ReadLock(org.jboss.stm.annotations.ReadLock) WriteLock(org.jboss.stm.annotations.WriteLock) AtomicAction(com.arjuna.ats.arjuna.AtomicAction) TransactionException(org.jboss.stm.TransactionException) LockException(org.jboss.stm.LockException) Retry(org.jboss.stm.annotations.Retry) OptimisticLock(org.jboss.stm.internal.optimistic.OptimisticLock)

Example 33 with AtomicAction

use of com.arjuna.ats.arjuna.AtomicAction in project narayana by jbosstm.

the class TypicalExampleUnitTest method test.

public void test() {
    MyExample ex = new MyExample(10);
    Container<Sample> theContainer = new Container<Sample>();
    Sample obj1 = theContainer.create(ex);
    Sample obj2 = theContainer.clone(new MyExample(), obj1);
    assertTrue(obj2 != null);
    AtomicAction act = new AtomicAction();
    act.begin();
    obj1.increment();
    act.commit();
    act = new AtomicAction();
    act.begin();
    assertEquals(obj2.value(), 11);
    act.commit();
    act = new AtomicAction();
    act.begin();
    for (int i = 0; i < 1000; i++) {
        obj1.increment();
    }
    act.abort();
    assertEquals(ex._numberOfTransactions, 1002);
}
Also used : AtomicAction(com.arjuna.ats.arjuna.AtomicAction)

Example 34 with AtomicAction

use of com.arjuna.ats.arjuna.AtomicAction in project narayana by jbosstm.

the class AtomicArrayUnitTest method testTransaction.

@SuppressWarnings("unchecked")
public void testTransaction() {
    AtomicAction act = new AtomicAction();
    AtomicArray<Integer> a1 = ArrayFactory.instance().createArray(10);
    assertEquals(a1.size(), 10);
    for (int i = 0; i < a1.size(); i++) a1.set(i, 0);
    act.begin();
    a1.set(0, 1);
    a1.set(1, 2);
    assertTrue(a1.get(0).equals(1));
    assertTrue(a1.get(1).equals(2));
    act.abort();
    assertTrue(a1.get(0).equals(0));
    assertTrue(a1.get(1).equals(0));
}
Also used : AtomicAction(com.arjuna.ats.arjuna.AtomicAction)

Example 35 with AtomicAction

use of com.arjuna.ats.arjuna.AtomicAction in project narayana by jbosstm.

the class Worker004 method run.

/**
 * The main method of the class that will perform the work.
 */
public void run() {
    expectedValue = new int[mNumberOfResources];
    for (int j = 0; j < mNumberOfResources; j++) {
        expectedValue[j] = 0;
    }
    try {
        for (int j = 0; j < mNumberOfResources; j++) {
            for (int i = 0; i < mMaxIteration; i++) {
                // start transaction
                AtomicAction a = new AtomicAction();
                a.begin();
                int incValue = mLockRecordList[j].increase();
                if (i % 2 == 0) {
                    a.commit();
                    expectedValue[j] += incValue;
                } else {
                    a.abort();
                }
            }
        }
        for (int j = 0; j < mNumberOfResources; j++) {
            for (int i = 0; i < mMaxIteration; i++) {
                AtomicAction b = new AtomicAction();
                b.begin();
                int incValue = mLockRecordList[j].increase();
                if (i % 2 == 0) {
                    b.commit();
                    expectedValue[j] += incValue;
                } else {
                    b.abort();
                }
            }
        }
    } catch (Exception e) {
        mCorrect = false;
        qautil.debug("exception in worker001: ", e);
    }
}
Also used : AtomicAction(com.arjuna.ats.arjuna.AtomicAction)

Aggregations

AtomicAction (com.arjuna.ats.arjuna.AtomicAction)179 Test (org.junit.Test)73 Uid (com.arjuna.ats.arjuna.common.Uid)31 TestException (com.hp.mwtests.ts.txoj.common.exceptions.TestException)30 AtomicObject (com.hp.mwtests.ts.txoj.common.resources.AtomicObject)18 Lock (com.arjuna.ats.txoj.Lock)17 RecoverableContainer (org.jboss.stm.internal.RecoverableContainer)8 OutputObjectState (com.arjuna.ats.arjuna.state.OutputObjectState)7 PrintWriter (java.io.PrintWriter)7 InputObjectState (com.arjuna.ats.arjuna.state.InputObjectState)5 ExtendedObject (com.hp.mwtests.ts.arjuna.resources.ExtendedObject)5 Container (org.jboss.stm.Container)5 RecoverAtomicAction (com.arjuna.ats.arjuna.recovery.RecoverAtomicAction)4 BasicRecord (com.hp.mwtests.ts.arjuna.resources.BasicRecord)4 TopLevelAction (com.arjuna.ats.arjuna.TopLevelAction)3 AbstractRecord (com.arjuna.ats.arjuna.coordinator.AbstractRecord)3 BasicAction (com.arjuna.ats.arjuna.coordinator.BasicAction)3 RecoveryModule (com.arjuna.ats.arjuna.recovery.RecoveryModule)3 LogStore (com.arjuna.ats.internal.arjuna.objectstore.LogStore)3 EditableAtomicAction (com.arjuna.ats.internal.arjuna.tools.log.EditableAtomicAction)3