Search in sources :

Example 1 with Buffer

use of io.atomix.storage.buffer.Buffer in project atomix by atomix.

the class FileSnapshot method openWriter.

@Override
public synchronized SnapshotWriter openWriter() {
    checkWriter();
    Buffer buffer = FileBuffer.allocate(file.file(), SnapshotDescriptor.BYTES);
    descriptor.copyTo(buffer);
    int length = buffer.position(SnapshotDescriptor.BYTES).readInt();
    return openWriter(new SnapshotWriter(buffer.skip(length).mark(), this), descriptor);
}
Also used : Buffer(io.atomix.storage.buffer.Buffer) FileBuffer(io.atomix.storage.buffer.FileBuffer)

Example 2 with Buffer

use of io.atomix.storage.buffer.Buffer in project atomix by atomix.

the class LeaderElectionServiceTest method testSnapshot.

@Test
public void testSnapshot() throws Exception {
    ServiceContext context = mock(ServiceContext.class);
    when(context.serviceType()).thenReturn(PrimitiveTypes.leaderElection());
    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));
    LeaderElectionService service = new LeaderElectionService();
    service.init(context);
    byte[] id = "a".getBytes();
    service.run(new DefaultCommit<>(2, RUN, new Run(id), session, System.currentTimeMillis()));
    Buffer buffer = HeapBuffer.allocate();
    service.backup(buffer);
    service = new LeaderElectionService();
    service.init(context);
    service.restore(buffer.flip());
    Leadership<byte[]> value = service.getLeadership();
    assertNotNull(value);
    assertArrayEquals(value.leader().id(), 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) LeaderElectionService(io.atomix.core.election.impl.LeaderElectionService) Run(io.atomix.core.election.impl.LeaderElectionOperations.Run) Session(io.atomix.primitive.session.Session) Test(org.junit.Test)

Example 3 with Buffer

use of io.atomix.storage.buffer.Buffer in project atomix by atomix.

the class AtomicCounterMapServiceTest method testSnapshot.

@Test
public void testSnapshot() throws Exception {
    AtomicCounterMapService service = new AtomicCounterMapService();
    service.put(new DefaultCommit<>(2, PUT, new Put("foo", 1), mock(Session.class), System.currentTimeMillis()));
    Buffer buffer = HeapBuffer.allocate();
    service.backup(buffer);
    service = new AtomicCounterMapService();
    service.restore(buffer.flip());
    long value = service.get(new DefaultCommit<>(2, GET, new Get("foo"), mock(Session.class), System.currentTimeMillis()));
    assertEquals(1, value);
}
Also used : Buffer(io.atomix.storage.buffer.Buffer) HeapBuffer(io.atomix.storage.buffer.HeapBuffer) Get(io.atomix.core.map.impl.AtomicCounterMapOperations.Get) AtomicCounterMapService(io.atomix.core.map.impl.AtomicCounterMapService) Put(io.atomix.core.map.impl.AtomicCounterMapOperations.Put) Test(org.junit.Test)

Example 4 with Buffer

use of io.atomix.storage.buffer.Buffer in project atomix by atomix.

the class DocumentTreeServiceTest method testSnapshot.

private void testSnapshot(Ordering ordering) throws Exception {
    DocumentTreeService service = new DocumentTreeService(ordering);
    service.update(new DefaultCommit<>(2, UPDATE, new Update(DocumentPath.from("root|foo"), Optional.of("Hello world!".getBytes()), Match.any(), Match.ifNull()), mock(Session.class), System.currentTimeMillis()));
    Buffer buffer = HeapBuffer.allocate();
    service.backup(buffer);
    service = new DocumentTreeService(ordering);
    service.restore(buffer.flip());
    Versioned<byte[]> value = service.get(new DefaultCommit<>(2, GET, new Get(DocumentPath.from("root|foo")), mock(Session.class), System.currentTimeMillis()));
    assertNotNull(value);
    assertArrayEquals("Hello world!".getBytes(), value.value());
}
Also used : Buffer(io.atomix.storage.buffer.Buffer) HeapBuffer(io.atomix.storage.buffer.HeapBuffer) DocumentTreeService(io.atomix.core.tree.impl.DocumentTreeService) Get(io.atomix.core.tree.impl.DocumentTreeOperations.Get) Update(io.atomix.core.tree.impl.DocumentTreeOperations.Update)

Example 5 with Buffer

use of io.atomix.storage.buffer.Buffer in project atomix by atomix.

the class ConsistentMapServiceTest method testSnapshot.

@Test
@SuppressWarnings("unchecked")
public void testSnapshot() throws Exception {
    ConsistentMapService service = new TestConsistentMapService();
    service.put(new DefaultCommit<>(2, PUT, new Put("foo", "Hello world!".getBytes(), 1000), mock(Session.class), System.currentTimeMillis()));
    Buffer buffer = HeapBuffer.allocate();
    service.backup(buffer);
    service = new TestConsistentMapService();
    service.restore(buffer.flip());
    Versioned<byte[]> value = service.get(new DefaultCommit<>(2, GET, new Get("foo"), mock(Session.class), System.currentTimeMillis()));
    assertNotNull(value);
    assertArrayEquals("Hello world!".getBytes(), value.value());
    assertNotNull(service.entries().get("foo").timer);
}
Also used : Buffer(io.atomix.storage.buffer.Buffer) HeapBuffer(io.atomix.storage.buffer.HeapBuffer) Get(io.atomix.core.map.impl.ConsistentMapOperations.Get) Put(io.atomix.core.map.impl.ConsistentMapOperations.Put) Test(org.junit.Test)

Aggregations

Buffer (io.atomix.storage.buffer.Buffer)17 HeapBuffer (io.atomix.storage.buffer.HeapBuffer)14 FileBuffer (io.atomix.storage.buffer.FileBuffer)9 Test (org.junit.Test)8 MappedBuffer (io.atomix.storage.buffer.MappedBuffer)6 File (java.io.File)6 ServiceContext (io.atomix.primitive.service.ServiceContext)2 Session (io.atomix.primitive.session.Session)2 Set (io.atomix.core.counter.impl.AtomicCounterOperations.Set)1 AtomicCounterService (io.atomix.core.counter.impl.AtomicCounterService)1 Run (io.atomix.core.election.impl.LeaderElectionOperations.Run)1 LeaderElectionService (io.atomix.core.election.impl.LeaderElectionService)1 Get (io.atomix.core.map.impl.AtomicCounterMapOperations.Get)1 Put (io.atomix.core.map.impl.AtomicCounterMapOperations.Put)1 AtomicCounterMapService (io.atomix.core.map.impl.AtomicCounterMapService)1 Get (io.atomix.core.map.impl.ConsistentMapOperations.Get)1 Put (io.atomix.core.map.impl.ConsistentMapOperations.Put)1 Get (io.atomix.core.multimap.impl.ConsistentSetMultimapOperations.Get)1 Put (io.atomix.core.multimap.impl.ConsistentSetMultimapOperations.Put)1 ConsistentSetMultimapService (io.atomix.core.multimap.impl.ConsistentSetMultimapService)1