Skip to content

Commit 6bf61a7

Browse files
author
maxlisongsong
committed
Adds the bookie cookie service for http api
1 parent 96c6dc0 commit 6bf61a7

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

bookkeeper-http/http-server/src/main/java/org/apache/bookkeeper/http/HttpRouter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public abstract class HttpRouter<Handler> {
5656
public static final String BOOKIE_INFO = "/api/v1/bookie/info";
5757
public static final String CLUSTER_INFO = "/api/v1/bookie/cluster_info";
5858
public static final String ENTRY_LOCATION_COMPACT = "/api/v1/bookie/entry_location_compact";
59+
public static final String BOOKIE_COOKIE = "/api/v1/bookie/cookie";
5960
// autorecovery
6061
public static final String AUTORECOVERY_STATUS = "/api/v1/autorecovery/status";
6162
public static final String RECOVERY_BOOKIE = "/api/v1/autorecovery/bookie";
@@ -100,6 +101,7 @@ public HttpRouter(AbstractHttpHandlerFactory<Handler> handlerFactory) {
100101
handlerFactory.newHandler(HttpServer.ApiType.RESUME_GC_COMPACTION));
101102
this.endpointHandlers.put(ENTRY_LOCATION_COMPACT,
102103
handlerFactory.newHandler(HttpServer.ApiType.TRIGGER_ENTRY_LOCATION_COMPACT));
104+
this.endpointHandlers.put(BOOKIE_COOKIE, handlerFactory.newHandler(HttpServer.ApiType.BOOKIE_COOKIE));
103105

104106
// autorecovery
105107
this.endpointHandlers.put(AUTORECOVERY_STATUS, handlerFactory

bookkeeper-http/http-server/src/main/java/org/apache/bookkeeper/http/HttpServer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ enum ApiType {
9191
RESUME_GC_COMPACTION,
9292
SUSPEND_GC_COMPACTION,
9393
TRIGGER_ENTRY_LOCATION_COMPACT,
94+
BOOKIE_COOKIE,
9495
// autorecovery
9596
AUTORECOVERY_STATUS,
9697
RECOVERY_BOOKIE,

bookkeeper-server/src/main/java/org/apache/bookkeeper/server/http/BKHttpServiceProvider.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apache.bookkeeper.replication.Auditor;
3939
import org.apache.bookkeeper.replication.AutoRecoveryMain;
4040
import org.apache.bookkeeper.server.http.service.AutoRecoveryStatusService;
41+
import org.apache.bookkeeper.server.http.service.BookieCookieService;
4142
import org.apache.bookkeeper.server.http.service.BookieInfoService;
4243
import org.apache.bookkeeper.server.http.service.BookieIsReadyService;
4344
import org.apache.bookkeeper.server.http.service.BookieSanityService;
@@ -238,6 +239,8 @@ public HttpEndpointService provideHttpEndpointService(ApiType type) {
238239
return new ResumeCompactionService(bookieServer);
239240
case TRIGGER_ENTRY_LOCATION_COMPACT:
240241
return new TriggerLocationCompactService(bookieServer);
242+
case BOOKIE_COOKIE:
243+
return new BookieCookieService(configuration);
241244

242245
// autorecovery
243246
case AUTORECOVERY_STATUS:
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.apache.bookkeeper.server.http.service;
2+
3+
import java.net.UnknownHostException;
4+
import java.util.Map;
5+
import org.apache.bookkeeper.bookie.BookieException;
6+
import org.apache.bookkeeper.bookie.Cookie;
7+
import org.apache.bookkeeper.conf.ServerConfiguration;
8+
import org.apache.bookkeeper.http.HttpServer;
9+
import org.apache.bookkeeper.http.service.HttpEndpointService;
10+
import org.apache.bookkeeper.http.service.HttpServiceRequest;
11+
import org.apache.bookkeeper.http.service.HttpServiceResponse;
12+
import org.apache.bookkeeper.meta.MetadataDrivers;
13+
import org.apache.bookkeeper.net.BookieId;
14+
import org.apache.bookkeeper.net.BookieSocketAddress;
15+
import org.apache.bookkeeper.versioning.LongVersion;
16+
import org.apache.bookkeeper.versioning.Versioned;
17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
19+
20+
public class BookieCookieService implements HttpEndpointService {
21+
static final Logger LOG = LoggerFactory.getLogger(BookieCookieService.class);
22+
private final ServerConfiguration conf;
23+
24+
public BookieCookieService(ServerConfiguration conf) {
25+
this.conf = conf;
26+
}
27+
28+
@Override
29+
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
30+
Map<String, String> params = request.getParams();
31+
if (params == null || !params.containsKey("bookie_id")) {
32+
return new HttpServiceResponse("Not found bookie id. Should provide bookie_id=<ip:port>",
33+
HttpServer.StatusCode.BAD_REQUEST);
34+
}
35+
36+
String bookieIdStr = params.get("bookie_id");
37+
try {
38+
new BookieSocketAddress(bookieIdStr);
39+
} catch (UnknownHostException nhe) {
40+
return new HttpServiceResponse("Illegal bookie id. Should provide bookie_id=<ip:port>",
41+
HttpServer.StatusCode.BAD_REQUEST);
42+
}
43+
44+
BookieId bookieId = BookieId.parse(bookieIdStr);
45+
return MetadataDrivers.runFunctionWithRegistrationManager(conf, registrationManager -> {
46+
try {
47+
switch (request.getMethod()) {
48+
case GET:
49+
Versioned<Cookie> cookie = Cookie.readFromRegistrationManager(registrationManager, bookieId);
50+
return new HttpServiceResponse(cookie.toString(), HttpServer.StatusCode.OK);
51+
case DELETE:
52+
registrationManager.removeCookie(bookieId, new LongVersion(-1));
53+
return new HttpServiceResponse("Deleted cookie: " + bookieId, HttpServer.StatusCode.OK);
54+
default:
55+
return new HttpServiceResponse("Method not allowed. Should be GET or DELETE method",
56+
HttpServer.StatusCode.METHOD_NOT_ALLOWED);
57+
}
58+
} catch (BookieException.CookieNotFoundException e) {
59+
return new HttpServiceResponse("Not found cookie: " + bookieId, HttpServer.StatusCode.NOT_FOUND);
60+
} catch (BookieException e) {
61+
LOG.error("Failed to op bookie cookie: ", e);
62+
return new HttpServiceResponse("Request failed, e:" + e.getMessage(),
63+
HttpServer.StatusCode.INTERNAL_ERROR);
64+
}
65+
});
66+
}
67+
}

bookkeeper-server/src/test/java/org/apache/bookkeeper/server/http/TestHttpService.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.bookkeeper.server.http;
2020

2121
import static org.apache.bookkeeper.meta.MetadataDrivers.runFunctionWithLedgerManagerFactory;
22+
import static org.apache.bookkeeper.meta.MetadataDrivers.runFunctionWithRegistrationManager;
2223
import static org.junit.jupiter.api.Assertions.assertEquals;
2324
import static org.junit.jupiter.api.Assertions.assertFalse;
2425
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -39,6 +40,7 @@
3940
import java.util.concurrent.Future;
4041
import lombok.Cleanup;
4142
import org.apache.bookkeeper.bookie.BookieResources;
43+
import org.apache.bookkeeper.bookie.Cookie;
4244
import org.apache.bookkeeper.bookie.LedgerStorage;
4345
import org.apache.bookkeeper.client.BookKeeper;
4446
import org.apache.bookkeeper.client.ClientUtil;
@@ -55,6 +57,7 @@
5557
import org.apache.bookkeeper.meta.LedgerManagerFactory;
5658
import org.apache.bookkeeper.meta.LedgerUnderreplicationManager;
5759
import org.apache.bookkeeper.meta.MetadataBookieDriver;
60+
import org.apache.bookkeeper.net.BookieId;
5861
import org.apache.bookkeeper.net.BookieSocketAddress;
5962
import org.apache.bookkeeper.proto.BookieServer;
6063
import org.apache.bookkeeper.replication.AuditorElector;
@@ -66,6 +69,7 @@
6669
import org.apache.bookkeeper.server.http.service.ClusterInfoService;
6770
import org.apache.bookkeeper.stats.NullStatsLogger;
6871
import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
72+
import org.apache.bookkeeper.versioning.Versioned;
6973
import org.junit.jupiter.api.AfterEach;
7074
import org.junit.jupiter.api.BeforeEach;
7175
import org.junit.jupiter.api.Test;
@@ -1217,4 +1221,51 @@ public void testTriggerEntryLocationCompactService() throws Exception {
12171221
HttpServiceResponse response7 = triggerEntryLocationCompactService.handle(request7);
12181222
assertEquals(HttpServer.StatusCode.METHOD_NOT_ALLOWED.getValue(), response7.getStatusCode());
12191223
}
1224+
1225+
@Test
1226+
public void testBookieCookieService() throws Exception {
1227+
runFunctionWithRegistrationManager(baseConf, registrationManager -> {
1228+
try {
1229+
String bookieId = getBookie(0).toString();
1230+
Versioned<Cookie> cookieFromZk = Cookie.readFromRegistrationManager(registrationManager,
1231+
BookieId.parse(bookieId));
1232+
HttpEndpointService bookieCookieService = bkHttpServiceProvider.provideHttpEndpointService(HttpServer.ApiType.BOOKIE_COOKIE);
1233+
Map<String, String> params = new HashMap<>();
1234+
// empty params
1235+
HttpServiceRequest request = new HttpServiceRequest(null, HttpServer.Method.GET, params);
1236+
HttpServiceResponse response = bookieCookieService.handle(request);
1237+
assertEquals(response.getStatusCode(), HttpServer.StatusCode.BAD_REQUEST.getValue());
1238+
assertEquals(response.getBody(), "Not found bookie id. Should provide bookie_id=<ip:port>");
1239+
// invalid params
1240+
params.put("bookie_id", "bookie_id");
1241+
response = bookieCookieService.handle(request);
1242+
assertEquals(response.getStatusCode(), HttpServer.StatusCode.BAD_REQUEST.getValue());
1243+
assertEquals(response.getBody(), "Illegal bookie id. Should provide bookie_id=<ip:port>");
1244+
1245+
// cookie not found
1246+
params.put("bookie_id", "127.2.1.0:3181");
1247+
response = bookieCookieService.handle(request);
1248+
assertEquals(response.getStatusCode(), HttpServer.StatusCode.NOT_FOUND.getValue());
1249+
1250+
params.put("bookie_id", bookieId);
1251+
// GET cookie
1252+
HttpServiceRequest request1 = new HttpServiceRequest(null, HttpServer.Method.GET, params);
1253+
HttpServiceResponse response1 = bookieCookieService.handle(request1);
1254+
assertEquals(response1.getStatusCode(), HttpServer.StatusCode.OK.getValue());
1255+
assertEquals(cookieFromZk.toString(), response1.getBody());
1256+
1257+
// DELETE cookie
1258+
HttpServiceRequest request2 = new HttpServiceRequest(null, HttpServer.Method.DELETE, params);
1259+
HttpServiceResponse response2 = bookieCookieService.handle(request2);
1260+
assertEquals(response2.getStatusCode(), HttpServer.StatusCode.OK.getValue());
1261+
1262+
// GET cookie
1263+
HttpServiceResponse response3 = bookieCookieService.handle(request1);
1264+
assertEquals(response3.getStatusCode(), HttpServer.StatusCode.NOT_FOUND.getValue());
1265+
return true;
1266+
} catch (Exception e) {
1267+
throw new RuntimeException(e);
1268+
}
1269+
});
1270+
}
12201271
}

0 commit comments

Comments
 (0)