Core+ C++ Client
The Deephaven Core+ C++ client allows you to create and connect to Persistent Queries using the Community Core engine in the Deephaven Enterprise system. This page provides an overview of how to use the C++ client.
Create a SessionManager
A SessionManager
object allows the client to keep connections to a Deephaven Enterprise Controller and Authentication services.
First, we need to create a SessionManager
object, which will create the connections and keep them alive for as long as the SessionManager
object is valid (not destroyed).
#include <string>
#include "deephaven_enterprise/session/session_manager.h"
#include "deephaven_enterprise/utility/utility.h"
using deephaven_enterprise::session::SessionManager;
using deephaven_enterprise::utility::GetTidAsString;
int main(char *argv[], int argc) {
// ...
const std::string descriptive_name =
std::string(basename(argv[0])) +
" tid=" + GetTidAsString();
const json_url = "https://server1.mydomain.com:8000/iris/connection.json";
SessionManager session_manager = SessionManager::FromUrl(descriptive_name, json_url);
// ...
Note
The code block above connects to a Deephaven server at port 8000
. Use port 8000
for servers with an Envoy service and port 8123
for servers without an Envoy service.
The SessionManager::FromUrl
factory method takes two arguments.
- A descriptive name that will be used on the server and client sides to log information about operations performed by this client.
- A URL pointing to a file in JSON format containing information about connectivity parameters for the Deephaven installation. Such a file is provided by a Deephaven installation under
iris/connection.json
.
The descriptive name can, in principle, be any string of our choosing. However, it is important to select a value that allows us to distinguish our client in logs. It makes sense to include the process and/or thread ID where the client was created; the function GetTidAsString()
is provided for that purpose.
Note
The client implementation will append information about the host where this client is running, so there is no need to include that in the provided descriptive_name
.
Three network connections are made in the order indicated below during the creation of the SessionManager
object.
- The
connection.json
file is downloaded from the provided URL. This is a temporary connection that only lives until we get the file. - A connection is made to the Deephaven Enterprise Authentication service.
- A connection is made to the Deephaven Enterprise Controller service.
The connections to Authentication and Controller are made based on the information downloaded from connection.json
. These two connections are maintained as long as the SessionManager
object is valid.
All these operations must succeed for SessionManager
to be properly initialized, or an error results. When experiencing connectivity issues, or if the information contained in the connection.json
file downloaded is wrong, you may see an error similar to:
Error: std::string deephaven_enterprise::utility::GetUrl(const string&)@/opt/deephaven/src/iris/DhcInDhe/cpp-client/utility/src/net.cc:121: Timeout was reached
Note
See the appendix at the end of this document for more information about Deephaven client connectivity and some tools to debug network issues.
More information about Core+ clients and their connectivity can be found in the Core+ Clients section of our Communications Protocols documentation.
Authenticate
Once a SessionManager
is created, we need to authenticate it. We have two options: password authentication (providing a user and password) or private key authentication (providing a private key file).
bool auth_result = false;
if (use_password_authentication) {
// use password authentication
auth_result =
session_manager.PasswordAuthentication(user, password, operate_as);
} else {
// use private key authentication
auth_result =
session_manager.PrivateKeyAuthentication(private_key_filename);
}
Note
The operate_as
argument for the PasswordAuthentication
method is only interesting to system administrators; under normal circumstances, you would pass this argument as the same value provided for user
.
An authenticated SessionManager
connects to the PQ controller, which then creates a new connection to an existing Persistent Query (PQ) or a new PQ. In either case, the result of a connection is a DndClient
object.
The DndClient
object can get results from the PQ.
Connect to an existing Persistent Query
To connect to an existing PQ, we need to know either the string name or the numeric serial of the PQ we want to connect to.
const std::string pq_name = "my_favorite_pq";
DndClient client1 = session_manager.ConnectToPqByName(pq_name);
using deephaven_enterprise::session::pqserial_t; // a signed numeric type
const pqserial_t pq_serial = 9876543210L;
DndClient client2 = session_manager.ConnectToPqBySerial(pq_serial);
Create a new temporary Persistent Query and connect to it
To create a temporary PQ, we first need to create a PQ configuration. We can use a PqConfigBuilder
object for that.
The config object comes with predefined defaults for most parameters. Consult the class documentation for PqConfigBuilder
for the different options. At a minimum, we have to set the desired heap size for our worker in gigabytes.
const std::string pq_name = "my_new_pq";
PqConfigBuilder pq_builder = session_manager.MakeTempPqConfigBuilder(pq_name);
pq_builder.SetMaxHeapSizeGb(4);
pq_builder.SetScriptLanguage("groovy");
Once we have a PqConfigBuilder
ready with our options, we can call its Build
method to obtain the configuration, and pass it to the AddQueryAndConnect
method of our SessionManager
.
DndClient client3 = session_manager.AddQueryAndConnect(pq_builder.Build());
Send a query to a DndClient
and get the result
To interact with the tables in a PQ from the client, we first need to create a DndTableHandleManager
object from the DndClient
.
// DndClient client = ...
DndTableHandleManager table_manager = client.GetManager();
With a DndTableHandleManager
on hand, we can run a script on the server (PQ). In the example below, we create a table on the server (PQ) and then fetch that table on the client using a TableHandle
object.
A TableHandle
object can perform additional operations on the table from the client side. In our example, we print the table to standard output.
// "PT1S" is an ISO-8601 duration, it specifies a duration of 1 second.
table_manager.RunScript(
"my_table = io.deephaven.engine.util.TableTools.timeTable(\"PT1S\")");
sleep(2);
TableHandle table_handle = table_manager.FetchTable("my_table");
std::cout << "*** Table my_table is \n"
<< table_handle.Stream(true) << "\n";
Note
The code given to DndTableHandleManager::RunScript
should match the script language configured for the PQ when it was created. For the case where a new PQ is being created, the PqConfigBuilder::SetScriptLanguage
method can be used to set the script language of the new PQ. This method accepts either "groovy"
or "python"
as a string argument.
Close DndClient
and SessionManager
objects
When DndClient
and SessionManager
objects are destroyed, they are automatically closed. This closes network connections and releases all server-side resources associated with these objects. You can call the Close
method before the object is destroyed to force it to close right away.
client1.Close();
client2.Close();
client3.Close();
session_manager.Close();
Caution
Once the Close
method is called on either of these objects, any subsequent operation results in an error.
Ensuring these objects are closed promptly supports the best utilization of system resources.
Appendix
Test scripts
When the generated build bundle is deployed, code examples can be found in $PREFIX/src/iris/DhcInDhe/cpp-client/examples
, where $PREFIX
is the --prefix
path passed into the build script. The examples are already built as binaries that can be run.
The quickest way to verify that the installation is working properly is the tiny_session_manager
example. It takes a URL pointing to a Deephaven connection.json file. When run, it prompts for a username/password. If the connection and credentials are correct, it prints Session manager services authenticated
:
# Assuming $PREFIX was not set (/opt/deephaven)
cd /opt/deephaven
source env.sh # This defines LD_LIBRARY_PATH - required for the client
cd src/iris/DhcInDhe/cpp-client/build/examples/session_manager_examples
# For an Enterprise instance running without Envoy. If Envoy is in use, the port is 8123.
./tiny_session_manager https://<deephaven_enterprise_url>:8000/iris/connection.json
Creating session manager...
Session manager created.
Enter user: deephaven_user
enter deephaven_user's password:
Authenticating with the session manager services...
Session manager services authenticated.
Connectivity and tools to debug network issues
Deephaven clients use gRPC, an open-source RPC framework from Google, to implement communications with Deephaven servers. Part of the nature of how gRPC works implies treating network errors as potentially transient: network failures are based on a timeout model. This implies that the wrong host address or a server that is down does not immediately cause a connection attempt to fail; instead, gRPC keeps trying to connect, in the assumption that a new host name registration may appear or a server may restart and make the expected port available.
Eventually, if the network issue is not resolved before a timeout window expires, the connection attempt is considered failed, and an error results.
Important
The default timeout used by Deephaven in its clients is 2 minutes.
Network connectivity issues may appear opaque due to the two-minute timeout. However, treating network connectivity issues always as potentially transient failures is very useful in terms of making clients and servers resilient. It does, however, result in some opaqueness for users: the client may look like it just 'sits there' for two minutes before raising an error.
Tip
Setting the GRPC_VERBOSITY
environment variable before starting a client program can help debug network and connectivity issues. The possible values are info
and debug
(debug
provides more details while being more noisy/chatty).
Setting GRPC_VERBOSITY
logs details of gRPC
connection establishment, including SSL authentication and certificate validation to standard out.