Search in sources :

Example 1 with ReplicatedSubscriptionsSnapshot

use of org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshot in project pulsar by apache.

the class AbstractBaseDispatcher method processReplicatedSubscriptionSnapshot.

private void processReplicatedSubscriptionSnapshot(PositionImpl pos, ByteBuf headersAndPayload) {
    // Remove the protobuf headers
    Commands.skipMessageMetadata(headersAndPayload);
    try {
        ReplicatedSubscriptionsSnapshot snapshot = Markers.parseReplicatedSubscriptionsSnapshot(headersAndPayload);
        subscription.processReplicatedSubscriptionSnapshot(snapshot);
    } catch (Throwable t) {
        log.warn("Failed to process replicated subscription snapshot at {} -- {}", pos, t.getMessage(), t);
        return;
    }
}
Also used : ReplicatedSubscriptionsSnapshot(org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshot)

Example 2 with ReplicatedSubscriptionsSnapshot

use of org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshot in project pulsar by apache.

the class ReplicatedSubscriptionSnapshotCacheTest method testSnapshotCachePruning.

@Test
public void testSnapshotCachePruning() {
    ReplicatedSubscriptionSnapshotCache cache = new ReplicatedSubscriptionSnapshotCache("my-subscription", 3);
    ReplicatedSubscriptionsSnapshot s1 = new ReplicatedSubscriptionsSnapshot().setSnapshotId("snapshot-1");
    s1.setLocalMessageId().setLedgerId(1).setEntryId(1);
    ReplicatedSubscriptionsSnapshot s2 = new ReplicatedSubscriptionsSnapshot().setSnapshotId("snapshot-2");
    s2.setLocalMessageId().setLedgerId(2).setEntryId(2);
    ReplicatedSubscriptionsSnapshot s3 = new ReplicatedSubscriptionsSnapshot().setSnapshotId("snapshot-3");
    s3.setLocalMessageId().setLedgerId(3).setEntryId(3);
    ReplicatedSubscriptionsSnapshot s4 = new ReplicatedSubscriptionsSnapshot().setSnapshotId("snapshot-4");
    s4.setLocalMessageId().setLedgerId(4).setEntryId(4);
    cache.addNewSnapshot(s1);
    cache.addNewSnapshot(s2);
    cache.addNewSnapshot(s3);
    cache.addNewSnapshot(s4);
    // Snapshot-1 was already pruned
    assertNull(cache.advancedMarkDeletePosition(new PositionImpl(1, 1)));
    ReplicatedSubscriptionsSnapshot snapshot = cache.advancedMarkDeletePosition(new PositionImpl(2, 2));
    assertNotNull(snapshot);
    assertEquals(snapshot.getSnapshotId(), "snapshot-2");
    snapshot = cache.advancedMarkDeletePosition(new PositionImpl(5, 5));
    assertNotNull(snapshot);
    assertEquals(snapshot.getSnapshotId(), "snapshot-4");
}
Also used : PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) ReplicatedSubscriptionsSnapshot(org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshot) Test(org.testng.annotations.Test)

Example 3 with ReplicatedSubscriptionsSnapshot

use of org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshot in project pulsar by apache.

the class ReplicatedSubscriptionsSnapshotBuilderTest method testBuildSnapshotWith3Clusters.

@Test
public void testBuildSnapshotWith3Clusters() throws Exception {
    List<String> remoteClusters = Arrays.asList("b", "c");
    ReplicatedSubscriptionsSnapshotBuilder builder = new ReplicatedSubscriptionsSnapshotBuilder(controller, remoteClusters, conf, clock);
    assertTrue(markers.isEmpty());
    builder.start();
    // Should have sent out a marker to initiate the snapshot
    assertEquals(markers.size(), 1);
    ReplicatedSubscriptionsSnapshotRequest request = Markers.parseReplicatedSubscriptionsSnapshotRequest(markers.remove(0));
    assertEquals(request.getSourceCluster(), localCluster);
    // Simulate the responses coming back
    ReplicatedSubscriptionsSnapshotResponse response1 = new ReplicatedSubscriptionsSnapshotResponse().setSnapshotId("snapshot-1");
    response1.setCluster().setCluster("b").setMessageId().setLedgerId(11).setEntryId(11);
    builder.receivedSnapshotResponse(new PositionImpl(1, 1), response1);
    // No markers should be sent out
    assertTrue(markers.isEmpty());
    ReplicatedSubscriptionsSnapshotResponse response2 = new ReplicatedSubscriptionsSnapshotResponse().setSnapshotId("snapshot-1");
    response2.setCluster().setCluster("c").setMessageId().setLedgerId(22).setEntryId(22);
    builder.receivedSnapshotResponse(new PositionImpl(2, 2), response2);
    // Since we have 2 remote clusters, a 2nd round of snapshot will be taken
    assertEquals(markers.size(), 1);
    request = Markers.parseReplicatedSubscriptionsSnapshotRequest(markers.remove(0));
    assertEquals(request.getSourceCluster(), localCluster);
    // Responses coming back
    ReplicatedSubscriptionsSnapshotResponse response3 = new ReplicatedSubscriptionsSnapshotResponse().setSnapshotId("snapshot-1");
    response3.setCluster().setCluster("b").setMessageId().setLedgerId(33).setEntryId(33);
    builder.receivedSnapshotResponse(new PositionImpl(3, 3), response3);
    // No markers should be sent out
    assertTrue(markers.isEmpty());
    ReplicatedSubscriptionsSnapshotResponse response4 = new ReplicatedSubscriptionsSnapshotResponse().setSnapshotId("snapshot-1");
    response4.setCluster().setCluster("c").setMessageId().setLedgerId(44).setEntryId(44);
    builder.receivedSnapshotResponse(new PositionImpl(4, 4), response4);
    // At this point the snapshot should be created
    assertEquals(markers.size(), 1);
    ReplicatedSubscriptionsSnapshot snapshot = Markers.parseReplicatedSubscriptionsSnapshot(markers.remove(0));
    assertEquals(snapshot.getClustersCount(), 2);
    assertEquals(snapshot.getClusterAt(0).getCluster(), "b");
    assertEquals(snapshot.getClusterAt(0).getMessageId().getLedgerId(), 11);
    assertEquals(snapshot.getClusterAt(0).getMessageId().getEntryId(), 11);
    assertEquals(snapshot.getClusterAt(1).getCluster(), "c");
    assertEquals(snapshot.getClusterAt(1).getMessageId().getLedgerId(), 22);
    assertEquals(snapshot.getClusterAt(1).getMessageId().getEntryId(), 22);
    assertEquals(snapshot.getLocalMessageId().getLedgerId(), 4);
    assertEquals(snapshot.getLocalMessageId().getEntryId(), 4);
}
Also used : ReplicatedSubscriptionsSnapshotResponse(org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshotResponse) ReplicatedSubscriptionsSnapshotRequest(org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshotRequest) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) ReplicatedSubscriptionsSnapshot(org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshot) Test(org.testng.annotations.Test)

