Search in sources :

Example 6 with UndefinedClusterViewException

use of org.apache.sling.discovery.base.commons.UndefinedClusterViewException in project sling by apache.

the class TopologyConnectorClient method ping.

/** ping the server and pass the announcements between the two **/
void ping(final boolean force) {
    if (autoStopped) {
        // then we suppress any further pings!
        logger.debug("ping: autoStopped=true, hence suppressing any further pings.");
        return;
    }
    if (force) {
        backoffPeriodEnd = -1;
    } else if (backoffPeriodEnd > 0) {
        if (System.currentTimeMillis() < backoffPeriodEnd) {
            logger.debug("ping: not issueing a heartbeat due to backoff instruction from peer.");
            return;
        } else {
            logger.debug("ping: backoff period ended, issuing another ping now.");
        }
    }
    final String uri = connectorUrl.toString() + "." + clusterViewService.getSlingId() + ".json";
    if (logger.isDebugEnabled()) {
        logger.debug("ping: connectorUrl=" + connectorUrl + ", complete uri=" + uri);
    }
    final HttpClientContext clientContext = HttpClientContext.create();
    final CloseableHttpClient httpClient = createHttpClient();
    final HttpPut putRequest = new HttpPut(uri);
    // setting the connection timeout (idle connection, configured in seconds)
    putRequest.setConfig(RequestConfig.custom().setConnectTimeout(1000 * config.getSocketConnectTimeout()).build());
    Announcement resultingAnnouncement = null;
    try {
        String userInfo = connectorUrl.getUserInfo();
        if (userInfo != null) {
            Credentials c = new UsernamePasswordCredentials(userInfo);
            clientContext.getCredentialsProvider().setCredentials(new AuthScope(putRequest.getURI().getHost(), putRequest.getURI().getPort()), c);
        }
        Announcement topologyAnnouncement = new Announcement(clusterViewService.getSlingId());
        topologyAnnouncement.setServerInfo(serverInfo);
        final ClusterView clusterView;
        try {
            clusterView = clusterViewService.getLocalClusterView();
        } catch (UndefinedClusterViewException e) {
            // SLING-5030 : then we cannot ping
            logger.warn("ping: no clusterView available at the moment, cannot ping others now: " + e);
            return;
        }
        topologyAnnouncement.setLocalCluster(clusterView);
        if (force) {
            logger.debug("ping: sending a resetBackoff");
            topologyAnnouncement.setResetBackoff(true);
        }
        announcementRegistry.addAllExcept(topologyAnnouncement, clusterView, new AnnouncementFilter() {

            public boolean accept(final String receivingSlingId, final Announcement announcement) {
                // filter out announcements that are of old cluster instances
                // which I dont really have in my cluster view at the moment
                final Iterator<InstanceDescription> it = clusterView.getInstances().iterator();
                while (it.hasNext()) {
                    final InstanceDescription instance = it.next();
                    if (instance.getSlingId().equals(receivingSlingId)) {
                        // all fine then
                        return true;
                    }
                }
                // then I should also not propagate that announcement anywhere
                return false;
            }
        });
        final String p = requestValidator.encodeMessage(topologyAnnouncement.asJSON());
        if (logger.isDebugEnabled()) {
            logger.debug("ping: topologyAnnouncement json is: " + p);
        }
        requestValidator.trustMessage(putRequest, p);
        if (config.isGzipConnectorRequestsEnabled()) {
            // tell the server that the content is gzipped:
            putRequest.addHeader("Content-Encoding", "gzip");
            // and gzip the body:
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            final GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
            gzipOut.write(p.getBytes("UTF-8"));
            gzipOut.close();
            final byte[] gzippedEncodedJson = baos.toByteArray();
            putRequest.setEntity(new ByteArrayEntity(gzippedEncodedJson, ContentType.APPLICATION_JSON));
            lastRequestEncoding = "gzip";
        } else {
            // otherwise plaintext:
            final StringEntity plaintext = new StringEntity(p, "UTF-8");
            plaintext.setContentType(ContentType.APPLICATION_JSON.getMimeType());
            putRequest.setEntity(plaintext);
            lastRequestEncoding = "plaintext";
        }
        // independent of request-gzipping, we do accept the response to be gzipped,
        // so indicate this to the server:
        putRequest.addHeader("Accept-Encoding", "gzip");
        final CloseableHttpResponse response = httpClient.execute(putRequest, clientContext);
        if (logger.isDebugEnabled()) {
            logger.debug("ping: done. code=" + response.getStatusLine().getStatusCode() + " - " + response.getStatusLine().getReasonPhrase());
        }
        lastStatusCode = response.getStatusLine().getStatusCode();
        lastResponseEncoding = null;
        if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {
            final Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue() != null && contentEncoding.getValue().contains("gzip")) {
                lastResponseEncoding = "gzip";
            } else {
                lastResponseEncoding = "plaintext";
            }
            // limiting to 16MB, should be way enough
            final String responseBody = requestValidator.decodeMessage(putRequest.getURI().getPath(), response);
            if (logger.isDebugEnabled()) {
                logger.debug("ping: response body=" + responseBody);
            }
            if (responseBody != null && responseBody.length() > 0) {
                Announcement inheritedAnnouncement = Announcement.fromJSON(responseBody);
                final long backoffInterval = inheritedAnnouncement.getBackoffInterval();
                if (backoffInterval > 0) {
                    // then reset the backoffPeriodEnd:
                    /* minus 1 sec to avoid slipping the interval by a few millis */
                    this.backoffPeriodEnd = System.currentTimeMillis() + (1000 * backoffInterval) - 1000;
                    logger.debug("ping: servlet instructed to backoff: backoffInterval=" + backoffInterval + ", resulting in period end of " + new Date(backoffPeriodEnd));
                } else {
                    logger.debug("ping: servlet did not instruct any backoff-ing at this stage");
                    this.backoffPeriodEnd = -1;
                }
                if (inheritedAnnouncement.isLoop()) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("ping: connector response indicated a loop detected. not registering this announcement from " + inheritedAnnouncement.getOwnerId());
                    }
                    if (inheritedAnnouncement.getOwnerId().equals(clusterViewService.getSlingId())) {
                        if (config.isAutoStopLocalLoopEnabled()) {
                            // results in connected -> false and representsloop -> true
                            inheritedAnnouncement = null;
                            // results in isAutoStopped -> true
                            autoStopped = true;
                        }
                    }
                } else {
                    inheritedAnnouncement.setInherited(true);
                    if (announcementRegistry.registerAnnouncement(inheritedAnnouncement) == -1) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("ping: connector response is from an instance which I already see in my topology" + inheritedAnnouncement);
                        }
                        statusDetails = "receiving side is seeing me via another path (connector or cluster) already (loop)";
                        return;
                    }
                }
                resultingAnnouncement = inheritedAnnouncement;
                statusDetails = null;
            } else {
                statusDetails = "no response body received";
            }
        } else {
            statusDetails = "got HTTP Status-Code: " + lastStatusCode;
        }
        // SLING-2882 : reset suppressPingWarnings_ flag in success case
        suppressPingWarnings_ = false;
    } catch (IOException e) {
        // SLING-2882 : set/check the suppressPingWarnings_ flag
        if (suppressPingWarnings_) {
            if (logger.isDebugEnabled()) {
                logger.debug("ping: got IOException: " + e + ", uri=" + uri);
            }
        } else {
            suppressPingWarnings_ = true;
            logger.warn("ping: got IOException [suppressing further warns]: " + e + ", uri=" + uri);
        }
        statusDetails = e.toString();
    } catch (JsonException e) {
        logger.warn("ping: got JSONException: " + e);
        statusDetails = e.toString();
    } catch (RuntimeException re) {
        logger.warn("ping: got RuntimeException: " + re, re);
        statusDetails = re.toString();
    } finally {
        putRequest.releaseConnection();
        lastInheritedAnnouncement = resultingAnnouncement;
        lastPingedAt = System.currentTimeMillis();
        try {
            httpClient.close();
        } catch (IOException e) {
            logger.error("disconnect: could not close httpClient: " + e, e);
        }
    }
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) JsonException(javax.json.JsonException) Announcement(org.apache.sling.discovery.base.connectors.announcement.Announcement) AnnouncementFilter(org.apache.sling.discovery.base.connectors.announcement.AnnouncementFilter) HttpPut(org.apache.http.client.methods.HttpPut) StringEntity(org.apache.http.entity.StringEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) GZIPOutputStream(java.util.zip.GZIPOutputStream) Iterator(java.util.Iterator) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Date(java.util.Date) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Header(org.apache.http.Header) AuthScope(org.apache.http.auth.AuthScope) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException) InstanceDescription(org.apache.sling.discovery.InstanceDescription) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 7 with UndefinedClusterViewException

