use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class SingleFilePageSwapper method tryReopen.
/**
* Reopens the channel if it has been closed and the close() method on
* this swapper has not been called. In other words, if the channel has
* been "accidentally" closed by an interrupt or the like.
*
* If the channel has been explicitly closed with the PageSwapper#close()
* method, then this method will re-throw the passed-in exception.
*
* If the reopening of the file fails with an exception for some reason,
* then that exception is added as a suppressed exception to the passed in
* ClosedChannelException, and the CCE is then rethrown.
*/
private synchronized void tryReopen(long filePageId, ClosedChannelException closedException) throws ClosedChannelException {
int stripe = stripe(filePageId);
StoreChannel channel = channels[stripe];
if (channel.isOpen()) {
// Someone got ahead of us, presumably. Nothing to do.
return;
}
if (closed) {
// channel.
throw closedException;
}
try {
channels[stripe] = fs.open(file, "rw");
if (stripe == tokenChannelStripe) {
// The closing of a FileChannel also releases all associated file locks.
acquireLock();
}
} catch (IOException e) {
closedException.addSuppressed(e);
throw closedException;
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class PageCacheTest method writesOfDifferentUnitsMustHaveCorrectEndianess.
@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void writesOfDifferentUnitsMustHaveCorrectEndianess() throws Exception {
configureStandardPageCache();
PagedFile pagedFile = pageCache.map(file("a"), 20);
try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK)) {
assertTrue(cursor.next());
byte[] data = { 42, 43, 44, 45, 46 };
// 0+8 = 8
cursor.putLong(41);
// 8+4 = 12
cursor.putInt(41);
// 12+2 = 14
cursor.putShort((short) 41);
// 14+1 = 15
cursor.putByte((byte) 41);
// 15+5 = 20
cursor.putBytes(data);
}
try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK)) {
assertTrue(cursor.next());
// 8
long a = cursor.getLong();
// 12
int b = cursor.getInt();
// 14
short c = cursor.getShort();
byte[] data = new byte[] { // 15
cursor.getByte(), // 16
cursor.getByte(), // 17
cursor.getByte(), // 18
cursor.getByte(), // 19
cursor.getByte(), // 20
cursor.getByte() };
cursor.setOffset(0);
cursor.putLong(1 + a);
cursor.putInt(1 + b);
cursor.putShort((short) (1 + c));
for (byte d : data) {
d++;
cursor.putByte(d);
}
}
pagedFile.close();
StoreChannel channel = fs.open(file("a"), "r");
ByteBuffer buf = ByteBuffer.allocate(20);
channel.read(buf);
buf.flip();
assertThat(buf.getLong(), is(42L));
assertThat(buf.getInt(), is(42));
assertThat(buf.getShort(), is((short) 42));
assertThat(buf.get(), is((byte) 42));
assertThat(buf.get(), is((byte) 43));
assertThat(buf.get(), is((byte) 44));
assertThat(buf.get(), is((byte) 45));
assertThat(buf.get(), is((byte) 46));
assertThat(buf.get(), is((byte) 47));
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class PageCacheTest method evictionMustFlushPagesToTheRightFiles.
@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void evictionMustFlushPagesToTheRightFiles() throws IOException {
generateFileWithRecords(file("a"), recordCount, recordSize);
// diff. page size just to be difficult
int filePageSize2 = filePageSize - 3;
long maxPageIdCursor1 = recordCount / recordsPerFilePage;
File file2 = file("b");
OutputStream outputStream = fs.openAsOutputStream(file2, false);
long file2sizeBytes = (maxPageIdCursor1 + 17) * filePageSize2;
for (int i = 0; i < file2sizeBytes; i++) {
// We will ues the page cache to change these 'a's into 'b's.
outputStream.write('a');
}
outputStream.flush();
outputStream.close();
configureStandardPageCache();
PagedFile pagedFile1 = pageCache.map(file("a"), filePageSize);
PagedFile pagedFile2 = pageCache.map(file2, filePageSize2);
long pageId1 = 0;
long pageId2 = 0;
boolean moreWorkToDo;
do {
boolean cursorReady1;
boolean cursorReady2;
try (PageCursor cursor = pagedFile1.io(pageId1, PF_SHARED_WRITE_LOCK)) {
cursorReady1 = cursor.next() && cursor.getCurrentPageId() < maxPageIdCursor1;
if (cursorReady1) {
writeRecords(cursor);
pageId1++;
}
}
try (PageCursor cursor = pagedFile2.io(pageId2, PF_SHARED_WRITE_LOCK | PF_NO_GROW)) {
cursorReady2 = cursor.next();
if (cursorReady2) {
for (int i = 0; i < filePageSize2; i++) {
cursor.putByte((byte) 'b');
}
assertFalse(cursor.shouldRetry());
}
pageId2++;
}
moreWorkToDo = cursorReady1 || cursorReady2;
} while (moreWorkToDo);
pagedFile1.close();
pagedFile2.close();
// Verify the file contents
assertThat(fs.getFileSize(file2), is(file2sizeBytes));
InputStream inputStream = fs.openAsInputStream(file2);
for (int i = 0; i < file2sizeBytes; i++) {
int b = inputStream.read();
assertThat(b, is((int) 'b'));
}
assertThat(inputStream.read(), is(-1));
inputStream.close();
StoreChannel channel = fs.open(file("a"), "r");
ByteBuffer bufB = ByteBuffer.allocate(recordSize);
for (int i = 0; i < recordCount; i++) {
bufA.clear();
channel.read(bufA);
bufA.flip();
bufB.clear();
generateRecordForId(i, bufB);
assertThat(bufB.array(), byteArray(bufA.array()));
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class PageCacheTest method lastPageIdOfFileWithOneByteIsZero.
@Test
public void lastPageIdOfFileWithOneByteIsZero() throws IOException {
StoreChannel channel = fs.create(file("a"));
channel.write(ByteBuffer.wrap(new byte[] { 1 }));
channel.close();
configureStandardPageCache();
try (PagedFile pagedFile = pageCache.map(file("a"), filePageSize)) {
assertThat(pagedFile.getLastPageId(), is(0L));
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class StateRecoveryManagerTest method shouldReturnTheEmptyFileAsPreviouslyInactiveWhenActiveContainsCorruptEntry.
@Test
public void shouldReturnTheEmptyFileAsPreviouslyInactiveWhenActiveContainsCorruptEntry() throws Exception {
// given
EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
fsa.mkdir(testDir.directory());
File fileA = fileA();
StoreChannel channel = fsa.create(fileA);
ByteBuffer buffer = writeLong(999);
channel.writeAll(buffer);
channel.force(false);
File fileB = fileB();
channel = fsa.create(fileB);
channel.close();
StateRecoveryManager<Long> manager = new StateRecoveryManager<>(fsa, new LongMarshal());
// when
final StateRecoveryManager.RecoveryStatus recoveryStatus = manager.recover(fileA, fileB);
// then
assertEquals(999L, recoveryStatus.recoveredState());
assertEquals(fileB, recoveryStatus.activeFile());
}
Aggregations