Search in sources :

Example 6 with HttpServiceResponse

use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.

the class TestHttpService method testHeartbeatService.

@Test
public void testHeartbeatService() throws Exception {
    // test heartbeat service
    HttpEndpointService heartbeatService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.HEARTBEAT);
    HttpServiceResponse response = heartbeatService.handle(null);
    assertEquals(HttpServer.StatusCode.OK.getValue(), response.getStatusCode());
    assertEquals("OK\n", response.getBody());
}
Also used : HttpEndpointService(org.apache.bookkeeper.http.service.HttpEndpointService) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) Test(org.junit.Test)

Example 7 with HttpServiceResponse

use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.

the class ExpandStorageService method handle.

/*
     * Add new empty ledger/index directories.
     * Update the directories info in the conf file before running the command.
     */
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
    HttpServiceResponse response = new HttpServiceResponse();
    if (HttpServer.Method.PUT == request.getMethod()) {
        File[] ledgerDirectories = Bookie.getCurrentDirectories(conf.getLedgerDirs());
        File[] journalDirectories = Bookie.getCurrentDirectories(conf.getJournalDirs());
        File[] indexDirectories;
        if (null == conf.getIndexDirs()) {
            indexDirectories = ledgerDirectories;
        } else {
            indexDirectories = Bookie.getCurrentDirectories(conf.getIndexDirs());
        }
        List<File> allLedgerDirs = Lists.newArrayList();
        allLedgerDirs.addAll(Arrays.asList(ledgerDirectories));
        if (indexDirectories != ledgerDirectories) {
            allLedgerDirs.addAll(Arrays.asList(indexDirectories));
        }
        try (MetadataBookieDriver driver = MetadataDrivers.getBookieDriver(URI.create(conf.getMetadataServiceUri()))) {
            driver.initialize(conf, () -> {
            }, NullStatsLogger.INSTANCE);
            Bookie.checkEnvironmentWithStorageExpansion(conf, driver, Lists.newArrayList(journalDirectories), allLedgerDirs);
        } catch (BookieException e) {
            LOG.error("Exception occurred while updating cookie for storage expansion", e);
            response.setCode(HttpServer.StatusCode.INTERNAL_ERROR);
            response.setBody("Exception while updating cookie for storage expansion");
            return response;
        }
        String jsonResponse = "Success expand storage";
        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 PUT method");
        return response;
    }
}
Also used : MetadataBookieDriver(org.apache.bookkeeper.meta.MetadataBookieDriver) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) File(java.io.File) BookieException(org.apache.bookkeeper.bookie.BookieException)

Example 8 with HttpServiceResponse

use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.

the class ListLedgerService method handle.

@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
    HttpServiceResponse response = new HttpServiceResponse();
    // parameter could be like: print_metadata=true&page=PageIndex
    if (HttpServer.Method.GET == request.getMethod()) {
        Map<String, String> params = request.getParams();
        // default not print metadata
        boolean printMeta = (params != null) && params.containsKey("print_metadata") && params.get("print_metadata").equals("true");
        // Page index should start from 1;
        int pageIndex = (printMeta && params.containsKey("page")) ? Integer.parseInt(params.get("page")) : -1;
        LedgerManagerFactory mFactory = bookieServer.getBookie().getLedgerManagerFactory();
        LedgerManager manager = mFactory.newLedgerManager();
        LedgerManager.LedgerRangeIterator iter = manager.getLedgerRanges();
        // output <ledgerId: ledgerMetadata>
        LinkedHashMap<String, String> output = Maps.newLinkedHashMap();
        // futures for readLedgerMetadata for each page.
        List<ReadLedgerMetadataCallback> futures = Lists.newArrayListWithExpectedSize(LIST_LEDGER_BATCH_SIZE);
        if (printMeta) {
            int ledgerIndex = 0;
            // start and end ledger index for wanted page.
            int startLedgerIndex = 0;
            int endLedgerIndex = 0;
            if (pageIndex > 0) {
                startLedgerIndex = (pageIndex - 1) * LIST_LEDGER_BATCH_SIZE;
                endLedgerIndex = startLedgerIndex + LIST_LEDGER_BATCH_SIZE - 1;
            }
            // get metadata
            while (iter.hasNext()) {
                LedgerManager.LedgerRange r = iter.next();
                for (Long lid : r.getLedgers()) {
                    ledgerIndex++;
                    if (// no actual page parameter provided
                    endLedgerIndex == 0 || (ledgerIndex >= startLedgerIndex && ledgerIndex <= endLedgerIndex)) {
                        ReadLedgerMetadataCallback cb = new ReadLedgerMetadataCallback(lid);
                        manager.readLedgerMetadata(lid, cb);
                        futures.add(cb);
                    }
                }
                if (futures.size() >= LIST_LEDGER_BATCH_SIZE) {
                    while (futures.size() > 0) {
                        ReadLedgerMetadataCallback cb = futures.remove(0);
                        keepLedgerMetadata(cb, output);
                    }
                }
            }
            while (futures.size() > 0) {
                ReadLedgerMetadataCallback cb = futures.remove(0);
                keepLedgerMetadata(cb, output);
            }
        } else {
            while (iter.hasNext()) {
                LedgerManager.LedgerRange r = iter.next();
                for (Long lid : r.getLedgers()) {
                    output.put(lid.toString(), null);
                }
            }
        }
        manager.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 : LedgerManager(org.apache.bookkeeper.meta.LedgerManager) LedgerManagerFactory(org.apache.bookkeeper.meta.LedgerManagerFactory) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse)

