Repository navigation
Conversation
Adds the sdks/java/io/couchbase module with ConnectionConfiguration and CouchbaseIO.write(), which upserts each element as a JSON document in batches, with bounded concurrency through the reactive API. Includes unit tests, Testcontainers integration tests against Couchbase Server 8.0.3, and a PreCommit workflow for the module.
Encodes a JsonObject as its JSON string, which is 2 to 3 times smaller than the SerializableCoder Beam would otherwise infer. It will be the default output coder for CouchbaseIO.read().
Adds CouchbaseIO.read(), which runs a SQL++ query through the reactive API and streams the rows. Supports named and positional parameters, scan consistency, a parsing function applied inside the read, an output coder, and an optional redistribute after parsing. The default output is JsonObject, encoded with JsonObjectCoder.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #40648 +/- ##
============================================
- Coverage 60.86% 60.86% -0.01%
+ Complexity 14589 14587 -2
============================================
Files 2614 2614
Lines 267627 267627
Branches 11795 11795
============================================
- Hits 162897 162895 -2
Misses 98498 98498
- Partials 6232 6234 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@kennknowles just checking in on this PR, would appreciate your review whenever you have a chance. Thanks! |
|
@kennknowles I’ve synced with master. Could you please approve the pending CI workflows and review the PR when you have a chance? Thanks! |
|
@kennknowles, a gentle follow-up whenever you have a chance. I’d appreciate your review, or a suggestion for another reviewer if you’re busy. Thank you! |
|
Hi @stankiewicz would you have some time to review this CouchbaseIO PR? I’d appreciate your feedback whenever you have a chance. Thanks! |
|
assign to next reviewer |
|
sure, will look into this! |
| DEVELOCITY_ACCESS_KEY: ${{ secrets.DEVELOCITY_ACCESS_KEY }} | ||
| GRADLE_ENTERPRISE_CACHE_USERNAME: ${{ secrets.GE_CACHE_USERNAME }} | ||
| GRADLE_ENTERPRISE_CACHE_PASSWORD: ${{ secrets.GE_CACHE_PASSWORD }} |
There was a problem hiding this comment.
Removed the obsolete Gradle Enterprise cache settings
| implementation "io.projectreactor:reactor-core:3.6.9" | ||
| implementation "org.reactivestreams:reactive-streams:1.0.4" |
There was a problem hiding this comment.
declare version in BeamModulePlugin.groovy. Centralizing those avoids version drift when couchbase_java_client is upgraded.
There was a problem hiding this comment.
Moved Reactor Core and Reactive Streams dependency declarations into BeamModulePlugin.groovy and updated the module to use the shared references.
|
|
||
| /** Writes to a single-node Couchbase Server started by Testcontainers. */ | ||
| @RunWith(JUnit4.class) | ||
| public class CouchbaseIOWriteTest { |
There was a problem hiding this comment.
should be named IT test.
There was a problem hiding this comment.
Renamed to CouchbaseIOWriteIT and updated CI to run the integration-test task. All 6 write integration tests passed locally against Docker.
|
|
||
| /** Reads from a single-node Couchbase Server started by Testcontainers. */ | ||
| @RunWith(JUnit4.class) | ||
| public class CouchbaseIOReadTest { |
There was a problem hiding this comment.
Should be named IT test.
There was a problem hiding this comment.
Renamed to CouchbaseIOReadIT and updated CI to run the integration-test task. All 9 read integration tests passed locally against Docker.
| Flux.fromIterable(buffer) | ||
| .flatMap(kv -> target.upsert(kv.getKey(), kv.getValue()), maxConcurrency) | ||
| .then() | ||
| .block(); | ||
| buffer.clear(); |
There was a problem hiding this comment.
If two elements in the same batch map to the same document ID (e.g., (id1, v1) then (id1, v2)), Flux.flatMap issues both upserts concurrently with no ordering guarantee, so v1 can overwrite v2. Either document that Write requires unique IDs per bundle/batch, deduplicate within buffer (keeping the last value per key), or serialize same-key writes.
There was a problem hiding this comment.
Thanks @stankiewicz , good catch. Fixed, now each buffered batch is deduplicated by document ID, retaining the last encountered value before issuing concurrent upserts for distinct IDs. The batch-size threshold still counts input elements. Javadoc now clarifies that this does not provide ordering or latest-value guarantees across batches or workers, and the retry wording has been qualified accordingly. Added unit coverage and a Docker integration test for duplicate IDs in full and partial batches. Formatting/Checkstyle, all 41 unit tests, and all 16 integration tests passed locally.
|
Hi @stankiewicz, thanks for the review! I’ve addressed all your comments, and all local checks pass. When you have a chance, could you please take another look and approve if everything looks good? Thank you! |
| static Map<String, JsonObject> deduplicateBatch(List<KV<String, JsonObject>> buffer) { | ||
| Map<String, JsonObject> documents = new LinkedHashMap<>(); | ||
| for (KV<String, JsonObject> document : buffer) { | ||
| documents.put(document.getKey(), document.getValue()); | ||
| } | ||
| return documents; | ||
| } |
There was a problem hiding this comment.
You can make a buffer a Map< > and just replace the items, this way you will always keep the latest, and when flushing you get all the entries.
There was a problem hiding this comment.
Changed the buffer to a map, retaining the last encountered value per document ID. A separate input counter preserves the batch-size threshold. Added coverage for duplicate-only batches and counter resets.
| * .withScope("inventory") | ||
| * .withCollection("airline")) | ||
| * .withIdFn(airline -> airline.getId()) | ||
| * .withContentFn(airline -> JsonObject.create().put("name", airline.getName()))); |
There was a problem hiding this comment.
withFormatFunction may be better name, aligned with other IOs.
There was a problem hiding this comment.
Renamed withContentFn() to withFormatFunction() and updated the implementation, examples, and tests.
|
|
||
| abstract Builder<T> setScanConsistency(QueryScanConsistency scanConsistency); | ||
|
|
||
| abstract Builder<T> setParseFn(SerializableFunction<JsonObject, T> parseFn); |
There was a problem hiding this comment.
setRowMapper maybe is better function name.
There was a problem hiding this comment.
Renamed the read mapping API to withRowMapper(), with corresponding rowMapper and setRowMapper() names internally. Updated the examples and tests.
| * for the previous output type. | ||
| */ | ||
| public <OutputT> Read<OutputT> withParseFn(SerializableFunction<JsonObject, OutputT> parseFn) { | ||
| return new AutoValue_CouchbaseIO_Read.Builder<OutputT>() |
There was a problem hiding this comment.
@stankiewicz withRowMapper() can change the output type for example, from Read to Read. toBuilder() returns Builder, so it cannot accept a mapper returning a different type without an unchecked cast. Creating Builder keeps this type-safe and deliberately resets the old coder, which may no longer match the output type. All other configuration is preserved.
There was a problem hiding this comment.
@SuppressWarnings("unchecked")
public <OutputT> Read<OutputT> withRowMapper(
SerializableFunction<JsonObject, OutputT> rowMapper) {
Builder<OutputT> builder = (Builder<OutputT>) toBuilder();
return builder.setRowMapper(rowMapper).build();
}
maybe?
Why - if you add new parameters, you don't need to remember to add it to withRowMapper.
There was a problem hiding this comment.
Updated to use toBuilder() as suggested. The existing coder is cleared because the mapper may change the output type. Added a test verifying configuration preservation.
| } | ||
|
|
||
| /** Sets the function that returns the document ID for an element. */ | ||
| public Write<T> withIdFn(SerializableFunction<T, String> idFn) { |
There was a problem hiding this comment.
I'm curious if we could mimic BigQuery's
withRowMutationInformationFn so eventually this write can also support Delete in the future.
RowMutationInformation would contain op type (upsert today) and ID.
There was a problem hiding this comment.
@stankiewicz Adopted withRowMutationInformationFn() with a Couchbase-specific RowMutationInformation containing the document ID and operation. V1 supports UPSERT only, leaving room for future delete support. Updated the examples and tests; all local checks pass.
|
Hi @stankiewicz, thanks for the thorough review! I’ve addressed all your comments, and all local checks pass. Could you please take another look when you have a chance and approve if everything looks good? Thanks! |
|
|
||
| @Setup | ||
| public void setup() { | ||
| cluster = config.connect(); |
There was a problem hiding this comment.
maybe connected.bucket(bucket).waitUntilReady(...) when config.bucket() != null
There was a problem hiding this comment.
Added a readiness wait when a bucket is configured, using a 30-second timeout. Bucket-free reads remain supported, and the connection is closed if readiness fails.
| checkArgument( | ||
| mutation.getMutationType() == RowMutationInformation.MutationType.UPSERT, | ||
| "Only UPSERT mutations are supported"); | ||
| buffer.put(mutation.getDocumentId(), formatFunction.apply(element)); |
There was a problem hiding this comment.
make sure formatFunction returns non-null element.
There was a problem hiding this comment.
Added a non-null check before buffering the formatted document. Added a unit test verifying that null output fails with a clear message and is not buffered.
|
@nitinware overall looks good, few more fixes and it will be ok to merge. |
|
Thanks @stankiewicz! I really appreciate your thorough review and helpful suggestions. I’ve addressed the remaining fixes, and all local checks pass. Could you please take another look and approve if everything looks good? |
please run IT tests several times, make sure we don't introduce flakes. |
|
Thanks @stankiewicz for the approval and thorough review! Fixed the index-creation race during startup rebalance with a bounded retry. All 17 integration tests passed five fresh-container runs locally, 85 test executions passed. The merge conflict is also resolved. |
Adds CouchbaseIO, a Java connector for Couchbase Server. Fixes #18381.
Design discussion: https://lists.apache.org/thread/cvzpfnw717zbnf7z6slfkfbp6zg6604g
Design doc: https://docs.google.com/document/d/1ALlXaiVSa_Dn9jJxM9MsmhEhBLPW_FP8RswN8HG33iU
Implementation
The commits are organized by component to make the changes easier to review:
withIdFnandwithContentFn, with bounded in-flight concurrency and flushing in@FinishBundle.JsonObjectvalues.JsonObject), named/positional parameters, scan consistency, and opt-inwithRedistribute()applied after parsing.readRows(Schema)converts query results into BeamRowvalues usingRowJson, respecting the supplied schema's nullability constraints.CHANGES.md, and the connector documentation table.Testing
CouchbaseIOTestandJsonObjectCoderTest.CouchbaseIOReadTestandCouchbaseIOWriteTest, usingcouchbase/server:enterprise-8.0.3.toString, or display data), type mismatches, missing required fields, and repeated UPSERT operations../gradlew :sdks:java:io:couchbase:build(Checkstyle, Javadoc, Checker Framework nullness validation, and dependency analysis).checkJavaLinkagecould not run locally because of a corporate repository proxy restriction; CI validation is pending.couchbases://) is delegated to the Couchbase SDK but is not covered by an integration test.Known limitations / follow-ups
ClusterperDoFninstance. JVM-wide sharing may be considered after benchmarking.R: @kennknowles
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
addresses #123orfixes #456).CHANGES.mdwith noteworthy changes.See CONTRIBUTING.md for more details.