Skip to content

Commit 26ea12b

Browse files
authored
HIVE-29363: Separate out thread pools from the housekeeping tasks (#6233)
1 parent ca24aeb commit 26ea12b

File tree

6 files changed

+34
-98
lines changed

6 files changed

+34
-98
lines changed

itests/hive-unit/src/test/java/org/apache/hadoop/hive/metastore/MetastoreHousekeepingLeaderTestBase.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.hadoop.hive.common.TableName;
2525
import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
2626
import org.apache.hadoop.hive.metastore.conf.MetastoreConf.ConfVars;
27+
import org.apache.hadoop.hive.metastore.leader.HouseKeepingTasks;
2728
import org.apache.hadoop.hive.metastore.leader.LeaderElection;
2829
import org.apache.hadoop.hive.metastore.leader.LeaderElectionContext;
2930
import org.apache.hadoop.hive.metastore.leader.LeaderElectionFactory;
@@ -42,6 +43,7 @@
4243
import java.util.HashMap;
4344
import java.util.List;
4445
import java.util.Map;
46+
import java.util.Optional;
4547
import java.util.Set;
4648
import java.util.concurrent.CountDownLatch;
4749
import java.util.concurrent.ScheduledExecutorService;
@@ -290,9 +292,10 @@ public void setName(String name) {
290292
protected void notifyListener() {
291293
ScheduledExecutorService service = null;
292294
if (!isLeader) {
293-
try {
294-
service = ThreadPool.getPool();
295-
} catch (Exception ignored) {
295+
Optional<LeadershipStateListener> houseKeepingTasks =
296+
listeners.stream().filter(s -> s instanceof HouseKeepingTasks).findFirst();
297+
if (houseKeepingTasks.isPresent()) {
298+
service = ((HouseKeepingTasks) houseKeepingTasks.get()).getExecutorService();
296299
}
297300
}
298301
super.notifyListener();

ql/src/test/org/apache/hadoop/hive/ql/lockmgr/TestDbTxnManager.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.apache.hadoop.fs.Path;
2121
import org.apache.hadoop.hive.conf.HiveConf;
2222
import org.apache.hadoop.hive.conf.HiveConfForTest;
23-
import org.apache.hadoop.hive.metastore.ThreadPool;
2423
import org.apache.hadoop.hive.metastore.api.FieldSchema;
2524
import org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse;
2625
import org.apache.hadoop.hive.metastore.api.ShowLocksResponse;
@@ -549,7 +548,6 @@ public void setUp() throws Exception {
549548
public void tearDown() throws Exception {
550549
if (txnMgr != null) txnMgr.closeTxnManager();
551550
TestTxnDbUtil.cleanDb(conf);
552-
ThreadPool.shutdown();
553551
}
554552

555553
private static class MockQueryPlan extends QueryPlan {

standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,6 @@ public static void main(String[] args) throws Throwable {
302302
} catch (Exception e) {
303303
LOG.error("Error removing znode for this metastore instance from ZooKeeper.", e);
304304
}
305-
ThreadPool.shutdown();
306305
}, 10);
307306

308307
//Start Metrics for Standalone (Remote) Mode

standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ThreadPool.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/leader/HouseKeepingTasks.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,22 @@
1818

1919
package org.apache.hadoop.hive.metastore.leader;
2020

21+
import com.cronutils.utils.VisibleForTesting;
22+
import com.google.common.util.concurrent.ThreadFactoryBuilder;
23+
2124
import org.apache.hadoop.conf.Configuration;
2225
import org.apache.hadoop.hive.metastore.HiveMetaStore;
2326
import org.apache.hadoop.hive.metastore.MetastoreTaskThread;
24-
import org.apache.hadoop.hive.metastore.ThreadPool;
2527
import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
2628
import org.apache.hadoop.hive.metastore.txn.service.CompactionHouseKeeperService;
2729
import org.apache.hadoop.hive.metastore.utils.JavaUtils;
2830

2931
import java.util.ArrayList;
3032
import java.util.Collection;
3133
import java.util.List;
34+
import java.util.concurrent.Executors;
35+
import java.util.concurrent.ScheduledExecutorService;
36+
import java.util.concurrent.ThreadFactory;
3237
import java.util.concurrent.TimeUnit;
3338

3439
import static java.util.Objects.requireNonNull;
@@ -38,7 +43,7 @@ public class HouseKeepingTasks implements LeaderElection.LeadershipStateListener
3843
private final Configuration configuration;
3944

4045
// shut down pool when new leader is selected
41-
private ThreadPool metastoreTaskThreadPool;
46+
private ScheduledExecutorService metastoreTaskThreadPool;
4247

4348
private boolean runOnlyRemoteTasks;
4449

@@ -94,30 +99,24 @@ public void takeLeadership(LeaderElection election) throws Exception {
9499
throw new IllegalStateException("There should be no running tasks before taking the leadership!");
95100
}
96101
runningTasks = new ArrayList<>();
97-
metastoreTaskThreadPool = ThreadPool.initialize(configuration);
102+
ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true)
103+
.setNameFormat("Metastore Scheduled Worker(" + election.getName() + ") %d").build();
104+
final List<MetastoreTaskThread> tasks;
98105
if (!runOnlyRemoteTasks) {
99-
List<MetastoreTaskThread> alwaysTasks = new ArrayList<>(getAlwaysTasks());
100-
for (MetastoreTaskThread task : alwaysTasks) {
101-
task.setConf(configuration);
102-
task.enforceMutex(election.enforceMutex());
103-
long freq = task.runFrequency(TimeUnit.MILLISECONDS);
104-
// For backwards compatibility, since some threads used to be hard coded but only run if
105-
// frequency was > 0
106-
if (freq > 0) {
107-
runningTasks.add(task);
108-
metastoreTaskThreadPool.getPool().scheduleAtFixedRate(task, freq, freq, TimeUnit.MILLISECONDS);
109-
}
110-
}
106+
tasks = new ArrayList<>(getAlwaysTasks());
111107
} else {
112-
List<MetastoreTaskThread> remoteOnlyTasks = new ArrayList<>(getRemoteOnlyTasks());
113-
for (MetastoreTaskThread task : remoteOnlyTasks) {
114-
task.setConf(configuration);
115-
task.enforceMutex(election.enforceMutex());
116-
long freq = task.runFrequency(TimeUnit.MILLISECONDS);
117-
if (freq > 0) {
118-
runningTasks.add(task);
119-
metastoreTaskThreadPool.getPool().scheduleAtFixedRate(task, freq, freq, TimeUnit.MILLISECONDS);
120-
}
108+
tasks = new ArrayList<>(getRemoteOnlyTasks());
109+
}
110+
int poolSize = Math.min(MetastoreConf.getIntVar(configuration,
111+
MetastoreConf.ConfVars.THREAD_POOL_SIZE), tasks.size());
112+
metastoreTaskThreadPool = Executors.newScheduledThreadPool(poolSize, threadFactory);
113+
for (MetastoreTaskThread task : tasks) {
114+
task.setConf(configuration);
115+
task.enforceMutex(election.enforceMutex());
116+
long freq = task.runFrequency(TimeUnit.MILLISECONDS);
117+
if (freq > 0) {
118+
runningTasks.add(task);
119+
metastoreTaskThreadPool.scheduleAtFixedRate(task, freq, freq, TimeUnit.MILLISECONDS);
121120
}
122121
}
123122

@@ -141,4 +140,8 @@ public void lossLeadership(LeaderElection election) throws Exception {
141140
}
142141
}
143142

143+
@VisibleForTesting
144+
public ScheduledExecutorService getExecutorService() {
145+
return metastoreTaskThreadPool;
146+
}
144147
}

standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/leader/LeaseLeaderElection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public class LeaseLeaderElection implements LeaderElection<TableName> {
8989
private volatile boolean stopped = false;
9090

9191
// Leadership change listeners
92-
private final List<LeadershipStateListener> listeners = new ArrayList<>();
92+
protected final List<LeadershipStateListener> listeners = new ArrayList<>();
9393

9494
protected String name;
9595
private final String userName;

0 commit comments

Comments
 (0)