wardlican commented on a change in pull request #2129:
URL: https://github.com/apache/incubator-inlong/pull/2129#discussion_r781890490



##########
File path: 
inlong-sort-standalone/sort-standalone-source/src/main/java/org/apache/inlong/sort/standalone/sink/hive/PartitionLeaderElectionRunnable.java
##########
@@ -0,0 +1,217 @@
+/**
+ * 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.inlong.sort.standalone.sink.hive;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FSDataInputStream;
+import org.apache.hadoop.fs.FSDataOutputStream;
+import org.apache.hadoop.fs.FileStatus;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.inlong.sort.standalone.config.pojo.InlongId;
+import org.apache.inlong.sort.standalone.utils.InlongLoggerFactory;
+import org.slf4j.Logger;
+
+/**
+ * 
+ * PartitionLeaderElectionRunnable
+ */
+public class PartitionLeaderElectionRunnable implements Runnable {
+
+    public static final Logger LOG = 
InlongLoggerFactory.getLogger(HiveSinkContext.class);
+    private final HiveSinkContext context;
+    private Map<String, PartitionCreateRunnable> partitionCreateMap = new 
ConcurrentHashMap<>();
+
+    /**
+     * Constructor
+     * 
+     * @param context
+     */
+    public PartitionLeaderElectionRunnable(HiveSinkContext context) {
+        this.context = context;
+    }
+
+    /**
+     * run
+     */
+    @Override
+    public void run() {
+        // clear stopped runnable
+        Set<String> uidPartitions = new HashSet<>();
+        for (Entry<String, PartitionCreateRunnable> entry : 
this.partitionCreateMap.entrySet()) {
+            if (entry.getValue().getState() != PartitionState.INIT
+                    && entry.getValue().getState() != PartitionState.CREATING) 
{
+                uidPartitions.add(entry.getKey());
+            }
+        }
+        uidPartitions.forEach(item -> this.partitionCreateMap.remove(item));
+        // add runnable
+        try (Connection conn = context.getHiveConnection()) {
+            Map<String, HdfsIdConfig> idConfigMap = context.getIdConfigMap();
+            ExecutorService partitionCreatePool = 
context.getPartitionCreatePool();
+            Statement stat = conn.createStatement();
+            for (Entry<String, HdfsIdConfig> entry : idConfigMap.entrySet()) {
+                if (hasToken(entry.getValue())) {
+                    HdfsIdConfig idConfig = entry.getValue();
+                    // get partition list of table
+                    String tableName = idConfig.getHiveTableName();
+                    ResultSet rs = stat.executeQuery("show partitions " + 
tableName);
+                    Set<String> partitionSet = new HashSet<>();
+                    while (rs.next()) {
+                        String strPartition = rs.getString(1);
+                        int index = strPartition.indexOf('=');
+                        if (index < 0) {
+                            continue;
+                        }
+                        partitionSet.add(strPartition.substring(index + 1));
+                    }
+                    rs.close();
+                    // close partition
+                    long currentTime = System.currentTimeMillis();
+                    long beginScanTime = currentTime

Review comment:
       The logical suggestion here is to extract a method




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