Search in sources :

Example 1 with PartitionState

use of com.couchbase.client.dcp.state.PartitionState in project couchbase-elasticsearch-connector by couchbase.

the class CheckpointClear method setCheckpointToNow.

private static void setCheckpointToNow(ConnectorConfig config, Set<SeedNode> kvNodes, CheckpointDao checkpointDao) throws IOException {
    final Client dcpClient = DcpHelper.newClient(config.group().name(), config.couchbase(), kvNodes, config.trustStore());
    try {
        dcpClient.connect().block();
        final int numPartitions = dcpClient.numPartitions();
        final Set<Integer> allPartitions = new HashSet<>(allPartitions(numPartitions));
        DcpHelper.getCurrentSeqnos(dcpClient, allPartitions);
        final SessionState sessionState = dcpClient.sessionState();
        final Map<Integer, Checkpoint> now = new HashMap<>();
        for (int i = 0; i < allPartitions.size(); i++) {
            PartitionState p = sessionState.get(i);
            final long seqno = p.getStartSeqno();
            now.put(i, new Checkpoint(p.getLastUuid(), seqno, new SnapshotMarker(seqno, seqno)));
        }
        checkpointDao.save("", now);
    } finally {
        dcpClient.disconnect().block();
    }
}
Also used : SessionState(com.couchbase.client.dcp.state.SessionState) Checkpoint(com.couchbase.connector.dcp.Checkpoint) HashMap(java.util.HashMap) PartitionState(com.couchbase.client.dcp.state.PartitionState) SnapshotMarker(com.couchbase.connector.dcp.SnapshotMarker) Client(com.couchbase.client.dcp.Client) Checkpoint(com.couchbase.connector.dcp.Checkpoint) HashSet(java.util.HashSet)

Example 2 with PartitionState

use of com.couchbase.client.dcp.state.PartitionState in project kafka-connect-couchbase by couchbase.

the class CouchbaseReader method restoreSavedOffsets.

private void restoreSavedOffsets() {
    LOGGER.info("Resuming from saved offsets for {} of {} partitions", partitionToSavedSeqno.size(), partitions.size());
    for (Map.Entry<Integer, SeqnoAndVbucketUuid> entry : partitionToSavedSeqno.entrySet()) {
        final int partition = entry.getKey();
        final SeqnoAndVbucketUuid offset = entry.getValue();
        final long savedSeqno = offset.seqno();
        PartitionState ps = client.sessionState().get(partition);
        ps.setStartSeqno(savedSeqno);
        ps.setSnapshot(new SnapshotMarker(savedSeqno, savedSeqno));
        if (offset.vbucketUuid().isPresent()) {
            long vbuuid = offset.vbucketUuid().getAsLong();
            LOGGER.debug("Initializing failover log for partition {} using stored vbuuid {} ", partition, vbuuid);
            // Use seqno -1 (max unsigned) so this synthetic failover log entry will always be pruned
            // if the initial streamOpen request gets a rollback response. If there's no rollback
            // on initial request, then the seqno used here doesn't matter, because the failover log
            // gets reset when the stream is opened.
            ps.setFailoverLog(singletonList(new FailoverLogEntry(-1L, vbuuid)));
        } else {
            // If we get here, we're probably restoring the stream offset from a previous version of the connector
            // which didn't save vbucket UUIDs. Hope the current vbuuid in the failover log is the correct one.
            // CAVEAT: This doesn't always work, and sometimes triggers a rollback to zero.
            LOGGER.warn("No vBucket UUID is associated with stream offset for partition {}." + " This is normal if you're upgrading from connector version 3.4.5 or earlier," + " and should stop happening once the Kafka Connect framework asks the connector" + " to save its offsets (see connector worker config property 'offset.flush.interval.ms').", partition);
        }
        client.sessionState().set(partition, ps);
    }
}
Also used : FailoverLogEntry(com.couchbase.client.dcp.state.FailoverLogEntry) PartitionState(com.couchbase.client.dcp.state.PartitionState) SnapshotMarker(com.couchbase.client.dcp.highlevel.SnapshotMarker) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with PartitionState

use of com.couchbase.client.dcp.state.PartitionState in project couchbase-elasticsearch-connector by couchbase.

the class DcpHelper method initSessionState.

public static void initSessionState(Client dcpClient, CheckpointService checkpointService, Set<Integer> partitions) throws IOException {
    final Map<Integer, Checkpoint> positions = checkpointService.load(partitions);
    final SessionState sessionState = dcpClient.sessionState();
    LOGGER.debug("Initializing DCP session state from checkpoint: {}", positions);
    for (Map.Entry<Integer, Checkpoint> entry : positions.entrySet()) {
        final int partition = entry.getKey();
        final Checkpoint checkpoint = entry.getValue();
        if (checkpoint == null) {
            continue;
        }
        final PartitionState ps = sessionState.get(partition);
        ps.setStartSeqno(checkpoint.getSeqno());
        ps.setSnapshot(new SnapshotMarker(checkpoint.getSnapshot().getStartSeqno(), checkpoint.getSnapshot().getEndSeqno()));
        // Use seqno -1 (max unsigned) so this synthetic failover log entry will always be pruned
        // if the initial streamOpen request gets a rollback response. If there's no rollback
        // on initial request, then the seqno used here doesn't matter, because the failover log
        // gets reset when the stream is opened.
        ps.setFailoverLog(singletonList(new FailoverLogEntry(-1L, checkpoint.getVbuuid())));
        LOGGER.debug("Initialized partition {} state = {}", partition, ps);
    }
}
Also used : SessionState(com.couchbase.client.dcp.state.SessionState) FailoverLogEntry(com.couchbase.client.dcp.state.FailoverLogEntry) PartitionState(com.couchbase.client.dcp.state.PartitionState) SnapshotMarker(com.couchbase.client.dcp.highlevel.SnapshotMarker) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

PartitionState (com.couchbase.client.dcp.state.PartitionState)3 SnapshotMarker (com.couchbase.client.dcp.highlevel.SnapshotMarker)2 FailoverLogEntry (com.couchbase.client.dcp.state.FailoverLogEntry)2 SessionState (com.couchbase.client.dcp.state.SessionState)2 Map (java.util.Map)2 Client (com.couchbase.client.dcp.Client)1 Checkpoint (com.couchbase.connector.dcp.Checkpoint)1 SnapshotMarker (com.couchbase.connector.dcp.SnapshotMarker)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 TreeMap (java.util.TreeMap)1