private boolean cleanup()

in src/main/java/org/apache/sling/discovery/oak/SlingIdCleanupTask.java [405:510]


    private boolean cleanup() {
        logger.debug("cleanup: start");
        if (!isEnabled()) {
            // bit of overkill probably, as this shouldn't happen.
            // but adds to a good night's sleep.
            logger.debug("cleanup: not enabled, stopping.");
            return false;
        }

        final ResourceResolverFactory localFactory = resourceResolverFactory;
        final Config localConfig = config;
        if (localFactory == null || localConfig == null) {
            logger.warn("cleanup: cannot cleanup due to rrf={} or c={}", localFactory,
                    localConfig);
            return true;
        }
        final TopologyView localCurrentView = currentView;
        if (localCurrentView == null || !localCurrentView.isCurrent()) {
            logger.debug("cleanup : cannot cleanup as topology recently changed : {}",
                    localCurrentView);
            return true;
        }
        final Set<String> activeSlingIds = getActiveSlingIds(localCurrentView);
        try (ResourceResolver resolver = localFactory.getServiceResourceResolver(null)) {
            final Resource clusterInstances = resolver
                    .getResource(localConfig.getClusterInstancesPath());
            final Resource idMap = resolver.getResource(localConfig.getIdMapPath());
            final Resource syncTokens = resolver
                    .getResource(localConfig.getSyncTokenPath());
            if (clusterInstances == null || idMap == null || syncTokens == null) {
                logger.warn("cleanup: no resource found at {}, {} or {}, stopping.",
                        localConfig.getClusterInstancesPath(), localConfig.getIdMapPath(),
                        localConfig.getSyncTokenPath());
                return false;
            }
            resolver.refresh();
            final ValueMap idMapMap = idMap.adaptTo(ValueMap.class);
            final ModifiableValueMap syncTokenMap = syncTokens
                    .adaptTo(ModifiableValueMap.class);
            final Calendar now = Calendar.getInstance();
            int removed = 0;
            boolean mightHaveMore = false;
            final int localBatchSize = batchSize;
            final long localMinCreationAgeMillis = minCreationAgeMillis;
            for (Resource resource : clusterInstances.getChildren()) {
                if (topologyChanged(localCurrentView)) {
                    return true;
                }
                final String slingId = resource.getName();
                if (deleteIfOldSlingId(resource, slingId, syncTokenMap, idMapMap,
                        activeSlingIds, now, localMinCreationAgeMillis)) {
                    if (++removed >= localBatchSize) {
                        // we need to stop
                        mightHaveMore = true;
                        break;
                    }
                }
            }
            // if we're not already at the batch limit, check syncTokens too
            if (!mightHaveMore) {
                for (String slingId : syncTokenMap.keySet()) {
                    try {
                        UUID.fromString(slingId);
                    } catch (Exception e) {
                        // not a uuid
                        continue;
                    }
                    if (topologyChanged(localCurrentView)) {
                        return true;
                    }
                    Resource resourceOrNull = clusterInstances.getChild(slingId);
                    if (deleteIfOldSlingId(resourceOrNull, slingId, syncTokenMap,
                            idMapMap, activeSlingIds, now, localMinCreationAgeMillis)) {
                        if (++removed >= localBatchSize) {
                            // we need to stop
                            mightHaveMore = true;
                            break;
                        }
                    }
                }
            }
            if (topologyChanged(localCurrentView)) {
                return true;
            }
            if (removed > 0) {
                // only if we removed something we commit
                resolver.commit();
                logger.info(
                        "cleanup : removed {} old slingIds (batch size : {}), potentially has more: {}",
                        removed, localBatchSize, mightHaveMore);
                deleteCount.addAndGet(removed);
            }
            firstRun = false;
            completionCount.incrementAndGet();
            return mightHaveMore;
        } catch (LoginException e) {
            logger.error("cleanup: could not log in administratively: " + e, e);
            throw new RuntimeException("Could not log in to repository (" + e + ")", e);
        } catch (PersistenceException e) {
            logger.error("cleanup: got a PersistenceException: " + e, e);
            throw new RuntimeException(
                    "Exception while talking to repository (" + e + ")", e);
        } finally {
            logger.debug("cleanup: done.");
        }
    }