Search in sources :

Example 1 with BytesAccumulatingResponseHandler

use of org.apache.druid.server.coordinator.BytesAccumulatingResponseHandler in project druid by druid-io.

the class ChangeRequestHttpSyncer method sync.

private void sync() {
    if (!startStopLock.awaitStarted(1, TimeUnit.MILLISECONDS)) {
        log.info("Skipping sync() call for server[%s].", logIdentity);
        return;
    }
    lastSyncTime = System.currentTimeMillis();
    try {
        final String req = getRequestString();
        BytesAccumulatingResponseHandler responseHandler = new BytesAccumulatingResponseHandler();
        log.debug("Sending sync request to server[%s]", logIdentity);
        ListenableFuture<InputStream> syncRequestFuture = httpClient.go(new Request(HttpMethod.GET, new URL(baseServerURL, req)).addHeader(HttpHeaders.Names.ACCEPT, SmileMediaTypes.APPLICATION_JACKSON_SMILE).addHeader(HttpHeaders.Names.CONTENT_TYPE, SmileMediaTypes.APPLICATION_JACKSON_SMILE), responseHandler, Duration.millis(serverHttpTimeout));
        log.debug("Sent sync request to [%s]", logIdentity);
        Futures.addCallback(syncRequestFuture, new FutureCallback<InputStream>() {

            @Override
            public void onSuccess(InputStream stream) {
                synchronized (startStopLock) {
                    if (!startStopLock.awaitStarted(1, TimeUnit.MILLISECONDS)) {
                        log.info("Skipping sync() success for server[%s].", logIdentity);
                        return;
                    }
                    try {
                        if (responseHandler.getStatus() == HttpServletResponse.SC_NO_CONTENT) {
                            log.debug("Received NO CONTENT from server[%s]", logIdentity);
                            lastSuccessfulSyncTime = System.currentTimeMillis();
                            return;
                        } else if (responseHandler.getStatus() != HttpServletResponse.SC_OK) {
                            handleFailure(new RE("Bad Sync Response."));
                            return;
                        }
                        log.debug("Received sync response from [%s]", logIdentity);
                        ChangeRequestsSnapshot<T> changes = smileMapper.readValue(stream, responseTypeReferences);
                        log.debug("Finished reading sync response from [%s]", logIdentity);
                        if (changes.isResetCounter()) {
                            log.info("[%s] requested resetCounter for reason [%s].", logIdentity, changes.getResetCause());
                            counter = null;
                            return;
                        }
                        if (counter == null) {
                            listener.fullSync(changes.getRequests());
                        } else {
                            listener.deltaSync(changes.getRequests());
                        }
                        counter = changes.getCounter();
                        if (initializationLatch.getCount() > 0) {
                            initializationLatch.countDown();
                            log.info("[%s] synced successfully for the first time.", logIdentity);
                        }
                        if (consecutiveFailedAttemptCount > 0) {
                            consecutiveFailedAttemptCount = 0;
                            log.info("[%s] synced successfully.", logIdentity);
                        }
                        lastSuccessfulSyncTime = System.currentTimeMillis();
                    } catch (Exception ex) {
                        String logMsg = StringUtils.nonStrictFormat("Error processing sync response from [%s]. Reason [%s]", logIdentity, ex.getMessage());
                        if (incrementFailedAttemptAndCheckUnstabilityTimeout()) {
                            log.error(ex, logMsg);
                        } else {
                            log.info("Temporary Failure. %s", logMsg);
                            log.debug(ex, logMsg);
                        }
                    } finally {
                        addNextSyncToWorkQueue();
                    }
                }
            }

            @Override
            public void onFailure(Throwable t) {
                synchronized (startStopLock) {
                    if (!startStopLock.awaitStarted(1, TimeUnit.MILLISECONDS)) {
                        log.info("Skipping sync() failure for URL[%s].", logIdentity);
                        return;
                    }
                    try {
                        handleFailure(t);
                    } finally {
                        addNextSyncToWorkQueue();
                    }
                }
            }

            private void handleFailure(Throwable t) {
                String logMsg = StringUtils.nonStrictFormat("failed to get sync response from [%s]. Return code [%s], Reason: [%s]", logIdentity, responseHandler.getStatus(), responseHandler.getDescription());
                if (incrementFailedAttemptAndCheckUnstabilityTimeout()) {
                    log.error(t, logMsg);
                } else {
                    log.info("Temporary Failure. %s", logMsg);
                    log.debug(t, logMsg);
                }
            }
        }, executor);
    } catch (Throwable th) {
        try {
            String logMsg = StringUtils.nonStrictFormat("Fatal error while fetching segment list from [%s].", logIdentity);
            if (incrementFailedAttemptAndCheckUnstabilityTimeout()) {
                log.makeAlert(th, logMsg).emit();
            } else {
                log.info("Temporary Failure. %s", logMsg);
                log.debug(th, logMsg);
            }
        } finally {
            addNextSyncToWorkQueue();
        }
    }
}
Also used : RE(org.apache.druid.java.util.common.RE) InputStream(java.io.InputStream) Request(org.apache.druid.java.util.http.client.Request) URL(java.net.URL) BytesAccumulatingResponseHandler(org.apache.druid.server.coordinator.BytesAccumulatingResponseHandler)

Aggregations

InputStream (java.io.InputStream)1 URL (java.net.URL)1 RE (org.apache.druid.java.util.common.RE)1 Request (org.apache.druid.java.util.http.client.Request)1 BytesAccumulatingResponseHandler (org.apache.druid.server.coordinator.BytesAccumulatingResponseHandler)1