Search in sources :

Example 1 with WriteSet

use of org.apache.bookkeeper.client.DistributionSchedule.WriteSet 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 WriteSet

use of org.apache.bookkeeper.client.DistributionSchedule.WriteSet 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)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)2 HashSet (java.util.HashSet)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 WriteSet (org.apache.bookkeeper.client.DistributionSchedule.WriteSet)2 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)2 ReadEntryCallback (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback)2 Unpooled (io.netty.buffer.Unpooled)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 SortedMap (java.util.SortedMap)1 BKNoSuchEntryException (org.apache.bookkeeper.client.BKException.BKNoSuchEntryException)1 Code (org.apache.bookkeeper.client.BKException.Code)1 LedgerEntries (org.apache.bookkeeper.client.api.LedgerEntries)1 LedgerEntryImpl (org.apache.bookkeeper.client.impl.LedgerEntryImpl)1 FutureEventListener (org.apache.bookkeeper.common.concurrent.FutureEventListener)1 BookieClient (org.apache.bookkeeper.proto.BookieClient)1 BookieProtocol (org.apache.bookkeeper.proto.BookieProtocol)1 GenericCallback (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback)1