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();
}
}
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();
}
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();
}
}
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());
}
}
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);
}
}
}
Aggregations