Search in sources :

Example 1 with NotificationListener

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();
        }
    }
}
Also used : NotificationListener(org.apache.flink.runtime.util.event.NotificationListener)

Example 2 with NotificationListener

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();
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) ByteBuffer(java.nio.ByteBuffer) NetworkBuffer(org.apache.flink.runtime.io.network.buffer.NetworkBuffer) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) NotificationListener(org.apache.flink.runtime.util.event.NotificationListener) Test(org.junit.Test)

Example 3 with NotificationListener

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);
}
Also used : IOException(java.io.IOException) NotificationListener(org.apache.flink.runtime.util.event.NotificationListener)

Aggregations

NotificationListener (org.apache.flink.runtime.util.event.NotificationListener)3 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Buffer (org.apache.flink.runtime.io.network.buffer.Buffer)1 NetworkBuffer (org.apache.flink.runtime.io.network.buffer.NetworkBuffer)1 Test (org.junit.Test)1