Search in sources :

Example 16 with HttpServiceResponse

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

Example 17 with HttpServiceResponse

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

Example 18 with HttpServiceResponse

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:
             *  {
             *    "&lt;Journal_id&gt;" : "&lt;Pos&gt;",
             *    ...
             *  }
             */
            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;
    }
}
Also used : LogMark(org.apache.bookkeeper.bookie.LogMark) LedgerDirsManager(org.apache.bookkeeper.bookie.LedgerDirsManager) DiskChecker(org.apache.bookkeeper.util.DiskChecker) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) Journal(org.apache.bookkeeper.bookie.Journal) File(java.io.File)

Example 19 with HttpServiceResponse

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;
    }
}
Also used : BookieInfoReader(org.apache.bookkeeper.client.BookieInfoReader) BookKeeper(org.apache.bookkeeper.client.BookKeeper) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration)

Example 20 with HttpServiceResponse

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