Skip to content

Commit b703d72

Browse files
authored
CPP-741 - Allow client ID to be configurable
1 parent 88b00ab commit b703d72

8 files changed

Lines changed: 79 additions & 18 deletions

File tree

cpp-driver/gtests/src/unit/tests/test_startup_options.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,28 @@
1919
#include "driver_info.hpp"
2020
#include "query_request.hpp"
2121
#include "session.hpp"
22-
22+
#include "testing.hpp"
2323

2424
#define APPLICATION_NAME "DataStax C/C++ Test Harness"
2525
#define APPLICATION_VERSION "1.0.0"
2626

27+
inline bool operator==(const CassUuid& rhs, const CassUuid& lhs) {
28+
return rhs.clock_seq_and_node == lhs.clock_seq_and_node &&
29+
rhs.time_and_version == lhs.time_and_version;
30+
}
31+
32+
inline bool operator!=(const CassUuid& rhs, const CassUuid& lhs) {
33+
return !(rhs == lhs);
34+
}
35+
2736
class StartupRequestUnitTest : public Unit {
2837
public:
2938
void TearDown() {
3039
ASSERT_TRUE(session_.close()->wait_for(WAIT_FOR_TIME));
3140
Unit::TearDown();
3241
}
3342

43+
cass::Session& session() { return session_; }
3444
const cass::String& client_id() const { return client_id_; }
3545
cass::Config& config() { return config_; }
3646
const mockssandra::RequestHandler* simple_with_client_options() {
@@ -129,3 +139,27 @@ TEST_F(StartupRequestUnitTest, Application) {
129139
ASSERT_EQ(cass::driver_name(), options["DRIVER_NAME"]);
130140
ASSERT_EQ(cass::driver_version(), options["DRIVER_VERSION"]);
131141
}
142+
143+
TEST_F(StartupRequestUnitTest, SetClientId) {
144+
mockssandra::SimpleCluster cluster(simple_with_client_options());
145+
ASSERT_EQ(cluster.start_all(), 0);
146+
147+
CassUuid generated_client_id = session().client_id();
148+
CassUuid assigned_client_id;
149+
ASSERT_EQ(CASS_OK,
150+
cass_uuid_from_string("03398c99-c635-4fad-b30a-3b2c49f785c2",
151+
&assigned_client_id));
152+
config().set_client_id(assigned_client_id);
153+
154+
connect();
155+
CassUuid current_client_id = session().client_id();
156+
ASSERT_EQ(assigned_client_id, current_client_id);
157+
ASSERT_NE(generated_client_id, current_client_id);
158+
cass::Map<cass::String, cass::String> options = client_options();
159+
ASSERT_EQ(4u, options.size());
160+
161+
ASSERT_EQ("03398c99-c635-4fad-b30a-3b2c49f785c2", options["CLIENT_ID"]);
162+
ASSERT_EQ(CASS_DEFAULT_CQL_VERSION, options["CQL_VERSION"]);
163+
ASSERT_EQ(cass::driver_name(), options["DRIVER_NAME"]);
164+
ASSERT_EQ(cass::driver_version(), options["DRIVER_VERSION"]);
165+
}

cpp-driver/src/config.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class Config {
6868
, prepare_on_all_hosts_(CASS_DEFAULT_PREPARE_ON_ALL_HOSTS)
6969
, prepare_on_up_or_add_host_(CASS_DEFAULT_PREPARE_ON_UP_OR_ADD_HOST)
7070
, no_compact_(CASS_DEFAULT_NO_COMPACT)
71+
, is_client_id_set_(false)
7172
, host_listener_(new DefaultHostListener()) {
7273
profiles_.set_empty_key(String());
7374

@@ -398,8 +399,16 @@ class Config {
398399
application_version_ = application_version;
399400
}
400401

402+
CassUuid client_id() const { return client_id_; }
403+
bool is_client_id_set() const { return is_client_id_set_; }
404+
405+
void set_client_id(CassUuid client_id) {
406+
client_id_ = client_id;
407+
is_client_id_set_ = true;
408+
}
409+
401410
const DefaultHostListener::Ptr& host_listener() const { return host_listener_; }
402-
411+
403412
void set_host_listener(const DefaultHostListener::Ptr& listener) {
404413
if (listener) {
405414
host_listener_ = listener;
@@ -451,6 +460,8 @@ class Config {
451460
bool no_compact_;
452461
String application_name_;
453462
String application_version_;
463+
bool is_client_id_set_;
464+
CassUuid client_id_;
454465
DefaultHostListener::Ptr host_listener_;
455466
};
456467

cpp-driver/src/session_base.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ Future::Ptr SessionBase::connect(const Config& config,
8080
}
8181
}
8282

83+
if (config.is_client_id_set()) {
84+
client_id_ = config.client_id();
85+
}
8386
LOG_INFO("Client id is %s", to_string(client_id_).c_str());
8487

8588
config_ = config.new_instance();

cpp-driver/test/ccm_bridge/src/process.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ Process::Result Process::execute(const Args& command) {
9494
rc = uv_loop_close(&loop);
9595
if (rc != 0) {
9696
uv_print_all_handles(&loop, stderr);
97+
if (!result.standard_error.empty()) {
98+
std::cerr << result.standard_error << std::endl;
99+
}
97100
assert(false && "Process loop still has pending handles");
98101
}
99102

include/dse.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,6 @@ cass_cluster_set_dse_plaintext_authenticator_proxy_n(CassCluster* cluster,
459459
* that can aid in debugging issues with larger clusters where there are a lot
460460
* of client (or application) connections.
461461
*
462-
* @dse{6.0.0+}
463-
*
464462
* @public @memberof CassCluster
465463
*
466464
* @param[in] cluster
@@ -474,8 +472,6 @@ cass_cluster_set_application_name(CassCluster* cluster,
474472
* Same as cass_cluster_set_application_name(), but with lengths for string
475473
* parameters.
476474
*
477-
* @dse{6.0.0+}
478-
*
479475
* @public @memberof CassCluster
480476
*
481477
* @param[in] cluster
@@ -495,8 +491,6 @@ cass_cluster_set_application_name_n(CassCluster* cluster,
495491
* a lot of client (or application) connections that may have different
496492
* versions in use.
497493
*
498-
* @dse{6.0.0+}
499-
*
500494
* @public @memberof CassCluster
501495
*
502496
* @param[in] cluster
@@ -511,8 +505,6 @@ cass_cluster_set_application_version(CassCluster* cluster,
511505
* Same as cass_cluster_set_application_version(), but with lengths for string
512506
* parameters.
513507
*
514-
* @dse{6.0.0+}
515-
*
516508
* @public @memberof CassCluster
517509
*
518510
* @param[in] cluster
@@ -524,6 +516,23 @@ cass_cluster_set_application_version_n(CassCluster* cluster,
524516
const char* application_version,
525517
size_t application_version_length);
526518

519+
/**
520+
* Set the client id.
521+
*
522+
* This is optional; however it provides the server with the client ID that can
523+
* aid in debugging issues with large clusters where there are a lot of client
524+
* connections.
525+
*
526+
* Default: UUID v4 generated (@see cass_session_get_client_id())
527+
*
528+
* @public @memberof CassCluster
529+
*
530+
* @param[in] cluster
531+
* @param[in] client_id
532+
*/
533+
CASS_EXPORT void
534+
cass_cluster_set_client_id(CassCluster* cluster, CassUuid client_id);
535+
527536
/***********************************************************************************
528537
*
529538
* Session
@@ -548,13 +557,6 @@ cass_session_execute_dse_graph(CassSession* session,
548557
/**
549558
* Get the client id.
550559
*
551-
* This can help with debugging issues in large clusters where there are a lot
552-
* of client connections. A session's unique identifier (UUID) is constant for
553-
* its lifetime and does not change when re-establishing connection to a
554-
* cluster.
555-
*
556-
* @dse{6.0.0+}
557-
*
558560
* @public @memberof CassSession
559561
*
560562
* @param[in] session

src/cluster_config.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,8 @@ void cass_cluster_set_application_version_n(CassCluster* cluster,
131131
application_version_length));
132132
}
133133

134+
void cass_cluster_set_client_id(CassCluster* cluster, CassUuid client_id) {
135+
cluster->config().set_client_id(client_id);
136+
}
134137

135138
} // extern "C"

tests/src/integration/dse_integration.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ void DseIntegration::connect(dse::Cluster cluster) {
6565
void DseIntegration::connect() {
6666
// Create the cluster configuration and establish the session connection
6767
cluster_ = default_cluster();
68-
connect(cluster_);
68+
dse_cluster_ = cluster_;
69+
connect(dse_cluster_);
6970
}
7071

7172
Cluster DseIntegration::default_cluster() {

tests/src/integration/dse_integration.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ class DseIntegration : public Integration {
7373
virtual Cluster default_cluster();
7474

7575
protected:
76+
/**
77+
* Configured DSE cluster
78+
*/
79+
dse::Cluster dse_cluster_;
7680
/**
7781
* Connected database DSE session
7882
*/

0 commit comments

Comments
 (0)