Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [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 | package org.onosproject.store.primitives.resources.impl; |
| 17 | |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 19 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 20 | import io.atomix.protocols.raft.service.RaftService; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 21 | import org.junit.Test; |
Jordan Halterman | ca7660a | 2018-04-04 23:43:23 -0700 | [diff] [blame] | 22 | import org.onlab.util.HexString; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 23 | import org.onlab.util.Tools; |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 24 | import org.onosproject.store.primitives.MapUpdate; |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 25 | import org.onosproject.store.primitives.TransactionId; |
Jordan Halterman | ca7660a | 2018-04-04 23:43:23 -0700 | [diff] [blame] | 26 | import org.onosproject.store.primitives.impl.CompatibleValue; |
| 27 | import org.onosproject.store.primitives.impl.DistributedPrimitives; |
| 28 | import org.onosproject.store.serializers.KryoNamespaces; |
| 29 | import org.onosproject.store.service.AsyncConsistentMap; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 30 | import org.onosproject.store.service.MapEvent; |
| 31 | import org.onosproject.store.service.MapEventListener; |
Jordan Halterman | ca7660a | 2018-04-04 23:43:23 -0700 | [diff] [blame] | 32 | import org.onosproject.store.service.Serializer; |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 33 | import org.onosproject.store.service.TransactionLog; |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 34 | import org.onosproject.store.service.Version; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 35 | import org.onosproject.store.service.Versioned; |
| 36 | |
Ray Milkey | 6a51cb9 | 2018-03-06 09:03:03 -0800 | [diff] [blame] | 37 | import java.util.Arrays; |
| 38 | import java.util.ConcurrentModificationException; |
| 39 | import java.util.List; |
| 40 | import java.util.concurrent.ArrayBlockingQueue; |
| 41 | import java.util.concurrent.BlockingQueue; |
| 42 | import java.util.concurrent.CompletionException; |
| 43 | import java.util.stream.Collectors; |
| 44 | |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 45 | import static org.hamcrest.Matchers.is; |
| 46 | import static org.junit.Assert.assertArrayEquals; |
| 47 | import static org.junit.Assert.assertEquals; |
| 48 | import static org.junit.Assert.assertFalse; |
| 49 | import static org.junit.Assert.assertNotNull; |
| 50 | import static org.junit.Assert.assertNull; |
| 51 | import static org.junit.Assert.assertThat; |
| 52 | import static org.junit.Assert.assertTrue; |
| 53 | import static org.junit.Assert.fail; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Unit tests for {@link AtomixConsistentMap}. |
| 57 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 58 | public class AtomixConsistentMapTest extends AtomixTestBase<AtomixConsistentMap> { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 59 | |
| 60 | @Override |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 61 | protected RaftService createService() { |
| 62 | return new AtomixConsistentMapService(); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | protected AtomixConsistentMap createPrimitive(RaftProxy proxy) { |
| 67 | return new AtomixConsistentMap(proxy); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Tests various basic map operations. |
| 72 | */ |
| 73 | @Test |
| 74 | public void testBasicMapOperations() throws Throwable { |
Aaron Kruglikov | c38dc7f | 2016-07-19 10:00:11 -0700 | [diff] [blame] | 75 | basicMapOperationTests(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Tests various map compute* operations on different cluster sizes. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testMapComputeOperations() throws Throwable { |
Aaron Kruglikov | c38dc7f | 2016-07-19 10:00:11 -0700 | [diff] [blame] | 83 | mapComputeOperationTests(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
Jordan Halterman | 4922a06 | 2017-07-31 15:55:36 -0700 | [diff] [blame] | 87 | * Tests null values. |
| 88 | */ |
| 89 | @Test |
| 90 | public void testNullValues() throws Throwable { |
| 91 | final byte[] rawFooValue = Tools.getBytesUtf8("Hello foo!"); |
| 92 | final byte[] rawBarValue = Tools.getBytesUtf8("Hello bar!"); |
| 93 | |
| 94 | AtomixConsistentMap map = newPrimitive("testNullValues"); |
| 95 | |
| 96 | map.get("foo") |
| 97 | .thenAccept(v -> assertNull(v)).join(); |
| 98 | map.put("foo", null) |
| 99 | .thenAccept(v -> assertNull(v)).join(); |
| 100 | map.put("foo", rawFooValue).thenAccept(v -> { |
| 101 | assertNotNull(v); |
| 102 | assertNull(v.value()); |
| 103 | }).join(); |
| 104 | map.get("foo").thenAccept(v -> { |
| 105 | assertNotNull(v); |
| 106 | assertTrue(Arrays.equals(v.value(), rawFooValue)); |
| 107 | }).join(); |
| 108 | map.replace("foo", rawFooValue, null) |
| 109 | .thenAccept(replaced -> assertTrue(replaced)).join(); |
| 110 | map.get("foo").thenAccept(v -> { |
| 111 | assertNotNull(v); |
| 112 | assertNull(v.value()); |
| 113 | }).join(); |
| 114 | map.replace("foo", rawFooValue, rawBarValue) |
| 115 | .thenAccept(replaced -> assertFalse(replaced)).join(); |
| 116 | map.replace("foo", null, rawBarValue) |
| 117 | .thenAccept(replaced -> assertTrue(replaced)).join(); |
| 118 | map.get("foo").thenAccept(v -> { |
| 119 | assertNotNull(v); |
| 120 | assertTrue(Arrays.equals(v.value(), rawBarValue)); |
| 121 | }).join(); |
| 122 | } |
| 123 | |
| 124 | /** |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 125 | * Tests map event notifications. |
| 126 | */ |
| 127 | @Test |
| 128 | public void testMapListeners() throws Throwable { |
Aaron Kruglikov | c38dc7f | 2016-07-19 10:00:11 -0700 | [diff] [blame] | 129 | mapListenerTests(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 133 | * Tests map transaction prepare. |
| 134 | */ |
| 135 | @Test |
| 136 | public void testTransactionPrepare() throws Throwable { |
| 137 | transactionPrepareTests(); |
| 138 | } |
| 139 | |
| 140 | /** |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 141 | * Tests map transaction commit. |
| 142 | */ |
| 143 | @Test |
| 144 | public void testTransactionCommit() throws Throwable { |
Aaron Kruglikov | c38dc7f | 2016-07-19 10:00:11 -0700 | [diff] [blame] | 145 | transactionCommitTests(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Tests map transaction rollback. |
| 150 | */ |
| 151 | @Test |
| 152 | public void testTransactionRollback() throws Throwable { |
Aaron Kruglikov | c38dc7f | 2016-07-19 10:00:11 -0700 | [diff] [blame] | 153 | transactionRollbackTests(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Aaron Kruglikov | c38dc7f | 2016-07-19 10:00:11 -0700 | [diff] [blame] | 156 | protected void basicMapOperationTests() throws Throwable { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 157 | final byte[] rawFooValue = Tools.getBytesUtf8("Hello foo!"); |
| 158 | final byte[] rawBarValue = Tools.getBytesUtf8("Hello bar!"); |
| 159 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 160 | AtomixConsistentMap map = newPrimitive("testBasicMapOperationMap"); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 161 | |
| 162 | map.isEmpty().thenAccept(result -> { |
| 163 | assertTrue(result); |
| 164 | }).join(); |
| 165 | |
| 166 | map.put("foo", rawFooValue).thenAccept(result -> { |
| 167 | assertNull(result); |
| 168 | }).join(); |
| 169 | |
| 170 | map.size().thenAccept(result -> { |
| 171 | assertTrue(result == 1); |
| 172 | }).join(); |
| 173 | |
| 174 | map.isEmpty().thenAccept(result -> { |
| 175 | assertFalse(result); |
| 176 | }).join(); |
| 177 | |
| 178 | map.putIfAbsent("foo", "Hello foo again!".getBytes()).thenAccept(result -> { |
| 179 | assertNotNull(result); |
| 180 | assertTrue(Arrays.equals(Versioned.valueOrElse(result, null), rawFooValue)); |
| 181 | }).join(); |
| 182 | |
| 183 | map.putIfAbsent("bar", rawBarValue).thenAccept(result -> { |
| 184 | assertNull(result); |
| 185 | }).join(); |
| 186 | |
| 187 | map.size().thenAccept(result -> { |
| 188 | assertTrue(result == 2); |
| 189 | }).join(); |
| 190 | |
| 191 | map.keySet().thenAccept(result -> { |
| 192 | assertTrue(result.size() == 2); |
| 193 | assertTrue(result.containsAll(Sets.newHashSet("foo", "bar"))); |
| 194 | }).join(); |
| 195 | |
| 196 | map.values().thenAccept(result -> { |
| 197 | assertTrue(result.size() == 2); |
| 198 | List<String> rawValues = |
| 199 | result.stream().map(v -> Tools.toStringUtf8(v.value())).collect(Collectors.toList()); |
| 200 | assertTrue(rawValues.contains("Hello foo!")); |
| 201 | assertTrue(rawValues.contains("Hello bar!")); |
| 202 | }).join(); |
| 203 | |
| 204 | map.entrySet().thenAccept(result -> { |
| 205 | assertTrue(result.size() == 2); |
| 206 | // TODO: check entries |
| 207 | }).join(); |
| 208 | |
| 209 | map.get("foo").thenAccept(result -> { |
| 210 | assertTrue(Arrays.equals(Versioned.valueOrElse(result, null), rawFooValue)); |
| 211 | }).join(); |
| 212 | |
| 213 | map.remove("foo").thenAccept(result -> { |
| 214 | assertTrue(Arrays.equals(Versioned.valueOrElse(result, null), rawFooValue)); |
| 215 | }).join(); |
| 216 | |
| 217 | map.containsKey("foo").thenAccept(result -> { |
| 218 | assertFalse(result); |
| 219 | }).join(); |
| 220 | |
| 221 | map.get("foo").thenAccept(result -> { |
| 222 | assertNull(result); |
| 223 | }).join(); |
| 224 | |
| 225 | map.get("bar").thenAccept(result -> { |
| 226 | assertNotNull(result); |
| 227 | assertTrue(Arrays.equals(Versioned.valueOrElse(result, null), rawBarValue)); |
| 228 | }).join(); |
| 229 | |
| 230 | map.containsKey("bar").thenAccept(result -> { |
| 231 | assertTrue(result); |
| 232 | }).join(); |
| 233 | |
| 234 | map.size().thenAccept(result -> { |
| 235 | assertTrue(result == 1); |
| 236 | }).join(); |
| 237 | |
| 238 | map.containsValue(rawBarValue).thenAccept(result -> { |
| 239 | assertTrue(result); |
| 240 | }).join(); |
| 241 | |
| 242 | map.containsValue(rawFooValue).thenAccept(result -> { |
| 243 | assertFalse(result); |
| 244 | }).join(); |
| 245 | |
| 246 | map.replace("bar", "Goodbye bar!".getBytes()).thenAccept(result -> { |
| 247 | assertNotNull(result); |
| 248 | assertTrue(Arrays.equals(Versioned.valueOrElse(result, null), rawBarValue)); |
| 249 | }).join(); |
| 250 | |
| 251 | map.replace("foo", "Goodbye foo!".getBytes()).thenAccept(result -> { |
| 252 | assertNull(result); |
| 253 | }).join(); |
| 254 | |
| 255 | // try replace_if_value_match for a non-existent key |
| 256 | map.replace("foo", "Goodbye foo!".getBytes(), rawFooValue).thenAccept(result -> { |
| 257 | assertFalse(result); |
| 258 | }).join(); |
| 259 | |
| 260 | map.replace("bar", "Goodbye bar!".getBytes(), rawBarValue).thenAccept(result -> { |
| 261 | assertTrue(result); |
| 262 | }).join(); |
| 263 | |
| 264 | map.replace("bar", "Goodbye bar!".getBytes(), rawBarValue).thenAccept(result -> { |
| 265 | assertFalse(result); |
| 266 | }).join(); |
| 267 | |
| 268 | Versioned<byte[]> barValue = map.get("bar").join(); |
| 269 | map.replace("bar", barValue.version(), "Goodbye bar!".getBytes()).thenAccept(result -> { |
| 270 | assertTrue(result); |
| 271 | }).join(); |
| 272 | |
| 273 | map.replace("bar", barValue.version(), rawBarValue).thenAccept(result -> { |
| 274 | assertFalse(result); |
| 275 | }).join(); |
| 276 | |
| 277 | map.clear().join(); |
| 278 | |
| 279 | map.size().thenAccept(result -> { |
| 280 | assertTrue(result == 0); |
| 281 | }).join(); |
| 282 | } |
| 283 | |
Aaron Kruglikov | c38dc7f | 2016-07-19 10:00:11 -0700 | [diff] [blame] | 284 | public void mapComputeOperationTests() throws Throwable { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 285 | final byte[] value1 = Tools.getBytesUtf8("value1"); |
| 286 | final byte[] value2 = Tools.getBytesUtf8("value2"); |
| 287 | final byte[] value3 = Tools.getBytesUtf8("value3"); |
| 288 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 289 | AtomixConsistentMap map = newPrimitive("testMapComputeOperationsMap"); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 290 | |
| 291 | map.computeIfAbsent("foo", k -> value1).thenAccept(result -> { |
| 292 | assertTrue(Arrays.equals(Versioned.valueOrElse(result, null), value1)); |
| 293 | }).join(); |
| 294 | |
| 295 | map.computeIfAbsent("foo", k -> value2).thenAccept(result -> { |
| 296 | assertTrue(Arrays.equals(Versioned.valueOrElse(result, null), value1)); |
| 297 | }).join(); |
| 298 | |
| 299 | map.computeIfPresent("bar", (k, v) -> value2).thenAccept(result -> { |
| 300 | assertNull(result); |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 301 | }).join(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 302 | |
| 303 | map.computeIfPresent("foo", (k, v) -> value3).thenAccept(result -> { |
| 304 | assertTrue(Arrays.equals(Versioned.valueOrElse(result, null), value3)); |
| 305 | }).join(); |
| 306 | |
| 307 | map.computeIfPresent("foo", (k, v) -> null).thenAccept(result -> { |
| 308 | assertNull(result); |
| 309 | }).join(); |
| 310 | |
| 311 | map.computeIf("foo", v -> v == null, (k, v) -> value1).thenAccept(result -> { |
| 312 | assertTrue(Arrays.equals(Versioned.valueOrElse(result, null), value1)); |
| 313 | }).join(); |
| 314 | |
| 315 | map.compute("foo", (k, v) -> value2).thenAccept(result -> { |
| 316 | assertTrue(Arrays.equals(Versioned.valueOrElse(result, null), value2)); |
| 317 | }).join(); |
| 318 | } |
| 319 | |
Aaron Kruglikov | c38dc7f | 2016-07-19 10:00:11 -0700 | [diff] [blame] | 320 | protected void mapListenerTests() throws Throwable { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 321 | final byte[] value1 = Tools.getBytesUtf8("value1"); |
| 322 | final byte[] value2 = Tools.getBytesUtf8("value2"); |
| 323 | final byte[] value3 = Tools.getBytesUtf8("value3"); |
| 324 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 325 | AtomixConsistentMap map = newPrimitive("testMapListenerMap"); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 326 | TestMapEventListener listener = new TestMapEventListener(); |
| 327 | |
| 328 | // add listener; insert new value into map and verify an INSERT event is received. |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 329 | map.addListener(listener).thenCompose(v -> map.put("foo", value1)).join(); |
| 330 | MapEvent<String, byte[]> event = listener.event(); |
| 331 | assertNotNull(event); |
| 332 | assertEquals(MapEvent.Type.INSERT, event.type()); |
| 333 | assertTrue(Arrays.equals(value1, event.newValue().value())); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 334 | |
| 335 | // remove listener and verify listener is not notified. |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 336 | map.removeListener(listener).thenCompose(v -> map.put("foo", value2)).join(); |
| 337 | assertFalse(listener.eventReceived()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 338 | |
| 339 | // add the listener back and verify UPDATE events are received correctly |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 340 | map.addListener(listener).thenCompose(v -> map.put("foo", value3)).join(); |
| 341 | event = listener.event(); |
| 342 | assertNotNull(event); |
| 343 | assertEquals(MapEvent.Type.UPDATE, event.type()); |
| 344 | assertTrue(Arrays.equals(value3, event.newValue().value())); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 345 | |
| 346 | // perform a non-state changing operation and verify no events are received. |
| 347 | map.putIfAbsent("foo", value1).join(); |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 348 | assertFalse(listener.eventReceived()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 349 | |
| 350 | // verify REMOVE events are received correctly. |
| 351 | map.remove("foo").join(); |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 352 | event = listener.event(); |
| 353 | assertNotNull(event); |
| 354 | assertEquals(MapEvent.Type.REMOVE, event.type()); |
| 355 | assertTrue(Arrays.equals(value3, event.oldValue().value())); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 356 | |
| 357 | // verify compute methods also generate events. |
| 358 | map.computeIf("foo", v -> v == null, (k, v) -> value1).join(); |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 359 | event = listener.event(); |
| 360 | assertNotNull(event); |
| 361 | assertEquals(MapEvent.Type.INSERT, event.type()); |
| 362 | assertTrue(Arrays.equals(value1, event.newValue().value())); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 363 | |
| 364 | map.compute("foo", (k, v) -> value2).join(); |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 365 | event = listener.event(); |
| 366 | assertNotNull(event); |
| 367 | assertEquals(MapEvent.Type.UPDATE, event.type()); |
| 368 | assertTrue(Arrays.equals(value2, event.newValue().value())); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 369 | |
| 370 | map.computeIf("foo", v -> Arrays.equals(v, value2), (k, v) -> null).join(); |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 371 | event = listener.event(); |
| 372 | assertNotNull(event); |
| 373 | assertEquals(MapEvent.Type.REMOVE, event.type()); |
| 374 | assertTrue(Arrays.equals(value2, event.oldValue().value())); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 375 | |
| 376 | map.removeListener(listener).join(); |
| 377 | } |
| 378 | |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 379 | protected void transactionPrepareTests() throws Throwable { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 380 | AtomixConsistentMap map = newPrimitive("testPrepareTestsMap"); |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 381 | |
| 382 | TransactionId transactionId1 = TransactionId.from("tx1"); |
| 383 | TransactionId transactionId2 = TransactionId.from("tx2"); |
| 384 | TransactionId transactionId3 = TransactionId.from("tx3"); |
| 385 | TransactionId transactionId4 = TransactionId.from("tx4"); |
| 386 | |
| 387 | Version lock1 = map.begin(transactionId1).join(); |
| 388 | |
| 389 | MapUpdate<String, byte[]> update1 = |
| 390 | MapUpdate.<String, byte[]>newBuilder() |
| 391 | .withType(MapUpdate.Type.LOCK) |
| 392 | .withKey("foo") |
| 393 | .withVersion(lock1.value()) |
| 394 | .build(); |
| 395 | MapUpdate<String, byte[]> update2 = |
| 396 | MapUpdate.<String, byte[]>newBuilder() |
| 397 | .withType(MapUpdate.Type.LOCK) |
| 398 | .withKey("bar") |
| 399 | .withVersion(lock1.value()) |
| 400 | .build(); |
| 401 | |
| 402 | map.prepare(new TransactionLog<>(transactionId1, lock1.value(), Arrays.asList(update1, update2))) |
| 403 | .thenAccept(result -> { |
| 404 | assertTrue(result); |
| 405 | }).join(); |
| 406 | |
| 407 | Version lock2 = map.begin(transactionId2).join(); |
| 408 | |
| 409 | MapUpdate<String, byte[]> update3 = |
| 410 | MapUpdate.<String, byte[]>newBuilder() |
| 411 | .withType(MapUpdate.Type.LOCK) |
| 412 | .withKey("foo") |
| 413 | .withVersion(lock2.value()) |
| 414 | .build(); |
| 415 | |
| 416 | map.prepare(new TransactionLog<>(transactionId2, lock2.value(), Arrays.asList(update3))) |
| 417 | .thenAccept(result -> { |
| 418 | assertFalse(result); |
| 419 | }).join(); |
| 420 | map.rollback(transactionId2).join(); |
| 421 | |
| 422 | Version lock3 = map.begin(transactionId3).join(); |
| 423 | |
| 424 | MapUpdate<String, byte[]> update4 = |
| 425 | MapUpdate.<String, byte[]>newBuilder() |
| 426 | .withType(MapUpdate.Type.LOCK) |
| 427 | .withKey("baz") |
| 428 | .withVersion(0) |
| 429 | .build(); |
| 430 | |
| 431 | map.prepare(new TransactionLog<>(transactionId3, lock3.value(), Arrays.asList(update4))) |
| 432 | .thenAccept(result -> { |
| 433 | assertFalse(result); |
| 434 | }).join(); |
| 435 | map.rollback(transactionId3).join(); |
| 436 | |
| 437 | Version lock4 = map.begin(transactionId4).join(); |
| 438 | |
| 439 | MapUpdate<String, byte[]> update5 = |
| 440 | MapUpdate.<String, byte[]>newBuilder() |
| 441 | .withType(MapUpdate.Type.LOCK) |
| 442 | .withKey("baz") |
| 443 | .withVersion(lock4.value()) |
| 444 | .build(); |
| 445 | |
| 446 | map.prepare(new TransactionLog<>(transactionId4, lock4.value(), Arrays.asList(update5))) |
| 447 | .thenAccept(result -> { |
| 448 | assertTrue(result); |
| 449 | }).join(); |
| 450 | } |
| 451 | |
Aaron Kruglikov | c38dc7f | 2016-07-19 10:00:11 -0700 | [diff] [blame] | 452 | protected void transactionCommitTests() throws Throwable { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 453 | final byte[] value1 = Tools.getBytesUtf8("value1"); |
| 454 | final byte[] value2 = Tools.getBytesUtf8("value2"); |
| 455 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 456 | AtomixConsistentMap map = newPrimitive("testCommitTestsMap"); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 457 | TestMapEventListener listener = new TestMapEventListener(); |
| 458 | |
| 459 | map.addListener(listener).join(); |
| 460 | |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 461 | TransactionId transactionId = TransactionId.from("tx1"); |
| 462 | |
| 463 | // Begin the transaction. |
| 464 | Version lock = map.begin(transactionId).join(); |
| 465 | |
| 466 | // PUT_IF_VERSION_MATCH |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 467 | MapUpdate<String, byte[]> update1 = |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 468 | MapUpdate.<String, byte[]>newBuilder().withType(MapUpdate.Type.PUT_IF_VERSION_MATCH) |
| 469 | .withKey("foo") |
| 470 | .withValue(value1) |
| 471 | .withVersion(lock.value()) |
| 472 | .build(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 473 | |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 474 | map.prepare(new TransactionLog<>(transactionId, lock.value(), Arrays.asList(update1))).thenAccept(result -> { |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 475 | assertEquals(true, result); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 476 | }).join(); |
HIGUCHI Yuta | caad26b | 2016-04-16 16:06:11 -0700 | [diff] [blame] | 477 | // verify changes in Tx is not visible yet until commit |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 478 | assertFalse(listener.eventReceived()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 479 | |
| 480 | map.size().thenAccept(result -> { |
| 481 | assertTrue(result == 0); |
| 482 | }).join(); |
| 483 | |
| 484 | map.get("foo").thenAccept(result -> { |
| 485 | assertNull(result); |
| 486 | }).join(); |
| 487 | |
| 488 | try { |
| 489 | map.put("foo", value2).join(); |
HIGUCHI Yuta | caad26b | 2016-04-16 16:06:11 -0700 | [diff] [blame] | 490 | fail("update to map entry in open tx should fail with Exception"); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 491 | } catch (CompletionException e) { |
| 492 | assertEquals(ConcurrentModificationException.class, e.getCause().getClass()); |
| 493 | } |
| 494 | |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 495 | assertFalse(listener.eventReceived()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 496 | |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 497 | map.commit(transactionId).join(); |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 498 | MapEvent<String, byte[]> event = listener.event(); |
| 499 | assertNotNull(event); |
| 500 | assertEquals(MapEvent.Type.INSERT, event.type()); |
| 501 | assertTrue(Arrays.equals(value1, event.newValue().value())); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 502 | |
HIGUCHI Yuta | caad26b | 2016-04-16 16:06:11 -0700 | [diff] [blame] | 503 | // map should be update-able after commit |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 504 | map.put("foo", value2).thenAccept(result -> { |
| 505 | assertTrue(Arrays.equals(Versioned.valueOrElse(result, null), value1)); |
| 506 | }).join(); |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 507 | event = listener.event(); |
| 508 | assertNotNull(event); |
| 509 | assertEquals(MapEvent.Type.UPDATE, event.type()); |
| 510 | assertTrue(Arrays.equals(value2, event.newValue().value())); |
HIGUCHI Yuta | caad26b | 2016-04-16 16:06:11 -0700 | [diff] [blame] | 511 | |
HIGUCHI Yuta | caad26b | 2016-04-16 16:06:11 -0700 | [diff] [blame] | 512 | // REMOVE_IF_VERSION_MATCH |
| 513 | byte[] currFoo = map.get("foo").get().value(); |
| 514 | long currFooVersion = map.get("foo").get().version(); |
| 515 | MapUpdate<String, byte[]> remove1 = |
| 516 | MapUpdate.<String, byte[]>newBuilder().withType(MapUpdate.Type.REMOVE_IF_VERSION_MATCH) |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 517 | .withKey("foo") |
| 518 | .withVersion(currFooVersion) |
| 519 | .build(); |
HIGUCHI Yuta | caad26b | 2016-04-16 16:06:11 -0700 | [diff] [blame] | 520 | |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 521 | transactionId = TransactionId.from("tx2"); |
HIGUCHI Yuta | caad26b | 2016-04-16 16:06:11 -0700 | [diff] [blame] | 522 | |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 523 | // Begin the transaction. |
| 524 | map.begin(transactionId).join(); |
| 525 | |
| 526 | map.prepare(new TransactionLog<>(transactionId, lock.value(), Arrays.asList(remove1))).thenAccept(result -> { |
HIGUCHI Yuta | caad26b | 2016-04-16 16:06:11 -0700 | [diff] [blame] | 527 | assertTrue("prepare should succeed", result); |
| 528 | }).join(); |
| 529 | // verify changes in Tx is not visible yet until commit |
| 530 | assertFalse(listener.eventReceived()); |
| 531 | |
| 532 | map.size().thenAccept(size -> { |
| 533 | assertThat(size, is(1)); |
| 534 | }).join(); |
| 535 | |
| 536 | map.get("foo").thenAccept(result -> { |
| 537 | assertThat(result.value(), is(currFoo)); |
| 538 | }).join(); |
| 539 | |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 540 | map.commit(transactionId).join(); |
HIGUCHI Yuta | caad26b | 2016-04-16 16:06:11 -0700 | [diff] [blame] | 541 | event = listener.event(); |
| 542 | assertNotNull(event); |
| 543 | assertEquals(MapEvent.Type.REMOVE, event.type()); |
| 544 | assertArrayEquals(currFoo, event.oldValue().value()); |
| 545 | |
| 546 | map.size().thenAccept(size -> { |
| 547 | assertThat(size, is(0)); |
| 548 | }).join(); |
| 549 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 550 | } |
| 551 | |
Aaron Kruglikov | c38dc7f | 2016-07-19 10:00:11 -0700 | [diff] [blame] | 552 | protected void transactionRollbackTests() throws Throwable { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 553 | final byte[] value1 = Tools.getBytesUtf8("value1"); |
| 554 | final byte[] value2 = Tools.getBytesUtf8("value2"); |
| 555 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 556 | AtomixConsistentMap map = newPrimitive("testTransactionRollbackTestsMap"); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 557 | TestMapEventListener listener = new TestMapEventListener(); |
| 558 | |
| 559 | map.addListener(listener).join(); |
| 560 | |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 561 | TransactionId transactionId = TransactionId.from("tx1"); |
| 562 | |
| 563 | Version lock = map.begin(transactionId).join(); |
| 564 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 565 | MapUpdate<String, byte[]> update1 = |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 566 | MapUpdate.<String, byte[]>newBuilder().withType(MapUpdate.Type.PUT_IF_VERSION_MATCH) |
| 567 | .withKey("foo") |
| 568 | .withValue(value1) |
| 569 | .withVersion(lock.value()) |
| 570 | .build(); |
| 571 | |
| 572 | map.prepare(new TransactionLog<>(transactionId, lock.value(), Arrays.asList(update1))).thenAccept(result -> { |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 573 | assertEquals(true, result); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 574 | }).join(); |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 575 | assertFalse(listener.eventReceived()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 576 | |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame] | 577 | map.rollback(transactionId).join(); |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 578 | assertFalse(listener.eventReceived()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 579 | |
| 580 | map.get("foo").thenAccept(result -> { |
| 581 | assertNull(result); |
| 582 | }).join(); |
| 583 | |
| 584 | map.put("foo", value2).thenAccept(result -> { |
| 585 | assertNull(result); |
| 586 | }).join(); |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 587 | MapEvent<String, byte[]> event = listener.event(); |
| 588 | assertNotNull(event); |
| 589 | assertEquals(MapEvent.Type.INSERT, event.type()); |
| 590 | assertTrue(Arrays.equals(value2, event.newValue().value())); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Jordan Halterman | ca7660a | 2018-04-04 23:43:23 -0700 | [diff] [blame] | 593 | @Test |
| 594 | public void testCompatibilityFunction() throws Throwable { |
| 595 | AtomixConsistentMap atomixMap = newPrimitive("testCompatibilityFunction"); |
| 596 | |
| 597 | Serializer rawSerializer = Serializer.using(KryoNamespaces.API, CompatibleValue.class); |
| 598 | Serializer valueSerializer = Serializer.using(KryoNamespaces.BASIC); |
| 599 | |
| 600 | // Convert the byte[] value to CompatibleValue<byte[]> |
| 601 | AsyncConsistentMap<String, CompatibleValue<byte[]>> rawMap = DistributedPrimitives.newTranscodingMap( |
| 602 | atomixMap, |
| 603 | key -> HexString.toHexString(rawSerializer.encode(key)), |
| 604 | string -> rawSerializer.decode(HexString.fromHexString(string)), |
| 605 | value -> value == null ? null : rawSerializer.encode(value), |
| 606 | bytes -> rawSerializer.decode(bytes)); |
| 607 | |
| 608 | // Convert the CompatibleValue<byte[]> value to CompatibleValue<V> using the user-provided serializer. |
| 609 | AsyncConsistentMap<String, CompatibleValue<String>> compatibleMap = |
| 610 | DistributedPrimitives.newTranscodingMap( |
| 611 | rawMap, |
| 612 | key -> key, |
| 613 | key -> key, |
| 614 | value -> value == null ? null : |
| 615 | new CompatibleValue<byte[]>(valueSerializer.encode(value.value()), value.version()), |
| 616 | value -> value == null ? null : |
| 617 | new CompatibleValue<String>(valueSerializer.decode(value.value()), value.version())); |
| 618 | |
| 619 | AsyncConsistentMap<String, String> map1 = DistributedPrimitives.newCompatibleMap( |
| 620 | compatibleMap, |
| 621 | (value, version) -> version + ":" + value, |
| 622 | org.onosproject.core.Version.version("1.0.0")); |
| 623 | AsyncConsistentMap<String, String> map2 = DistributedPrimitives.newCompatibleMap( |
| 624 | compatibleMap, |
| 625 | (value, version) -> version + ":" + value, |
| 626 | org.onosproject.core.Version.version("1.0.1")); |
| 627 | |
| 628 | map1.put("foo", "Hello world!").join(); |
| 629 | assertEquals("Hello world!", map1.get("foo").join().value()); |
| 630 | assertEquals("1.0.0:Hello world!", map2.get("foo").join().value()); |
| 631 | |
| 632 | map2.put("bar", "Hello world again!").join(); |
| 633 | assertEquals("Hello world again!", map2.get("bar").join().value()); |
| 634 | assertEquals("1.0.1:Hello world again!", map1.get("bar").join().value()); |
| 635 | } |
| 636 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 637 | private static class TestMapEventListener implements MapEventListener<String, byte[]> { |
| 638 | |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 639 | private final BlockingQueue<MapEvent<String, byte[]>> queue = new ArrayBlockingQueue<>(1); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 640 | |
| 641 | @Override |
| 642 | public void event(MapEvent<String, byte[]> event) { |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 643 | try { |
| 644 | queue.put(event); |
| 645 | } catch (InterruptedException e) { |
Ray Milkey | 6a51cb9 | 2018-03-06 09:03:03 -0800 | [diff] [blame] | 646 | Thread.currentThread().interrupt(); |
| 647 | throw new IllegalStateException(e); |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 648 | } |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 651 | public boolean eventReceived() { |
| 652 | return !queue.isEmpty(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 653 | } |
| 654 | |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 655 | public MapEvent<String, byte[]> event() throws InterruptedException { |
| 656 | return queue.take(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | } |