public String runActualizeBuildRefs()

in tcbot-teamcity-ignited/src/main/java/org/apache/ignite/tcignited/buildref/BuildRefSync.java [74:177]


    public String runActualizeBuildRefs(String srvId,
        SyncMode syncMode,
        @Nullable Set<Integer> mandatoryToReload,
        ITeamcityConn conn) {

        AtomicReference<String> outLinkNext = new AtomicReference<>();
        List<BuildRef> tcDataFirstPage = conn.getBuildRefsPage(null, outLinkNext);

        long start = System.currentTimeMillis();
        int srvIdMaskHigh = ITeamcityIgnited.serverIdToInt(srvId);
        Set<Long> buildsUpdated = buildRefDao.saveChunk(srvIdMaskHigh, tcDataFirstPage);
        int totalUpdated = buildsUpdated.size();
        fatBuildSync.scheduleBuildsLoad(conn, cacheKeysToBuildIds(buildsUpdated));

        int totalChecked = tcDataFirstPage.size();
        int neededToFind = 0;
        if (mandatoryToReload != null) {
            neededToFind = mandatoryToReload.size();

            tcDataFirstPage.stream().map(BuildRef::getId).forEach(mandatoryToReload::remove);
        }

        if (syncMode == SyncMode.ULTRAFAST && isEmpty(mandatoryToReload)) {
            return "Entries saved " +
                totalUpdated +
                " Builds checked " +
                totalChecked +
                " Needed to find " +
                neededToFind +
                " remained to find " +
                mandatoryToReload.size();
        }

        long lastTimeUpdateFound = System.currentTimeMillis();
        long maxMsWithoutChanges = Duration.ofHours(1).toMillis();

        //reason for end for full sync
        boolean timeoutForNewBuild = false;
        //reason for end for incremental sync: decrementing counter of builds to find without modification to stop search.
        int buildsCntrToStop = INCREMENTAL_BUILDS_WO_MODIFICATION_TO_STOP;

        while (outLinkNext.get() != null) {
            String nextPageUrl = outLinkNext.get();
            outLinkNext.set(null);
            List<BuildRef> tcDataNextPage = conn.getBuildRefsPage(nextPageUrl, outLinkNext);
            Set<Long> curChunkBuildsSaved = buildRefDao.saveChunk(srvIdMaskHigh, tcDataNextPage);
            totalUpdated += curChunkBuildsSaved.size();
            fatBuildSync.scheduleBuildsLoad(conn, cacheKeysToBuildIds(curChunkBuildsSaved));

            int savedCurChunk = curChunkBuildsSaved.size();

            totalChecked += tcDataNextPage.size();
            if (savedCurChunk != 0) {
                lastTimeUpdateFound = System.currentTimeMillis();

                buildsCntrToStop = INCREMENTAL_BUILDS_WO_MODIFICATION_TO_STOP;
            } else
                buildsCntrToStop -= tcDataNextPage.size();

            if (syncMode == SyncMode.ULTRAFAST && isEmpty(mandatoryToReload))
                break;
            else if (syncMode==SyncMode.FULL_REINDEX) {
                timeoutForNewBuild = System.currentTimeMillis() > lastTimeUpdateFound + maxMsWithoutChanges;
                if (timeoutForNewBuild
                    && totalChecked > MAX_INCREMENTAL_BUILDS_TO_CHECK)
                    break;
            }
            else {
                boolean noMandatoryBuildsLeft = isEmpty(mandatoryToReload);
                if (!noMandatoryBuildsLeft)
                    tcDataNextPage.stream().map(BuildRef::getId).forEach(mandatoryToReload::remove);

                if (buildsCntrToStop <= 0
                    && (noMandatoryBuildsLeft || totalChecked > MAX_INCREMENTAL_BUILDS_TO_CHECK)) {
                    // There are no modification at current page, hopefully no modifications at all
                    break;
                }
            }
        }

        StringBuilder sb = new StringBuilder();
        sb.append("Entries saved ");
        sb.append(totalUpdated);
        sb.append(" Builds checked ");
        sb.append(totalChecked);
        if (mandatoryToReload != null) {
            sb.append(" Needed to find ");
            sb.append(neededToFind);
            int leftToFind = mandatoryToReload.size();
            sb.append(" remained to find ");
            sb.append(leftToFind);
        }

        sb.append(" Last time update found ");
        sb.append(TimeUtil.millisToDurationPrintable(System.currentTimeMillis()- lastTimeUpdateFound));
        sb.append(" ago");

        if(timeoutForNewBuild) {
            sb.append("TIMEOUT, total time: ");
            sb.append(TimeUtil.millisToDurationPrintable(System.currentTimeMillis()- start));
        }

        return sb.toString();
    }