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 
0024 import org.apache.commons.logging.Log;
0025 import org.apache.commons.logging.LogFactory;
0026 import org.apache.hadoop.hive.metastore.HiveMetaStore;
0027 import org.apache.hadoop.hive.metastore.RawStore;
0028 
0029 /**
0030  * A HiveServer2 thread used to construct new server threads.
0031  * In particular, this thread ensures an orderly cleanup,
0032  * when killed by its corresponding ExecutorService.
0033  */
0034 public class ThreadWithGarbageCleanup extends Thread {
0035   private static final Log LOG = LogFactory.getLog(ThreadWithGarbageCleanup.class);
0036 
0037   Map<Long, RawStore> threadRawStoreMap =
0038       ThreadFactoryWithGarbageCleanup.getThreadRawStoreMap();
0039 
0040   public ThreadWithGarbageCleanup(Runnable runnable) {
0041     super(runnable);
0042   }
0043 
0044   /**
0045    * Add any Thread specific garbage cleanup code here.
0046    * Currently, it shuts down the RawStore object for this thread if it is not null.
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    * Cache the ThreadLocal RawStore object. Called from the corresponding thread.
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 }