Sequoiadb test experience Series 6-Java development 3

Source: Internet
Author: User
In the previous note, operations such as delete, update, and query in the set are updated. Try SequoiaDB's cluster operation this time. This includes the creation and deletion of the replication group, the start and stop of the replication group, the addition, deletion, start and stop of nodes in the replication group, and the acquisition of master and slave nodes in the replication group. Importjava. util. ArrayList; importjava. util. L

In the previous note, operations such as delete, update, and query in the set are updated. Try SequoiaDB's cluster operation this time. This includes the creation and deletion of the replication group, the start and stop of the replication group, the addition, deletion, start and stop of nodes in the replication group, and the acquisition of master and slave nodes in the replication group. Import java. util. ArrayList; import java. util. L

In the previous note, operations such as delete, update, and query in the set are updated. Try SequoiaDB's cluster operation this time. This includes the creation and deletion of the replication group, the start and stop of the replication group, the addition, deletion, start and stop of nodes in the replication group, and the acquisition of master and slave nodes in the replication group.

Import java. util. arrayList; import java. util. list; import org. bson. BSONObject; import org. bson. basicBSONObject; import com. sequoiadb. base. node. nodeStatus; import com. sequoiadb. base. DBCursor; import com. sequoiadb. base. node; import com. sequoiadb. base. replicaGroup; import com. sequoiadb. base. sequoiadb; import com. sequoiadb. exception. baseException; public class BlogRG {static String rgName = "testRG"; static Strin G hostName = "sdbserver1"; public static void main (String [] args) {// connect to the database String host = "192.168.20.46"; String port = "11810 "; string usr = "admin"; String password = "admin"; Sequoiadb sdb = null; try {sdb = new Sequoiadb (host + ":" + port, usr, password );} catch (BaseException e) {e. printStackTrace (); System. exit (1);} // print information about the replication group (name, number) printGroupInfo (sdb); // clear the environment and delete the duplicate replication group if (isGroupExist (sdb, rgName) {Sys Tem. out. println ("Removing the old replica group... "); sdb. removeReplicaGroup (rgName);} printGroupInfo (sdb); // Add a new replication group System. out. println ("Adding the new replica group... "); ReplicaGroup rg = sdb. createReplicaGroup (rgName); printGroupInfo (sdb); // print the node information in the replication group System. out. println ("Tere are" + rg. getNodeNum (NodeStatus. SDB_NODE_ALL) + "nodes in the group. "); // Add three new nodes Node node1 = addNode (rg, 50000); Node node2 = AddNode (rg, 50010); Node node3 = addNode (rg, 50020); // print the Node information in the replication group System. out. println ("Tere are" + rg. getNodeNum (NodeStatus. SDB_NODE_ALL) + "nodes in the group. "); // obtain the master-slave Node of the replication group, Node master = rg. getMaster (); System. out. println ("The master node is" + master. getPort (); System. out. println ("The slave node is" + rg. getSlave (). getPort (); // stop the master node System. out. println ("stoping the master node... "); master. stop (); // Wait for the master node to stop while (rg. getMaster (). getPort () = master. getPort () {try {Thread. sleep (2000);} catch (Exception e) {}// view the newly elected master node System. out. println ("re-selecting the master node... "); System. out. println ("The master node is" + rg. getMaster (). getPort ();} private static void printGroupInfo (Sequoiadb sdb) {ArrayList names = sdb. getReplicaGroupNames (); int count = 0; System. out. print ("The replica groups are"); for (Ob Ject name: names) {count ++; System. out. print (String) name + ",");} System. out. println ("\ nThere are" + count + "replica groups in total. ");} private static boolean isGroupExist (Sequoiadb sdb, String rgName) {ArrayList names = sdb. getReplicaGroupNames (); for (Object name: names) {if (rgName. equals (String) name) return true;} return false;} private static Node addNode (ReplicaGroup rg, int port) {if (rg. getN Ode (hostName, port )! = Null) rg. removeNode (hostName, port, null); Node node = rg. createNode (hostName, port, "/opt/sequoiadb/database/test/" + port, null); System. out. println ("starting the node" + port + "... "); node. start (); return node ;}}

The above Code adds a new replication group to the database and adds three master nodes to the new replication group. The data group automatically selects a new master node. After the master node is stopped, re-elect the new master node in the replication group.

After running the above Code, the result is:

The replica groups are SYSCatalogGroup, datagroup, testRG, There are 3 replica groups in total.Removing the old replica group...The replica groups are SYSCatalogGroup, datagroup, There are 2 replica groups in total.Adding the new replica group...The replica groups are SYSCatalogGroup, datagroup, testRG, There are 3 replica groups in total.Tere are 0 nodes in the group.starting the node 50000...starting the node 50010...starting the node 50020...Tere are 3 nodes in the group.The master node is 50000The slave node is 50010stoping the master node...re-selecting the master node...The master node is 50020
As you can see, when the program starts to run, there are three replication groups in the database, testRG is the useless replication group left over from the last run, and the other two are the default two replication groups of the database. Use the removeReplicaGroup () method to delete redundant testRG replication groups, then use createReplicaGroup () to add new testRG replication groups, and use createNode () and start () in the new group () add and start three new nodes: 50020, and. Use the getMaster () and getSlave () methods to obtain the master and slave nodes in the group, and use stop () to stop master node 50000. After the master node is completely stopped, the group will automatically elect a new master node 50020.

After running, you can view the details of the database testRG copy group on the shell console:

>rg.getDetail(){  "Group": [    {      "HostName": "sdbserver1",      "dbpath": "/opt/sequoiadb/database/test/50000",      "Service": [        {          "Type": 0,          "Name": "50000"        },        {          "Type": 1,          "Name": "50001"        },        {          "Type": 2,          "Name": "50002"        }      ],      "NodeID": 1053    },    {      "HostName": "sdbserver1",      "dbpath": "/opt/sequoiadb/database/test/50010",      "Service": [        {          "Type": 0,          "Name": "50010"        },        {          "Type": 1,          "Name": "50011"        },        {          "Type": 2,          "Name": "50012"        }      ],      "NodeID": 1054    },    {      "HostName": "sdbserver1",      "dbpath": "/opt/sequoiadb/database/test/50020",      "Service": [        {          "Type": 0,          "Name": "50020"        },        {          "Type": 1,          "Name": "50021"        },        {          "Type": 2,          "Name": "50022"        }      ],      "NodeID": 1055    }  ],  "GroupID": 1023,  "GroupName": "testRG",  "PrimaryNode": 1055,  "Role": 0,  "Status": 0,  "Version": 4,  "_id": {    "$oid": "53d9d38e14a63a88c621edd8"  }}Return 1 row(s).Takes 0.4716s.
It can be seen that there are three nodes in the group. The master node (PrimaryNode) is 1055, that is, 50020.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.