Search in sources :

Example 91 with OLogSequenceNumber

use of com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber in project orientdb by orientechnologies.

the class OAbstract2pcTask method fromStream.

@Override
public void fromStream(final DataInput in, final ORemoteTaskFactory factory) throws IOException {
    final int size = in.readInt();
    for (int i = 0; i < size; ++i) {
        final ORemoteTask task = factory.createTask(in.readByte());
        task.fromStream(in, factory);
        tasks.add((OAbstractRecordReplicatedTask) task);
    }
    final boolean hasLastLSN = in.readBoolean();
    if (hasLastLSN)
        lastLSN = new OLogSequenceNumber(in);
}
Also used : OLogSequenceNumber(com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber) ORemoteTask(com.orientechnologies.orient.server.distributed.task.ORemoteTask)

Example 92 with OLogSequenceNumber

use of com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber in project orientdb by orientechnologies.

the class OAbstractSyncDatabaseTask method checkIfCurrentDatabaseIsNotOlder.

protected ODistributedDatabase checkIfCurrentDatabaseIsNotOlder(final ODistributedServerManager iManager, final String databaseName, final OLogSequenceNumber startLSN) {
    final ODistributedDatabase dDatabase = iManager.getMessageService().getDatabase(databaseName);
    if (startLSN != null) {
        final OLogSequenceNumber currentLSN = dDatabase.getSyncConfiguration().getLastLSN(iManager.getLocalNodeName());
        if (startLSN.equals(currentLSN))
            // REQUESTING LSN IS THE LAST ONE, FINE
            return dDatabase;
    }
    // CHECK THE DATE OF LAST OPERATION
    if (dDatabase.getSyncConfiguration().getLastOperationTimestamp() < lastOperationTimestamp) {
        final OLogSequenceNumber currentLSN = dDatabase.getSyncConfiguration().getLastLSN(getNodeSource());
        if (currentLSN == null)
            return dDatabase;
        if (lastLSN != null) {
            // USE THE LSN TO COMPARE DATABASES
            if (lastLSN.equals(currentLSN))
                return dDatabase;
        }
        databaseIsOld(iManager, databaseName, dDatabase);
    }
    return dDatabase;
}
Also used : ODistributedDatabase(com.orientechnologies.orient.server.distributed.ODistributedDatabase) OLogSequenceNumber(com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber)

Example 93 with OLogSequenceNumber

use of com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber in project orientdb by orientechnologies.

the class NonDurableTxTest method testWalNotGrowingWhileWalDisabledInAtomicManager.

