glusterfs-api
chenlw 9 years ago
parent cb3af77285
commit 817b99dcb0

@ -0,0 +1,106 @@
package com.platform.glusterfs;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.aspectj.weaver.ast.And;
import com.mysql.jdbc.log.Log;
import com.platform.utils.Constant;
public class CheckoutMD5 {
public static Logger log = Logger.getLogger(CheckoutMD5.class);
String sourcePath;
String destPath;
String dataName;
// String cmd_crateSourceMD5File="find "+sourcePath+dataName+"/app/ -type f
// -print0 | xargs -0 md5sum | sort >"+deskPath+dataName+"_md5.txt";
String cmd_getSourceMD5File;
// String cmd_crateDestMD5File="find "+destPath+dataName+"/app/ -type f
// -print0 | xargs -0 md5sum | sort >"+deskPath+dataName+"_md5.txt";
String cmd_getDestMD5File;
Map<String, String> source_md5 = new HashMap<String, String>();
Map<String, String> dest_md5 = new HashMap<String, String>();
public CheckoutMD5() {
// TODO Auto-generated constructor stub
}
public CheckoutMD5(String sourcePath, String destPath, String dataName) {
// TODO Auto-generated constructor stub
this.sourcePath = sourcePath;
this.destPath = destPath;
this.dataName = dataName;
cmd_getSourceMD5File = "find " + sourcePath + dataName + "/app/ -type f -print0 | xargs -0 md5sum | sort ";
cmd_getDestMD5File = "find " + destPath + dataName + "/app/ -type f -print0 | xargs -0 md5sum | sort ";
}
/**
* sourcePathdestPath1
* 0MD5-1-2-3
*
* @param sourcePath
* @param destPath
* @return
* @see [##]
*/
public int checkoutMD5Folder(String sourcePath, String destPath) {
log.info("start checkout md5 "+sourcePath+" and "+ destPath);
List<String> wrong_files = new ArrayList<String>();
String source_cmd = "find " + sourcePath + " -type f -print0 | xargs -0 md5sum";
String dest_cmd = "find " + destPath + " -type f -print0 | xargs -0 md5sum";
List<String> sourceReStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(source_cmd);
if (sourceReStrings == null || sourceReStrings.size() == 0) {
log.error("get " + sourcePath + " MD5 error!");
return -1;
}
if(sourceReStrings.get(0).contains(Constant.noSuchFile)){
log.error(sourcePath+" is not exist!");
return -2;
}
List<String> destReStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(dest_cmd);
if (destReStrings == null || destReStrings.size() == 0) {
log.error("get " + destReStrings + " MD5 error!");
return -1;
}
if(destReStrings.get(0).contains(Constant.noSuchFile)){
log.error(destPath+" is not exist!");
return -3;
}
Map<String, String> source_md5 = new HashMap<String, String>();
Map<String, String> dest_md5 = new HashMap<String, String>();
for (String line : sourceReStrings) {
String[] lines = line.split(" ");
String key = lines[1].replace(sourcePath, "").trim();
String value = lines[0].trim();
source_md5.put(key, value);
}
for (String line : destReStrings) {
String[] lines = line.split(" ");
String key = lines[1].replace(destPath, "").trim();
String value = lines[0].trim();
dest_md5.put(key, value);
}
for (Map.Entry<String, String> mapEntry : source_md5.entrySet()) {
if (!(dest_md5.containsKey(mapEntry.getKey())
&& dest_md5.get(mapEntry.getKey()).equals(mapEntry.getValue()))) {
log.info(sourcePath + " and " + destPath + " is not same!");
return 0;
// System.out.println(mapEntry.getKey());
}
}
log.info(sourcePath + " and " + destPath + " is same!");
return 1;
}
public static void main(String[] args) {
CheckoutMD5 checkoutMD5 = new CheckoutMD5();
System.out.println(checkoutMD5.checkoutMD5Folder("/home/v1_copy","/home/ubuntu"));
}
}

