LeonBein commented on a change in pull request #15109:
URL: https://github.com/apache/flink/pull/15109#discussion_r595018829



##########
File path: 
flink-connectors/flink-connector-hbase/src/test/java/org/apache/flink/connector/hbase/testutil/HBaseTestCluster.java
##########
@@ -0,0 +1,328 @@
+/*
+ * 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 org.apache.flink.connector.hbase.testutil;
+
+import org.apache.flink.connector.hbase.source.HBaseSourceOptions;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HBaseTestingUtility;
+import org.apache.hadoop.hbase.MiniHBaseCluster;
+import org.apache.hadoop.hbase.StartMiniClusterOption;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.client.Admin;
+import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
+import org.apache.hadoop.hbase.client.Connection;
+import org.apache.hadoop.hbase.client.ConnectionFactory;
+import org.apache.hadoop.hbase.client.Delete;
+import org.apache.hadoop.hbase.client.HBaseAdmin;
+import org.apache.hadoop.hbase.client.Put;
+import org.apache.hadoop.hbase.client.Table;
+import org.apache.hadoop.hbase.client.TableDescriptor;
+import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
+import org.apache.hadoop.hbase.replication.ReplicationPeerDescription;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.apache.hbase.thirdparty.com.google.common.io.Closer;
+import org.junit.rules.ExternalResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.Properties;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/** Provides static access to a {@link MiniHBaseCluster} for testing. */
+public class HBaseTestCluster extends ExternalResource {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(HBaseTestCluster.class);
+
+    public static final String COLUMN_FAMILY_BASE = "info";
+    public static final String DEFAULT_COLUMN_FAMILY = COLUMN_FAMILY_BASE + 0;
+    public static final String QUALIFIER_BASE = "qualifier";
+    public static final String DEFAULT_QUALIFIER = QUALIFIER_BASE + 0;
+
+    private MiniHBaseCluster cluster;
+    private Configuration hbaseConf;
+    private String testFolder;
+
+    public HBaseTestCluster() {}
+
+    /*
+     * How to use it
+     */
+    public static void main(String[] args) throws Exception {
+        HBaseTestCluster hbaseTestCluster = new HBaseTestCluster();
+        hbaseTestCluster.startCluster();
+        hbaseTestCluster.makeTable("tableName");
+        // ...
+        hbaseTestCluster.shutdownCluster();
+    }
+
+    public void startCluster() throws IOException, InterruptedException, 
ExecutionException {
+        LOG.info("Starting HBase test cluster ...");
+        testFolder = Files.createTempDirectory(null).toString();
+
+        // Fallback for windows users with space in user name, will not work 
if path contains space.
+        if (testFolder.contains(" ")) {
+            testFolder = "/flink-hbase-test-data/";
+        }
+        
UserGroupInformation.setLoginUser(UserGroupInformation.createRemoteUser("tempusername"));
+
+        hbaseConf = HBaseConfiguration.create();
+        hbaseConf.setInt("replication.stats.thread.period.seconds", 5);
+        hbaseConf.setLong("replication.sleep.before.failover", 2000);
+        hbaseConf.setInt("replication.source.maxretriesmultiplier", 10);
+        hbaseConf.setBoolean("hbase.replication", true);
+
+        System.setProperty(HBaseTestingUtility.BASE_TEST_DIRECTORY_KEY, 
testFolder);
+
+        HBaseTestingUtility utility = new HBaseTestingUtility(hbaseConf);
+        LOG.info("Testfolder: {}", utility.getDataTestDir().toString());
+        try {
+            cluster =
+                    utility.startMiniCluster(
+                            
StartMiniClusterOption.builder().numRegionServers(3).build());
+            int numRegionServers = 
utility.getHBaseCluster().getRegionServerThreads().size();
+            LOG.info("Number of region servers: {}", numRegionServers);
+            LOG.info(
+                    "ZooKeeper client address: {}:{}",
+                    hbaseConf.get("hbase.zookeeper.quorum"),
+                    hbaseConf.get("hbase.zookeeper.property.clientPort"));
+            LOG.info(
+                    "Master port={}, web UI at port={}",
+                    hbaseConf.get("hbase.master.port"),
+                    hbaseConf.get("hbase.master.info.port"));
+
+            cluster.waitForActiveAndReadyMaster(30 * 1000);
+            HBaseAdmin.available(hbaseConf);
+            LOG.info("HBase test cluster up and running ...");
+
+        } catch (Exception e) {
+            throw new RuntimeException("Could not start HBase test mini 
cluster", e);
+        }
+
+        assert canConnectToCluster();
+    }
+
+    public void shutdownCluster() {
+        LOG.info("Shutting down HBase test cluster");
+        try {
+            try (Closer closer = Closer.create()) {
+                closer.register(this::clearTables);
+                closer.register(this::clearReplicationPeers);
+            }
+            try {
+                // Closer is not able to call this method correctly, instead 
logs process dump
+                cluster.shutdown();
+            } catch (IOException e) {
+                LOG.error("Error while shutting down HBase test cluster", e);
+            }
+            try (Closer closer = Closer.create()) {
+                closer.register(this::waitForShutDown);
+                closer.register(Paths.get(testFolder).toFile()::delete);
+            }

Review comment:
       ✅ in c4a4b2a3587860d50a563a62603c94989b17e851




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to