Search in sources :

Example 1 with LedgerData

use of org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorageDataFormats.LedgerData in project bookkeeper by apache.

the class LedgerMetadataIndex method setFenced.

public boolean setFenced(long ledgerId) throws IOException {
    LedgerData ledgerData = get(ledgerId);
    if (ledgerData.getFenced()) {
        return false;
    }
    LedgerData newLedgerData = LedgerData.newBuilder(ledgerData).setFenced(true).build();
    if (ledgers.put(ledgerId, newLedgerData) == null) {
        // Ledger had been deleted
        if (log.isDebugEnabled()) {
            log.debug("Re-inserted fenced ledger {}", ledgerId);
        }
        ledgersCount.incrementAndGet();
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Set fenced ledger {}", ledgerId);
        }
    }
    pendingLedgersUpdates.add(new SimpleEntry<Long, LedgerData>(ledgerId, newLedgerData));
    pendingDeletedLedgers.remove(ledgerId);
    return true;
}
Also used : LedgerData(org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorageDataFormats.LedgerData)

Example 2 with LedgerData

use of org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorageDataFormats.LedgerData in project bookkeeper by apache.

the class DbLedgerStorage method addLedgerToIndex.

/**
 * Add an already existing ledger to the index.
 *
 * <p>This method is only used as a tool to help the migration from InterleaveLedgerStorage to DbLedgerStorage
 *
 * @param ledgerId
 *            the ledger id
 * @param entries
 *            a map of entryId -> location
 * @return the number of
 */
public long addLedgerToIndex(long ledgerId, boolean isFenced, byte[] masterKey, Iterable<SortedMap<Long, Long>> entries) throws Exception {
    LedgerData ledgerData = LedgerData.newBuilder().setExists(true).setFenced(isFenced).setMasterKey(ByteString.copyFrom(masterKey)).build();
    ledgerIndex.set(ledgerId, ledgerData);
    AtomicLong numberOfEntries = new AtomicLong();
    // Iterate over all the entries pages
    Batch batch = entryLocationIndex.newBatch();
    entries.forEach(map -> {
        map.forEach((entryId, location) -> {
            try {
                entryLocationIndex.addLocation(batch, ledgerId, entryId, location);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            numberOfEntries.incrementAndGet();
        });
    });
    batch.flush();
    batch.close();
    return numberOfEntries.get();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Batch(org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorage.Batch) LedgerData(org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorageDataFormats.LedgerData) IOException(java.io.IOException)

Example 3 with LedgerData

use of org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorageDataFormats.LedgerData in project bookkeeper by apache.

the class LedgerMetadataIndex method setMasterKey.

public void setMasterKey(long ledgerId, byte[] masterKey) throws IOException {
    LedgerData ledgerData = ledgers.get(ledgerId);
    if (ledgerData == null) {
        // New ledger inserted
        ledgerData = LedgerData.newBuilder().setExists(true).setFenced(false).setMasterKey(ByteString.copyFrom(masterKey)).build();
        if (log.isDebugEnabled()) {
            log.debug("Inserting new ledger {}", ledgerId);
        }
    } else {
        byte[] storedMasterKey = ledgerData.getMasterKey().toByteArray();
        if (ArrayUtil.isArrayAllZeros(storedMasterKey)) {
            // update master key of the ledger
            ledgerData = LedgerData.newBuilder(ledgerData).setMasterKey(ByteString.copyFrom(masterKey)).build();
            if (log.isDebugEnabled()) {
                log.debug("Replace old master key {} with new master key {}", storedMasterKey, masterKey);
            }
        } else if (!Arrays.equals(storedMasterKey, masterKey) && !ArrayUtil.isArrayAllZeros(masterKey)) {
            log.warn("Ledger {} masterKey in db can only be set once.", ledgerId);
            throw new IOException(BookieException.create(BookieException.Code.IllegalOpException));
        }
    }
    if (ledgers.put(ledgerId, ledgerData) == null) {
        ledgersCount.incrementAndGet();
    }
    pendingLedgersUpdates.add(new SimpleEntry<Long, LedgerData>(ledgerId, ledgerData));
    pendingDeletedLedgers.remove(ledgerId);
}
Also used : LedgerData(org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorageDataFormats.LedgerData) IOException(java.io.IOException)

Aggregations

LedgerData (org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorageDataFormats.LedgerData)3 IOException (java.io.IOException)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Batch (org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorage.Batch)1