Skip to content
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 @@ -20,16 +20,21 @@
import jakarta.validation.constraints.NotNull;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

import static com.google.common.collect.Comparators.max;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;

public class CachingHiveMetastoreConfig
{
private Duration metastoreCacheTtl = new Duration(0, TimeUnit.SECONDS);
// Use 5 mins for stats cache TTL by default. 5 mins will be sufficient to help
// significantly when there is high number of concurrent queries.
// 5 mins will also prevent stats from being stalled for a long time since
// time window where table data can be altered is limited.
private Duration statsCacheTtl = new Duration(5, TimeUnit.MINUTES);
public static final Duration DEFAULT_STATS_CACHE_TTL = new Duration(5, MINUTES);

private Duration metastoreCacheTtl = new Duration(0, SECONDS);
private Optional<Duration> statsCacheTtl = Optional.empty();
private Optional<Duration> metastoreRefreshInterval = Optional.empty();
private long metastoreCacheMaximumSize = 10000;
private int maxMetastoreRefreshThreads = 10;
Expand All @@ -52,13 +57,13 @@ public CachingHiveMetastoreConfig setMetastoreCacheTtl(Duration metastoreCacheTt
@NotNull
public Duration getStatsCacheTtl()
{
return statsCacheTtl;
return statsCacheTtl.orElseGet(() -> max(metastoreCacheTtl, DEFAULT_STATS_CACHE_TTL));
}

@Config("hive.metastore-stats-cache-ttl")
public CachingHiveMetastoreConfig setStatsCacheTtl(Duration statsCacheTtl)
{
this.statsCacheTtl = statsCacheTtl;
this.statsCacheTtl = Optional.of(statsCacheTtl);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,7 @@ public SharedHiveMetastoreCache(
this.catalogName = catalogName;

metadataCacheTtl = config.getMetastoreCacheTtl();
if (metadataCacheTtl.compareTo(config.getStatsCacheTtl()) > 0) {
statsCacheTtl = metadataCacheTtl;
}
else {
statsCacheTtl = config.getStatsCacheTtl();
}

statsCacheTtl = config.getStatsCacheTtl();
maxMetastoreRefreshThreads = config.getMaxMetastoreRefreshThreads();
metastoreRefreshInterval = config.getMetastoreRefreshInterval();
metastoreCacheMaximumSize = config.getMetastoreCacheMaximumSize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,26 @@
import org.junit.jupiter.api.Test;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import static io.airlift.configuration.testing.ConfigAssertions.assertFullMapping;
import static io.airlift.configuration.testing.ConfigAssertions.assertRecordedDefaults;
import static io.airlift.configuration.testing.ConfigAssertions.recordDefaults;
import static io.trino.plugin.hive.metastore.cache.CachingHiveMetastoreConfig.DEFAULT_STATS_CACHE_TTL;
import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;

public class TestCachingHiveMetastoreConfig
{
@Test
public void testDefaults()
{
assertRecordedDefaults(recordDefaults(CachingHiveMetastoreConfig.class)
.setMetastoreCacheTtl(new Duration(0, TimeUnit.SECONDS))
.setStatsCacheTtl(new Duration(5, TimeUnit.MINUTES))
.setMetastoreCacheTtl(new Duration(0, SECONDS))
.setStatsCacheTtl(new Duration(5, MINUTES))
.setMetastoreRefreshInterval(null)
.setMetastoreCacheMaximumSize(10000)
.setMaxMetastoreRefreshThreads(10)
Expand All @@ -53,14 +59,35 @@ public void testExplicitPropertyMappings()
.buildOrThrow();

CachingHiveMetastoreConfig expected = new CachingHiveMetastoreConfig()
.setMetastoreCacheTtl(new Duration(2, TimeUnit.HOURS))
.setStatsCacheTtl(new Duration(10, TimeUnit.MINUTES))
.setMetastoreRefreshInterval(new Duration(30, TimeUnit.MINUTES))
.setMetastoreCacheTtl(new Duration(2, HOURS))
.setStatsCacheTtl(new Duration(10, MINUTES))
.setMetastoreRefreshInterval(new Duration(30, MINUTES))
.setMetastoreCacheMaximumSize(5000)
.setMaxMetastoreRefreshThreads(2500)
.setCacheMissing(false)
.setPartitionCacheEnabled(false);

assertFullMapping(properties, expected);
}

@Test
public void testStatsCacheTtl()
{
// enabled by default
assertThat(new CachingHiveMetastoreConfig().getStatsCacheTtl()).isEqualTo(DEFAULT_STATS_CACHE_TTL);

// takes higher of the DEFAULT_STATS_CACHE_TTL or metastoreTtl if not set explicitly
assertThat(new CachingHiveMetastoreConfig()
.setMetastoreCacheTtl(new Duration(1, SECONDS))
.getStatsCacheTtl()).isEqualTo(DEFAULT_STATS_CACHE_TTL);
assertThat(new CachingHiveMetastoreConfig()
.setMetastoreCacheTtl(new Duration(1111, DAYS))
.getStatsCacheTtl()).isEqualTo(new Duration(1111, DAYS));

// explicit configuration is honored
assertThat(new CachingHiveMetastoreConfig()
.setStatsCacheTtl(new Duration(135, MILLISECONDS))
.setMetastoreCacheTtl(new Duration(1111, DAYS))
Comment on lines +89 to +90
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we disallow this configuration ? I can't think of a good reason why someone would want statistics metadata ttl to be shorter than other metadata ttl.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes we can. Neither I can think of a good reason why someone would want that. OTOH, these are separate configuration toggles, so unclear what we gain by disallowing certain configurations which just work otherwise.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Disallowing it prevents users from making a mistake in the configuration and saves debugging time for us.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There are two toggles

  • A hive.metastore-cache-ttl
  • B hive.metastore-stats-cache-ttl

if B < A, we can call it a mistake and reject. Do we have guarantee this is a mistake?
Or we risk that we gonna need to spend time explaining why we thought this is a good idea to reject such configurations?

BTW I think this discussion isn't very important. The whole problem comes from the fact that separate hive.metastore-stats-cache-ttl toggle was introduced so that it can be enabled by default. This PR doesn't change anything with respect to this main issue -- being enabled by default; default behavior doesn't change. So I guess we're spending time about some edge case.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

discussed offline and concluded this will be as it is for now at least

.getStatsCacheTtl()).isEqualTo(new Duration(135, MILLISECONDS));
}
}