0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 package org.apache.hive.service.server;
0021
0022 import java.util.Map;
0023
0024 import org.apache.hadoop.hive.metastore.HiveMetaStore;
0025 import org.apache.hadoop.hive.metastore.RawStore;
0026 import org.slf4j.Logger;
0027 import org.slf4j.LoggerFactory;
0028
0029
0030
0031
0032
0033
0034 public class ThreadWithGarbageCleanup extends Thread {
0035 private static final Logger LOG = LoggerFactory.getLogger(ThreadWithGarbageCleanup.class);
0036
0037 Map<Long, RawStore> threadRawStoreMap =
0038 ThreadFactoryWithGarbageCleanup.getThreadRawStoreMap();
0039
0040 public ThreadWithGarbageCleanup(Runnable runnable) {
0041 super(runnable);
0042 }
0043
0044
0045
0046
0047
0048 @Override
0049 public void finalize() throws Throwable {
0050 cleanRawStore();
0051 super.finalize();
0052 }
0053
0054 private void cleanRawStore() {
0055 Long threadId = this.getId();
0056 RawStore threadLocalRawStore = threadRawStoreMap.get(threadId);
0057 if (threadLocalRawStore != null) {
0058 LOG.debug("RawStore: " + threadLocalRawStore + ", for the thread: " +
0059 this.getName() + " will be closed now.");
0060 threadLocalRawStore.shutdown();
0061 threadRawStoreMap.remove(threadId);
0062 }
0063 }
0064
0065
0066
0067
0068 public void cacheThreadLocalRawStore() {
0069 Long threadId = this.getId();
0070 RawStore threadLocalRawStore = HiveMetaStore.HMSHandler.getRawStore();
0071 if (threadLocalRawStore != null && !threadRawStoreMap.containsKey(threadId)) {
0072 LOG.debug("Adding RawStore: " + threadLocalRawStore + ", for the thread: " +
0073 this.getName() + " to threadRawStoreMap for future cleanup.");
0074 threadRawStoreMap.put(threadId, threadLocalRawStore);
0075 }
0076 }
0077 }