Search in sources :

Example 11 with PagedFile

use of org.neo4j.io.pagecache.PagedFile in project neo4j by neo4j.

the class PagedByteChannelsTest method mustCloseCursorOnClose.

@Theory
public void mustCloseCursorOnClose(ThrowingFunction<PagedFile, ? extends Channel, IOException> channelConstructor) throws Exception {
    AtomicInteger closeCounter = new AtomicInteger();
    PagedFile pf = new StubPagedFile(8192) {

        @Override
        public PageCursor io(long pageId, int pf_flags) throws IOException {
            return new DelegatingPageCursor(super.io(pageId, pf_flags)) {

                @Override
                public void close() {
                    super.close();
                    closeCounter.getAndIncrement();
                }
            };
        }
    };
    channelConstructor.apply(pf).close();
    assertThat(closeCounter.get(), is(1));
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) StubPagedFile(org.neo4j.io.pagecache.StubPagedFile) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StubPagedFile(org.neo4j.io.pagecache.StubPagedFile) Theory(org.junit.experimental.theories.Theory)

Example 12 with PagedFile

use of org.neo4j.io.pagecache.PagedFile in project neo4j by neo4j.

the class MetaDataStoreTest method transactionCommittedMustBeAtomic.

@Test
public void transactionCommittedMustBeAtomic() throws Throwable {
    try (MetaDataStore store = newMetaDataStore()) {
        PagedFile pf = store.storeFile;
        store.transactionCommitted(2, 2, 2);
        AtomicLong writeCount = new AtomicLong();
        AtomicLong fileReadCount = new AtomicLong();
        AtomicLong apiReadCount = new AtomicLong();
        int upperLimit = 10_000;
        int lowerLimit = 100;
        long endTime = currentTimeMillis() + SECONDS.toMillis(10);
        Race race = new Race();
        race.withEndCondition(() -> writeCount.get() >= upperLimit && fileReadCount.get() >= upperLimit && apiReadCount.get() >= upperLimit);
        race.withEndCondition(() -> writeCount.get() >= lowerLimit && fileReadCount.get() >= lowerLimit && apiReadCount.get() >= lowerLimit && currentTimeMillis() >= endTime);
        race.addContestants(3, () -> {
            long count = writeCount.incrementAndGet();
            store.transactionCommitted(count, count, count);
        });
        race.addContestants(3, throwing(() -> {
            try (PageCursor cursor = pf.io(0, PagedFile.PF_SHARED_READ_LOCK)) {
                assertTrue(cursor.next());
                long id, checksum;
                do {
                    id = store.getRecordValue(cursor, MetaDataStore.Position.LAST_TRANSACTION_ID);
                    checksum = store.getRecordValue(cursor, MetaDataStore.Position.LAST_TRANSACTION_CHECKSUM);
                } while (cursor.shouldRetry());
                assertIdEqualsChecksum(id, checksum, "file");
                fileReadCount.incrementAndGet();
            }
        }));
        race.addContestants(3, () -> {
            TransactionId transaction = store.getLastCommittedTransaction();
            assertIdEqualsChecksum(transaction.transactionId(), transaction.checksum(), "API");
            apiReadCount.incrementAndGet();
        });
        race.go();
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) PagedFile(org.neo4j.io.pagecache.PagedFile) DelegatingPagedFile(org.neo4j.io.pagecache.DelegatingPagedFile) Race(org.neo4j.test.Race) PageCursor(org.neo4j.io.pagecache.PageCursor) DelegatingPageCursor(org.neo4j.io.pagecache.impl.DelegatingPageCursor) Test(org.junit.Test)

Example 13 with PagedFile

use of org.neo4j.io.pagecache.PagedFile in project neo4j by neo4j.

the class SwitchToSlaveCopyThenBranchTest method shouldNotBranchStoreUnlessWeHaveCopiedDownAReplacement.

@Test
public void shouldNotBranchStoreUnlessWeHaveCopiedDownAReplacement() throws Throwable {
    // Given
    StoreCopyClient storeCopyClient = mock(StoreCopyClient.class);
    doAnswer(invocation -> {
        MoveAfterCopy moveAfterCopy = invocation.getArgumentAt(2, MoveAfterCopy.class);
        moveAfterCopy.move(Stream.empty(), new File(""), new File(""));
        return null;
    }).when(storeCopyClient).copyStore(any(StoreCopyClient.StoreCopyRequester.class), any(CancellationRequest.class), any(MoveAfterCopy.class));
    PageCache pageCacheMock = mock(PageCache.class);
    PagedFile pagedFileMock = mock(PagedFile.class);
    when(pagedFileMock.getLastPageId()).thenReturn(1L);
    when(pageCacheMock.map(any(File.class), anyInt())).thenReturn(pagedFileMock);
    SwitchToSlaveCopyThenBranch switchToSlave = newSwitchToSlaveSpy(pageCacheMock, storeCopyClient);
    URI masterUri = new URI("cluster://localhost?serverId=1");
    URI me = new URI("cluster://localhost?serverId=2");
    CancellationRequest cancellationRequest = CancellationRequest.NEVER_CANCELLED;
    MasterClient masterClient = mock(MasterClient.class);
    when(masterClient.handshake(anyLong(), any(StoreId.class))).thenThrow(new BranchedDataException(""));
    TransactionIdStore transactionIdStore = mock(TransactionIdStore.class);
    when(transactionIdStore.getLastCommittedTransaction()).thenReturn(new TransactionId(42, 42, 42));
    when(transactionIdStore.getLastCommittedTransactionId()).thenReturn(TransactionIdStore.BASE_TX_ID);
    // When
    BranchedDataPolicy branchPolicy = mock(BranchedDataPolicy.class);
    switchToSlave.stopServicesAndHandleBranchedStore(branchPolicy, masterUri, me, cancellationRequest);
    // Then
    InOrder inOrder = Mockito.inOrder(storeCopyClient, branchPolicy);
    inOrder.verify(storeCopyClient).copyStore(any(StoreCopyClient.StoreCopyRequester.class), any(CancellationRequest.class), any(MoveAfterCopy.class));
    inOrder.verify(branchPolicy).handle(new File(""), pageCacheMock, NullLogService.getInstance());
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) InOrder(org.mockito.InOrder) MasterClient(org.neo4j.kernel.ha.com.slave.MasterClient) URI(java.net.URI) TransactionId(org.neo4j.kernel.impl.store.TransactionId) StoreId(org.neo4j.kernel.impl.store.StoreId) StoreCopyClient(org.neo4j.com.storecopy.StoreCopyClient) BranchedDataPolicy(org.neo4j.kernel.ha.BranchedDataPolicy) PagedFile(org.neo4j.io.pagecache.PagedFile) File(java.io.File) BranchedDataException(org.neo4j.kernel.ha.BranchedDataException) MoveAfterCopy(org.neo4j.com.storecopy.MoveAfterCopy) CancellationRequest(org.neo4j.helpers.CancellationRequest) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Example 14 with PagedFile

use of org.neo4j.io.pagecache.PagedFile in project neo4j by neo4j.

the class StreamToDisk method write.

@Override
public void write(String destination, int requiredAlignment, byte[] data) throws IOException {
    File fileName = new File(storeDir, destination);
    fs.mkdirs(fileName.getParentFile());
    fileCopyMonitor.copyFile(fileName);
    if (StoreType.shouldBeManagedByPageCache(destination)) {
        WritableByteChannel channel = channels.get(destination);
        if (channel == null) {
            int filePageSize = pageCache.pageSize() - pageCache.pageSize() % requiredAlignment;
            PagedFile pagedFile = pageCache.map(fileName, filePageSize, StandardOpenOption.CREATE);
            channel = pagedFile.openWritableByteChannel();
            pagedFiles.put(destination, pagedFile);
            channels.put(destination, channel);
        }
        ByteBuffer buffer = ByteBuffer.wrap(data);
        while (buffer.hasRemaining()) {
            channel.write(buffer);
        }
    } else {
        try (OutputStream outputStream = fs.openAsOutputStream(fileName, true)) {
            outputStream.write(data);
        }
    }
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) OutputStream(java.io.OutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) PagedFile(org.neo4j.io.pagecache.PagedFile) File(java.io.File) ByteBuffer(java.nio.ByteBuffer)

Example 15 with PagedFile

use of org.neo4j.io.pagecache.PagedFile in project neo4j by neo4j.

the class GetStoreRequestHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, GetStoreRequest msg) throws Exception {
    if (!msg.expectedStoreId().equalToKernelStoreId(dataSource.get().getStoreId())) {
        endStoreCopy(SUCCESS, ctx, -1);
    } else {
        CheckPointer checkPointer = checkPointerSupplier.get();
        long lastCheckPointedTx;
        try (Resource lock = mutex.storeCopy(() -> checkPointer.tryCheckPoint(new SimpleTriggerInfo("Store copy")));
            ResourceIterator<StoreFileMetadata> files = dataSource.get().listStoreFiles(false)) {
            lastCheckPointedTx = checkPointer.lastCheckPointedTransactionId();
            while (files.hasNext()) {
                StoreFileMetadata fileMetadata = files.next();
                File file = fileMetadata.file();
                log.debug("Sending file " + file);
                ctx.writeAndFlush(ResponseMessageType.FILE);
                ctx.writeAndFlush(new FileHeader(relativePath(dataSource.get().getStoreDir(), file), fileMetadata.recordSize()));
                Optional<PagedFile> existingMapping = pageCache.getExistingMapping(file);
                if (existingMapping.isPresent()) {
                    try (PagedFile pagedFile = existingMapping.get()) {
                        ctx.writeAndFlush(new FileSender(pagedFile.openReadableByteChannel()));
                    }
                } else {
                    ctx.writeAndFlush(new FileSender(fs.open(file, "r")));
                }
            }
        }
        endStoreCopy(SUCCESS, ctx, lastCheckPointedTx);
    }
    protocol.expect(State.MESSAGE_TYPE);
}
Also used : SimpleTriggerInfo(org.neo4j.kernel.impl.transaction.log.checkpoint.SimpleTriggerInfo) PagedFile(org.neo4j.io.pagecache.PagedFile) CheckPointer(org.neo4j.kernel.impl.transaction.log.checkpoint.CheckPointer) Resource(org.neo4j.graphdb.Resource) StoreFileMetadata(org.neo4j.storageengine.api.StoreFileMetadata) PagedFile(org.neo4j.io.pagecache.PagedFile) File(java.io.File)

