use of com.bonree.brfs.common.service.Service in project BRFS by zhangnianli.
the class ResourceDuplicateNodeSelector method getResource.
private DuplicateNode[] getResource(int storageId, int nums, List<Pair<String, Integer>> servers) {
List<Service> serviceList = serviceManager.getServiceListByGroup(Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_DATA_SERVICE_GROUP_NAME));
if (serviceList.isEmpty()) {
LOG.error("[{}] resource select serviceList is empty !!!", Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_DATA_SERVICE_GROUP_NAME));
return new DuplicateNode[0];
}
Map<String, Service> map = new HashMap<String, Service>();
for (Service server : serviceList) {
map.put(server.getServiceId(), server);
}
String group = serviceList.get(0).getServiceGroup();
List<DuplicateNode> nodes = new ArrayList<DuplicateNode>();
List<String> uNeeds = new ArrayList<String>();
long start = System.currentTimeMillis();
while (!serviceList.isEmpty() && nodes.size() < nums) {
String serverId = WeightRandomPattern.getWeightRandom(servers, rand, uNeeds);
Service service = null;
if (BrStringUtils.isEmpty(serverId) && !serviceList.isEmpty()) {
service = serviceList.remove(rand.nextInt(serviceList.size()));
} else {
uNeeds.add(serverId);
service = map.get(serverId);
}
if (service == null) {
continue;
}
DuplicateNode node = new DuplicateNode(service.getServiceGroup(), service.getServiceId());
DiskNodeConnection conn = connectionPool.getConnection(node.getGroup(), node.getId());
if (conn == null || conn.getClient() == null) {
continue;
}
serviceList.remove(service);
nodes.add(node);
}
DuplicateNode[] result = new DuplicateNode[nodes.size()];
long end = System.currentTimeMillis();
LOG.info("select time {} ms, select services :{}", (end - start), uNeeds);
return nodes.toArray(result);
}
use of com.bonree.brfs.common.service.Service in project BRFS by zhangnianli.
the class MinimalDuplicateNodeSelector method getDuplicationNodes.
@Override
public DuplicateNode[] getDuplicationNodes(int storageId, int nums) {
List<Service> serviceList = serviceManager.getServiceListByGroup(Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_DATA_SERVICE_GROUP_NAME));
if (serviceList.isEmpty()) {
return new DuplicateNode[0];
}
List<DuplicateNode> nodes = new ArrayList<DuplicateNode>();
while (!serviceList.isEmpty() && nodes.size() < nums) {
Service service = serviceList.remove(rand.nextInt(serviceList.size()));
DuplicateNode node = new DuplicateNode(service.getServiceGroup(), service.getServiceId());
DiskNodeConnection conn = connectionPool.getConnection(node.getGroup(), node.getId());
if (conn == null || conn.getClient() == null || !conn.getClient().ping()) {
continue;
}
nodes.add(node);
}
DuplicateNode[] result = new DuplicateNode[nodes.size()];
return nodes.toArray(result);
}
use of com.bonree.brfs.common.service.Service in project BRFS by zhangnianli.
the class FileNodeDistributor method getServiceWithStorageRegionName.
// 获取可以接受属于特定数据库的文件的服务列表
private List<Service> getServiceWithStorageRegionName(String storageRegionName) {
List<Service> result = new ArrayList<Service>();
List<Service> activeServices = serviceManager.getServiceListByGroup(DUPLICATE_SERVICE_GROUP);
for (Service s : activeServices) {
try {
List<String> storageRegionNodes = client.getChildren().forPath(ZkFileCoordinatorPaths.buildServiceSinkPath(s));
if (storageRegionNodes.contains(storageRegionName)) {
result.add(s);
}
} catch (NoNodeException e) {
LOG.info("no sink node for service[{}], region[{}] error", s.getServiceId(), storageRegionName);
} catch (Exception e) {
LOG.error("get sink for service[{}], region[{}] error", s.getServiceId(), storageRegionName, e);
}
}
return result;
}
use of com.bonree.brfs.common.service.Service in project BRFS by zhangnianli.
the class FileNodeDistributor method handleFileNode.
private boolean handleFileNode(FileNode fileNode) {
LOG.info("handling wild FileNode[{}]", JsonUtils.toJsonStringQuietly(fileNode));
List<Service> serviceList = getServiceWithStorageRegionName(fileNode.getStorageName());
Service target = serviceSelector.selectWith(fileNode, serviceList);
if (target == null) {
LOG.info("no service to accept filenode[{}], add it to wild list", fileNode.getName());
return false;
}
LOG.info("transfer fileNode[{}] to service[{}]", fileNode.getName(), target.getServiceId());
FileNode newFileNode = FileNode.newBuilder(fileNode).setServiceId(target.getServiceId()).setServiceTime(target.getRegisterTime()).build();
try {
fileStorer.update(newFileNode);
} catch (Exception e) {
LOG.error("update file node[{}] info error", fileNode.getName(), e);
return false;
}
try {
// 在Sink中放入分配的文件名
String path = client.create().forPath(ZkFileCoordinatorPaths.buildSinkFileNodePath(newFileNode), JsonUtils.toJsonBytes(newFileNode));
LOG.info("filenode[{}] add to sink[{}]", newFileNode.getName(), path);
return true;
} catch (Exception e) {
LOG.error("add filenode[{}] to sink error", newFileNode.getName(), e);
try {
fileStorer.update(fileNode);
} catch (Exception e1) {
LOG.error("roll back to original info of filenode[{}] error", fileNode.getName(), e1);
}
return false;
}
}
use of com.bonree.brfs.common.service.Service in project BRFS by zhangnianli.
the class DeleteDataMessageHandler method handle.
@Override
public void handle(HttpMessage msg, HandleResultCallback callback) {
HandleResult result = new HandleResult();
List<String> deleteInfo = Splitter.on("/").omitEmptyStrings().trimResults().splitToList(msg.getPath());
if (deleteInfo.size() != 2) {
result.setSuccess(false);
result.setCause(new IllegalArgumentException(msg.getPath()));
callback.completed(result);
return;
}
int storageId = Integer.parseInt(deleteInfo.get(0));
LOG.info("DELETE data for storage[{}]", storageId);
StorageRegion sn = storageNameManager.findStorageRegionById(storageId);
if (sn == null) {
result.setSuccess(false);
result.setCause(new StorageNameNonexistentException(storageId));
callback.completed(result);
LOG.info("storage[{}] is null", storageId);
return;
}
List<String> times = Splitter.on("_").omitEmptyStrings().trimResults().splitToList(deleteInfo.get(1));
ReturnCode code = checkTime(times.get(0), times.get(1), sn.getCreateTime(), Duration.parse(sn.getFilePartitionDuration()).toMillis());
if (!ReturnCode.SUCCESS.equals(code)) {
result.setSuccess(false);
result.setData(BrStringUtils.toUtf8Bytes(code.name()));
callback.completed(result);
LOG.info("DELETE DATE Fail storage[{}] reason : {}", storageId, code.name());
return;
}
long startTime = DateTime.parse(times.get(0)).getMillis();
long endTime = DateTime.parse(times.get(1)).getMillis();
LOG.info("DELETE DATA [{}-->{}]", times.get(0), times.get(1));
List<Service> serviceList = serviceManager.getServiceListByGroup(Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_DATA_SERVICE_GROUP_NAME));
code = TasksUtils.createUserDeleteTask(serviceList, zkPaths, sn, startTime, endTime, false);
result.setSuccess(ReturnCode.SUCCESS.equals(code));
result.setData(BrStringUtils.toUtf8Bytes(code.name()));
callback.completed(result);
}
Aggregations