Example 9 with HttpServiceResponse

use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.

the class LostBookieRecoveryDelayService method handle.

/*
     * set/get lostBookieRecoveryDelay.
     */
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
    HttpServiceResponse response = new HttpServiceResponse();
    if (HttpServer.Method.PUT == request.getMethod()) {
        // request body as {"delay_seconds": <delay_seconds>}
        String requestBody = request.getBody();
        if (requestBody == null) {
            response.setCode(HttpServer.StatusCode.NOT_FOUND);
            response.setBody("Null request body for lostBookieRecoveryDelay.");
            return response;
        }
        @SuppressWarnings("unchecked") HashMap<String, Integer> configMap = JsonUtil.fromJson(requestBody, HashMap.class);
        if (configMap != null && configMap.containsKey("delay_seconds")) {
            int delaySeconds = configMap.get("delay_seconds");
            bka.setLostBookieRecoveryDelay(delaySeconds);
            response.setCode(HttpServer.StatusCode.OK);
            response.setBody("Success set lostBookieRecoveryDelay to " + delaySeconds);
            return response;
        } else {
            response.setCode(HttpServer.StatusCode.NOT_FOUND);
            response.setBody("Request body not contains lostBookieRecoveryDelay.");
            return response;
        }
    } else if (HttpServer.Method.GET == request.getMethod()) {
        try {
            int delaySeconds = bka.getLostBookieRecoveryDelay();
            response.setCode(HttpServer.StatusCode.OK);
            response.setBody("lostBookieRecoveryDelay value: " + delaySeconds);
            LOG.debug("response body:" + response.getBody());
            return response;
        } catch (Exception e) {
            // may get noNode exception
            LOG.error("Exception occurred while getting lost bookie recovery delay", e);
            response.setCode(HttpServer.StatusCode.NOT_FOUND);
            response.setBody("Exception when get lostBookieRecoveryDelay." + e.getMessage());
            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)

Example 10 with HttpServiceResponse

use of org.apache.bookkeeper.http.service.HttpServiceResponse in project bookkeeper by apache.

the class RecoveryBookieService method handle.

@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
    HttpServiceResponse response = new HttpServiceResponse();
    String requestBody = request.getBody();
    RecoveryRequestJsonBody requestJsonBody;
    if (requestBody == null) {
        response.setCode(HttpServer.StatusCode.NOT_FOUND);
        response.setBody("No request body provide.");
        return response;
    }
    try {
        requestJsonBody = JsonUtil.fromJson(requestBody, RecoveryRequestJsonBody.class);
        LOG.debug("bookie_src: [" + requestJsonBody.bookieSrc.get(0) + "],  delete_cookie: [" + requestJsonBody.deleteCookie + "]");
    } catch (JsonUtil.ParseJsonException e) {
        LOG.error("Meet Exception: ", e);
        response.setCode(HttpServer.StatusCode.NOT_FOUND);
        response.setBody("ERROR parameters: " + e.getMessage());
        return response;
    }
    if (HttpServer.Method.PUT == request.getMethod() && !requestJsonBody.bookieSrc.isEmpty()) {
        runFunctionWithRegistrationManager(conf, rm -> {
            String[] bookieSrcString = requestJsonBody.bookieSrc.get(0).split(":");
            BookieSocketAddress bookieSrc = new BookieSocketAddress(bookieSrcString[0], Integer.parseInt(bookieSrcString[1]));
            boolean deleteCookie = requestJsonBody.deleteCookie;
            executor.execute(() -> {
                try {
                    LOG.info("Start recovering bookie.");
                    bka.recoverBookieData(bookieSrc);
                    if (deleteCookie) {
                        Versioned<Cookie> cookie = Cookie.readFromRegistrationManager(rm, bookieSrc);
                        cookie.getValue().deleteFromRegistrationManager(rm, bookieSrc, cookie.getVersion());
                    }
                    LOG.info("Complete recovering bookie");
                } catch (Exception e) {
                    LOG.error("Exception occurred while recovering bookie", e);
                }
            });
            return null;
        });
        response.setCode(HttpServer.StatusCode.OK);
        response.setBody("Success send recovery request command.");
        return response;
    } else {
        response.setCode(HttpServer.StatusCode.NOT_FOUND);
        response.setBody("Not found method. Should be PUT method");
        return response;
    }
}
Also used : Cookie(org.apache.bookkeeper.bookie.Cookie) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) JsonUtil(org.apache.bookkeeper.util.JsonUtil)

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