Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.consistent.impl; |
| 17 | |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 18 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 19 | import org.onosproject.store.service.AtomicCounter; |
| 20 | import org.onosproject.store.service.StorageException; |
| 21 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 22 | import java.util.concurrent.CompletableFuture; |
| 23 | import java.util.concurrent.ExecutionException; |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 24 | import java.util.concurrent.ScheduledExecutorService; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 25 | import java.util.concurrent.TimeUnit; |
| 26 | import java.util.concurrent.TimeoutException; |
| 27 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Default implementation for a distributed AtomicCounter backed by |
| 30 | * partitioned Raft DB. |
| 31 | * <p> |
| 32 | * The initial value will be zero. |
| 33 | */ |
| 34 | public class DefaultAtomicCounter implements AtomicCounter { |
| 35 | |
| 36 | private static final int OPERATION_TIMEOUT_MILLIS = 5000; |
| 37 | |
| 38 | private final AsyncAtomicCounter asyncCounter; |
| 39 | |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 40 | public DefaultAtomicCounter(String name, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 41 | Database database, |
| 42 | boolean retryOnException, |
| 43 | boolean meteringEnabled, |
| 44 | ScheduledExecutorService retryExecutor) { |
| 45 | asyncCounter = new DefaultAsyncAtomicCounter(name, database, retryOnException, meteringEnabled, retryExecutor); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public long incrementAndGet() { |
| 50 | return complete(asyncCounter.incrementAndGet()); |
| 51 | } |
| 52 | |
| 53 | @Override |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 54 | public long getAndIncrement() { |
| 55 | return complete(asyncCounter.getAndIncrement()); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public long getAndAdd(long delta) { |
| 60 | return complete(asyncCounter.getAndAdd(delta)); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public long addAndGet(long delta) { |
| 65 | return complete(asyncCounter.getAndAdd(delta)); |
| 66 | } |
| 67 | |
| 68 | @Override |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 69 | public long get() { |
| 70 | return complete(asyncCounter.get()); |
| 71 | } |
| 72 | |
| 73 | private static <T> T complete(CompletableFuture<T> future) { |
| 74 | try { |
| 75 | return future.get(OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
| 76 | } catch (InterruptedException e) { |
| 77 | Thread.currentThread().interrupt(); |
| 78 | throw new StorageException.Interrupted(); |
| 79 | } catch (TimeoutException e) { |
| 80 | throw new StorageException.Timeout(); |
| 81 | } catch (ExecutionException e) { |
| 82 | throw new StorageException(e.getCause()); |
| 83 | } |
| 84 | } |
| 85 | } |