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;
}
}
}
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);
}
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;
}
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());
}
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);
}
Aggregations