Search in sources :

Example 1 with ReadEntryCallback

use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback in project bookkeeper by apache.

the class LedgerReader method readLacs.

public void readLacs(final LedgerHandle lh, long eid, final GenericCallback<Set<ReadResult<Long>>> callback) {
    WriteSet writeSet = lh.distributionSchedule.getWriteSet(eid);
    final AtomicInteger numBookies = new AtomicInteger(writeSet.size());
    final Set<ReadResult<Long>> readResults = new HashSet<ReadResult<Long>>();
    ReadEntryCallback readEntryCallback = (rc, lid, eid1, buffer, ctx) -> {
        InetSocketAddress bookieAddress = (InetSocketAddress) ctx;
        ReadResult<Long> rr;
        if (BKException.Code.OK != rc) {
            rr = new ReadResult<Long>(eid1, rc, null, bookieAddress);
        } else {
            try {
                DigestManager.RecoveryData data = lh.macManager.verifyDigestAndReturnLastConfirmed(buffer);
                rr = new ReadResult<Long>(eid1, BKException.Code.OK, data.getLastAddConfirmed(), bookieAddress);
            } catch (BKException.BKDigestMatchException e) {
                rr = new ReadResult<Long>(eid1, BKException.Code.DigestMatchException, null, bookieAddress);
            }
        }
        readResults.add(rr);
        if (numBookies.decrementAndGet() == 0) {
            callback.operationComplete(BKException.Code.OK, readResults);
        }
    };
    ArrayList<BookieSocketAddress> ensemble = lh.getLedgerMetadata().getEnsemble(eid);
    for (int i = 0; i < writeSet.size(); i++) {
        int idx = writeSet.get(i);
        bookieClient.readEntry(ensemble.get(idx), lh.getId(), eid, readEntryCallback, ensemble.get(idx), BookieProtocol.FLAG_NONE);
    }
}
Also used : ReadEntryCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback) FutureEventListener(org.apache.bookkeeper.common.concurrent.FutureEventListener) BookieProtocol(org.apache.bookkeeper.proto.BookieProtocol) GenericCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback) Set(java.util.Set) BKNoSuchEntryException(org.apache.bookkeeper.client.BKException.BKNoSuchEntryException) InetSocketAddress(java.net.InetSocketAddress) WriteSet(org.apache.bookkeeper.client.DistributionSchedule.WriteSet) Code(org.apache.bookkeeper.client.BKException.Code) Unpooled(io.netty.buffer.Unpooled) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LedgerEntryImpl(org.apache.bookkeeper.client.impl.LedgerEntryImpl) DigestManager(org.apache.bookkeeper.proto.checksum.DigestManager) BookieClient(org.apache.bookkeeper.proto.BookieClient) LedgerEntries(org.apache.bookkeeper.client.api.LedgerEntries) SortedMap(java.util.SortedMap) ReadEntryCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback) InetSocketAddress(java.net.InetSocketAddress) WriteSet(org.apache.bookkeeper.client.DistributionSchedule.WriteSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) HashSet(java.util.HashSet)

Example 2 with ReadEntryCallback

use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback in project bookkeeper by apache.

the class LedgerReader method readEntriesFromAllBookies.

