blob: cdaf79233f5280f60e5cc4cab762b5f8040fe489 [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;
Madan Jampanif4d58f32015-06-05 17:38:22 -070019import java.util.concurrent.ScheduledExecutorService;
20import java.util.concurrent.TimeUnit;
21import java.util.function.BiFunction;
Madan Jampani04aeb452015-05-02 16:12:24 -070022
Madan Jampanib5d72d52015-04-03 16:53:50 -070023import org.onosproject.store.service.AsyncAtomicCounter;
Madan Jampani04aeb452015-05-02 16:12:24 -070024
Madan Jampanif4d58f32015-06-05 17:38:22 -070025
26
27
28
29import org.slf4j.Logger;
30
Madan Jampanib5d72d52015-04-03 16:53:50 -070031import static com.google.common.base.Preconditions.*;
Madan Jampanif4d58f32015-06-05 17:38:22 -070032import static org.slf4j.LoggerFactory.getLogger;
Madan Jampanib5d72d52015-04-03 16:53:50 -070033
34/**
35 * Default implementation for a distributed AsyncAtomicCounter backed by
36 * partitioned Raft DB.
37 * <p>
38 * The initial value will be zero.
39 */
40public class DefaultAsyncAtomicCounter implements AsyncAtomicCounter {
41
42 private final String name;
43 private final Database database;
Madan Jampanif4d58f32015-06-05 17:38:22 -070044 private final boolean retryOnFailure;
45 private final ScheduledExecutorService retryExecutor;
46 // TODO: configure delay via builder
47 private static final int DELAY_BETWEEN_RETRY_SEC = 1;
48 private final Logger log = getLogger(getClass());
Madan Jampanib5d72d52015-04-03 16:53:50 -070049
Madan Jampanif4d58f32015-06-05 17:38:22 -070050 public DefaultAsyncAtomicCounter(String name,
51 Database database,
52 boolean retryOnException,
53 ScheduledExecutorService retryExecutor) {
Madan Jampanib5d72d52015-04-03 16:53:50 -070054 this.name = checkNotNull(name);
55 this.database = checkNotNull(database);
Madan Jampanif4d58f32015-06-05 17:38:22 -070056 this.retryOnFailure = retryOnException;
57 this.retryExecutor = retryExecutor;
Madan Jampanib5d72d52015-04-03 16:53:50 -070058 }
59
60 @Override
61 public CompletableFuture<Long> incrementAndGet() {
Madan Jampani04aeb452015-05-02 16:12:24 -070062 return addAndGet(1L);
Madan Jampanib5d72d52015-04-03 16:53:50 -070063 }
64
65 @Override
66 public CompletableFuture<Long> get() {
Madan Jampani04aeb452015-05-02 16:12:24 -070067 return database.counterGet(name);
68 }
69
70 @Override
71 public CompletableFuture<Long> getAndIncrement() {
72 return getAndAdd(1L);
73 }
74
75 @Override
76 public CompletableFuture<Long> getAndAdd(long delta) {
Madan Jampanif4d58f32015-06-05 17:38:22 -070077 CompletableFuture<Long> result = database.counterGetAndAdd(name, delta);
78 if (!retryOnFailure) {
79 return result;
80 }
81
82 CompletableFuture<Long> future = new CompletableFuture<>();
83 return result.whenComplete((r, e) -> {
84 if (e != null) {
85 log.warn("getAndAdd failed due to {}. Will retry", e.getMessage());
86 retryExecutor.schedule(new RetryTask(database::counterGetAndAdd, delta, future),
87 DELAY_BETWEEN_RETRY_SEC,
88 TimeUnit.SECONDS);
89 } else {
90 future.complete(r);
91 }
92 }).thenCompose(v -> future);
Madan Jampani04aeb452015-05-02 16:12:24 -070093 }
94
95 @Override
96 public CompletableFuture<Long> addAndGet(long delta) {
Madan Jampanif4d58f32015-06-05 17:38:22 -070097 CompletableFuture<Long> result = database.counterAddAndGet(name, delta);
98 if (!retryOnFailure) {
99 return result;
100 }
101
102 CompletableFuture<Long> future = new CompletableFuture<>();
103 return result.whenComplete((r, e) -> {
104 if (e != null) {
105 log.warn("addAndGet failed due to {}. Will retry", e.getMessage());
106 retryExecutor.schedule(new RetryTask(database::counterAddAndGet, delta, future),
107 DELAY_BETWEEN_RETRY_SEC,
108 TimeUnit.SECONDS);
109 } else {
110 future.complete(r);
111 }
112 }).thenCompose(v -> future);
Madan Jampanib5d72d52015-04-03 16:53:50 -0700113 }
Madan Jampanif4d58f32015-06-05 17:38:22 -0700114
115 private class RetryTask implements Runnable {
116
117 private final BiFunction<String, Long, CompletableFuture<Long>> function;
118 private final Long delta;
119 private final CompletableFuture<Long> result;
120
121 public RetryTask(BiFunction<String, Long, CompletableFuture<Long>> function,
122 Long delta,
123 CompletableFuture<Long> result) {
124 this.function = function;
125 this.delta = delta;
126 this.result = result;
127 }
128
129 @Override
130 public void run() {
131 function.apply(name, delta).whenComplete((r, e) -> {
132 if (e == null) {
133 result.complete(r);
134 } else {
135 log.warn("{} retry failed due to {}. Will try again...", function, e.getMessage());
136 // TODO: Exponential backoff
137 // TODO: limit retries
138 retryExecutor.schedule(this, DELAY_BETWEEN_RETRY_SEC, TimeUnit.SECONDS);
139 }
140 });
141 }
142 }
143}