Sync Cluster

class aerospike_sdk.sync.cluster.Cluster[source]

Bases: ClusterBase[Session, TransactionalSession, Node]

Synchronous cluster handle from sync.cluster_definition.ClusterDefinition.connect.

Mirrors Cluster but uses SyncClient and Session. The create_session() / transaction() / is_connected() factories are inherited from ClusterBase.

Example:

with ClusterDefinition("localhost", 3100).connect() as cluster:
    session = cluster.create_session(Behavior.DEFAULT)

See also

Cluster

__init__(sdk_client)[source]

Initialize a Cluster instance.

Parameters:

sdk_client (SyncClient) – The underlying SyncClient instance

Note

This should not be called directly. Use ClusterDefinition.connect() instead.

register_udf(body, server_path, language=UDFLang.LUA, *, policy=None)[source]

Register a UDF module from bytes (synchronous).

Raises:
Return type:

RegisterTask

register_udf_from_file(client_path, server_path, language=UDFLang.LUA, *, policy=None)[source]

Register a UDF module from a local file (synchronous).

Raises:
Return type:

RegisterTask

register_udf_from_resource(package, resource, server_path, language=UDFLang.LUA, *, policy=None)[source]

Register a UDF from a Python package resource (synchronous).

Raises:
Return type:

RegisterTask

remove_udf(server_path, *, policy=None)[source]

Remove a UDF module from the cluster (synchronous).

Raises:
Return type:

UdfRemoveTask

list_udf()[source]

List the UDF modules registered on the cluster (synchronous).

Raises:
Return type:

list[dict[str, str]]

list_indexes()[source]

List the secondary indexes defined on the cluster (synchronous).

Raises:
Return type:

list[dict[str, str]]

property exporters: tuple

The exporters the application registered, in registration order.

add_exporter(exporter)[source]

Register an exporter; snapshots are pushed to each one registered.

Registered exporters receive the push while metrics.exporter is custom; file (the default) and none leave them uncalled. They receive every snapshot in registration order. One that keeps raising is suspended and periodically retried without affecting the others. The cluster never closes an exporter registered here – the application owns its lifecycle.

Parameters:

exporter (Any) – A MetricsExporter (plain def export).

Raises:

TypeError – If exporter has no export method or defines an async one.

Return type:

None

Example:

exporter = MetricsWriter("/var/log/aerospike")
cluster.add_exporter(exporter)
remove_exporter(exporter)[source]

Unregister an exporter previously passed to add_exporter().

Parameters:

exporter (Any) – The exporter object to remove.

Return type:

bool

Returns:

True if it was registered, False if it was not found.

enable_metrics(policy=None)[source]

Enable metrics collection for this cluster.

Collection is off until enabled. Re-enabling with a changed latency unit or histogram shape discards the accumulated latency samples; counters are retained.

Parameters:

policy (Optional[MetricsPolicy]) – Collection configuration. Defaults to MetricsPolicy’s milliseconds/7-column scheme with every command recorded.

Raises:

AerospikeError – With PARAMETER_ERROR when a loaded configuration file sets metrics.enabled; the file owns the switch.

Return type:

None

Example:

cluster.enable_metrics(MetricsPolicy(sampler=Sampler.probability(0.1)))
disable_metrics()[source]

Disable metrics collection. Accumulated data is retained.

Stops the export push after one last snapshot, so the window since the previous export still reaches the exporters before this returns. Exporters the application registered stay registered and are not closed – their lifecycle belongs to the application.

Raises:

AerospikeError – With PARAMETER_ERROR when a loaded configuration file sets metrics.enabled; the file owns the switch.

Example:

cluster.disable_metrics()  # exporters have the closing snapshot
Return type:

None

metrics_enabled()[source]

Whether metrics collection is currently enabled.

Return type:

bool

close()[source]

Closes the cluster connection and releases all associated resources.

This method closes the underlying client connection. It should be called when the cluster is no longer needed to ensure proper resource cleanup.

This method is automatically called when using context manager:

with ClusterDefinition("localhost", 3100).connect() as cluster:
    # Use the cluster...
# cluster.close() is automatically called here

With metrics enabled, exporters receive one closing snapshot first.

Return type:

None

property cluster_name: str | None

The cluster name the servers report.

