diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-07-05 16:06:56 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-07-05 16:06:56 +0200 |
commit | 0409e74ad21d1fccf3909f96bda64bacd54b102d (patch) | |
tree | 6c53453bbecc0965004cfac45b8870519c4b98e7 /zkfacade/src | |
parent | c84211d12966c9a3a7c62a1f0440064f52921d94 (diff) |
Towards transactional application delete
Diffstat (limited to 'zkfacade/src')
4 files changed, 60 insertions, 48 deletions
diff --git a/zkfacade/src/main/java/com/yahoo/vespa/curator/recipes/CuratorCounter.java b/zkfacade/src/main/java/com/yahoo/vespa/curator/recipes/CuratorCounter.java index 8a5c2d35851..950d29d8070 100644 --- a/zkfacade/src/main/java/com/yahoo/vespa/curator/recipes/CuratorCounter.java +++ b/zkfacade/src/main/java/com/yahoo/vespa/curator/recipes/CuratorCounter.java @@ -14,25 +14,46 @@ import org.apache.curator.framework.recipes.atomic.DistributedAtomicLong; public class CuratorCounter { private final DistributedAtomicLong counter; + private final String counterPath; public CuratorCounter(Curator curator, String counterPath) { this.counter = curator.createAtomicCounter(counterPath); + this.counterPath = counterPath; } /** - * Atomically increment and return next value. + * Atomically increment and return resulting value. * - * @return the new value. + * @return the resulting value + * @throws IllegalStateException if increment fails */ public synchronized long next() { try { AtomicValue<Long> value = counter.increment(); if (!value.succeeded()) { - throw new RuntimeException("Increment did not succeed"); + throw new IllegalStateException("Increment did not succeed"); } return value.postValue(); } catch (Exception e) { - throw new RuntimeException("Unable to get next value", e); + throw new IllegalStateException("Unable to get next value", e); + } + } + + /** + * Atomically decrement and return the resulting value. + * + * @return the resulting value + * @throws IllegalStateException if decrement fails + */ + public synchronized long previous() { + try { + AtomicValue<Long> value = counter.subtract(1L); + if (!value.succeeded()) { + throw new IllegalStateException("Decrement did not succeed"); + } + return value.postValue(); + } catch (Exception e) { + throw new IllegalStateException("Unable to get previous value", e); } } @@ -40,7 +61,7 @@ public class CuratorCounter { try { counter.trySet(current); } catch (Exception e) { - throw new RuntimeException("Unable to set value", e); + throw new IllegalStateException("Unable to set value", e); } } @@ -60,8 +81,13 @@ public class CuratorCounter { try { counter.initialize(value); } catch (Exception e) { - throw new RuntimeException("Error initializing atomic counter", e); + throw new IllegalStateException("Error initializing atomic counter", e); } } + + @Override + public String toString() { + return "curator counter " + counterPath; + } } diff --git a/zkfacade/src/main/java/com/yahoo/vespa/curator/transaction/CuratorDeleteOperation.java b/zkfacade/src/main/java/com/yahoo/vespa/curator/transaction/CuratorDeleteOperation.java index c9e180b4827..5ff0d4a143f 100644 --- a/zkfacade/src/main/java/com/yahoo/vespa/curator/transaction/CuratorDeleteOperation.java +++ b/zkfacade/src/main/java/com/yahoo/vespa/curator/transaction/CuratorDeleteOperation.java @@ -12,14 +12,16 @@ import org.apache.curator.framework.api.transaction.CuratorTransaction; class CuratorDeleteOperation implements CuratorOperation { private final String path; + private final boolean throwIfNotExist; - CuratorDeleteOperation(String path) { + CuratorDeleteOperation(String path, boolean throwIfNotExist) { this.path = path; + this.throwIfNotExist = throwIfNotExist; } @Override public void check(Curator curator) { - if ( ! curator.exists(Path.fromString(path)) ) + if ( throwIfNotExist && ! curator.exists(Path.fromString(path)) ) throw new IllegalStateException("Cannot perform " + this + ": Path does not exist"); } diff --git a/zkfacade/src/main/java/com/yahoo/vespa/curator/transaction/CuratorOperations.java b/zkfacade/src/main/java/com/yahoo/vespa/curator/transaction/CuratorOperations.java index 6dc009b4cfd..55ec5eab2d8 100644 --- a/zkfacade/src/main/java/com/yahoo/vespa/curator/transaction/CuratorOperations.java +++ b/zkfacade/src/main/java/com/yahoo/vespa/curator/transaction/CuratorOperations.java @@ -22,8 +22,19 @@ public class CuratorOperations { return new CuratorCreateOperation(path, Optional.empty()); } + /** + * Deletes this path recursively. + * Throws an IllegalStateException in check() if the path does not exist. + */ + public static CuratorOperation deleteOrThrow(String path) { + return new CuratorDeleteOperation(path, true); + } + + /** + * Deletes this path recursively. Does nothing if the path does not exist. + */ public static CuratorOperation delete(String path) { - return new CuratorDeleteOperation(path); + return new CuratorDeleteOperation(path, false); } } diff --git a/zkfacade/src/main/java/com/yahoo/vespa/curator/transaction/CuratorTransaction.java b/zkfacade/src/main/java/com/yahoo/vespa/curator/transaction/CuratorTransaction.java index 886a08a3ca0..aa7643fb7ae 100644 --- a/zkfacade/src/main/java/com/yahoo/vespa/curator/transaction/CuratorTransaction.java +++ b/zkfacade/src/main/java/com/yahoo/vespa/curator/transaction/CuratorTransaction.java @@ -1,58 +1,42 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.curator.transaction; -import com.yahoo.transaction.Transaction; +import com.yahoo.transaction.AbstractTransaction; import com.yahoo.vespa.curator.Curator; -import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.api.transaction.CuratorTransactionFinal; -import java.util.ArrayList; -import java.util.List; -import java.util.logging.Logger; - /** * Transaction implementation against ZooKeeper. * * @author lulf */ -public class CuratorTransaction implements Transaction { +public class CuratorTransaction extends AbstractTransaction<CuratorOperation> { - private static final Logger log = Logger.getLogger(CuratorTransaction.class.getName()); - private final List<Operation> operations = new ArrayList<>(); private final Curator curator; public CuratorTransaction(Curator curator) { this.curator = curator; } - - @Override - public Transaction add(Operation operation) { - this.operations.add(operation); - return this; + + /** Returns a curator transaction having a single operation */ + public static CuratorTransaction from(CuratorOperation operation, Curator curator) { + CuratorTransaction transaction = new CuratorTransaction(curator); + transaction.add(operation); + return transaction; } @Override - public Transaction add(List<Operation> operations) { - this.operations.addAll(operations); - return this; - } - - @Override - public List<Operation> operations() { return operations; } - - @Override public void prepare() { - for (Operation operation : operations) - ((CuratorOperation)operation).check(curator); + for (CuratorOperation operation : operations()) + operation.check(curator); } @Override public void commit() { try { org.apache.curator.framework.api.transaction.CuratorTransaction transaction = curator.framework().inTransaction(); - for (Operation operation : operations) { - CuratorOperation zkOperation = (CuratorOperation) operation; - transaction = zkOperation.and(transaction); + for (CuratorOperation operation : operations()) { + transaction = operation.and(transaction); } ((CuratorTransactionFinal) transaction).commit(); } catch (Exception e) { @@ -60,15 +44,4 @@ public class CuratorTransaction implements Transaction { } } - @Override - public void rollbackOrLog() { - log.severe("The following ZooKeeper operations were incorrectly committed and probably require " + - "manual correction: " + operations); - } - - @Override - public void close() { - operations.clear(); - } - } |