use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.
the class WhoIsAuditorService method handle.
/*
* Print the node which holds the auditor lock.
*/
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
HttpServiceResponse response = new HttpServiceResponse();
if (HttpServer.Method.GET == request.getMethod()) {
BookieSocketAddress bookieId = null;
try {
bookieId = AuditorElector.getCurrentAuditor(conf, zk);
if (bookieId == null) {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("No auditor elected");
return response;
}
} catch (Exception e) {
LOG.error("Meet Exception: ", e);
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Exception when get." + e.getMessage());
return response;
}
response.setCode(HttpServer.StatusCode.OK);
response.setBody("Auditor: " + bookieId.getSocketAddress().getAddress().getCanonicalHostName() + "/" + bookieId.getSocketAddress().getAddress().getHostAddress() + ":" + bookieId.getSocketAddress().getPort());
LOG.debug("response body:" + response.getBody());
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 ListDiskFilesService method handle.
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
HttpServiceResponse response = new HttpServiceResponse();
Map<String, String> params = request.getParams();
if (HttpServer.Method.GET == request.getMethod()) {
/**
* output:
* {
* "journal files" : "filename \t ...",
* "entrylog files" : "filename \t ...",
* "index files" : "filename \t ..."
* }
*/
Map<String, String> output = Maps.newHashMap();
boolean journal = params != null && params.containsKey("file_type") && params.get("file_type").equals("journal");
boolean entrylog = params != null && params.containsKey("file_type") && params.get("file_type").equals("entrylog");
boolean index = params != null && params.containsKey("file_type") && params.get("file_type").equals("index");
boolean all = false;
if (!journal && !entrylog && !index && !all) {
all = true;
}
if (all || journal) {
File[] journalDirs = conf.getJournalDirs();
List<File> journalFiles = listFilesAndSort(journalDirs, "txn");
StringBuffer files = new StringBuffer();
for (File journalFile : journalFiles) {
files.append(journalFile.getName() + "\t");
}
output.put("journal files", files.toString());
}
if (all || entrylog) {
File[] ledgerDirs = conf.getLedgerDirs();
List<File> ledgerFiles = listFilesAndSort(ledgerDirs, "log");
StringBuffer files = new StringBuffer();
for (File ledgerFile : ledgerFiles) {
files.append(ledgerFile.getName() + "\t");
}
output.put("entrylog files", files.toString());
}
if (all || index) {
File[] indexDirs = (conf.getIndexDirs() == null) ? conf.getLedgerDirs() : conf.getIndexDirs();
List<File> indexFiles = listFilesAndSort(indexDirs, "idx");
StringBuffer files = new StringBuffer();
for (File indexFile : indexFiles) {
files.append(indexFile.getName() + "\t");
}
output.put("index files", files.toString());
}
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 ListUnderReplicatedLedgerService method handle.
/*
* Print the node which holds the auditor lock.
*/
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
HttpServiceResponse response = new HttpServiceResponse();
// parameter as this: ?missingreplica=<bookie_address>&excludingmissingreplica=<bookid_address>
Map<String, String> params = request.getParams();
if (HttpServer.Method.GET == request.getMethod()) {
final String includingBookieId;
final String excludingBookieId;
if (params != null && params.containsKey("missingreplica")) {
includingBookieId = params.get("missingreplica");
} else {
includingBookieId = null;
}
if (params != null && params.containsKey("excludingmissingreplica")) {
excludingBookieId = params.get("excludingmissingreplica");
} else {
excludingBookieId = null;
}
Predicate<List<String>> predicate = null;
if (!StringUtils.isBlank(includingBookieId) && !StringUtils.isBlank(excludingBookieId)) {
predicate = replicasList -> (replicasList.contains(includingBookieId) && !replicasList.contains(excludingBookieId));
} else if (!StringUtils.isBlank(includingBookieId)) {
predicate = replicasList -> replicasList.contains(includingBookieId);
} else if (!StringUtils.isBlank(excludingBookieId)) {
predicate = replicasList -> !replicasList.contains(excludingBookieId);
}
try {
List<Long> outputLedgers = Lists.newArrayList();
LedgerManagerFactory mFactory = bookieServer.getBookie().getLedgerManagerFactory();
LedgerUnderreplicationManager underreplicationManager = mFactory.newLedgerUnderreplicationManager();
Iterator<Long> iter = underreplicationManager.listLedgersToRereplicate(predicate);
while (iter.hasNext()) {
outputLedgers.add(iter.next());
}
if (outputLedgers.isEmpty()) {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("No under replicated ledgers found");
return response;
} else {
response.setCode(HttpServer.StatusCode.OK);
String jsonResponse = JsonUtil.toJson(outputLedgers);
LOG.debug("output body: " + jsonResponse);
response.setBody(jsonResponse);
return response;
}
} catch (Exception e) {
LOG.error("Exception occurred while listing under replicated ledgers", e);
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Exception when get." + 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 ReadLedgerEntryService method handle.
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
HttpServiceResponse response = new HttpServiceResponse();
Map<String, String> params = request.getParams();
if (HttpServer.Method.GET == request.getMethod() && (params != null) && params.containsKey("ledger_id")) {
Long ledgerId = Long.parseLong(params.get("ledger_id"));
Long startEntryId = 0L;
Long endEntryId = -1L;
if (params.containsKey("start_entry_id")) {
startEntryId = Long.parseLong(params.get("start_entry_id"));
}
if (params.containsKey("end_entry_id")) {
endEntryId = Long.parseLong(params.get("end_entry_id"));
}
// output <entryid: entry_content>
Map<String, String> output = Maps.newHashMap();
// Page index should start from 1;
Integer pageIndex = params.containsKey("page") ? Integer.parseInt(params.get("page")) : -1;
if (pageIndex > 0) {
// start and end ledger index for wanted page.
Long startIndexInPage = (pageIndex - 1) * ENTRIES_PER_PAE;
Long endIndexInPage = startIndexInPage + ENTRIES_PER_PAE - 1;
if ((startEntryId == 0L) || (startEntryId < startIndexInPage)) {
startEntryId = startIndexInPage;
}
if ((endEntryId == -1L) || (endEntryId > endIndexInPage)) {
endEntryId = endIndexInPage;
}
output.put("Entries for page: ", pageIndex.toString());
}
if (endEntryId != -1L && startEntryId > endEntryId) {
response.setCode(HttpServer.StatusCode.INTERNAL_ERROR);
response.setBody("parameter for start_entry_id: " + startEntryId + " and end_entry_id: " + endEntryId + " conflict with page=" + pageIndex);
return response;
}
Iterator<LedgerEntry> entries = bka.readEntries(ledgerId, startEntryId, endEntryId).iterator();
while (entries.hasNext()) {
LedgerEntry entry = entries.next();
output.put(Long.valueOf(entry.getEntryId()).toString(), new String(entry.getEntry(), US_ASCII));
}
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, with ledger_id provided");
return response;
}
}
use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.
the class TriggerAuditService method handle.
/*
* Force trigger the Audit by resetting the lostBookieRecoveryDelay.
*/
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
HttpServiceResponse response = new HttpServiceResponse();
if (HttpServer.Method.PUT == request.getMethod()) {
try {
bka.triggerAudit();
} catch (Exception e) {
LOG.error("Meet Exception: ", e);
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Exception when do operation." + e.getMessage());
return response;
}
response.setCode(HttpServer.StatusCode.OK);
response.setBody("Success trigger audit.");
LOG.debug("response body:" + response.getBody());
return response;
} else {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Not found method. Should be PUT method");
return response;
}
}
Aggregations