Search in sources :

Example 6 with DefaultBackupInput

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

the class DefaultAtomicCounterMapServiceTest method testSnapshot.

@Test
public void testSnapshot() throws Exception {
    DefaultAtomicCounterMapService service = new DefaultAtomicCounterMapService();
    service.put("foo", 1);
    Buffer buffer = HeapBuffer.allocate();
    service.backup(new DefaultBackupOutput(buffer, service.serializer()));
    service = new DefaultAtomicCounterMapService();
    service.restore(new DefaultBackupInput(buffer.flip(), service.serializer()));
    long value = service.get("foo");
    assertEquals(1, value);
}
Also used : Buffer(io.atomix.storage.buffer.Buffer) HeapBuffer(io.atomix.storage.buffer.HeapBuffer) DefaultBackupOutput(io.atomix.primitive.service.impl.DefaultBackupOutput) DefaultBackupInput(io.atomix.primitive.service.impl.DefaultBackupInput) Test(org.junit.Test)

Example 7 with DefaultBackupInput

use of io.atomix.primitive.service.impl.DefaultBackupInput 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 8 with DefaultBackupInput

use of io.atomix.primitive.service.impl.DefaultBackupInput 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 9 with DefaultBackupInput

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

the class RaftServiceContext method installSnapshot.

/**
 * Installs a snapshot.
 */
public void installSnapshot(SnapshotReader reader) {
    log.debug("Installing snapshot {}", reader.snapshot().index());
    // Skip the service ID
    reader.skip(Bytes.LONG);
    PrimitiveType primitiveType;
    try {
        primitiveType = raft.getPrimitiveTypes().getPrimitiveType(reader.readString());
    } catch (ConfigurationException e) {
        log.error(e.getMessage(), e);
        return;
    }
    String serviceName = reader.readString();
    currentIndex = reader.readLong();
    currentTimestamp = reader.readLong();
    timestampDelta = reader.readLong();
    int sessionCount = reader.readInt();
    for (int i = 0; i < sessionCount; i++) {
        SessionId sessionId = SessionId.from(reader.readLong());
        MemberId node = MemberId.from(reader.readString());
        ReadConsistency readConsistency = ReadConsistency.valueOf(reader.readString());
        long minTimeout = reader.readLong();
        long maxTimeout = reader.readLong();
        long sessionTimestamp = reader.readLong();
        // Only create a new session if one does not already exist. This is necessary to ensure only a single session
        // is ever opened and exposed to the state machine.
        RaftSession session = raft.getSessions().addSession(new RaftSession(sessionId, node, serviceName, primitiveType, readConsistency, minTimeout, maxTimeout, sessionTimestamp, service.serializer(), this, raft, threadContextFactory));
        session.setRequestSequence(reader.readLong());
        session.setCommandSequence(reader.readLong());
        session.setEventIndex(reader.readLong());
        session.setLastCompleted(reader.readLong());
        session.setLastApplied(reader.snapshot().index());
        session.setLastUpdated(sessionTimestamp);
        session.open();
        service.register(sessions.addSession(session));
    }
    service.restore(new DefaultBackupInput(reader, service.serializer()));
}
Also used : ReadConsistency(io.atomix.protocols.raft.ReadConsistency) MemberId(io.atomix.cluster.MemberId) RaftSession(io.atomix.protocols.raft.session.RaftSession) ConfigurationException(io.atomix.utils.config.ConfigurationException) PrimitiveType(io.atomix.primitive.PrimitiveType) SessionId(io.atomix.primitive.session.SessionId) DefaultBackupInput(io.atomix.primitive.service.impl.DefaultBackupInput)

Example 10 with DefaultBackupInput

use of io.atomix.primitive.service.impl.DefaultBackupInput 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

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