Sequoiadb test experience Series 6-Java Development 3 and sequoiadb 6

Source: Internet
Author: User

Sequoiadb test experience Series 6-Java Development 3 and sequoiadb 6

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 String hostName = "sdbserver1";
public static void main(String[] args) {
//Connect to 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 replication groups (name, number)
printGroupInfo(sdb);
//Clean environment, delete duplicate replication groups
if(isGroupExist(sdb,rgName)){
System.out.println("Removing the old replica group...");
sdb.removeReplicaGroup(rgName);
}
printGroupInfo(sdb);
//Add new replication group
System.out.println("Adding the new replica group...");
ReplicaGroup rg = sdb.createReplicaGroup(rgName);
printGroupInfo(sdb);
//Print node information in 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 node information in replication group
System.out.println("Tere are " + rg.getNodeNum(NodeStatus.SDB_NODE_ALL) + " nodes in the group.");
//Get the primary and secondary nodes 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 master
System.out.println("stoping the master node...");
master.stop();
//Wait for 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 (Object 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.getNode(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 50000
The slave node is 50010
stoping 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.



Now I have 3 years of java Development Experience. What should I do if I turn to test? What test is better?

If you have development experience, you can turn to a development test or automated test. The treatment is no less than development, but you may still need to have some development work, but you also need to take both tests into consideration. I don't know why the landlord wants to switch to test. If you think the test is easier, I advise you not to switch, because I did the test and the work content is very tedious, without patience, it is easy to crash.

3 years of java development experience,

Related Article

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.