diff --git a/src/com/platform/glusterfs/CheckoutMD5.java b/src/com/platform/glusterfs/CheckoutMD5.java new file mode 100644 index 00000000..d0be3dad --- /dev/null +++ b/src/com/platform/glusterfs/CheckoutMD5.java @@ -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 source_md5 = new HashMap(); + Map dest_md5 = new HashMap(); + + 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 "; + } + + /** + * 文件夹校验 校验sourcePath和destPath是否完全相同,如果相同,返回1; + * 如果不相同,返回0,如果获取文件MD5出错,返回-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 wrong_files = new ArrayList(); + String source_cmd = "find " + sourcePath + " -type f -print0 | xargs -0 md5sum"; + String dest_cmd = "find " + destPath + " -type f -print0 | xargs -0 md5sum"; + List 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 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 source_md5 = new HashMap(); + Map dest_md5 = new HashMap(); + 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 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")); + } +} diff --git a/src/com/platform/service/IMoveDataService.java b/src/com/platform/service/IMoveDataService.java new file mode 100644 index 00000000..bddb1e04 --- /dev/null +++ b/src/com/platform/service/IMoveDataService.java @@ -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 volume下的某个folder路径(需要补齐路径,eg: XXX/320198_16/1,or XXX/320122KFQ_15/1) + * @return + * @throws Exception + */ + public boolean moveData(List data, String dstPath) throws Exception; + + /** 查询所有进度 + * @return 返回所有的实体 + * @throws Exception + */ + public List 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 list) throws Exception; + +} diff --git a/src/com/platform/service/impl/MoveDataServiceImpl.java b/src/com/platform/service/impl/MoveDataServiceImpl.java new file mode 100644 index 00000000..511676a7 --- /dev/null +++ b/src/com/platform/service/impl/MoveDataServiceImpl.java @@ -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 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 exist = dataInfoMoveTmpDao.findAll(); + List existIds = new ArrayList(); + 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 moveList = new ArrayList(); + 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 findAll() throws Exception { + List result = new ArrayList(); + 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 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; + } + +} diff --git a/src/com/platform/service/thread/TreadMoveData2Start.java b/src/com/platform/service/thread/TreadMoveData2Start.java new file mode 100644 index 00000000..91f9bb92 --- /dev/null +++ b/src/com/platform/service/thread/TreadMoveData2Start.java @@ -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 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(); + } + } + } + +} diff --git a/src/com/platform/utils/DateForm.java b/src/com/platform/utils/DateForm.java new file mode 100644 index 00000000..c6531466 --- /dev/null +++ b/src/com/platform/utils/DateForm.java @@ -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 threadLocal_second = new ThreadLocal(); + + private static ThreadLocal threadLocal_second_non = new ThreadLocal(); + + private static ThreadLocal threadLocal_min = new ThreadLocal(); + + private static ThreadLocal threadLocal_day = new ThreadLocal(); + + 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; + } + +}