Creating a replication cluster

To create a replication cluster you should set at least its name.

In case of a single cluster or if you are creating the first cluster, path option may be omitted, in this case data_dir option will be used as the cluster path. For all subsequent clusters you need to specify path and this path should be available. nodes option may be also set to enumerate all the nodes in the cluster.

  • SQL
  • JSON
  • PHP
  • Python
  • javascript
  • Java

SQL JSON PHP Python javascript Java

  1. CREATE CLUSTER posts
  2. CREATE CLUSTER click_query '/var/data/click_query/' as path
  3. CREATE CLUSTER click_query '/var/data/click_query/' as path, 'clicks_mirror1:9312,clicks_mirror2:9312,clicks_mirror3:9312' as nodes
  1. POST /cli -d "
  2. CREATE CLUSTER posts
  3. "
  4. POST /cli -d "
  5. CREATE CLUSTER click_query '/var/data/click_query/' as path
  6. "
  7. POST /cli -d "
  8. CREATE CLUSTER click_query '/var/data/click_query/' as path, 'clicks_mirror1:9312,clicks_mirror2:9312,clicks_mirror3:9312' as nodes
  9. "
  1. $params = [
  2. 'cluster' => 'posts',
  3. ]
  4. ];
  5. $response = $client->cluster()->create($params);
  6. $params = [
  7. 'cluster' => 'click_query',
  8. 'body' => [
  9. 'path' => '/var/data/click_query/'
  10. ]
  11. ]
  12. ];
  13. $response = $client->cluster()->create($params);
  14. $params = [
  15. 'cluster' => 'click_query',
  16. 'body' => [
  17. 'path' => '/var/data/click_query/',
  18. 'nodes' => 'clicks_mirror1:9312,clicks_mirror2:9312,clicks_mirror3:9312'
  19. ]
  20. ]
  21. ];
  22. $response = $client->cluster()->create($params);
  1. utilsApi.sql('CREATE CLUSTER posts')
  2. utilsApi.sql('CREATE CLUSTER click_query \'/var/data/click_query/\' as path')
  3. utilsApi.sql('CREATE CLUSTER click_query \'/var/data/click_query/\' as path, \'clicks_mirror1:9312,clicks_mirror2:9312,clicks_mirror3:9312\' as nodes')
  1. res = await utilsApi.sql('CREATE CLUSTER posts');
  2. res = await utilsApi.sql('CREATE CLUSTER click_query \'/var/data/click_query/\' as path');
  3. res = await utilsApi.sql('CREATE CLUSTER click_query \'/var/data/click_query/\' as path, \'clicks_mirror1:9312,clicks_mirror2:9312,clicks_mirror3:9312\' as nodes');
  1. utilsApi.sql("CREATE CLUSTER posts");
  2. utilsApi.sql("CREATE CLUSTER click_query '/var/data/click_query/' as path");
  3. utilsApi.sql("CREATE CLUSTER click_query '/var/data/click_query/' as path, 'clicks_mirror1:9312,clicks_mirror2:9312,clicks_mirror3:9312' as nodes");

If a cluster is created without the nodes option, the first node that gets joined to the cluster will be saved as nodes.

Joining a replication cluster

To join an existing cluster you should specify at least:

  • name
  • and host:port of another working node of the cluster you are joining
  • SQL
  • JSON
  • PHP
  • Python
  • javascript
  • Java

SQL JSON PHP Python javascript Java

  1. JOIN CLUSTER posts AT '10.12.1.35:9312'
  1. POST /cli -d "
  2. JOIN CLUSTER posts AT '10.12.1.35:9312'
  3. "
  1. $params = [
  2. 'cluster' => 'posts',
  3. 'body' => [
  4. '10.12.1.35:9312'
  5. ]
  6. ];
  7. $response = $client->cluster->join($params);
  1. utilsApi.sql('JOIN CLUSTER posts AT \'10.12.1.35:9312\'')
  1. res = await utilsApi.sql('JOIN CLUSTER posts AT \'10.12.1.35:9312\'');
  1. utilsApi.sql("JOIN CLUSTER posts AT '10.12.1.35:9312'");

