blob: 7d487f4b5dec1ef9dffa75fd6e57d40e94fcf809 [file] [log] [blame]
Jordan Halterman5a1053e2017-05-19 18:03:47 -07001/*
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 */
16package org.onosproject.store.primitives.resources.impl;
17
18import java.util.concurrent.CompletableFuture;
19import java.util.concurrent.atomic.AtomicLong;
20
Jordan Halterman2bf177c2017-06-29 01:49:08 -070021import org.onosproject.store.service.AsyncAtomicCounter;
Jordan Halterman5a1053e2017-05-19 18:03:47 -070022import org.onosproject.store.service.AsyncAtomicIdGenerator;
23
24/**
25 * {@code AsyncAtomicIdGenerator} implementation backed by Atomix
Jordan Halterman2bf177c2017-06-29 01:49:08 -070026 * {@link AsyncAtomicCounter}.
Jordan Halterman5a1053e2017-05-19 18:03:47 -070027 */
28public class AtomixIdGenerator implements AsyncAtomicIdGenerator {
29
30 private static final long DEFAULT_BATCH_SIZE = 1000;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070031 private final AsyncAtomicCounter counter;
Jordan Halterman5a1053e2017-05-19 18:03:47 -070032 private final long batchSize;
33 private CompletableFuture<Long> reserveFuture;
34 private long base;
35 private final AtomicLong delta = new AtomicLong();
36
Jordan Halterman2bf177c2017-06-29 01:49:08 -070037 public AtomixIdGenerator(AsyncAtomicCounter counter) {
38 this(counter, DEFAULT_BATCH_SIZE);
Jordan Halterman5a1053e2017-05-19 18:03:47 -070039 }
40
Jordan Halterman2bf177c2017-06-29 01:49:08 -070041 AtomixIdGenerator(AsyncAtomicCounter counter, long batchSize) {
42 this.counter = counter;
Jordan Halterman5a1053e2017-05-19 18:03:47 -070043 this.batchSize = batchSize;
44 }
45
46 @Override
47 public String name() {
Jordan Halterman2bf177c2017-06-29 01:49:08 -070048 return counter.name();
Jordan Halterman5a1053e2017-05-19 18:03:47 -070049 }
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 Halterman2bf177c2017-06-29 01:49:08 -070065 reserveFuture = counter.getAndAdd(batchSize);
Jordan Halterman5a1053e2017-05-19 18:03:47 -070066 } else {
Jordan Halterman2bf177c2017-06-29 01:49:08 -070067 reserveFuture = reserveFuture.thenCompose(v -> counter.getAndAdd(batchSize));
Jordan Halterman5a1053e2017-05-19 18:03:47 -070068 }
69 reserveFuture = reserveFuture.thenApply(base -> {
70 this.base = base;
71 return base;
72 });
73 return reserveFuture;
74 }
75}