Search in sources :

Example 1 with DefaultBackupInput

use of io.atomix.primitive.service.impl.DefaultBackupInput in project atomix by atomix.

the class BackupRole method requestRestore.

/**
 * Requests a restore from the primary.
 */
private void requestRestore(MemberId primary) {
    context.protocol().restore(primary, RestoreRequest.request(context.descriptor(), context.currentTerm())).whenCompleteAsync((response, error) -> {
        if (error == null && response.status() == PrimaryBackupResponse.Status.OK) {
            context.resetIndex(response.index(), response.timestamp());
            Buffer buffer = HeapBuffer.wrap(response.data());
            int sessions = buffer.readInt();
            for (int i = 0; i < sessions; i++) {
                context.getOrCreateSession(buffer.readLong(), MemberId.from(buffer.readString()));
            }
            context.service().restore(new DefaultBackupInput(buffer, context.service().serializer()));
            operations.clear();
        }
    }, context.threadContext());
}
Also used : Buffer(io.atomix.storage.buffer.Buffer) HeapBuffer(io.atomix.storage.buffer.HeapBuffer) DefaultBackupInput(io.atomix.primitive.service.impl.DefaultBackupInput)

Example 2 with DefaultBackupInput

use of io.atomix.primitive.service.impl.DefaultBackupInput in project atomix by atomix.

the class AtomicSemaphoreTest method testSnapshot.