use of org.apache.sling.discovery.base.commons.UndefinedClusterViewException in project sling by apache.

the class TopologyConnectorServlet method doPut.

@Override
protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (!isWhitelisted(request)) {
        // in theory it would be 403==forbidden, but that would reveal that
        // a resource would exist there in the first place
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    final String[] pathInfo = request.getPathInfo().split("\\.");
    final String extension = pathInfo.length == 3 ? pathInfo[2] : "";
    if (!"json".equals(extension)) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    final String selector = pathInfo.length == 3 ? pathInfo[1] : "";
    String topologyAnnouncementJSON = requestValidator.decodeMessage(request);
    if (logger.isDebugEnabled()) {
        logger.debug("doPost: incoming topology announcement is: " + topologyAnnouncementJSON);
    }
    final Announcement incomingTopologyAnnouncement;
    try {
        incomingTopologyAnnouncement = Announcement.fromJSON(topologyAnnouncementJSON);
        if (!incomingTopologyAnnouncement.getOwnerId().equals(selector)) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
        String slingId = clusterViewService.getSlingId();
        if (slingId == null) {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            logger.info("doPut: no slingId available. Service not ready as expected at the moment.");
            return;
        }
        incomingTopologyAnnouncement.removeInherited(slingId);
        final Announcement replyAnnouncement = new Announcement(slingId);
        long backoffInterval = -1;
        ClusterView clusterView = clusterViewService.getLocalClusterView();
        if (!incomingTopologyAnnouncement.isCorrectVersion()) {
            logger.warn("doPost: rejecting an announcement from an incompatible connector protocol version: " + incomingTopologyAnnouncement);
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            return;
        } else if (ClusterViewHelper.contains(clusterView, incomingTopologyAnnouncement.getOwnerId())) {
            if (logger.isDebugEnabled()) {
                logger.debug("doPost: rejecting an announcement from an instance that is part of my cluster: " + incomingTopologyAnnouncement);
            }
            // marking as 'loop'
            replyAnnouncement.setLoop(true);
            backoffInterval = config.getBackoffStandbyInterval();
        } else if (ClusterViewHelper.containsAny(clusterView, incomingTopologyAnnouncement.listInstances())) {
            if (logger.isDebugEnabled()) {
                logger.debug("doPost: rejecting an announcement as it contains instance(s) that is/are part of my cluster: " + incomingTopologyAnnouncement);
            }
            // marking as 'loop'
            replyAnnouncement.setLoop(true);
            backoffInterval = config.getBackoffStandbyInterval();
        } else {
            backoffInterval = announcementRegistry.registerAnnouncement(incomingTopologyAnnouncement);
            if (logger.isDebugEnabled()) {
                logger.debug("doPost: backoffInterval after registration: " + backoffInterval);
            }
            if (backoffInterval == -1) {
                if (logger.isDebugEnabled()) {
                    logger.debug("doPost: rejecting an announcement from an instance that I already see in my topology: " + incomingTopologyAnnouncement);
                }
                // marking as 'loop'
                replyAnnouncement.setLoop(true);
                backoffInterval = config.getBackoffStandbyInterval();
            } else {
                // normal, successful case: replying with the part of the topology which this instance sees
                replyAnnouncement.setLocalCluster(clusterView);
                announcementRegistry.addAllExcept(replyAnnouncement, clusterView, new AnnouncementFilter() {

                    public boolean accept(final String receivingSlingId, Announcement announcement) {
                        if (announcement.getPrimaryKey().equals(incomingTopologyAnnouncement.getPrimaryKey())) {
                            return false;
                        }
                        return true;
                    }
                });
            }
        }
        if (backoffInterval > 0) {
            replyAnnouncement.setBackoffInterval(backoffInterval);
            if (logger.isDebugEnabled()) {
                logger.debug("doPost: backoffInterval for client set to " + replyAnnouncement.getBackoffInterval());
            }
        }
        final String p = requestValidator.encodeMessage(replyAnnouncement.asJSON());
        requestValidator.trustMessage(response, request, p);
        // gzip the response if the client accepts this
        final String acceptEncodingHeader = request.getHeader("Accept-Encoding");
        if (acceptEncodingHeader != null && acceptEncodingHeader.contains("gzip")) {
            // tell the client that the content is gzipped:
            response.setHeader("Content-Encoding", "gzip");
            // then gzip the body
            final GZIPOutputStream gzipOut = new GZIPOutputStream(response.getOutputStream());
            gzipOut.write(p.getBytes("UTF-8"));
            gzipOut.close();
        } else {
            // otherwise plaintext
            final PrintWriter pw = response.getWriter();
            pw.print(p);
            pw.flush();
        }
    } catch (JsonException e) {
        logger.error("doPost: Got a JSONException: " + e, e);
        response.sendError(500);
    } catch (UndefinedClusterViewException e) {
        logger.warn("doPost: no clusterView available at the moment - cannot handle connectors now: " + e);
        // "please retry, but atm I can't help since I'm isolated"
        response.sendError(503);
    }
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) JsonException(javax.json.JsonException) Announcement(org.apache.sling.discovery.base.connectors.announcement.Announcement) GZIPOutputStream(java.util.zip.GZIPOutputStream) AnnouncementFilter(org.apache.sling.discovery.base.connectors.announcement.AnnouncementFilter) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException) PrintWriter(java.io.PrintWriter)

