Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Http-service to check bookie sanity state #3630

Merged
merged 3 commits into from Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -50,6 +50,7 @@ public abstract class HttpRouter<Handler> {
public static final String SUSPEND_GC_COMPACTION = "/api/v1/bookie/gc/suspend_compaction";
public static final String RESUME_GC_COMPACTION = "/api/v1/bookie/gc/resume_compaction";
public static final String BOOKIE_STATE = "/api/v1/bookie/state";
public static final String BOOKIE_SANITY = "/api/v1/bookie/sanity";
public static final String BOOKIE_STATE_READONLY = "/api/v1/bookie/state/readonly";
public static final String BOOKIE_IS_READY = "/api/v1/bookie/is_ready";
public static final String BOOKIE_INFO = "/api/v1/bookie/info";
Expand Down Expand Up @@ -85,6 +86,7 @@ public HttpRouter(AbstractHttpHandlerFactory<Handler> handlerFactory) {
this.endpointHandlers.put(GC, handlerFactory.newHandler(HttpServer.ApiType.GC));
this.endpointHandlers.put(GC_DETAILS, handlerFactory.newHandler(HttpServer.ApiType.GC_DETAILS));
this.endpointHandlers.put(BOOKIE_STATE, handlerFactory.newHandler(HttpServer.ApiType.BOOKIE_STATE));
this.endpointHandlers.put(BOOKIE_SANITY, handlerFactory.newHandler(HttpServer.ApiType.BOOKIE_SANITY));
this.endpointHandlers.put(BOOKIE_STATE_READONLY,
handlerFactory.newHandler(HttpServer.ApiType.BOOKIE_STATE_READONLY));
this.endpointHandlers.put(BOOKIE_IS_READY, handlerFactory.newHandler(HttpServer.ApiType.BOOKIE_IS_READY));
Expand Down
Expand Up @@ -82,6 +82,7 @@ enum ApiType {
GC,
GC_DETAILS,
BOOKIE_STATE,
BOOKIE_SANITY,
BOOKIE_STATE_READONLY,
BOOKIE_IS_READY,
BOOKIE_INFO,
Expand Down
Expand Up @@ -40,6 +40,7 @@
import org.apache.bookkeeper.server.http.service.AutoRecoveryStatusService;
import org.apache.bookkeeper.server.http.service.BookieInfoService;
import org.apache.bookkeeper.server.http.service.BookieIsReadyService;
import org.apache.bookkeeper.server.http.service.BookieSanityService;
import org.apache.bookkeeper.server.http.service.BookieStateReadOnlyService;
import org.apache.bookkeeper.server.http.service.BookieStateService;
import org.apache.bookkeeper.server.http.service.ConfigurationService;
Expand Down Expand Up @@ -219,6 +220,8 @@ public HttpEndpointService provideHttpEndpointService(ApiType type) {
return new GCDetailsService(configuration, bookieServer);
case BOOKIE_STATE:
return new BookieStateService(bookieServer.getBookie());
case BOOKIE_SANITY:
return new BookieSanityService(configuration);
case BOOKIE_STATE_READONLY:
return new BookieStateReadOnlyService(bookieServer.getBookie());
case BOOKIE_IS_READY:
Expand Down
@@ -0,0 +1,100 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.bookkeeper.server.http.service;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.concurrent.Semaphore;

import org.apache.bookkeeper.bookie.StateManager;
import org.apache.bookkeeper.common.util.JsonUtil;
import org.apache.bookkeeper.conf.ServerConfiguration;
import org.apache.bookkeeper.http.HttpServer;
import org.apache.bookkeeper.http.service.HttpEndpointService;
import org.apache.bookkeeper.http.service.HttpServiceRequest;
import org.apache.bookkeeper.http.service.HttpServiceResponse;
import org.apache.bookkeeper.tools.cli.commands.bookie.SanityTestCommand;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
* HttpEndpointService that exposes the bookie sanity state.
*
* <p>Get the current bookie sanity response:
*
* <pre>
* <code>
* {
* "passed" : true,
* "readOnly" : false
*}
* </code>
* </pre>
*/
public class BookieSanityService implements HttpEndpointService {

private final ServerConfiguration config;
private Semaphore lock = new Semaphore(1);

public BookieSanityService(ServerConfiguration config) {
this.config = checkNotNull(config);
}

/**
* POJO definition for the bookie sanity response.
*/
@Data
@NoArgsConstructor
public static class BookieSanity {
private boolean passed;
private boolean readOnly;
}

@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
HttpServiceResponse response = new HttpServiceResponse();

if (HttpServer.Method.GET != request.getMethod()) {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Only support GET method to retrieve bookie sanity state.");
return response;
}

BookieSanity bs = new BookieSanity();
if (config.isForceReadOnlyBookie()) {
bs.readOnly = true;
} else {
try {
// allow max concurrent request as sanity-test check relatively
// longer time to complete
lock.acquire();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wait at most for X seconds (5?) and if the semaphore cannot be acquired fail the request. We don't want many requests piling up.

Please also fix formatting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

SanityTestCommand sanity = new SanityTestCommand();
bs.passed = sanity.apply(config, new SanityTestCommand.SanityFlags());
} finally {
lock.release();
}
}

String jsonResponse = JsonUtil.toJson(bs);
response.setBody(jsonResponse);
response.setCode(HttpServer.StatusCode.OK);
return response;
}
}
Expand Up @@ -52,6 +52,8 @@
import org.apache.bookkeeper.net.BookieSocketAddress;
import org.apache.bookkeeper.replication.AuditorElector;
import org.apache.bookkeeper.server.http.service.BookieInfoService;
import org.apache.bookkeeper.server.http.service.BookieSanityService;
import org.apache.bookkeeper.server.http.service.BookieSanityService.BookieSanity;
import org.apache.bookkeeper.server.http.service.BookieStateReadOnlyService.ReadOnlyState;
import org.apache.bookkeeper.server.http.service.BookieStateService.BookieState;
import org.apache.bookkeeper.stats.NullStatsLogger;
Expand Down Expand Up @@ -853,6 +855,27 @@ public void testGetBookieState() throws Exception {
assertEquals(false, bs.isShuttingDown());
}

@Test
public void testGetBookieSanity() throws Exception {
HttpEndpointService bookieStateServer = bkHttpServiceProvider
.provideHttpEndpointService(HttpServer.ApiType.BOOKIE_SANITY);

HttpServiceRequest request1 = new HttpServiceRequest(null, HttpServer.Method.GET, null);
ServerConfiguration conf = servers.get(0).getConfiguration();
BookieSanityService service = new BookieSanityService(conf);
HttpServiceResponse response1 = service.handle(request1);
assertEquals(HttpServer.StatusCode.OK.getValue(), response1.getStatusCode());
// run multiple iteration to validate any server side throttling doesn't
// fail sequential requests.
for (int i = 0; i < 3; i++) {
BookieSanity bs = JsonUtil.fromJson(response1.getBody(), BookieSanity.class);
assertEquals(true, bs.isPassed());
assertEquals(false, bs.isReadOnly());
}
HttpServiceResponse response2 = bookieStateServer.handle(request1);
assertEquals(HttpServer.StatusCode.OK.getValue(), response2.getStatusCode());
}

@Test
public void testGetBookieIsReady() throws Exception {
HttpEndpointService bookieStateServer = bkHttpServiceProvider
Expand Down
19 changes: 19 additions & 0 deletions site3/website/docs/admin/http.md
Expand Up @@ -413,6 +413,25 @@ Currently all the HTTP endpoints could be divided into these 5 components:
}
```

### Endpoint: /api/v1/bookie/sanity
1. Method: GET
* Description: Exposes the bookie sanity state
* Response:

| Code | Description |
|:-------|:------------|
|200 | Successful operation |
|403 | Permission denied |
|404 | Not found |
* Body:
```json
{
"passed" : true,
"readOnly" : false
}
```


### Endpoint: /api/v1/bookie/is_ready
1. Method: GET
* Description: Return true if the bookie is ready
Expand Down