Skip to content

Commit 91a0daf

Browse files
authored
Merge pull request #361 from metrico/always-use-dbname
3.0: Always use dbname
2 parents fdc3153 + 050781c commit 91a0daf

File tree

5 files changed

+79
-79
lines changed

5 files changed

+79
-79
lines changed

lib/db/clickhouse.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ const samplesReadTable = {
13191319
}
13201320
logger.info('checking last timestamp')
13211321
const v1EndTime = await axios.post(`${getClickhouseUrl()}/?database=${UTILS.DATABASE_NAME()}`,
1322-
`SELECT max(timestamp_ns) as ts FROM ${tableName} format JSON`)
1322+
`SELECT max(timestamp_ns) as ts FROM ${UTILS.DATABASE_NAME()}.${tableName} format JSON`)
13231323
if (!v1EndTime.data.rows) {
13241324
samplesReadTable.v1 = false
13251325
return
@@ -1339,7 +1339,7 @@ const samplesReadTable = {
13391339
settingsVersions: async function () {
13401340
const versions = await rawRequest(
13411341
`SELECT argMax(name, inserted_at) as _name, argMax(value, inserted_at) as _value
1342-
FROM settings${dist} WHERE type == 'update' GROUP BY fingerprint HAVING _name != '' FORMAT JSON`,
1342+
FROM ${UTILS.DATABASE_NAME()}.settings${dist} WHERE type == 'update' GROUP BY fingerprint HAVING _name != '' FORMAT JSON`,
13431343
null,
13441344
UTILS.DATABASE_NAME()
13451345
)
@@ -1385,7 +1385,7 @@ const getSettings = async (names, database) => {
13851385
'short-hash'))
13861386
const settings = await rawRequest(`SELECT argMax(name, inserted_at) as _name,
13871387
argMax(value, inserted_at) as _value
1388-
FROM settings${dist} WHERE fingerprint IN (${fps.join(',')}) GROUP BY fingerprint HAVING _name != '' FORMAT JSON`,
1388+
FROM ${database}.settings${dist} WHERE fingerprint IN (${fps.join(',')}) GROUP BY fingerprint HAVING _name != '' FORMAT JSON`,
13891389
null, database)
13901390
return settings.data.data.reduce((sum, cur) => {
13911391
sum[cur._name] = cur._value
@@ -1403,7 +1403,7 @@ const getSettings = async (names, database) => {
14031403
*/
14041404
const addSetting = async (type, name, value, database) => {
14051405
const fp = UTILS.fingerPrint(JSON.stringify({ type: type, name: name }), false, 'short-hash')
1406-
return rawRequest('INSERT INTO settings (fingerprint, type, name, value, inserted_at) FORMAT JSONEachRow',
1406+
return rawRequest(`INSERT INTO ${UTILS.DATABASE_NAME()}.settings (fingerprint, type, name, value, inserted_at) FORMAT JSONEachRow`,
14071407
JSON.stringify({
14081408
fingerprint: fp,
14091409
type: type,

lib/db/clickhouse_alerting.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
const axios = require('axios')
66
const { DATABASE_NAME } = require('../utils')
77
const UTILS = require('../utils')
8-
const { getClickhouseUrl } = require('./clickhouse')
8+
const { getClickhouseUrl, rawRequest } = require('./clickhouse')
99
const Sql = require('@cloki/clickhouse-sql')
1010
const { clusterName } = require('../../common')
1111
const onCluster = clusterName ? `ON CLUSTER ${clusterName}` : ''
@@ -19,14 +19,13 @@ const dist = clusterName ? '_dist' : ''
1919
module.exports.getAlertRule = async (ns, group, name) => {
2020
const fp = getRuleFP(ns, group, name)
2121
const mark = Math.random()
22-
const res = await axios.post(getClickhouseUrl(),
22+
const res = await rawRequest(
2323
'SELECT fingerprint, argMax(name, inserted_at) as name, argMax(value, inserted_at) as value ' +
2424
`FROM ${DATABASE_NAME()}.settings${dist} ` +
2525
`WHERE fingerprint = ${fp} AND ${mark} == ${mark} ` +
2626
'GROUP BY fingerprint ' +
2727
'HAVING name != \'\' ' +
28-
'FORMAT JSON'
29-
)
28+
'FORMAT JSON', null, DATABASE_NAME())
3029
if (!res.data.data.length) {
3130
return undefined
3231
}
@@ -49,10 +48,11 @@ module.exports.putAlertRule = async (namespace, group, rule) => {
4948
const groupName = JSON.stringify({ type: 'alert_group', ns: namespace, group: group.name })
5049
const groupFp = getGroupFp(namespace, group.name)
5150
const groupVal = JSON.stringify({ name: group.name, interval: group.interval })
52-
await axios.post(getClickhouseUrl(),
53-
`INSERT INTO ${DATABASE_NAME()}.settings${dist} (fingerprint, type, name, value, inserted_at) FORMAT JSONEachRow \n` +
51+
await rawRequest(
52+
`INSERT INTO ${DATABASE_NAME()}.settings${dist} (fingerprint, type, name, value, inserted_at) FORMAT JSONEachRow`,
5453
JSON.stringify({ fingerprint: ruleFp, type: 'alert_rule', name: ruleName, value: JSON.stringify(ruleVal), inserted_at: Date.now() * 1000000 }) + '\n' +
55-
JSON.stringify({ fingerprint: groupFp, type: 'alert_group', name: groupName, value: groupVal, inserted_at: Date.now() * 1000000 })
54+
JSON.stringify({ fingerprint: groupFp, type: 'alert_group', name: groupName, value: groupVal, inserted_at: Date.now() * 1000000 }),
55+
DATABASE_NAME()
5656
)
5757
}
5858

@@ -66,8 +66,9 @@ module.exports.putAlertRule = async (namespace, group, rule) => {
6666
module.exports.getLastCheck = async (ns, group, rule, id) => {
6767
const fp = getRuleFP(ns, group, rule)
6868
id = id || 0
69-
const resp = await axios.post(getClickhouseUrl(),
70-
`SELECT max(mark) as maxmark FROM ${DATABASE_NAME()}._alert_view_${fp}_mark WHERE id = ${id} FORMAT JSON`
69+
const resp = await rawRequest(
70+
`SELECT max(mark) as maxmark FROM ${DATABASE_NAME()}._alert_view_${fp}_mark WHERE id = ${id} FORMAT JSON`,
71+
null, DATABASE_NAME()
7172
)
7273
if (!resp.data.data[0]) {
7374
return 0
@@ -100,11 +101,12 @@ module.exports.getAlertRules = async (limit, offset) => {
100101
const _limit = limit ? `LIMIT ${limit}` : ''
101102
const _offset = offset ? `OFFSET ${offset}` : ''
102103
const mark = Math.random()
103-
const res = await axios.post(getClickhouseUrl(),
104+
const res = await rawRequest(
104105
'SELECT fingerprint, argMax(name, inserted_at) as name, argMax(value, inserted_at) as value ' +
105106
`FROM ${DATABASE_NAME()}.settings${dist} ` +
106107
`WHERE type == 'alert_rule' AND ${mark} == ${mark} ` +
107-
`GROUP BY fingerprint HAVING name != '' ORDER BY name ${_limit} ${_offset} FORMAT JSON`)
108+
`GROUP BY fingerprint HAVING name != '' ORDER BY name ${_limit} ${_offset} FORMAT JSON`,
109+
null, DATABASE_NAME())
108110
return res.data.data.map(e => {
109111
return { rule: JSON.parse(e.value), name: JSON.parse(e.name) }
110112
})

lib/db/maintain/index.js

+24-24
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ module.exports.rotate = async (opts) => {
110110
return upgradeRequest({ db: db.db, useDefaultDB: true }, req)
111111
}
112112
if (db.samples_days + '' !== settings.v3_samples_days) {
113-
const alterTable = 'ALTER TABLE samples_v3 {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
114-
const rotateTable = `ALTER TABLE samples_v3 {{{OnCluster}}} MODIFY TTL toDateTime(timestamp_ns / 1000000000) + INTERVAL ${db.samples_days} DAY`
113+
const alterTable = 'ALTER TABLE {{DB}}.samples_v3 {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
114+
const rotateTable = `ALTER TABLE {{DB}}.samples_v3 {{{OnCluster}}} MODIFY TTL toDateTime(timestamp_ns / 1000000000) + INTERVAL ${db.samples_days} DAY`
115115
await _update(alterTable, null, db.db)
116116
await _update(rotateTable, null, db.db)
117117
await client.addSetting('rotate', 'v3_samples_days', db.samples_days + '', db.db)
118118
}
119119
if (db.time_series_days + '' !== settings.v3_time_series_days) {
120-
const alterTable = 'ALTER TABLE time_series {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
121-
const rotateTable = `ALTER TABLE time_series {{{OnCluster}}} MODIFY TTL "date" + INTERVAL ${db.time_series_days} DAY`
120+
const alterTable = 'ALTER TABLE {{DB}}.time_series {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
121+
const rotateTable = `ALTER TABLE {{DB}}.time_series {{{OnCluster}}} MODIFY TTL "date" + INTERVAL ${db.time_series_days} DAY`
122122
await _update(alterTable, null, db.db)
123123
await _update(rotateTable, null, db.db)
124124
const alterView = 'ALTER TABLE time_series_gin {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
@@ -129,54 +129,54 @@ module.exports.rotate = async (opts) => {
129129
}
130130
if (db.storage_policy && db.storage_policy !== settings.v3_storage_policy) {
131131
logger.debug(`Altering storage policy: ${db.storage_policy}`)
132-
const alterTs = `ALTER TABLE time_series {{{OnCluster}}} MODIFY SETTING storage_policy='${db.storage_policy}'`
133-
const alterTsVw = `ALTER TABLE time_series_gin {{{OnCluster}}} MODIFY SETTING storage_policy='${db.storage_policy}'`
134-
const alterSm = `ALTER TABLE samples_v3 {{{OnCluster}}} MODIFY SETTING storage_policy='${db.storage_policy}'`
132+
const alterTs = `ALTER TABLE {{DB}}.time_series {{{OnCluster}}} MODIFY SETTING storage_policy='${db.storage_policy}'`
133+
const alterTsVw = `ALTER TABLE {{DB}}.time_series_gin {{{OnCluster}}} MODIFY SETTING storage_policy='${db.storage_policy}'`
134+
const alterSm = `ALTER TABLE {{DB}}.samples_v3 {{{OnCluster}}} MODIFY SETTING storage_policy='${db.storage_policy}'`
135135
await _update(alterTs, null, db.db)
136136
await _update(alterTsVw, null, db.db)
137137
await _update(alterSm, null, db.db)
138138
await client.addSetting('rotate', 'v3_storage_policy', db.storage_policy, db.db)
139139
}
140140
if (db.samples_days + '' !== settings.v1_traces_days) {
141-
let alterTable = 'ALTER TABLE tempo_traces {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
142-
let rotateTable = `ALTER TABLE tempo_traces {{{OnCluster}}} MODIFY TTL toDateTime(timestamp_ns / 1000000000) + INTERVAL ${db.samples_days} DAY`
141+
let alterTable = 'ALTER TABLE {{DB}}.tempo_traces {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
142+
let rotateTable = `ALTER TABLE {{DB}}.tempo_traces {{{OnCluster}}} MODIFY TTL toDateTime(timestamp_ns / 1000000000) + INTERVAL ${db.samples_days} DAY`
143143
await _update(alterTable, null, db.db)
144144
await _update(rotateTable, null, db.db)
145-
alterTable = 'ALTER TABLE tempo_traces_attrs_gin {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
146-
rotateTable = `ALTER TABLE tempo_traces_attrs_gin {{{OnCluster}}} MODIFY TTL date + INTERVAL ${db.samples_days} DAY`
145+
alterTable = 'ALTER TABLE {{DB}}.tempo_traces_attrs_gin {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
146+
rotateTable = `ALTER TABLE {{DB}}.tempo_traces_attrs_gin {{{OnCluster}}} MODIFY TTL date + INTERVAL ${db.samples_days} DAY`
147147
await _update(alterTable, null, db.db)
148148
await _update(rotateTable, null, db.db)
149-
alterTable = 'ALTER TABLE tempo_traces_kv {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
150-
rotateTable = `ALTER TABLE tempo_traces_kv {{{OnCluster}}} MODIFY TTL date + INTERVAL ${db.samples_days} DAY`
149+
alterTable = 'ALTER TABLE {{DB}}.tempo_traces_kv {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
150+
rotateTable = `ALTER TABLE {{DB}}.tempo_traces_kv {{{OnCluster}}} MODIFY TTL date + INTERVAL ${db.samples_days} DAY`
151151
await _update(alterTable, null, db.db)
152152
await _update(rotateTable, null, db.db)
153153
await client.addSetting('rotate', 'v1_traces_days', db.samples_days + '', db.db)
154154
}
155155
if (db.storage_policy && db.storage_policy !== settings.v1_traces_storage_policy) {
156156
logger.debug(`Altering storage policy: ${db.storage_policy}`)
157-
const alterTs = `ALTER TABLE tempo_traces MODIFY SETTING storage_policy='${db.storage_policy}'`
158-
const alterTsVw = `ALTER TABLE tempo_traces_attrs_gin MODIFY SETTING storage_policy='${db.storage_policy}'`
159-
const alterSm = `ALTER TABLE tempo_traces_kv MODIFY SETTING storage_policy='${db.storage_policy}'`
157+
const alterTs = `ALTER TABLE {{DB}}.tempo_traces MODIFY SETTING storage_policy='${db.storage_policy}'`
158+
const alterTsVw = `ALTER TABLE {{DB}}.tempo_traces_attrs_gin MODIFY SETTING storage_policy='${db.storage_policy}'`
159+
const alterSm = `ALTER TABLE {{DB}}.tempo_traces_kv MODIFY SETTING storage_policy='${db.storage_policy}'`
160160
await _update(alterTs, null, db.db)
161161
await _update(alterTsVw, null, db.db)
162162
await _update(alterSm, null, db.db)
163163
await client.addSetting('rotate', 'v1_traces_storage_policy', db.storage_policy, db.db)
164164
}
165165
if (db.samples_days + '' !== settings.v1_profiles_days) {
166-
let alterTable = 'ALTER TABLE profiles {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
167-
let rotateTable = `ALTER TABLE profiles {{{OnCluster}}} MODIFY TTL toDateTime(timestamp_ns / 1000000000) + INTERVAL ${db.samples_days} DAY`
166+
let alterTable = 'ALTER TABLE {{DB}}.profiles {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
167+
let rotateTable = `ALTER TABLE {{DB}}.profiles {{{OnCluster}}} MODIFY TTL toDateTime(timestamp_ns / 1000000000) + INTERVAL ${db.samples_days} DAY`
168168
await _update(alterTable, null, db.db)
169169
await _update(rotateTable, null, db.db)
170-
alterTable = 'ALTER TABLE profiles_series {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
171-
rotateTable = `ALTER TABLE profiles_series {{{OnCluster}}} MODIFY TTL date + INTERVAL ${db.samples_days} DAY`
170+
alterTable = 'ALTER TABLE {{DB}}.profiles_series {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
171+
rotateTable = `ALTER TABLE {{DB}}.profiles_series {{{OnCluster}}} MODIFY TTL date + INTERVAL ${db.samples_days} DAY`
172172
await _update(alterTable, null, db.db)
173173
await _update(rotateTable, null, db.db)
174-
alterTable = 'ALTER TABLE profiles_series_gin {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
175-
rotateTable = `ALTER TABLE profiles_series_gin {{{OnCluster}}} MODIFY TTL date + INTERVAL ${db.samples_days} DAY`
174+
alterTable = 'ALTER TABLE {{DB}}.profiles_series_gin {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
175+
rotateTable = `ALTER TABLE {{DB}}.profiles_series_gin {{{OnCluster}}} MODIFY TTL date + INTERVAL ${db.samples_days} DAY`
176176
await _update(alterTable, null, db.db)
177177
await _update(rotateTable, null, db.db)
178-
alterTable = 'ALTER TABLE profiles_series_keys {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
179-
rotateTable = `ALTER TABLE profiles_series_keys {{{OnCluster}}} MODIFY TTL date + INTERVAL ${db.samples_days} DAY`
178+
alterTable = 'ALTER TABLE {{DB}}.profiles_series_keys {{{OnCluster}}} MODIFY SETTING ttl_only_drop_parts = 1, merge_with_ttl_timeout = 3600, index_granularity = 8192'
179+
rotateTable = `ALTER TABLE {{DB}}.profiles_series_keys {{{OnCluster}}} MODIFY TTL date + INTERVAL ${db.samples_days} DAY`
180180
await _update(alterTable, null, db.db)
181181
await _update(rotateTable, null, db.db)
182182
await client.addSetting('rotate', 'v1_profiles_days', db.samples_days + '', db.db)

0 commit comments

Comments
 (0)