@Test
public void testWalNotGrowingWhileWalDisabledInAtomicManager() {
    db.newInstance().field("some-unrelated-key", "some-unrelated-value").save();
    wal.flush();
    final OLogSequenceNumber startLsn = wal.getFlushedLsn();
    atomicOperationsManager.switchOnUnsafeMode();
    try {
        db.begin();
        final ODocument doc1 = db.newInstance().field("tx-key", "tx-value").save();
        db.commit();
        doc1.field("non-tx-key", "non-tx-value").save();
        wal.flush();
        final OLogSequenceNumber endLsn = wal.getFlushedLsn();
        Assert.assertEquals(startLsn, endLsn);
    } finally {
        atomicOperationsManager.switchOffUnsafeMode();
    }
}
Also used : OLogSequenceNumber(com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 94 with OLogSequenceNumber

use of com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber in project orientdb by orientechnologies.

the class OAbstractRecordReplicatedTask method fromStream.

@Override
public void fromStream(final DataInput in, final ORemoteTaskFactory factory) throws IOException {
    rid = new ORecordId();
    rid.fromStream(in);
    version = in.readInt();
    partitionKey = in.readInt();
    final boolean hasLastLSN = in.readBoolean();
    if (hasLastLSN)
        lastLSN = new OLogSequenceNumber(in);
}
Also used : OLogSequenceNumber(com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 95 with OLogSequenceNumber

use of com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber in project orientdb by orientechnologies.

the class OWOWCache method load.

public OCachePointer[] load(long fileId, long startPageIndex, int pageCount, boolean addNewPages, OModifiableBoolean cacheHit, boolean verifyChecksums) throws IOException {
    final int intId = extractFileId(fileId);
    if (pageCount < 1)
        throw new IllegalArgumentException("Amount of pages to load should be not less than 1 but provided value is " + pageCount);
    filesLock.acquireReadLock();
    try {
        // first check that requested page is already cached so we do not need to load it from file
        final PageKey startPageKey = new PageKey(intId, startPageIndex);
        final Lock startPageLock = lockManager.acquireSharedLock(startPageKey);
        // check if page already presented in write cache
        final PageGroup startPageGroup = writeCachePages.get(startPageKey);
        // page is not cached load it from file
        if (startPageGroup == null) {
            // load it from file and preload requested pages
            // there is small optimization
            // if we need single page no need to release already locked page
            Lock[] pageLocks;
            PageKey[] pageKeys;
            if (pageCount > 1) {
                startPageLock.unlock();
                pageKeys = new PageKey[pageCount];
                for (int i = 0; i < pageCount; i++) {
                    pageKeys[i] = new PageKey(intId, startPageIndex + i);
                }
                pageLocks = lockManager.acquireSharedLocksInBatch(pageKeys);
            } else {
                pageLocks = new Lock[] { startPageLock };
                pageKeys = new PageKey[] { startPageKey };
            }
            OCachePointer[] pagePointers;
            try {
                // load requested page and preload requested amount of pages
                pagePointers = loadFileContent(intId, startPageIndex, pageCount, verifyChecksums);
                if (pagePointers != null) {
                    assert pagePointers.length > 0;
                    for (int n = 0; n < pagePointers.length; n++) {
                        pagePointers[n].incrementReadersReferrer();
                        if (n > 0) {
                            PageGroup pageGroup = writeCachePages.get(pageKeys[n]);
                            assert pageKeys[n].pageIndex == pagePointers[n].getPageIndex();
                            // if page already exists in cache we should drop already loaded page and load cache page instead
                            if (pageGroup != null) {
                                pagePointers[n].decrementReadersReferrer();
                                pagePointers[n] = pageGroup.page;
                                pagePointers[n].incrementReadersReferrer();
                            }
                        }
                    }
                    return pagePointers;
                }
            } finally {
                for (Lock pageLock : pageLocks) {
                    pageLock.unlock();
                }
            }
            // we need to allocate pages on the disk first
            if (!addNewPages)
                return new OCachePointer[0];
            final OClosableEntry<Long, OFileClassic> entry = files.acquire(fileId);
            try {
                final OFileClassic fileClassic = entry.get();
                long startAllocationIndex = fileClassic.getFileSize() / pageSize;
                long stopAllocationIndex = startPageIndex;
                final PageKey[] allocationPageKeys = new PageKey[(int) (stopAllocationIndex - startAllocationIndex + 1)];
                for (long pageIndex = startAllocationIndex; pageIndex <= stopAllocationIndex; pageIndex++) {
                    int index = (int) (pageIndex - startAllocationIndex);
                    allocationPageKeys[index] = new PageKey(intId, pageIndex);
                }
                // use exclusive locks to prevent to have duplication of pointers
                // when page is loaded from file because space is already allocated
                // but it the same moment another page for the same index is added to the write cache
                Lock[] locks = lockManager.acquireExclusiveLocksInBatch(allocationPageKeys);
                try {
                    final long fileSize = fileClassic.getFileSize();
                    final long spaceToAllocate = ((stopAllocationIndex + 1) * pageSize - fileSize);
                    OCachePointer resultPointer = null;
                    if (spaceToAllocate > 0) {
                        final OLogSequenceNumber lastLsn = writeAheadLog == null ? new OLogSequenceNumber(-1, -1) : writeAheadLog.getFlushedLsn();
                        fileClassic.allocateSpace(spaceToAllocate);
                        startAllocationIndex = fileSize / pageSize;
                        for (long index = startAllocationIndex; index <= stopAllocationIndex; index++) {
                            final ByteBuffer buffer = bufferPool.acquireDirect(true);
                            buffer.putLong(MAGIC_NUMBER_OFFSET, MAGIC_NUMBER_WITHOUT_CHECKSUM);
                            final OCachePointer cachePointer = new OCachePointer(buffer, bufferPool, lastLsn, fileId, index);
                            // item only in write cache till we will not return
                            // it to read cache so we increment exclusive size by one
                            // otherwise call of write listener inside pointer may set exclusive size to negative value
                            exclusiveWriteCacheSize.increment();
                            doPutInCache(cachePointer, new PageKey(intId, index));
                            if (index == startPageIndex) {
                                resultPointer = cachePointer;
                            }
                        }
                        // we check is it enough space on disk to continue to write data on it
                        // otherwise we switch storage in read-only mode
                        freeSpaceCheckAfterNewPageAdd((int) (stopAllocationIndex - startAllocationIndex + 1));
                    }
                    if (resultPointer != null) {
                        resultPointer.incrementReadersReferrer();
                        cacheHit.setValue(true);
                        return new OCachePointer[] { resultPointer };
                    }
                } finally {
                    for (Lock lock : locks) {
                        lock.unlock();
                    }
                }
            } finally {
                files.release(entry);
            }
            // in such case we read it again
            return load(fileId, startPageIndex, pageCount, true, cacheHit, verifyChecksums);
        } else {
            startPageGroup.page.incrementReadersReferrer();
            startPageLock.unlock();
            cacheHit.setValue(true);
            return new OCachePointer[] { startPageGroup.page };
        }
    } catch (InterruptedException e) {
        throw OException.wrapException(new OStorageException("Load was interrupted"), e);
    } finally {
        filesLock.releaseReadLock();
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) OFileClassic(com.orientechnologies.orient.core.storage.fs.OFileClassic) Lock(java.util.concurrent.locks.Lock) OLogSequenceNumber(com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber) OStorageException(com.orientechnologies.orient.core.exception.OStorageException) AtomicLong(java.util.concurrent.atomic.AtomicLong) OCachePointer(com.orientechnologies.orient.core.storage.cache.OCachePointer)

Aggregations

OLogSequenceNumber (com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber)97 ByteBuffer (java.nio.ByteBuffer)78 OCacheEntry (com.orientechnologies.orient.core.storage.cache.OCacheEntry)74 OByteBufferPool (com.orientechnologies.common.directmemory.OByteBufferPool)69 OCachePointer (com.orientechnologies.orient.core.storage.cache.OCachePointer)68 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)20 OWALChangesTree (com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OWALChangesTree)19 Test (org.testng.annotations.Test)16 ORecordId (com.orientechnologies.orient.core.id.ORecordId)10 Random (java.util.Random)10 HashMap (java.util.HashMap)8 Map (java.util.Map)8 TreeSet (java.util.TreeSet)8 OFileClassic (com.orientechnologies.orient.core.storage.fs.OFileClassic)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 OModifiableBoolean (com.orientechnologies.common.types.OModifiableBoolean)3 OStorageException (com.orientechnologies.orient.core.exception.OStorageException)3 HazelcastException (com.hazelcast.core.HazelcastException)2 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)2 OOfflineNodeException (com.orientechnologies.common.concur.OOfflineNodeException)2