use of com.bonree.brfs.common.zookeeper.curator.CuratorClient in project BRFS by zhangnianli.
the class CopyCountCheck method collectionSnFiles.
/**
* 概述:获取集群对应目录的文件
* @param services
* @param snList
* @return
* @user <a href=mailto:zhucg@bonree.com>朱成岗</a>
*/
public static Map<StorageRegion, List<String>> collectionSnFiles(List<Service> services, List<StorageRegion> snList, final Map<String, Long> snTimes) throws Exception {
Map<StorageRegion, List<String>> snMap = new HashMap<>();
DiskNodeClient client = null;
int reCount;
String snName = null;
String path;
List<String> strs;
long time;
String dirName;
String sid;
ManagerContralFactory mcf = ManagerContralFactory.getInstance();
ServerIDManager sim = mcf.getSim();
CuratorClient zkClient = mcf.getClient();
SecondIDParser parser;
String basePath = mcf.getZkPath().getBaseRoutePath();
int timeout = 10000;
for (Service service : services) {
try {
client = TcpClientUtils.getClient(service.getHost(), service.getPort(), service.getExtraPort(), timeout);
long granule;
for (StorageRegion sn : snList) {
parser = new SecondIDParser(zkClient, sn.getId(), basePath);
parser.updateRoute();
sid = sim.getOtherSecondID(service.getServiceId(), sn.getId());
granule = Duration.parse(sn.getFilePartitionDuration()).toMillis();
reCount = sn.getReplicateNum();
snName = sn.getName();
if (!snTimes.containsKey(snName)) {
LOG.debug("sntime don't contain {}", snName);
continue;
}
time = snTimes.get(snName);
dirName = TimeUtils.timeInterval(time, granule);
for (int i = 1; i <= reCount; i++) {
path = "/" + snName + "/" + i + "/" + dirName;
LOG.debug("path :{}", path);
strs = getFileList(parser, client, path, sid);
if (strs == null || strs.isEmpty()) {
LOG.debug("files is empty {}", path);
continue;
}
LOG.debug("Collection dirName :{},{} size :{}", dirName, path, strs.size());
if (!snMap.containsKey(sn)) {
snMap.put(sn, new ArrayList<>());
}
snMap.get(sn).addAll(strs);
}
}
} catch (Exception e) {
EmailPool emailPool = EmailPool.getInstance();
MailWorker.Builder builder = MailWorker.newBuilder(emailPool.getProgramInfo());
builder.setModel("collect file execute 模块服务发生问题");
builder.setException(e);
builder.setMessage(mcf.getGroupName() + "(" + mcf.getServerId() + ")服务 执行任务时发生问题");
Map<String, String> map = new HashedMap();
map.put("remote ", service.getHost());
map.put("connectTimeout", String.valueOf(timeout));
map.put("sn", StringUtils.isEmpty(snName) ? "" : snName);
if (snTimes != null && !snTimes.isEmpty()) {
map.put("sntime", JSON.toJSONString(snTimes));
}
builder.setVariable(map);
emailPool.sendEmail(builder);
throw e;
} finally {
if (client != null) {
try {
client.close();
} catch (IOException e) {
LOG.error("{}", e);
}
}
}
}
return clearUnLawFiles(snMap);
}
use of com.bonree.brfs.common.zookeeper.curator.CuratorClient in project BRFS by zhangnianli.
the class ReadMetaData method main.
public static void main(String[] args) throws JsonException {
String path = "/brfs/test1/rebalance/changes/0";
Map<String, List<ChangeSummary>> csMap = Maps.newHashMap();
CuratorClient curatorClient = CuratorClient.getClientInstance("192.168.107.13");
List<String> childNodeList = curatorClient.getChildren(path);
Collections.sort(childNodeList);
for (String node : childNodeList) {
String nodePath = ZKPaths.makePath(path, node);
ChangeSummary cs = JsonUtils.toObject(curatorClient.getData(nodePath), ChangeSummary.class);
if (csMap.get(cs.getChangeServer()) == null) {
List<ChangeSummary> csList = Lists.newArrayList();
csList.add(cs);
csMap.put(cs.getChangeServer(), csList);
} else {
csMap.get(cs.getChangeServer()).add(cs);
}
}
for (Entry<String, List<ChangeSummary>> entry : csMap.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
System.out.println(entry.getKey() + ":" + entry.getValue().stream().map(x -> x.getChangeType()).collect(Collectors.toList()));
}
curatorClient.close();
}
use of com.bonree.brfs.common.zookeeper.curator.CuratorClient in project BRFS by zhangnianli.
the class GatherResourceJob method operation.
@Override
public void operation(JobExecutionContext context) throws Exception {
if (mountPoints == null) {
String[] mounts = StringUtils.split(Configs.getConfiguration().GetConfig(ResourceConfigs.CONFIG_UNMONITOR_PARTITION), ",");
if (mounts != null) {
mountPoints = new ArrayList<>(mounts.length);
for (String mount : mounts) {
if (BrStringUtils.isEmpty(mount)) {
continue;
}
mountPoints.add(mount.trim());
}
}
}
JobDataMap data = context.getJobDetail().getJobDataMap();
if (data == null || data.isEmpty()) {
throw new NullPointerException("job data map is empty");
}
String dataDir = data.getString(JobDataMapConstract.DATA_PATH);
String zkPath = data.getString(JobDataMapConstract.BASE_SERVER_ID_PATH);
String ip = data.getString(JobDataMapConstract.IP);
ManagerContralFactory mcf = ManagerContralFactory.getInstance();
CuratorClient client = mcf.getClient();
if (client == null) {
LOG.error("zookeeper is empty !!!!");
return;
}
String basePath = zkPath + "/" + mcf.getGroupName();
String bPath = basePath + "/base/" + mcf.getServerId();
if (!client.checkExists(bPath)) {
saveLocal(client, mcf.getServerId(), dataDir, bPath);
}
long gatherInveral = data.getLongValueFromString(JobDataMapConstract.GATHER_INVERAL_TIME);
int count = data.getIntFromString(JobDataMapConstract.CALC_RESOURCE_COUNT);
StateMetaServerModel metaSource = GatherResource.gatherResource(dataDir, ip, mountPoints);
if (metaSource != null) {
queue.add(metaSource);
LOG.info("gather stat info !!! {}", queue.size());
}
int queueSize = queue.size();
if (queueSize < count) {
return;
}
// 更新任务的可执行资源
StatServerModel sum = calcStateServer(gatherInveral, dataDir, count);
if (sum != null) {
RunnableTaskInterface rt = mcf.getRt();
rt.update(sum);
}
// 计算可用服务
BaseMetaServerModel base = getClusterBases(client, basePath + "/base", mcf.getGroupName());
if (base == null) {
return;
}
String serverId = mcf.getServerId();
// 计算资源值
ResourceModel resource = GatherResource.calcResourceValue(base, sum, serverId, ip);
if (resource == null) {
LOG.warn("calc resource value is null !!!");
return;
}
sendWarnEmail(resource, mcf.getLimitServerResource());
resource.setServerId(serverId);
Map<Integer, String> snIds = getStorageNameIdWithName();
resource.setSnIds(snIds);
byte[] rdata = JsonUtils.toJsonBytesQuietly(resource);
String rPath = basePath + "/resource/" + serverId;
if (!saveDataToZK(client, rPath, rdata)) {
LOG.error("resource content :{} save to zk fail !!!", JsonUtils.toJsonStringQuietly(resource));
} else {
LOG.info("resource: succefull !!!");
}
BaseMetaServerModel local = GatherResource.gatherBase(serverId, dataDir, mountPoints);
if (local == null) {
LOG.error("gather base data is empty !!!");
return;
}
byte[] bData = JsonUtils.toJsonBytesQuietly(local);
if (!saveDataToZK(client, bPath, bData)) {
LOG.error("base content : {} save to zk fail!!!", JsonUtils.toJsonStringQuietly(local));
}
saveLocal(client, mcf.getServerId(), dataDir, bPath);
}
use of com.bonree.brfs.common.zookeeper.curator.CuratorClient in project BRFS by zhangnianli.
the class TaskDispatcher method taskTerminal.
public void taskTerminal(CuratorFramework client, TreeCacheEvent event) {
if (leaderLath.hasLeadership()) {
CuratorClient curatorClient = CuratorClient.wrapClient(client);
LOG.info("leaderLath:" + getLeaderLatch().hasLeadership());
LOG.info("task Dispatch event detail:" + RebalanceUtils.convertEvent(event));
if (event.getType() == Type.NODE_UPDATED) {
if (event.getData() != null && event.getData().getData() != null) {
// 此处会检测任务是否完成
String eventPath = event.getData().getPath();
if (eventPath.substring(eventPath.lastIndexOf('/') + 1, eventPath.length()).equals(Constants.TASK_NODE)) {
return;
}
String parentPath = StringUtils.substring(eventPath, 0, eventPath.lastIndexOf('/'));
// 节点已经删除,则忽略
if (!curatorClient.checkExists(parentPath)) {
return;
}
BalanceTaskSummary bts = JsonUtils.toObjectQuietly(curatorClient.getData(parentPath), BalanceTaskSummary.class);
List<String> serverIds = curatorClient.getChildren(parentPath);
// 判断是否所有的节点做完任务
boolean finishFlag = true;
if (serverIds != null) {
if (serverIds.isEmpty()) {
LOG.info("taskoperation is not execute task!!!");
finishFlag = false;
} else {
for (String serverId : serverIds) {
String nodePath = ZKPaths.makePath(parentPath, serverId);
TaskDetail td = JsonUtils.toObjectQuietly(curatorClient.getData(nodePath), TaskDetail.class);
if (td.getStatus() != DataRecover.ExecutionStatus.FINISH) {
finishFlag = false;
break;
}
}
}
}
// 所有的服务都则发布迁移规则,并清理任务
if (finishFlag) {
// 先更新任务状态为finish
updateTaskStatus(bts, TaskStatus.FINISH);
// 发布路由规则
if (bts.getTaskType() == RecoverType.VIRTUAL) {
LOG.info("one virtual task finish,detail:" + RebalanceUtils.convertEvent(event));
String virtualRouteNode = ZKPaths.makePath(virtualRoutePath, String.valueOf(bts.getStorageIndex()), bts.getId());
VirtualRoute route = new VirtualRoute(bts.getChangeID(), bts.getStorageIndex(), bts.getServerId(), bts.getInputServers().get(0), TaskVersion.V1);
LOG.info("add virtual route:" + route);
addRoute(virtualRouteNode, JsonUtils.toJsonBytesQuietly(route));
// 因共享节点,所以得将余下的所有virtual server id,注册新迁移的server。不足之处,可能为导致副本数的恢复大于服务数。
// String firstID = idManager.getOtherFirstID(bts.getInputServers().get(0), bts.getStorageIndex());
// List<String> normalVirtualIDs = idManager.listNormalVirtualID(bts.getStorageIndex());
// if (normalVirtualIDs != null && !normalVirtualIDs.isEmpty()) {
// for (String virtualID : normalVirtualIDs) {
// idManager.registerFirstID(bts.getStorageIndex(), virtualID, firstID);
// }
// }
// 删除virtual server ID
LOG.info("delete the virtual server id:" + bts.getServerId());
idManager.deleteVirtualID(bts.getStorageIndex(), bts.getServerId());
} else if (bts.getTaskType() == RecoverType.NORMAL) {
LOG.info("one normal task finish,detail:" + RebalanceUtils.convertEvent(event));
String normalRouteNode = ZKPaths.makePath(normalRoutePath, String.valueOf(bts.getStorageIndex()), bts.getId());
NormalRoute route = new NormalRoute(bts.getChangeID(), bts.getStorageIndex(), bts.getServerId(), bts.getInputServers(), TaskVersion.V1);
LOG.info("add normal route:" + route);
addRoute(normalRouteNode, JsonUtils.toJsonBytesQuietly(route));
}
List<ChangeSummary> changeSummaries = cacheSummaryCache.get(bts.getStorageIndex());
// 清理zk上的变更
removeChange2History(bts);
// 清理change cache缓存
if (changeSummaries != null) {
Iterator<ChangeSummary> it = changeSummaries.iterator();
while (it.hasNext()) {
ChangeSummary cs = it.next();
if (cs.getChangeID().equals(bts.getChangeID())) {
changeSummaries.remove(cs);
}
}
}
// 删除zk上的任务节点
if (delBalanceTask(bts)) {
// 清理task缓存
removeRunTask(bts.getStorageIndex());
}
}
}
}
}
}
use of com.bonree.brfs.common.zookeeper.curator.CuratorClient in project BRFS by zhangnianli.
the class Test method main.
public static void main(String[] args) throws InterruptedException {
// ExecutorService threads = Executors.newFixedThreadPool(5);
//
// for(int i = 0;i<10;i++) {
// final int index = i;
// threads.execute(new Runnable() {
//
// @Override
// public void run() {
// CuratorClient client = CuratorClient.getClientInstance("192.168.101.86:2181");
// System.out.println("/brfs/wz/cache/"+index);
// // client.createEphemeral("/brfs/wz/cache/"+index, false);
// client.createPersistent("/brfs/wz/cache/"+index, false);
// // client.delete("/brfs/wz/cache/"+index, false);
// client.close();
// }
// });
// }
//
// threads.shutdown();
// threads.awaitTermination(1, TimeUnit.DAYS);
CuratorClient client = CuratorClient.getClientInstance("192.168.101.86:2181");
client.setData("/brfs/wz/cache/6", "1213213".getBytes());
client.close();
// client.createPersistent("/brfs/wz/cache/aaaa", true);
// client.delete("/brfs/wz/cache/aaaa",false);
// client.createEphemeral("/brfs/wz/cache/aaaa", true);
}
Aggregations