blob: 74b6942eed6b7adff2d01a0843b5927deff3f631 [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 */
Madan Jampanif4c88502016-01-21 12:35:36 -080016package org.onosproject.store.primitives.impl;
Madan Jampanib5d72d52015-04-03 16:53:50 -070017
Flavio Castro41b1f3a2015-07-31 13:51:32 -070018import org.onosproject.store.service.AsyncAtomicCounter;
19import org.onosproject.store.service.AtomicCounter;
20import org.onosproject.store.service.StorageException;
Madan Jampania090a112016-01-18 16:38:17 -080021import org.onosproject.store.service.Synchronous;
Flavio Castro41b1f3a2015-07-31 13:51:32 -070022
Madan Jampanib5d72d52015-04-03 16:53:50 -070023import java.util.concurrent.CompletableFuture;
24import java.util.concurrent.ExecutionException;
25import java.util.concurrent.TimeUnit;
26import java.util.concurrent.TimeoutException;
27
Madan Jampanib5d72d52015-04-03 16:53:50 -070028/**
29 * Default implementation for a distributed AtomicCounter backed by
30 * partitioned Raft DB.
31 * <p>
32 * The initial value will be zero.
33 */
Madan Jampania090a112016-01-18 16:38:17 -080034public class DefaultAtomicCounter extends Synchronous<AsyncAtomicCounter> implements AtomicCounter {
Madan Jampanib5d72d52015-04-03 16:53:50 -070035
36 private static final int OPERATION_TIMEOUT_MILLIS = 5000;
37
38 private final AsyncAtomicCounter asyncCounter;
39
Madan Jampania090a112016-01-18 16:38:17 -080040 public DefaultAtomicCounter(AsyncAtomicCounter asyncCounter) {
41 super(asyncCounter);
42 this.asyncCounter = asyncCounter;
Madan Jampanib5d72d52015-04-03 16:53:50 -070043 }
44
45 @Override
46 public long incrementAndGet() {
47 return complete(asyncCounter.incrementAndGet());
48 }
49
50 @Override
Madan Jampani04aeb452015-05-02 16:12:24 -070051 public long getAndIncrement() {
52 return complete(asyncCounter.getAndIncrement());
53 }
54
55 @Override
56 public long getAndAdd(long delta) {
57 return complete(asyncCounter.getAndAdd(delta));
58 }
59
60 @Override
61 public long addAndGet(long delta) {
62 return complete(asyncCounter.getAndAdd(delta));
63 }
64
65 @Override
andreafd912ac2015-10-02 14:58:35 -070066 public void set(long value) {
67 complete(asyncCounter.set(value));
68 }
69
70 @Override
Aaron Kruglikov82fd6322015-10-06 12:02:46 -070071 public boolean compareAndSet(long expectedValue, long updateValue) {
72 return complete(asyncCounter.compareAndSet(expectedValue, updateValue));
73 }
74
75 @Override
Madan Jampanib5d72d52015-04-03 16:53:50 -070076 public long get() {
77 return complete(asyncCounter.get());
78 }
79
80 private static <T> T complete(CompletableFuture<T> future) {
81 try {
82 return future.get(OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
83 } catch (InterruptedException e) {
84 Thread.currentThread().interrupt();
85 throw new StorageException.Interrupted();
86 } catch (TimeoutException e) {
87 throw new StorageException.Timeout();
88 } catch (ExecutionException e) {
89 throw new StorageException(e.getCause());
90 }
91 }
92}