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 2017-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 | package org.onosproject.store.primitives.resources.impl; |
| 17 | |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 18 | import io.atomix.protocols.raft.ReadConsistency; |
| 19 | import io.atomix.protocols.raft.cluster.MemberId; |
| 20 | import io.atomix.protocols.raft.impl.RaftContext; |
| 21 | import io.atomix.protocols.raft.protocol.RaftServerProtocol; |
| 22 | import io.atomix.protocols.raft.service.ServiceId; |
| 23 | import io.atomix.protocols.raft.service.ServiceType; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 24 | import io.atomix.protocols.raft.service.impl.DefaultCommit; |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 25 | import io.atomix.protocols.raft.service.impl.DefaultServiceContext; |
| 26 | import io.atomix.protocols.raft.session.RaftSession; |
| 27 | import io.atomix.protocols.raft.session.SessionId; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 28 | import io.atomix.protocols.raft.session.impl.RaftSessionContext; |
| 29 | import io.atomix.protocols.raft.storage.RaftStorage; |
| 30 | import io.atomix.protocols.raft.storage.snapshot.Snapshot; |
| 31 | import io.atomix.protocols.raft.storage.snapshot.SnapshotReader; |
| 32 | import io.atomix.protocols.raft.storage.snapshot.SnapshotStore; |
| 33 | import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter; |
| 34 | import io.atomix.storage.StorageLevel; |
| 35 | import io.atomix.time.WallClockTimestamp; |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 36 | import io.atomix.utils.concurrent.AtomixThreadFactory; |
| 37 | import io.atomix.utils.concurrent.SingleThreadContextFactory; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 38 | import org.junit.Test; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 39 | import org.onosproject.store.service.Versioned; |
| 40 | |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 41 | import static org.easymock.EasyMock.expect; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 42 | import static org.easymock.EasyMock.mock; |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 43 | import static org.easymock.EasyMock.replay; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 44 | import static org.junit.Assert.assertArrayEquals; |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 45 | import static org.junit.Assert.assertEquals; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 46 | import static org.junit.Assert.assertNotNull; |
| 47 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.GET; |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 48 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.NEXT; |
| 49 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.OPEN_ITERATOR; |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 50 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.PUT; |
| 51 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.Put; |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 52 | import static org.onosproject.store.service.DistributedPrimitive.Type.LEADER_ELECTOR; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Consistent map service test. |
| 56 | */ |
| 57 | public class AtomixConsistentMapServiceTest { |
| 58 | @Test |
| 59 | @SuppressWarnings("unchecked") |
| 60 | public void testSnapshot() throws Exception { |
| 61 | SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder() |
| 62 | .withPrefix("test") |
| 63 | .withStorageLevel(StorageLevel.MEMORY) |
| 64 | .build()); |
Jordan Halterman | 4ce65e8 | 2018-02-15 18:09:38 -0800 | [diff] [blame] | 65 | Snapshot snapshot = store.newSnapshot(2, new WallClockTimestamp()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 66 | |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 67 | DefaultServiceContext context = mock(DefaultServiceContext.class); |
| 68 | expect(context.serviceType()).andReturn(ServiceType.from(LEADER_ELECTOR.name())).anyTimes(); |
| 69 | expect(context.serviceName()).andReturn("test").anyTimes(); |
| 70 | expect(context.serviceId()).andReturn(ServiceId.from(1)).anyTimes(); |
| 71 | |
| 72 | RaftContext server = mock(RaftContext.class); |
| 73 | expect(server.getProtocol()).andReturn(mock(RaftServerProtocol.class)); |
| 74 | |
| 75 | replay(context, server); |
| 76 | |
| 77 | RaftSession session = new RaftSessionContext( |
| 78 | SessionId.from(1), |
| 79 | MemberId.from("1"), |
| 80 | "test", |
| 81 | ServiceType.from(LEADER_ELECTOR.name()), |
| 82 | ReadConsistency.LINEARIZABLE, |
| 83 | 100, |
| 84 | 5000, |
| 85 | System.currentTimeMillis(), |
| 86 | context, |
| 87 | server, |
| 88 | new SingleThreadContextFactory(new AtomixThreadFactory())); |
| 89 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 90 | AtomixConsistentMapService service = new AtomixConsistentMapService(); |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 91 | service.put(new DefaultCommit<>( |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 92 | 2, |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 93 | PUT, |
| 94 | new Put("foo", "Hello world!".getBytes()), |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 95 | session, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 96 | System.currentTimeMillis())); |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 97 | service.openIterator(new DefaultCommit<>( |
| 98 | 3, |
| 99 | OPEN_ITERATOR, |
| 100 | null, |
| 101 | session, |
| 102 | System.currentTimeMillis())); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 103 | |
| 104 | try (SnapshotWriter writer = snapshot.openWriter()) { |
| 105 | service.snapshot(writer); |
| 106 | } |
| 107 | |
| 108 | snapshot.complete(); |
| 109 | |
| 110 | service = new AtomixConsistentMapService(); |
| 111 | try (SnapshotReader reader = snapshot.openReader()) { |
| 112 | service.install(reader); |
| 113 | } |
| 114 | |
| 115 | Versioned<byte[]> value = service.get(new DefaultCommit<>( |
| 116 | 2, |
| 117 | GET, |
| 118 | new AtomixConsistentMapOperations.Get("foo"), |
| 119 | mock(RaftSessionContext.class), |
| 120 | System.currentTimeMillis())); |
| 121 | assertNotNull(value); |
| 122 | assertArrayEquals("Hello world!".getBytes(), value.value()); |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 123 | |
| 124 | assertEquals(1, service.next(new DefaultCommit<>( |
| 125 | 4, |
| 126 | NEXT, |
| 127 | new AtomixConsistentMapOperations.IteratorPosition(3L, 0), |
| 128 | session, |
| 129 | System.currentTimeMillis())).entries().size()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 130 | } |
| 131 | } |