Example 8 with UndefinedClusterViewException

use of org.apache.sling.discovery.base.commons.UndefinedClusterViewException in project sling by apache.

the class DiscoveryServiceImpl method checkForLocalClusterViewChange.

/**
     * only checks for local clusterView changes.
     * thus eg avoids doing synchronized with annotationregistry
     **/
public void checkForLocalClusterViewChange() {
    viewStateManagerLock.lock();
    try {
        if (!activated) {
            logger.debug("checkForLocalClusterViewChange: not yet activated, ignoring");
            return;
        }
        try {
            ClusterViewService clusterViewService = getClusterViewService();
            if (clusterViewService == null) {
                throw new UndefinedClusterViewException(Reason.REPOSITORY_EXCEPTION, "no ClusterViewService available at the moment");
            }
            LocalClusterView localClusterView = clusterViewService.getLocalClusterView();
        } catch (UndefinedClusterViewException e) {
            // SLING-5030 : when we're cut off from the local cluster we also
            // treat it as being cut off from the entire topology, ie we don't
            // update the announcements but just return
            // the previous oldView marked as !current
            logger.info("checkForLocalClusterViewChange: undefined cluster view: " + e.getReason() + "] " + e);
            getOldView().setNotCurrent();
            viewStateManager.handleChanging();
            if (e.getReason() == Reason.ISOLATED_FROM_TOPOLOGY) {
                handleIsolatedFromTopology();
            }
        }
    } finally {
        if (viewStateManagerLock != null) {
            viewStateManagerLock.unlock();
        }
    }
}
Also used : LocalClusterView(org.apache.sling.discovery.commons.providers.spi.LocalClusterView) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException) ClusterViewService(org.apache.sling.discovery.base.commons.ClusterViewService)

