use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.
the class ConfigurationService method handle.
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
HttpServiceResponse response = new HttpServiceResponse();
// GET
if (HttpServer.Method.GET == request.getMethod()) {
String jsonResponse = conf.asJson();
response.setBody(jsonResponse);
return response;
} else if (HttpServer.Method.PUT == request.getMethod()) {
String requestBody = request.getBody();
if (null == requestBody) {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Request body not found. should contains k-v pairs");
return response;
}
@SuppressWarnings("unchecked") HashMap<String, Object> configMap = JsonUtil.fromJson(requestBody, HashMap.class);
for (Map.Entry<String, Object> entry : configMap.entrySet()) {
conf.setProperty(entry.getKey(), entry.getValue());
}
response.setCode(HttpServer.StatusCode.OK);
response.setBody("Success set server config.");
return response;
} else {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Request body not found. should contains k-v pairs");
return response;
}
}
use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.
the class DeleteLedgerService method handle.
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
HttpServiceResponse response = new HttpServiceResponse();
// only handle DELETE method
if (HttpServer.Method.DELETE == request.getMethod()) {
Map<String, String> params = request.getParams();
if (params != null && params.containsKey("ledger_id")) {
ClientConfiguration clientConf = new ClientConfiguration();
clientConf.addConfiguration(conf);
BookKeeper bk = new BookKeeper(clientConf);
Long ledgerId = Long.parseLong(params.get("ledger_id"));
bk.deleteLedger(ledgerId);
String output = "Deleted ledger: " + ledgerId;
String jsonResponse = JsonUtil.toJson(output);
LOG.debug("output body:" + jsonResponse);
response.setBody(jsonResponse);
response.setCode(HttpServer.StatusCode.OK);
return response;
} else {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Not ledger found. Should provide ledger_id=<id>");
return response;
}
} else {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Not found method. Should be DELETE method");
return response;
}
}
use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.
the class GetLastLogMarkService method handle.
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
HttpServiceResponse response = new HttpServiceResponse();
if (HttpServer.Method.GET == request.getMethod()) {
try {
/**
* output:
* {
* "<Journal_id>" : "<Pos>",
* ...
* }
*/
Map<String, String> output = Maps.newHashMap();
List<Journal> journals = Lists.newArrayListWithCapacity(conf.getJournalDirs().length);
int idx = 0;
for (File journalDir : conf.getJournalDirs()) {
journals.add(new Journal(idx++, journalDir, conf, new LedgerDirsManager(conf, conf.getLedgerDirs(), new DiskChecker(conf.getDiskUsageThreshold(), conf.getDiskUsageWarnThreshold()))));
}
for (Journal journal : journals) {
LogMark lastLogMark = journal.getLastLogMark().getCurMark();
LOG.debug("LastLogMark: Journal Id - " + lastLogMark.getLogFileId() + "(" + Long.toHexString(lastLogMark.getLogFileId()) + ".txn), Pos - " + lastLogMark.getLogFileOffset());
output.put("LastLogMark: Journal Id - " + lastLogMark.getLogFileId() + "(" + Long.toHexString(lastLogMark.getLogFileId()) + ".txn)", "Pos - " + lastLogMark.getLogFileOffset());
}
String jsonResponse = JsonUtil.toJson(output);
LOG.debug("output body:" + jsonResponse);
response.setBody(jsonResponse);
response.setCode(HttpServer.StatusCode.OK);
return response;
} catch (Exception e) {
LOG.error("Exception occurred while getting last log mark", e);
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("ERROR handling request: " + e.getMessage());
return response;
}
} else {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Not found method. Should be GET method");
return response;
}
}
use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.
the class ListBookieInfoService method handle.
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
HttpServiceResponse response = new HttpServiceResponse();
if (HttpServer.Method.GET == request.getMethod()) {
ClientConfiguration clientConf = new ClientConfiguration(conf);
clientConf.setZkServers(conf.getZkServers());
clientConf.setDiskWeightBasedPlacementEnabled(true);
BookKeeper bk = new BookKeeper(clientConf);
Map<BookieSocketAddress, BookieInfoReader.BookieInfo> map = bk.getBookieInfo();
if (map.size() == 0) {
bk.close();
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Not found any Bookie info.");
return response;
}
/**
* output:
* {
* "bookieAddress" : {free: xxx, total: xxx}",
* "bookieAddress" : {free: xxx, total: xxx},
* ...
* "clusterInfo" : {total_free: xxx, total: xxx}"
* }
*/
LinkedHashMap<String, String> output = Maps.newLinkedHashMapWithExpectedSize(map.size());
Long totalFree = 0L, total = 0L;
for (Map.Entry<BookieSocketAddress, BookieInfoReader.BookieInfo> infoEntry : map.entrySet()) {
BookieInfoReader.BookieInfo bInfo = infoEntry.getValue();
output.put(infoEntry.getKey().toString(), ": {Free: " + bInfo.getFreeDiskSpace() + getReadable(bInfo.getFreeDiskSpace()) + ", Total: " + bInfo.getTotalDiskSpace() + getReadable(bInfo.getTotalDiskSpace()) + "},");
totalFree += bInfo.getFreeDiskSpace();
total += bInfo.getTotalDiskSpace();
}
output.put("ClusterInfo: ", "{Free: " + totalFree + getReadable(totalFree) + ", Total: " + total + getReadable(total) + "}");
bk.close();
String jsonResponse = JsonUtil.toJson(output);
LOG.debug("output body:" + jsonResponse);
response.setBody(jsonResponse);
response.setCode(HttpServer.StatusCode.OK);
return response;
} else {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Not found method. Should be GET method");
return response;
}
}
use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.
the class DecommissionService method handle.
/*
* decommission bookie.
*/
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
HttpServiceResponse response = new HttpServiceResponse();
if (HttpServer.Method.PUT == request.getMethod()) {
String requestBody = request.getBody();
if (requestBody == null) {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Null request body for DecommissionService.");
return response;
}
@SuppressWarnings("unchecked") HashMap<String, String> configMap = JsonUtil.fromJson(requestBody, HashMap.class);
if (configMap != null && configMap.containsKey("bookie_src")) {
try {
String[] bookieSrcString = configMap.get("bookie_src").split(":");
BookieSocketAddress bookieSrc = new BookieSocketAddress(bookieSrcString[0], Integer.parseInt(bookieSrcString[1]));
executor.execute(() -> {
try {
LOG.info("Start decommissioning bookie.");
bka.decommissionBookie(bookieSrc);
LOG.info("Complete decommissioning bookie.");
} catch (Exception e) {
LOG.error("Error handling decommissionBookie: {} with exception {}", bookieSrc, e);
}
});
response.setCode(HttpServer.StatusCode.OK);
response.setBody("Success send decommission Bookie command " + bookieSrc.toString());
return response;
} catch (Exception e) {
LOG.error("Exception occurred while decommissioning bookie: ", e);
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Exception when send decommission command." + e.getMessage());
return response;
}
} else {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Request body not contains bookie_src.");
return response;
}
} else {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Not found method. Should be PUT method");
return response;
}
}
Aggregations