Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 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.primitives.resources.impl; |
| 17 | |
| 18 | import java.util.concurrent.CompletableFuture; |
| 19 | import java.util.concurrent.atomic.AtomicLong; |
| 20 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 21 | import org.onosproject.store.service.AsyncAtomicCounter; |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 22 | import org.onosproject.store.service.AsyncAtomicIdGenerator; |
| 23 | |
| 24 | /** |
| 25 | * {@code AsyncAtomicIdGenerator} implementation backed by Atomix |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 26 | * {@link AsyncAtomicCounter}. |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 27 | */ |
| 28 | public class AtomixIdGenerator implements AsyncAtomicIdGenerator { |
| 29 | |
| 30 | private static final long DEFAULT_BATCH_SIZE = 1000; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 31 | private final AsyncAtomicCounter counter; |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 32 | private final long batchSize; |
| 33 | private CompletableFuture<Long> reserveFuture; |
| 34 | private long base; |
| 35 | private final AtomicLong delta = new AtomicLong(); |
| 36 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 37 | public AtomixIdGenerator(AsyncAtomicCounter counter) { |
| 38 | this(counter, DEFAULT_BATCH_SIZE); |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 41 | AtomixIdGenerator(AsyncAtomicCounter counter, long batchSize) { |
| 42 | this.counter = counter; |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 43 | this.batchSize = batchSize; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public String name() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 48 | return counter.name(); |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public synchronized CompletableFuture<Long> nextId() { |
| 53 | long nextDelta = delta.incrementAndGet(); |
| 54 | if ((base == 0 && reserveFuture == null) || nextDelta > batchSize) { |
| 55 | delta.set(0); |
| 56 | long delta = this.delta.incrementAndGet(); |
| 57 | return reserve().thenApply(base -> base + delta); |
| 58 | } else { |
| 59 | return reserveFuture.thenApply(base -> base + nextDelta); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private CompletableFuture<Long> reserve() { |
| 64 | if (reserveFuture == null || reserveFuture.isDone()) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 65 | reserveFuture = counter.getAndAdd(batchSize); |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 66 | } else { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 67 | reserveFuture = reserveFuture.thenCompose(v -> counter.getAndAdd(batchSize)); |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 68 | } |
| 69 | reserveFuture = reserveFuture.thenApply(base -> { |
| 70 | this.base = base; |
| 71 | return base; |
| 72 | }); |
| 73 | return reserveFuture; |
| 74 | } |
| 75 | } |