Aggregations

PagedFile (org.neo4j.io.pagecache.PagedFile)39 File (java.io.File)23 PageCursor (org.neo4j.io.pagecache.PageCursor)20 Test (org.junit.Test)16 IOException (java.io.IOException)13 PageCache (org.neo4j.io.pagecache.PageCache)10 ByteBuffer (java.nio.ByteBuffer)8 PageCacheTest (org.neo4j.io.pagecache.PageCacheTest)7 DelegatingStoreChannel (org.neo4j.graphdb.mockfs.DelegatingStoreChannel)5 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)5 StoreChannel (org.neo4j.io.fs.StoreChannel)5 NoSuchFileException (java.nio.file.NoSuchFileException)4 DelegatingPagedFile (org.neo4j.io.pagecache.DelegatingPagedFile)3 DelegatingPageCursor (org.neo4j.io.pagecache.impl.DelegatingPageCursor)3 ConfigurablePageCursorTracerSupplier (org.neo4j.io.pagecache.tracing.ConfigurablePageCursorTracerSupplier)3 RecordingPageCacheTracer (org.neo4j.io.pagecache.tracing.recording.RecordingPageCacheTracer)3 Evict (org.neo4j.io.pagecache.tracing.recording.RecordingPageCacheTracer.Evict)3 RecordingPageCursorTracer (org.neo4j.io.pagecache.tracing.recording.RecordingPageCursorTracer)3 Fault (org.neo4j.io.pagecache.tracing.recording.RecordingPageCursorTracer.Fault)3 TransactionId (org.neo4j.kernel.impl.store.TransactionId)3