Search in sources :

Example 26 with BookieSocketAddress

use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.

the class BookieWatcher method replaceBookie.

/**
 * Choose a bookie to replace bookie <i>bookieIdx</i> in <i>existingBookies</i>.
 * @param existingBookies
 *          list of existing bookies.
 * @param bookieIdx
 *          index of the bookie in the list to be replaced.
 * @return the bookie to replace.
 * @throws BKNotEnoughBookiesException
 */
public BookieSocketAddress replaceBookie(int ensembleSize, int writeQuorumSize, int ackQuorumSize, Map<String, byte[]> customMetadata, List<BookieSocketAddress> existingBookies, int bookieIdx, Set<BookieSocketAddress> excludeBookies) throws BKNotEnoughBookiesException {
    long startTime = MathUtils.nowInNano();
    BookieSocketAddress addr = existingBookies.get(bookieIdx);
    BookieSocketAddress socketAddress;
    try {
        // we exclude the quarantined bookies also first
        Set<BookieSocketAddress> existingAndQuarantinedBookies = new HashSet<BookieSocketAddress>(existingBookies);
        existingAndQuarantinedBookies.addAll(quarantinedBookies.asMap().keySet());
        socketAddress = placementPolicy.replaceBookie(ensembleSize, writeQuorumSize, ackQuorumSize, customMetadata, existingAndQuarantinedBookies, addr, excludeBookies);
        replaceBookieTimer.registerSuccessfulEvent(MathUtils.nowInNano() - startTime, TimeUnit.NANOSECONDS);
    } catch (BKNotEnoughBookiesException e) {
        if (log.isDebugEnabled()) {
            log.debug("Not enough healthy bookies available, using quarantined bookies");
        }
        socketAddress = placementPolicy.replaceBookie(ensembleSize, writeQuorumSize, ackQuorumSize, customMetadata, new HashSet<BookieSocketAddress>(existingBookies), addr, excludeBookies);
        replaceBookieTimer.registerFailedEvent(MathUtils.nowInNano() - startTime, TimeUnit.NANOSECONDS);
    }
    return socketAddress;
}
Also used : BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) BKNotEnoughBookiesException(org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException) HashSet(java.util.HashSet)

Example 27 with BookieSocketAddress

use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.

the class Cookie method writeToRegistrationManager.

/**
 * Writes cookie details to registration manager.
 *
 * @param rm registration manager
 * @param conf configuration
 * @param version version
 * @throws BookieException when fail to write the cookie.
 */
public void writeToRegistrationManager(RegistrationManager rm, ServerConfiguration conf, Version version) throws BookieException {
    BookieSocketAddress address = null;
    try {
        address = Bookie.getBookieAddress(conf);
    } catch (UnknownHostException e) {
        throw new UnknownBookieIdException(e);
    }
    byte[] data = toString().getBytes(UTF_8);
    rm.writeCookie(address.toString(), new Versioned<>(data, version));
}
Also used : UnknownHostException(java.net.UnknownHostException) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) UnknownBookieIdException(org.apache.bookkeeper.bookie.BookieException.UnknownBookieIdException)

Example 28 with BookieSocketAddress

use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.

the class Cookie method deleteFromRegistrationManager.

/**
 * Deletes cookie from registration manager.
 *
 * @param rm registration manager
 * @param conf configuration
 * @param version cookie version
 * @throws BookieException when fail to delete cookie.
 */
public void deleteFromRegistrationManager(RegistrationManager rm, ServerConfiguration conf, Version version) throws BookieException {
    BookieSocketAddress address = null;
    try {
        address = Bookie.getBookieAddress(conf);
    } catch (UnknownHostException e) {
        throw new UnknownBookieIdException(e);
    }
    deleteFromRegistrationManager(rm, address, version);
}
Also used : UnknownHostException(java.net.UnknownHostException) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) UnknownBookieIdException(org.apache.bookkeeper.bookie.BookieException.UnknownBookieIdException)

Example 29 with BookieSocketAddress

use of org.apache.bookkeeper.net.BookieSocketAddress 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)

Example 30 with BookieSocketAddress

use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.

the class WhoIsAuditorService method handle.

/*
     * Print the node which holds the auditor lock.
     */
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
    HttpServiceResponse response = new HttpServiceResponse();
    if (HttpServer.Method.GET == request.getMethod()) {
        BookieSocketAddress bookieId = null;
        try {
            bookieId = AuditorElector.getCurrentAuditor(conf, zk);
            if (bookieId == null) {
                response.setCode(HttpServer.StatusCode.NOT_FOUND);
                response.setBody("No auditor elected");
                return response;
            }
        } catch (Exception e) {
            LOG.error("Meet Exception: ", e);
            response.setCode(HttpServer.StatusCode.NOT_FOUND);
            response.setBody("Exception when get." + e.getMessage());
            return response;
        }
        response.setCode(HttpServer.StatusCode.OK);
        response.setBody("Auditor: " + bookieId.getSocketAddress().getAddress().getCanonicalHostName() + "/" + bookieId.getSocketAddress().getAddress().getHostAddress() + ":" + bookieId.getSocketAddress().getPort());
        LOG.debug("response body:" + response.getBody());
        return response;
    } else {
        response.setCode(HttpServer.StatusCode.NOT_FOUND);
        response.setBody("Not found method. Should be GET method");
        return response;
    }
}
Also used : BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) HttpServiceResponse(org.apache.bookkeeper.http.service.HttpServiceResponse)

Aggregations

BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)254 Test (org.junit.Test)140 HashSet (java.util.HashSet)67 CountDownLatch (java.util.concurrent.CountDownLatch)42 ArrayList (java.util.ArrayList)40 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)38 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)37 BKNotEnoughBookiesException (org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException)29 HashMap (java.util.HashMap)28 Map (java.util.Map)24 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)23 IOException (java.io.IOException)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 BookieServer (org.apache.bookkeeper.proto.BookieServer)14 WriteCallback (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.WriteCallback)13 Set (java.util.Set)11 ByteBuf (io.netty.buffer.ByteBuf)10 ByteBuffer (java.nio.ByteBuffer)10 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10