Search in sources :

Example 1 with Revision

use of io.pravega.client.state.Revision in project pravega by pravega.

the class StateSynchronizerImpl method conditionallyWrite.

private void conditionallyWrite(Function<StateT, UpdateOrInit<StateT>> generator) {
    while (true) {
        StateT state = getState();
        if (state == null) {
            fetchUpdates();
            state = getState();
            if (state == null) {
                throw new IllegalStateException("Write was called before the state was initialized.");
            }
        }
        log.trace("Conditionally Writing {} ", state);
        Revision revision = state.getRevision();
        UpdateOrInit<StateT> toWrite = generator.apply(state);
        if (toWrite == null) {
            log.debug("Conditional write to segment {} completed as there is nothing to update after revision {}", segment, revision);
            break;
        }
        Revision newRevision = client.writeConditionally(revision, toWrite);
        log.trace("Conditional write returned {} ", newRevision);
        if (newRevision == null) {
            fetchUpdates();
        } else {
            if (!toWrite.isInit()) {
                applyUpdates(newRevision, toWrite.getUpdates());
            }
            log.debug("Conditional write to segment {} completed with revision {}", segment, newRevision);
            break;
        }
    }
}
Also used : Revision(io.pravega.client.state.Revision)

Example 2 with Revision

use of io.pravega.client.state.Revision in project pravega by pravega.

the class StateSynchronizerImpl method bytesWrittenSinceCompaction.

@Override
public long bytesWrittenSinceCompaction() {
    Revision mark = client.getMark();
    StateT state = getState();
    long compaction = (mark == null) ? 0 : mark.asImpl().getOffsetInSegment();
    long current = (state == null) ? 0 : state.getRevision().asImpl().getOffsetInSegment();
    return Math.max(0, current - compaction);
}
Also used : Revision(io.pravega.client.state.Revision)

Example 3 with Revision

use of io.pravega.client.state.Revision in project pravega by pravega.

the class StateSynchronizerImpl method getRevisionToReadFrom.

private Revision getRevisionToReadFrom(boolean useState) {
    StateT state = getState();
    Revision revision;
    if (!useState || state == null) {
        revision = client.getMark();
        if (revision == null) {
            revision = client.fetchOldestRevision();
        }
    } else {
        revision = state.getRevision();
    }
    return revision;
}
Also used : Revision(io.pravega.client.state.Revision)

Example 4 with Revision

use of io.pravega.client.state.Revision in project pravega by pravega.

the class RevisionedStreamClientTest method testMark.

@Test
public void testMark() {
    String scope = "scope";
    String stream = "stream";
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, false);
    createScopeAndStream(scope, stream, controller);
    MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
    @Cleanup SynchronizerClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory, streamFactory);
    SynchronizerConfig config = SynchronizerConfig.builder().build();
    @Cleanup RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, new JavaSerializer<>(), config);
    client.writeUnconditionally("a");
    Revision ra = client.fetchLatestRevision();
    client.writeUnconditionally("b");
    Revision rb = client.fetchLatestRevision();
    client.writeUnconditionally("c");
    Revision rc = client.fetchLatestRevision();
    assertTrue(client.compareAndSetMark(null, ra));
    assertEquals(ra, client.getMark());
    assertTrue(client.compareAndSetMark(ra, rb));
    assertEquals(rb, client.getMark());
    assertFalse(client.compareAndSetMark(ra, rc));
    assertEquals(rb, client.getMark());
    assertTrue(client.compareAndSetMark(rb, rc));
    assertEquals(rc, client.getMark());
    assertTrue(client.compareAndSetMark(rc, ra));
    assertEquals(ra, client.getMark());
    assertTrue(client.compareAndSetMark(ra, null));
    assertEquals(null, client.getMark());
}
Also used : SynchronizerClientFactory(io.pravega.client.SynchronizerClientFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) Revision(io.pravega.client.state.Revision) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) Cleanup(lombok.Cleanup) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Test(org.junit.Test)

Example 5 with Revision

use of io.pravega.client.state.Revision in project pravega by pravega.

the class SerializationTest method testRevision.

@Test
public void testRevision() throws IOException, ClassNotFoundException {
    RevisionImpl revision = new RevisionImpl(Segment.fromScopedName("Foo/Bar/1"), 2, 3);
    String string = revision.toString();
    Revision revision2 = Revision.fromString(string);
    assertEquals(revision, revision2);
    assertEquals(Segment.fromScopedName("Foo/Bar/1"), revision2.asImpl().getSegment());
    assertEquals(2, revision2.asImpl().getOffsetInSegment());
    assertEquals(3, revision2.asImpl().getEventAtOffset());
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    @Cleanup ObjectOutputStream oout = new ObjectOutputStream(bout);
    oout.writeObject(revision);
    byte[] byteArray = bout.toByteArray();
    ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(byteArray));
    Object revision3 = oin.readObject();
    assertEquals(revision, revision3);
    assertEquals(revision2, revision3);
}
Also used : Revision(io.pravega.client.state.Revision) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Cleanup(lombok.Cleanup) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

Revision (io.pravega.client.state.Revision)16 Test (org.junit.Test)10 Cleanup (lombok.Cleanup)9 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)7 Entry (java.util.Map.Entry)7 SynchronizerConfig (io.pravega.client.state.SynchronizerConfig)6 SynchronizerClientFactory (io.pravega.client.SynchronizerClientFactory)5 Segment (io.pravega.client.segment.impl.Segment)5 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)5 MockController (io.pravega.client.stream.mock.MockController)5 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)5 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)5 TruncatedDataException (io.pravega.client.stream.TruncatedDataException)3 lombok.val (lombok.val)3 ClientConfig (io.pravega.client.ClientConfig)2 Controller (io.pravega.client.control.impl.Controller)2 RevisionedStreamClient (io.pravega.client.state.RevisionedStreamClient)2 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)2 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)2 JavaSerializer (io.pravega.client.stream.impl.JavaSerializer)2