Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -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 | |
| 17 | package org.onosproject.store.primitives.resources.impl; |
| 18 | |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 19 | import java.util.Collection; |
| 20 | import java.util.ConcurrentModificationException; |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 21 | import java.util.List; |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 22 | import java.util.Map; |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 23 | import java.util.Set; |
| 24 | import java.util.concurrent.CompletableFuture; |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 25 | import java.util.concurrent.ConcurrentHashMap; |
| 26 | import java.util.concurrent.Executor; |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 27 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 28 | import com.google.common.collect.Lists; |
| 29 | import com.google.common.collect.Multiset; |
| 30 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 31 | import org.onlab.util.KryoNamespace; |
| 32 | import org.onosproject.store.serializers.KryoNamespaces; |
| 33 | import org.onosproject.store.service.AsyncConsistentMultimap; |
| 34 | import org.onosproject.store.service.MultimapEvent; |
| 35 | import org.onosproject.store.service.MultimapEventListener; |
| 36 | import org.onosproject.store.service.Serializer; |
| 37 | import org.onosproject.store.service.Versioned; |
| 38 | |
| 39 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapEvents.CHANGE; |
| 40 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.ADD_LISTENER; |
| 41 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.CLEAR; |
| 42 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.CONTAINS_ENTRY; |
| 43 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.CONTAINS_KEY; |
| 44 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.CONTAINS_VALUE; |
| 45 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.ContainsEntry; |
| 46 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.ContainsKey; |
| 47 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.ContainsValue; |
| 48 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.ENTRIES; |
| 49 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.GET; |
| 50 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.Get; |
| 51 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.IS_EMPTY; |
| 52 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.KEYS; |
| 53 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.KEY_SET; |
| 54 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.MultiRemove; |
| 55 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.PUT; |
| 56 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.Put; |
| 57 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.REMOVE; |
| 58 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.REMOVE_ALL; |
| 59 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.REMOVE_LISTENER; |
| 60 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.REPLACE; |
| 61 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.RemoveAll; |
| 62 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.Replace; |
| 63 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.SIZE; |
| 64 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.VALUES; |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 65 | |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 66 | |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Set based implementation of the {@link AsyncConsistentMultimap}. |
| 69 | * <p> |
| 70 | * Note: this implementation does not allow null entries or duplicate entries. |
| 71 | */ |
Aaron Kruglikov | a1801aa | 2016-07-12 15:17:30 -0700 | [diff] [blame] | 72 | public class AtomixConsistentSetMultimap |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 73 | extends AbstractRaftPrimitive |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 74 | implements AsyncConsistentMultimap<String, byte[]> { |
| 75 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 76 | private static final Serializer SERIALIZER = Serializer.using(KryoNamespace.newBuilder() |
| 77 | .register(KryoNamespaces.BASIC) |
| 78 | .register(AtomixConsistentSetMultimapOperations.NAMESPACE) |
| 79 | .register(AtomixConsistentSetMultimapEvents.NAMESPACE) |
| 80 | .build()); |
| 81 | |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 82 | private final Map<MultimapEventListener<String, byte[]>, Executor> mapEventListeners = new ConcurrentHashMap<>(); |
| 83 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 84 | public AtomixConsistentSetMultimap(RaftProxy proxy) { |
| 85 | super(proxy); |
| 86 | proxy.addEventListener(CHANGE, SERIALIZER::decode, this::handleEvent); |
| 87 | proxy.addStateChangeListener(state -> { |
| 88 | if (state == RaftProxy.State.CONNECTED && isListening()) { |
| 89 | proxy.invoke(ADD_LISTENER); |
| 90 | } |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 91 | }); |
| 92 | } |
| 93 | |
| 94 | private void handleEvent(List<MultimapEvent<String, byte[]>> events) { |
| 95 | events.forEach(event -> |
| 96 | mapEventListeners.forEach((listener, executor) -> executor.execute(() -> listener.event(event)))); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public CompletableFuture<Integer> size() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 101 | return proxy.invoke(SIZE, SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public CompletableFuture<Boolean> isEmpty() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 106 | return proxy.invoke(IS_EMPTY, SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public CompletableFuture<Boolean> containsKey(String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 111 | return proxy.invoke(CONTAINS_KEY, SERIALIZER::encode, new ContainsKey(key), SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public CompletableFuture<Boolean> containsValue(byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 116 | return proxy.invoke(CONTAINS_VALUE, SERIALIZER::encode, new ContainsValue(value), SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public CompletableFuture<Boolean> containsEntry(String key, byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 121 | return proxy.invoke(CONTAINS_ENTRY, SERIALIZER::encode, new ContainsEntry(key, value), SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public CompletableFuture<Boolean> put(String key, byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 126 | return proxy.invoke( |
| 127 | PUT, |
| 128 | SERIALIZER::encode, |
| 129 | new Put(key, Lists.newArrayList(value), null), |
| 130 | SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public CompletableFuture<Boolean> remove(String key, byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 135 | return proxy.invoke(REMOVE, SERIALIZER::encode, new MultiRemove(key, |
| 136 | Lists.newArrayList(value), |
| 137 | null), SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 141 | public CompletableFuture<Boolean> removeAll(String key, Collection<? extends byte[]> values) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 142 | return proxy.invoke( |
| 143 | REMOVE, |
| 144 | SERIALIZER::encode, |
| 145 | new MultiRemove(key, (Collection<byte[]>) values, null), |
| 146 | SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 150 | public CompletableFuture<Versioned<Collection<? extends byte[]>>> removeAll(String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 151 | return proxy.invoke(REMOVE_ALL, SERIALIZER::encode, new RemoveAll(key, null), SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 155 | public CompletableFuture<Boolean> putAll( |
| 156 | String key, Collection<? extends byte[]> values) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 157 | return proxy.invoke(PUT, SERIALIZER::encode, new Put(key, values, null), SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 161 | public CompletableFuture<Versioned<Collection<? extends byte[]>>> replaceValues( |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 162 | String key, Collection<byte[]> values) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 163 | return proxy.invoke( |
| 164 | REPLACE, |
| 165 | SERIALIZER::encode, |
| 166 | new Replace(key, values, null), |
| 167 | SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public CompletableFuture<Void> clear() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 172 | return proxy.invoke(CLEAR); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
Madan Jampani | 630e7ac | 2016-05-31 11:34:05 -0700 | [diff] [blame] | 176 | public CompletableFuture<Versioned<Collection<? extends byte[]>>> get(String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 177 | return proxy.invoke(GET, SERIALIZER::encode, new Get(key), SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public CompletableFuture<Set<String>> keySet() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 182 | return proxy.invoke(KEY_SET, SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public CompletableFuture<Multiset<String>> keys() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 187 | return proxy.invoke(KEYS, SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Jonathan Hart | ad0c302 | 2017-02-22 14:06:01 -0800 | [diff] [blame] | 190 | @Override |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 191 | public CompletableFuture<Multiset<byte[]>> values() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 192 | return proxy.invoke(VALUES, SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public CompletableFuture<Collection<Map.Entry<String, byte[]>>> entries() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 197 | return proxy.invoke(ENTRIES, SERIALIZER::decode); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 201 | public CompletableFuture<Void> addListener(MultimapEventListener<String, byte[]> listener, Executor executor) { |
| 202 | if (mapEventListeners.isEmpty()) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 203 | return proxy.invoke(ADD_LISTENER).thenRun(() -> mapEventListeners.put(listener, executor)); |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 204 | } else { |
| 205 | mapEventListeners.put(listener, executor); |
| 206 | return CompletableFuture.completedFuture(null); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public CompletableFuture<Void> removeListener(MultimapEventListener<String, byte[]> listener) { |
| 212 | if (mapEventListeners.remove(listener) != null && mapEventListeners.isEmpty()) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 213 | return proxy.invoke(REMOVE_LISTENER).thenApply(v -> null); |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 214 | } |
| 215 | return CompletableFuture.completedFuture(null); |
| 216 | } |
| 217 | |
| 218 | @Override |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 219 | public CompletableFuture<Map<String, Collection<byte[]>>> asMap() { |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 220 | throw new UnsupportedOperationException("Expensive operation."); |
| 221 | } |
| 222 | |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 223 | /** |
| 224 | * Helper to check if there was a lock based issue. |
| 225 | * @param status the status of an update result |
| 226 | */ |
| 227 | private void throwIfLocked(MapEntryUpdateResult.Status status) { |
| 228 | if (status == MapEntryUpdateResult.Status.WRITE_LOCK) { |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 229 | throw new ConcurrentModificationException("Cannot update map: " + |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 230 | "Another transaction " + |
| 231 | "in progress"); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 234 | |
| 235 | private boolean isListening() { |
| 236 | return !mapEventListeners.isEmpty(); |
| 237 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 238 | } |