10bet网址
MySQL NDB Cluster API Developer Guide
Download this Manual

2.3.12 The Ndb_cluster_connection Class

This section provides information about theNdb_cluster_connectionclass, which models a connection by a management server (ndb_mgmd) to a set of data nodes.

Ndb_cluster_connection Class Overview

Parent class

None

Child classes

None

Description

An NDB application program should begin with the creation of a singleNdb_cluster_connectionobject, 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 ofNdb_cluster_connectionis used to create anNdbobject. Prior to NDB 7.3.8 and NDB 7.4.3, it was possible to delete theNdb_cluster_connectionused to create a given instance ofNdbwithout first deleting the dependentNdbobject. (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 thisNdb_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 multipleNdbobjects.
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 newNdbobjects.
set_auto_reconnect() Enables or disables auto-reconnection of API nodes using thisNdb_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 thendbinfo.processestable
set_timeout() Sets a connection timeout
unlock_ndb_objects() Enables the creation of newNdbobjects.
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.

Ndb_cluster_connection Class Constructor

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 anNdbobject. Thus, every NDB API application requires the use of anNdb_cluster_connection.

Signatures

Ndb_cluster_connectionhas 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 singleconnection_stringparameter, pointing to the location of the management server.

The second version of the constructor takes two arguments, aconnection_stringand the node ID (force_api_nodeid) to be used by this API node. This node ID overrides any node ID value set in theconnection_stringargument.

Return value

An instance ofNdb_cluster_connection.

Ndb_cluster_connection::connect()

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:

  • retriesspecifies the number of times to retry the connection in the event of failure. The default value is 30.

    0means that no additional attempts to connect are made in the event of failure; using a negative value forretriesresults in the connection attempt being repeated indefinitely.

  • Thedelayrepresents the number of seconds between reconnect attempts; the default is1second.

  • verboseindicates whether the method should output a report of its progress, with1causing this reporting to be enabled; the default is0(reporting disabled).

Return value

This method returns anint, 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.

Ndb_cluster_connection::get_auto_reconnect()

Description

This method retrieves the currentAutoReconnectsetting 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 value0or1, corresponding to the currentAutoReconnectsetting in effect for for this connection.0forces API nodes to use new connections to the cluster, while1enables API nodes to re-use existing connections.

Ndb_cluster_connection::get_latest_error()

Description

This method can be used to determine whether or not the most recentconnect()attempt made by thisNdb_cluster_connectionsucceeded . 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

1or0. A return value of1indicates that the latest attempt to connect failed; if the attempt succeeded, a0is returned.

Ndb_cluster_connection::get_latest_error_msg()

Description

If the most recent connection attempt by thisNdb_cluster_connectionfailed (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 byNdb_cluster_connection::connect(). If the most recent connection attempt succeeded, an empty string is returned.

Ndb_cluster_connection::get_max_adaptive_send_time()

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.

Ndb_cluster_connection::get_next_ndb_object()

Description

This method is used to iterate over a set ofNdbobjects, retrieving them one at a time.

Signature
const Ndb* get_next_ndb_object ( const Ndb*p)
Parameters

This method takes a single parameter, a pointer to the lastNdbobject to have been retrieved orNULL.

Return value

返回下一个Ndbobject, orNULLif no moreNdbobjects are available.

Iterating over Ndb objects.To retrieve all existingNdbobjects, perform the following three steps:

  1. Invoke thelock_ndb_objects()method. This prevents the creation of any new instances ofNdbuntil theunlock_ndb_objects()method is called.

  2. Retrieve the first availableNdbobject by passingNULLtoget_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 availableNdbinstance is used, the method returnsNULL.

  3. After you have retrieved all desiredNdbobjects, you should re-enableNdbobject creation by calling theunlock_ndb_objects()method.

Ndb_cluster_connection::get_num_recv_threads()

Description

Get the number of receiver threads.

Signature
int get_num_recv_threads ( void ) const
Parameters

None.

Return value

The number of receiver threads.

Ndb_cluster_connection::get_recv_thread_activation_threshold()

Description

Get the level set for activating the receiver thread bound byset_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.

Ndb_cluster_connection::get_system_name()

Description

Gets the system name from the cluster configuration. This is the value of theNamesystem configuration parameter set in the cluster'sconfig.iniconfiguration 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 formMC_timestamp(for example,MC_20170426182343), using the time that the management server was started.

ndb_cluster_connection::lock_ndb_objects()

Description

Calling this method prevents the creation of new instances of theNdbclass. This method must be called prior to iterating over multipleNdbobjects usingget_next_ndb_object().

Signature
void lock_ndb_objects ( void ) const

This method was madeconstin 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.

Ndb_cluster_connection::set_auto_reconnect()

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 settingAutoReconnect = 1in theconfig.inifile or calling this method with 1 as the input value. Calling the method with 0 for the value has the same effect as setting theAutoReconnectconfiguration parameter (also introduced in those NDB Cluster versions) to 0; that is, API nodes are forced to create new connections.

Important

When called, this method overrides any setting forAutoReconnectmade in theconfig.inifile.

For more information, seeDefining SQL and Other API Nodes in an NDB Cluster.

Signature
void set_auto_reconnect ( intvalue)
Parameters

Avalueof 0 or 1 which determines API node reconnection behavior. 0 forces API nodes to use new connections (Ndb_cluster_connectionobjects); 1 permits API nodes to re-use existing connections to the cluster.

Return value

None.

Ndb_cluster_connection::set_data_node_neighbour()

Description

Set data node neighbor of the connection, used for optimal placement of the transaction coordinator. This method be used after creating theNdb_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 thendb_data_node_neighbourserver 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.

Ndb_cluster_connection::set_max_adaptive_send_time()

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.

Ndb_cluster_connection::set_name()

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

Thenameto be used as an identifier for the connection.

Return value

None.

Ndb_cluster_connection::set_num_recv_threads()

Description

Set the number of receiver threads bound to the CPU (or CPUs) determined usingset_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 is1.

Return value

-1indicates an error; any other value indicates success.

Ndb_cluster_connection::set_optimized_node_selection()

Description

This method can be used to override theconnect()method's default behavior as regards which node should be connected to first.

Signature
void set_optimized_node_selection ( intvalue)
Parameters

An integervalue.

Return value

None.

Ndb_cluster_connection::set_recv_thread_activation_threshold()

Description

Set the level for activating the receiver thread bound byset_recv_thread_cpu(). Below this level, normal user threads are used to receive signals.

Signature
int集_recv_thread_activation_threshold ( Uint32threshold)
Parameters

An integerthreshold价值。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

-1indicates an error; any other value indicates success.

Ndb_cluster_connection::set_service_uri()

Description

Beginning with NDB 7.5.7 and NDB 7.6.2, this method can be used to create a URI for publication inservice_URIcolumn of the the application's row in thendbinfo.processestable.

Provided that this method is called prior to invokingconnect(), the service URI is published immediately upon connection; otherwise, it is published after a delay of up toHeartbeatIntervalDbApimilliseconds.

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_connectionthat uses multiple transporters or network addresses to connect to different data nodes is reflected in multiple rows in thendbinfo.processestable.

  • 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.

Ndb_cluster_connection::set_recv_thread_cpu()

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 invokingset_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 is0.

Return value

-1indicates an error; any other value indicates success.

Ndb_cluster_connection::set_timeout()

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 functionndb_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.

Ndb_cluster_connection::unlock_ndb_objects()

Description

This method undoes the effects of thelock_ndb_objects()method, making it possible to create new instances ofNdb.unlock_ndb_objects()should be called after you have finished retrievingNdbobjects using theget_next_ndb_object()method.

Signature
void unlock_ndb_objects ( void ) const

This method was madeconstin 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.

Ndb_cluster_connection::unset_recv_thread_cpu()

Description

Unset the CPU or CPUs to which the receiver thread was bound usingset_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

-1indicates an error; any other value indicates success.

Ndb_cluster_connection::wait_until_ready()

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:

  • timeoutBeforedetermines the number of seconds to wait until the firstlivenode is detected. If this amount of time is exceeded with no live nodes detected, then the method immediately returns a negative value.

  • timeoutAfterdetermines the number of seconds to wait after the firstlivenode 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 arelive.

  • > 0: At least one node islive(however, it is not known whetherallnodes arelive).

  • < 0: An error occurred.