public void readEntriesFromAllBookies(final LedgerHandle lh, long eid, final GenericCallback<Set<ReadResult<ByteBuf>>> callback) {
    WriteSet writeSet = lh.distributionSchedule.getWriteSet(eid);
    final AtomicInteger numBookies = new AtomicInteger(writeSet.size());
    final Set<ReadResult<ByteBuf>> readResults = new HashSet<>();
    ReadEntryCallback readEntryCallback = new ReadEntryCallback() {

        @Override
        public void readEntryComplete(int rc, long lid, long eid, ByteBuf buffer, Object ctx) {
            BookieSocketAddress bookieAddress = (BookieSocketAddress) ctx;
            ReadResult<ByteBuf> rr;
            if (BKException.Code.OK != rc) {
                rr = new ReadResult<>(eid, rc, null, bookieAddress.getSocketAddress());
            } else {
                ByteBuf content;
                try {
                    content = lh.macManager.verifyDigestAndReturnData(eid, buffer);
                    ByteBuf toRet = Unpooled.copiedBuffer(content);
                    rr = new ReadResult<>(eid, BKException.Code.OK, toRet, bookieAddress.getSocketAddress());
                } catch (BKException.BKDigestMatchException e) {
                    rr = new ReadResult<>(eid, BKException.Code.DigestMatchException, null, bookieAddress.getSocketAddress());
                } finally {
                    buffer.release();
                }
            }
            readResults.add(rr);
            if (numBookies.decrementAndGet() == 0) {
                callback.operationComplete(BKException.Code.OK, readResults);
            }
        }
    };
    ArrayList<BookieSocketAddress> ensemble = lh.getLedgerMetadata().getEnsemble(eid);
    for (int i = 0; i < writeSet.size(); i++) {
        int idx = writeSet.get(i);
        bookieClient.readEntry(ensemble.get(idx), lh.getId(), eid, readEntryCallback, ensemble.get(idx), BookieProtocol.FLAG_NONE);
    }
}
Also used : ReadEntryCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback) WriteSet(org.apache.bookkeeper.client.DistributionSchedule.WriteSet) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) HashSet(java.util.HashSet)

Example 3 with ReadEntryCallback

use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback in project bookkeeper by apache.

the class TestPerChannelBookieClient method testRequestCompletesAfterDisconnectRace.

/**
 * Test that requests are completed even if the channel is disconnected
 * {@link https://issues.apache.org/jira/browse/BOOKKEEPER-668}.
 */
@Test
public void testRequestCompletesAfterDisconnectRace() throws Exception {
    ServerConfiguration conf = killBookie(0);
    Bookie delayBookie = new Bookie(conf) {

        @Override
        public ByteBuf readEntry(long ledgerId, long entryId) throws IOException, NoLedgerException {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                throw new IOException("Interrupted waiting", ie);
            }
            return super.readEntry(ledgerId, entryId);
        }
    };
    bsConfs.add(conf);
    bs.add(startBookie(conf, delayBookie));
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    final OrderedExecutor executor = getOrderedSafeExecutor();
    BookieSocketAddress addr = getBookie(0);
    final PerChannelBookieClient client = new PerChannelBookieClient(executor, eventLoopGroup, addr, authProvider, extRegistry);
    final CountDownLatch completion = new CountDownLatch(1);
    final ReadEntryCallback cb = new ReadEntryCallback() {

        @Override
        public void readEntryComplete(int rc, long ledgerId, long entryId, ByteBuf buffer, Object ctx) {
            completion.countDown();
        }
    };
    client.connectIfNeededAndDoOp(new GenericCallback<PerChannelBookieClient>() {

        @Override
        public void operationComplete(final int rc, PerChannelBookieClient pcbc) {
            if (rc != BKException.Code.OK) {
                executor.executeOrdered(1, new SafeRunnable() {

                    @Override
                    public void safeRun() {
                        cb.readEntryComplete(rc, 1, 1, null, null);
                    }
                });
                return;
            }
            client.readEntry(1, 1, cb, null, BookieProtocol.FLAG_DO_FENCING, "00000111112222233333".getBytes());
        }
    });
    Thread.sleep(1000);
    client.disconnect();
    client.close();
    assertTrue("Request should have completed", completion.await(5, TimeUnit.SECONDS));
    eventLoopGroup.shutdownGracefully();
    executor.shutdown();
}
Also used : ReadEntryCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) SafeRunnable(org.apache.bookkeeper.util.SafeRunnable) IOException(java.io.IOException) OrderedExecutor(org.apache.bookkeeper.common.util.OrderedExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bookie(org.apache.bookkeeper.bookie.Bookie) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 4 with ReadEntryCallback

use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback in project distributedlog by twitter.

the class LedgerReader method readEntriesFromAllBookies.

public void readEntriesFromAllBookies(final LedgerHandle lh, long eid, final GenericCallback<Set<ReadResult<InputStream>>> callback) {
    List<Integer> writeSet = lh.distributionSchedule.getWriteSet(eid);
    final AtomicInteger numBookies = new AtomicInteger(writeSet.size());
    final Set<ReadResult<InputStream>> readResults = new HashSet<ReadResult<InputStream>>();
    ReadEntryCallback readEntryCallback = new ReadEntryCallback() {

        @Override
        public void readEntryComplete(int rc, long lid, long eid, ChannelBuffer buffer, Object ctx) {
            BookieSocketAddress bookieAddress = (BookieSocketAddress) ctx;
            ReadResult<InputStream> rr;
            if (BKException.Code.OK != rc) {
                rr = new ReadResult<InputStream>(eid, rc, null, bookieAddress.getSocketAddress());
            } else {
                try {
                    ChannelBufferInputStream is = lh.macManager.verifyDigestAndReturnData(eid, buffer);
                    rr = new ReadResult<InputStream>(eid, BKException.Code.OK, is, bookieAddress.getSocketAddress());
                } catch (BKException.BKDigestMatchException e) {
                    rr = new ReadResult<InputStream>(eid, BKException.Code.DigestMatchException, null, bookieAddress.getSocketAddress());
                }
            }
            readResults.add(rr);
            if (numBookies.decrementAndGet() == 0) {
                callback.operationComplete(BKException.Code.OK, readResults);
            }
        }
    };
    ArrayList<BookieSocketAddress> ensemble = lh.getLedgerMetadata().getEnsemble(eid);
    for (Integer idx : writeSet) {
        bookieClient.readEntry(ensemble.get(idx), lh.getId(), eid, readEntryCallback, ensemble.get(idx));
    }
}
Also used : ReadEntryCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) InputStream(java.io.InputStream) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) HashSet(java.util.HashSet)

