Search in sources :

Example 1 with ResolvedBucketConfig

use of com.couchbase.connector.dcp.ResolvedBucketConfig in project couchbase-elasticsearch-connector by couchbase.

the class CheckpointBackup method backup.

public static void backup(ConnectorConfig config, File outputFile) throws IOException {
    final Bucket bucket = CouchbaseHelper.openMetadataBucket(config.couchbase(), config.trustStore());
    final ResolvedBucketConfig bucketConfig = getBucketConfig(config.couchbase(), bucket);
    // don't care bucketConfig.uuid();
    final String bucketUuid = "";
    final CheckpointDao checkpointDao = new CouchbaseCheckpointDao(getMetadataCollection(bucket, config.couchbase()), config.group().name());
    final int numVbuckets = bucketConfig.numberOfPartitions();
    final Set<Integer> vbuckets = IntStream.range(0, numVbuckets).boxed().collect(toSet());
    final Map<Integer, Checkpoint> checkpoints = checkpointDao.load(bucketUuid, vbuckets);
    final Map<String, Object> output = new LinkedHashMap<>();
    output.put("formatVersion", 1);
    output.put("bucketUuid", bucketConfig.uuid());
    output.put("vbuckets", checkpoints);
    atomicWrite(outputFile, tempFile -> {
        try (FileOutputStream out = new FileOutputStream(tempFile)) {
            new ObjectMapper().writeValue(out, output);
        }
    });
    System.out.println("Wrote checkpoint for connector '" + config.group().name() + "' to file " + outputFile.getAbsolutePath());
}
Also used : CouchbaseCheckpointDao(com.couchbase.connector.dcp.CouchbaseCheckpointDao) Checkpoint(com.couchbase.connector.dcp.Checkpoint) LinkedHashMap(java.util.LinkedHashMap) Checkpoint(com.couchbase.connector.dcp.Checkpoint) CouchbaseCheckpointDao(com.couchbase.connector.dcp.CouchbaseCheckpointDao) CheckpointDao(com.couchbase.connector.dcp.CheckpointDao) Bucket(com.couchbase.client.java.Bucket) FileOutputStream(java.io.FileOutputStream) ResolvedBucketConfig(com.couchbase.connector.dcp.ResolvedBucketConfig) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with ResolvedBucketConfig

use of com.couchbase.connector.dcp.ResolvedBucketConfig in project couchbase-elasticsearch-connector by couchbase.

the class CheckpointRestore method restore.

