Skip to content

Commit

Permalink
[Refactor] Synchronize OLAP external table metadata when loading data (
Browse files Browse the repository at this point in the history
…StarRocks#24739)

Currently, StarRocks use StarRocksRepository to synchronize OLAP
external table metadata every 10 seconds, which is
inefficient, because the meta data will never be used when there is no
load job on that external table. Change the synchronization to be
triggered when there is load job on that table.

Since the synchronization is time costly, so we should release the
database lock when synchronizing meta data.

Signed-off-by: gengjun-git <gengjun@starrocks.com>
  • Loading branch information
gengjun-git committed Jun 16, 2023
1 parent 121f3a7 commit 4620617
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 322 deletions.
4 changes: 4 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/catalog/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ public void readUnlock() {
this.rwLock.readLock().unlock();
}

public boolean isReadLockHeldByCurrentThread() {
return this.rwLock.getReadHoldCount() > 0;
}

public void writeLock() {
long startMs = TimeUnit.MILLISECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS);
String threadDump = getOwnerInfo(rwLock.getOwner());
Expand Down
407 changes: 200 additions & 207 deletions fe/fe-core/src/main/java/com/starrocks/catalog/ExternalOlapTable.java

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/catalog/OlapTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,40 @@ public OlapTable(long id, String tableName, List<Column> baseSchema, KeysType ke
this.tableProperty = null;
}

// Only Copy necessary metadata for query.
// We don't do deep copy, because which is very expensive;
public void copyOnlyForQuery(OlapTable olapTable) {
olapTable.id = this.id;
olapTable.name = this.name;
olapTable.fullSchema = Lists.newArrayList(this.fullSchema);
olapTable.nameToColumn = Maps.newHashMap(this.nameToColumn);
olapTable.relatedMaterializedViews = Sets.newHashSet(this.relatedMaterializedViews);
olapTable.state = this.state;
olapTable.indexNameToId = Maps.newHashMap(this.indexNameToId);
olapTable.indexIdToMeta = Maps.newHashMap(this.indexIdToMeta);
olapTable.keysType = this.keysType;
olapTable.partitionInfo = new PartitionInfo();
if (this.partitionInfo instanceof RangePartitionInfo) {
olapTable.partitionInfo = new RangePartitionInfo((RangePartitionInfo) this.partitionInfo);
} else if (this.partitionInfo instanceof SinglePartitionInfo) {
olapTable.partitionInfo = this.partitionInfo;
}
olapTable.defaultDistributionInfo = this.defaultDistributionInfo;
Map<Long, Partition> idToPartitions = new HashMap<>(this.idToPartition.size());
Map<String, Partition> nameToPartitions = Maps.newTreeMap(String.CASE_INSENSITIVE_ORDER);
for (Map.Entry<Long, Partition> kv : this.idToPartition.entrySet()) {
Partition copiedPartition = kv.getValue().shallowCopy();
idToPartitions.put(kv.getKey(), copiedPartition);
nameToPartitions.put(kv.getValue().getName(), copiedPartition);
}
olapTable.idToPartition = idToPartitions;
olapTable.nameToPartition = nameToPartitions;
olapTable.baseIndexId = this.baseIndexId;
if (this.tableProperty != null) {
olapTable.tableProperty = this.tableProperty.copy();
}
}

public void setTableProperty(TableProperty tableProperty) {
this.tableProperty = tableProperty;
}
Expand Down
15 changes: 15 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/catalog/Partition.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ public Partition(long id, String name,
this.distributionInfo = distributionInfo;
}

public Partition shallowCopy() {
Partition partition = new Partition();
partition.id = this.id;
partition.name = this.name;
partition.state = this.state;
partition.baseIndex = this.baseIndex;
partition.idToVisibleRollupIndex = Maps.newHashMap(this.idToVisibleRollupIndex);
partition.idToShadowIndex = Maps.newHashMap(this.idToShadowIndex);
partition.visibleVersion = this.visibleVersion;
partition.visibleVersionTime = this.visibleVersionTime;
partition.nextVersion = this.nextVersion;
partition.distributionInfo = this.distributionInfo;
return partition;
}

public void setIdForRestore(long id) {
this.id = id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ public RangePartitionInfo(List<Column> partitionColumns) {
this.isMultiColumnPartition = partitionColumns.size() > 1;
}

public RangePartitionInfo(RangePartitionInfo other) {
super(other.type);
this.partitionColumns = Lists.newArrayList(other.partitionColumns);
this.idToRange = Maps.newHashMap(other.idToRange);
this.idToTempRange = Maps.newHashMap(other.idToTempRange);
this.isMultiColumnPartition = partitionColumns.size() > 1;
}

@Override
public List<Column> getPartitionColumns() {
return partitionColumns;
Expand Down
2 changes: 1 addition & 1 deletion fe/fe-core/src/main/java/com/starrocks/catalog/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public enum TableType {

// not serialized field
// record all materialized views based on this Table
private Set<MvId> relatedMaterializedViews;
protected Set<MvId> relatedMaterializedViews;

// unique constraints for mv rewrite
// a table may have multi unique constraints
Expand Down
13 changes: 13 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/catalog/TableProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package com.starrocks.catalog;

import com.clearspring.analytics.util.Lists;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.Maps;
import com.google.gson.annotations.SerializedName;
Expand Down Expand Up @@ -134,6 +135,18 @@ public TableProperty(Map<String, String> properties) {
this.properties = properties;
}

public TableProperty copy() {
TableProperty newTableProperty = new TableProperty(Maps.newHashMap(this.properties));
try {
newTableProperty.gsonPostProcess();
} catch (IOException e) {
Preconditions.checkState(false, "gsonPostProcess shouldn't fail");
}
newTableProperty.hasDelete = this.hasDelete;
newTableProperty.hasForbitGlobalDict = this.hasDelete;
return newTableProperty;
}

public static boolean isSamePrefixProperties(Map<String, String> properties, String prefix) {
for (String value : properties.keySet()) {
if (!value.startsWith(prefix)) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.starrocks.catalog.ExternalOlapTable;
import com.starrocks.common.Config;
import com.starrocks.rpc.FrontendServiceProxy;
import com.starrocks.sql.common.MetaNotFoundException;
import com.starrocks.thrift.TGetTableMetaRequest;
import com.starrocks.thrift.TGetTableMetaResponse;
import com.starrocks.thrift.TNetworkAddress;
Expand All @@ -17,7 +18,7 @@
public class TableMetaSyncer {
private static final Logger LOG = LogManager.getLogger(TableMetaSyncer.class);

public void syncTable(ExternalOlapTable table) {
public void syncTable(ExternalOlapTable table) throws MetaNotFoundException {
String host = table.getSourceTableHost();
int port = table.getSourceTablePort();
TNetworkAddress addr = new TNetworkAddress(host, port);
Expand All @@ -37,11 +38,13 @@ public void syncTable(ExternalOlapTable table) {
errMsg = "";
}
LOG.warn("get TableMeta failed: {}", errMsg);
throw new MetaNotFoundException(errMsg);
} else {
table.updateMeta(request.getDb_name(), response.getTable_meta(), response.getBackends());
}
} catch (Exception e) {
LOG.warn("call fe {} refreshTable rpc method failed", addr, e);
throw new MetaNotFoundException("get TableMeta failed from " + addr + ", error: " + e.getMessage());
}
}
};
}
7 changes: 6 additions & 1 deletion fe/fe-core/src/main/java/com/starrocks/qe/StmtExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,12 @@ private void handleDMLStmtImpl(ExecPlan execPlan, DmlStmt stmt) throws Exception

MetaUtils.normalizationTableName(context, stmt.getTableName());
Database database = MetaUtils.getDatabase(context, stmt.getTableName());
Table targetTable = MetaUtils.getTable(context, stmt.getTableName());
Table targetTable;
if (stmt instanceof InsertStmt && ((InsertStmt) stmt).getTargetTable() != null) {
targetTable = ((InsertStmt) stmt).getTargetTable();
} else {
targetTable = MetaUtils.getTable(context, stmt.getTableName());
}

String label = DebugUtil.printId(context.getExecutionId());
if (stmt instanceof InsertStmt) {
Expand Down
10 changes: 0 additions & 10 deletions fe/fe-core/src/main/java/com/starrocks/server/GlobalStateMgr.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@
import com.starrocks.consistency.ConsistencyChecker;
import com.starrocks.credential.CloudCredentialUtil;
import com.starrocks.external.elasticsearch.EsRepository;
import com.starrocks.external.starrocks.StarRocksRepository;
import com.starrocks.ha.FrontendNodeType;
import com.starrocks.ha.HAProtocol;
import com.starrocks.ha.LeaderInfo;
Expand Down Expand Up @@ -338,7 +337,6 @@ public class GlobalStateMgr {
private Daemon replayer;
private Daemon timePrinter;
private EsRepository esRepository; // it is a daemon, so add it here
private StarRocksRepository starRocksRepository;
private IcebergRepository icebergRepository;
private MetastoreEventsProcessor metastoreEventsProcessor;
private ConnectorTableMetadataProcessor connectorTableMetadataProcessor;
Expand Down Expand Up @@ -592,7 +590,6 @@ private GlobalStateMgr(boolean isCheckpointCatalog) {
this.resourceGroupMgr = new ResourceGroupMgr(this);

this.esRepository = new EsRepository();
this.starRocksRepository = new StarRocksRepository();
this.icebergRepository = new IcebergRepository();
this.metastoreEventsProcessor = new MetastoreEventsProcessor();
this.connectorTableMetadataProcessor = new ConnectorTableMetadataProcessor();
Expand Down Expand Up @@ -1217,7 +1214,6 @@ private void startNonLeaderDaemonThreads() {
labelCleaner.start();
// ES state store
esRepository.start();
starRocksRepository.start();

if (Config.enable_hms_events_incremental_sync) {
metastoreEventsProcessor.start();
Expand Down Expand Up @@ -1291,7 +1287,6 @@ public void loadImage(String imageDir) throws IOException, DdlException {
localMetastore.recreateTabletInvertIndex();
// rebuild es state state
esRepository.loadTableFromCatalog();
starRocksRepository.loadTableFromCatalog();

checksum = load.loadLoadJob(dis, checksum);
checksum = loadAlterJob(dis, checksum);
Expand Down Expand Up @@ -2821,15 +2816,10 @@ public EsRepository getEsRepository() {
return this.esRepository;
}

public StarRocksRepository getStarRocksRepository() {
return this.starRocksRepository;
}

public IcebergRepository getIcebergRepository() {
return this.icebergRepository;
}


public MetastoreEventsProcessor getMetastoreEventsProcessor() {
return this.metastoreEventsProcessor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import com.starrocks.analysis.Expr;
import com.starrocks.catalog.Column;
import com.starrocks.catalog.Database;
import com.starrocks.catalog.ExternalOlapTable;
import com.starrocks.catalog.MaterializedView;
import com.starrocks.catalog.MysqlTable;
import com.starrocks.catalog.OlapTable;
import com.starrocks.catalog.Partition;
import com.starrocks.catalog.Table;
import com.starrocks.common.AnalysisException;
import com.starrocks.external.starrocks.TableMetaSyncer;
import com.starrocks.qe.ConnectContext;
import com.starrocks.sql.ast.DefaultValueExpr;
import com.starrocks.sql.ast.InsertStmt;
Expand Down Expand Up @@ -40,6 +43,10 @@ public static void analyze(InsertStmt insertStmt, ConnectContext session) {
Database database = MetaUtils.getDatabase(session, insertStmt.getTableName());
Table table = MetaUtils.getTable(session, insertStmt.getTableName());

if (table instanceof ExternalOlapTable) {
table = getOLAPExternalTableMeta(database, (ExternalOlapTable) table);
}

if (table instanceof MaterializedView && !insertStmt.isSystem()) {
throw new SemanticException(
"The data of '%s' cannot be inserted because '%s' is a materialized view," +
Expand Down Expand Up @@ -151,4 +158,23 @@ public static void analyze(InsertStmt insertStmt, ConnectContext session) {
insertStmt.setTargetColumns(targetColumns);
session.getDumpInfo().addTable(database.getFullName(), table);
}

private static ExternalOlapTable getOLAPExternalTableMeta(Database database, ExternalOlapTable externalOlapTable) {
// copy the table, and release database lock when synchronize table meta
ExternalOlapTable copiedTable = new ExternalOlapTable();
externalOlapTable.copyOnlyForQuery(copiedTable);
int lockTimes = 0;
while (database.isReadLockHeldByCurrentThread()) {
database.readUnlock();
lockTimes++;
}
try {
new TableMetaSyncer().syncTable(copiedTable);
} finally {
while (lockTimes-- > 0) {
database.readLock();
}
}
return copiedTable;
}
}

0 comments on commit 4620617

Please sign in to comment.