private void doCheckViewWith()

in src/main/java/org/apache/sling/discovery/impl/common/heartbeat/HeartbeatHandler.java [566:670]


    private void doCheckViewWith(final ResourceResolver resourceResolver) throws PersistenceException {

        if (votingHandler==null) {
            logger.info("doCheckViewWith: votingHandler is null! slingId="+slingId);
        } else {
            votingHandler.analyzeVotings(resourceResolver);
            try{
                votingHandler.cleanupTimedoutVotings(resourceResolver);
            } catch(Exception e) {
                logger.warn("doCheckViewWith: Exception occurred while cleaning up votings: "+e, e);
            }
        }

        final VotingView winningVoting = VotingHelper.getWinningVoting(
                resourceResolver, config);
        int numOpenNonWinningVotes = VotingHelper.listOpenNonWinningVotings(
                resourceResolver, config).size();
        if (winningVoting != null || (numOpenNonWinningVotes > 0)) {
            // then there are votings pending and I shall wait for them to
            // settle

            // but first: make sure we sent the TOPOLOGY_CHANGING
            logger.info("doCheckViewWith: there are pending votings, marking topology as changing...");
            invalidateCurrentEstablishedView();
            discoveryServiceImpl.handleTopologyChanging();

        	if (logger.isDebugEnabled()) {
	            logger.debug("doCheckViewWith: "
	                    + numOpenNonWinningVotes
	                    + " ongoing votings, no one winning yet - I shall wait for them to settle.");
        	}
            return;
        }

        final Resource clusterNodesRes = ResourceHelper.getOrCreateResource(
                resourceResolver, config.getClusterInstancesPath());
        final Set<String> liveInstances = ViewHelper.determineLiveInstances(
                clusterNodesRes, config);

        final View establishedView = ViewHelper.getEstablishedView(resourceResolver, config);
        lastEstablishedViewId = establishedView == null ? null : establishedView.getResource().getName();
        boolean establishedViewMatches;
        if (lastEstablishedViewId != null && failedEstablishedViewId != null
                && lastEstablishedViewId.equals(failedEstablishedViewId)) {
            // SLING-5195 : heartbeat-self-check caused this establishedViewId
            // to be declared as failed - so we must now cause a new voting
            logger.info("doCheckView: current establishedViewId ({}) was declared as failed earlier already.", lastEstablishedViewId);
            establishedViewMatches = false;
        } else {
            if (establishedView == null) {
                establishedViewMatches = false;
            } else {
                String mismatchDetails;
                try{
                    mismatchDetails = establishedView.matches(liveInstances);
                } catch(Exception e) {
                    logger.error("doCheckViewWith: could not compare established view with live ones: "+e, e);
                    invalidateCurrentEstablishedView();
                    discoveryServiceImpl.handleTopologyChanging();
                    return;
                }
                if (mismatchDetails != null) {
                    logger.info("doCheckView: established view does not match. (details: " + mismatchDetails + ")");
                } else {
                    logger.debug("doCheckView: established view matches with expected.");
                }
                establishedViewMatches = mismatchDetails == null;
            }
        }

        if (establishedViewMatches) {
            // that's the normal case. the established view matches what we're
            // seeing.
            // all happy and fine
            logger.debug("doCheckViewWith: no pending nor winning votes. view is fine. we're all happy.");
            return;
        }

        // immediately send a TOPOLOGY_CHANGING - could already be sent, but just to be sure
        logger.info("doCheckViewWith: no matching established view, marking topology as changing");
        invalidateCurrentEstablishedView();
        discoveryServiceImpl.handleTopologyChanging();

        List<VotingView> myYesVotes = VotingHelper.getYesVotingsOf(resourceResolver, config, slingId);
        if (myYesVotes != null && myYesVotes.size() > 0) {
            logger.info("doCheckViewWith: I have voted yes (" + myYesVotes.size() + "x)- the vote was not yet promoted but expecting it to be soon. Not voting again in the meantime. My yes vote was for: "+myYesVotes);
            return;
        }

    	if (logger.isDebugEnabled()) {
	        logger.debug("doCheckViewWith: no pending nor winning votes. But: view does not match established or no established yet. Initiating a new voting");
	        Iterator<String> it = liveInstances.iterator();
	        while (it.hasNext()) {
	            logger.debug("doCheckViewWith: one of the live instances is: "
	                    + it.next());
	        }
    	}

        // we seem to be the first to realize that the currently established
        // view doesnt match
        // the currently live instances.

        // initiate a new voting
        doStartNewVoting(resourceResolver, liveInstances);
    }