use of com.bonree.brfs.schedulers.task.model.BatchAtomModel in project BRFS by zhangnianli.
the class JobDataMapConstract method groupAtoms.
public static Map<Integer, BatchAtomModel> groupAtoms(final List<AtomTaskModel> atoms, int batchCount) throws IllegalArgumentException {
Map<Integer, BatchAtomModel> batchMap = new HashMap<Integer, BatchAtomModel>();
BatchAtomModel batch = null;
int size = atoms == null ? 0 : atoms.size();
if (batchCount <= 0) {
throw new IllegalArgumentException("batch time below zero !!!");
}
int batchSize = size % batchCount == 0 ? size / batchCount : size / batchCount + size % batchCount;
int fromIndex = 0;
int rSize = 0;
for (int i = 1; i <= batchCount; i++) {
if (!batchMap.containsKey(i)) {
batchMap.put(i, new BatchAtomModel());
}
if (size == 0) {
continue;
}
batch = batchMap.get(i);
if (fromIndex > size) {
continue;
} else if (fromIndex + batchSize <= size) {
batch.addAll(atoms.subList(fromIndex, fromIndex + batchSize));
} else {
batch.addAll(atoms.subList(fromIndex, size));
}
rSize += batch.getAtoms().size();
fromIndex += batchSize;
}
if (size != rSize) {
throw new IllegalArgumentException("total batch [" + rSize + "] not equal source [" + size + "], batch count [" + batchCount + "]");
}
return batchMap;
}
use of com.bonree.brfs.schedulers.task.model.BatchAtomModel in project BRFS by zhangnianli.
the class SystemDeleteJob method operation.
@Override
public void operation(JobExecutionContext context) throws Exception {
LOG.debug("----------> system delete work");
JobDataMap data = context.getJobDetail().getJobDataMap();
String currentIndex = data.getString(JobDataMapConstract.CURRENT_INDEX);
String dataPath = data.getString(JobDataMapConstract.DATA_PATH);
String content = data.getString(currentIndex);
LOG.debug("batch {}", content);
// 获取当前执行的任务类型
BatchAtomModel batch = JsonUtils.toObject(content, BatchAtomModel.class);
if (batch == null) {
LOG.debug("batch data is empty !!!");
return;
}
List<AtomTaskModel> atoms = batch.getAtoms();
if (atoms == null || atoms.isEmpty()) {
LOG.debug("atom task is empty !!!");
return;
}
String snName;
TaskResultModel result = new TaskResultModel();
AtomTaskResultModel usrResult;
for (AtomTaskModel atom : atoms) {
snName = atom.getStorageName();
if (BrStringUtils.isEmpty(snName)) {
LOG.debug("sn is empty !!!");
continue;
}
usrResult = deleteDirs(atom, dataPath);
if (usrResult == null) {
continue;
}
if (!usrResult.isSuccess()) {
result.setSuccess(false);
}
result.add(usrResult);
}
// 更新任务状态
TaskStateLifeContral.updateMapTaskMessage(context, result);
}
use of com.bonree.brfs.schedulers.task.model.BatchAtomModel in project BRFS by zhangnianli.
the class BatchTaskFactory method createBatch.
/**
* 概述:
* @param task
* @param batchSize
* @return
* @user <a href=mailto:zhucg@bonree.com>朱成岗</a>
*/
public static Map<String, String> createBatch(TaskModel task, int batchSize) {
Map<String, String> batchDatas = new HashMap<String, String>();
if (task == null) {
return batchDatas;
}
List<AtomTaskModel> atoms = convernTaskModel(task);
if (atoms == null || atoms.isEmpty()) {
return batchDatas;
}
int size = atoms.size();
int batchCount = size % batchSize == 0 ? size / batchSize : size / batchSize + 1;
BatchAtomModel batch = null;
List<AtomTaskModel> tmp = null;
// 若不足一次则按一次来
if (batchCount == 0) {
batch = new BatchAtomModel();
batch.addAll(atoms);
batchDatas.put(JobDataMapConstract.CURRENT_INDEX, "1");
batchDatas.put("1", JsonUtils.toJsonStringQuietly(batch));
return batchDatas;
}
batchDatas.put(JobDataMapConstract.CURRENT_INDEX, batchCount + "");
int index = 0;
for (int i = 1; i <= batchCount; i++) {
batch = new BatchAtomModel();
if (index >= size) {
tmp = new ArrayList<>();
} else if (index + batchSize > size) {
tmp = atoms.subList(index, size);
} else if (index + batchSize <= size) {
tmp = atoms.subList(index, index + batchSize);
}
batch.addAll(tmp);
batchDatas.put(i + "", JsonUtils.toJsonStringQuietly(batch));
index = index + batchSize;
}
return batchDatas;
}
use of com.bonree.brfs.schedulers.task.model.BatchAtomModel in project BRFS by zhangnianli.
the class SystemCheckJob method operation.
@Override
public void operation(JobExecutionContext context) throws Exception {
LOG.debug("check task work");
JobDataMap data = context.getJobDetail().getJobDataMap();
String currentIndex = data.getString(JobDataMapConstract.CURRENT_INDEX);
String dataPath = data.getString(JobDataMapConstract.DATA_PATH);
String content = data.getString(currentIndex);
if (BrStringUtils.isEmpty(content)) {
LOG.debug("batch data is empty !!!");
return;
}
BatchAtomModel batch = JsonUtils.toObject(content, BatchAtomModel.class);
if (batch == null) {
LOG.debug("batch data is empty !!!");
return;
}
List<AtomTaskModel> atoms = batch.getAtoms();
if (atoms == null || atoms.isEmpty()) {
LOG.debug("atom task is empty !!!");
return;
}
String snName = null;
TaskResultModel result = new TaskResultModel();
TaskResultModel batchResult = null;
for (AtomTaskModel atom : atoms) {
snName = atom.getStorageName();
if (BrStringUtils.isEmpty(snName)) {
LOG.warn("sn is empty !!!");
continue;
}
batchResult = checkFiles(atom, dataPath);
if (batchResult == null) {
continue;
}
if (!batchResult.isSuccess()) {
result.setSuccess(batchResult.isSuccess());
}
result.addAll(batchResult.getAtoms());
}
// 更新任务状态
TaskStateLifeContral.updateMapTaskMessage(context, result);
}
use of com.bonree.brfs.schedulers.task.model.BatchAtomModel in project BRFS by zhangnianli.
the class UserDeleteJob method operation.
@Override
public void operation(JobExecutionContext context) throws Exception {
JobDataMap data = context.getJobDetail().getJobDataMap();
String currentIndex = data.getString(JobDataMapConstract.CURRENT_INDEX);
String dataPath = data.getString(JobDataMapConstract.DATA_PATH);
String content = data.getString(currentIndex);
// 获取当前执行的任务类型
if (BrStringUtils.isEmpty(content)) {
LOG.debug("batch data is empty !!!");
return;
}
BatchAtomModel batch = JsonUtils.toObject(content, BatchAtomModel.class);
if (batch == null) {
LOG.debug("batch data is empty !!!");
return;
}
List<AtomTaskModel> atoms = batch.getAtoms();
if (atoms == null || atoms.isEmpty()) {
LOG.debug("atom task is empty !!!");
return;
}
String snName;
TaskResultModel result = new TaskResultModel();
AtomTaskResultModel usrResult;
List<String> dSns = new ArrayList<String>();
String operation;
for (AtomTaskModel atom : atoms) {
snName = atom.getStorageName();
if ("1".equals(currentIndex)) {
operation = atom.getTaskOperation();
LOG.debug("task operation {} ", DELETE_SN_ALL.equals(operation) ? "Delete_Storage_Region" : "Delete_Part_Of_Storage_Region_Data");
if (DELETE_SN_ALL.equals(operation)) {
dSns.add(snName);
}
}
usrResult = deleteFiles(atom, dataPath);
if (usrResult == null) {
continue;
}
if (!usrResult.isSuccess()) {
result.setSuccess(false);
}
result.add(usrResult);
}
if ("1".equals(currentIndex)) {
for (String sn : dSns) {
if (FileUtils.deleteDir(dataPath + "/" + sn, true)) {
LOG.debug("deltete {} successfull", sn);
} else {
result.setSuccess(false);
}
}
}
// 更新任务状态
TaskStateLifeContral.updateMapTaskMessage(context, result);
}
Aggregations