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());
}
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);
}
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();
}
}
Aggregations