Client Library Concepts¶
The Python and C++ client libraries share one model of clusters, connections, callbacks, live updates, and column types, described on this page.
Overview¶
A program creates one client object and adds one or more clusters to it. A cluster is a named set of connections to one deployment, and every call names the cluster it uses. The Python client is built on the C++ library, so the model, the method names, and the behavior on this page apply to both. The Java classes and the ODBC driver work differently and are described on their own pages.
The 3 connections¶
Each cluster has up to 3 connections, configured separately, and a program configures only the ones it uses:
| Connection | Listener, default port | Used for | Python configuration | C++ configuration |
|---|---|---|---|---|
| SQL connection | Center's JDBC listener, 3280 | Queries, inserts, bulk loads | AmiDatabaseConfig |
DatabaseConfig |
| Center connection | Center port, 3270 | Subscribing to tables | AmiSubscriptionConfig |
SubscriptionConfig |
| Relay connection | Relay port, 3289 | Publishing objects, registering commands | AmiPublicationConfig |
PublicationConfig |
The client sets no default port: every configuration names its port. TLS is on unless a configuration turns it off.
The SQL connection is not a driver for an external database
The SQL connection uses the platform's own JDBC protocol to carry a program's SQL to 3forge Enterprise. It does not connect 3forge Enterprise to an external database such as Postgres or Oracle. That is a datasource, configured on the server.
When the client connects a cluster, it connects the Center connection first. If the Center connection fails, the SQL connection is never attempted.
Shards and failover¶
A configuration holds a list of shards, and a shard is one connection, not one server. Several shards can point at the same host and port to open parallel connections. For high availability, each shard can also fail over:
- Failover endpoints move a shard to another server: each shard can list further host and port pairs, and after a drop the client moves to the next one, with the same TLS settings.
- Retries back off to 60 seconds: the client tries every endpoint, waits 1 second, and doubles the wait between rounds, up to 60 seconds, until it connects.
- Several SQL shards act as one: a query or insert runs on every enabled SQL shard, the results are merged, and affected-row counts are added together.
- Several Relay shards all receive a publish: a published object goes to every shard unless a distribution function picks shards for it.
See Failover and sharding for configuration.
Callbacks and threads¶
A program receives events through a listener, an object with these callback methods:
on_connectandon_disconnect: a connection opened or closed.on_logged_in: a connection logged in.on_message_receivedandon_message_sent: a message arrived or was sent.on_command: the platform called a registered command.on_subscribe: a subscription request was sent.on_update: a subscribed row was added, updated, or deleted.on_log: the client logged a message, at level 0 Debug, 1 Info, 2 Warning, or 3 Error.
Every callback runs on one dedicated callback thread, one at a time, in the order the events arrived. The callback thread is separate from the program's own threads and from the network threads. This affects a program in 2 ways:
- A slow callback delays later callbacks: work done in a callback holds the thread, so every later callback for that client, including updates, waits for it.
- A query from inside a callback returns: a
sql()call made inon_updatereturns normally. - A listener added late misses events: listeners are added before clusters, so they receive the first connection events.
Several failures are reported only through on_log, including a refused client certificate. See Troubleshooting.
Live updates¶
Subscribing to a table delivers its current rows first, then a stream of changes. Each row change event arrives as an update in on_update.
| Field (Python / C++) | Meaning |
|---|---|
action / action_ |
1 added, 2 updated, 3 deleted |
id / id_ |
The row's id, on every update. Changes are merged into a copy of the table by this id |
table_name / table_name_ |
The table |
object_id / object_id_ |
The object's id on the update that adds a published object; "" on later updates and deletes, and for a row created by SQL |
field_names, row_data / field_names_, row_data_ |
The columns sent, and their values |
How the updates combine:
- The current rows arrive as additions: each row of the snapshot is an update with action
1. - An update carries only the changed columns: the rest of the row is unchanged, so the update is merged by
id. - A delete carries no columns: only the row's
idis sent. - Changes within one refresh arrive as one update: a row inserted and then changed at once arrives once, as action
1, with its latest values. - A synchronous subscribe returns before the rows arrive:
subscribe(..., sync=True)returns once the server has answered, while the rows are still queued for the callback, and no callback marks the end of the snapshot. - A subscription to a missing table reports success: it delivers nothing, so the name is worth checking in
__TABLEfirst.
In Python the update is a copy, so its values stay valid after the callback returns. In C++ the update object exists only for the call, so a program copies the values it keeps.
Publishing and the acknowledgement¶
A published object goes to the Relay, which acknowledges it and passes it to the Center. The object's type names the table:
- A new type creates its table: the table gets one column per field, plus an
Icolumn holding the object's id, and accepts new columns later. - An existing table needs an
Icolumn and every field: a table created withCREATE PUBLIC TABLErejects unknown columns, includingI, and a rejected object is not stored. - The acknowledgement does not mean a row was stored: the Relay replies
OK, object acceptedbefore the object reaches the Center, so the reply is the same for a stored object and a dropped one, and only the table shows which happened.
Column types¶
Query results come back as one value per column. In Python, a result is a dictionary of columns: text columns are lists of str, binary columns are lists of bytes, and every other column is a 1-dimensional numpy array.
| Column type | Python, from sql() |
C++ Column holds |
|---|---|---|
String |
list of str |
std::vector<std::string> |
Integer |
numpy int32 |
std::vector<std::int32_t> |
Long |
numpy int64 |
std::vector<std::int64_t> |
Double |
numpy float64 |
std::vector<double> |
Boolean |
numpy uint8, 1 or 0 |
std::vector<std::uint8_t> |
Character |
numpy uint16, the character code |
std::vector<std::uint16_t> |
UTC |
numpy int64, milliseconds since the epoch |
std::vector<std::int64_t> |
Byte |
numpy uint8 |
std::vector<std::uint8_t> |
Time values are numbers with no time zone conversion. A Boolean comes back as 1 or 0, not True or False. In Python, a NULL string comes back as ""; in C++, is_null tells a NULL apart.