Response

  1. {u'error': u'', u'total': 0, u'warning': u''}
  1. {"total":0,"error":"","warning":""}

In case of a single replication cluster, i.e. in most cases the above is just enough. In case you are creating multiple replication clusters path needs to be set as well and the directory should be available.

  • SQL

SQL

  1. JOIN CLUSTER c2 at '127.0.0.1:10201' 'c2' as path

A node joins a cluster by getting data from another specified node and, if successful, it updates node lists in all other cluster nodes similar to how it’s done manually via ALTER CLUSTER … UPDATE nodes. This list is used to rejoin nodes to the cluster on restart.

There are two lists of nodes:

  • cluster_<name>_nodes_set: used to rejoin nodes to the cluster on restart, it is updated across all nodes same way as ALTER CLUSTER … UPDATE nodes does. JOIN CLUSTER does the same update automatically. Cluster status shows this list as cluster_<name>_nodes_set.
  • cluster_<name>_nodes_view: list of all active nodes used for replication. This list doesn’t require manual management. ALTER CLUSTER … UPDATE nodes actually copies this list of nodes to the list of nodes used to rejoin on restart. Cluster status shows this list as cluster_<name>_nodes_view.

When nodes are located in different network segments or in different datacenters, nodes option may be set explicitly. That allows to minimize traffic between nodes and to use gateway nodes for datacenters intercommunication. The following command joins an existing cluster using the nodes option.

Note: that when this syntax is used, cluster_<name>_nodes_set list is not updated automatically. Use ALTER CLUSTER … UPDATE nodes to update it.

  • SQL
  • JSON
  • PHP
  • Python
  • javascript
  • Java

SQL JSON PHP Python javascript Java

  1. JOIN CLUSTER click_query 'clicks_mirror1:9312;clicks_mirror2:9312;clicks_mirror3:9312' as nodes
  1. POST /cli -d "
  2. JOIN CLUSTER click_query 'clicks_mirror1:9312;clicks_mirror2:9312;clicks_mirror3:9312' as nodes
  3. "
  1. $params = [
  2. 'cluster' => 'posts',
  3. 'body' => [
  4. 'nodes' => 'clicks_mirror1:9312;clicks_mirror2:9312;clicks_mirror3:9312'
  5. ]
  6. ];
  7. $response = $client->cluster->join($params);
  1. utilsApi.sql('JOIN CLUSTER click_query \'clicks_mirror1:9312;clicks_mirror2:9312;clicks_mirror3:9312\' as nodes')
  1. res = await utilsApi.sql('JOIN CLUSTER click_query \'clicks_mirror1:9312;clicks_mirror2:9312;clicks_mirror3:9312\' as nodes');
  1. utilsApi.sql("JOIN CLUSTER click_query 'clicks_mirror1:9312;clicks_mirror2:9312;clicks_mirror3:9312' as nodes");

Response

  1. {u'error': u'', u'total': 0, u'warning': u''}
  1. {"total":0,"error":"","warning":""}

JOIN CLUSTER works synchronously and completes as soon as the node receives all data from the other nodes in the cluster and is in sync with them.

Deleting a replication cluster

Delete statement removes a cluster specified with name. The cluster gets removed from all the nodes, but its tables are left intact and become active local non-replicated tables.

  • SQL
  • JSON
  • PHP
  • Python
  • javascript
  • Java

SQL JSON PHP Python javascript Java

  1. DELETE CLUSTER click_query
  1. POST /cli -d "DELETE CLUSTER click_query"
  1. $params = [
  2. 'cluster' => 'click_query',
  3. 'body' => []
  4. ];
  5. $response = $client->cluster()->delete($params);
  1. utilsApi.sql('DELETE CLUSTER click_query')
  1. res = await utilsApi.sql('DELETE CLUSTER click_query');
  1. utilsApi.sql("DELETE CLUSTER click_query");

Response

  1. {u'error': u'', u'total': 0, u'warning': u''}
  1. {"total":0,"error":"","warning":""}