A Shard-Aware Scylla C/C++ Driver

Why?

Scylla C/C++ driver was forked from Datastax driver with the view to adding Scylla-specific features. It’s a rich, fast, async, battle-proven piece of C++ code, although its interface is in pure C, which opens a lot of possibilities, like creating Python or Erlang bindings. Its newest feature, which will be discussed in this post, is shard-awareness.

cqlsh> SELECT address, port, client_type, shard_id FROM system.clients; address   | port  | client_type | shard_id
-----------+-------+-------------+----------
127.0.0.1 | 46730 | cql | 2
127.0.0.1 | 46732 | cql | 3
127.0.0.1 | 46768 | cql | 0
127.0.0.1 | 46770 | cql | 1
(4 rows)

How?

Scylla nodes extend the CQL SUPPORTED message with information about their shard count, current shard ID, sharding algorithm, etc. Knowing all this, the client can take one of two approaches to establish the pools of per-shard-connections: “basic” and “advanced”.

Installation

Our C/C++ driver releases are hosted on GitHub. If you are lucky to run CentOS 7, Ubuntu 18.04 or their relatives, you can simply download and install our packages along with their dependencies.

# Installation: CentOS 7
sudo yum install -y epel-release
sudo yum -y install libuv openssl zlib
wget https://github.com/scylladb/cpp-driver/releases/download/2.15.2-1/scylla-cpp-driver-2.15.2-1.el7.x86_64.rpm https://github.com/scylladb/cpp-driver/releases/download/2.15.2-1/scylla-cpp-driver-devel-2.15.2-1.el7.x86_64.rpm
sudo yum localinstall -y scylla-cpp-driver-2.15.2-1.el7.x86_64.rpm scylla-cpp-driver-devel-2.15.2-1.el7.x86_64.rpm
# Installation: Ubuntu 18.04
wget https://github.com/scylladb/cpp-driver/releases/download/2.15.2-1/scylla-cpp-driver_2.15.2-1_amd64.deb https://github.com/scylladb/cpp-driver/releases/download/2.15.2-1/scylla-cpp-driver-dev_2.15.2-1_amd64.deb
sudo apt-get update
sudo apt-get install -y ./scylla-cpp-driver_2.15.2-1_amd64.deb ./scylla-cpp-driver-dev_2.15.2-1_amd64.deb

Example

In this short example we’ll just open a number of connections and exit after a minute of sleep.

// conn.cpp - demonstration of advanced shard selection in C++
#include
#include
#include
#include

int main(int argc, char* argv[]) {
CassCluster* cluster = cass_cluster_new();
CassSession* session = cass_session_new();

cass_cluster_set_num_threads_io(cluster, 2);
cass_cluster_set_contact_points(cluster, "127.0.0.1");
cass_cluster_set_core_connections_per_host(cluster, 7);
// Now enable advanced shard-awareness
cass_cluster_set_local_port_range(cluster, 50000, 60000);

CassFuture* connect_future = cass_session_connect(session, cluster);

if (cass_future_error_code(connect_future) == CASS_OK) {
std::cout << "Connected\n";
std::this_thread::sleep_for(std::chrono::seconds(60)); // [1]
} else {
std::cout << "Connection ERROR\n";
}

// Cleanup
cass_future_free(connect_future);
cass_session_free(session);
cass_cluster_free(cluster);
}
cqlsh> SELECT address, port, client_type, shard_id FROM system.clients;

address | port | client_type | shard_id
-----------+-------+-------------+----------
127.0.0.1 | 37534 | cql | 0
127.0.0.1 | 37536 | cql | 1
127.0.0.1 | 38207 | cql | 4
127.0.0.1 | 50000 | cql | 0
127.0.0.1 | 50001 | cql | 1
127.0.0.1 | 50002 | cql | 2
127.0.0.1 | 50003 | cql | 3
127.0.0.1 | 50004 | cql | 4
127.0.0.1 | 50005 | cql | 5
127.0.0.1 | 50006 | cql | 6
127.0.0.1 | 50007 | cql | 7
127.0.0.1 | 50008 | cql | 0
127.0.0.1 | 50009 | cql | 1
127.0.0.1 | 50010 | cql | 2
127.0.0.1 | 50011 | cql | 3
127.0.0.1 | 50012 | cql | 4
127.0.0.1 | 50013 | cql | 5
127.0.0.1 | 50014 | cql | 6
127.0.0.1 | 50015 | cql | 7
(19 rows)

Summary

Shard-awareness has been present in Java, Go and Python drivers for a while, and we’re now actively developing a shard-aware Rust driver. Today we are delighted to welcome the C/C++ driver as a member of this honorable group. If you use the Datastax cpp-driver with Scylla you should switch to scylla-cpp-driver right now — it’s basically a free performance boost for your applications. And there is more to come: CDC partitioner, LWT support, RAII interface… The future will definitely bring these and a few other reasons to follow our driver!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
ScyllaDB

ScyllaDB

The monstrously fast and scalable NoSQL database.