Example 9 with UndefinedClusterViewException

use of org.apache.sling.discovery.base.commons.UndefinedClusterViewException in project sling by apache.

the class ClusterViewServiceImpl method getLocalClusterView.

@Override
public LocalClusterView getLocalClusterView() throws UndefinedClusterViewException {
    if (resourceResolverFactory == null) {
        logger.warn("getClusterView: no resourceResolverFactory set at the moment.");
        throw new UndefinedClusterViewException(Reason.REPOSITORY_EXCEPTION, "no resourceResolverFactory set");
    }
    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = resourceResolverFactory.getServiceResourceResolver(null);
        View view = ViewHelper.getEstablishedView(resourceResolver, config);
        if (view == null) {
            logger.debug("getClusterView: no view established at the moment. isolated mode");
            throw new UndefinedClusterViewException(Reason.NO_ESTABLISHED_VIEW, "no established view at the moment");
        }
        if (failedEstablishedViewId != null && failedEstablishedViewId.equals(view.getResource().getName())) {
            // SLING-5195 : the heartbeat-handler-self-check has declared the currently
            // established view as invalid - hence we should now treat this as
            // undefined clusterview
            logger.debug("getClusterView: current establishedView is marked as invalid: " + failedEstablishedViewId);
            throw new UndefinedClusterViewException(Reason.NO_ESTABLISHED_VIEW, "current established view was marked as invalid");
        }
        EstablishedClusterView clusterViewImpl = new EstablishedClusterView(config, view, getSlingId());
        InstanceDescription local = clusterViewImpl.getLocalInstance();
        if (local != null) {
            return clusterViewImpl;
        } else {
            logger.info("getClusterView: the local instance (" + getSlingId() + ") is currently not included in the existing established view! " + "This is normal at startup. At other times is pseudo-network-partitioning is an indicator for repository/network-delays or clocks-out-of-sync (SLING-3432). " + "(increasing the heartbeatTimeout can help as a workaround too) " + "The local instance will stay in TOPOLOGY_CHANGING or pre _INIT mode until a new vote was successful.");
            throw new UndefinedClusterViewException(Reason.ISOLATED_FROM_TOPOLOGY, "established view does not include local instance - isolated");
        }
    } catch (UndefinedClusterViewException e) {
        // pass through
        throw e;
    } catch (LoginException e) {
        logger.error("handleEvent: could not log in administratively: " + e, e);
        throw new UndefinedClusterViewException(Reason.REPOSITORY_EXCEPTION, "could not log in administratively: " + e);
    } catch (Exception e) {
        logger.error("handleEvent: got an exception: " + e, e);
        throw new UndefinedClusterViewException(Reason.REPOSITORY_EXCEPTION, "could not log in administratively: " + e);
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoginException(org.apache.sling.api.resource.LoginException) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException) InstanceDescription(org.apache.sling.discovery.InstanceDescription) EstablishedClusterView(org.apache.sling.discovery.impl.common.resource.EstablishedClusterView) View(org.apache.sling.discovery.impl.common.View) LocalClusterView(org.apache.sling.discovery.commons.providers.spi.LocalClusterView) EstablishedClusterView(org.apache.sling.discovery.impl.common.resource.EstablishedClusterView) LoginException(org.apache.sling.api.resource.LoginException) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException)

