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
5 changes: 5 additions & 0 deletions .changeset/mean-ghosts-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hyperdx/common-utils": patch
---

fix: Include connectionId in metadata cache key
4 changes: 2 additions & 2 deletions packages/common-utils/src/__tests__/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe('Metadata', () => {

// Setup the cache to return the mock data
mockCache.getOrFetch.mockImplementation((key, queryFn) => {
if (key === 'test_db.test_table.metadata') {
if (key === 'test_connection.test_db.test_table.metadata') {
return Promise.resolve(mockTableMetadata);
}
return queryFn();
Expand All @@ -212,7 +212,7 @@ describe('Metadata', () => {

// Verify the cache was called with the right key
expect(mockCache.getOrFetch).toHaveBeenCalledWith(
'test_db.test_table.metadata',
'test_connection.test_db.test_table.metadata',
expect.any(Function),
);

Expand Down
86 changes: 43 additions & 43 deletions packages/common-utils/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,21 @@ export class Metadata {
cache: MetadataCache;
connectionId: string;
}) {
return cache.getOrFetch(`${database}.${table}.metadata`, async () => {
const sql = chSql`SELECT * FROM system.tables where database = ${{ String: database }} AND name = ${{ String: table }}`;
const json = await this.clickhouseClient
.query<'JSON'>({
connectionId,
query: sql.sql,
query_params: sql.params,
clickhouse_settings: this.getClickHouseSettings(),
})
.then(res => res.json<TableMetadata>());
return json.data[0];
});
return cache.getOrFetch(
`${connectionId}.${database}.${table}.metadata`,
async () => {
const sql = chSql`SELECT * FROM system.tables where database = ${{ String: database }} AND name = ${{ String: table }}`;
const json = await this.clickhouseClient
.query<'JSON'>({
connectionId,
query: sql.sql,
query_params: sql.params,
clickhouse_settings: this.getClickHouseSettings(),
})
.then(res => res.json<TableMetadata>());
return json.data[0];
},
);
}

async getColumns({
Expand All @@ -153,7 +156,7 @@ export class Metadata {
connectionId: string;
}) {
return this.cache.getOrFetch<ColumnMeta[]>(
`${databaseName}.${tableName}.columns`,
`${connectionId}.${databaseName}.${tableName}.columns`,
async () => {
const sql = chSql`DESCRIBE ${tableExpr({ database: databaseName, table: tableName })}`;
const columns = await this.clickhouseClient
Expand Down Expand Up @@ -240,8 +243,8 @@ export class Metadata {
metricName?: string;
}) {
const cacheKey = metricName
? `${databaseName}.${tableName}.${column}.${metricName}.keys`
: `${databaseName}.${tableName}.${column}.keys`;
? `${connectionId}.${databaseName}.${tableName}.${column}.${metricName}.keys`
: `${connectionId}.${databaseName}.${tableName}.${column}.keys`;
const cachedKeys = this.cache.get<string[]>(cacheKey);

if (cachedKeys != null) {
Expand Down Expand Up @@ -351,8 +354,8 @@ export class Metadata {
// HDX-2480 delete line below to reenable json filters
return []; // Need to disable JSON keys for the time being.
const cacheKey = metricName
? `${databaseName}.${tableName}.${column}.${metricName}.keys`
: `${databaseName}.${tableName}.${column}.keys`;
? `${connectionId}.${databaseName}.${tableName}.${column}.${metricName}.keys`
: `${connectionId}.${databaseName}.${tableName}.${column}.keys`;

return this.cache.getOrFetch<{ key: string; chType: string }[]>(
cacheKey,
Expand Down Expand Up @@ -420,9 +423,9 @@ export class Metadata {
maxValues?: number;
connectionId: string;
}) {
const cachedValues = this.cache.get<string[]>(
`${databaseName}.${tableName}.${column}.${key}.values`,
);
const cacheKey = `${connectionId}.${databaseName}.${tableName}.${column}.${key}.values`;

const cachedValues = this.cache.get<string[]>(cacheKey);

if (cachedValues != null) {
return cachedValues;
Expand Down Expand Up @@ -450,28 +453,25 @@ export class Metadata {
}}
`;

return this.cache.getOrFetch<string[]>(
`${databaseName}.${tableName}.${column}.${key}.values`,
async () => {
const values = await this.clickhouseClient
.query<'JSON'>({
query: sql.sql,
query_params: sql.params,
connectionId,
clickhouse_settings: {
max_rows_to_read: String(
this.getClickHouseSettings().max_rows_to_read ??
DEFAULT_METADATA_MAX_ROWS_TO_READ,
),
read_overflow_mode: 'break',
...this.getClickHouseSettings(),
},
})
.then(res => res.json<Record<string, unknown>>())
.then(d => d.data.map(row => row.value as string));
return values;
},
);
return this.cache.getOrFetch<string[]>(cacheKey, async () => {
const values = await this.clickhouseClient
.query<'JSON'>({
query: sql.sql,
query_params: sql.params,
connectionId,
clickhouse_settings: {
max_rows_to_read: String(
this.getClickHouseSettings().max_rows_to_read ??
DEFAULT_METADATA_MAX_ROWS_TO_READ,
),
read_overflow_mode: 'break',
...this.getClickHouseSettings(),
},
})
.then(res => res.json<Record<string, unknown>>())
.then(d => d.data.map(row => row.value as string));
return values;
});
}

async getAllFields({
Expand Down Expand Up @@ -666,7 +666,7 @@ export class Metadata {
disableRowLimit?: boolean;
}) {
return this.cache.getOrFetch(
`${chartConfig.from.databaseName}.${chartConfig.from.tableName}.${keys.join(',')}.${chartConfig.dateRange.toString()}.${disableRowLimit}.values`,
`${chartConfig.connection}.${chartConfig.from.databaseName}.${chartConfig.from.tableName}.${keys.join(',')}.${chartConfig.dateRange.toString()}.${disableRowLimit}.values`,
async () => {
const selectClause = keys
.map((k, i) => `groupUniqArray(${limit})(${k}) AS param${i}`)
Expand Down
Loading