Search in sources :

Example 36 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.

the class ZKLogSegmentMetadataStore method storeMaxTxnId.

@Override
public void storeMaxTxnId(Transaction<Object> txn, LogMetadataForWriter logMetadata, Versioned<Long> transactionId, Transaction.OpListener<Version> listener) {
    Version version = transactionId.getVersion();
    assert (version instanceof LongVersion);
    LongVersion zkVersion = (LongVersion) version;
    byte[] data = DLUtils.serializeTransactionId(transactionId.getValue());
    Op setDataOp = Op.setData(logMetadata.getMaxTxIdPath(), data, (int) zkVersion.getLongVersion());
    ZKOp zkOp = new ZKVersionedSetOp(setDataOp, listener);
    txn.addOp(zkOp);
}
Also used : ZKOp(org.apache.distributedlog.zk.ZKOp) Op(org.apache.zookeeper.Op) DefaultZKOp(org.apache.distributedlog.zk.DefaultZKOp) ZKVersionedSetOp(org.apache.distributedlog.zk.ZKVersionedSetOp) LongVersion(org.apache.bookkeeper.versioning.LongVersion) Version(org.apache.bookkeeper.versioning.Version) LongVersion(org.apache.bookkeeper.versioning.LongVersion) ZKOp(org.apache.distributedlog.zk.ZKOp) DefaultZKOp(org.apache.distributedlog.zk.DefaultZKOp) ZKVersionedSetOp(org.apache.distributedlog.zk.ZKVersionedSetOp)

Example 37 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.

the class ZKLogSegmentMetadataStore method processResult.

@Override
@SuppressWarnings("unchecked")
public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
    CompletableFuture<Versioned<List<String>>> result = ((CompletableFuture<Versioned<List<String>>>) ctx);
    if (KeeperException.Code.OK.intValue() == rc) {
        /**
         * cversion: the number of changes to the children of this znode *
         */
        LongVersion zkVersion = new LongVersion(stat.getCversion());
        result.complete(new Versioned(children, zkVersion));
    } else if (KeeperException.Code.NONODE.intValue() == rc) {
        result.completeExceptionally(new LogNotFoundException("Log " + path + " not found"));
    } else {
        result.completeExceptionally(new ZKException("Failed to get log segments from " + path, KeeperException.Code.get(rc)));
    }
}
Also used : ZKException(org.apache.distributedlog.exceptions.ZKException) Versioned(org.apache.bookkeeper.versioning.Versioned) LongVersion(org.apache.bookkeeper.versioning.LongVersion) LogNotFoundException(org.apache.distributedlog.exceptions.LogNotFoundException)

Example 38 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.

the class TestLogSegmentsZK method getMaxLogSegmentSequenceNo.

private static MaxLogSegmentSequenceNo getMaxLogSegmentSequenceNo(ZooKeeperClient zkc, URI uri, String streamName, DistributedLogConfiguration conf) throws Exception {
    Stat stat = new Stat();
    String logSegmentsPath = LogMetadata.getLogSegmentsPath(uri, streamName, conf.getUnpartitionedStreamName());
    byte[] data = zkc.get().getData(logSegmentsPath, false, stat);
    Versioned<byte[]> maxLSSNData = new Versioned<byte[]>(data, new LongVersion(stat.getVersion()));
    return new MaxLogSegmentSequenceNo(maxLSSNData);
}
Also used : Stat(org.apache.zookeeper.data.Stat) Versioned(org.apache.bookkeeper.versioning.Versioned) LongVersion(org.apache.bookkeeper.versioning.LongVersion)

Example 39 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.

the class AbstractZkLedgerManager method createLedgerMetadata.

@Override
public void createLedgerMetadata(final long ledgerId, final LedgerMetadata metadata, final GenericCallback<Void> ledgerCb) {
    String ledgerPath = getLedgerPath(ledgerId);
    StringCallback scb = new StringCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, String name) {
            if (rc == Code.OK.intValue()) {
                // update version
                metadata.setVersion(new LongVersion(0));
                ledgerCb.operationComplete(BKException.Code.OK, null);
            } else if (rc == Code.NODEEXISTS.intValue()) {
                LOG.warn("Failed to create ledger metadata for {} which already exist", ledgerId);
                ledgerCb.operationComplete(BKException.Code.LedgerExistException, null);
            } else {
                LOG.error("Could not create node for ledger {}", ledgerId, KeeperException.create(Code.get(rc), path));
                ledgerCb.operationComplete(BKException.Code.ZKException, null);
            }
        }
    };
    List<ACL> zkAcls = ZkUtils.getACLs(conf);
    ZkUtils.asyncCreateFullPathOptimistic(zk, ledgerPath, metadata.serialize(), zkAcls, CreateMode.PERSISTENT, scb, null);
}
Also used : LongVersion(org.apache.bookkeeper.versioning.LongVersion) StringCallback(org.apache.zookeeper.AsyncCallback.StringCallback) ACL(org.apache.zookeeper.data.ACL)

