xx789633 commented on code in PR #1441:
URL: https://github.com/apache/fluss/pull/1441#discussion_r2286856713


##########
fluss-lake/fluss-lake-lance/src/main/java/com/alibaba/fluss/lake/lance/tiering/LanceLakeCommitter.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.fluss.lake.lance.tiering;
+
+import com.alibaba.fluss.config.Configuration;
+import com.alibaba.fluss.lake.committer.BucketOffset;
+import com.alibaba.fluss.lake.committer.CommittedLakeSnapshot;
+import com.alibaba.fluss.lake.committer.LakeCommitter;
+import com.alibaba.fluss.lake.lance.LanceConfig;
+import com.alibaba.fluss.lake.lance.utils.LanceDatasetAdapter;
+import com.alibaba.fluss.metadata.TablePath;
+import 
com.alibaba.fluss.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import 
com.alibaba.fluss.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+import com.alibaba.fluss.utils.json.BucketOffsetJsonSerde;
+
+import com.lancedb.lance.Dataset;
+import com.lancedb.lance.FragmentMetadata;
+import com.lancedb.lance.ReadOptions;
+import com.lancedb.lance.Transaction;
+import com.lancedb.lance.Version;
+import org.apache.arrow.memory.RootAllocator;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static 
com.alibaba.fluss.lake.committer.BucketOffset.FLUSS_LAKE_SNAP_BUCKET_OFFSET_PROPERTY;
+import static 
com.alibaba.fluss.lake.writer.LakeTieringFactory.FLUSS_LAKE_TIERING_COMMIT_USER;
+
+/** Implementation of {@link LakeCommitter} for Lance. */
+public class LanceLakeCommitter implements LakeCommitter<LanceWriteResult, 
LanceCommittable> {
+    private final LanceConfig config;
+    private final RootAllocator allocator = new RootAllocator();
+    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+
+    public LanceLakeCommitter(Configuration options, TablePath tablePath) {
+        this.config =
+                LanceConfig.from(
+                        options.toMap(),
+                        Collections.emptyMap(),
+                        tablePath.getDatabaseName(),
+                        tablePath.getTableName());
+    }
+
+    @Override
+    public LanceCommittable toCommittable(List<LanceWriteResult> 
lanceWriteResults)
+            throws IOException {
+        List<FragmentMetadata> fragments =
+                lanceWriteResults.stream()
+                        .map(LanceWriteResult::commitMessage)
+                        .flatMap(List::stream)
+                        .collect(Collectors.toList());
+        return new LanceCommittable(fragments);
+    }
+
+    @Override
+    public long commit(LanceCommittable committable, Map<String, String> 
snapshotProperties)
+            throws IOException {
+        Map<String, String> properties = new HashMap<>(snapshotProperties);
+        properties.put("commit-user", FLUSS_LAKE_TIERING_COMMIT_USER);
+        return LanceDatasetAdapter.commitAppend(config, 
committable.committable(), properties);
+    }
+
+    @Override
+    public void abort(LanceCommittable committable) throws IOException {}
+
+    @SuppressWarnings("checkstyle:LocalVariableName")
+    @Nullable
+    @Override
+    public CommittedLakeSnapshot getMissingLakeSnapshot(@Nullable Long 
latestLakeSnapshotIdOfFluss)
+            throws IOException {
+        Transaction latestLakeSnapshotIdOfLake =
+                
getCommittedLatestSnapshotOfLake(FLUSS_LAKE_TIERING_COMMIT_USER);
+
+        if (latestLakeSnapshotIdOfLake == null) {
+            return null;
+        }
+
+        // we get the latest snapshot committed by fluss,
+        // but the latest snapshot is not greater than 
latestLakeSnapshotIdOfFluss, no any missing
+        // snapshot, return directly
+        if (latestLakeSnapshotIdOfFluss != null
+                && latestLakeSnapshotIdOfLake.readVersion() <= 
latestLakeSnapshotIdOfFluss) {
+            return null;
+        }
+
+        CommittedLakeSnapshot committedLakeSnapshot =
+                new 
CommittedLakeSnapshot(latestLakeSnapshotIdOfLake.readVersion());
+        String flussOffsetProperties =
+                latestLakeSnapshotIdOfLake
+                        .transactionProperties()
+                        .get(FLUSS_LAKE_SNAP_BUCKET_OFFSET_PROPERTY);
+        for (JsonNode node : OBJECT_MAPPER.readTree(flussOffsetProperties)) {
+            BucketOffset bucketOffset = 
BucketOffsetJsonSerde.INSTANCE.deserialize(node);
+            if (bucketOffset.getPartitionId() != null) {
+                committedLakeSnapshot.addPartitionBucket(
+                        bucketOffset.getPartitionId(),
+                        bucketOffset.getPartitionQualifiedName(),
+                        bucketOffset.getBucket(),
+                        bucketOffset.getLogOffset());
+            } else {
+                committedLakeSnapshot.addBucket(
+                        bucketOffset.getBucket(), bucketOffset.getLogOffset());
+            }
+        }
+        return committedLakeSnapshot;
+    }
+
+    @Nullable
+    private Transaction getCommittedLatestSnapshotOfLake(String commitUser) {
+        Transaction latestFlussSnapshot = null;
+
+        ReadOptions.Builder builder = new ReadOptions.Builder();
+        builder.setStorageOptions(LanceConfig.genStorageOptions(config));
+        try (Dataset dataset = Dataset.open(allocator, config.getDatasetUri(), 
builder.build())) {
+            for (Version version : dataset.listVersions()) {
+                builder.setVersion((int) version.getId());
+                try (Dataset datasetVersion =
+                        Dataset.open(allocator, config.getDatasetUri(), 
builder.build())) {
+                    Transaction transaction = 
datasetVersion.readTransaction().orElse(null);
+                    if (transaction != null
+                            && commitUser.equals(
+                                    
transaction.transactionProperties().get("commit-user"))) {

Review Comment:
   done



##########
fluss-lake/fluss-lake-lance/src/main/java/com/alibaba/fluss/lake/lance/tiering/LanceLakeCommitter.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.fluss.lake.lance.tiering;
+
+import com.alibaba.fluss.config.Configuration;
+import com.alibaba.fluss.lake.committer.BucketOffset;
+import com.alibaba.fluss.lake.committer.CommittedLakeSnapshot;
+import com.alibaba.fluss.lake.committer.LakeCommitter;
+import com.alibaba.fluss.lake.lance.LanceConfig;
+import com.alibaba.fluss.lake.lance.utils.LanceDatasetAdapter;
+import com.alibaba.fluss.metadata.TablePath;
+import 
com.alibaba.fluss.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import 
com.alibaba.fluss.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+import com.alibaba.fluss.utils.json.BucketOffsetJsonSerde;
+
+import com.lancedb.lance.Dataset;
+import com.lancedb.lance.FragmentMetadata;
+import com.lancedb.lance.ReadOptions;
+import com.lancedb.lance.Transaction;
+import com.lancedb.lance.Version;
+import org.apache.arrow.memory.RootAllocator;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static 
com.alibaba.fluss.lake.committer.BucketOffset.FLUSS_LAKE_SNAP_BUCKET_OFFSET_PROPERTY;
+import static 
com.alibaba.fluss.lake.writer.LakeTieringFactory.FLUSS_LAKE_TIERING_COMMIT_USER;
+
+/** Implementation of {@link LakeCommitter} for Lance. */
+public class LanceLakeCommitter implements LakeCommitter<LanceWriteResult, 
LanceCommittable> {
+    private final LanceConfig config;
+    private final RootAllocator allocator = new RootAllocator();
+    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+
+    public LanceLakeCommitter(Configuration options, TablePath tablePath) {
+        this.config =
+                LanceConfig.from(
+                        options.toMap(),
+                        Collections.emptyMap(),
+                        tablePath.getDatabaseName(),
+                        tablePath.getTableName());
+    }
+
+    @Override
+    public LanceCommittable toCommittable(List<LanceWriteResult> 
lanceWriteResults)
+            throws IOException {
+        List<FragmentMetadata> fragments =
+                lanceWriteResults.stream()
+                        .map(LanceWriteResult::commitMessage)
+                        .flatMap(List::stream)
+                        .collect(Collectors.toList());
+        return new LanceCommittable(fragments);
+    }
+
+    @Override
+    public long commit(LanceCommittable committable, Map<String, String> 
snapshotProperties)
+            throws IOException {
+        Map<String, String> properties = new HashMap<>(snapshotProperties);
+        properties.put("commit-user", FLUSS_LAKE_TIERING_COMMIT_USER);
+        return LanceDatasetAdapter.commitAppend(config, 
committable.committable(), properties);
+    }
+
+    @Override
+    public void abort(LanceCommittable committable) throws IOException {}
+
+    @SuppressWarnings("checkstyle:LocalVariableName")
+    @Nullable
+    @Override
+    public CommittedLakeSnapshot getMissingLakeSnapshot(@Nullable Long 
latestLakeSnapshotIdOfFluss)
+            throws IOException {
+        Transaction latestLakeSnapshotIdOfLake =
+                
getCommittedLatestSnapshotOfLake(FLUSS_LAKE_TIERING_COMMIT_USER);
+
+        if (latestLakeSnapshotIdOfLake == null) {
+            return null;
+        }
+
+        // we get the latest snapshot committed by fluss,
+        // but the latest snapshot is not greater than 
latestLakeSnapshotIdOfFluss, no any missing
+        // snapshot, return directly
+        if (latestLakeSnapshotIdOfFluss != null
+                && latestLakeSnapshotIdOfLake.readVersion() <= 
latestLakeSnapshotIdOfFluss) {
+            return null;
+        }
+
+        CommittedLakeSnapshot committedLakeSnapshot =
+                new 
CommittedLakeSnapshot(latestLakeSnapshotIdOfLake.readVersion());
+        String flussOffsetProperties =
+                latestLakeSnapshotIdOfLake
+                        .transactionProperties()
+                        .get(FLUSS_LAKE_SNAP_BUCKET_OFFSET_PROPERTY);
+        for (JsonNode node : OBJECT_MAPPER.readTree(flussOffsetProperties)) {
+            BucketOffset bucketOffset = 
BucketOffsetJsonSerde.INSTANCE.deserialize(node);
+            if (bucketOffset.getPartitionId() != null) {
+                committedLakeSnapshot.addPartitionBucket(
+                        bucketOffset.getPartitionId(),
+                        bucketOffset.getPartitionQualifiedName(),
+                        bucketOffset.getBucket(),
+                        bucketOffset.getLogOffset());
+            } else {
+                committedLakeSnapshot.addBucket(
+                        bucketOffset.getBucket(), bucketOffset.getLogOffset());
+            }
+        }
+        return committedLakeSnapshot;
+    }
+
+    @Nullable
+    private Transaction getCommittedLatestSnapshotOfLake(String commitUser) {
+        Transaction latestFlussSnapshot = null;
+
+        ReadOptions.Builder builder = new ReadOptions.Builder();
+        builder.setStorageOptions(LanceConfig.genStorageOptions(config));
+        try (Dataset dataset = Dataset.open(allocator, config.getDatasetUri(), 
builder.build())) {
+            for (Version version : dataset.listVersions()) {
+                builder.setVersion((int) version.getId());
+                try (Dataset datasetVersion =
+                        Dataset.open(allocator, config.getDatasetUri(), 
builder.build())) {
+                    Transaction transaction = 
datasetVersion.readTransaction().orElse(null);
+                    if (transaction != null
+                            && commitUser.equals(
+                                    
transaction.transactionProperties().get("commit-user"))) {
+                        if (latestFlussSnapshot == null
+                                || transaction.readVersion() > 
latestFlussSnapshot.readVersion()) {
+                            latestFlussSnapshot = transaction;
+                        }
+                    }
+                }
+            }
+        }
+        return latestFlussSnapshot;
+    }
+
+    @Override
+    public void close() throws Exception {}

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to