Search in sources :

Example 1 with BKDigestMatchException

use of org.apache.bookkeeper.client.BKException.BKDigestMatchException in project bookkeeper by apache.

the class DigestManager method verifyDigestAndReturnLac.

public long verifyDigestAndReturnLac(ByteBuf dataReceived) throws BKDigestMatchException {
    if ((LAC_METADATA_LENGTH + macCodeLength) > dataReceived.readableBytes()) {
        logger.error("Data received is smaller than the minimum for this digest type." + " Either the packet it corrupt, or the wrong digest is configured. " + " Digest type: {}, Packet Length: {}", this.getClass().getName(), dataReceived.readableBytes());
        throw new BKDigestMatchException();
    }
    update(dataReceived.slice(0, LAC_METADATA_LENGTH));
    ByteBuf digest = PooledByteBufAllocator.DEFAULT.buffer(macCodeLength);
    try {
        populateValueAndReset(digest);
        if (digest.compareTo(dataReceived.slice(LAC_METADATA_LENGTH, macCodeLength)) != 0) {
            logger.error("Mac mismatch for ledger-id LAC: " + ledgerId);
            throw new BKDigestMatchException();
        }
    } finally {
        digest.release();
    }
    long actualLedgerId = dataReceived.readLong();
    long lac = dataReceived.readLong();
    if (actualLedgerId != ledgerId) {
        logger.error("Ledger-id mismatch in authenticated message, expected: " + ledgerId + " , actual: " + actualLedgerId);
        throw new BKDigestMatchException();
    }
    return lac;
}
Also used : BKDigestMatchException(org.apache.bookkeeper.client.BKException.BKDigestMatchException) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with BKDigestMatchException

use of org.apache.bookkeeper.client.BKException.BKDigestMatchException in project bookkeeper by apache.

the class DigestManager method verifyDigest.

private void verifyDigest(long entryId, ByteBuf dataReceived, boolean skipEntryIdCheck) throws BKDigestMatchException {
    if ((METADATA_LENGTH + macCodeLength) > dataReceived.readableBytes()) {
        logger.error("Data received is smaller than the minimum for this digest type. " + " Either the packet it corrupt, or the wrong digest is configured. " + " Digest type: {}, Packet Length: {}", this.getClass().getName(), dataReceived.readableBytes());
        throw new BKDigestMatchException();
    }
    update(dataReceived.slice(0, METADATA_LENGTH));
    int offset = METADATA_LENGTH + macCodeLength;
    update(dataReceived.slice(offset, dataReceived.readableBytes() - offset));
    ByteBuf digest = PooledByteBufAllocator.DEFAULT.buffer(macCodeLength);
    populateValueAndReset(digest);
    try {
        if (digest.compareTo(dataReceived.slice(METADATA_LENGTH, macCodeLength)) != 0) {
            logger.error("Mac mismatch for ledger-id: " + ledgerId + ", entry-id: " + entryId);
            throw new BKDigestMatchException();
        }
    } finally {
        digest.release();
    }
    long actualLedgerId = dataReceived.readLong();
    long actualEntryId = dataReceived.readLong();
    if (actualLedgerId != ledgerId) {
        logger.error("Ledger-id mismatch in authenticated message, expected: " + ledgerId + " , actual: " + actualLedgerId);
        throw new BKDigestMatchException();
    }
    if (!skipEntryIdCheck && actualEntryId != entryId) {
        logger.error("Entry-id mismatch in authenticated message, expected: " + entryId + " , actual: " + actualEntryId);
        throw new BKDigestMatchException();
    }
}
Also used : BKDigestMatchException(org.apache.bookkeeper.client.BKException.BKDigestMatchException) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with BKDigestMatchException

use of org.apache.bookkeeper.client.BKException.BKDigestMatchException in project bookkeeper by apache.

the class MockBookKeeperTestCase method setupBookieClientAddEntry.

