This section provides information about theNdb_cluster_connection
class, which models a connection by a management server (ndb_mgmd) to a set of data nodes.
- Parent class
-
None
- Child classes
-
None
- Description
-
An NDB application program should begin with the creation of a single
Ndb_cluster_connection
object, and typically makes use of a singleNdb_cluster_connection
. The application connects to a cluster management server when this object'sconnect()
method is called. By using thewait_until_ready()
method it is possible to wait for the connection to reach one or more data nodes.An instance of
Ndb_cluster_connection
is used to create anNdb
object. Prior to NDB 7.3.8 and NDB 7.4.3, it was possible to delete theNdb_cluster_connection
used to create a given instance ofNdb
without first deleting the dependentNdb
object. (Bug #19999242) - Methods
-
The following table lists the public methods of this class and the purpose or use of each method:
Table 2.32 Ndb_cluster_connection class methods and descriptions
Name Description Ndb_cluster_connection()
Constructor; creates a connection to a cluster of data nodes. connect()
Connects to a cluster management server. get_auto_reconnect()
Gets the auto-reconnection setting for API nodes using this Ndb_cluster_connection
.get_latest_error()
Whether or not the most recent attempt to connect succeeded. get_latest_error_msg()
If the most recent attempt to connect failed, provides the reason. get_max_adaptive_send_time()
Get timeout before adaptive send forces the sending of all pending signals. get_num_recv_threads()
Get number of receive threads. get_next_ndb_object()
Used to iterate through multiple Ndb
objects.get_recv_thread_activation_threshold()
Get activation level for bound receive threads. get_system_name()
Get the cluster's system name. lock_ndb_objects()
Disables the creation of new Ndb
objects.set_auto_reconnect()
Enables or disables auto-reconnection of API nodes using this Ndb_cluster_connection
.set_data_node_neighbour ()
Sets a neighbor node for for optimal transaction coordinator placement set_max_adaptive_send_time()
Set timeout to elapse before adaptive send forces the sending of all pending signals. set_name()
Provides a name for the connection set_num_recv_threads()
Set number of receive threads to be bound. set_recv_thread_cpu()
Set one or more CPUs to bind receive threads to. set_optimized_node_selection()
Used to control node-selection behavior. set_service_uri()
Set a URI for publication in the ndbinfo.processes
tableset_timeout()
Sets a connection timeout unlock_ndb_objects()
Enables the creation of new Ndb
objects.unset_recv_thread_cpu()
Unset the binding of the receive thread to one or more CPUs. wait_until_ready()
Waits until a connection with one or more data nodes is successful.
- Description
-
This method creates a connection to an NDB Cluster, that is, to a cluster of data nodes. The object returned by this method is required in order to instantiate an
Ndb
object. Thus, every NDB API application requires the use of anNdb_cluster_connection
. - Signatures
-
Ndb_cluster_connection
has two constructors. The first of these is shown here:Ndb_cluster_connection ( const char*connection_string= 0 )
The second constructor takes a node ID in addition to the connection string argument. Its signature and parameters are shown here:
Ndb_cluster_connection ( const char*connection_string, intforce_api_nodeid)
- Parameters
-
The first version of the constructor requires a single
connection_string
parameter, pointing to the location of the management server.The second version of the constructor takes two arguments, a
connection_string
and the node ID (force_api_nodeid
) to be used by this API node. This node ID overrides any node ID value set in theconnection_string
argument. - Return value
-
An instance of
Ndb_cluster_connection
.
- Description
-
This method connects to a cluster management server.
- Signature
-
int connect ( intretries= 30, intdelay= 1, intverbose= 0 )
- Parameters
-
This method takes three parameters, all of which are optional:
retries
specifies the number of times to retry the connection in the event of failure. The default value is 30.0
means that no additional attempts to connect are made in the event of failure; using a negative value forretries
results in the connection attempt being repeated indefinitely.The
delay
represents the number of seconds between reconnect attempts; the default is1
second.verbose
indicates whether the method should output a report of its progress, with1
causing this reporting to be enabled; the default is0
(reporting disabled).
- Return value
-
This method returns an
int
, which can have one of the following 3 values:0: The connection attempt was successful.
1: Indicates a recoverable error.
-1: Indicates an unrecoverable error.
- Description
-
This method retrieves the current
AutoReconnect
setting for a givenNdb_cluster_connection
. For more detailed information, seeNdb_cluster_connection::set_auto_reconnect(). - Signature
-
int get_auto_reconnect ( void )
- Parameters
-
None.
- Return value
-
An integer value
0
or1
, corresponding to the currentAutoReconnect
setting in effect for for this connection.0
forces API nodes to use new connections to the cluster, while1
enables API nodes to re-use existing connections.
- Description
-
This method can be used to determine whether or not the most recent
connect()
attempt made by thisNdb_cluster_connection
succeeded . If the connection succeeded,get_latest_error()
returns0
; otherwise, it returns1
. If the connection attempt failed, useNdb_cluster_connection::get_latest_error_msg()
to obtain an error message giving the reason for the failure. - Signature
-
int get_latest_error ( void ) const
- Parameters
-
None.
- Return value
-
1
or0
. A return value of1
indicates that the latest attempt to connect failed; if the attempt succeeded, a0
is returned.
- Description
-
If the most recent connection attempt by this
Ndb_cluster_connection
failed (as determined by callingget_latest_error()
), this method provides an error message supplying information about the reason for the failure. - Signature
-
const char* get_latest_error_msg ( void ) const
- Parameters
-
None.
- Return value
-
A string containing an error message describing a failure by
Ndb_cluster_connection::connect()
. If the most recent connection attempt succeeded, an empty string is returned.
- Description
-
Get the minimum time in milliseconds that is permit to lapse before the adaptive send mechanism forces all pending signals to be sent.
- Signature
-
Uint32 get_max_adaptive_send_time ( )
- Parameters
-
None.
- Return value
-
Wait time as a number of milliseconds. This should always be a value between 0 and 10, inclusive.
Iterating over Ndb objects.To retrieve all existingNdb
objects, perform the following three steps:
Invoke the
lock_ndb_objects()
method. This prevents the creation of any new instances ofNdb
until theunlock_ndb_objects()
method is called.Retrieve the first available
Ndb
object by passingNULL
toget_next_ndb_object()
. You can retrieve the secondNdb
对象通过检索的冷杉的指针st call to the nextget_next_ndb_object()
call, and so on. When a pointer to the last availableNdb
instance is used, the method returnsNULL
.After you have retrieved all desired
Ndb
objects, you should re-enableNdb
object creation by calling theunlock_ndb_objects()
method.
- Description
-
Get the number of receiver threads.
- Signature
-
int get_num_recv_threads ( void ) const
- Parameters
-
None.
- Return value
-
The number of receiver threads.
- Description
-
Get the level set for activating the receiver thread bound by
set_recv_thread_cpu()
. - Signature
-
int get_recv_thread_activation_threshold ( void ) const
- Parameters
-
None.
- Return value
-
An integer threshold value. SeeNdb_cluster_connection::set_recv_thread_activation_threshold(), for information about interpreting this value.
- Description
-
Gets the system name from the cluster configuration. This is the value of the
Name
system configuration parameter set in the cluster'sconfig.ini
configuration file. - Signature
-
const char* get_system_name ( void ) const
- Parameters
-
None.
- Return value
-
The cluster system name. If not set in the cluster configuration file, this is a generated value in the form
MC_
(for example,timestamp
MC_20170426182343
), using the time that the management server was started.
- Description
-
Calling this method prevents the creation of new instances of the
Ndb
class. This method must be called prior to iterating over multipleNdb
objects usingget_next_ndb_object()
. - Signature
-
void lock_ndb_objects ( void ) const
This method was made
const
in NDB 7.3.15, 7.4.13, and 7.5.4 (Bug #23709232).For more information, seeNdb_cluster_connection::get_next_ndb_object().
- Parameters
-
None.
- Return value
-
None.
- Description
-
An API node that is disconnected from the cluster is forced to use a new connection object to reconnect, unless this behavior is overridden by setting
AutoReconnect = 1
in theconfig.ini
file or calling this method with 1 as the input value. Calling the method with 0 for the value has the same effect as setting theAutoReconnect
configuration parameter (also introduced in those NDB Cluster versions) to 0; that is, API nodes are forced to create new connections.
When called, this method overrides any setting forAutoReconnect
made in theconfig.ini
file.
For more information, seeDefining SQL and Other API Nodes in an NDB Cluster.
- Signature
-
void set_auto_reconnect ( intvalue)
- Parameters
-
A
value
of 0 or 1 which determines API node reconnection behavior. 0 forces API nodes to use new connections (Ndb_cluster_connection
objects); 1 permits API nodes to re-use existing connections to the cluster. - Return value
-
None.
- Description
-
Set data node neighbor of the connection, used for optimal placement of the transaction coordinator. This method be used after creating the
Ndb_cluster_connection
, but prior to starting any query threads. This is due to the fact that this method may change the internal state of theNdb_cluster_connection
共享的线程使用。这种状态是不thread-safe; changing it can lead to non-optimal node selection at the time of the change.You can use the
ndb_data_node_neighbour
server system variable to set a data node neighbor for an NDB Cluster SQL node.This method was added in NDB 7.5.2.
- Signature
-
void set_data_node_neighbour ( Uint32neighbour_node)
- Parameters
-
The ID of the node to be used as the neighbor.
- Return value
-
None.
- Description
-
Set the minimum time in milliseconds that is permit to lapse before the adaptive send mechanism forces all pending signals to be sent.
- Signature
-
void set_max_adaptive_send_time ( Uint32milliseconds)
- Parameters
-
Wait time in milliseconds. The range is 0-10, with 10 being the default value.
- Return value
-
None.
- Description
-
Sets a name for the connection. If the name is specified, it is reported in the cluster log.
- Signature
-
void set_name ( const char*name)
- Parameters
-
The
name
to be used as an identifier for the connection. - Return value
-
None.
- Description
-
Set the number of receiver threads bound to the CPU (or CPUs) determined using
set_recv_thread_cpu()
and with the threshold set byset_recv_thread_activation_threshold()
.This method should be invoked before trying to connect to any other nodes.
- Signature
-
int集_num_recv_threads ( Uint32num_recv_threads)
- Parameters
-
The number of receive threads. The only supported value is
1
. - Return value
-
-1
indicates an error; any other value indicates success.
- Description
-
This method can be used to override the
connect()
method's default behavior as regards which node should be connected to first. - Signature
-
void set_optimized_node_selection ( intvalue)
- Parameters
-
An integer
value
. - Return value
-
None.
- Description
-
Set the level for activating the receiver thread bound by
set_recv_thread_cpu()
. Below this level, normal user threads are used to receive signals. - Signature
-
int集_recv_thread_activation_threshold ( Uint32threshold)
- Parameters
-
An integer
threshold
价值。16或更高意味着接收线程never used as receivers. 0 means that the receive thread is always active, and that retains poll rights for its own exclusive use, effectively blocking all user threads from becoming receivers. In such cases care should be taken to ensure that the receive thread does not compete with the user thread for CPU resources; it is preferable for it to be locked to a CPU for its own exclusive use. The default is 8. - Return value
-
-1
indicates an error; any other value indicates success.
- Description
-
Beginning with NDB 7.5.7 and NDB 7.6.2, this method can be used to create a URI for publication in
service_URI
column of the the application's row in thendbinfo.processes
table.Provided that this method is called prior to invoking
connect()
, the service URI is published immediately upon connection; otherwise, it is published after a delay of up toHeartbeatIntervalDbApi
milliseconds. - Signature
-
int集_service_uri ( const char*scheme, const char*host, intport, const char*path)
- Parameters
-
This method takes the parameters listed here:
scheme: The URI scheme. This is resticted to lowercase letters, numbers, and the characters
.
,+
, and-
(period, plus sign, and dash). The maximu length is 16 characters; any characters over this limit are truncated.host
: The URI network address or host name. The maximum length is 48 characters (sufficient for an IPv6 network address); any characters over this limit are truncated. If null, each data node reports the network address from its own connection to this node. AnNdb_cluster_connection
that uses multiple transporters or network addresses to connect to different data nodes is reflected in multiple rows in thendbinfo.processes
table.port
: The URI port. This is not published if it is equal to 0.path
: The URI path, possibly followed by a query string beginning with?
. The maximum combined length of the path and query may not exceed 128 characters; if longer, it is truncated to this length.The path may not begin with a double slash (
//
).
- Return value
-
0 on success, 1 in the event of a syntax error.
- Description
-
Set the CPU or CPUs to which the receiver thread should be bound. Set the level for activating the receiver thread as a receiver by invoking
set_recv_thread_activation_threshold()
. Unset the binding for this receiver thread by invokingunset_recv_thread_cpu()
. - Signature
-
int集_recv_thread_cpu ( Uint16*cpuid_array, Uint32array_len, Uint32recv_thread_id= 0 )
- Parameters
-
This method takes three parameters, listed here:
An array of one or more CPU IDs to which the receive thread should be bound
The length of this array
The thread ID of the receive thread to bind. The default value is
0
.
- Return value
-
-1
indicates an error; any other value indicates success.
- Description
-
Used to set a timeout for the connection, to limit the amount of time that we may block when connecting.
This method is actually a wrapper for the MGM API function
ndb_mgm_set_timeout()
. - Signature
-
int集_timeout ( inttimeout_ms)
- Parameters
-
The length of the timeout, in milliseconds (
timeout_ms
). Currently, only multiples of 1000 are accepted. - Return value
-
0 on success; any other value indicates failure.
- Description
-
This method undoes the effects of the
lock_ndb_objects()
method, making it possible to create new instances ofNdb
.unlock_ndb_objects()
should be called after you have finished retrievingNdb
objects using theget_next_ndb_object()
method. - Signature
-
void unlock_ndb_objects ( void ) const
This method was made
const
in NDB 7.3.15, 7.4.13, and 7.5.4 (Bug #23709232).For more information, seeNdb_cluster_connection::get_next_ndb_object().
- Parameters
-
None.
- Return value
-
None.
- Description
-
Unset the CPU or CPUs to which the receiver thread was bound using
set_recv_thread_cpu()
. - Signature
-
int unset_recv_thread_cpu ( Uint32recv_thread_id)
- Parameters
-
The thread ID of the receiver thread to be unbound.
- Return value
-
-1
indicates an error; any other value indicates success.
- Description
-
This method is needed to establish connections with the data nodes. It waits until the requested connection with one or more data nodes is successful, or until a timeout condition is met.
- Signature
-
int wait_until_ready ( inttimeoutBefore, inttimeoutAfter)
- Parameters
-
This method takes two parameters:
timeoutBefore
determines the number of seconds to wait until the first“live”node is detected. If this amount of time is exceeded with no live nodes detected, then the method immediately returns a negative value.timeoutAfter
determines the number of seconds to wait after the first“live”node is detected for all nodes to become active. If this amount of time is exceeded without all nodes becoming active, then the method immediately returns a value greater than zero.
- Return value
-
wait_until_ready()
returns anint
, whose value is interpreted as follows:= 0
: All nodes are“live”.> 0
: At least one node is“live”(however, it is not known whetherallnodes are“live”).< 0
: An error occurred.