|
| 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.catalog; |
| 20 | + |
| 21 | +import org.apache.paimon.Snapshot; |
| 22 | +import org.apache.paimon.annotation.VisibleForTesting; |
| 23 | +import org.apache.paimon.fs.FileIO; |
| 24 | +import org.apache.paimon.fs.Path; |
| 25 | +import org.apache.paimon.operation.Lock; |
| 26 | +import org.apache.paimon.utils.SnapshotManager; |
| 27 | + |
| 28 | +import javax.annotation.Nullable; |
| 29 | + |
| 30 | +import java.util.Optional; |
| 31 | +import java.util.concurrent.Callable; |
| 32 | + |
| 33 | +/** |
| 34 | + * A {@link SnapshotCommit} using file renaming to commit. |
| 35 | + * |
| 36 | + * <p>Note that when the file system is local or HDFS, rename is atomic. But if the file system is |
| 37 | + * object storage, we need additional lock protection. |
| 38 | + */ |
| 39 | +public class RenamingSnapshotCommit implements SnapshotCommit { |
| 40 | + |
| 41 | + private final SnapshotManager snapshotManager; |
| 42 | + private final FileIO fileIO; |
| 43 | + private final Lock lock; |
| 44 | + |
| 45 | + public RenamingSnapshotCommit(SnapshotManager snapshotManager, Lock lock) { |
| 46 | + this.snapshotManager = snapshotManager; |
| 47 | + this.fileIO = snapshotManager.fileIO(); |
| 48 | + this.lock = lock; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public boolean commit(Snapshot snapshot, String branch) throws Exception { |
| 53 | + Path newSnapshotPath = |
| 54 | + snapshotManager.branch().equals(branch) |
| 55 | + ? snapshotManager.snapshotPath(snapshot.id()) |
| 56 | + : snapshotManager.copyWithBranch(branch).snapshotPath(snapshot.id()); |
| 57 | + |
| 58 | + Callable<Boolean> callable = |
| 59 | + () -> { |
| 60 | + boolean committed = fileIO.tryToWriteAtomic(newSnapshotPath, snapshot.toJson()); |
| 61 | + if (committed) { |
| 62 | + snapshotManager.commitLatestHint(snapshot.id()); |
| 63 | + } |
| 64 | + return committed; |
| 65 | + }; |
| 66 | + return lock.runWithLock( |
| 67 | + () -> |
| 68 | + // fs.rename may not returns false if target file |
| 69 | + // already exists, or even not atomic |
| 70 | + // as we're relying on external locking, we can first |
| 71 | + // check if file exist then rename to work around this |
| 72 | + // case |
| 73 | + !fileIO.exists(newSnapshotPath) && callable.call()); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void close() throws Exception { |
| 78 | + this.lock.close(); |
| 79 | + } |
| 80 | + |
| 81 | + /** Factory to create {@link RenamingSnapshotCommit}. */ |
| 82 | + public static class Factory implements SnapshotCommit.Factory { |
| 83 | + |
| 84 | + private static final long serialVersionUID = 1L; |
| 85 | + |
| 86 | + @Nullable private final CatalogLockFactory lockFactory; |
| 87 | + @Nullable private final CatalogLockContext lockContext; |
| 88 | + |
| 89 | + public Factory( |
| 90 | + @Nullable CatalogLockFactory lockFactory, |
| 91 | + @Nullable CatalogLockContext lockContext) { |
| 92 | + this.lockFactory = lockFactory; |
| 93 | + this.lockContext = lockContext; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public SnapshotCommit create(Identifier identifier, SnapshotManager snapshotManager) { |
| 98 | + Lock lock = |
| 99 | + Optional.ofNullable(lockFactory) |
| 100 | + .map(factory -> factory.createLock(lockContext)) |
| 101 | + .map(l -> Lock.fromCatalog(l, identifier)) |
| 102 | + .orElseGet(() -> Lock.emptyFactory().create()); |
| 103 | + return new RenamingSnapshotCommit(snapshotManager, lock); |
| 104 | + } |
| 105 | + |
| 106 | + @VisibleForTesting |
| 107 | + @Nullable |
| 108 | + public CatalogLockFactory lockFactory() { |
| 109 | + return lockFactory; |
| 110 | + } |
| 111 | + |
| 112 | + @VisibleForTesting |
| 113 | + @Nullable |
| 114 | + public CatalogLockContext lockContext() { |
| 115 | + return lockContext; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments