use of org.apache.flink.runtime.util.event.NotificationListener in project flink by apache.
the class SeekRequest method handleProcessedBuffer.
/**
* Handles a processed <tt>Buffer</tt>. This method is invoked by the asynchronous IO worker
* threads upon completion of the IO request with the provided buffer and/or an exception that
* occurred while processing the request for that buffer.
*
* @param buffer The buffer to be processed.
* @param ex The exception that occurred in the I/O threads when processing the buffer's
* request.
*/
protected final void handleProcessedBuffer(T buffer, IOException ex) {
if (buffer == null) {
return;
}
// even if the callbacks throw an error, we need to maintain our bookkeeping
try {
if (ex != null && this.exception == null) {
this.exception = ex;
this.resultHandler.requestFailed(buffer, ex);
} else {
this.resultHandler.requestSuccessful(buffer);
}
} finally {
NotificationListener listener = null;
// waiters. If there is a listener, notify her as well.
synchronized (this.closeLock) {
if (this.requestsNotReturned.decrementAndGet() == 0) {
if (this.closed) {
this.closeLock.notifyAll();
}
synchronized (listenerLock) {
listener = allRequestsProcessedListener;
allRequestsProcessedListener = null;
}
}
}
if (listener != null) {
listener.onNotification();
}
}
}
use of org.apache.flink.runtime.util.event.NotificationListener in project flink by apache.
the class BufferFileWriterFileSegmentReaderTest method testWriteRead.
@Test
public void testWriteRead() throws IOException, InterruptedException {
int numBuffers = 1024;
int currentNumber = 0;
final int minBufferSize = BUFFER_SIZE / 4;
// Write buffers filled with ascending numbers...
for (int i = 0; i < numBuffers; i++) {
final Buffer buffer = createBuffer();
int size = getNextMultipleOf(getRandomNumberInRange(minBufferSize, BUFFER_SIZE), 4);
currentNumber = fillBufferWithAscendingNumbers(buffer, currentNumber, size);
writer.writeBlock(buffer);
}
// Make sure that the writes are finished
writer.close();
// Read buffers back in...
for (int i = 0; i < numBuffers; i++) {
assertFalse(reader.hasReachedEndOfFile());
reader.read();
}
// Wait for all requests to be finished
final CountDownLatch sync = new CountDownLatch(1);
final NotificationListener listener = new NotificationListener() {
@Override
public void onNotification() {
sync.countDown();
}
};
if (reader.registerAllRequestsProcessedListener(listener)) {
sync.await();
}
assertTrue(reader.hasReachedEndOfFile());
// Verify that the content is the same
assertEquals("Read less buffers than written.", numBuffers, returnedFileSegments.size());
currentNumber = 0;
FileSegment fileSegment;
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
while ((fileSegment = returnedFileSegments.poll()) != null) {
buffer.position(0);
buffer.limit(fileSegment.getLength());
fileSegment.getFileChannel().read(buffer, fileSegment.getPosition());
Buffer buffer1 = new NetworkBuffer(MemorySegmentFactory.wrap(buffer.array()), BUFFER_RECYCLER);
buffer1.setSize(fileSegment.getLength());
currentNumber = verifyBufferFilledWithAscendingNumbers(buffer1, currentNumber);
}
reader.close();
}
use of org.apache.flink.runtime.util.event.NotificationListener in project flink by apache.
the class SeekRequest method addRequest.
protected final void addRequest(R request) throws IOException {
// check the error state of this channel
checkErroneous();
// write the current buffer and get the next one
this.requestsNotReturned.incrementAndGet();
if (this.closed || this.requestQueue.isClosed()) {
// if we found ourselves closed after the counter increment,
// decrement the counter again and do not forward the request
this.requestsNotReturned.decrementAndGet();
final NotificationListener listener;
synchronized (listenerLock) {
listener = allRequestsProcessedListener;
allRequestsProcessedListener = null;
}
if (listener != null) {
listener.onNotification();
}
throw new IOException("I/O channel already closed. Could not fulfill: " + request);
}
this.requestQueue.add(request);
}
Aggregations