@ -0,0 +1,40 @@
package com.platform.service;
import java.util.List;
import com.platform.entities.DataInfoEntity;
import com.platform.entities.DataInfoEntityMoveTmp;
/**
* @author chen
*
*/
public interface IMoveDataService {
/**
* @param a dataInfo
* @param dstPath volumefoldereg: XXX/320198_16/1,or XXX/320122KFQ_15/1
* @return
* @throws Exception
*/
public boolean moveData(List<DataInfoEntity> data, String dstPath) throws Exception;
/**
* @return
* @throws Exception
*/
public List<DataInfoEntityMoveTmp> findAll() throws Exception;
/**
* @return
* @throws Exception
*/
public int delete(DataInfoEntityMoveTmp dataMove) throws Exception;
public int update(DataInfoEntityMoveTmp data) throws Exception;
public int save(DataInfoEntityMoveTmp data) throws Exception;
public int insertBatch(List<DataInfoEntityMoveTmp> list) throws Exception;
}

@ -0,0 +1,141 @@
package com.platform.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.platform.dao.DataInfoDao;
import com.platform.dao.DataInfoMoveTmpDao;
import com.platform.entities.DataInfoEntity;
import com.platform.entities.DataInfoEntityMoveTmp;
import com.platform.glusterfs.CheckoutMD5;
import com.platform.glusterfs.CopyData;
import com.platform.glusterfs.ShowData;
import com.platform.service.IMoveDataService;
import com.platform.utils.Bean2MapUtils;
import com.platform.utils.Constant;
import com.platform.utils.DateForm;
@Component
@Service(value = "moveDataService")
public class MoveDataServiceImpl implements IMoveDataService{
@Resource(name = "dataInfoDao")
private DataInfoDao dataInfoDao;
/**
*
*/
CopyData copy = new CopyData();
/**
* MD5
*/
CheckoutMD5 check = new CheckoutMD5();
@Resource(name = "dataInfoMoveTmpDao")
private DataInfoMoveTmpDao dataInfoMoveTmpDao;
/**
*
*/
ShowData show = new ShowData();
@Override
public boolean moveData(List<DataInfoEntity> data, String dstPath) throws Exception {
boolean isSuccess = false;
String tailPath = "";
if (null != data) {
//XXX/320198_16/1,or XXX/320122KFQ_15/1 ---> /320198_16/1, or /320122KFQ_15/1
List<DataInfoEntityMoveTmp> exist = dataInfoMoveTmpDao.findAll();
List<String> existIds = new ArrayList<String>();
if (null != exist) {
for (DataInfoEntityMoveTmp dataInfoEntityMoveTmp : exist) {
if ("0".equals(dataInfoEntityMoveTmp.getCompleteStatus()) || "1".equals(dataInfoEntityMoveTmp.getCompleteStatus())) {
if (null != dataInfoEntityMoveTmp.getDataPath()) {
existIds.add(dataInfoEntityMoveTmp.getDataPath());
}
}
}
}
Pattern pattern = Pattern.compile("\\/\\d+[a-z]*[A-Z]*_\\d+\\/\\d*\\/*$");
// 末尾 含有 /
Pattern pattern2 = Pattern.compile("\\/$");
Matcher matcher2 = pattern2.matcher(dstPath);
//去掉 最后 的 / 符合
if (matcher2.find()) {
dstPath = dstPath.substring(0, dstPath.length()-1);
}
List<DataInfoEntityMoveTmp> moveList = new ArrayList<DataInfoEntityMoveTmp>();
for (DataInfoEntity dataInfoEntity : data) {
if (existIds.contains(dataInfoEntity.getDataPath())) {
continue;
}
//TODO 正则:取出 data 的后面 的 路径eg: XXX/320198_16/1,or XXX/320122KFQ_15/1
Matcher matcher = pattern.matcher(dataInfoEntity.getDataPath());
// tailPath 第一个字符是 / 符号
if (matcher.find()) {
tailPath = matcher.group();
}
String finalDestPath = dstPath + tailPath;
DataInfoEntityMoveTmp dataMove = new DataInfoEntityMoveTmp();
dataMove.setSystemCode(dataInfoEntity.getSystemCode());
dataMove.setRegionalismCode(dataInfoEntity.getRegionalismCode());
dataMove.setDstPath(finalDestPath);
dataMove.setLastTime(DateForm.date2StringBysecond(new Date()));
moveList.add(dataMove);
}
if (moveList.size() > 0) {
dataInfoMoveTmpDao.insertBatch(moveList);
isSuccess = true;
}
}
return isSuccess;
}
@Override
public List<DataInfoEntityMoveTmp> findAll() throws Exception {
List<DataInfoEntityMoveTmp> result = new ArrayList<DataInfoEntityMoveTmp>();
try {
result = dataInfoMoveTmpDao.findAll();
} catch (Exception e) {
System.err.println(e);
}
return result;
}
@Override
public int delete(DataInfoEntityMoveTmp dataMove) throws Exception {
int result = dataInfoMoveTmpDao.remove(dataMove.getId());
return result;
}
@Override
public int save(DataInfoEntityMoveTmp data) throws Exception {
dataInfoMoveTmpDao.save(data);
return 0;
}
@Override
public int insertBatch(List<DataInfoEntityMoveTmp> list) throws Exception {
// TODO Auto-generated method stub
return 0;
}
@Override
public int update(DataInfoEntityMoveTmp data) throws Exception {
int result = dataInfoMoveTmpDao.update(data);
return result;
}
}

