Search in sources :

Example 6 with DefaultBackupOutput

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

the class DefaultAtomicMultimapServiceTest method testSnapshot.

@Test
@SuppressWarnings("unchecked")
public void testSnapshot() throws Exception {
    ServiceContext context = mock(ServiceContext.class);
    when(context.serviceType()).thenReturn(AtomicMultimapType.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));
    DefaultAtomicMultimapService service = new DefaultAtomicMultimapService();
    service.init(context);
    service.put("foo", "Hello world!".getBytes());
    Buffer buffer = HeapBuffer.allocate();
    service.backup(new DefaultBackupOutput(buffer, service.serializer()));
    service = new DefaultAtomicMultimapService();
    service.init(context);
    service.restore(new DefaultBackupInput(buffer.flip(), service.serializer()));
    Versioned<Collection<byte[]>> value = service.get("foo");
    assertNotNull(value);
    assertEquals(1, value.value().size());
    assertArrayEquals("Hello world!".getBytes(), value.value().iterator().next());
}
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) Collection(java.util.Collection) 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 7 with DefaultBackupOutput

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

the class DefaultDistributedListServiceTest 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));
    DefaultDistributedListService service = new DefaultDistributedListService();
    service.init(context);
    service.add("foo");
    Buffer buffer = HeapBuffer.allocate();
    service.backup(new DefaultBackupOutput(buffer, service.serializer()));
    service = new DefaultDistributedListService();
    service.restore(new DefaultBackupInput(buffer.flip(), service.serializer()));
    assertEquals("foo", service.get(0));
}
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 8 with DefaultBackupOutput

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

the class PrimaryRole method restore.

@Override
public CompletableFuture<RestoreResponse> restore(RestoreRequest request) {
    logRequest(request);
    if (request.term() != context.currentTerm()) {
        return CompletableFuture.completedFuture(logResponse(RestoreResponse.error()));
    }
    HeapBuffer buffer = HeapBuffer.allocate();
    try {
        Collection<PrimaryBackupSession> sessions = context.getSessions();
        buffer.writeInt(sessions.size());
        for (Session session : sessions) {
            buffer.writeLong(session.sessionId().id());
            buffer.writeString(session.memberId().id());
        }
        context.service().backup(new DefaultBackupOutput(buffer, context.service().serializer()));
        buffer.flip();
        byte[] bytes = buffer.readBytes(buffer.remaining());
        return CompletableFuture.completedFuture(RestoreResponse.ok(context.currentIndex(), context.currentTimestamp(), bytes)).thenApply(this::logResponse);
    } finally {
        buffer.release();
    }
}
Also used : HeapBuffer(io.atomix.storage.buffer.HeapBuffer) PrimaryBackupSession(io.atomix.protocols.backup.impl.PrimaryBackupSession) DefaultBackupOutput(io.atomix.primitive.service.impl.DefaultBackupOutput) PrimaryBackupSession(io.atomix.protocols.backup.impl.PrimaryBackupSession) Session(io.atomix.primitive.session.Session)

Example 9 with DefaultBackupOutput

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

the class RaftServiceContext method takeSnapshot.

/**
 * Takes a snapshot of the service state.
 */
public void takeSnapshot(SnapshotWriter writer) {
    log.debug("Taking snapshot {}", writer.snapshot().index());
    // Serialize sessions to the in-memory snapshot and request a snapshot from the state machine.
    writer.writeLong(primitiveId.id());
    writer.writeString(primitiveType.name());
    writer.writeString(serviceName);
    writer.writeLong(currentIndex);
    writer.writeLong(currentTimestamp);
    writer.writeLong(timestampDelta);
    writer.writeInt(sessions.getSessions().size());
    for (RaftSession session : sessions.getSessions()) {
        writer.writeLong(session.sessionId().id());
        writer.writeString(session.memberId().id());
        writer.writeString(session.readConsistency().name());
        writer.writeLong(session.minTimeout());
        writer.writeLong(session.maxTimeout());
        writer.writeLong(session.getLastUpdated());
        writer.writeLong(session.getRequestSequence());
        writer.writeLong(session.getCommandSequence());
        writer.writeLong(session.getEventIndex());
        writer.writeLong(session.getLastCompleted());
    }
    service.backup(new DefaultBackupOutput(writer, service.serializer()));
}
Also used : RaftSession(io.atomix.protocols.raft.session.RaftSession) DefaultBackupOutput(io.atomix.primitive.service.impl.DefaultBackupOutput)

Example 10 with DefaultBackupOutput

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

the class DefaultAtomicValueServiceTest method testSnapshot.

@Test
@SuppressWarnings("unchecked")
public void testSnapshot() throws Exception {
    ServiceContext context = mock(ServiceContext.class);
    when(context.serviceType()).thenReturn(AtomicMapType.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));
    DefaultAtomicValueService service = new DefaultAtomicValueService();
    service.init(context);
    assertNull(service.get());
    Buffer buffer = HeapBuffer.allocate();
    service.backup(new DefaultBackupOutput(buffer, service.serializer()));
    assertNull(service.get());
    service = new DefaultAtomicValueService();
    service.restore(new DefaultBackupInput(buffer.flip(), service.serializer()));
    assertNull(service.get());
    service.set("Hello world!".getBytes());
    assertArrayEquals("Hello world!".getBytes(), service.get());
    buffer = HeapBuffer.allocate();
    service.backup(new DefaultBackupOutput(buffer, service.serializer()));
    assertArrayEquals("Hello world!".getBytes(), service.get());
    service = new DefaultAtomicValueService();
    service.restore(new DefaultBackupInput(buffer.flip(), service.serializer()));
    assertArrayEquals("Hello world!".getBytes(), service.get());
    service.set(null);
    assertNull(service.get());
}
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

DefaultBackupOutput (io.atomix.primitive.service.impl.DefaultBackupOutput)18 HeapBuffer (io.atomix.storage.buffer.HeapBuffer)17 DefaultBackupInput (io.atomix.primitive.service.impl.DefaultBackupInput)16 Buffer (io.atomix.storage.buffer.Buffer)16 Test (org.junit.Test)16 Session (io.atomix.primitive.session.Session)13 ServiceContext (io.atomix.primitive.service.ServiceContext)12 WallClock (io.atomix.utils.time.WallClock)11 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 PrimaryBackupSession (io.atomix.protocols.backup.impl.PrimaryBackupSession)1 RaftSession (io.atomix.protocols.raft.session.RaftSession)1 WallClockTimestamp (io.atomix.utils.time.WallClockTimestamp)1 Field (java.lang.reflect.Field)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1