use of io.pravega.client.state.Revision in project pravega by pravega.
the class StateSynchronizerImpl method compact.
@Override
public void compact(Function<StateT, InitialUpdate<StateT>> compactor) {
AtomicReference<Revision> compactedVersion = new AtomicReference<Revision>(null);
conditionallyWrite(state -> {
InitialUpdate<StateT> init = compactor.apply(state);
if (init == null) {
compactedVersion.set(null);
return null;
} else {
compactedVersion.set(state.getRevision());
return new UpdateOrInit<>(init);
}
});
Revision newMark = compactedVersion.get();
if (newMark != null) {
Revision oldMark = client.getMark();
if (oldMark == null || oldMark.compareTo(newMark) < 0) {
client.compareAndSetMark(oldMark, newMark);
}
}
}
use of io.pravega.client.state.Revision in project pravega by pravega.
the class StateSynchronizerImpl method fetchUpdates.
@Override
public void fetchUpdates() {
Revision revision = getRevisionToReadFrom(true);
log.trace("Fetching updates after {} ", revision);
try {
val iter = client.readFrom(revision);
while (iter.hasNext()) {
Entry<Revision, UpdateOrInit<StateT>> entry = iter.next();
log.trace("Found entry {} ", entry.getValue());
if (entry.getValue().isInit()) {
InitialUpdate<StateT> init = entry.getValue().getInit();
if (isNewer(entry.getKey())) {
updateCurrentState(init.create(segment.getScopedStreamName(), entry.getKey()));
}
} else {
applyUpdates(entry.getKey().asImpl(), entry.getValue().getUpdates());
}
}
} catch (TruncatedDataException e) {
log.warn("{} encountered truncation on segment {}", this, segment);
handleTruncation();
}
}
use of io.pravega.client.state.Revision in project pravega by pravega.
the class StateSynchronizerImpl method handleTruncation.
private void handleTruncation() {
log.info(this + " Encountered truncation");
Revision revision = getRevisionToReadFrom(false);
log.trace("Fetching updates after {} ", revision);
boolean foundInit = false;
val iter = client.readFrom(revision);
while (!foundInit && iter.hasNext()) {
Entry<Revision, UpdateOrInit<StateT>> entry = iter.next();
if (entry.getValue().isInit()) {
log.trace("Found entry {} ", entry.getValue());
InitialUpdate<StateT> init = entry.getValue().getInit();
if (isNewer(entry.getKey())) {
updateCurrentState(init.create(segment.getScopedStreamName(), entry.getKey()));
foundInit = true;
}
}
}
if (!foundInit) {
throw new IllegalStateException("Data was truncated but there is not init after the truncation point.");
}
fetchUpdates();
}
use of io.pravega.client.state.Revision in project pravega by pravega.
the class RevisionedStreamClientTest method testConditionalWrite.
@Test
public void testConditionalWrite() {
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);
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
@Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
SynchronizerConfig config = SynchronizerConfig.builder().build();
RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, new JavaSerializer<>(), config);
client.writeUnconditionally("a");
Revision revision = client.fetchLatestRevision();
Revision newRevision = client.writeConditionally(revision, "b");
assertNotNull(newRevision);
assertTrue(newRevision.compareTo(revision) > 0);
assertEquals(newRevision, client.fetchLatestRevision());
Revision failed = client.writeConditionally(revision, "fail");
assertNull(failed);
assertEquals(newRevision, client.fetchLatestRevision());
Iterator<Entry<Revision, String>> iter = client.readFrom(revision);
assertTrue(iter.hasNext());
Entry<Revision, String> entry = iter.next();
assertEquals(newRevision, entry.getKey());
assertEquals("b", entry.getValue());
assertFalse(iter.hasNext());
}
use of io.pravega.client.state.Revision in project pravega by pravega.
the class RevisionedStreamClientTest method testSegmentTruncation.
@Test
public void testSegmentTruncation() {
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);
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
@Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
SynchronizerConfig config = SynchronizerConfig.builder().build();
@Cleanup RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, new JavaSerializer<>(), config);
Revision r0 = client.fetchLatestRevision();
client.writeUnconditionally("a");
Revision ra = client.fetchLatestRevision();
client.writeUnconditionally("b");
Revision rb = client.fetchLatestRevision();
client.writeUnconditionally("c");
Revision rc = client.fetchLatestRevision();
assertEquals(r0, client.fetchOldestRevision());
client.truncateToRevision(r0);
assertEquals(r0, client.fetchOldestRevision());
client.truncateToRevision(ra);
assertEquals(ra, client.fetchOldestRevision());
client.truncateToRevision(r0);
assertEquals(ra, client.fetchOldestRevision());
AssertExtensions.assertThrows(TruncatedDataException.class, () -> client.readFrom(r0));
Iterator<Entry<Revision, String>> iterA = client.readFrom(ra);
assertTrue(iterA.hasNext());
Iterator<Entry<Revision, String>> iterB = client.readFrom(ra);
assertTrue(iterB.hasNext());
assertEquals("b", iterA.next().getValue());
assertEquals("b", iterB.next().getValue());
client.truncateToRevision(rb);
assertTrue(iterA.hasNext());
assertEquals("c", iterA.next().getValue());
client.truncateToRevision(rc);
assertFalse(iterA.hasNext());
assertTrue(iterB.hasNext());
AssertExtensions.assertThrows(TruncatedDataException.class, () -> iterB.next());
}
Aggregations