@Test
public void testSnapshot() throws Exception {
    AbstractAtomicSemaphoreService service = new DefaultAtomicSemaphoreService(new AtomicSemaphoreServiceConfig().setInitialCapacity(10));
    Field available = AbstractAtomicSemaphoreService.class.getDeclaredField("available");
    available.setAccessible(true);
    Field holders = AbstractAtomicSemaphoreService.class.getDeclaredField("holders");
    holders.setAccessible(true);
    Field waiterQueue = AbstractAtomicSemaphoreService.class.getDeclaredField("waiterQueue");
    waiterQueue.setAccessible(true);
    Field timers = AbstractAtomicSemaphoreService.class.getDeclaredField("timers");
    timers.setAccessible(true);
    available.set(service, 10);
    Map<Long, Integer> holdersMap = new HashMap<>();
    holdersMap.put((long) 1, 2);
    holdersMap.put((long) 3, 4);
    holdersMap.put((long) 5, 6);
    holders.set(service, holdersMap);
    // Class<?> waiter = Class.forName("io.atomix.core.semaphore.impl.DistributedSemaphoreService$Waiter");
    // LinkedList<Object> waiterLinkedList = new LinkedList<>();
    // 
    // waiterLinkedList.add(waiter.getConstructors()[0].newInstance(service,10L, 20L, 30L, 40, Long.MAX_VALUE));
    // waiterQueue.set(service, waiterLinkedList);
    Buffer buffer = HeapBuffer.allocate();
    service.backup(new DefaultBackupOutput(buffer, service.serializer()));
    AbstractAtomicSemaphoreService serviceRestore = new DefaultAtomicSemaphoreService(new AtomicSemaphoreServiceConfig().setInitialCapacity(10));
    serviceRestore.restore(new DefaultBackupInput(buffer.flip(), service.serializer()));
    assertEquals(10, available.get(serviceRestore));
    assertEquals(holdersMap, holders.get(serviceRestore));
// assertEquals(waiterQueue.get(serviceRestore), waiterLinkedList);
// assertEquals(1, ((Map) (timers.get(serviceRestore))).keySet().size());
}
Also used : AbstractAtomicSemaphoreService(io.atomix.core.semaphore.impl.AbstractAtomicSemaphoreService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Buffer(io.atomix.storage.buffer.Buffer) HeapBuffer(io.atomix.storage.buffer.HeapBuffer) Field(java.lang.reflect.Field) HashMap(java.util.HashMap) AtomicSemaphoreServiceConfig(io.atomix.core.semaphore.impl.AtomicSemaphoreServiceConfig) DefaultBackupOutput(io.atomix.primitive.service.impl.DefaultBackupOutput) DefaultAtomicSemaphoreService(io.atomix.core.semaphore.impl.DefaultAtomicSemaphoreService) DefaultBackupInput(io.atomix.primitive.service.impl.DefaultBackupInput) Test(org.junit.Test) AbstractPrimitiveTest(io.atomix.core.AbstractPrimitiveTest)

Example 3 with DefaultBackupInput

use of io.atomix.primitive.service.impl.DefaultBackupInput in project atomix by atomix.

the class DefaultLeaderElectionServiceTest method testSnapshot.

@Test
public void testSnapshot() throws Exception {
    ServiceContext context = mock(ServiceContext.class);
    when(context.serviceType()).thenReturn(LeaderElectionType.instance());
    when(context.serviceName()).thenReturn("test");
    when(context.serviceId()).thenReturn(PrimitiveId.from(1));
    when(context.wallClock()).thenReturn(new WallClock());
    Session session = mock(Session.class);
    when(session.sessionId()).thenReturn(SessionId.from(1));
    when(context.currentSession()).thenReturn(session);
    DefaultLeaderElectionService service = new DefaultLeaderElectionService();
    service.init(context);
    service.register(session);
    byte[] id = "a".getBytes();
    service.run(id);
    Buffer buffer = HeapBuffer.allocate();
    service.backup(new DefaultBackupOutput(buffer, service.serializer()));
    service = new DefaultLeaderElectionService();
    service.init(context);
    service.register(session);
    service.restore(new DefaultBackupInput(buffer.flip(), service.serializer()));
    Leadership<byte[]> value = service.getLeadership();
    assertNotNull(value);
    assertArrayEquals(id, value.leader().id());
}
Also used : Buffer(io.atomix.storage.buffer.Buffer) HeapBuffer(io.atomix.storage.buffer.HeapBuffer) ServiceContext(io.atomix.primitive.service.ServiceContext) WallClock(io.atomix.utils.time.WallClock) DefaultBackupOutput(io.atomix.primitive.service.impl.DefaultBackupOutput) Session(io.atomix.primitive.session.Session) DefaultBackupInput(io.atomix.primitive.service.impl.DefaultBackupInput) Test(org.junit.Test)

Example 4 with DefaultBackupInput

use of io.atomix.primitive.service.impl.DefaultBackupInput in project atomix by atomix.

the class DefaultDistributedQueueServiceTest method testSnapshot.

@Test
@SuppressWarnings("unchecked")
public void testSnapshot() throws Exception {
    ServiceContext context = mock(ServiceContext.class);
    when(context.serviceType()).thenReturn(DistributedSetType.instance());
    when(context.serviceName()).thenReturn("test");
    when(context.serviceId()).thenReturn(PrimitiveId.from(1));
    when(context.wallClock()).thenReturn(new WallClock());
    Session session = mock(Session.class);
    when(session.sessionId()).thenReturn(SessionId.from(1));
    DefaultDistributedQueueService service = new DefaultDistributedQueueService();
    service.init(context);
    service.add("foo");
    Buffer buffer = HeapBuffer.allocate();
    service.backup(new DefaultBackupOutput(buffer, service.serializer()));
    service = new DefaultDistributedQueueService();
    service.restore(new DefaultBackupInput(buffer.flip(), service.serializer()));
    assertEquals("foo", service.remove());
}
Also used : Buffer(io.atomix.storage.buffer.Buffer) HeapBuffer(io.atomix.storage.buffer.HeapBuffer) ServiceContext(io.atomix.primitive.service.ServiceContext) WallClock(io.atomix.utils.time.WallClock) DefaultBackupOutput(io.atomix.primitive.service.impl.DefaultBackupOutput) Session(io.atomix.primitive.session.Session) DefaultBackupInput(io.atomix.primitive.service.impl.DefaultBackupInput) Test(org.junit.Test)

Example 5 with DefaultBackupInput

use of io.atomix.primitive.service.impl.DefaultBackupInput in project atomix by atomix.

the class DefaultDistributedNavigableSetServiceTest method testSnapshot.

@Test
@SuppressWarnings("unchecked")
public void testSnapshot() throws Exception {
    ServiceContext context = mock(ServiceContext.class);
    when(context.serviceType()).thenReturn(DistributedSetType.instance());
    when(context.serviceName()).thenReturn("test");
    when(context.serviceId()).thenReturn(PrimitiveId.from(1));
    when(context.wallClock()).thenReturn(new WallClock());
    Session session = mock(Session.class);
    when(session.sessionId()).thenReturn(SessionId.from(1));
    DefaultDistributedNavigableSetService service = new DefaultDistributedNavigableSetService();
    service.init(context);
    service.add("foo");
    Buffer buffer = HeapBuffer.allocate();
    service.backup(new DefaultBackupOutput(buffer, service.serializer()));
    service = new DefaultDistributedNavigableSetService();
    service.restore(new DefaultBackupInput(buffer.flip(), service.serializer()));
    assertTrue(service.contains("foo"));
}
Also used : Buffer(io.atomix.storage.buffer.Buffer) HeapBuffer(io.atomix.storage.buffer.HeapBuffer) ServiceContext(io.atomix.primitive.service.ServiceContext) WallClock(io.atomix.utils.time.WallClock) DefaultBackupOutput(io.atomix.primitive.service.impl.DefaultBackupOutput) Session(io.atomix.primitive.session.Session) DefaultBackupInput(io.atomix.primitive.service.impl.DefaultBackupInput) Test(org.junit.Test)

Aggregations

DefaultBackupInput (io.atomix.primitive.service.impl.DefaultBackupInput)18 Buffer (io.atomix.storage.buffer.Buffer)17 HeapBuffer (io.atomix.storage.buffer.HeapBuffer)17 DefaultBackupOutput (io.atomix.primitive.service.impl.DefaultBackupOutput)16 Test (org.junit.Test)16 ServiceContext (io.atomix.primitive.service.ServiceContext)12 Session (io.atomix.primitive.session.Session)12 WallClock (io.atomix.utils.time.WallClock)11 MemberId (io.atomix.cluster.MemberId)1 AbstractPrimitiveTest (io.atomix.core.AbstractPrimitiveTest)1 AbstractAtomicSemaphoreService (io.atomix.core.semaphore.impl.AbstractAtomicSemaphoreService)1 AtomicSemaphoreServiceConfig (io.atomix.core.semaphore.impl.AtomicSemaphoreServiceConfig)1 DefaultAtomicSemaphoreService (io.atomix.core.semaphore.impl.DefaultAtomicSemaphoreService)1 Task (io.atomix.core.workqueue.Task)1 PrimitiveType (io.atomix.primitive.PrimitiveType)1 SessionId (io.atomix.primitive.session.SessionId)1 ReadConsistency (io.atomix.protocols.raft.ReadConsistency)1 RaftSession (io.atomix.protocols.raft.session.RaftSession)1 ConfigurationException (io.atomix.utils.config.ConfigurationException)1 WallClockTimestamp (io.atomix.utils.time.WallClockTimestamp)1