From 3efded1f4085e6e1c323d47485ba4e6a1df5c537 Mon Sep 17 00:00:00 2001 From: Sankar Hariappan Date: Wed, 18 Jul 2018 19:44:55 +0530 Subject: [PATCH] HIVE-20192: HS2 with embedded metastore is leaking JDOPersistenceManager objects. --- .../hive/service/server/ThreadWithGarbageCleanup.java | 6 +++++- .../org/apache/hadoop/hive/metastore/HiveMetaStore.java | 8 +++++++- .../org/apache/hadoop/hive/metastore/ObjectStore.java | 7 ++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/service/src/java/org/apache/hive/service/server/ThreadWithGarbageCleanup.java b/service/src/java/org/apache/hive/service/server/ThreadWithGarbageCleanup.java index 1ec80978213b..a8ed93e81970 100644 --- a/service/src/java/org/apache/hive/service/server/ThreadWithGarbageCleanup.java +++ b/service/src/java/org/apache/hive/service/server/ThreadWithGarbageCleanup.java @@ -68,7 +68,11 @@ private void cleanRawStore() { public void cacheThreadLocalRawStore() { Long threadId = this.getId(); RawStore threadLocalRawStore = HiveMetaStore.HMSHandler.getRawStore(); - if (threadLocalRawStore != null && !threadRawStoreMap.containsKey(threadId)) { + if (threadLocalRawStore == null) { + LOG.debug("Thread Local RawStore is null, for the thread: " + + this.getName() + " and so removing entry from threadRawStoreMap."); + threadRawStoreMap.remove(threadId); + } else { LOG.debug("Adding RawStore: " + threadLocalRawStore + ", for the thread: " + this.getName() + " to threadRawStoreMap for future cleanup."); threadRawStoreMap.put(threadId, threadLocalRawStore); diff --git a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java index 47f819b60a6c..7fe5777fa8d8 100644 --- a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java +++ b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java @@ -698,9 +698,15 @@ public static RawStore getMSForConf(Configuration conf) throws MetaException { RawStore ms = threadLocalMS.get(); if (ms == null) { ms = newRawStoreForConf(conf); - ms.verifySchema(); + try { + ms.verifySchema(); + } catch (MetaException e) { + ms.shutdown(); + throw e; + } threadLocalMS.set(ms); ms = threadLocalMS.get(); + LOG.info("Created RawStore: " + ms + " from thread id: " + Thread.currentThread().getId()); } return ms; } diff --git a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java index bdcbf41e6a6e..6f8276f4e94b 100644 --- a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java +++ b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java @@ -425,6 +425,7 @@ private void initialize(Properties dsProps) { initializeHelper(dsProps); return; // If we reach here, we succeed. } catch (Exception e){ + shutdown(); numTries--; boolean retriable = isRetriableException(e); if ((numTries > 0) && retriable){ @@ -486,6 +487,8 @@ private void initializeHelper(Properties dsProps) { LOG.debug("ObjectStore, initialize called"); prop = dsProps; pm = getPersistenceManager(); + LOG.info("RawStore: {}, with PersistenceManager: {}" + + " created in the thread with id: {}", this, pm, Thread.currentThread().getId()); try { String productName = MetaStoreDirectSql.getProductName(pm); sqlGenerator = new SQLGenerator(DatabaseProduct.determineDatabaseProduct(productName), conf); @@ -503,8 +506,6 @@ private void initializeHelper(Properties dsProps) { directSql = new MetaStoreDirectSql(pm, conf, schema); } } - LOG.debug("RawStore: {}, with PersistenceManager: {}" + - " created in the thread with id: {}", this, pm, Thread.currentThread().getId()); } private DatabaseProduct determineDatabaseProduct() { @@ -703,7 +704,7 @@ public PersistenceManager getPersistenceManager() { @Override public void shutdown() { - LOG.debug("RawStore: {}, with PersistenceManager: {} will be shutdown", this, pm); + LOG.info("RawStore: {}, with PersistenceManager: {} will be shutdown", this, pm); if (pm != null) { pm.close(); pm = null;