Madan Jampani | 63c659f | 2015-06-11 00:52:58 -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 | |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
| 19 | import com.google.common.util.concurrent.Futures; |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 20 | |
| 21 | import org.onlab.util.SharedExecutors; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 22 | import org.onosproject.store.service.DistributedQueue; |
| 23 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 24 | |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 25 | import java.util.List; |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 26 | import java.util.Set; |
| 27 | import java.util.concurrent.CompletableFuture; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 29 | import static org.onosproject.store.consistent.impl.StateMachineUpdate.Target.QUEUE_PUSH; |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * DistributedQueue implementation that provides FIFO ordering semantics. |
| 33 | * |
| 34 | * @param <E> queue entry type |
| 35 | */ |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 36 | public class DefaultDistributedQueue<E> implements DistributedQueue<E> { |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 37 | |
| 38 | private final String name; |
| 39 | private final Database database; |
| 40 | private final Serializer serializer; |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 41 | private final Set<CompletableFuture<E>> pendingFutures = Sets.newIdentityHashSet(); |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 42 | |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 43 | private static final String PRIMITIVE_NAME = "distributedQueue"; |
| 44 | private static final String SIZE = "size"; |
| 45 | private static final String PUSH = "push"; |
| 46 | private static final String POP = "pop"; |
| 47 | private static final String PEEK = "peek"; |
| 48 | |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 49 | private static final String ERROR_NULL_ENTRY = "Null entries are not allowed"; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 50 | private final MeteringAgent monitor; |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 51 | |
| 52 | public DefaultDistributedQueue(String name, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 53 | Database database, |
| 54 | Serializer serializer, |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 55 | boolean meteringEnabled) { |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 56 | this.name = checkNotNull(name, "queue name cannot be null"); |
| 57 | this.database = checkNotNull(database, "database cannot be null"); |
| 58 | this.serializer = checkNotNull(serializer, "serializer cannot be null"); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 59 | this.monitor = new MeteringAgent(PRIMITIVE_NAME, name, meteringEnabled); |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 60 | this.database.registerConsumer(update -> { |
| 61 | SharedExecutors.getSingleThreadExecutor().execute(() -> { |
| 62 | if (update.target() == QUEUE_PUSH) { |
| 63 | List<Object> input = update.input(); |
| 64 | String queueName = (String) input.get(0); |
| 65 | if (queueName.equals(name)) { |
| 66 | tryPoll(); |
| 67 | } |
| 68 | } |
| 69 | }); |
| 70 | }); |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public long size() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 75 | final MeteringAgent.Context timer = monitor.startTimer(SIZE); |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 76 | return Futures.getUnchecked(database.queueSize(name).whenComplete((r, e) -> timer.stop(e))); |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void push(E entry) { |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 81 | checkNotNull(entry, ERROR_NULL_ENTRY); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 82 | final MeteringAgent.Context timer = monitor.startTimer(PUSH); |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 83 | Futures.getUnchecked(database.queuePush(name, serializer.encode(entry)) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 84 | .whenComplete((r, e) -> timer.stop(e))); |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public CompletableFuture<E> pop() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 89 | final MeteringAgent.Context timer = monitor.startTimer(POP); |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 90 | return database.queuePop(name) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 91 | .whenComplete((r, e) -> timer.stop(e)) |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 92 | .thenCompose(v -> { |
| 93 | if (v != null) { |
HIGUCHI Yuta | 1b9c8ac | 2015-08-28 15:35:43 -0700 | [diff] [blame] | 94 | return CompletableFuture.<E>completedFuture(serializer.decode(v)); |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 95 | } |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 96 | CompletableFuture<E> newPendingFuture = new CompletableFuture<>(); |
| 97 | pendingFutures.add(newPendingFuture); |
| 98 | return newPendingFuture; |
| 99 | }); |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 100 | |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public E peek() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 105 | final MeteringAgent.Context timer = monitor.startTimer(PEEK); |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 106 | return Futures.getUnchecked(database.queuePeek(name) |
| 107 | .thenApply(v -> v != null ? serializer.<E>decode(v) : null) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 108 | .whenComplete((r, e) -> timer.stop(e))); |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | public String name() { |
| 112 | return name; |
| 113 | } |
| 114 | |
| 115 | protected void tryPoll() { |
| 116 | Set<CompletableFuture<E>> completedFutures = Sets.newHashSet(); |
| 117 | for (CompletableFuture<E> future : pendingFutures) { |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 118 | E entry = Futures.getUnchecked(database.queuePop(name) |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 119 | .thenApply(v -> v != null ? serializer.decode(v) : null)); |
| 120 | if (entry != null) { |
| 121 | future.complete(entry); |
| 122 | completedFutures.add(future); |
| 123 | } else { |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | pendingFutures.removeAll(completedFutures); |
| 128 | } |
| 129 | } |