|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.paimon.append.cluster; |
| 20 | + |
| 21 | +import org.apache.paimon.AppendOnlyFileStore; |
| 22 | +import org.apache.paimon.annotation.VisibleForTesting; |
| 23 | +import org.apache.paimon.compact.CompactFutureManager; |
| 24 | +import org.apache.paimon.compact.CompactResult; |
| 25 | +import org.apache.paimon.compact.CompactTask; |
| 26 | +import org.apache.paimon.compact.CompactUnit; |
| 27 | +import org.apache.paimon.io.DataFileMeta; |
| 28 | +import org.apache.paimon.mergetree.LevelSortedRun; |
| 29 | +import org.apache.paimon.schema.SchemaManager; |
| 30 | +import org.apache.paimon.utils.Preconditions; |
| 31 | + |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Optional; |
| 38 | +import java.util.concurrent.ExecutionException; |
| 39 | +import java.util.concurrent.ExecutorService; |
| 40 | +import java.util.stream.Collectors; |
| 41 | + |
| 42 | +/** Cluster manager for {@link AppendOnlyFileStore}. */ |
| 43 | +public class BucketedAppendClusterManager extends CompactFutureManager { |
| 44 | + |
| 45 | + private static final Logger LOG = LoggerFactory.getLogger(BucketedAppendClusterManager.class); |
| 46 | + |
| 47 | + private final ExecutorService executor; |
| 48 | + private final BucketedAppendLevels levels; |
| 49 | + private final IncrementalClusterStrategy strategy; |
| 50 | + private final CompactRewriter rewriter; |
| 51 | + |
| 52 | + public BucketedAppendClusterManager( |
| 53 | + ExecutorService executor, |
| 54 | + List<DataFileMeta> restored, |
| 55 | + SchemaManager schemaManager, |
| 56 | + List<String> clusterKeys, |
| 57 | + int maxSizeAmp, |
| 58 | + int sizeRatio, |
| 59 | + int numRunCompactionTrigger, |
| 60 | + int numLevels, |
| 61 | + CompactRewriter rewriter) { |
| 62 | + this.executor = executor; |
| 63 | + this.levels = new BucketedAppendLevels(restored, numLevels); |
| 64 | + this.strategy = |
| 65 | + new IncrementalClusterStrategy( |
| 66 | + schemaManager, clusterKeys, maxSizeAmp, sizeRatio, numRunCompactionTrigger); |
| 67 | + this.rewriter = rewriter; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public boolean shouldWaitForLatestCompaction() { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean shouldWaitForPreparingCheckpoint() { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void addNewFile(DataFileMeta file) { |
| 82 | + levels.addLevel0File(file); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public List<DataFileMeta> allFiles() { |
| 87 | + return levels.allFiles(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void triggerCompaction(boolean fullCompaction) { |
| 92 | + Optional<CompactUnit> optionalUnit; |
| 93 | + List<LevelSortedRun> runs = levels.levelSortedRuns(); |
| 94 | + if (fullCompaction) { |
| 95 | + Preconditions.checkState( |
| 96 | + taskFuture == null, |
| 97 | + "A compaction task is still running while the user " |
| 98 | + + "forces a new compaction. This is unexpected."); |
| 99 | + if (LOG.isDebugEnabled()) { |
| 100 | + LOG.debug( |
| 101 | + "Trigger forced full compaction. Picking from the following runs\n{}", |
| 102 | + runs); |
| 103 | + } |
| 104 | + optionalUnit = strategy.pick(levels.numberOfLevels(), runs, true); |
| 105 | + } else { |
| 106 | + if (taskFuture != null) { |
| 107 | + return; |
| 108 | + } |
| 109 | + if (LOG.isDebugEnabled()) { |
| 110 | + LOG.debug("Trigger normal compaction. Picking from the following runs\n{}", runs); |
| 111 | + } |
| 112 | + optionalUnit = |
| 113 | + strategy.pick(levels.numberOfLevels(), runs, false) |
| 114 | + .filter(unit -> !unit.files().isEmpty()); |
| 115 | + } |
| 116 | + |
| 117 | + optionalUnit.ifPresent( |
| 118 | + unit -> { |
| 119 | + if (LOG.isDebugEnabled()) { |
| 120 | + LOG.debug( |
| 121 | + "Submit compaction with files (name, level, size): " |
| 122 | + + levels.levelSortedRuns().stream() |
| 123 | + .flatMap(lsr -> lsr.run().files().stream()) |
| 124 | + .map( |
| 125 | + file -> |
| 126 | + String.format( |
| 127 | + "(%s, %d, %d)", |
| 128 | + file.fileName(), |
| 129 | + file.level(), |
| 130 | + file.fileSize())) |
| 131 | + .collect(Collectors.joining(", "))); |
| 132 | + } |
| 133 | + submitCompaction(unit); |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + private void submitCompaction(CompactUnit unit) { |
| 138 | + |
| 139 | + BucketedAppendClusterTask task = |
| 140 | + new BucketedAppendClusterTask(unit.files(), unit.outputLevel(), rewriter); |
| 141 | + |
| 142 | + if (LOG.isDebugEnabled()) { |
| 143 | + LOG.debug( |
| 144 | + "Pick these files (name, level, size) for {} compaction: {}", |
| 145 | + task.getClass().getSimpleName(), |
| 146 | + unit.files().stream() |
| 147 | + .map( |
| 148 | + file -> |
| 149 | + String.format( |
| 150 | + "(%s, %d, %d)", |
| 151 | + file.fileName(), file.level(), file.fileSize())) |
| 152 | + .collect(Collectors.joining(", "))); |
| 153 | + } |
| 154 | + taskFuture = executor.submit(task); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public Optional<CompactResult> getCompactionResult(boolean blocking) |
| 159 | + throws ExecutionException, InterruptedException { |
| 160 | + Optional<CompactResult> result = innerGetCompactionResult(blocking); |
| 161 | + result.ifPresent( |
| 162 | + r -> { |
| 163 | + if (LOG.isDebugEnabled()) { |
| 164 | + LOG.debug( |
| 165 | + "Update levels in compact manager with these changes:\nBefore:\n{}\nAfter:\n{}", |
| 166 | + r.before(), |
| 167 | + r.after()); |
| 168 | + } |
| 169 | + levels.update(r.before(), r.after()); |
| 170 | + if (LOG.isDebugEnabled()) { |
| 171 | + LOG.debug( |
| 172 | + "Levels in compact manager updated. Current runs are\n{}", |
| 173 | + levels.levelSortedRuns()); |
| 174 | + } |
| 175 | + }); |
| 176 | + return result; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public void close() throws IOException {} |
| 181 | + |
| 182 | + @VisibleForTesting |
| 183 | + public BucketedAppendLevels levels() { |
| 184 | + return levels; |
| 185 | + } |
| 186 | + |
| 187 | + /** A {@link CompactTask} impl for clustering of append bucketed table. */ |
| 188 | + public static class BucketedAppendClusterTask extends CompactTask { |
| 189 | + |
| 190 | + private final List<DataFileMeta> toCluster; |
| 191 | + private final int outputLevel; |
| 192 | + private final CompactRewriter rewriter; |
| 193 | + |
| 194 | + public BucketedAppendClusterTask( |
| 195 | + List<DataFileMeta> toCluster, int outputLevel, CompactRewriter rewriter) { |
| 196 | + super(null); |
| 197 | + this.toCluster = toCluster; |
| 198 | + this.outputLevel = outputLevel; |
| 199 | + this.rewriter = rewriter; |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + protected CompactResult doCompact() throws Exception { |
| 204 | + List<DataFileMeta> rewrite = rewriter.rewrite(toCluster); |
| 205 | + return new CompactResult(toCluster, upgrade(rewrite)); |
| 206 | + } |
| 207 | + |
| 208 | + protected List<DataFileMeta> upgrade(List<DataFileMeta> files) { |
| 209 | + return files.stream() |
| 210 | + .map(file -> file.upgrade(outputLevel)) |
| 211 | + .collect(Collectors.toList()); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + /** Compact rewriter for append-only table. */ |
| 216 | + public interface CompactRewriter { |
| 217 | + List<DataFileMeta> rewrite(List<DataFileMeta> compactBefore) throws Exception; |
| 218 | + } |
| 219 | +} |
0 commit comments