Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](HMSExternalTable) refactor code #46466

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.datasource.hive;

import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.MTMV;
import org.apache.doris.catalog.PartitionItem;
import org.apache.doris.catalog.PartitionType;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.DdlException;
import org.apache.doris.datasource.mvcc.MvccSnapshot;
import org.apache.doris.mtmv.MTMVBaseTableIf;
import org.apache.doris.mtmv.MTMVRefreshContext;
import org.apache.doris.mtmv.MTMVSnapshotIf;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
* This abstract class represents a Hive Metastore (HMS) Dla Table and provides a blueprint for
* various operations related to metastore tables in Doris.
*
* Purpose:
* - To encapsulate common functionalities that HMS Dla tables should have for implementing other interfaces
*
* Why needed:
* - To provide a unified way to manage and interact with different kinds of Dla Table
* - To facilitate the implementation of multi-table materialized views (MTMV) by providing necessary
* methods for snapshot and partition management.
* - To abstract out the specific details of HMS table operations, making the code more modular and maintainable.
*/
public abstract class HMSDlaTable implements MTMVBaseTableIf {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some comment for this class.
eg, why need this class, what's is purpose?

HMSExternalTable hmsTable;

public HMSDlaTable(HMSExternalTable table) {
this.hmsTable = table;
}

abstract Map<String, PartitionItem> getAndCopyPartitionItems(Optional<MvccSnapshot> snapshot)
throws AnalysisException;

abstract PartitionType getPartitionType(Optional<MvccSnapshot> snapshot);

abstract Set<String> getPartitionColumnNames(Optional<MvccSnapshot> snapshot) throws DdlException;

abstract List<Column> getPartitionColumns(Optional<MvccSnapshot> snapshot);

abstract MTMVSnapshotIf getPartitionSnapshot(String partitionName, MTMVRefreshContext context,
Optional<MvccSnapshot> snapshot) throws AnalysisException;

abstract MTMVSnapshotIf getTableSnapshot(MTMVRefreshContext context, Optional<MvccSnapshot> snapshot)
throws AnalysisException;

abstract boolean isPartitionColumnAllowNull();

@Override
public void beforeMTMVRefresh(MTMV mtmv) throws DdlException {
Env.getCurrentEnv().getRefreshManager()
.refreshTable(hmsTable.getCatalog().getName(), hmsTable.getDbName(), hmsTable.getName(), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@
import org.apache.doris.datasource.iceberg.IcebergUtils;
import org.apache.doris.datasource.mvcc.MvccSnapshot;
import org.apache.doris.mtmv.MTMVBaseTableIf;
import org.apache.doris.mtmv.MTMVMaxTimestampSnapshot;
import org.apache.doris.mtmv.MTMVRefreshContext;
import org.apache.doris.mtmv.MTMVRelatedTableIf;
import org.apache.doris.mtmv.MTMVSnapshotIf;
import org.apache.doris.mtmv.MTMVTimestampSnapshot;
import org.apache.doris.nereids.exceptions.NotSupportedException;
import org.apache.doris.nereids.trees.plans.logical.LogicalFileScan.SelectedPartitions;
import org.apache.doris.qe.GlobalVariable;
Expand Down Expand Up @@ -159,6 +157,8 @@ public class HMSExternalTable extends ExternalTable implements MTMVRelatedTableI

private DLAType dlaType = DLAType.UNKNOWN;

private HMSDlaTable dlaTable;

// record the event update time when enable hms event listener
protected volatile long eventUpdateTime;

Expand Down Expand Up @@ -200,6 +200,7 @@ protected synchronized void makeSureInitialized() {
dlaType = DLAType.HUDI;
} else if (supportedHiveTable()) {
dlaType = DLAType.HIVE;
dlaTable = new HiveDlaTable(this);
} else {
// Should not reach here. Because `supportedHiveTable` will throw exception if not return true.
throw new NotSupportedException("Unsupported dlaType for table: " + getNameWithFullQualifiers());
Expand Down Expand Up @@ -306,7 +307,8 @@ public List<Column> getPartitionColumns() {

@Override
public List<Column> getPartitionColumns(Optional<MvccSnapshot> snapshot) {
return getPartitionColumns();
makeSureInitialized();
return dlaTable.getPartitionColumns(snapshot);
}

@Override
Expand Down Expand Up @@ -800,16 +802,18 @@ public Set<String> getDistributionColumnNames() {

@Override
public PartitionType getPartitionType(Optional<MvccSnapshot> snapshot) {
return getPartitionType();
makeSureInitialized();
return dlaTable.getPartitionType(snapshot);
}

public PartitionType getPartitionType() {
return getPartitionColumns().size() > 0 ? PartitionType.LIST : PartitionType.UNPARTITIONED;
}

@Override
public Set<String> getPartitionColumnNames(Optional<MvccSnapshot> snapshot) {
return getPartitionColumnNames();
public Set<String> getPartitionColumnNames(Optional<MvccSnapshot> snapshot) throws DdlException {
makeSureInitialized();
return dlaTable.getPartitionColumnNames(snapshot);
}

public Set<String> getPartitionColumnNames() {
Expand All @@ -818,78 +822,30 @@ public Set<String> getPartitionColumnNames() {
}

@Override
public Map<String, PartitionItem> getAndCopyPartitionItems(Optional<MvccSnapshot> snapshot) {
return getNameToPartitionItems();
public Map<String, PartitionItem> getAndCopyPartitionItems(Optional<MvccSnapshot> snapshot)
throws AnalysisException {
makeSureInitialized();
return dlaTable.getAndCopyPartitionItems(snapshot);
}

@Override
public MTMVSnapshotIf getPartitionSnapshot(String partitionName, MTMVRefreshContext context,
Optional<MvccSnapshot> snapshot) throws AnalysisException {
HiveMetaStoreCache cache = Env.getCurrentEnv().getExtMetaCacheMgr()
.getMetaStoreCache((HMSExternalCatalog) getCatalog());
HiveMetaStoreCache.HivePartitionValues hivePartitionValues = cache.getPartitionValues(
getDbName(), getName(), getPartitionColumnTypes());
Long partitionId = getPartitionIdByNameOrAnalysisException(partitionName, hivePartitionValues);
HivePartition hivePartition = getHivePartitionByIdOrAnalysisException(partitionId,
hivePartitionValues, cache);
return new MTMVTimestampSnapshot(hivePartition.getLastModifiedTime());
makeSureInitialized();
return dlaTable.getPartitionSnapshot(partitionName, context, snapshot);
}

@Override
public MTMVSnapshotIf getTableSnapshot(MTMVRefreshContext context, Optional<MvccSnapshot> snapshot)
throws AnalysisException {
if (getPartitionType() == PartitionType.UNPARTITIONED) {
return new MTMVMaxTimestampSnapshot(getName(), getLastDdlTime());
}
HivePartition maxPartition = null;
long maxVersionTime = 0L;
long visibleVersionTime;
HiveMetaStoreCache cache = Env.getCurrentEnv().getExtMetaCacheMgr()
.getMetaStoreCache((HMSExternalCatalog) getCatalog());
HiveMetaStoreCache.HivePartitionValues hivePartitionValues = cache.getPartitionValues(
getDbName(), getName(), getPartitionColumnTypes());
List<HivePartition> partitionList = cache.getAllPartitionsWithCache(getDbName(), getName(),
Lists.newArrayList(hivePartitionValues.getPartitionValuesMap().values()));
if (CollectionUtils.isEmpty(partitionList)) {
throw new AnalysisException("partitionList is empty, table name: " + getName());
}
for (HivePartition hivePartition : partitionList) {
visibleVersionTime = hivePartition.getLastModifiedTime();
if (visibleVersionTime > maxVersionTime) {
maxVersionTime = visibleVersionTime;
maxPartition = hivePartition;
}
}
return new MTMVMaxTimestampSnapshot(maxPartition.getPartitionName(getPartitionColumns()), maxVersionTime);
}

private Long getPartitionIdByNameOrAnalysisException(String partitionName,
HiveMetaStoreCache.HivePartitionValues hivePartitionValues)
throws AnalysisException {
Long partitionId = hivePartitionValues.getPartitionNameToIdMap().get(partitionName);
if (partitionId == null) {
throw new AnalysisException("can not find partition: " + partitionName);
}
return partitionId;
}

private HivePartition getHivePartitionByIdOrAnalysisException(Long partitionId,
HiveMetaStoreCache.HivePartitionValues hivePartitionValues,
HiveMetaStoreCache cache) throws AnalysisException {
List<String> partitionValues = hivePartitionValues.getPartitionValuesMap().get(partitionId);
if (CollectionUtils.isEmpty(partitionValues)) {
throw new AnalysisException("can not find partitionValues: " + partitionId);
}
HivePartition partition = cache.getHivePartition(getDbName(), getName(), partitionValues);
if (partition == null) {
throw new AnalysisException("can not find partition: " + partitionId);
}
return partition;
makeSureInitialized();
return dlaTable.getTableSnapshot(context, snapshot);
}

@Override
public boolean isPartitionColumnAllowNull() {
return true;
makeSureInitialized();
return dlaTable.isPartitionColumnAllowNull();
}

/**
Expand Down Expand Up @@ -1024,8 +980,8 @@ public boolean isPartitionedTable() {

@Override
public void beforeMTMVRefresh(MTMV mtmv) throws DdlException {
Env.getCurrentEnv().getRefreshManager()
.refreshTable(getCatalog().getName(), getDbName(), getName(), true);
makeSureInitialized();
dlaTable.beforeMTMVRefresh(mtmv);
}

public HoodieTableMetaClient getHudiClient() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.datasource.hive;

import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.PartitionItem;
import org.apache.doris.catalog.PartitionType;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.datasource.mvcc.MvccSnapshot;
import org.apache.doris.mtmv.MTMVMaxTimestampSnapshot;
import org.apache.doris.mtmv.MTMVRefreshContext;
import org.apache.doris.mtmv.MTMVSnapshotIf;
import org.apache.doris.mtmv.MTMVTimestampSnapshot;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

public class HiveDlaTable extends HMSDlaTable {

public HiveDlaTable(HMSExternalTable table) {
super(table);
}

@Override
public PartitionType getPartitionType(Optional<MvccSnapshot> snapshot) {
return hmsTable.getPartitionType();
}

@Override
public Set<String> getPartitionColumnNames(Optional<MvccSnapshot> snapshot) {
return hmsTable.getPartitionColumnNames();
}

@Override
public List<Column> getPartitionColumns(Optional<MvccSnapshot> snapshot) {
return hmsTable.getPartitionColumns();
}

@Override
public Map<String, PartitionItem> getAndCopyPartitionItems(Optional<MvccSnapshot> snapshot) {
return hmsTable.getNameToPartitionItems();
}

@Override
public MTMVSnapshotIf getPartitionSnapshot(String partitionName, MTMVRefreshContext context,
Optional<MvccSnapshot> snapshot) throws AnalysisException {
HiveMetaStoreCache cache = Env.getCurrentEnv().getExtMetaCacheMgr()
.getMetaStoreCache((HMSExternalCatalog) hmsTable.getCatalog());
HiveMetaStoreCache.HivePartitionValues hivePartitionValues = cache.getPartitionValues(
hmsTable.getDbName(), hmsTable.getName(), hmsTable.getPartitionColumnTypes());
Long partitionId = getPartitionIdByNameOrAnalysisException(partitionName, hivePartitionValues);
HivePartition hivePartition = getHivePartitionByIdOrAnalysisException(partitionId,
hivePartitionValues, cache);
return new MTMVTimestampSnapshot(hivePartition.getLastModifiedTime());
}

@Override
public MTMVSnapshotIf getTableSnapshot(MTMVRefreshContext context, Optional<MvccSnapshot> snapshot)
throws AnalysisException {
if (hmsTable.getPartitionType() == PartitionType.UNPARTITIONED) {
return new MTMVMaxTimestampSnapshot(hmsTable.getName(), hmsTable.getLastDdlTime());
}
HivePartition maxPartition = null;
long maxVersionTime = 0L;
long visibleVersionTime;
HiveMetaStoreCache cache = Env.getCurrentEnv().getExtMetaCacheMgr()
.getMetaStoreCache((HMSExternalCatalog) hmsTable.getCatalog());
HiveMetaStoreCache.HivePartitionValues hivePartitionValues = cache.getPartitionValues(
hmsTable.getDbName(), hmsTable.getName(), hmsTable.getPartitionColumnTypes());
List<HivePartition> partitionList = cache.getAllPartitionsWithCache(hmsTable.getDbName(), hmsTable.getName(),
Lists.newArrayList(hivePartitionValues.getPartitionValuesMap().values()));
if (CollectionUtils.isEmpty(partitionList)) {
throw new AnalysisException("partitionList is empty, table name: " + hmsTable.getName());
}
for (HivePartition hivePartition : partitionList) {
visibleVersionTime = hivePartition.getLastModifiedTime();
if (visibleVersionTime > maxVersionTime) {
maxVersionTime = visibleVersionTime;
maxPartition = hivePartition;
}
}
return new MTMVMaxTimestampSnapshot(maxPartition.getPartitionName(
hmsTable.getPartitionColumns()), maxVersionTime);
}

private Long getPartitionIdByNameOrAnalysisException(String partitionName,
HiveMetaStoreCache.HivePartitionValues hivePartitionValues)
throws AnalysisException {
Long partitionId = hivePartitionValues.getPartitionNameToIdMap().get(partitionName);
if (partitionId == null) {
throw new AnalysisException("can not find partition: " + partitionName);
}
return partitionId;
}

private HivePartition getHivePartitionByIdOrAnalysisException(Long partitionId,
HiveMetaStoreCache.HivePartitionValues hivePartitionValues,
HiveMetaStoreCache cache) throws AnalysisException {
List<String> partitionValues = hivePartitionValues.getPartitionValuesMap().get(partitionId);
if (CollectionUtils.isEmpty(partitionValues)) {
throw new AnalysisException("can not find partitionValues: " + partitionId);
}
HivePartition partition = cache.getHivePartition(hmsTable.getDbName(), hmsTable.getName(), partitionValues);
if (partition == null) {
throw new AnalysisException("can not find partition: " + partitionId);
}
return partition;
}

@Override
public boolean isPartitionColumnAllowNull() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.doris.datasource.hive.HMSExternalDatabase;
import org.apache.doris.datasource.hive.HMSExternalTable;
import org.apache.doris.datasource.hive.HMSExternalTable.DLAType;
import org.apache.doris.datasource.hive.HiveDlaTable;
import org.apache.doris.nereids.datasets.tpch.AnalyzeCheckTestBase;
import org.apache.doris.qe.SessionVariable;

Expand Down Expand Up @@ -107,6 +108,7 @@ private void createDbAndTableForHmsCatalog(HMSExternalCatalog hmsCatalog) {
Deencapsulation.setField(tbl, "catalog", hmsCatalog);
Deencapsulation.setField(tbl, "dbName", "hms_db");
Deencapsulation.setField(tbl, "name", "hms_tbl");
Deencapsulation.setField(tbl, "dlaTable", new HiveDlaTable(tbl));
new Expectations(tbl) {
{
tbl.getId();
Expand Down
Loading
Loading