private long includeNearestSnapshot()

in hollow/src/main/java/com/netflix/hollow/api/client/HollowUpdatePlanner.java [212:270]


    private long includeNearestSnapshot(HollowUpdatePlan plan, HollowConsumer.VersionInfo desiredVersionInfo) {
        long desiredVersion = desiredVersionInfo.getVersion();
        HollowConsumer.Blob transition = transitionCreator.retrieveSnapshotBlob(desiredVersion);
        if (transition != null) {
            // exact match with desired version
            if (transition.getToVersion() == desiredVersion) {
                plan.add(transition);
                return transition.getToVersion();
            }

            // else if update is to an announced version then add only announced versions to plan
            if (updatePlanBlobVerifier != null
                && updatePlanBlobVerifier.announcementVerificationEnabled()
                && desiredVersionInfo.wasAnnounced() != null
                && desiredVersionInfo.wasAnnounced().isPresent()
                && desiredVersionInfo.wasAnnounced().get()) {

                int lookback = 1;
                int maxLookback = updatePlanBlobVerifier.announcementVerificationMaxLookback();
                HollowConsumer.AnnouncementWatcher announcementWatcher = updatePlanBlobVerifier.announcementWatcher();
                while (lookback <= maxLookback) {
                    HollowConsumer.AnnouncementStatus announcementStatus = announcementWatcher == null
                            ? HollowConsumer.AnnouncementStatus.UNKNOWN : announcementWatcher.getVersionAnnouncementStatus(transition.getToVersion());

                    if (announcementStatus == null
                     || announcementStatus.equals(HollowConsumer.AnnouncementStatus.UNKNOWN)
                     || announcementStatus.equals(HollowConsumer.AnnouncementStatus.ANNOUNCED)) {
                        // backwards compatibility
                        if (announcementStatus == HollowConsumer.AnnouncementStatus.UNKNOWN) {
                            if (announcementWatcher == null) {
                                LOG.warning("HollowUpdatePlanner was not initialized with an announcement watcher so it does not support getVersionAnnouncementStatus. " +
                                        "Consumer will continue with the update but runs the risk of consuming a snapshot version that was not announced");
                            } else {
                                LOG.warning(String.format("Announcement watcher impl bound (%s) to HollowUpdatePlanner does not support getVersionAnnouncementStatus. " +
                                        "Consumer will continue with the update but runs the risk of consuming a snapshot version that was not announced", announcementWatcher.getClass().getName()));
                            }
                        } else if (announcementStatus == null) {
                            LOG.warning(String.format("Expecting a valid announcement stats for version(%s), but Announcement watcher impl (%s) " +
                                    "returned null", transition.getToVersion(), announcementWatcher.getClass().getName()));
                        }
                        plan.add(transition);
                        return transition.getToVersion();
                    }
                    lookback ++;
                    desiredVersion = transition.getToVersion() - 1; // try the next highest snapshot version less than the previous one
                    transition = transitionCreator.retrieveSnapshotBlob(desiredVersion);
                    if (transition == null) {
                        break;
                    }
                }
                LOG.warning("No past snapshot found within lookback period that corresponded to an announced version, maxLookback configured to " + maxLookback);
            } else {
                // if desired version is either not an announced version, or unknown whether it is an announced version (e.g. backwards compatibility)
                plan.add(transition);
                return transition.getToVersion();
            }
        }
        return HollowConstants.VERSION_LATEST;
    }