Skip to content

Commit

Permalink
perf: enable compression for insertion (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
wrn14897 authored Sep 8, 2024
1 parent f7ae1a4 commit c5efb84
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-cobras-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperdx/api': patch
---

perf: enable compression for insertion
8 changes: 4 additions & 4 deletions packages/api/src/clickhouse/__tests__/clickhouse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ Array [

it('clientInsertWithRetries (success)', async () => {
jest
.spyOn(clickhouse.client, 'insert')
.spyOn(clickhouse.insertCHClient, 'insert')
.mockRejectedValueOnce(new Error('first error'))
.mockRejectedValueOnce(new Error('second error'))
.mockResolvedValueOnce(null as any);
Expand All @@ -1320,12 +1320,12 @@ Array [
timeout: 100,
});

expect(clickhouse.client.insert).toHaveBeenCalledTimes(3);
expect(clickhouse.insertCHClient.insert).toHaveBeenCalledTimes(3);
});

it('clientInsertWithRetries (fail)', async () => {
jest
.spyOn(clickhouse.client, 'insert')
.spyOn(clickhouse.insertCHClient, 'insert')
.mockRejectedValueOnce(new Error('first error'))
.mockRejectedValueOnce(new Error('second error'));

Expand All @@ -1340,7 +1340,7 @@ Array [
expect(error.message).toBe('second error');
}

expect(clickhouse.client.insert).toHaveBeenCalledTimes(2);
expect(clickhouse.insertCHClient.insert).toHaveBeenCalledTimes(2);
expect.assertions(2);
});

Expand Down
14 changes: 12 additions & 2 deletions packages/api/src/clickhouse/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ClickHouseClientConfigOptions,
createClient,
ErrorLogParams as _CHErrorLogParams,
Logger as _CHLogger,
Expand Down Expand Up @@ -156,7 +157,7 @@ export class CHLogger implements _CHLogger {
}

// TODO: move this to somewhere else
export const client = createClient({
const CH_CLIENT_CONFIG_OPTIONS: ClickHouseClientConfigOptions = {
url: config.CLICKHOUSE_HOST,
username: config.CLICKHOUSE_USER,
password: config.CLICKHOUSE_PASSWORD,
Expand All @@ -183,6 +184,15 @@ export const client = createClient({
log: {
LoggerClass: CHLogger,
},
};

export const client = createClient(CH_CLIENT_CONFIG_OPTIONS);
export const insertCHClient = createClient({
...CH_CLIENT_CONFIG_OPTIONS,
compression: {
request: true,
response: true,
},
});

export const getLogStreamTableName = (
Expand Down Expand Up @@ -380,7 +390,7 @@ export const clientInsertWithRetries = async <T>({
const ts = Date.now();
while (maxRetries > 0) {
try {
await client.insert({
await insertCHClient.insert({
table,
values,
format: 'JSONEachRow',
Expand Down

0 comments on commit c5efb84

Please sign in to comment.