Example 5 with ReadEntryCallback

use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback in project distributedlog by twitter.

the class LedgerReader method readLacs.

public void readLacs(final LedgerHandle lh, long eid, final GenericCallback<Set<ReadResult<Long>>> callback) {
    List<Integer> writeSet = lh.distributionSchedule.getWriteSet(eid);
    final AtomicInteger numBookies = new AtomicInteger(writeSet.size());
    final Set<ReadResult<Long>> readResults = new HashSet<ReadResult<Long>>();
    ReadEntryCallback readEntryCallback = new ReadEntryCallback() {

        @Override
        public void readEntryComplete(int rc, long lid, long eid, ChannelBuffer buffer, Object ctx) {
            InetSocketAddress bookieAddress = (InetSocketAddress) ctx;
            ReadResult<Long> rr;
            if (BKException.Code.OK != rc) {
                rr = new ReadResult<Long>(eid, rc, null, bookieAddress);
            } else {
                try {
                    DigestManager.RecoveryData data = lh.macManager.verifyDigestAndReturnLastConfirmed(buffer);
                    rr = new ReadResult<Long>(eid, BKException.Code.OK, data.lastAddConfirmed, bookieAddress);
                } catch (BKException.BKDigestMatchException e) {
                    rr = new ReadResult<Long>(eid, BKException.Code.DigestMatchException, null, bookieAddress);
                }
            }
            readResults.add(rr);
            if (numBookies.decrementAndGet() == 0) {
                callback.operationComplete(BKException.Code.OK, readResults);
            }
        }
    };
    ArrayList<BookieSocketAddress> ensemble = lh.getLedgerMetadata().getEnsemble(eid);
    for (Integer idx : writeSet) {
        bookieClient.readEntry(ensemble.get(idx), lh.getId(), eid, readEntryCallback, ensemble.get(idx));
    }
}
Also used : ReadEntryCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback) InetSocketAddress(java.net.InetSocketAddress) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) HashSet(java.util.HashSet)

Aggregations

BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)5 ReadEntryCallback (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback)5 HashSet (java.util.HashSet)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ByteBuf (io.netty.buffer.ByteBuf)3 InetSocketAddress (java.net.InetSocketAddress)2 WriteSet (org.apache.bookkeeper.client.DistributionSchedule.WriteSet)2 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)2 Unpooled (io.netty.buffer.Unpooled)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 SortedMap (java.util.SortedMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Bookie (org.apache.bookkeeper.bookie.Bookie)1 BKNoSuchEntryException (org.apache.bookkeeper.client.BKException.BKNoSuchEntryException)1