Search in sources :

Example 1 with TransactionData

use of com.arjuna.ats.internal.arjuna.objectstore.LogInstance.TransactionData in project narayana by jbosstm.

the class LogStore method read_state.

/**
 * Shouldn't be called during normal execution only during recovery.
 */
protected InputObjectState read_state(Uid u, String tn, int s) throws ObjectStoreException {
    /*
           * In case of asynchronous removals of state, let's trigger the purger
           * thread to flush its cache now. Try to avoid false positives during
           * recovery wherever possible!
           */
    _purger.trigger();
    /*
           * It's possible that recovery got hold of a state id while it was
           * being deleted (marker written and pruning thread not yet active).
           * In which case when it comes to do a read it's not going to find
           * the state there any longer. Conversely it's possible that it could do
           * a read on a state that is about to be deleted. Recovery should be
           * able to cope with these edge cases.
           */
    TransactionData td = getLogName(u, tn, -1);
    if (td == null)
        throw new ObjectStoreException();
    ArrayList<InputObjectState> states = scanLog(td.container.getName(), tn);
    if ((states == null) || (states.size() == 0))
        return null;
    for (int i = 0; i < states.size(); i++) {
        if (states.get(i).stateUid().equals(u))
            return states.get(i);
    }
    return null;
}
Also used : ObjectStoreException(com.arjuna.ats.arjuna.exceptions.ObjectStoreException) InputObjectState(com.arjuna.ats.arjuna.state.InputObjectState) TransactionData(com.arjuna.ats.internal.arjuna.objectstore.LogInstance.TransactionData)

Example 2 with TransactionData

use of com.arjuna.ats.internal.arjuna.objectstore.LogInstance.TransactionData in project narayana by jbosstm.

the class LogStore method addTxId.

public final TransactionData addTxId(Uid txId, long size) {
    TransactionData td = new TransactionData(txId, _used, this);
    // allow multiple entries in the same log
    _transactions.add(td);
    _ids.put(txId, txId);
    _used += size;
    return td;
}
Also used : TransactionData(com.arjuna.ats.internal.arjuna.objectstore.LogInstance.TransactionData)

Example 3 with TransactionData

use of com.arjuna.ats.internal.arjuna.objectstore.LogInstance.TransactionData in project narayana by jbosstm.

the class LogStore method write_state.

/**
 * write_state saves the ObjectState in a file named by the type and Uid of
 * the ObjectState. If the second argument is SHADOW, then the file name is
 * different so that a subsequent commit_state invocation will rename the
 * file.
 *
 * We need to make sure that each entry is written to the next empty location
 * in the log even if there's already an entry for this tx.
 */
protected boolean write_state(Uid objUid, String tName, OutputObjectState state, int ft) throws ObjectStoreException {
    if (tsLogger.logger.isTraceEnabled()) {
        tsLogger.logger.trace("ShadowingStore.write_state(" + objUid + ", " + tName + ", " + StateType.stateTypeString(ft) + ")");
    }
    String fname = null;
    File fd = null;
    if (tName != null) {
        int imageSize = (int) state.length();
        byte[] uidString = objUid.stringForm().getBytes(StandardCharsets.UTF_8);
        // don't put in endOfLog since we keep overwriting that.
        int buffSize = _redzone.length + uidString.length + imageSize + 8;
        RandomAccessFile ofile = null;
        java.nio.channels.FileLock lock = null;
        if (imageSize > 0) {
            // always adds entry to log
            TransactionData theLogEntry = getLogName(objUid, tName, buffSize);
            LogInstance theLog = theLogEntry.container;
            if (theLog == null)
                throw new ObjectStoreException();
            fname = genPathName(theLog.getName(), tName, ft);
            fd = openAndLock(fname, FileLock.F_WRLCK, true);
            if (fd == null) {
                tsLogger.i18NLogger.warn_objectstore_ShadowingStore_18(fname);
                return false;
            }
            boolean setLength = !fd.exists();
            try {
                ofile = new RandomAccessFile(fd, FILE_MODE);
                if (setLength) {
                    ofile.setLength(_maxFileSize);
                } else {
                    if (theLog.remaining() < buffSize) {
                        long size = ofile.length() + buffSize - theLog.remaining();
                        ofile.setLength(size);
                        theLog.resize(size);
                    }
                }
                java.nio.ByteBuffer buff = java.nio.ByteBuffer.allocate(buffSize);
                buff.put(_redzone);
                buff.putInt(uidString.length);
                buff.put(uidString);
                buff.putInt(imageSize);
                buff.put(state.buffer());
                synchronized (_lock) {
                    ofile.seek(theLogEntry.offset);
                    ofile.write(buff.array());
                }
            } catch (SyncFailedException e) {
                unlockAndClose(fd, ofile);
                throw new ObjectStoreException("ShadowingStore::write_state() - write failed to sync for " + fname, e);
            } catch (FileNotFoundException e) {
                unlockAndClose(fd, ofile);
                e.printStackTrace();
                throw new ObjectStoreException("ShadowingStore::write_state() - write failed to locate file " + fname + ": " + e, e);
            } catch (IOException e) {
                unlockAndClose(fd, ofile);
                e.printStackTrace();
                throw new ObjectStoreException("ShadowingStore::write_state() - write failed for " + fname + ": " + e, e);
            } finally {
                try {
                    if (lock != null)
                        lock.release();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        if (!unlockAndClose(fd, ofile)) {
            tsLogger.i18NLogger.warn_objectstore_ShadowingStore_19(fname);
        }
        super.addToCache(fname);
        return true;
    } else
        throw new ObjectStoreException("ShadowStore::write_state - " + tsLogger.i18NLogger.get_objectstore_notypenameuid() + objUid);
}
Also used : ObjectStoreException(com.arjuna.ats.arjuna.exceptions.ObjectStoreException) FileNotFoundException(java.io.FileNotFoundException) SyncFailedException(java.io.SyncFailedException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) TransactionData(com.arjuna.ats.internal.arjuna.objectstore.LogInstance.TransactionData) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

TransactionData (com.arjuna.ats.internal.arjuna.objectstore.LogInstance.TransactionData)3 ObjectStoreException (com.arjuna.ats.arjuna.exceptions.ObjectStoreException)2 InputObjectState (com.arjuna.ats.arjuna.state.InputObjectState)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 SyncFailedException (java.io.SyncFailedException)1