luoyuxia commented on code in PR #1572: URL: https://github.com/apache/fluss/pull/1572#discussion_r2293068782
########## fluss-lake/fluss-lake-iceberg/src/test/java/com/alibaba/fluss/lake/iceberg/testutils/FlinkIcebergTieringTestBase.java: ########## @@ -0,0 +1,466 @@ +/* + * 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.iceberg.testutils; + +import com.alibaba.fluss.client.Connection; +import com.alibaba.fluss.client.ConnectionFactory; +import com.alibaba.fluss.client.admin.Admin; +import com.alibaba.fluss.client.table.Table; +import com.alibaba.fluss.client.table.writer.AppendWriter; +import com.alibaba.fluss.client.table.writer.TableWriter; +import com.alibaba.fluss.client.table.writer.UpsertWriter; +import com.alibaba.fluss.config.AutoPartitionTimeUnit; +import com.alibaba.fluss.config.ConfigOptions; +import com.alibaba.fluss.config.Configuration; +import com.alibaba.fluss.exception.FlussRuntimeException; +import com.alibaba.fluss.flink.tiering.LakeTieringJobBuilder; +import com.alibaba.fluss.metadata.DataLakeFormat; +import com.alibaba.fluss.metadata.Schema; +import com.alibaba.fluss.metadata.TableBucket; +import com.alibaba.fluss.metadata.TableDescriptor; +import com.alibaba.fluss.metadata.TablePath; +import com.alibaba.fluss.row.InternalRow; +import com.alibaba.fluss.server.replica.Replica; +import com.alibaba.fluss.server.testutils.FlussClusterExtension; +import com.alibaba.fluss.server.zk.ZooKeeperClient; +import com.alibaba.fluss.types.DataTypes; + +import org.apache.flink.api.common.RuntimeExecutionMode; +import org.apache.flink.core.execution.JobClient; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.Snapshot; +import org.apache.iceberg.TableScan; +import org.apache.iceberg.catalog.Catalog; +import org.apache.iceberg.data.IcebergGenerics; +import org.apache.iceberg.data.Record; +import org.apache.iceberg.data.parquet.GenericParquetReaders; +import org.apache.iceberg.hadoop.HadoopCatalog; +import org.apache.iceberg.io.CloseableIterator; +import org.apache.iceberg.parquet.Parquet; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.io.Closeable; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.file.Files; +import java.time.Duration; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.SortedSet; +import java.util.TreeSet; + +import static com.alibaba.fluss.flink.tiering.source.TieringSourceOptions.POLL_TIERING_TABLE_INTERVAL; +import static com.alibaba.fluss.lake.iceberg.utils.IcebergConversions.toIceberg; +import static com.alibaba.fluss.metadata.TableDescriptor.OFFSET_COLUMN_NAME; +import static com.alibaba.fluss.testutils.DataTestUtils.row; +import static com.alibaba.fluss.testutils.common.CommonTestUtils.retry; +import static com.alibaba.fluss.testutils.common.CommonTestUtils.waitValue; +import static org.apache.iceberg.expressions.Expressions.equal; +import static org.assertj.core.api.Assertions.assertThat; + +/** Test base for tiering to Iceberg by Flink. */ +public class FlinkIcebergTieringTestBase { + + @RegisterExtension + public static final FlussClusterExtension FLUSS_CLUSTER_EXTENSION = + FlussClusterExtension.builder() + .setClusterConf(initConfig()) + .setNumOfTabletServers(3) + .build(); + + protected StreamExecutionEnvironment execEnv; + + protected static Connection conn; + protected static Admin admin; + protected static Configuration clientConf; + protected static String warehousePath; + protected static Catalog icebergCatalog; + + private static Configuration initConfig() { + Configuration conf = new Configuration(); + conf.set(ConfigOptions.KV_SNAPSHOT_INTERVAL, Duration.ofSeconds(1)) + .set(ConfigOptions.KV_MAX_RETAINED_SNAPSHOTS, Integer.MAX_VALUE); + + // Configure the tiering sink to be Iceberg + conf.set(ConfigOptions.DATALAKE_FORMAT, DataLakeFormat.ICEBERG); + conf.setString("datalake.iceberg.type", "hadoop"); + try { + warehousePath = + Files.createTempDirectory("fluss-testing-iceberg-tiered") + .resolve("warehouse") + .toString(); + } catch (Exception e) { + throw new FlussRuntimeException("Failed to create Iceberg warehouse path", e); + } + conf.setString("datalake.iceberg.warehouse", warehousePath); + return conf; + } + + @BeforeAll + protected static void beforeAll() { + clientConf = FLUSS_CLUSTER_EXTENSION.getClientConfig(); + conn = ConnectionFactory.createConnection(clientConf); + admin = conn.getAdmin(); + icebergCatalog = getIcebergCatalog(); + } + + @AfterAll + static void afterAll() throws Exception { + if (admin != null) { + admin.close(); + admin = null; + } + if (conn != null) { + conn.close(); + conn = null; + } + if (icebergCatalog instanceof Closeable) { + ((Closeable) icebergCatalog).close(); + icebergCatalog = null; + } + } + + @BeforeEach + public void beforeEach() { + execEnv = StreamExecutionEnvironment.getExecutionEnvironment(); + execEnv.setRuntimeMode(RuntimeExecutionMode.STREAMING); + execEnv.setParallelism(2); + } + + protected JobClient buildTieringJob(StreamExecutionEnvironment execEnv) throws Exception { + Configuration flussConfig = new Configuration(clientConf); + flussConfig.set(POLL_TIERING_TABLE_INTERVAL, Duration.ofMillis(500L)); + return LakeTieringJobBuilder.newBuilder( + execEnv, + flussConfig, + Configuration.fromMap(getIcebergCatalogConf()), + DataLakeFormat.ICEBERG.toString()) + .build(); + } + + protected static Map<String, String> getIcebergCatalogConf() { + Map<String, String> icebergConf = new HashMap<>(); + icebergConf.put("type", "hadoop"); + icebergConf.put("warehouse", warehousePath); + return icebergConf; + } + + protected static Catalog getIcebergCatalog() { + HadoopCatalog catalog = new HadoopCatalog(); + catalog.setConf(new org.apache.hadoop.conf.Configuration()); + Map<String, String> properties = new HashMap<>(); + properties.put("warehouse", warehousePath); + catalog.initialize("hadoop", properties); + return catalog; + } + + protected long createPkTable(TablePath tablePath) throws Exception { + return createPkTable(tablePath, 1); + } + + protected long createLogTable(TablePath tablePath) throws Exception { + return createLogTable(tablePath, 1); + } + + protected long createLogTable(TablePath tablePath, int bucketNum) throws Exception { + return createLogTable(tablePath, bucketNum, false); + } + + protected long createLogTable(TablePath tablePath, int bucketNum, boolean isPartitioned) + throws Exception { + Schema.Builder schemaBuilder = + Schema.newBuilder().column("a", DataTypes.INT()).column("b", DataTypes.STRING()); Review Comment: Good point. I create a issue #1574 to track it. Let's first merge this pr. -- 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]