Example 40 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.

the class AbstractZkLedgerManager method removeLedgerMetadata.

/**
 * Removes ledger metadata from ZooKeeper and deletes its parent znodes
 * recursively if they dont have anymore children.
 *
 * @param ledgerId
 *            ledger identifier
 * @param version
 *            local version of metadata znode
 * @param cb
 *            callback object
 */
@Override
public void removeLedgerMetadata(final long ledgerId, final Version version, final GenericCallback<Void> cb) {
    int znodeVersion = -1;
    if (Version.NEW == version) {
        LOG.error("Request to delete ledger {} metadata with version set to the initial one", ledgerId);
        cb.operationComplete(BKException.Code.MetadataVersionException, (Void) null);
        return;
    } else if (Version.ANY != version) {
        if (!(version instanceof LongVersion)) {
            LOG.info("Not an instance of ZKVersion: {}", ledgerId);
            cb.operationComplete(BKException.Code.MetadataVersionException, (Void) null);
            return;
        } else {
            znodeVersion = (int) ((LongVersion) version).getLongVersion();
        }
    }
    VoidCallback callbackForDelete = new VoidCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx) {
            int bkRc;
            if (rc == KeeperException.Code.NONODE.intValue()) {
                LOG.warn("Ledger node does not exist in ZooKeeper: ledgerId={}", ledgerId);
                bkRc = BKException.Code.NoSuchLedgerExistsException;
            } else if (rc == KeeperException.Code.OK.intValue()) {
                // removed listener on ledgerId
                Set<LedgerMetadataListener> listenerSet = listeners.remove(ledgerId);
                if (null != listenerSet) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Remove registered ledger metadata listeners on ledger {} after ledger is deleted.", ledgerId, listenerSet);
                    }
                } else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("No ledger metadata listeners to remove from ledger {} when it's being deleted.", ledgerId);
                    }
                }
                bkRc = BKException.Code.OK;
            } else {
                bkRc = BKException.Code.ZKException;
            }
            cb.operationComplete(bkRc, (Void) null);
        }
    };
    String ledgerZnodePath = getLedgerPath(ledgerId);
    if (this instanceof HierarchicalLedgerManager || this instanceof LongHierarchicalLedgerManager) {
        /*
             * do recursive deletes only for HierarchicalLedgerManager and
             * LongHierarchicalLedgerManager
             */
        ZkUtils.asyncDeleteFullPathOptimistic(zk, ledgerZnodePath, znodeVersion, callbackForDelete, ledgerZnodePath);
    } else {
        zk.delete(ledgerZnodePath, znodeVersion, callbackForDelete, null);
    }
}
Also used : VoidCallback(org.apache.zookeeper.AsyncCallback.VoidCallback) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) NavigableSet(java.util.NavigableSet) LongVersion(org.apache.bookkeeper.versioning.LongVersion)

Aggregations

LongVersion (org.apache.bookkeeper.versioning.LongVersion)56 Test (org.junit.Test)37 Versioned (org.apache.bookkeeper.versioning.Versioned)29 Stat (org.apache.zookeeper.data.Stat)25 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)21 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Version (org.apache.bookkeeper.versioning.Version)18 GenericCallbackFuture (org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture)16 BKException (org.apache.bookkeeper.client.BKException)12 VoidCallback (org.apache.zookeeper.AsyncCallback.VoidCallback)12 Set (java.util.Set)11 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)10 StatCallback (org.apache.zookeeper.AsyncCallback.StatCallback)10 DataCallback (org.apache.zookeeper.AsyncCallback.DataCallback)9 Before (org.junit.Before)9 URI (java.net.URI)8 ZKException (org.apache.distributedlog.exceptions.ZKException)8 KeeperException (org.apache.zookeeper.KeeperException)8 Assert.assertEquals (org.junit.Assert.assertEquals)8 Assert.fail (org.junit.Assert.fail)8