|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.polaris.persistence.nosql.maintenance.api; |
| 20 | + |
| 21 | +import static com.google.common.base.Preconditions.checkState; |
| 22 | + |
| 23 | +import com.fasterxml.jackson.annotation.JsonFormat; |
| 24 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 25 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 26 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 27 | +import io.smallrye.config.ConfigMapping; |
| 28 | +import io.smallrye.config.WithDefault; |
| 29 | +import java.time.Duration; |
| 30 | +import java.util.Optional; |
| 31 | +import java.util.OptionalDouble; |
| 32 | +import java.util.OptionalInt; |
| 33 | +import java.util.OptionalLong; |
| 34 | +import org.apache.polaris.immutables.PolarisImmutable; |
| 35 | +import org.immutables.value.Value; |
| 36 | + |
| 37 | +/** Maintenance service configuration. */ |
| 38 | +@ConfigMapping(prefix = "polaris.persistence.maintenance") |
| 39 | +@PolarisImmutable |
| 40 | +@JsonSerialize(as = ImmutableMaintenanceConfig.class) |
| 41 | +@JsonDeserialize(as = ImmutableMaintenanceConfig.class) |
| 42 | +public interface MaintenanceConfig { |
| 43 | + |
| 44 | + long DEFAULT_EXPECTED_REFERENCE_COUNT = 100; |
| 45 | + |
| 46 | + /** |
| 47 | + * Provides the expected number of references in all realms to retain, defaults to {@value |
| 48 | + * #DEFAULT_EXPECTED_REFERENCE_COUNT}, must be at least {@code 100}. This value is used as the |
| 49 | + * default if no information of a previous maintenance run is present, it is also the minimum |
| 50 | + * number of expected references. |
| 51 | + */ |
| 52 | + @WithDefault("" + DEFAULT_EXPECTED_REFERENCE_COUNT) |
| 53 | + @JsonInclude(JsonInclude.Include.NON_ABSENT) |
| 54 | + OptionalLong expectedReferenceCount(); |
| 55 | + |
| 56 | + long DEFAULT_EXPECTED_OBJ_COUNT = 100_000; |
| 57 | + |
| 58 | + /** |
| 59 | + * Provides the expected number of objects in all realms to retain, defaults to {@value |
| 60 | + * #DEFAULT_EXPECTED_OBJ_COUNT}, must be at least {@code 100000}. This value is used as the |
| 61 | + * default if no information of a previous maintenance run is present, it is also the minimum |
| 62 | + * number of expected objects. |
| 63 | + */ |
| 64 | + @WithDefault("" + DEFAULT_EXPECTED_OBJ_COUNT) |
| 65 | + @JsonInclude(JsonInclude.Include.NON_ABSENT) |
| 66 | + OptionalLong expectedObjCount(); |
| 67 | + |
| 68 | + double DEFAULT_COUNT_FROM_LAST_RUN_MULTIPLIER = 1.1; |
| 69 | + |
| 70 | + /** |
| 71 | + * Maintenance service sizes the bloom-filters used to hold the identified references and objects |
| 72 | + * according to the expression {@code lastRun.numberOfIdentified * countFromLastRunMultiplier}. |
| 73 | + * The default is to add 10% to the number of identified items. |
| 74 | + */ |
| 75 | + @WithDefault("" + DEFAULT_COUNT_FROM_LAST_RUN_MULTIPLIER) |
| 76 | + @JsonInclude(JsonInclude.Include.NON_ABSENT) |
| 77 | + OptionalDouble countFromLastRunMultiplier(); |
| 78 | + |
| 79 | + double DEFAULT_INITIALIZED_FPP = 0.00001; |
| 80 | + |
| 81 | + /** |
| 82 | + * False-positive-probability (FPP) used to initialize the bloom-filters for identified references |
| 83 | + * and objects. |
| 84 | + */ |
| 85 | + @WithDefault("" + DEFAULT_INITIALIZED_FPP) |
| 86 | + @JsonInclude(JsonInclude.Include.NON_ABSENT) |
| 87 | + OptionalDouble filterInitializedFpp(); |
| 88 | + |
| 89 | + double DEFAULT_MAX_ACCEPTABLE_FPP = 0.00005; |
| 90 | + |
| 91 | + /** |
| 92 | + * Expected maximum false-positive-probability (FPP) used to check the bloom-filters for |
| 93 | + * identified references and objects. |
| 94 | + * |
| 95 | + * <p>If the FPP of a bloom filter exceeds this value, no individual references or objects will be |
| 96 | + * purged. |
| 97 | + */ |
| 98 | + @WithDefault("" + DEFAULT_MAX_ACCEPTABLE_FPP) |
| 99 | + @JsonInclude(JsonInclude.Include.NON_ABSENT) |
| 100 | + OptionalDouble maxAcceptableFilterFpp(); |
| 101 | + |
| 102 | + int DEFAULT_RETAINED_RUNS = 50; |
| 103 | + |
| 104 | + /** |
| 105 | + * Number of retained {@linkplain MaintenanceRunInformation maintenance run objects}, must be at |
| 106 | + * least {@code 2}. |
| 107 | + */ |
| 108 | + @WithDefault("" + DEFAULT_RETAINED_RUNS) |
| 109 | + @JsonInclude(JsonInclude.Include.NON_ABSENT) |
| 110 | + OptionalInt retainedRuns(); |
| 111 | + |
| 112 | + String DEFAULT_CREATED_AT_GRACE_TIME_STRING = "PT3H"; |
| 113 | + Duration DEFAULT_CREATED_AT_GRACE_TIME = Duration.parse(DEFAULT_CREATED_AT_GRACE_TIME_STRING); |
| 114 | + |
| 115 | + /** |
| 116 | + * Objects and references that have been created <em>after</em> a maintenance run has started are |
| 117 | + * never purged. This option defines an additional grace time to when the maintenance run has |
| 118 | + * started. |
| 119 | + * |
| 120 | + * <p>This value is a safety net for two reasons: |
| 121 | + * |
| 122 | + * <ul> |
| 123 | + * <li>Respect the wall-clock drift between Polaris nodes. |
| 124 | + * <li>Respect the order of writes in Polaris persistence. Objects are written <em>before</em> |
| 125 | + * those become reachable via a commit. Commits may take a little time (milliseconds, up to |
| 126 | + * a few seconds, depending on the system load) to complete. Therefore, implementations |
| 127 | + * enforce a minimum of 5 minutes. |
| 128 | + * </ul> |
| 129 | + */ |
| 130 | + @WithDefault(DEFAULT_CREATED_AT_GRACE_TIME_STRING) |
| 131 | + @JsonInclude(JsonInclude.Include.NON_ABSENT) |
| 132 | + @JsonFormat(shape = JsonFormat.Shape.STRING) |
| 133 | + Optional<Duration> createdAtGraceTime(); |
| 134 | + |
| 135 | + /** |
| 136 | + * Optionally limit the number of objects scanned per second. Default is to not throttle object |
| 137 | + * scanning. |
| 138 | + */ |
| 139 | + @JsonInclude(JsonInclude.Include.NON_ABSENT) |
| 140 | + OptionalInt objectScanRateLimitPerSecond(); |
| 141 | + |
| 142 | + /** |
| 143 | + * Optionally limit the number of references scanned per second. |
| 144 | + * |
| 145 | + * <p>Default is to not throttle reference scanning. |
| 146 | + */ |
| 147 | + @JsonInclude(JsonInclude.Include.NON_ABSENT) |
| 148 | + OptionalInt referenceScanRateLimitPerSecond(); |
| 149 | + |
| 150 | + int DEFAULT_DELETE_BATCH_SIZE = 10; |
| 151 | + |
| 152 | + /** Size of the delete-batches when purging objects. */ |
| 153 | + @WithDefault("" + DEFAULT_DELETE_BATCH_SIZE) |
| 154 | + @JsonInclude(JsonInclude.Include.NON_ABSENT) |
| 155 | + OptionalInt deleteBatchSize(); |
| 156 | + |
| 157 | + static ImmutableMaintenanceConfig.Builder builder() { |
| 158 | + return ImmutableMaintenanceConfig.builder(); |
| 159 | + } |
| 160 | + |
| 161 | + @Value.Check |
| 162 | + default void check() { |
| 163 | + expectedReferenceCount() |
| 164 | + .ifPresent(v -> checkState(v > 0, "expectedReferenceCount must be positive")); |
| 165 | + expectedObjCount().ifPresent(v -> checkState(v > 0, "expectedObjCount must be positive")); |
| 166 | + countFromLastRunMultiplier() |
| 167 | + .ifPresent(v -> checkState(v > 1d, "countFromLastRunMultiplier must be greater than 1.0d")); |
| 168 | + filterInitializedFpp() |
| 169 | + .ifPresent( |
| 170 | + v -> checkState(v > 0d && v <= 1d, "filterInitializedFpp must be > 0.0d and <= 1.0d")); |
| 171 | + maxAcceptableFilterFpp() |
| 172 | + .ifPresent( |
| 173 | + v -> |
| 174 | + checkState(v > 0d && v <= 1d, "maxAcceptableFilterFpp must be > 0.0d and <= 1.0d")); |
| 175 | + retainedRuns().ifPresent(v -> checkState(v >= 2, "retainedRuns must 2 or greater")); |
| 176 | + createdAtGraceTime() |
| 177 | + .ifPresent(v -> checkState(!v.isNegative(), "createdAtGraceTime must not be negative")); |
| 178 | + objectScanRateLimitPerSecond() |
| 179 | + .ifPresent(v -> checkState(v >= 0, "objectScanRateLimitPerSecond must not be negative")); |
| 180 | + referenceScanRateLimitPerSecond() |
| 181 | + .ifPresent(v -> checkState(v >= 0, "referenceScanRateLimitPerSecond must not be negative")); |
| 182 | + deleteBatchSize().ifPresent(v -> checkState(v > 0, "deleteBatchSize must be positive")); |
| 183 | + } |
| 184 | +} |
0 commit comments