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



##########
File path: 
flink-connectors/flink-connector-hbase/src/main/java/org/apache/flink/connector/hbase/source/hbaseendpoint/HBaseEndpoint.java
##########
@@ -0,0 +1,285 @@
+/*
+ * 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.source.hbaseendpoint;
+
+import 
org.apache.flink.connector.base.source.reader.synchronization.FutureCompletingBlockingQueue;
+import org.apache.flink.connector.hbase.source.reader.HBaseEvent;
+import org.apache.flink.connector.hbase.util.HBaseConfigurationUtil;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.Cell;
+import org.apache.hadoop.hbase.CellScanner;
+import org.apache.hadoop.hbase.Server;
+import org.apache.hadoop.hbase.ServerName;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.client.Admin;
+import org.apache.hadoop.hbase.client.Connection;
+import org.apache.hadoop.hbase.client.ConnectionFactory;
+import org.apache.hadoop.hbase.ipc.FifoRpcScheduler;
+import org.apache.hadoop.hbase.ipc.HBaseRpcController;
+import org.apache.hadoop.hbase.ipc.RpcServer;
+import org.apache.hadoop.hbase.ipc.RpcServerFactory;
+import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
+import org.apache.hadoop.hbase.replication.ReplicationPeerDescription;
+import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos;
+import 
org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.AdminService.BlockingInterface;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hadoop.hbase.zookeeper.RecoverableZooKeeper;
+import org.apache.hadoop.hbase.zookeeper.ZKUtil;
+import org.apache.hbase.thirdparty.com.google.protobuf.RpcController;
+import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.ZooDefs;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+/** Consumer of HBase WAL edits. */
+public class HBaseEndpoint implements ReplicationTargetInterface {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(HBaseEndpoint.class);
+    private static final int QUEUE_CAPACITY = 1000;
+    private final String clusterKey;
+    /** The id under which the replication target is made known to the source 
cluster. */
+    private final String replicationPeerId;
+
+    private final Configuration hbaseConf;
+    private final RecoverableZooKeeper zooKeeper;
+    private final RpcServer rpcServer;
+    private final FutureCompletingBlockingQueue<HBaseEvent> walEdits;
+    // TODO
+    String hostName = "localhost";
+    private boolean isRunning = false;
+
+    public HBaseEndpoint(byte[] serializedConfig)
+            throws IOException, KeeperException, InterruptedException {
+        this(HBaseConfigurationUtil.deserializeConfiguration(serializedConfig, 
null));
+    }
+
+    public HBaseEndpoint(Configuration hbaseConf)
+            throws InterruptedException, KeeperException, IOException {
+        this(UUID.randomUUID().toString().substring(0, 5), hbaseConf);
+    }
+
+    public HBaseEndpoint(String peerId, Configuration hbaseConf)
+            throws IOException, KeeperException, InterruptedException {
+        this.hbaseConf = hbaseConf;
+        this.clusterKey = peerId + "_clusterKey";
+        this.replicationPeerId = peerId;
+        this.walEdits = new FutureCompletingBlockingQueue<>(QUEUE_CAPACITY);
+
+        // Setup
+        zooKeeper = connectToZooKeeper();
+        rpcServer = createServer();
+        registerAtZooKeeper();
+    }
+
+    private RecoverableZooKeeper connectToZooKeeper() throws IOException {
+        return ZKUtil.connect(hbaseConf, "localhost:" + getZookeeperPort(), 
null);
+    }
+
+    private RpcServer createServer() throws IOException {
+        Server server = new EmptyHBaseServer(hbaseConf);
+        InetSocketAddress initialIsa = new InetSocketAddress(hostName, 0);
+        String name = "regionserver/" + initialIsa.toString();
+
+        RpcServer.BlockingServiceAndInterface bsai =
+                new RpcServer.BlockingServiceAndInterface(
+                        
AdminProtos.AdminService.newReflectiveBlockingService(this),
+                        BlockingInterface.class);
+        RpcServer rpcServer =
+                RpcServerFactory.createRpcServer(
+                        server,
+                        name,
+                        Collections.singletonList(bsai),
+                        initialIsa,
+                        hbaseConf,
+                        new FifoRpcScheduler(
+                                hbaseConf,
+                                
hbaseConf.getInt("hbase.regionserver.handler.count", 10)));
+
+        rpcServer.start();
+        return rpcServer;
+    }
+
+    private void registerAtZooKeeper() throws KeeperException, 
InterruptedException {
+        createZKPath(getBaseString() + "/" + clusterKey, null);
+        createZKPath(getBaseString() + "/" + clusterKey + "/rs", null);
+
+        UUID uuid = UUID.nameUUIDFromBytes(Bytes.toBytes(clusterKey));
+        createZKPath(
+                getBaseString() + "/" + clusterKey + "/hbaseid", 
Bytes.toBytes(uuid.toString()));
+
+        ServerName serverName =
+                ServerName.valueOf(
+                        hostName,
+                        rpcServer.getListenerAddress().getPort(),
+                        System.currentTimeMillis());
+        zooKeeper.create(
+                getBaseString() + "/" + clusterKey + "/rs/" + 
serverName.getServerName(),
+                null,
+                ZooDefs.Ids.OPEN_ACL_UNSAFE,
+                CreateMode.EPHEMERAL);
+    }
+
+    public HBaseEvent next() {
+        if (!isRunning) {
+            // Protects from infinite waiting
+            throw new RuntimeException("Consumer is not running");
+        }
+
+        try {
+            return walEdits.take();
+        } catch (InterruptedException e) {
+            throw new RuntimeException(

Review comment:
       `HBaseSourceSplitReader.fetch` does not allow to throw 
`InterruptedException`, so must be handled here




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