Search in sources :

Example 6 with HttpServiceRequest

use of org.apache.bookkeeper.http.service.HttpServiceRequest 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 7 with HttpServiceRequest

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

the class TwitterAbstractHandler method processRequest.

/**
 * Process the request using the given httpEndpointService.
 */
Future<Response> processRequest(HttpEndpointService httpEndpointService, Request request) {
    HttpServiceRequest httpServiceRequest = new HttpServiceRequest().setMethod(convertMethod(request)).setParams(convertParams(request)).setBody(request.contentString());
    HttpServiceResponse httpServiceResponse = null;
    try {
        httpServiceResponse = httpEndpointService.handle(httpServiceRequest);
    } catch (Exception e) {
        httpServiceResponse = new ErrorHttpService().handle(httpServiceRequest);
    }
    Response response = Response.apply();
    response.setContentString(httpServiceResponse.getBody());
    response.statusCode(httpServiceResponse.getStatusCode());
    return Future.value(response);
}
Also used : Response(com.twitter.finagle.http.Response) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) ErrorHttpService(org.apache.bookkeeper.http.service.ErrorHttpService) HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse)

Example 8 with HttpServiceRequest

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

the class VertxAbstractHandler method processRequest.

/**
 * Process the request using the given httpEndpointService.
 */
void processRequest(HttpEndpointService httpEndpointService, RoutingContext context) {
    HttpServerRequest httpRequest = context.request();
    HttpServerResponse httpResponse = context.response();
    HttpServiceRequest request = new HttpServiceRequest().setMethod(convertMethod(httpRequest)).setParams(convertParams(httpRequest)).setBody(context.getBodyAsString());
    HttpServiceResponse response = null;
    try {
        response = httpEndpointService.handle(request);
    } catch (Exception e) {
        response = new ErrorHttpService().handle(request);
    }
    httpResponse.setStatusCode(response.getStatusCode());
    httpResponse.end(response.getBody());
}
Also used : ErrorHttpService(org.apache.bookkeeper.http.service.ErrorHttpService) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse)

Example 9 with HttpServiceRequest

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

the class TestHttpService method testWhoIsAuditorService.

@Test
public void testWhoIsAuditorService() throws Exception {
    baseConf.setZkServers(zkUtil.getZooKeeperConnectString());
    startAuditorElector();
    HttpEndpointService whoIsAuditorService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.WHO_IS_AUDITOR);
    // 1,  GET, should return success
    HttpServiceRequest request1 = new HttpServiceRequest(null, HttpServer.Method.GET, null);
    HttpServiceResponse response1 = whoIsAuditorService.handle(request1);
    assertEquals(HttpServer.StatusCode.OK.getValue(), response1.getStatusCode());
    LOG.info(response1.getBody());
    stopAuditorElector();
}
Also used : HttpEndpointService(org.apache.bookkeeper.http.service.HttpEndpointService) HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) Test(org.junit.Test)

Example 10 with HttpServiceRequest

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

the class TestHttpService method testRecoveryBookieService.

@Test
public void testRecoveryBookieService() throws Exception {
    baseConf.setZkServers(zkUtil.getZooKeeperConnectString());
    HttpEndpointService recoveryBookieService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.RECOVERY_BOOKIE);
    // 1,  null body of GET, should return error
    HttpServiceRequest request1 = new HttpServiceRequest(null, HttpServer.Method.GET, null);
    HttpServiceResponse response1 = recoveryBookieService.handle(request1);
    assertEquals(HttpServer.StatusCode.NOT_FOUND.getValue(), response1.getStatusCode());
    // 2,  null body of PUT, should return error
    HttpServiceRequest request2 = new HttpServiceRequest(null, HttpServer.Method.PUT, null);
    HttpServiceResponse response2 = recoveryBookieService.handle(request2);
    assertEquals(HttpServer.StatusCode.NOT_FOUND.getValue(), response2.getStatusCode());
    // 3, body with bookie_src, bookie_dest and delete_cookie of PUT, should success.
    String bookieSrc = getBookie(0).toString();
    String putBody3 = "{\"bookie_src\": [ \"" + bookieSrc + "\" ]," + "\"delete_cookie\": false }";
    HttpServiceRequest request3 = new HttpServiceRequest(putBody3, HttpServer.Method.PUT, null);
    HttpServiceResponse response3 = recoveryBookieService.handle(request3);
    assertEquals(HttpServer.StatusCode.OK.getValue(), response3.getStatusCode());
    // 5, body with bookie_src of PUT, should success.
    String putBody5 = "{\"bookie_src\": [ \"" + bookieSrc + "\" ] }";
    HttpServiceRequest request5 = new HttpServiceRequest(putBody5, HttpServer.Method.PUT, null);
    HttpServiceResponse response5 = recoveryBookieService.handle(request5);
    assertEquals(HttpServer.StatusCode.OK.getValue(), response5.getStatusCode());
}
Also used : HttpEndpointService(org.apache.bookkeeper.http.service.HttpEndpointService) HttpServiceRequest(org.apache.bookkeeper.http.service.HttpServiceRequest) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse) Test(org.junit.Test)

Aggregations

HttpServiceRequest (org.apache.bookkeeper.http.service.HttpServiceRequest)19 HttpServiceResponse (org.apache.bookkeeper.http.service.HttpServiceResponse)19 HttpEndpointService (org.apache.bookkeeper.http.service.HttpEndpointService)17 Test (org.junit.Test)15 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)7 BookKeeper (org.apache.bookkeeper.client.BookKeeper)6 Map (java.util.Map)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ErrorHttpService (org.apache.bookkeeper.http.service.ErrorHttpService)2 LedgerUnderreplicationManager (org.apache.bookkeeper.meta.LedgerUnderreplicationManager)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Lists (com.google.common.collect.Lists)1 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 Response (com.twitter.finagle.http.Response)1 HttpServerRequest (io.vertx.core.http.HttpServerRequest)1 HttpServerResponse (io.vertx.core.http.HttpServerResponse)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Predicate (java.util.function.Predicate)1