Skip to content

[backport #37549][branch-3.2][Enhancement] invalidate table cache after hive table do not need to refresh by ConnectorTableMetadataProcessor #37635

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

Merged
merged 1 commit into from
Dec 22, 2023
Merged
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
Expand Up @@ -553,15 +553,20 @@ public List<HivePartitionName> refreshTableBackground(String hiveDbName, String
if (lastAccessTimeMap.containsKey(hiveTableName)) {
long lastAccessTime = lastAccessTimeMap.get(hiveTableName);
long intervalSec = (System.currentTimeMillis() - lastAccessTime) / 1000;
if (intervalSec > Config.background_refresh_metadata_time_secs_since_last_access_secs) {
long refreshIntervalSinceLastAccess = Config.background_refresh_metadata_time_secs_since_last_access_secs;
if (refreshIntervalSinceLastAccess >= 0 && intervalSec > refreshIntervalSinceLastAccess) {
// invalidate table cache
invalidateTable(hiveDbName, hiveTblName);
lastAccessTimeMap.remove(hiveTableName);
LOG.info("{}.{} skip refresh because of the last access time is {}", hiveDbName, hiveTblName,
LocalDateTime.ofInstant(Instant.ofEpochMilli(lastAccessTime), ZoneId.systemDefault()));
return null;
}
}

List<HivePartitionName> refreshPartitionNames = refreshTable(hiveDbName, hiveTblName, onlyCachedPartitions);
lastAccessTimeMap.keySet().removeIf(tableName -> !getCachedTableNames().contains(tableName));
Set<HiveTableName> cachedTableNames = getCachedTableNames();
lastAccessTimeMap.keySet().removeIf(tableName -> !(cachedTableNames.contains(tableName)));
return refreshPartitionNames;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.starrocks.catalog.HiveTable;
import com.starrocks.catalog.PrimitiveType;
import com.starrocks.catalog.ScalarType;
import com.starrocks.common.Config;
import com.starrocks.connector.MetastoreType;
import com.starrocks.connector.PartitionUtil;
import com.starrocks.connector.exception.StarRocksConnectorException;
Expand All @@ -44,7 +45,7 @@ public class CachingHiveMetastoreTest {
private HiveMetaClient client;
private HiveMetastore metastore;
private ExecutorService executor;
private long expireAfterWriteSec = 10;
private long expireAfterWriteSec = 30;
private long refreshAfterWriteSec = -1;

@Before
Expand Down Expand Up @@ -166,6 +167,49 @@ public void testRefreshTableSync() {
Assert.assertEquals(1, cachingHiveMetastore.tableNameLockMap.size());
}

@Test
public void testRefreshTableBackground() throws InterruptedException {
CachingHiveMetastore cachingHiveMetastore = new CachingHiveMetastore(
metastore, executor, expireAfterWriteSec, refreshAfterWriteSec, 1000, false);
Assert.assertFalse(cachingHiveMetastore.tableNameLockMap.containsKey(
HiveTableName.of("db1", "tbl1")));
try {
// mock query table tbl1
List<String> partitionNames = cachingHiveMetastore.getPartitionKeysByValue("db1", "tbl1",
HivePartitionValue.ALL_PARTITION_VALUES);
cachingHiveMetastore.getPartitionsByNames("db1",
"tbl1", partitionNames);
// put table tbl1 in table cache
cachingHiveMetastore.refreshTable("db1", "tbl1", true);
} catch (Exception e) {
Assert.fail();
}
Assert.assertTrue(cachingHiveMetastore.isTablePresent(HiveTableName.of("db1", "tbl1")));

try {
cachingHiveMetastore.refreshTableBackground("db1", "tbl1", true);
} catch (Exception e) {
Assert.fail();
}
// not skip refresh table, table cache still exist
Assert.assertTrue(cachingHiveMetastore.isTablePresent(HiveTableName.of("db1", "tbl1")));
// sleep 1s, background refresh table will be skipped
Thread.sleep(1000);
long oldValue = Config.background_refresh_metadata_time_secs_since_last_access_secs;
// not refresh table, just skip refresh table
Config.background_refresh_metadata_time_secs_since_last_access_secs = 0;

try {
cachingHiveMetastore.refreshTableBackground("db1", "tbl1", true);
} catch (Exception e) {
Assert.fail();
} finally {
Config.background_refresh_metadata_time_secs_since_last_access_secs = oldValue;
}
// table cache will be removed because of skip refresh table
Assert.assertFalse(cachingHiveMetastore.isTablePresent(HiveTableName.of("db1", "tbl1")));
}

@Test
public void testRefreshHiveView() {
CachingHiveMetastore cachingHiveMetastore = new CachingHiveMetastore(
Expand Down