Example 10 with UndefinedClusterViewException

use of org.apache.sling.discovery.base.commons.UndefinedClusterViewException in project sling by apache.

the class AbstractClusterLoadTest method testFramework.

@Test
public void testFramework() throws Exception {
    logger.info("testFramework: building 1st instance..");
    VirtualInstanceBuilder builder = newBuilder().newRepository("/var/discovery/impl/ClusterLoadTest/testFramework/", true).setDebugName("firstInstance").setConnectorPingTimeout(3).setConnectorPingInterval(20).setMinEventDelay(0);
    VirtualInstance firstInstance = builder.build();
    instances.add(firstInstance);
    Thread.sleep(2000);
    // in so called 'isolated' mode - lets test for that
    try {
        firstInstance.getClusterViewService().getLocalClusterView();
        fail("should complain");
    } catch (UndefinedClusterViewException e) {
    // SLING-5030:
    }
    firstInstance.startViewChecker(1);
    Thread.sleep(4000);
    // after a heartbeat and letting it settle, the discovery service must have
    // established a view - test for that
    firstInstance.dumpRepo();
    firstInstance.assertEstablishedView();
    VirtualInstanceBuilder builder2 = newBuilder().useRepositoryOf(builder).setDebugName("secondInstance").setConnectorPingTimeout(3).setConnectorPingInterval(20).setMinEventDelay(0);
    firstInstance.dumpRepo();
    logger.info("testFramework: building 2nd instance..");
    VirtualInstance secondInstance = builder2.build();
    instances.add(secondInstance);
    secondInstance.startViewChecker(1);
    Thread.sleep(4000);
    firstInstance.dumpRepo();
    assertEquals(firstInstance.getClusterViewService().getLocalClusterView().getInstances().size(), 2);
    assertEquals(secondInstance.getClusterViewService().getLocalClusterView().getInstances().size(), 2);
}
Also used : VirtualInstanceBuilder(org.apache.sling.discovery.base.its.setup.VirtualInstanceBuilder) UndefinedClusterViewException(org.apache.sling.discovery.base.commons.UndefinedClusterViewException) VirtualInstance(org.apache.sling.discovery.base.its.setup.VirtualInstance) Test(org.junit.Test)

Aggregations

UndefinedClusterViewException (org.apache.sling.discovery.base.commons.UndefinedClusterViewException)12 InstanceDescription (org.apache.sling.discovery.InstanceDescription)5 Test (org.junit.Test)5 ClusterView (org.apache.sling.discovery.ClusterView)4 LoginException (org.apache.sling.api.resource.LoginException)3 Announcement (org.apache.sling.discovery.base.connectors.announcement.Announcement)3 LocalClusterView (org.apache.sling.discovery.commons.providers.spi.LocalClusterView)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 JsonException (javax.json.JsonException)2 PersistenceException (org.apache.sling.api.resource.PersistenceException)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 AnnouncementFilter (org.apache.sling.discovery.base.connectors.announcement.AnnouncementFilter)2 AssertingTopologyEventListener (org.apache.sling.discovery.base.its.setup.mock.AssertingTopologyEventListener)2 Category (org.junit.experimental.categories.Category)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1