diff --git a/fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HMSExternalTable.java b/fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HMSExternalTable.java index d73276cc6c43f2..e75bb3724174a8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HMSExternalTable.java +++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HMSExternalTable.java @@ -416,11 +416,8 @@ public Map getNameToPartitionItems() { if (CollectionUtils.isEmpty(this.getPartitionColumns())) { return Collections.emptyMap(); } - HiveMetaStoreCache cache = Env.getCurrentEnv().getExtMetaCacheMgr() - .getMetaStoreCache((HMSExternalCatalog) this.getCatalog()); - List partitionColumnTypes = this.getPartitionColumnTypes(MvccUtil.getSnapshotFromContext(this)); - HiveMetaStoreCache.HivePartitionValues hivePartitionValues = cache.getPartitionValues( - this, partitionColumnTypes); + HiveMetaStoreCache.HivePartitionValues hivePartitionValues = getHivePartitionValues( + MvccUtil.getSnapshotFromContext(this)); Map idToPartitionItem = hivePartitionValues.getIdToPartitionItem(); // transfer id to name BiMap idToName = hivePartitionValues.getPartitionNameToIdMap().inverse(); @@ -983,10 +980,10 @@ public MTMVSnapshotIf getTableSnapshot(Optional snapshot) throws A @Override public long getNewestUpdateVersionOrTime() { + HiveMetaStoreCache.HivePartitionValues hivePartitionValues = getHivePartitionValues( + MvccUtil.getSnapshotFromContext(this)); HiveMetaStoreCache cache = Env.getCurrentEnv().getExtMetaCacheMgr() .getMetaStoreCache((HMSExternalCatalog) getCatalog()); - HiveMetaStoreCache.HivePartitionValues hivePartitionValues = cache.getPartitionValues(this, - getPartitionColumnTypes(MvccUtil.getSnapshotFromContext(this))); List partitionList = cache.getAllPartitionsWithCache(this, Lists.newArrayList(hivePartitionValues.getPartitionValuesMap().values())); if (CollectionUtils.isEmpty(partitionList)) { @@ -1062,16 +1059,15 @@ private HiveMetaStoreCache.HivePartitionValues getAllPartitionValues() { if (isView()) { return null; } - HiveMetaStoreCache cache = Env.getCurrentEnv().getExtMetaCacheMgr() - .getMetaStoreCache((HMSExternalCatalog) catalog); - List partitionColumnTypes = getPartitionColumnTypes(MvccUtil.getSnapshotFromContext(this)); + Optional snapshot = MvccUtil.getSnapshotFromContext(this); + List partitionColumnTypes = getPartitionColumnTypes(snapshot); HiveMetaStoreCache.HivePartitionValues partitionValues = null; // Get table partitions from cache. if (!partitionColumnTypes.isEmpty()) { // It is ok to get partition values from cache, // no need to worry that this call will invalid or refresh the cache. // because it has enough space to keep partition info of all tables in cache. - partitionValues = cache.getPartitionValues(this, partitionColumnTypes); + partitionValues = getHivePartitionValues(snapshot); if (partitionValues == null || partitionValues.getPartitionNameToIdMap() == null) { LOG.warn("Partition values for hive table {} is null", name); } else { @@ -1192,4 +1188,25 @@ private Table loadHiveTable() { HMSCachedClient client = ((HMSExternalCatalog) catalog).getClient(); return client.getTable(getRemoteDbName(), remoteName); } + + public HiveMetaStoreCache.HivePartitionValues getHivePartitionValues(Optional snapshot) { + HiveMetaStoreCache cache = Env.getCurrentEnv().getExtMetaCacheMgr() + .getMetaStoreCache((HMSExternalCatalog) this.getCatalog()); + try { + List partitionColumnTypes = this.getPartitionColumnTypes(snapshot); + return cache.getPartitionValues(this, partitionColumnTypes); + } catch (Exception e) { + if (e.getMessage().contains(HiveMetaStoreCache.ERR_CACHE_INCONSISTENCY)) { + LOG.warn("Hive metastore cache inconsistency detected for table: {}.{}.{}. " + + "Clearing cache and retrying to get partition values.", + this.getCatalog().getName(), this.getDbName(), this.getName(), e); + ExternalSchemaCache schemaCache = Env.getCurrentEnv().getExtMetaCacheMgr().getSchemaCache(catalog); + schemaCache.invalidateTableCache(this); + List partitionColumnTypes = this.getPartitionColumnTypes(snapshot); + return cache.getPartitionValues(this, partitionColumnTypes); + } else { + throw e; + } + } + } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HiveDlaTable.java b/fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HiveDlaTable.java index 8c93c5095597fb..c49081de60da31 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HiveDlaTable.java +++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HiveDlaTable.java @@ -71,11 +71,10 @@ public Map getAndCopyPartitionItems(Optional snapshot) throws AnalysisException { + HiveMetaStoreCache.HivePartitionValues hivePartitionValues = hmsTable.getHivePartitionValues(snapshot); + Long partitionId = getPartitionIdByNameOrAnalysisException(partitionName, hivePartitionValues); HiveMetaStoreCache cache = Env.getCurrentEnv().getExtMetaCacheMgr() .getMetaStoreCache((HMSExternalCatalog) hmsTable.getCatalog()); - HiveMetaStoreCache.HivePartitionValues hivePartitionValues = cache.getPartitionValues( - hmsTable, hmsTable.getPartitionColumnTypes(snapshot)); - Long partitionId = getPartitionIdByNameOrAnalysisException(partitionName, hivePartitionValues); HivePartition hivePartition = getHivePartitionByIdOrAnalysisException(partitionId, hivePartitionValues, cache); return new MTMVTimestampSnapshot(hivePartition.getLastModifiedTime()); @@ -95,10 +94,9 @@ public MTMVSnapshotIf getTableSnapshot(Optional snapshot) throws A HivePartition maxPartition = null; long maxVersionTime = 0L; long visibleVersionTime; + HiveMetaStoreCache.HivePartitionValues hivePartitionValues = hmsTable.getHivePartitionValues(snapshot); HiveMetaStoreCache cache = Env.getCurrentEnv().getExtMetaCacheMgr() .getMetaStoreCache((HMSExternalCatalog) hmsTable.getCatalog()); - HiveMetaStoreCache.HivePartitionValues hivePartitionValues = cache.getPartitionValues( - hmsTable, hmsTable.getPartitionColumnTypes(snapshot)); List partitionList = cache.getAllPartitionsWithCache(hmsTable, Lists.newArrayList(hivePartitionValues.getPartitionValuesMap().values())); if (CollectionUtils.isEmpty(partitionList)) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HiveMetaStoreCache.java b/fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HiveMetaStoreCache.java index 0de4b4002e9f46..649e5004a3511d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HiveMetaStoreCache.java +++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/hive/HiveMetaStoreCache.java @@ -100,6 +100,7 @@ public class HiveMetaStoreCache { private static final Logger LOG = LogManager.getLogger(HiveMetaStoreCache.class); public static final String HIVE_DEFAULT_PARTITION = "__HIVE_DEFAULT_PARTITION__"; + public static final String ERR_CACHE_INCONSISTENCY = "ERR_CACHE_INCONSISTENCY: "; private final HMSExternalCatalog catalog; private JobConf jobConf; @@ -276,11 +277,12 @@ private HivePartitionValues loadPartitionValues(PartitionValueCacheKey key) { partitionNameToIdMap, idToUniqueIdsMap, singleUidToColumnRangeMap, partitionValuesMap); } - public ListPartitionItem toListPartitionItem(String partitionName, List types) { + private ListPartitionItem toListPartitionItem(String partitionName, List types) { // Partition name will be in format: nation=cn/city=beijing // parse it to get values "cn" and "beijing" List partitionValues = HiveUtil.toPartitionValues(partitionName); - Preconditions.checkState(partitionValues.size() == types.size(), partitionName + " vs. " + types); + Preconditions.checkState(partitionValues.size() == types.size(), + ERR_CACHE_INCONSISTENCY + partitionName + " vs. " + types); List values = Lists.newArrayListWithExpectedSize(types.size()); for (String partitionValue : partitionValues) { values.add(new PartitionValue(partitionValue, HIVE_DEFAULT_PARTITION.equals(partitionValue))); @@ -433,6 +435,7 @@ public HivePartitionValues getPartitionValues(ExternalTable dorisTable, List partitionValuesMetadataResultForHmsTable(HMSExternalTa "column " + colNames + " does not match partition columns of table " + tbl.getName()); } - HiveMetaStoreCache cache = Env.getCurrentEnv().getExtMetaCacheMgr() - .getMetaStoreCache((HMSExternalCatalog) tbl.getCatalog()); - HiveMetaStoreCache.HivePartitionValues hivePartitionValues = cache.getPartitionValues( - tbl, tbl.getPartitionColumnTypes(MvccUtil.getSnapshotFromContext(tbl))); + HiveMetaStoreCache.HivePartitionValues hivePartitionValues = tbl.getHivePartitionValues( + MvccUtil.getSnapshotFromContext(tbl)); Map> valuesMap = hivePartitionValues.getPartitionValuesMap(); List dataBatch = Lists.newArrayList(); for (Map.Entry> entry : valuesMap.entrySet()) {