|
|
/**
|
|
|
* @author 李乾坤
|
|
|
* 进行volume的一系列操作,如创建、开启停止volume,为volume添加或删除brick
|
|
|
*/
|
|
|
package com.platform.http.gfs;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import net.sf.json.JSONArray;
|
|
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
|
|
import com.base.MyException;
|
|
|
import com.base.PostData;
|
|
|
import com.google.gson.Gson;
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
import com.platform.entities.FolderNode;
|
|
|
import com.platform.entities.VolumeDataEntity;
|
|
|
import com.platform.http.HttpUtils;
|
|
|
import com.platform.http.VolumeEntity;
|
|
|
import com.platform.utils.BeanCopy;
|
|
|
|
|
|
public class SetVolume {
|
|
|
public static Logger log = Logger.getLogger(SetVolume.class);
|
|
|
HttpUtils ht = new HttpUtils();
|
|
|
|
|
|
/** 查询volume信息(包括其下目录)
|
|
|
* @return json字符串
|
|
|
*/
|
|
|
public String getAllvolume() {
|
|
|
List<VolumeDataEntity> datas = new ArrayList<VolumeDataEntity>();
|
|
|
try {
|
|
|
datas = getAllvolumeEntity();
|
|
|
} catch (Exception e) {
|
|
|
log.error(e);
|
|
|
}
|
|
|
if (datas.size() == 0) {
|
|
|
return "";
|
|
|
}
|
|
|
JSONArray jsonarr = JSONArray.fromObject(datas);
|
|
|
return jsonarr.toString();
|
|
|
}
|
|
|
|
|
|
/** 查询volume信息(包括其下目录)
|
|
|
* @return json字符串
|
|
|
*/
|
|
|
public List<VolumeDataEntity> getAllvolumeEntity() throws Exception{
|
|
|
List<VolumeDataEntity> volumeList = new ArrayList<VolumeDataEntity>();
|
|
|
String rest = ht.sendPost("gfs/getAllVolume", null);
|
|
|
if (null == rest || "".equals(rest)) {
|
|
|
log.error(" --- gluster is disconnect ! \n");
|
|
|
return volumeList;
|
|
|
}
|
|
|
// JSONObject json = JSONObject.fromObject(rest);
|
|
|
|
|
|
Gson gs = new Gson();
|
|
|
PostData data = gs.fromJson(rest, (new PostData(new ArrayList<VolumeEntity>())).getClass());
|
|
|
List<VolumeEntity> volumes = (List<VolumeEntity>) data.getData();
|
|
|
if(null == volumes){
|
|
|
log.error("-----gfs/getAllVolume----- no volume ----");
|
|
|
getExcept(data);
|
|
|
return volumeList;
|
|
|
}
|
|
|
//解析 PostData
|
|
|
if(volumes.size() > 0){
|
|
|
JSONArray jsonarr = JSONArray.fromObject(data.getData());
|
|
|
try {
|
|
|
volumes = gs.fromJson(jsonarr.toString(), new TypeToken<List<VolumeEntity>>(){}.getType());
|
|
|
} catch (Exception e) {
|
|
|
log.error(e);
|
|
|
}
|
|
|
}
|
|
|
for (VolumeEntity volumeEntity : volumes) {
|
|
|
VolumeDataEntity entity = new VolumeDataEntity();
|
|
|
BeanCopy.copyBean(volumeEntity, entity, "folder", "status");
|
|
|
if (null != volumeEntity.getFolder()) {
|
|
|
List<FolderNode> fn = new ArrayList<FolderNode>();
|
|
|
fn.add(volumeEntity.getFolder());
|
|
|
entity.setFolder(fn);
|
|
|
if ("Started".equals(volumeEntity.getStatus())) {
|
|
|
entity.setStatus(true);
|
|
|
}
|
|
|
}
|
|
|
volumeList.add(entity);
|
|
|
}
|
|
|
getExcept(data);
|
|
|
return volumeList;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建volume 返回值:创建并挂载成功 1 1:可以创建 ;-1:brick的ip不在集群中或者未连接; -2 -3
|
|
|
* -4:类型与brick数目不匹配 ; -5 :volumeName 已经存在;-6:挂载点存在且不为空,不能作为挂载点; -7:未知错误
|
|
|
*
|
|
|
* @param volumeName
|
|
|
* @param count
|
|
|
* @param type
|
|
|
* @param bricks
|
|
|
* @param mountPoint
|
|
|
* @return
|
|
|
* @see [类、类#方法、类#成员]
|
|
|
*/
|
|
|
public int createVolume(String volumeName, int count, String type, List<String> bricks, String mountPoint) throws Exception{
|
|
|
int reslut = 0;
|
|
|
log.info("Creat new volume " + volumeName);
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
map.put("volumeName", volumeName);
|
|
|
map.put("count", count);
|
|
|
map.put("type", type);
|
|
|
map.put("bricks", bricks);
|
|
|
map.put("mountPoint", mountPoint);
|
|
|
//请求 gfs 的 web
|
|
|
String rest = ht.sendPost("gfs/createVolume", map);
|
|
|
if (null == rest || "".equals(rest)) {
|
|
|
log.error(" --- gluster is disconnect ! \n");
|
|
|
return 0;
|
|
|
}
|
|
|
Gson gs = new Gson();
|
|
|
PostData data = gs.fromJson(rest, PostData.class);
|
|
|
|
|
|
// 1 : 成功
|
|
|
double copyResult = (double) data.getData();
|
|
|
reslut = (int) copyResult;
|
|
|
getExcept(data);
|
|
|
return reslut;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除volume 1 表示成功 ;-1表示volume name不存在;-2表示停止volume 失败;
|
|
|
* -3表示删除失败,-4表示/gfsAutoMount/mountPoint.record文件不存在
|
|
|
* @param volumeName
|
|
|
* @return
|
|
|
* @see [类、类#方法、类#成员]
|
|
|
*/
|
|
|
public int deleteVolume(String volumeName) throws Exception{
|
|
|
int reslut = 0;
|
|
|
this.stopVolume(volumeName);
|
|
|
try {
|
|
|
Thread.sleep(500);
|
|
|
} catch (Exception e) {
|
|
|
log.info(e);
|
|
|
}
|
|
|
log.info("delete volume " + volumeName);
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
map.put("volumeName", volumeName);
|
|
|
//请求web
|
|
|
String rest = ht.sendPost("gfs/deleteVolume", map);
|
|
|
if (null == rest || "".equals(rest)) {
|
|
|
log.error(" --- gluster is disconnect ! \n");
|
|
|
return 0;
|
|
|
}
|
|
|
Gson gs = new Gson();
|
|
|
PostData data = gs.fromJson(rest, PostData.class);
|
|
|
// 1 : 成功
|
|
|
double copyResult = (double) data.getData();
|
|
|
reslut = (int) copyResult;
|
|
|
getExcept(data);
|
|
|
return reslut;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 为指定的volume添加brick,参数中需要指定类型、数量等 返回值:1成功 ;其他失败
|
|
|
* 过程中需要先检查volume是否存在,还需检查给出的brick数量与类型、count是否相符
|
|
|
*
|
|
|
* @param volumeName
|
|
|
* @param brickName
|
|
|
* @param count
|
|
|
* @param type
|
|
|
* @return
|
|
|
* @see [类、类#方法、类#成员]
|
|
|
*/
|
|
|
public int addBrickVolume(String volumeName, List<String> brickName, int count, String type) throws Exception{
|
|
|
int reslut = 0;
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
for (String string : brickName) {
|
|
|
sb.append(string).append(",");
|
|
|
}
|
|
|
log.info("addBrick Volume " + volumeName+" Brick "+ sb.toString());
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
map.put("volumeName", volumeName);
|
|
|
map.put("bricks", brickName);
|
|
|
//请求web
|
|
|
String rest = ht.sendPost("gfs/addBricks", map);
|
|
|
if (null == rest || "".equals(rest)) {
|
|
|
log.error(" --- gluster is disconnect ! \n");
|
|
|
return 0;
|
|
|
}
|
|
|
Gson gs = new Gson();
|
|
|
PostData data = gs.fromJson(rest, PostData.class);
|
|
|
// 1 : 成功
|
|
|
double copyResult = (double) data.getData();
|
|
|
reslut = (int) copyResult;
|
|
|
getExcept(data);
|
|
|
return reslut;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 为指定的volume删除brick,参数中需要指定类型、数量等 返回值:1 成功 ;其他 失败
|
|
|
* 过程中需要先检查volume是否存在,还需检查给出的brick数量与类型、count是否相符
|
|
|
*
|
|
|
* @param volumeName
|
|
|
* @param brickName
|
|
|
* @param count
|
|
|
* @param type
|
|
|
* @return
|
|
|
* @see [类、类#方法、类#成员]
|
|
|
*/
|
|
|
public int deleteBrickVolume(String volumeName, List<String> brickName, int count, String type) throws Exception {
|
|
|
int reslut = 0;
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
for (String string : brickName) {
|
|
|
sb.append(string).append(",");
|
|
|
}
|
|
|
log.info("deleteBrick Volume " + volumeName+" Brick "+ sb.toString());
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
map.put("volumeName", volumeName);
|
|
|
map.put("bricks", brickName);
|
|
|
//请求web
|
|
|
String rest = ht.sendPost("gfs/deleteBrickForce", map);
|
|
|
if (null == rest || "".equals(rest)) {
|
|
|
log.error(" --- gluster is disconnect ! \n");
|
|
|
return 0;
|
|
|
}
|
|
|
Gson gs = new Gson();
|
|
|
PostData data = gs.fromJson(rest, PostData.class);
|
|
|
// 1 : 成功
|
|
|
double copyResult = (double) data.getData();
|
|
|
reslut = (int) copyResult;
|
|
|
getExcept(data);
|
|
|
return reslut;
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
* 停止指定volume 参数中需给出volume的名字 返回值: 0 成功 -1 失败
|
|
|
* 需要先检查volume是否存在,然后判断volume的状态是否已经是停止状态
|
|
|
*/
|
|
|
public int stopVolume(String volumeName) throws Exception{
|
|
|
int reslut = 0;
|
|
|
log.info("stop volume " + volumeName);
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
map.put("volumeName", volumeName);
|
|
|
//请求web
|
|
|
String rest = ht.sendPost("gfs/stopVolume", map);
|
|
|
if (null == rest || "".equals(rest)) {
|
|
|
log.error(" --- gluster is disconnect ! \n");
|
|
|
return 0;
|
|
|
}
|
|
|
Gson gs = new Gson();
|
|
|
PostData data = gs.fromJson(rest, PostData.class);
|
|
|
// 1 : 成功
|
|
|
double copyResult = (double) data.getData();
|
|
|
reslut = (int) copyResult;
|
|
|
getExcept(data);
|
|
|
return reslut;
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
* 开启指定volume 参数中需给出volume的名字 返回值: 0 成功 -1 失败
|
|
|
* 需要先检查volume是否存在,然后判断volume的状态是否已经是开启状态
|
|
|
*/
|
|
|
public int startVolume(String volumeName) throws Exception {
|
|
|
int reslut = 0;
|
|
|
log.info("start volume " + volumeName);
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
map.put("volumeName", volumeName);
|
|
|
//请求web
|
|
|
String rest = ht.sendPost("gfs/startVolume", map);
|
|
|
if (null == rest || "".equals(rest)) {
|
|
|
log.error(" --- gluster is disconnect ! \n");
|
|
|
return 0;
|
|
|
}
|
|
|
Gson gs = new Gson();
|
|
|
PostData data = gs.fromJson(rest, PostData.class);
|
|
|
// 1 : 成功
|
|
|
double copyResult = (double) data.getData();
|
|
|
reslut = (int) copyResult;
|
|
|
getExcept(data);
|
|
|
return reslut;
|
|
|
}
|
|
|
|
|
|
/** 记录异常信息
|
|
|
* @param pd
|
|
|
*/
|
|
|
private void getExcept(PostData pd) {
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
for ( MyException object : pd.getExceptionsStack()) {
|
|
|
sb.append(object.getMess()).append(",");
|
|
|
}
|
|
|
if (!"".equals(sb.toString())) {
|
|
|
log.error(sb.toString());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// /** json 转 对象
|
|
|
// * @param folds
|
|
|
// * @return
|
|
|
// */
|
|
|
// private List<FolderNode> setChildFolders(List<FolderNode> folds) {
|
|
|
// List<FolderNode> folders = null;
|
|
|
// if (null != folds) {
|
|
|
// JSONArray foldJson = JSONArray.fromObject(folds);
|
|
|
// folders = (List<FolderNode>)JSONArray.toCollection(foldJson, FolderNode.class);
|
|
|
// for (FolderNode folderNode : folders) {
|
|
|
// List<FolderNode> childFolds = folderNode.getChildNodes();
|
|
|
// //递归
|
|
|
// folderNode.setChildNodes(setChildFolders(childFolds));
|
|
|
// }
|
|
|
// }
|
|
|
// return folders;
|
|
|
// }
|
|
|
//
|
|
|
// /** json 转 对象
|
|
|
// * @param bricks
|
|
|
// * @return
|
|
|
// */
|
|
|
// private List<Brick> setBricks(List<Brick> bricks) {
|
|
|
// List<Brick> bs = null;
|
|
|
// if (null != bricks) {
|
|
|
// JSONArray bsJson = JSONArray.fromObject(bricks);
|
|
|
// bs = (List<Brick>)JSONArray.toCollection(bsJson, Brick.class);
|
|
|
// }
|
|
|
// return bs;
|
|
|
// }
|
|
|
}
|