JobRuntimePredictionSelectors { public static JobRuntimePredictionSelector aboveThreshold(double runtimeThresholdInSeconds, double sigmaThreshold, double quantile, Map<String, String> selectionMetadata) { Map<String, String> allMetadata = appendSelectorInfoIfMissing( selectionMetadata, () -> String.format("aboveThreshold{runtimeThresholdInSeconds=%s, sigmaThreshold=%s}, createdAt: %s", runtimeThresholdInSeconds, sigmaThreshold, DateTimeExt.toLocalDateTimeString(System.currentTimeMillis()) ) ); return (jobDescriptor, predictions) -> { SortedSet<JobRuntimePrediction> predictionsSet = predictions.getPredictions(); if (CollectionsExt.isNullOrEmpty(predictionsSet) || predictionsSet.size() < 2) { return Optional.empty(); } JobRuntimePrediction first = predictionsSet.first(); if (!JobRuntimePredictionUtil.expectedLowest(first)) { return Optional.empty(); } JobRuntimePrediction requested = JobRuntimePredictionUtil.findRequested(predictionsSet, quantile).orElse(null); if (requested == null || requested.getRuntimeInSeconds() > runtimeThresholdInSeconds) { return Optional.empty(); } double sigma = (requested.getRuntimeInSeconds() - first.getRuntimeInSeconds()) / JobRuntimePredictionUtil.NORM_SIGMA; if (sigma > sigmaThreshold) { return Optional.empty(); } return Optional.of(JobRuntimePredictionSelection.newBuilder() .withPrediction(requested) .withMetadata(allMetadata) .build() ); }; } private  JobRuntimePredictionSelectors(); static JobRuntimePredictionSelector never(); static JobRuntimePredictionSelector best(); static JobRuntimePredictionSelector best(Map<String, String> selectionMetadata); static JobRuntimePredictionSelector aboveThreshold(double runtimeThresholdInSeconds,
                                                              double sigmaThreshold,
                                                              double quantile,
                                                              Map<String, String> selectionMetadata); static JobRuntimePredictionSelector aboveThreshold(Config config, Map<String, String> selectionMetadata); static Map<String, JobRuntimePredictionSelector> aboveThresholds(Config config, Map<String, String> selectionMetadata); static CloseableReference<JobRuntimePredictionSelector> reloadedOnConfigurationUpdate(Config config, Function<Config, JobRuntimePredictionSelector> factory); static JobRuntimePredictionSelector abTestRandom(Map<String, JobRuntimePredictionSelector> selectors);  }
@Test public void testAboveThreshold() { JobRuntimePredictionSelector selector = aboveThreshold(60, 10, HIGH_QUANTILE, METADATA); checkSelection(selector.apply(JOB_DESCRIPTOR, JOB_PREDICTIONS), HIGH_QUANTILE); }
@Test public void testCustomQuantile() { JobRuntimePredictionSelector selector = aboveThreshold(60, 10, 0.25, METADATA); checkSelection(selector.apply(JOB_DESCRIPTOR, JOB_PREDICTIONS), 0.25); }
@Test public void testQuantileFromConfig() { ImmutableMap<String, String> config = ImmutableMap.<String, String>builder() .put("runtimeThresholdInSeconds", "60") .put("sigmaThreshold", "10") .build(); JobRuntimePredictionSelector selector = aboveThreshold(new MapConfig(config), METADATA); checkSelection(selector.apply(JOB_DESCRIPTOR, JOB_PREDICTIONS), HIGH_QUANTILE); }
@Test public void testCustomQuantileFromConfig() { ImmutableMap<String, String> config = ImmutableMap.<String, String>builder() .put("runtimeThresholdInSeconds", "60") .put("sigmaThreshold", "10") .put("quantile", "0.25") .build(); JobRuntimePredictionSelector selector = aboveThreshold(new MapConfig(config), METADATA); checkSelection(selector.apply(JOB_DESCRIPTOR, JOB_PREDICTIONS), 0.25); }
@Test public void testBadQuantileFromConfig() { ImmutableMap<String, String> config = ImmutableMap.<String, String>builder() .put("runtimeThresholdInSeconds", "60") .put("sigmaThreshold", "10") .put("quantile", "0.123") .build(); JobRuntimePredictionSelector selector = aboveThreshold(new MapConfig(config), METADATA); assertThat(selector.apply(JOB_DESCRIPTOR, JOB_PREDICTIONS)).isEmpty(); }
@Test public void testAboveThresholdWithWrongConfidenceLevels() { JobRuntimePredictionSelector selector = aboveThreshold(60, 10, HIGH_QUANTILE, METADATA); Optional<JobRuntimePredictionSelection> selectionOpt = selector.apply(JOB_DESCRIPTOR, BAD_CONFIDENCE_LEVELS); assertThat(selectionOpt).isEmpty(); }
@Test public void testAboveThresholdWithLowRuntimeThreshold() { JobRuntimePredictionSelector selector = aboveThreshold(10, 1, HIGH_QUANTILE, METADATA); Optional<JobRuntimePredictionSelection> selectionOpt = selector.apply(JOB_DESCRIPTOR, JOB_PREDICTIONS); assertThat(selectionOpt).isEmpty(); }
@Test public void testAboveThresholdFromConfig() { ImmutableMap<String, String> config = ImmutableMap.<String, String>builder() .put("runtimeThresholdInSeconds", "60") .put("sigmaThreshold", "10") .build(); JobRuntimePredictionSelector selector = aboveThreshold(new MapConfig(config), METADATA); checkSelection(selector.apply(JOB_DESCRIPTOR, JOB_PREDICTIONS), HIGH_QUANTILE); }