|
18 | 18 | */ |
19 | 19 | package org.apache.bookkeeper.server.http; |
20 | 20 |
|
| 21 | +import static java.nio.charset.StandardCharsets.UTF_8; |
21 | 22 | import static org.apache.bookkeeper.meta.MetadataDrivers.runFunctionWithLedgerManagerFactory; |
| 23 | +import static org.apache.bookkeeper.meta.MetadataDrivers.runFunctionWithRegistrationManager; |
22 | 24 | import static org.junit.jupiter.api.Assertions.assertEquals; |
23 | 25 | import static org.junit.jupiter.api.Assertions.assertFalse; |
24 | 26 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
|
39 | 41 | import java.util.concurrent.Future; |
40 | 42 | import lombok.Cleanup; |
41 | 43 | import org.apache.bookkeeper.bookie.BookieResources; |
| 44 | +import org.apache.bookkeeper.bookie.Cookie; |
42 | 45 | import org.apache.bookkeeper.bookie.LedgerStorage; |
43 | 46 | import org.apache.bookkeeper.client.BookKeeper; |
44 | 47 | import org.apache.bookkeeper.client.ClientUtil; |
|
55 | 58 | import org.apache.bookkeeper.meta.LedgerManagerFactory; |
56 | 59 | import org.apache.bookkeeper.meta.LedgerUnderreplicationManager; |
57 | 60 | import org.apache.bookkeeper.meta.MetadataBookieDriver; |
| 61 | +import org.apache.bookkeeper.net.BookieId; |
58 | 62 | import org.apache.bookkeeper.net.BookieSocketAddress; |
59 | 63 | import org.apache.bookkeeper.proto.BookieServer; |
60 | 64 | import org.apache.bookkeeper.replication.AuditorElector; |
|
66 | 70 | import org.apache.bookkeeper.server.http.service.ClusterInfoService; |
67 | 71 | import org.apache.bookkeeper.stats.NullStatsLogger; |
68 | 72 | import org.apache.bookkeeper.test.BookKeeperClusterTestCase; |
| 73 | +import org.apache.bookkeeper.versioning.Versioned; |
69 | 74 | import org.junit.jupiter.api.AfterEach; |
70 | 75 | import org.junit.jupiter.api.BeforeEach; |
71 | 76 | import org.junit.jupiter.api.Test; |
@@ -1217,4 +1222,63 @@ public void testTriggerEntryLocationCompactService() throws Exception { |
1217 | 1222 | HttpServiceResponse response7 = triggerEntryLocationCompactService.handle(request7); |
1218 | 1223 | assertEquals(HttpServer.StatusCode.METHOD_NOT_ALLOWED.getValue(), response7.getStatusCode()); |
1219 | 1224 | } |
| 1225 | + |
| 1226 | + @SuppressWarnings("checkstyle:RegexpSingleline") |
| 1227 | + @Test |
| 1228 | + public void testBookieCookieService() throws Exception { |
| 1229 | + runFunctionWithRegistrationManager(baseConf, registrationManager -> { |
| 1230 | + try { |
| 1231 | + String bookieId = getBookie(0).toString(); |
| 1232 | + Versioned<Cookie> cookieFromZk = Cookie.readFromRegistrationManager(registrationManager, |
| 1233 | + BookieId.parse(bookieId)); |
| 1234 | + HttpEndpointService bookieCookieService = bkHttpServiceProvider.provideHttpEndpointService( |
| 1235 | + HttpServer.ApiType.BOOKIE_COOKIE); |
| 1236 | + Map<String, String> params = new HashMap<>(); |
| 1237 | + // empty params |
| 1238 | + HttpServiceRequest request = new HttpServiceRequest(null, HttpServer.Method.GET, params); |
| 1239 | + HttpServiceResponse response = bookieCookieService.handle(request); |
| 1240 | + assertEquals(response.getStatusCode(), HttpServer.StatusCode.BAD_REQUEST.getValue()); |
| 1241 | + assertEquals(response.getBody(), "Not found bookie id. Should provide bookie_id=<ip:port>"); |
| 1242 | + // invalid params |
| 1243 | + params.put("bookie_id", "bookie_id"); |
| 1244 | + response = bookieCookieService.handle(request); |
| 1245 | + assertEquals(response.getStatusCode(), HttpServer.StatusCode.BAD_REQUEST.getValue()); |
| 1246 | + assertEquals(response.getBody(), "Illegal bookie id. Should provide bookie_id=<ip:port>"); |
| 1247 | + |
| 1248 | + // cookie not found |
| 1249 | + params.put("bookie_id", "127.2.1.0:3181"); |
| 1250 | + response = bookieCookieService.handle(request); |
| 1251 | + assertEquals(response.getStatusCode(), HttpServer.StatusCode.NOT_FOUND.getValue()); |
| 1252 | + |
| 1253 | + params.put("bookie_id", bookieId); |
| 1254 | + // GET cookie |
| 1255 | + HttpServiceRequest request1 = new HttpServiceRequest(null, HttpServer.Method.GET, params); |
| 1256 | + HttpServiceResponse response1 = bookieCookieService.handle(request1); |
| 1257 | + assertEquals(response1.getStatusCode(), HttpServer.StatusCode.OK.getValue()); |
| 1258 | + assertEquals(cookieFromZk.getValue().toString(), response1.getBody()); |
| 1259 | + Cookie old = Cookie.parseFromBytes(response1.getBody().getBytes(UTF_8)); |
| 1260 | + // DELETE cookie |
| 1261 | + HttpServiceRequest request2 = new HttpServiceRequest(null, HttpServer.Method.DELETE, params); |
| 1262 | + HttpServiceResponse response2 = bookieCookieService.handle(request2); |
| 1263 | + assertEquals(response2.getStatusCode(), HttpServer.StatusCode.OK.getValue()); |
| 1264 | + |
| 1265 | + // GET cookie |
| 1266 | + HttpServiceResponse response3 = bookieCookieService.handle(request1); |
| 1267 | + assertEquals(response3.getStatusCode(), HttpServer.StatusCode.NOT_FOUND.getValue()); |
| 1268 | + |
| 1269 | + // create cookie |
| 1270 | + params.put("cookie", old.toString()); |
| 1271 | + HttpServiceRequest request3 = new HttpServiceRequest(null, HttpServer.Method.PUT, params); |
| 1272 | + HttpServiceResponse response4 = bookieCookieService.handle(request3); |
| 1273 | + assertEquals(response4.getStatusCode(), HttpServer.StatusCode.OK.getValue()); |
| 1274 | + |
| 1275 | + HttpServiceResponse response5 = bookieCookieService.handle(request1); |
| 1276 | + assertEquals(response5.getStatusCode(), HttpServer.StatusCode.OK.getValue()); |
| 1277 | + assertEquals(cookieFromZk.getValue().toString(), response5.getBody()); |
| 1278 | + return true; |
| 1279 | + } catch (Exception e) { |
| 1280 | + throw new RuntimeException(e); |
| 1281 | + } |
| 1282 | + }); |
| 1283 | + } |
1220 | 1284 | } |
0 commit comments