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