Example 4 with ReplicatedSubscriptionsSnapshot

use of org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshot in project pulsar by apache.

the class MarkersTest method testSnapshot.

@Test
public void testSnapshot() throws Exception {
    Map<String, MarkersMessageIdData> clusters = new TreeMap<>();
    clusters.put("us-east", new MarkersMessageIdData().setLedgerId(10).setEntryId(11));
    clusters.put("us-cent", new MarkersMessageIdData().setLedgerId(20).setEntryId(21));
    ByteBuf buf = Markers.newReplicatedSubscriptionsSnapshot("sid", "us-west", 5, 7, clusters);
    MessageMetadata msgMetadata = Commands.parseMessageMetadata(buf);
    assertEquals(msgMetadata.getReplicateTosCount(), 1);
    assertEquals(msgMetadata.getReplicateToAt(0), "us-west");
    ReplicatedSubscriptionsSnapshot snapshot = Markers.parseReplicatedSubscriptionsSnapshot(buf);
    assertEquals(snapshot.getSnapshotId(), "sid");
    assertEquals(snapshot.getLocalMessageId().getLedgerId(), 5);
    assertEquals(snapshot.getLocalMessageId().getEntryId(), 7);
    assertEquals(snapshot.getClustersCount(), 2);
    assertEquals(snapshot.getClusterAt(0).getCluster(), "us-cent");
    assertEquals(snapshot.getClusterAt(0).getMessageId().getLedgerId(), 20);
    assertEquals(snapshot.getClusterAt(0).getMessageId().getEntryId(), 21);
    assertEquals(snapshot.getClusterAt(1).getCluster(), "us-east");
    assertEquals(snapshot.getClusterAt(1).getMessageId().getLedgerId(), 10);
    assertEquals(snapshot.getClusterAt(1).getMessageId().getEntryId(), 11);
}
Also used : MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) MarkersMessageIdData(org.apache.pulsar.common.api.proto.MarkersMessageIdData) TreeMap(java.util.TreeMap) ByteBuf(io.netty.buffer.ByteBuf) ReplicatedSubscriptionsSnapshot(org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshot) Test(org.testng.annotations.Test)

Example 5 with ReplicatedSubscriptionsSnapshot

use of org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshot in project pulsar by apache.

the class Markers method parseReplicatedSubscriptionsSnapshot.

public static ReplicatedSubscriptionsSnapshot parseReplicatedSubscriptionsSnapshot(ByteBuf payload) throws IOException {
    ReplicatedSubscriptionsSnapshot snapshot = LOCAL_SNAPSHOT.get();
    snapshot.parseFrom(payload, payload.readableBytes());
    return snapshot;
}
Also used : ReplicatedSubscriptionsSnapshot(org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshot)

Aggregations

ReplicatedSubscriptionsSnapshot (org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshot)9 Test (org.testng.annotations.Test)5 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)4 ByteBuf (io.netty.buffer.ByteBuf)2 ReplicatedSubscriptionsSnapshotRequest (org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshotRequest)2 ReplicatedSubscriptionsSnapshotResponse (org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshotResponse)2 TreeMap (java.util.TreeMap)1 SneakyThrows (lombok.SneakyThrows)1 Position (org.apache.bookkeeper.mledger.Position)1 ManagedCursorImpl (org.apache.bookkeeper.mledger.impl.ManagedCursorImpl)1 SubscriptionInvalidCursorPosition (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionInvalidCursorPosition)1 Consumer (org.apache.pulsar.broker.service.Consumer)1 MarkersMessageIdData (org.apache.pulsar.common.api.proto.MarkersMessageIdData)1 MessageMetadata (org.apache.pulsar.common.api.proto.MessageMetadata)1