Search in sources :

Example 71 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class EphemeralFileSystemAbstractionTest method shouldBeConsistentAfterConcurrentWritesAndForces.

@Test
public void shouldBeConsistentAfterConcurrentWritesAndForces() throws Exception {
    ExecutorService executorService = Executors.newCachedThreadPool();
    try {
        for (int attempt = 0; attempt < 100; attempt++) {
            try (EphemeralFileSystemAbstraction fs = new EphemeralFileSystemAbstraction()) {
                File aFile = new File("contendedFile");
                Collection<Callable<Void>> workers = new ArrayList<>();
                for (int i = 0; i < 100; i++) {
                    workers.add(() -> {
                        try {
                            StoreChannel channel = fs.open(aFile, "rw");
                            channel.position(channel.size());
                            writeLong(channel, 1);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                        return null;
                    });
                    workers.add(() -> {
                        StoreChannel channel = fs.open(aFile, "rw");
                        channel.force(true);
                        return null;
                    });
                }
                List<Future<Void>> futures = executorService.invokeAll(workers);
                for (Future<Void> future : futures) {
                    future.get();
                }
                fs.crash();
                verifyFileIsFullOfLongIntegerOnes(fs.open(aFile, "rw"));
            }
        }
    } finally {
        executorService.shutdown();
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) File(java.io.File) Test(org.junit.Test)

Example 72 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class EphemeralFileSystemAbstractionTest method allowStoreThatExceedDefaultSize.

@Test
public void allowStoreThatExceedDefaultSize() throws IOException {
    File aFile = new File("test");
    StoreChannel channel = fs.open(aFile, "rw");
    ByteBuffer buffer = allocateDirect(Long.BYTES);
    int mebiBytes = (int) ByteUnit.mebiBytes(1);
    for (int position = mebiBytes + 42; position < 10_000_000; position += mebiBytes) {
        buffer.putLong(1);
        buffer.flip();
        channel.write(buffer, position);
        buffer.clear();
    }
    channel.close();
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 73 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class EphemeralFileSystemAbstractionTest method shouldBeConsistentAfterConcurrentWritesAndCrashes.

@Test
public void shouldBeConsistentAfterConcurrentWritesAndCrashes() throws Exception {
    ExecutorService executorService = Executors.newCachedThreadPool();
    try (EphemeralFileSystemAbstraction fs = new EphemeralFileSystemAbstraction()) {
        File aFile = new File("contendedFile");
        for (int attempt = 0; attempt < 100; attempt++) {
            Collection<Callable<Void>> workers = new ArrayList<>();
            for (int i = 0; i < 100; i++) {
                workers.add(() -> {
                    try {
                        StoreChannel channel = fs.open(aFile, "rw");
                        channel.position(0);
                        writeLong(channel, 1);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    return null;
                });
                workers.add(() -> {
                    fs.crash();
                    return null;
                });
            }
            List<Future<Void>> futures = executorService.invokeAll(workers);
            for (Future<Void> future : futures) {
                future.get();
            }
            verifyFileIsEitherEmptyOrContainsLongIntegerValueOne(fs.open(aFile, "rw"));
        }
    } finally {
        executorService.shutdown();
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) File(java.io.File) Test(org.junit.Test)

Example 74 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class EphemeralFileSystemAbstractionTest method shouldNotLoseDataForcedBeforeFileSystemCrashes.

@Test
public void shouldNotLoseDataForcedBeforeFileSystemCrashes() throws Exception {
    try (EphemeralFileSystemAbstraction fs = new EphemeralFileSystemAbstraction()) {
        // given
        int numberOfBytesForced = 8;
        File aFile = new File("yo");
        StoreChannel channel = fs.open(aFile, "rw");
        writeLong(channel, 1111);
        // when
        channel.force(true);
        writeLong(channel, 2222);
        fs.crash();
        // then
        StoreChannel readChannel = fs.open(aFile, "r");
        assertEquals(numberOfBytesForced, readChannel.size());
        assertEquals(1111, readLong(readChannel).getLong());
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) Test(org.junit.Test)

Example 75 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class EphemeralFileSystemAbstraction method copyFile.

private void copyFile(File from, FileSystemAbstraction fromFs, File to, ByteBuffer buffer) throws IOException {
    try (StoreChannel source = fromFs.open(from, "r");
        StoreChannel sink = this.open(to, "rw")) {
        for (int available; (available = (int) (source.size() - source.position())) > 0; ) {
            buffer.clear();
            buffer.limit(min(available, buffer.capacity()));
            source.read(buffer);
            buffer.flip();
            sink.write(buffer);
        }
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel)

Aggregations

StoreChannel (org.neo4j.io.fs.StoreChannel)113 Test (org.junit.Test)71 File (java.io.File)65 ByteBuffer (java.nio.ByteBuffer)48 IOException (java.io.IOException)20 DelegatingStoreChannel (org.neo4j.graphdb.mockfs.DelegatingStoreChannel)17 PhysicalLogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel)15 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)13 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)11 LogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel)10 ReadAheadLogChannel (org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel)7 LogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeader)7 LogHeaderReader.readLogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader)7 InputStream (java.io.InputStream)6 PageSwapperFactory (org.neo4j.io.pagecache.PageSwapperFactory)6 PageSwapperTest (org.neo4j.io.pagecache.PageSwapperTest)6 HashSet (java.util.HashSet)5 AdversarialPagedFile (org.neo4j.adversaries.pagecache.AdversarialPagedFile)5 PageSwapper (org.neo4j.io.pagecache.PageSwapper)5 PagedFile (org.neo4j.io.pagecache.PagedFile)5