|
|
package com.platform.service.impl;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.platform.dao.VolumeDao;
|
|
|
import com.platform.entities.Brick;
|
|
|
import com.platform.entities.VolumeEntity;
|
|
|
import com.platform.entities.VolumeInitEntity;
|
|
|
import com.platform.glusterfs.SetVolume;
|
|
|
import com.platform.service.IGfsService;
|
|
|
import com.platform.service.IVolumeService;
|
|
|
|
|
|
@Service(value = "volumeService")
|
|
|
public class VolumeServiceImpl implements IVolumeService {
|
|
|
|
|
|
/** gfs的api */
|
|
|
SetVolume volumeService = new SetVolume();
|
|
|
|
|
|
@Resource(name = "gfsService")
|
|
|
private IGfsService gfsService;
|
|
|
|
|
|
@Resource(name = "volumeDao")
|
|
|
private VolumeDao volumeDao;
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public int save(VolumeEntity entity) throws Exception {
|
|
|
int rest = 1;
|
|
|
//createVolume("lili_test1", 0, "distributed", bricksToCreate, "/home/lili_test1_point")
|
|
|
List<VolumeEntity> result = gfsService.getAllVolumes();
|
|
|
List<VolumeEntity> addVolumes = new ArrayList<VolumeEntity>();
|
|
|
VolumeEntity volumeTmp = null;
|
|
|
if (null != result) {
|
|
|
boolean isExits = false;
|
|
|
//.trim() 去掉空格
|
|
|
for (VolumeEntity volumeOnServer : result) {
|
|
|
if(entity.getName().trim().equals(volumeOnServer.getName().trim())){
|
|
|
isExits = true;
|
|
|
//TODO 服务器上有该volume,
|
|
|
volumeTmp = volumeOnServer;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
//如果服务器上没有该volume,
|
|
|
if (!isExits) {
|
|
|
addVolumes.add(entity);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
boolean isContinue = true;
|
|
|
// 对比volume信息--原数据没有该volume,则新增volume:
|
|
|
for (VolumeEntity add : addVolumes) {
|
|
|
List<String> bristr = new ArrayList<String>();
|
|
|
if (null == add.getName() || "".equals(add.getName())) {
|
|
|
continue;
|
|
|
}
|
|
|
if (null == add.getPath() || "".equals(add.getPath())) {
|
|
|
continue;
|
|
|
}
|
|
|
for ( Brick bri : add.getBrick()) {
|
|
|
if (null == bri.getIp() || "".equals(bri.getIp())) {
|
|
|
continue;
|
|
|
}
|
|
|
if (null == bri.getPath() || "".equals(bri.getPath())) {
|
|
|
continue;
|
|
|
}
|
|
|
bristr.add(bri.getIp()+":"+bri.getPath());
|
|
|
};
|
|
|
|
|
|
//创建volume
|
|
|
if (bristr.size() > 0) {
|
|
|
int createreslt = volumeService.createVolume(add.getName(), 0, "distributed", bristr, add.getPath());
|
|
|
if (createreslt != 1) {
|
|
|
rest = createreslt;
|
|
|
}
|
|
|
}
|
|
|
//记录volume信息,
|
|
|
VolumeInitEntity volInSql = new VolumeInitEntity();
|
|
|
volInSql.setName(add.getName());
|
|
|
volInSql.setPath(add.getPath());
|
|
|
volumeDao.save(volInSql);
|
|
|
isContinue = false;
|
|
|
}
|
|
|
// 修改 brick
|
|
|
if (isContinue && null != volumeTmp) {
|
|
|
List<Brick> newBricks = entity.getBrick();
|
|
|
List<Brick> serverBricks = volumeTmp.getBrick();
|
|
|
Map<String, Brick> newMap = new HashMap<String, Brick>();
|
|
|
Map<String, Brick> serverMap = new HashMap<String, Brick>();
|
|
|
for (Brick brick : newBricks) {
|
|
|
//IP + path 才 确认唯一的 brick
|
|
|
newMap.put(brick.getIp().trim() + ":" + brick.getPath().trim(), null);
|
|
|
}
|
|
|
for (Brick brick : serverBricks) {
|
|
|
serverMap.put(brick.getIp().trim() + ":" + brick.getPath().trim(), null);
|
|
|
}
|
|
|
// ----对比volume信息--原数据有该volume,则对比brick信息,确认那几个brick是新增的,那几个brick是待删除的----start
|
|
|
Set<String> newBrickKeys = newMap.keySet();
|
|
|
Set<String> serverBrickKeys = serverMap.keySet();
|
|
|
//新增的Brick的 Keys
|
|
|
List<String> newKeys = new ArrayList<String>();
|
|
|
newBrickKeys.removeAll(serverBrickKeys);
|
|
|
newKeys.addAll(newBrickKeys);
|
|
|
// 新增brick s,
|
|
|
if (newKeys.size() > 0) {
|
|
|
int createreslt = volumeService.addBrickVolume(entity.getName(), newKeys, 0, "distributed");
|
|
|
if (createreslt != 1) {
|
|
|
rest = createreslt;
|
|
|
}
|
|
|
}
|
|
|
//待删除的Brick的 Keys
|
|
|
for (Brick brick : newBricks) {
|
|
|
//IP + path 才 确认唯一的 brick
|
|
|
newMap.put(brick.getIp().trim() + ":" + brick.getPath().trim(), null);
|
|
|
}
|
|
|
newBrickKeys = newMap.keySet();
|
|
|
serverBrickKeys.removeAll(newBrickKeys);
|
|
|
List<String> deleteKeys = new ArrayList<String>();
|
|
|
deleteKeys.addAll(serverBrickKeys);
|
|
|
// 删除brick s,
|
|
|
if (deleteKeys.size() > 0) {
|
|
|
int createreslt = volumeService.deleteBrickVolume(entity.getName(), deleteKeys, 0, "distributed");
|
|
|
if (createreslt != 1) {
|
|
|
rest = createreslt;
|
|
|
}
|
|
|
}
|
|
|
// ---对比volume信息--原数据有该volume,则对比brick信息,确认那几个brick是新增的,那几个brick是待删除的----- end
|
|
|
|
|
|
//TODO 查看状态 状态Started,Stopped,Created
|
|
|
if (volumeTmp.isStatus() != entity.isStatus()) {
|
|
|
if (entity.isStatus()) {
|
|
|
this.start(entity);
|
|
|
}
|
|
|
else{
|
|
|
this.stop(entity);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return rest;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int delete(VolumeEntity entity) throws Exception {
|
|
|
if (null == entity.getName() || "".equals(entity.getName())) {
|
|
|
return -1;
|
|
|
}
|
|
|
int rest = volumeService.deleteVolume(entity.getName());
|
|
|
volumeDao.remove(entity.getName());
|
|
|
return rest;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int start(VolumeEntity entity) throws Exception {
|
|
|
if (null == entity.getName() || "".equals(entity.getName())) {
|
|
|
return -1;
|
|
|
}
|
|
|
return volumeService.startVolume(entity.getName());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int stop(VolumeEntity entity) throws Exception {
|
|
|
if (null == entity.getName() || "".equals(entity.getName())) {
|
|
|
return -1;
|
|
|
}
|
|
|
return volumeService.stopVolume(entity.getName());
|
|
|
}
|
|
|
|
|
|
}
|