use of org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator in project ignite by apache.
the class GridIoManagerFileTransmissionSelfTest method testChunkHandlerWithReconnect.
/**
* @throws Exception If fails.
*/
@Test
public void testChunkHandlerWithReconnect() throws Exception {
snd = startGrid(0);
rcv = startGrid(1);
final String filePrefix = "testFile";
final AtomicInteger cnt = new AtomicInteger();
final AtomicInteger acceptedChunks = new AtomicInteger();
final File file = new File(tempStore, filePrefix + "_" + rcv.localNode().id());
File fileToSend = createFileRandomData(filePrefix, 10 * 1024 * 1024);
transmissionFileIoFactory(snd, new FileIOFactory() {
@Override
public FileIO create(File file, OpenOption... modes) throws IOException {
FileIO fileIo = IO_FACTORY.create(file, modes);
return new FileIODecorator(fileIo) {
/**
* {@inheritDoc}
*/
@Override
public long transferTo(long position, long count, WritableByteChannel target) throws IOException {
// Send 5 chunks and close the channel.
if (cnt.incrementAndGet() == 10)
target.close();
return super.transferTo(position, count, target);
}
};
}
});
rcv.context().io().addTransmissionHandler(topic, new TransmissionHandlerAdapter() {
/**
* {@inheritDoc}
*/
@Override
public void onEnd(UUID rmtNodeId) {
U.closeQuiet(fileIo[0]);
fileIo[0] = null;
}
/**
* {@inheritDoc}
*/
@Override
public void onException(UUID nodeId, Throwable err) {
U.closeQuiet(fileIo[0]);
fileIo[0] = null;
}
/**
* {@inheritDoc}
*/
@Override
public Consumer<ByteBuffer> chunkHandler(UUID nodeId, TransmissionMeta initMeta) {
if (fileIo[0] == null) {
try {
fileIo[0] = IO_FACTORY.create(file);
fileIo[0].position(initMeta.offset());
} catch (IOException e) {
throw new IgniteException(e);
}
}
return new Consumer<ByteBuffer>() {
@Override
public void accept(ByteBuffer buff) {
try {
assertTrue(buff.order() == ByteOrder.nativeOrder());
assertEquals(0, buff.position());
assertEquals(buff.limit(), buff.capacity());
fileIo[0].writeFully(buff);
acceptedChunks.getAndIncrement();
} catch (Throwable e) {
throw new IgniteException(e);
}
}
};
}
});
try (GridIoManager.TransmissionSender sender = snd.context().io().openTransmissionSender(rcv.localNode().id(), topic)) {
sender.send(fileToSend, TransmissionPolicy.CHUNK);
}
assertEquals("Remote node must accept all chunks", fileToSend.length() / rcv.configuration().getDataStorageConfiguration().getPageSize(), acceptedChunks.get());
assertEquals("Received file and sent files have not the same lengtgh", fileToSend.length(), file.length());
assertCrcEquals(fileToSend, file);
assertNull(fileIo[0]);
}
use of org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator in project ignite by apache.
the class GridIoManagerFileTransmissionSelfTest method testFileHandlerCleanedUpIfSenderLeft.
/**
* @throws Exception If fails.
*/
@Test
public void testFileHandlerCleanedUpIfSenderLeft() throws Exception {
snd = startGrid(0);
rcv = startGrid(1);
File fileToSend = createFileRandomData("tempFile15Mb", 15 * 1024 * 1024);
File downloadTo = U.resolveWorkDirectory(tempStore.getAbsolutePath(), "download", true);
transmissionFileIoFactory(snd, new FileIOFactory() {
@Override
public FileIO create(File file, OpenOption... modes) throws IOException {
FileIO fileIo = IO_FACTORY.create(file, modes);
return new FileIODecorator(fileIo) {
/**
* {@inheritDoc}
*/
@Override
public long transferTo(long position, long count, WritableByteChannel target) throws IOException {
long transferred = super.transferTo(position, count, target);
stopGrid(snd.name(), true);
return transferred;
}
};
}
});
rcv.context().io().addTransmissionHandler(topic, new DefaultTransmissionHandler(rcv, fileToSend, tempStore) {
/**
* {@inheritDoc}
*/
@Override
public String filePath(UUID nodeId, TransmissionMeta fileMeta) {
return new File(downloadTo, fileMeta.name()).getAbsolutePath();
}
});
Exception err = null;
try (GridIoManager.TransmissionSender sender = snd.context().io().openTransmissionSender(rcv.localNode().id(), topic)) {
sender.send(fileToSend, TransmissionPolicy.FILE);
} catch (Exception e) {
// Ignore node stopping exception.
err = e;
}
assertEquals(NodeStoppingException.class, err.getClass());
assertEquals("Incomplete resources must be cleaned up on sender left", 0, fileCount(downloadTo.toPath()));
}
use of org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator in project ignite by apache.
the class GridIoManagerFileTransmissionSelfTest method testFileHandlerOnReceiverLeft.
/**
* @throws Exception If fails.
*/
@Test(expected = IgniteCheckedException.class)
public void testFileHandlerOnReceiverLeft() throws Exception {
final int fileSizeBytes = 5 * 1024 * 1024;
final AtomicInteger chunksCnt = new AtomicInteger();
snd = startGrid(0);
rcv = startGrid(1);
File fileToSend = createFileRandomData("testFile", fileSizeBytes);
transmissionFileIoFactory(snd, new FileIOFactory() {
@Override
public FileIO create(File file, OpenOption... modes) throws IOException {
FileIO fileIo = IO_FACTORY.create(file, modes);
// Blocking writer and stopping node FileIo.
return new FileIODecorator(fileIo) {
/**
* {@inheritDoc}
*/
@Override
public long transferTo(long position, long count, WritableByteChannel target) throws IOException {
// Send 5 chunks than stop the rcv.
if (chunksCnt.incrementAndGet() == 5)
stopGrid(rcv.name(), true);
return super.transferTo(position, count, target);
}
};
}
});
rcv.context().io().addTransmissionHandler(topic, new DefaultTransmissionHandler(rcv, fileToSend, tempStore));
try (GridIoManager.TransmissionSender sender = snd.context().io().openTransmissionSender(rcv.localNode().id(), topic)) {
sender.send(fileToSend, TransmissionPolicy.FILE);
}
}
Aggregations