|
| 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.admintool.maintenance; |
| 20 | + |
| 21 | +import static org.apache.polaris.persistence.nosql.maintenance.api.MaintenanceConfig.DEFAULT_COUNT_FROM_LAST_RUN_MULTIPLIER; |
| 22 | +import static org.apache.polaris.persistence.nosql.maintenance.api.MaintenanceConfig.DEFAULT_CREATED_AT_GRACE_TIME; |
| 23 | +import static org.apache.polaris.persistence.nosql.maintenance.api.MaintenanceConfig.DEFAULT_DELETE_BATCH_SIZE; |
| 24 | +import static org.apache.polaris.persistence.nosql.maintenance.api.MaintenanceConfig.DEFAULT_EXPECTED_OBJ_COUNT; |
| 25 | +import static org.apache.polaris.persistence.nosql.maintenance.api.MaintenanceConfig.DEFAULT_EXPECTED_REFERENCE_COUNT; |
| 26 | +import static org.apache.polaris.persistence.nosql.maintenance.api.MaintenanceConfig.DEFAULT_INITIALIZED_FPP; |
| 27 | +import static org.apache.polaris.persistence.nosql.maintenance.api.MaintenanceConfig.DEFAULT_MAX_ACCEPTABLE_FPP; |
| 28 | +import static org.apache.polaris.persistence.nosql.maintenance.api.MaintenanceConfig.DEFAULT_RETAINED_RUNS; |
| 29 | + |
| 30 | +import jakarta.inject.Inject; |
| 31 | +import java.io.PrintWriter; |
| 32 | +import java.time.Instant; |
| 33 | +import org.apache.polaris.admintool.BaseCommand; |
| 34 | +import org.apache.polaris.persistence.nosql.api.backend.Backend; |
| 35 | +import org.apache.polaris.persistence.nosql.api.obj.ObjTypes; |
| 36 | +import org.apache.polaris.persistence.nosql.maintenance.api.MaintenanceConfig; |
| 37 | +import org.apache.polaris.persistence.nosql.maintenance.api.MaintenanceRunInformation; |
| 38 | +import org.apache.polaris.persistence.nosql.maintenance.api.MaintenanceRunInformation.MaintenanceStats; |
| 39 | +import org.apache.polaris.persistence.nosql.maintenance.api.MaintenanceRunSpec; |
| 40 | +import org.apache.polaris.persistence.nosql.maintenance.api.MaintenanceService; |
| 41 | + |
| 42 | +@SuppressWarnings("CdiInjectionPointsInspection") |
| 43 | +public abstract class BaseMaintenanceCommand extends BaseCommand { |
| 44 | + @Inject protected MaintenanceService maintenanceService; |
| 45 | + @Inject protected Backend backend; |
| 46 | + @Inject protected MaintenanceConfig maintenanceConfig; |
| 47 | + |
| 48 | + protected void checkInMemory() { |
| 49 | + if ("InMemory".equals(backend.type())) { |
| 50 | + var err = spec.commandLine().getErr(); |
| 51 | + |
| 52 | + err.println(); |
| 53 | + err.println("Running persistence-maintenance against InMemory is useless..."); |
| 54 | + err.println(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + protected MaintenanceRunSpec printRealmStates() { |
| 59 | + var out = spec.commandLine().getOut(); |
| 60 | + |
| 61 | + var runSpec = maintenanceService.buildMaintenanceRunSpec(); |
| 62 | + out.println(); |
| 63 | + out.printf("Process system realm: %s%n", runSpec.includeSystemRealm()); |
| 64 | + out.println("Realms to process:"); |
| 65 | + var realms = runSpec.realmsToProcess(); |
| 66 | + if (realms.isEmpty()) { |
| 67 | + out.println("(none)"); |
| 68 | + } |
| 69 | + for (var realm : realms) { |
| 70 | + out.printf(" %s%n", realm); |
| 71 | + } |
| 72 | + |
| 73 | + out.println("Realms to purge:"); |
| 74 | + realms = runSpec.realmsToPurge(); |
| 75 | + if (realms.isEmpty()) { |
| 76 | + out.println("(none)"); |
| 77 | + } |
| 78 | + for (var realm : realms) { |
| 79 | + out.printf(" %s%n", realm); |
| 80 | + } |
| 81 | + |
| 82 | + return runSpec; |
| 83 | + } |
| 84 | + |
| 85 | + protected void printMaintenanceConfig() { |
| 86 | + var out = spec.commandLine().getOut(); |
| 87 | + |
| 88 | + out.println(); |
| 89 | + out.println("Maintenance configuration:"); |
| 90 | + out.printf( |
| 91 | + " created-at grace time: %s%n", |
| 92 | + maintenanceConfig.createdAtGraceTime().orElse(DEFAULT_CREATED_AT_GRACE_TIME)); |
| 93 | + out.printf( |
| 94 | + " delete batch size: %s%n", |
| 95 | + maintenanceConfig.deleteBatchSize().orElse(DEFAULT_DELETE_BATCH_SIZE)); |
| 96 | + out.printf( |
| 97 | + " retained runs: %s%n", |
| 98 | + maintenanceConfig.retainedRuns().orElse(DEFAULT_RETAINED_RUNS)); |
| 99 | + |
| 100 | + out.printf( |
| 101 | + " expected object count: %d%n", |
| 102 | + maintenanceConfig.expectedObjCount().orElse(DEFAULT_EXPECTED_OBJ_COUNT)); |
| 103 | + out.printf( |
| 104 | + " expected reference count: %d%n", |
| 105 | + maintenanceConfig.expectedReferenceCount().orElse(DEFAULT_EXPECTED_REFERENCE_COUNT)); |
| 106 | + out.printf( |
| 107 | + " last-run multiplier: %f%n", |
| 108 | + maintenanceConfig |
| 109 | + .countFromLastRunMultiplier() |
| 110 | + .orElse(DEFAULT_COUNT_FROM_LAST_RUN_MULTIPLIER)); |
| 111 | + out.printf( |
| 112 | + " initialized FPP: %f%n", |
| 113 | + maintenanceConfig.filterInitializedFpp().orElse(DEFAULT_INITIALIZED_FPP)); |
| 114 | + out.printf( |
| 115 | + " expected FPP: %f%n", |
| 116 | + maintenanceConfig.maxAcceptableFilterFpp().orElse(DEFAULT_MAX_ACCEPTABLE_FPP)); |
| 117 | + |
| 118 | + out.printf( |
| 119 | + " reference scan rate limit / sec: %s%n", |
| 120 | + maintenanceConfig.referenceScanRateLimitPerSecond().stream() |
| 121 | + .mapToObj(Integer::toString) |
| 122 | + .findFirst() |
| 123 | + .orElse("(unlimited)")); |
| 124 | + out.printf( |
| 125 | + " object scan rate limit / sec: %s%n", |
| 126 | + maintenanceConfig.objectScanRateLimitPerSecond().stream() |
| 127 | + .mapToObj(Integer::toString) |
| 128 | + .findFirst() |
| 129 | + .orElse("(unlimited)")); |
| 130 | + } |
| 131 | + |
| 132 | + protected void printRunInformation(MaintenanceRunInformation info, boolean expert) { |
| 133 | + var out = spec.commandLine().getOut(); |
| 134 | + out.println(); |
| 135 | + out.println( |
| 136 | + "=================================================================================="); |
| 137 | + out.println(); |
| 138 | + out.printf("Run started: %s%n", info.started()); |
| 139 | + out.printf( |
| 140 | + " status: %s%n", |
| 141 | + info.statusMessage().orElse("(no exceptional information, all good so far)")); |
| 142 | + out.printf(" finished: %s%n", info.finished().map(Instant::toString).orElse("(running)")); |
| 143 | + out.printf(" details: %s%n", info.detailedInformation().orElse("-")); |
| 144 | + |
| 145 | + out.println(); |
| 146 | + out.println("Realms:"); |
| 147 | + out.printf(" purged: %d%n", info.purgedRealms().orElse(0)); |
| 148 | + |
| 149 | + out.println(); |
| 150 | + out.println("References:"); |
| 151 | + if (expert) { |
| 152 | + // This is the number of calls to RetainedCollector.retainReference(), which is usually higher |
| 153 | + // than the actual number of distinct reference names. |
| 154 | + out.printf(" identified calls: %d%n", info.identifiedReferences().orElse(0)); |
| 155 | + } |
| 156 | + info.referenceStats().ifPresent(stats -> printStats(out, " ", stats)); |
| 157 | + info.perRealmReferenceStats() |
| 158 | + .forEach( |
| 159 | + (realm, stats) -> { |
| 160 | + out.printf(" Realm: %s%n", realm); |
| 161 | + printStats(out, " ", stats); |
| 162 | + }); |
| 163 | + |
| 164 | + out.println(); |
| 165 | + out.println("Objects:"); |
| 166 | + if (expert) { |
| 167 | + // This is the number of calls to RetainedCollector.retainObj(), which is usually much higher |
| 168 | + // than the actual number of distinct object references. |
| 169 | + out.printf(" identified calls: %d%n", info.identifiedObjs().orElse(0)); |
| 170 | + } |
| 171 | + info.objStats().ifPresent(stats -> printStats(out, " ", stats)); |
| 172 | + info.perRealmPerObjTypeStats() |
| 173 | + .forEach( |
| 174 | + (realm, perTypeStats) -> { |
| 175 | + out.printf(" Realm: %s%n", realm); |
| 176 | + perTypeStats.forEach( |
| 177 | + (type, stats) -> { |
| 178 | + out.printf(" Type: %s (%s)%n", type, ObjTypes.objTypeById(type).name()); |
| 179 | + printStats(out, " ", stats); |
| 180 | + }); |
| 181 | + }); |
| 182 | + } |
| 183 | + |
| 184 | + private void printStats(PrintWriter out, String indent, MaintenanceStats stats) { |
| 185 | + out.printf("%s scanned: %d%n", indent, stats.scanned().orElse(0L)); |
| 186 | + out.printf("%s retained: %d%n", indent, stats.retained().orElse(0L)); |
| 187 | + out.printf("%s too new: %d%n", indent, stats.newer().orElse(0L)); |
| 188 | + out.printf("%s purged: %d%n", indent, stats.purged().orElse(0L)); |
| 189 | + } |
| 190 | +} |
0 commit comments