@ -0,0 +1,47 @@
package com.platform.service.thread;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.platform.dao.DataInfoMoveTmpDao;
import com.platform.entities.DataInfoEntityMoveTmp;
import com.platform.service.IMoveDataService;
import com.platform.service.impl.MoveDataServiceImpl;
import com.platform.utils.Constant;
public class TreadMoveData2Start extends Thread{
private IMoveDataService dataInfoMove= new MoveDataServiceImpl();
public TreadMoveData2Start() {
}
@Override
public void run() {
boolean isBreak = false;
while(true){
if (isBreak) {
break;
}
try {
List<DataInfoEntityMoveTmp> resultlist = dataInfoMove.findAll();
for (DataInfoEntityMoveTmp moveEntity : resultlist) {
if ("1".equals(moveEntity.getCompleteStatus())) {
moveEntity.setCompleteStatus("3");
dataInfoMove.update(moveEntity);
}
}
isBreak = true;
Thread.sleep(Constant.update_dataInfo_sleep_time);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

@ -0,0 +1,137 @@
package com.platform.utils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateForm {
private static final String date_format_second = "yyyy-MM-dd HH:mm:ss";
private static final String date_format_second_non = "yyyy-MM-dd_HH-mm-ss";
private static final String data_format_min = "yyyy-MM-dd HH:mm";
private static final String data_format_day = "yyyy-MM-dd";
private static ThreadLocal<DateFormat> threadLocal_second = new ThreadLocal<DateFormat>();
private static ThreadLocal<DateFormat> threadLocal_second_non = new ThreadLocal<DateFormat>();
private static ThreadLocal<DateFormat> threadLocal_min = new ThreadLocal<DateFormat>();
private static ThreadLocal<DateFormat> threadLocal_day = new ThreadLocal<DateFormat>();
public static String date2StringBysecond(Date date) {
if (date == null) {
return null;
}
DateFormat format = threadLocal_second.get();
if(format == null){
format = new SimpleDateFormat(date_format_second);
threadLocal_second.set(format);
}
return format.format(date);
}
public static String date2StringBysecondNon(Date date) {
if (date == null) {
return null;
}
DateFormat format = threadLocal_second_non.get();
if(format == null){
format = new SimpleDateFormat(date_format_second_non);
threadLocal_second_non.set(format);
}
return format.format(date);
}
public static String date2StringByMin(Date date) {
if (date == null) {
return null;
}
DateFormat format = threadLocal_min.get();
if(format == null){
format = new SimpleDateFormat(data_format_min);
threadLocal_min.set(format);
}
return format.format(date);
}
public static String date2StringByDay(Date date) {
if (date == null) {
return null;
}
DateFormat format = threadLocal_day.get();
if(format == null){
format = new SimpleDateFormat(data_format_day);
threadLocal_day.set(format);
}
return format.format(date);
}
public static Date string2DateBysecond(String date) {
if (date == null) {
return null;
}
date = date.trim();
if (date.isEmpty()) {
return null;
}
DateFormat format = threadLocal_second.get();
try {
if(format == null){
format = new SimpleDateFormat(date_format_second);
threadLocal_second.set(format);
}
return format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static Date string2DateByMin(String date) {
if (date == null) {
return null;
}
date = date.trim();
if (date.isEmpty()) {
return null;
}
DateFormat format = threadLocal_min.get();
try {
if(format == null){
format = new SimpleDateFormat(data_format_min);
threadLocal_min.set(format);
}
return format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static Date string2DateByDay(String date) {
if (date == null) {
return null;
}
date = date.trim();
if (date.isEmpty()) {
return null;
}
DateFormat format = threadLocal_day.get();
try {
if(format == null){
format = new SimpleDateFormat(data_format_day);
threadLocal_day.set(format);
}
return format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
Loading…
Cancel
Save