Back to home page

OSCL-LXR

 
 

    


0001 /**
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements.  See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership.  The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License.  You may obtain a copy of the License at
0009  *
0010  *     http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing, software
0013  * distributed under the License is distributed on an "AS IS" BASIS,
0014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015  * See the License for the specific language governing permissions and
0016  * limitations under the License.
0017  */
0018 
0019 
0020 package org.apache.hive.service.server;
0021 
0022 import java.util.Map;
0023 import java.util.concurrent.ConcurrentHashMap;
0024 import java.util.concurrent.ThreadFactory;
0025 
0026 import org.apache.hadoop.hive.metastore.RawStore;
0027 
0028 /**
0029  * A ThreadFactory for constructing new HiveServer2 threads that lets you plug
0030  * in custom cleanup code to be called before this thread is GC-ed.
0031  * Currently cleans up the following:
0032  * 1. ThreadLocal RawStore object:
0033  * In case of an embedded metastore, HiveServer2 threads (foreground and background)
0034  * end up caching a ThreadLocal RawStore object. The ThreadLocal RawStore object has
0035  * an instance of PersistenceManagerFactory and PersistenceManager.
0036  * The PersistenceManagerFactory keeps a cache of PersistenceManager objects,
0037  * which are only removed when PersistenceManager#close method is called.
0038  * HiveServer2 uses ExecutorService for managing thread pools for foreground and background threads.
0039  * ExecutorService unfortunately does not provide any hooks to be called,
0040  * when a thread from the pool is terminated.
0041  * As a solution, we're using this ThreadFactory to keep a cache of RawStore objects per thread.
0042  * And we are doing clean shutdown in the finalizer for each thread.
0043  */
0044 public class ThreadFactoryWithGarbageCleanup implements ThreadFactory {
0045 
0046   private static Map<Long, RawStore> threadRawStoreMap = new ConcurrentHashMap<Long, RawStore>();
0047 
0048   private final String namePrefix;
0049 
0050   public ThreadFactoryWithGarbageCleanup(String threadPoolName) {
0051     namePrefix = threadPoolName;
0052   }
0053 
0054   @Override
0055   public Thread newThread(Runnable runnable) {
0056     Thread newThread = new ThreadWithGarbageCleanup(runnable);
0057     newThread.setName(namePrefix + ": Thread-" + newThread.getId());
0058     return newThread;
0059   }
0060 
0061   public static Map<Long, RawStore> getThreadRawStoreMap() {
0062     return threadRawStoreMap;
0063   }
0064 }