首页 > 代码库 > Sequoiadb 测试体验系列之六 – Java 开发3

Sequoiadb 测试体验系列之六 – Java 开发3

上一篇笔记中更新了在集合中的删除,更新,查询等操作。这次尝试一下SequoiaDB的集群操作。包括复制组的创建和删除,复制组的启动和停止,在复制组中添加、删除、启动、停止节点,获取复制组中的主从节点等。

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) {

		
		//连接数据库
		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);
		}
		
		//打印有关复制组信息(名称、个数)
		printGroupInfo(sdb);
		
		//清除环境,删掉重复的复制组
		if(isGroupExist(sdb,rgName)){
			System.out.println("Removing the old replica group...");
			sdb.removeReplicaGroup(rgName);
		}
		
		printGroupInfo(sdb);
		
		//添加新复制组
		System.out.println("Adding the new replica group...");
		ReplicaGroup rg = sdb.createReplicaGroup(rgName);
		
		printGroupInfo(sdb);
		
		//打印复制组内节点信息
		System.out.println("Tere are " + rg.getNodeNum(NodeStatus.SDB_NODE_ALL) + " nodes in the group.");
		
		//添加三个新的节点
		Node node1 = addNode(rg,50000);
		Node node2 = addNode(rg,50010);
		Node node3 = addNode(rg,50020);
		
		//打印复制组内节点信息
		System.out.println("Tere are " + rg.getNodeNum(NodeStatus.SDB_NODE_ALL) + " nodes in the group.");

		//获取复制组的主从节点
		Node master = rg.getMaster();
		System.out.println("The master node is " +master.getPort());
		System.out.println("The slave node is " + rg.getSlave().getPort());
		
		//停止主节点
		System.out.println("stoping the master node...");
		master.stop();
		
		//等待主节点停止
		while(rg.getMaster().getPort() == master.getPort()){
			try{
				Thread.sleep(2000);
			} catch (Exception e){
			}
		}

		//查看新选举的主节点
		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 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

可以看出,程序开始运行时,数据库内有三个复制组,其中testRG是上次运行遗留下来的无用复制组,另外两个是数据库默认的两个复制组。通过removeReplicaGroup()方法删除多余的testRG复制组,之后通过createReplicaGroup()添加新的testRG复制组,并且在新建的组内通过createNode()和start()添加并启动50000,50010,50020三个新的节点。通过getMaster()和getSlave()方法获取组内的主从节点,并利用stop()停止主节点50000,主节点完全停止后,组内将重新自动选举出新的主节点50020。

运行完成后,通过shell控制台查看数据库testRG复制组的详细情况:

>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.
可以看出组内有三个节点,主节点(PrimaryNode)为1055,也就是50020。