Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -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 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 19 | import java.util.HashMap; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 20 | import java.util.Map; |
| 21 | import java.util.NavigableMap; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 22 | import java.util.TreeMap; |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame^] | 23 | import java.util.concurrent.ConcurrentSkipListMap; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 24 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 25 | import com.google.common.collect.Maps; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 26 | import io.atomix.protocols.raft.service.Commit; |
| 27 | import io.atomix.protocols.raft.service.RaftServiceExecutor; |
| 28 | import io.atomix.protocols.raft.session.RaftSession; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 29 | import org.onlab.util.KryoNamespace; |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 30 | import org.onosproject.store.primitives.TransactionId; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 31 | import org.onosproject.store.serializers.KryoNamespaces; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 32 | import org.onosproject.store.service.Serializer; |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 33 | import org.onosproject.store.service.TransactionLog; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 34 | import org.onosproject.store.service.Versioned; |
| 35 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 36 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.CEILING_ENTRY; |
| 37 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.CEILING_KEY; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 38 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.CeilingEntry; |
| 39 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.CeilingKey; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 40 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FIRST_ENTRY; |
| 41 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FIRST_KEY; |
| 42 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FLOOR_ENTRY; |
| 43 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FLOOR_KEY; |
| 44 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FloorEntry; |
| 45 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FloorKey; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 46 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.HIGHER_ENTRY; |
| 47 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.HIGHER_KEY; |
| 48 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.HigherEntry; |
| 49 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.HigherKey; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 50 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LAST_ENTRY; |
| 51 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LAST_KEY; |
| 52 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LOWER_ENTRY; |
| 53 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LOWER_KEY; |
| 54 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LowerEntry; |
| 55 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LowerKey; |
| 56 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.POLL_FIRST_ENTRY; |
| 57 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.POLL_LAST_ENTRY; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 58 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.SUB_MAP; |
| 59 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.SubMap; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * State machine corresponding to {@link AtomixConsistentTreeMap} backed by a |
| 63 | * {@link TreeMap}. |
| 64 | */ |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 65 | public class AtomixConsistentTreeMapService extends AtomixConsistentMapService { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 66 | |
| 67 | private static final Serializer SERIALIZER = Serializer.using(KryoNamespace.newBuilder() |
| 68 | .register(KryoNamespaces.BASIC) |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 69 | .register(AtomixConsistentMapOperations.NAMESPACE) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 70 | .register(AtomixConsistentTreeMapOperations.NAMESPACE) |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 71 | .register(AtomixConsistentMapEvents.NAMESPACE) |
| 72 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID + 150) |
| 73 | .register(TransactionScope.class) |
| 74 | .register(TransactionLog.class) |
| 75 | .register(TransactionId.class) |
| 76 | .register(MapEntryValue.class) |
| 77 | .register(MapEntryValue.Type.class) |
| 78 | .register(new HashMap().keySet().getClass()) |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame^] | 79 | .register(ConcurrentSkipListMap.class) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 80 | .build()); |
| 81 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 82 | @Override |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame^] | 83 | protected NavigableMap<String, MapEntryValue> createMap() { |
| 84 | return new ConcurrentSkipListMap<>(); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame^] | 88 | protected NavigableMap<String, MapEntryValue> entries() { |
| 89 | return (NavigableMap<String, MapEntryValue>) super.entries(); |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 90 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 91 | |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 92 | @Override |
| 93 | protected Serializer serializer() { |
| 94 | return SERIALIZER; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void configure(RaftServiceExecutor executor) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 99 | super.configure(executor); |
| 100 | executor.register(SUB_MAP, serializer()::decode, this::subMap, serializer()::encode); |
| 101 | executor.register(FIRST_KEY, (Commit<Void> c) -> firstKey(), serializer()::encode); |
| 102 | executor.register(LAST_KEY, (Commit<Void> c) -> lastKey(), serializer()::encode); |
| 103 | executor.register(FIRST_ENTRY, (Commit<Void> c) -> firstEntry(), serializer()::encode); |
| 104 | executor.register(LAST_ENTRY, (Commit<Void> c) -> lastEntry(), serializer()::encode); |
| 105 | executor.register(POLL_FIRST_ENTRY, (Commit<Void> c) -> pollFirstEntry(), serializer()::encode); |
| 106 | executor.register(POLL_LAST_ENTRY, (Commit<Void> c) -> pollLastEntry(), serializer()::encode); |
| 107 | executor.register(LOWER_ENTRY, serializer()::decode, this::lowerEntry, serializer()::encode); |
| 108 | executor.register(LOWER_KEY, serializer()::decode, this::lowerKey, serializer()::encode); |
| 109 | executor.register(FLOOR_ENTRY, serializer()::decode, this::floorEntry, serializer()::encode); |
| 110 | executor.register(FLOOR_KEY, serializer()::decode, this::floorKey, serializer()::encode); |
| 111 | executor.register(CEILING_ENTRY, serializer()::decode, this::ceilingEntry, serializer()::encode); |
| 112 | executor.register(CEILING_KEY, serializer()::decode, this::ceilingKey, serializer()::encode); |
| 113 | executor.register(HIGHER_ENTRY, serializer()::decode, this::higherEntry, serializer()::encode); |
| 114 | executor.register(HIGHER_KEY, serializer()::decode, this::higherKey, serializer()::encode); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 117 | protected NavigableMap<String, MapEntryValue> subMap( |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 118 | Commit<? extends SubMap> commit) { |
| 119 | // Do not support this until lazy communication is possible. At present |
| 120 | // it transmits up to the entire map. |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 121 | SubMap<String, MapEntryValue> subMap = commit.value(); |
| 122 | return entries().subMap(subMap.fromKey(), subMap.isInclusiveFrom(), |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 123 | subMap.toKey(), subMap.isInclusiveTo()); |
| 124 | } |
| 125 | |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 126 | protected String firstKey() { |
| 127 | return isEmpty() ? null : entries().firstKey(); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 130 | protected String lastKey() { |
| 131 | return isEmpty() ? null : entries().lastKey(); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | protected Map.Entry<String, Versioned<byte[]>> higherEntry(Commit<? extends HigherEntry> commit) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 135 | return isEmpty() ? null : toVersionedEntry(entries().higherEntry(commit.value().key())); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 138 | protected Map.Entry<String, Versioned<byte[]>> firstEntry() { |
| 139 | return isEmpty() ? null : toVersionedEntry(entries().firstEntry()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 142 | protected Map.Entry<String, Versioned<byte[]>> lastEntry() { |
| 143 | return isEmpty() ? null : toVersionedEntry(entries().lastEntry()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 146 | protected Map.Entry<String, Versioned<byte[]>> pollFirstEntry() { |
| 147 | return toVersionedEntry(entries().pollFirstEntry()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 150 | protected Map.Entry<String, Versioned<byte[]>> pollLastEntry() { |
| 151 | return toVersionedEntry(entries().pollLastEntry()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | protected Map.Entry<String, Versioned<byte[]>> lowerEntry(Commit<? extends LowerEntry> commit) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 155 | return toVersionedEntry(entries().lowerEntry(commit.value().key())); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | protected String lowerKey(Commit<? extends LowerKey> commit) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 159 | return entries().lowerKey(commit.value().key()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | protected Map.Entry<String, Versioned<byte[]>> floorEntry(Commit<? extends FloorEntry> commit) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 163 | return toVersionedEntry(entries().floorEntry(commit.value().key())); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | protected String floorKey(Commit<? extends FloorKey> commit) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 167 | return entries().floorKey(commit.value().key()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | protected Map.Entry<String, Versioned<byte[]>> ceilingEntry(Commit<CeilingEntry> commit) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 171 | return toVersionedEntry(entries().ceilingEntry(commit.value().key())); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | protected String ceilingKey(Commit<CeilingKey> commit) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 175 | return entries().ceilingKey(commit.value().key()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | protected String higherKey(Commit<HigherKey> commit) { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 179 | return entries().higherKey(commit.value().key()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | private Map.Entry<String, Versioned<byte[]>> toVersionedEntry( |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 183 | Map.Entry<String, MapEntryValue> entry) { |
| 184 | return entry == null || valueIsNull(entry.getValue()) |
| 185 | ? null : Maps.immutableEntry(entry.getKey(), toVersioned(entry.getValue())); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void onExpire(RaftSession session) { |
| 190 | closeListener(session.sessionId().id()); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void onClose(RaftSession session) { |
| 195 | closeListener(session.sessionId().id()); |
| 196 | } |
| 197 | |
| 198 | private void closeListener(Long sessionId) { |
| 199 | listeners.remove(sessionId); |
| 200 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 201 | } |