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 | |
| 18 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 19 | import java.util.concurrent.ScheduledExecutorService; |
| 20 | import java.util.concurrent.TimeUnit; |
| 21 | import java.util.function.BiFunction; |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 22 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 23 | import org.onosproject.store.service.AsyncAtomicCounter; |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 24 | |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | import org.slf4j.Logger; |
| 30 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 31 | import static com.google.common.base.Preconditions.*; |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 32 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Default implementation for a distributed AsyncAtomicCounter backed by |
| 36 | * partitioned Raft DB. |
| 37 | * <p> |
| 38 | * The initial value will be zero. |
| 39 | */ |
| 40 | public class DefaultAsyncAtomicCounter implements AsyncAtomicCounter { |
| 41 | |
| 42 | private final String name; |
| 43 | private final Database database; |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 44 | 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 Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 49 | |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 50 | public DefaultAsyncAtomicCounter(String name, |
| 51 | Database database, |
| 52 | boolean retryOnException, |
| 53 | ScheduledExecutorService retryExecutor) { |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 54 | this.name = checkNotNull(name); |
| 55 | this.database = checkNotNull(database); |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 56 | this.retryOnFailure = retryOnException; |
| 57 | this.retryExecutor = retryExecutor; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public CompletableFuture<Long> incrementAndGet() { |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 62 | return addAndGet(1L); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public CompletableFuture<Long> get() { |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 67 | 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 Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 77 | 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 Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public CompletableFuture<Long> addAndGet(long delta) { |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 97 | 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 Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 113 | } |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 114 | |
| 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 | } |