blob: 18120bb8efe73cab4d2043d08b2a491667982942 [file] [log] [blame]
Madan Jampanib5d72d52015-04-03 16:53:50 -07001/*
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 */
16package org.onosproject.store.consistent.impl;
17
18import java.util.concurrent.CompletableFuture;
19import java.util.concurrent.ExecutionException;
Madan Jampanif4d58f32015-06-05 17:38:22 -070020import java.util.concurrent.ScheduledExecutorService;
Madan Jampanib5d72d52015-04-03 16:53:50 -070021import java.util.concurrent.TimeUnit;
22import java.util.concurrent.TimeoutException;
23
24import org.onosproject.store.service.AsyncAtomicCounter;
25import org.onosproject.store.service.AtomicCounter;
26import org.onosproject.store.service.StorageException;
27
28/**
29 * Default implementation for a distributed AtomicCounter backed by
30 * partitioned Raft DB.
31 * <p>
32 * The initial value will be zero.
33 */
34public class DefaultAtomicCounter implements AtomicCounter {
35
36 private static final int OPERATION_TIMEOUT_MILLIS = 5000;
37
38 private final AsyncAtomicCounter asyncCounter;
39
Madan Jampanif4d58f32015-06-05 17:38:22 -070040 public DefaultAtomicCounter(String name,
41 Database database,
42 boolean retryOnException,
43 ScheduledExecutorService retryExecutor) {
44 asyncCounter = new DefaultAsyncAtomicCounter(name, database, retryOnException, retryExecutor);
Madan Jampanib5d72d52015-04-03 16:53:50 -070045 }
46
47 @Override
48 public long incrementAndGet() {
49 return complete(asyncCounter.incrementAndGet());
50 }
51
52 @Override
Madan Jampani04aeb452015-05-02 16:12:24 -070053 public long getAndIncrement() {
54 return complete(asyncCounter.getAndIncrement());
55 }
56
57 @Override
58 public long getAndAdd(long delta) {
59 return complete(asyncCounter.getAndAdd(delta));
60 }
61
62 @Override
63 public long addAndGet(long delta) {
64 return complete(asyncCounter.getAndAdd(delta));
65 }
66
67 @Override
Madan Jampanib5d72d52015-04-03 16:53:50 -070068 public long get() {
69 return complete(asyncCounter.get());
70 }
71
72 private static <T> T complete(CompletableFuture<T> future) {
73 try {
74 return future.get(OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
75 } catch (InterruptedException e) {
76 Thread.currentThread().interrupt();
77 throw new StorageException.Interrupted();
78 } catch (TimeoutException e) {
79 throw new StorageException.Timeout();
80 } catch (ExecutionException e) {
81 throw new StorageException(e.getCause());
82 }
83 }
84}