Search in sources :

Example 6 with Revision

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);
        }
    }
}
Also used : Revision(io.pravega.client.state.Revision) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 7 with Revision

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();
    }
}
Also used : lombok.val(lombok.val) Revision(io.pravega.client.state.Revision) TruncatedDataException(io.pravega.client.stream.TruncatedDataException)

Example 8 with Revision

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();
}
Also used : lombok.val(lombok.val) Revision(io.pravega.client.state.Revision)

Example 9 with Revision

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());
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) ClientFactory(io.pravega.client.ClientFactory) Cleanup(lombok.Cleanup) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) Entry(java.util.Map.Entry) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) Revision(io.pravega.client.state.Revision) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Test(org.junit.Test)

Example 10 with Revision

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());
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) ClientFactory(io.pravega.client.ClientFactory) Cleanup(lombok.Cleanup) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) Entry(java.util.Map.Entry) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) Revision(io.pravega.client.state.Revision) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Test(org.junit.Test)

Aggregations

Revision (io.pravega.client.state.Revision)10 ClientFactory (io.pravega.client.ClientFactory)4 SynchronizerConfig (io.pravega.client.state.SynchronizerConfig)4 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)4 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)4 MockController (io.pravega.client.stream.mock.MockController)4 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)4 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)4 Cleanup (lombok.Cleanup)4 Test (org.junit.Test)4 Entry (java.util.Map.Entry)3 lombok.val (lombok.val)2 TruncatedDataException (io.pravega.client.stream.TruncatedDataException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1