Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@giriraj-singh-couchbase
Copy link

@giriraj-singh-couchbase giriraj-singh-couchbase commented Sep 10, 2025

This pull request introduces Couchbase Binary Protocol support to bRPC, enabling communication with Couchbase Server and Capella deployments. It adds a new protocol handler, request/response builders, comprehensive example clients, performance benchmarking tools, and detailed documentation. The changes are grouped below by theme:

Couchbase Binary Protocol Integration

Protocol Layer (src/brpc/policy/):

  • Added couchbase_protocol.h and couchbase_protocol.cpp implementing the complete binary protocol framing, parsing, with support for all standard Couchbase commands (GET, SET, ADD, REPLACE, DELETE, etc.)

High-Level API (src/brpc/):

  • Implemented CouchbaseOperations class in src/brpc/couchbase.h and src/brpc/couchbase.cpp (~3,500 lines), providing simplified APIs for:

    • Authentication: authenticate() and authenticateSSL() for both non-SSL and SSL connections (required for Couchbase Capella)
    • Bucket management: selectBucket() for bucket selection
    • CRUD operations: get(), upsert(), add(), replace(), append(), prepend(), delete_() with Result struct for clean error handling
    • Pipeline batching: beginPipeline(), pipelineRequest(), executePipeline(), clearPipeline() for batching multiple operations in a single network call.
  • Implemented CouchbaseRequest and CouchbaseResponse classes (inherit from NonreflectableMessage) providing low-level request construction and response parsing

  • Implemented CouchbaseManifestManager singleton for thread-safe collection manifest management with automatic caching and refresh

Example Clients

Basic Client (example/couchbase_c++/couchbase_client.cpp - 462 lines):

  • Demonstrates authentication (both SSL and non-SSL), bucket selection, basic CRUD operations, collection-scoped operations, and error handling patterns

Multithreaded Client (example/couchbase_c++/multithreaded_couchbase_client.cpp - 375 lines):

  • Demonstrates two threading patterns:
    • Multiple threads sharing a single CouchbaseOperations instance (same bucket, shared connection)
    • Multiple threads with dedicated CouchbaseOperations instances (different buckets, isolated connections)

Documentation

Comprehensive Guide (docs/en/couchbase_example.md):

Key Features

  • SSL/TLS Support: Built-in encryption for Couchbase Capella and secure on-premises deployments
  • Collection Management: Native Couchbase 7.0+ collections with automatic ID resolution, manifest caching, and scope support
  • Pipeline Batching: Batch multiple operations.
  • Thread Safety: Reader-writer locks for manifest caching, support for shared or isolated connection patterns
  • Connection Pooling: Intelligent connection sharing per server+bucket combination
  • Error Handling: Clean Result struct with success flag, error messages, status codes, and values

What problem does this PR solve?

Added native support to interact with Couchbase servers using the binary protocol, enabling efficient CRUD operations, SSL/TLS encrypted connections and collection management.

Side effects:

  • Performance effects: None

  • Breaking backward compatibility: NO


Check List:

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

license-eye has totally checked 1732 files.

Valid Invalid Ignored Fixed
869 3 860 0
Click to see the invalid file list
  • example/couchbase_c++/couchbase_client.cpp
  • src/brpc/couchbase.h
  • src/brpc/policy/couchbase_protocol.h

@@ -0,0 +1,718 @@
#include <gflags/gflags.h>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <gflags/gflags.h>
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <gflags/gflags.h>

@@ -0,0 +1,229 @@
#ifndef BRPC_COUCHBASE_H

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#ifndef BRPC_COUCHBASE_H
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef BRPC_COUCHBASE_H

@@ -0,0 +1,167 @@

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

@giriraj-singh-couchbase giriraj-singh-couchbase changed the title Couchbase binary protocol brpc Added support to connect and perform CRUD operations with couchbase Sep 19, 2025
Copy link
Collaborator

@ingenthr ingenthr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only a few comments on the docs are most important, but there are some questions around the scope/collection management. If you want to proceed with those as is and let the user manage it or improve it in the future, I'd be fine with that too.

// the status codes are from Couchbase Binary Protocol documentation,
// for original reference of status codes visit
// https://github.com/couchbase/kv_engine/blob/master/include/mcbp/protocol/status.h
enum Status {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error_status code has been referenced.


// collectionID fetching either from the metadata cache or if doesn't exist then
// fetch from the server.
bool CouchbaseOperations::CouchbaseRequest::getCachedOrFetchCollectionId(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method checks the global cache, and upon a miss the manifest is fetched from server using refreshCollectionManifest(). It shall be noted that upon a hit in global cache, local cache is also updated.


brpc::CouchbaseMetadataTracking::CollectionManifest manifest;
// check if the server/bucket exists in the cached collection manifest
if (!metadata_tracking->getBucketToCollectionManifest(server, selected_bucket,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probes server->bucket if a miss then the manifest for that bucket is fetched.

<< selected_bucket << " on server " << server
<< ", fetching from server");
// No cached manifest found, fetch from server
if (!refreshCollectionManifest(channel, server, selected_bucket)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refreshCollectionManifest for the server using which channel/connection is established, and the bucket which is currently selected.

return true;
}

bool CouchbaseOperations::CouchbaseRequest::refreshCollectionManifest(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method refreshes the manifest for a particular sever/bucket and updates the caches.

return result;
}

CouchbaseOperations::Result CouchbaseOperations::selectBucket(
Copy link
Author

@giriraj-singh-couchbase giriraj-singh-couchbase Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method selects a bucket for the connection and can be used to select different buckets.
As a bucket is selected the collection manifest is fetched for that bucket.
In case of failure of fetching the collection manifest, it will be retried later when a collection operation is operated on this bucket.

Using connection_groups to differentiate between connections across CouchbaseOperations instances to different buckets.

Renamed CollectionManifestTracker class to CollectionManifestManager and all the related functionality inside it as before refreshing method was outside this class

Added two different authenticate method authenticate(not secure) and authenticateSSL(secure)
…ouchbaselabs/cb_brpc into couchbase_binary_protocol_brpc

merging as the branch is not synced with the local-code base.
msg->pi = pi;
return MakeMessage(msg);
} else {
// if (header->command == CB_BINARY_SASL_AUTH) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed the logic where authentication throws an error during parsing itself if the credentials are wrong.


#include <zlib.h> //for crc32 Vbucket_id

#define CB_ADD(a,b) (a+b)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not needed anywhere.


// Debug flag for enabling debug statements
static bool DBUG = true; // Set to true to enable debug logs
static bool DBUG = false; // Set to true to enable debug logs

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a debug flag which can be set as true/false and is intended to be used for debugging purposes.

Added an example where a single instance is being shared across the threads when operating on single bucket.
updated the documentation on thread safe operations and fixed small small discrepancies.
Copy link
Collaborator

@ingenthr ingenthr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small comments that you can use to improve this if you'd like, but otherwise offering approval. I'm sure you want to wrap this one up.

@giriraj-singh-couchbase giriraj-singh-couchbase merged commit cf7ce31 into master Nov 5, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants