|
| 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.gravitino.maintenance.policy; |
| 20 | + |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.LinkedHashMap; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Objects; |
| 25 | +import javax.annotation.Nullable; |
| 26 | +import org.apache.gravitino.policy.PolicyContent; |
| 27 | + |
| 28 | +/** |
| 29 | + * Optional TMS fields stored in the policy content JSON document alongside task content; not |
| 30 | + * members of Iceberg*Content classes. |
| 31 | + * |
| 32 | + * <p>This type is not {@link PolicyContent}. It is a SerDe, DTO, and {@link |
| 33 | + * org.apache.gravitino.meta.PolicyEntity} carrier only. |
| 34 | + */ |
| 35 | +public final class MaintenancePeerFields { |
| 36 | + |
| 37 | + /** JSON property for {@link MaintenanceTrigger}. */ |
| 38 | + public static final String SCHEDULE_JSON_KEY = "schedule"; |
| 39 | + |
| 40 | + /** JSON property for minimum interval between finished jobs. */ |
| 41 | + public static final String MIN_INTERVAL_MS_JSON_KEY = "minIntervalMs"; |
| 42 | + |
| 43 | + /** JSON property for Spark / IRC client options. */ |
| 44 | + public static final String JOB_OPTIONS_JSON_KEY = "jobOptions"; |
| 45 | + |
| 46 | + /** JSON property for Gravitino updater options. */ |
| 47 | + public static final String UPDATE_OPTIONS_JSON_KEY = "updateOptions"; |
| 48 | + |
| 49 | + private static final MaintenancePeerFields EMPTY = |
| 50 | + new MaintenancePeerFields(null, null, Collections.emptyMap(), Collections.emptyMap()); |
| 51 | + |
| 52 | + @Nullable private final MaintenanceTrigger schedule; |
| 53 | + @Nullable private final Long minIntervalMs; |
| 54 | + private final Map<String, String> jobOptions; |
| 55 | + private final Map<String, String> updateOptions; |
| 56 | + |
| 57 | + private MaintenancePeerFields( |
| 58 | + @Nullable MaintenanceTrigger schedule, |
| 59 | + @Nullable Long minIntervalMs, |
| 60 | + Map<String, String> jobOptions, |
| 61 | + Map<String, String> updateOptions) { |
| 62 | + this.schedule = schedule; |
| 63 | + this.minIntervalMs = minIntervalMs; |
| 64 | + this.jobOptions = jobOptions; |
| 65 | + this.updateOptions = updateOptions; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Creates peer fields from optional values. |
| 70 | + * |
| 71 | + * @param schedule evaluate trigger, or null |
| 72 | + * @param minIntervalMs cooldown ms, or null |
| 73 | + * @param jobOptions Spark / IRC options, or null |
| 74 | + * @param updateOptions updater options, or null |
| 75 | + * @return peer fields instance |
| 76 | + */ |
| 77 | + public static MaintenancePeerFields of( |
| 78 | + @Nullable MaintenanceTrigger schedule, |
| 79 | + @Nullable Long minIntervalMs, |
| 80 | + @Nullable Map<String, String> jobOptions, |
| 81 | + @Nullable Map<String, String> updateOptions) { |
| 82 | + Map<String, String> normalizedJobOptions = copyOptions(jobOptions); |
| 83 | + Map<String, String> normalizedUpdateOptions = copyOptions(updateOptions); |
| 84 | + if (schedule == null |
| 85 | + && minIntervalMs == null |
| 86 | + && normalizedJobOptions.isEmpty() |
| 87 | + && normalizedUpdateOptions.isEmpty()) { |
| 88 | + return EMPTY; |
| 89 | + } |
| 90 | + return new MaintenancePeerFields( |
| 91 | + schedule, minIntervalMs, normalizedJobOptions, normalizedUpdateOptions); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @return shared empty peer fields |
| 96 | + */ |
| 97 | + public static MaintenancePeerFields empty() { |
| 98 | + return EMPTY; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @return evaluate trigger, or {@code null} when unset |
| 103 | + */ |
| 104 | + @Nullable |
| 105 | + public MaintenanceTrigger schedule() { |
| 106 | + return schedule; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @return cooldown after last finished job, or {@code null} |
| 111 | + */ |
| 112 | + @Nullable |
| 113 | + public Long minIntervalMs() { |
| 114 | + return minIntervalMs; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @return Spark / IRC client properties; never {@code null} |
| 119 | + */ |
| 120 | + public Map<String, String> jobOptions() { |
| 121 | + return jobOptions; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @return Gravitino updater options; never {@code null} |
| 126 | + */ |
| 127 | + public Map<String, String> updateOptions() { |
| 128 | + return updateOptions; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Whether any TMS peer field is set. |
| 133 | + * |
| 134 | + * @return {@code true} when schedule, interval, or either options map is present |
| 135 | + */ |
| 136 | + public boolean hasAnyField() { |
| 137 | + return schedule != null |
| 138 | + || minIntervalMs != null |
| 139 | + || !jobOptions.isEmpty() |
| 140 | + || !updateOptions.isEmpty(); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public boolean equals(Object o) { |
| 145 | + if (!(o instanceof MaintenancePeerFields)) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + MaintenancePeerFields that = (MaintenancePeerFields) o; |
| 149 | + return Objects.equals(schedule, that.schedule) |
| 150 | + && Objects.equals(minIntervalMs, that.minIntervalMs) |
| 151 | + && Objects.equals(jobOptions, that.jobOptions) |
| 152 | + && Objects.equals(updateOptions, that.updateOptions); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public int hashCode() { |
| 157 | + return Objects.hash(schedule, minIntervalMs, jobOptions, updateOptions); |
| 158 | + } |
| 159 | + |
| 160 | + private static Map<String, String> copyOptions(@Nullable Map<String, String> options) { |
| 161 | + if (options == null || options.isEmpty()) { |
| 162 | + return Collections.emptyMap(); |
| 163 | + } |
| 164 | + return Collections.unmodifiableMap(new LinkedHashMap<>(options)); |
| 165 | + } |
| 166 | +} |
0 commit comments