use of com.bonree.brfs.common.service.Service in project BRFS by zhangnianli.
the class DefaultBRFileSystem method createStorageName.
@Override
public boolean createStorageName(String storageName, Map<String, Object> attrs) {
try {
Service[] serviceList = regionNodeSelector.select(regionNodeSelector.serviceNum());
if (serviceList.length == 0) {
throw new BRFSException("none disknode!!!");
}
for (Service service : serviceList) {
URIBuilder uriBuilder = new URIBuilder().setScheme(config.getUrlSchema()).setHost(service.getHost()).setPort(service.getPort()).setPath(config.getStorageUrlRoot() + "/" + storageName);
for (Entry<String, Object> attr : attrs.entrySet()) {
uriBuilder.addParameter(attr.getKey(), String.valueOf(attr.getValue()));
}
HttpResponse response = null;
try {
response = httpClient.executePut(uriBuilder.build(), defaultHeaders);
} catch (Exception e) {
LOG.warn("createStorageName http request failed", e);
continue;
}
if (response == null) {
throw new Exception("can not get response for createStorageName!");
}
if (response.isReponseOK()) {
return true;
}
}
} catch (Exception e) {
LOG.error("createStorageName error", e);
}
return false;
}
use of com.bonree.brfs.common.service.Service in project BRFS by zhangnianli.
the class DefaultBRFileSystem method openStorageName.
@Override
public StorageNameStick openStorageName(String storageName) {
StorageNameStick stick = null;
try {
Service[] serviceList = regionNodeSelector.select(regionNodeSelector.serviceNum());
if (serviceList.length == 0) {
throw new BRFSException("none disknode!!!");
}
for (Service service : serviceList) {
URI uri = new URIBuilder().setScheme(config.getUrlSchema()).setHost(service.getHost()).setPort(service.getPort()).setPath(config.getStorageUrlRoot() + "/" + storageName).build();
HttpResponse response = null;
try {
response = httpClient.executeGet(uri, defaultHeaders);
} catch (Exception e) {
LOG.warn("openStorageName http request failed", e);
continue;
}
if (response == null) {
throw new Exception("can not get response for deleteStorageName!");
}
if (response.isReponseOK()) {
int storageId = Ints.fromByteArray(response.getResponseBody());
stick = new DefaultStorageNameStick(storageName, storageId, httpClient, serviceSelectorManager.useDiskSelector(storageId), regionNodeSelector, config);
return stick;
}
}
} catch (Exception e) {
LOG.error("openStorageName error", e);
}
return stick;
}
use of com.bonree.brfs.common.service.Service in project BRFS by zhangnianli.
the class CreateSystemTask method getServerIds.
public static List<String> getServerIds(List<Service> sList) {
List<String> sids = new ArrayList<>();
if (sList == null || sList.isEmpty()) {
return sids;
}
String sid;
for (Service server : sList) {
sid = server.getServiceId();
if (BrStringUtils.isEmpty(sid)) {
continue;
}
sids.add(sid);
}
return sids;
}
use of com.bonree.brfs.common.service.Service in project BRFS by zhangnianli.
the class CopyRecovery method recoveryFileByName.
/**
* 概述:恢复单个文件
* @param sm
* @param sim
* @param parser
* @param snNode
* @param fileName
* @param dirName
* @return
* @user <a href=mailto:zhucg@bonree.com>朱成岗</a>
*/
public static boolean recoveryFileByName(ServiceManager sm, ServerIDManager sim, SecondIDParser parser, StorageRegion snNode, String fileName, String dirName, String dataPath, String operation) {
String[] sss;
String remoteName;
Service remoteService;
String localPath;
int remoteIndex;
int localIndex;
String remotePath;
boolean isSuccess = true;
String snName = snNode.getName();
int snId = snNode.getId();
sss = parser.getAliveSecondID(fileName);
if (sss == null) {
LOG.warn("alive second Ids is empty");
return false;
}
String secondId = sim.getSecondServerID(snId);
if (BrStringUtils.isEmpty(secondId)) {
LOG.warn("{} {} secondid is empty ", snName, snId);
return false;
}
localIndex = isContain(sss, secondId);
if (-1 == localIndex) {
LOG.info("{} {} {} is not mine !! skip", secondId, snName, fileName);
return true;
}
localPath = "/" + snName + "/" + localIndex + "/" + dirName + "/" + fileName;
String localDir = "/" + snName + "/" + localIndex + "/" + dirName + "/";
File dir = new File(dataPath + localDir);
if (!dir.exists()) {
boolean createFlag = dir.mkdirs();
LOG.debug("create dir :{}, stat:{}", localDir, createFlag);
}
if (CopyCheckJob.RECOVERY_CRC.equals(operation)) {
boolean flag = FileCollection.check(dataPath + localPath);
LOG.debug("locaPath : {}, CRCSTATUS: {}", dataPath + localPath, flag);
if (flag) {
return true;
} else {
boolean status = FileUtils.deleteFile(dataPath + localPath);
LOG.warn("{} crc is error!! delete {}", localPath, status);
}
} else {
File file = new File(dataPath + localPath);
if (file.exists()) {
LOG.debug("{} {} is exists, skip", snName, fileName);
return true;
}
}
remoteIndex = 0;
for (String snsid : sss) {
remoteIndex++;
// 排除自己
if (secondId.equals(snsid)) {
LOG.debug(" my son is right,not need to do {} {} {}", fileName, secondId, snsid);
continue;
}
remoteName = sim.getOtherFirstID(snsid, snId);
if (BrStringUtils.isEmpty(remoteName)) {
LOG.warn("remote name is empty");
continue;
}
remoteService = sm.getServiceById(Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_DATA_SERVICE_GROUP_NAME), remoteName);
if (remoteService == null) {
LOG.warn("remote service is empty");
continue;
}
remotePath = "/" + snName + "/" + remoteIndex + "/" + dirName + "/" + fileName;
isSuccess = copyFrom(remoteService.getHost(), remoteService.getPort(), remoteService.getExtraPort(), 5000, remotePath, dataPath + localPath);
LOG.info("remote address [{}: {} :{}], remote [{}], local [{}], stat [{}]", remoteService.getHost(), remoteService.getPort(), remoteService.getExtraPort(), remotePath, localPath, isSuccess ? "success" : "fail");
if (isSuccess) {
return true;
}
}
return isSuccess;
}
use of com.bonree.brfs.common.service.Service in project BRFS by zhangnianli.
the class DefaultStorageNameStick method writeData.
@Override
public String[] writeData(InputItem[] itemArrays) {
WriteDataMessage dataMessage = new WriteDataMessage();
dataMessage.setStorageNameId(storageId);
DataItem[] dataItems = new DataItem[itemArrays.length];
for (int i = 0; i < dataItems.length; i++) {
dataItems[i] = new DataItem();
dataItems[i].setBytes(itemArrays[i].getBytes());
}
dataMessage.setItems(dataItems);
try {
Service[] serviceList = regionNodeSelector.select(regionNodeSelector.serviceNum());
if (serviceList.length == 0) {
throw new BRFSException("none disknode!!!");
}
for (Service service : serviceList) {
URI uri = new URIBuilder().setScheme(config.getUrlSchema()).setHost(service.getHost()).setPort(service.getPort()).setPath(config.getDuplicateUrlRoot() + "/").build();
HttpResponse response = null;
try {
response = client.executePost(uri, defaultHeaders, ProtoStuffUtils.serialize(dataMessage));
} catch (Exception e) {
LOG.warn("write data http request failed", e);
continue;
}
if (response == null) {
throw new Exception("can not get response for writeData!");
}
if (response.isReponseOK()) {
List<String> results = JSON.parseArray(BrStringUtils.fromUtf8Bytes(response.getResponseBody()), String.class);
String[] fids = new String[results.size()];
return results.toArray(fids);
}
}
} catch (Exception e) {
LOG.error("write data error", e);
}
return null;
}
Aggregations