@SuppressWarnings("unchecked")
protected void setupBookieClientAddEntry() {
    doAnswer(invokation -> {
        Object[] args = invokation.getArguments();
        BookkeeperInternalCallbacks.WriteCallback callback = (BookkeeperInternalCallbacks.WriteCallback) args[5];
        BookieSocketAddress bookieSocketAddress = (BookieSocketAddress) args[0];
        long ledgerId = (Long) args[1];
        long entryId = (Long) args[3];
        ByteBufList toSend = (ByteBufList) args[4];
        Object ctx = args[6];
        int options = (int) args[7];
        boolean isRecoveryAdd = ((short) options & BookieProtocol.FLAG_RECOVERY_ADD) == BookieProtocol.FLAG_RECOVERY_ADD;
        executor.executeOrdered(ledgerId, () -> {
            byte[] entry;
            try {
                entry = extractEntryPayload(ledgerId, entryId, toSend);
            } catch (BKDigestMatchException e) {
                callback.writeComplete(Code.DigestMatchException, ledgerId, entryId, bookieSocketAddress, ctx);
                return;
            }
            boolean fenced = fencedLedgers.contains(ledgerId);
            if (fenced && !isRecoveryAdd) {
                callback.writeComplete(BKException.Code.LedgerFencedException, ledgerId, entryId, bookieSocketAddress, ctx);
            } else {
                if (failedBookies.contains(bookieSocketAddress)) {
                    callback.writeComplete(NoBookieAvailableException, ledgerId, entryId, bookieSocketAddress, ctx);
                    return;
                }
                if (getMockLedgerContentsInBookie(ledgerId, bookieSocketAddress).isEmpty()) {
                    registerMockEntryForRead(ledgerId, BookieProtocol.LAST_ADD_CONFIRMED, bookieSocketAddress, new byte[0], BookieProtocol.INVALID_ENTRY_ID);
                }
                registerMockEntryForRead(ledgerId, entryId, bookieSocketAddress, entry, ledgerId);
                callback.writeComplete(BKException.Code.OK, ledgerId, entryId, bookieSocketAddress, ctx);
            }
        });
        return null;
    }).when(bookieClient).addEntry(any(BookieSocketAddress.class), anyLong(), any(byte[].class), anyLong(), any(ByteBufList.class), any(BookkeeperInternalCallbacks.WriteCallback.class), any(), anyInt());
}
Also used : ByteBufList(org.apache.bookkeeper.util.ByteBufList) BKDigestMatchException(org.apache.bookkeeper.client.BKException.BKDigestMatchException) BookkeeperInternalCallbacks(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 4 with BKDigestMatchException

use of org.apache.bookkeeper.client.BKException.BKDigestMatchException in project bookkeeper by apache.

the class PendingReadLacOp method readLacComplete.

@Override
public void readLacComplete(int rc, long ledgerId, final ByteBuf lacBuffer, final ByteBuf lastEntryBuffer, Object ctx) {
    int bookieIndex = (Integer) ctx;
    // add the response to coverage set
    coverageSet.addBookie(bookieIndex, rc);
    numResponsesPending--;
    boolean heardValidResponse = false;
    if (completed) {
        return;
    }
    if (rc == BKException.Code.OK) {
        try {
            // Extract lac from FileInfo on the ledger.
            if (lacBuffer != null && lacBuffer.readableBytes() > 0) {
                long lac = lh.macManager.verifyDigestAndReturnLac(lacBuffer);
                if (lac > maxLac) {
                    maxLac = lac;
                }
            }
            // Extract lac from last entry on the disk
            if (lastEntryBuffer != null && lastEntryBuffer.readableBytes() > 0) {
                RecoveryData recoveryData = lh.macManager.verifyDigestAndReturnLastConfirmed(lastEntryBuffer);
                long recoveredLac = recoveryData.getLastAddConfirmed();
                if (recoveredLac > maxLac) {
                    maxLac = recoveredLac;
                }
            }
            heardValidResponse = true;
        } catch (BKDigestMatchException e) {
            // Too bad, this bookie did not give us a valid answer, we
            // still might be able to recover. So, continue
            LOG.error("Mac mismatch while reading  ledger: " + ledgerId + " LAC from bookie: " + lh.metadata.currentEnsemble.get(bookieIndex));
            rc = BKException.Code.DigestMatchException;
        }
    }
    if (rc == BKException.Code.NoSuchLedgerExistsException || rc == BKException.Code.NoSuchEntryException) {
        heardValidResponse = true;
    }
    if (rc == BKException.Code.UnauthorizedAccessException && !completed) {
        cb.getLacComplete(rc, maxLac);
        completed = true;
        return;
    }
    if (!heardValidResponse && BKException.Code.OK != rc) {
        lastSeenError = rc;
    }
    // We don't consider a success until we have coverage set responses.
    if (heardValidResponse && coverageSet.checkCovered() && !completed) {
        completed = true;
        if (LOG.isDebugEnabled()) {
            LOG.debug("Read LAC complete with enough validResponse for ledger: {} LAC: {}", ledgerId, maxLac);
        }
        cb.getLacComplete(BKException.Code.OK, maxLac);
        return;
    }
    if (numResponsesPending == 0 && !completed) {
        LOG.info("While readLac ledger: " + ledgerId + " did not hear success responses from all of ensemble");
        cb.getLacComplete(lastSeenError, maxLac);
    }
}
Also used : BKDigestMatchException(org.apache.bookkeeper.client.BKException.BKDigestMatchException) RecoveryData(org.apache.bookkeeper.proto.checksum.DigestManager.RecoveryData)

Example 5 with BKDigestMatchException

use of org.apache.bookkeeper.client.BKException.BKDigestMatchException in project bookkeeper by apache.

the class ReadLastConfirmedOp method readEntryComplete.

public synchronized void readEntryComplete(final int rc, final long ledgerId, final long entryId, final ByteBuf buffer, final Object ctx) {
    int bookieIndex = (Integer) ctx;
    // add the response to coverage set
    coverageSet.addBookie(bookieIndex, rc);
    numResponsesPending--;
    boolean heardValidResponse = false;
    if (rc == BKException.Code.OK) {
        try {
            RecoveryData recoveryData = lh.macManager.verifyDigestAndReturnLastConfirmed(buffer);
            if (recoveryData.getLastAddConfirmed() > maxRecoveredData.getLastAddConfirmed()) {
                maxRecoveredData = recoveryData;
            }
            heardValidResponse = true;
        } catch (BKDigestMatchException e) {
            // Too bad, this bookie didn't give us a valid answer, we
            // still might be able to recover though so continue
            LOG.error("Mac mismatch for ledger: " + ledgerId + ", entry: " + entryId + " while reading last entry from bookie: " + lh.metadata.currentEnsemble.get(bookieIndex));
        }
    }
    if (rc == BKException.Code.NoSuchLedgerExistsException || rc == BKException.Code.NoSuchEntryException) {
        // this still counts as a valid response, e.g., if the client crashed without writing any entry
        heardValidResponse = true;
    }
    if (rc == BKException.Code.UnauthorizedAccessException && !completed) {
        cb.readLastConfirmedDataComplete(rc, maxRecoveredData);
        completed = true;
    }
    if (!heardValidResponse && BKException.Code.OK != rc) {
        lastSeenError = rc;
    }
    // other return codes dont count as valid responses
    if (heardValidResponse && coverageSet.checkCovered() && !completed) {
        completed = true;
        if (LOG.isDebugEnabled()) {
            LOG.debug("Read Complete with enough validResponses for ledger: {}, entry: {}", ledgerId, entryId);
        }
        cb.readLastConfirmedDataComplete(BKException.Code.OK, maxRecoveredData);
        return;
    }
    if (numResponsesPending == 0 && !completed) {
        // Have got all responses back but was still not enough, just fail the operation
        LOG.error("While readLastConfirmed ledger: {} did not hear success responses from all quorums", ledgerId);
        cb.readLastConfirmedDataComplete(lastSeenError, maxRecoveredData);
    }
}
Also used : BKDigestMatchException(org.apache.bookkeeper.client.BKException.BKDigestMatchException) RecoveryData(org.apache.bookkeeper.proto.checksum.DigestManager.RecoveryData)

Aggregations

BKDigestMatchException (org.apache.bookkeeper.client.BKException.BKDigestMatchException)6 ByteBuf (io.netty.buffer.ByteBuf)2 RecoveryData (org.apache.bookkeeper.proto.checksum.DigestManager.RecoveryData)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)1 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)1 BookkeeperInternalCallbacks (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks)1 ByteBufList (org.apache.bookkeeper.util.ByteBufList)1 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)1