Jordan Halterman | 4743258 | 2018-01-25 16:56:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | |
| 18 | import java.util.concurrent.atomic.AtomicLong; |
| 19 | |
| 20 | import io.atomix.protocols.raft.ReadConsistency; |
| 21 | import io.atomix.protocols.raft.cluster.MemberId; |
| 22 | import io.atomix.protocols.raft.impl.RaftContext; |
Jordan Halterman | 4ce65e8 | 2018-02-15 18:09:38 -0800 | [diff] [blame] | 23 | import io.atomix.protocols.raft.impl.RaftServiceManager; |
Jordan Halterman | 4743258 | 2018-01-25 16:56:45 -0800 | [diff] [blame] | 24 | import io.atomix.protocols.raft.operation.OperationType; |
| 25 | import io.atomix.protocols.raft.protocol.RaftServerProtocol; |
| 26 | import io.atomix.protocols.raft.service.ServiceId; |
| 27 | import io.atomix.protocols.raft.service.ServiceType; |
| 28 | import io.atomix.protocols.raft.service.impl.DefaultCommit; |
| 29 | import io.atomix.protocols.raft.service.impl.DefaultServiceContext; |
| 30 | import io.atomix.protocols.raft.session.SessionId; |
| 31 | import io.atomix.protocols.raft.session.impl.RaftSessionContext; |
| 32 | import io.atomix.protocols.raft.storage.RaftStorage; |
| 33 | import io.atomix.protocols.raft.storage.snapshot.Snapshot; |
| 34 | import io.atomix.protocols.raft.storage.snapshot.SnapshotReader; |
| 35 | import io.atomix.protocols.raft.storage.snapshot.SnapshotStore; |
| 36 | import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter; |
| 37 | import io.atomix.storage.StorageLevel; |
| 38 | import io.atomix.time.WallClockTimestamp; |
| 39 | import io.atomix.utils.concurrent.AtomixThreadFactory; |
| 40 | import io.atomix.utils.concurrent.SingleThreadContextFactory; |
| 41 | import io.atomix.utils.concurrent.ThreadContext; |
| 42 | import org.junit.Test; |
| 43 | |
| 44 | import static org.easymock.EasyMock.expect; |
| 45 | import static org.easymock.EasyMock.mock; |
| 46 | import static org.easymock.EasyMock.replay; |
| 47 | import static org.onosproject.store.service.DistributedPrimitive.Type.LEADER_ELECTOR; |
| 48 | |
| 49 | /** |
| 50 | * Distributed lock service test. |
| 51 | */ |
| 52 | public class AtomixDistributedLockServiceTest { |
| 53 | @Test |
| 54 | public void testSnapshot() throws Exception { |
| 55 | SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder() |
| 56 | .withPrefix("test") |
| 57 | .withStorageLevel(StorageLevel.MEMORY) |
| 58 | .build()); |
Jordan Halterman | 4ce65e8 | 2018-02-15 18:09:38 -0800 | [diff] [blame] | 59 | Snapshot snapshot = store.newSnapshot(2, new WallClockTimestamp()); |
Jordan Halterman | 4743258 | 2018-01-25 16:56:45 -0800 | [diff] [blame] | 60 | |
| 61 | AtomicLong index = new AtomicLong(); |
| 62 | DefaultServiceContext context = mock(DefaultServiceContext.class); |
| 63 | expect(context.serviceType()).andReturn(ServiceType.from(LEADER_ELECTOR.name())).anyTimes(); |
| 64 | expect(context.serviceName()).andReturn("test").anyTimes(); |
| 65 | expect(context.serviceId()).andReturn(ServiceId.from(1)).anyTimes(); |
Jordan Halterman | 4743258 | 2018-01-25 16:56:45 -0800 | [diff] [blame] | 66 | expect(context.currentIndex()).andReturn(index.get()).anyTimes(); |
| 67 | expect(context.currentOperation()).andReturn(OperationType.COMMAND).anyTimes(); |
| 68 | |
| 69 | RaftContext server = mock(RaftContext.class); |
Jordan Halterman | 4ce65e8 | 2018-02-15 18:09:38 -0800 | [diff] [blame] | 70 | expect(server.getProtocol()).andReturn(mock(RaftServerProtocol.class)).anyTimes(); |
| 71 | RaftServiceManager manager = mock(RaftServiceManager.class); |
| 72 | expect(manager.executor()).andReturn(mock(ThreadContext.class)).anyTimes(); |
| 73 | expect(server.getServiceManager()).andReturn(manager).anyTimes(); |
Jordan Halterman | 4743258 | 2018-01-25 16:56:45 -0800 | [diff] [blame] | 74 | |
| 75 | replay(context, server); |
| 76 | |
| 77 | AtomixDistributedLockService service = new AtomixDistributedLockService(); |
| 78 | service.init(context); |
| 79 | |
| 80 | RaftSessionContext session = new RaftSessionContext( |
| 81 | SessionId.from(1), |
| 82 | MemberId.from("1"), |
| 83 | "test", |
| 84 | ServiceType.from(LEADER_ELECTOR.name()), |
| 85 | ReadConsistency.LINEARIZABLE, |
| 86 | 100, |
| 87 | 5000, |
| 88 | System.currentTimeMillis(), |
| 89 | context, |
| 90 | server, |
| 91 | new SingleThreadContextFactory(new AtomixThreadFactory())); |
| 92 | session.open(); |
| 93 | |
| 94 | service.lock(new DefaultCommit<>( |
| 95 | index.incrementAndGet(), |
| 96 | AtomixDistributedLockOperations.LOCK, |
| 97 | new AtomixDistributedLockOperations.Lock(1, 0), |
| 98 | session, |
| 99 | System.currentTimeMillis())); |
| 100 | |
| 101 | try (SnapshotWriter writer = snapshot.openWriter()) { |
| 102 | service.snapshot(writer); |
| 103 | } |
| 104 | |
| 105 | snapshot.complete(); |
| 106 | |
| 107 | service = new AtomixDistributedLockService(); |
| 108 | try (SnapshotReader reader = snapshot.openReader()) { |
| 109 | service.install(reader); |
| 110 | } |
| 111 | |
| 112 | service.unlock(new DefaultCommit<>( |
| 113 | index.incrementAndGet(), |
| 114 | AtomixDistributedLockOperations.UNLOCK, |
| 115 | new AtomixDistributedLockOperations.Unlock(1), |
| 116 | session, |
| 117 | System.currentTimeMillis())); |
| 118 | } |
| 119 | } |