When the connection was built with ClusterDefinition.cluster_name(), nodes reporting any other name were rejected, so this equals the validated name.

Example:

print(f"connected to {cluster.cluster_name}")   # connected to prod-east
Returns:

The server’s cluster-name, or None when the servers are configured without one.

See also

nodes(): The nodes making up this cluster.

create_session(behavior=None)

Open a session on this cluster with optional behavior.

A session is a logical connection carrying behavior settings (timeouts, retry policies, consistency levels, …) that govern how operations run.

Parameters:

behavior (Optional[Behavior]) – Policy bundle for the session. Defaults to DEFAULT.

Return type:

TypeVar(_S)

Returns:

A session bound to this cluster’s SDK client.

Example:

session = cluster.create_session(Behavior.DEFAULT)

See also

transaction(): Open a multi-record transaction instead.

get_node(name)

Look up one node by its name.

Example:

node = cluster.get_node("BB9D4EB574A8DA6")
print(node.version, node.address)
Parameters:

name (str) – The node name, as reported by a node’s name or the *_per_node info helpers.

Return type:

TypeVar(_N, bound= NodeBase)

Returns:

The matching node view.

Raises:

See also

nodes(): Every node at once.

property is_connected: bool

Whether the cluster connection is currently active.

Returns:

True if the underlying client reports a live connection.

Example:

assert cluster.is_connected

See also

close(): Release the connection.

metrics_snapshot()

Snapshot the accumulated cluster metrics.

Available whether or not collection is on. Values are cumulative since metrics were enabled and frozen while collection is off; connection gauges are point-in-time. Snapshotting drains and aggregates per-node state, so poll at an export interval rather than per operation.

Return type:

MetricsSnapshot

Returns:

A MetricsSnapshot; zeroed counters if metrics were never enabled.

Example:

snapshot = cluster.metrics_snapshot()
reads = snapshot.latency(LatencyType.READ)
print(f"{reads.count} reads, avg {reads.average:.1f}")

See also

enable_metrics(): Start collection.

node_names()

The names of the nodes currently in the cluster.

Example:

names = cluster.node_names()
print(f"{len(names)} nodes: {', '.join(names)}")
Return type:

list[str]

Returns:

One name per active node.

Raises:

RuntimeError – If not connected.

See also

nodes(): The nodes themselves.

nodes()

The nodes currently in the cluster.

Example:

versions = {node.name: str(node.version) for node in cluster.nodes()}
if len(set(versions.values())) > 1:
    print(f"rolling upgrade in progress: {versions}")
Return type:

list[TypeVar(_N, bound= NodeBase)]

Returns:

One node view per active node.

Raises:

RuntimeError – If not connected.

See also

get_node(): Look up one node by name. node_names(): Just the names.

server_version()

The minimum server version across connected nodes.

Return type:

Optional[Version]

Returns:

The least-capable node’s Version, or None when the cluster reports no nodes. Guarding against the minimum is what makes a feature check safe on a mixed-version or mid-upgrade cluster.

Example:

v = cluster.server_version()
if v is not None and (v.major, v.minor, v.patch) >= (8, 2, 0):
    ...

See also

nodes(): Each node’s own version.

supports_ael()

Whether every node parses server-compiled AEL (filters, exp reads/writes).

Return type:

bool

supports_query_operations()

Whether every node supports read operations inside an index query.

Return type:

bool

supports_query_selection()

Whether every node supports server-led index selection (>= 8.2.0).

Return type:

bool

supports_string_operations()

Whether every node supports the server-side string operations.

Example:

if cluster.supports_string_operations():
    await session.upsert(key).bin("s").str_append("!").execute()
Return type:

bool

transaction(behavior=None)

Open a transactional session for a multi-record transaction (MRT).

Operations run inside the returned context manager use behavior (or DEFAULT when omitted) and auto-participate in a fresh Txn, committed on clean exit and aborted if an exception propagates. Requires a strong-consistency (SC) namespace.

Parameters:

behavior (Optional[Behavior]) – Policy bundle for operations inside the transaction. Defaults to Behavior.DEFAULT.

Return type:

TypeVar(_TS)

Returns:

The tree’s transactional session, bound to this cluster’s client.

Example:

with cluster.transaction() as tx:
    tx.upsert(accounts.id("A")).bin("balance").set_to(100).execute()

See also

create_session(): Non-transactional session.