Search in sources :

Example 11 with HttpServiceResponse

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;
    }
}
Also used : BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse)

Example 12 with HttpServiceResponse

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;
    }
}
Also used : HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) File(java.io.File)

Example 13 with HttpServiceResponse

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;
    }
}
Also used : HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) StringUtils(org.apache.commons.lang.StringUtils) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) LedgerUnderreplicationManager(org.apache.bookkeeper.meta.LedgerUnderreplicationManager) Predicate(java.util.function.Predicate) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) LoggerFactory(org.slf4j.LoggerFactory) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) HttpServer(org.apache.bookkeeper.http.HttpServer) BookieServer(org.apache.bookkeeper.proto.BookieServer) LedgerManagerFactory(org.apache.bookkeeper.meta.LedgerManagerFactory) List(java.util.List) Lists(com.google.common.collect.Lists) Map(java.util.Map) HttpEndpointService(org.apache.bookkeeper.http.service.HttpEndpointService) JsonUtil(org.apache.bookkeeper.util.JsonUtil) LedgerUnderreplicationManager(org.apache.bookkeeper.meta.LedgerUnderreplicationManager) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) List(java.util.List) LedgerManagerFactory(org.apache.bookkeeper.meta.LedgerManagerFactory)

Example 14 with HttpServiceResponse

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;
    }
}
Also used : LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse)

Example 15 with HttpServiceResponse

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;
    }
}
Also used : HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse)

Aggregations

HttpServiceResponse (org.apache.bookkeeper.http.service.HttpServiceResponse)35 HttpServiceRequest (org.apache.bookkeeper.http.service.HttpServiceRequest)19 HttpEndpointService (org.apache.bookkeeper.http.service.HttpEndpointService)18 Test (org.junit.Test)16 BookKeeper (org.apache.bookkeeper.client.BookKeeper)8 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)7 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)6 Map (java.util.Map)4 File (java.io.File)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 LedgerManager (org.apache.bookkeeper.meta.LedgerManager)3 LedgerManagerFactory (org.apache.bookkeeper.meta.LedgerManagerFactory)3 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)2 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)2 ErrorHttpService (org.apache.bookkeeper.http.service.ErrorHttpService)2 LedgerUnderreplicationManager (org.apache.bookkeeper.meta.LedgerUnderreplicationManager)2 JsonUtil (org.apache.bookkeeper.util.JsonUtil)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Lists (com.google.common.collect.Lists)1