public static void restore(ConnectorConfig config, File inputFile) throws IOException {
    final Bucket bucket = CouchbaseHelper.openMetadataBucket(config.couchbase(), config.trustStore());
    final ResolvedBucketConfig bucketConfig = getBucketConfig(config.couchbase(), bucket);
    final CheckpointDao checkpointDao = new CouchbaseCheckpointDao(getMetadataCollection(bucket, config.couchbase()), config.group().name());
    final ObjectMapper mapper = new ObjectMapper();
    try (InputStream is = new FileInputStream(inputFile)) {
        final JsonNode json = mapper.readTree(is);
        final int formatVersion = json.path("formatVersion").intValue();
        final String bucketUuid = json.path("bucketUuid").asText();
        if (formatVersion != 1) {
            throw new IllegalArgumentException("Unrecognized checkpoint format version: " + formatVersion);
        }
        if (!bucketUuid.equals(bucketConfig.uuid())) {
            throw new IllegalArgumentException("Bucket UUID mismatch; checkpoint is from a bucket with UUID " + bucketUuid + " but this bucket has UUID " + bucketConfig.uuid());
        }
        final Map<Integer, Checkpoint> checkpoints = mapper.convertValue(json.get("vbuckets"), new TypeReference<Map<Integer, Checkpoint>>() {
        });
        if (checkpoints.size() != bucketConfig.numberOfPartitions()) {
            throw new IllegalArgumentException("Bucket has " + bucketConfig.numberOfPartitions() + " vbuckets but the checkpoint file has " + checkpoints.size() + " -- is it from a different operating system (for example, macOS vs Linux)?");
        }
        checkpointDao.save("", checkpoints);
    }
    System.out.println("Restored checkpoint for connector '" + config.group().name() + "' from file " + inputFile);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonNode(com.fasterxml.jackson.databind.JsonNode) CouchbaseCheckpointDao(com.couchbase.connector.dcp.CouchbaseCheckpointDao) FileInputStream(java.io.FileInputStream) Checkpoint(com.couchbase.connector.dcp.Checkpoint) Checkpoint(com.couchbase.connector.dcp.Checkpoint) CouchbaseCheckpointDao(com.couchbase.connector.dcp.CouchbaseCheckpointDao) CheckpointDao(com.couchbase.connector.dcp.CheckpointDao) Bucket(com.couchbase.client.java.Bucket) ResolvedBucketConfig(com.couchbase.connector.dcp.ResolvedBucketConfig) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with ResolvedBucketConfig

use of com.couchbase.connector.dcp.ResolvedBucketConfig in project couchbase-elasticsearch-connector by couchbase.

the class CheckpointClear method run.

private static void run(ConnectorConfig config, boolean catchUp) throws IOException {
    final ClusterEnvironment env = environmentBuilder(config.couchbase(), config.trustStore()).build();
    final Cluster cluster = createCluster(config.couchbase(), env);
    try {
        final Bucket metadataBucket = CouchbaseHelper.waitForBucket(cluster, config.couchbase().metadataBucket());
        final Collection metadataCollection = CouchbaseHelper.getMetadataCollection(metadataBucket, config.couchbase());
        final ResolvedBucketConfig bucketConfig = getBucketConfig(config.couchbase(), metadataBucket);
        final Set<SeedNode> kvNodes = getKvNodes(config.couchbase(), metadataBucket);
        final CheckpointDao checkpointDao = new CouchbaseCheckpointDao(metadataCollection, config.group().name());
        if (catchUp) {
            setCheckpointToNow(config, kvNodes, checkpointDao);
            System.out.println("Set checkpoint for connector '" + config.group().name() + "' to match current state of Couchbase bucket.");
        } else {
            final int numVbuckets = bucketConfig.numberOfPartitions();
            final Set<Integer> vbuckets = IntStream.range(0, numVbuckets).boxed().collect(toSet());
            checkpointDao.clear(bucketConfig.uuid(), vbuckets);
            System.out.println("Cleared checkpoint for connector '" + config.group().name() + "'.");
        }
    } finally {
        cluster.disconnect();
        env.shutdown();
    }
}
Also used : ClusterEnvironment(com.couchbase.client.java.env.ClusterEnvironment) CheckpointDao(com.couchbase.connector.dcp.CheckpointDao) CouchbaseCheckpointDao(com.couchbase.connector.dcp.CouchbaseCheckpointDao) Bucket(com.couchbase.client.java.Bucket) SeedNode(com.couchbase.client.core.env.SeedNode) ResolvedBucketConfig(com.couchbase.connector.dcp.ResolvedBucketConfig) CouchbaseHelper.createCluster(com.couchbase.connector.dcp.CouchbaseHelper.createCluster) Cluster(com.couchbase.client.java.Cluster) Collection(com.couchbase.client.java.Collection) CouchbaseCheckpointDao(com.couchbase.connector.dcp.CouchbaseCheckpointDao) Checkpoint(com.couchbase.connector.dcp.Checkpoint)

Aggregations

Bucket (com.couchbase.client.java.Bucket)3 Checkpoint (com.couchbase.connector.dcp.Checkpoint)3 CheckpointDao (com.couchbase.connector.dcp.CheckpointDao)3 CouchbaseCheckpointDao (com.couchbase.connector.dcp.CouchbaseCheckpointDao)3 ResolvedBucketConfig (com.couchbase.connector.dcp.ResolvedBucketConfig)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 SeedNode (com.couchbase.client.core.env.SeedNode)1 Cluster (com.couchbase.client.java.Cluster)1 Collection (com.couchbase.client.java.Collection)1 ClusterEnvironment (com.couchbase.client.java.env.ClusterEnvironment)1 CouchbaseHelper.createCluster (com.couchbase.connector.dcp.CouchbaseHelper.createCluster)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1