-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[OPIK-7256] fix(backend): make project size-estimate cache actually engage #7371
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
Open
thiagohora
wants to merge
4
commits into
main
Choose a base branch
from
thiagoh/OPIK-7256-fix-project-metadata-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+142
−2
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f27f88a
[OPIK-7256] fix(backend): make project size-estimate cache actually e…
thiagohora 819bfa8
[OPIK-7256] test: guard that the project_metadata cache engages
thiagohora 7ebbfd2
[OPIK-7256] test: replace fixed sleep with deterministic wait on the …
thiagohora acd802b
[OPIK-7256] make the cached overload public, matching codebase conven…
thiagohora File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...end/src/test/java/com/comet/opik/domain/workspaces/WorkspaceMetadataServiceCacheTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package com.comet.opik.domain.workspaces; | ||
|
|
||
| import com.comet.opik.api.resources.utils.ClickHouseContainerUtils; | ||
| import com.comet.opik.api.resources.utils.MigrationUtils; | ||
| import com.comet.opik.api.resources.utils.MySQLContainerUtils; | ||
| import com.comet.opik.api.resources.utils.RedisContainerUtils; | ||
| import com.comet.opik.api.resources.utils.TestDropwizardAppExtensionUtils; | ||
| import com.comet.opik.api.resources.utils.TestDropwizardAppExtensionUtils.AppContextConfig; | ||
| import com.comet.opik.api.resources.utils.TestDropwizardAppExtensionUtils.CustomConfig; | ||
| import com.comet.opik.extensions.DropwizardAppExtensionProvider; | ||
| import com.comet.opik.extensions.RegisterApp; | ||
| import com.google.inject.AbstractModule; | ||
| import com.redis.testcontainers.RedisContainer; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.TestInstance; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.testcontainers.clickhouse.ClickHouseContainer; | ||
| import org.testcontainers.containers.GenericContainer; | ||
| import org.testcontainers.lifecycle.Startables; | ||
| import org.testcontainers.mysql.MySQLContainer; | ||
| import reactor.core.publisher.Mono; | ||
| import ru.vyarus.dropwizard.guice.test.jupiter.ext.TestDropwizardAppExtension; | ||
|
|
||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.ThreadLocalRandom; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import static com.comet.opik.api.resources.utils.ClickHouseContainerUtils.DATABASE_NAME; | ||
| import static com.comet.opik.api.resources.utils.TestUtils.waitForMillis; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * Guards that the {@code project_metadata} cache on | ||
| * {@link WorkspaceMetadataServiceImpl#getProjectMetadata(String, UUID)} actually engages: the | ||
| * method must stay interceptable by Guice (not private) and the cache name must stay configured, | ||
| * otherwise the expensive per-project size estimate silently runs on every call. | ||
| */ | ||
| @TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
| @ExtendWith(DropwizardAppExtensionProvider.class) | ||
| class WorkspaceMetadataServiceCacheTest { | ||
|
|
||
| private final RedisContainer REDIS = RedisContainerUtils.newRedisContainer(); | ||
| private final MySQLContainer MYSQL = MySQLContainerUtils.newMySQLContainer(); | ||
| private final GenericContainer<?> ZOOKEEPER_CONTAINER = ClickHouseContainerUtils.newZookeeperContainer(); | ||
| private final ClickHouseContainer CLICKHOUSE = ClickHouseContainerUtils.newClickHouseContainer(ZOOKEEPER_CONTAINER); | ||
|
|
||
| private static final long CACHE_ASYNC_WAIT_MILLIS = 100; | ||
|
|
||
| private static final AtomicInteger PROJECT_METADATA_DAO_CALLS = new AtomicInteger(); | ||
|
|
||
| private static final WorkspaceMetadataDAO COUNTING_DAO = new WorkspaceMetadataDAO() { | ||
|
|
||
| @Override | ||
| public Mono<ScopeMetadata> getProjectMetadata(String workspaceId, UUID projectId) { | ||
| PROJECT_METADATA_DAO_CALLS.incrementAndGet(); | ||
| return Mono.just(ScopeMetadata.builder() | ||
| .sizeGb(ThreadLocalRandom.current().nextDouble()) | ||
| .totalTableSizeGb(100) | ||
| .percentageOfTable(1) | ||
| .limitSizeGb(10) | ||
| .build()); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<ExperimentScopeMetadata> getExperimentMetadata(String workspaceId, UUID datasetId) { | ||
| return Mono.just(ExperimentScopeMetadata.builder().build()); | ||
| } | ||
| }; | ||
|
|
||
| @RegisterApp | ||
| private final TestDropwizardAppExtension APP; | ||
|
|
||
| { | ||
| Startables.deepStart(MYSQL, CLICKHOUSE, REDIS, ZOOKEEPER_CONTAINER).join(); | ||
|
|
||
| var databaseAnalyticsFactory = ClickHouseContainerUtils.newDatabaseAnalyticsFactory( | ||
| CLICKHOUSE, DATABASE_NAME); | ||
|
|
||
| MigrationUtils.runMysqlDbMigration(MYSQL); | ||
| MigrationUtils.runClickhouseDbMigration(CLICKHOUSE); | ||
|
|
||
| APP = TestDropwizardAppExtensionUtils.newTestDropwizardAppExtension( | ||
| AppContextConfig.builder() | ||
| .jdbcUrl(MYSQL.getJdbcUrl()) | ||
| .databaseAnalyticsFactory(databaseAnalyticsFactory) | ||
| .redisUrl(REDIS.getRedisURI()) | ||
| .modules(List.of(new AbstractModule() { | ||
|
|
||
| @Override | ||
| protected void configure() { | ||
| bind(WorkspaceMetadataDAO.class).toInstance(COUNTING_DAO); | ||
| } | ||
|
|
||
| })) | ||
| .customConfigs( | ||
| List.of( | ||
| new CustomConfig("cacheManager.enabled", "true"), | ||
| new CustomConfig("cacheManager.caches.project_metadata", "PT30S"))) | ||
| .build()); | ||
| } | ||
|
|
||
| @Test | ||
| void getProjectMetadata__repeatedCallsAreServedFromCache(WorkspaceMetadataService service) { | ||
| var impl = (WorkspaceMetadataServiceImpl) service; | ||
| var workspaceId = UUID.randomUUID().toString(); | ||
| var projectId = UUID.randomUUID(); | ||
|
|
||
| var first = impl.getProjectMetadata(workspaceId, projectId).block(); | ||
|
|
||
| assertThat(first).isNotNull(); | ||
| assertThat(PROJECT_METADATA_DAO_CALLS.get()).isEqualTo(1); | ||
|
|
||
| // wait for the cache put async to complete | ||
| waitForMillis(CACHE_ASYNC_WAIT_MILLIS); | ||
|
|
||
| // same key: served from cache, the DAO must not be called again | ||
| var second = impl.getProjectMetadata(workspaceId, projectId).block(); | ||
|
|
||
| assertThat(second).isEqualTo(first); | ||
| assertThat(PROJECT_METADATA_DAO_CALLS.get()).isEqualTo(1); | ||
|
|
||
| // different project: cache miss, the DAO is called again | ||
| var other = impl.getProjectMetadata(workspaceId, UUID.randomUUID()).block(); | ||
|
|
||
| assertThat(other).isNotEqualTo(first); | ||
| assertThat(PROJECT_METADATA_DAO_CALLS.get()).isEqualTo(2); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.