Skip to content

Commit

Permalink
fix: create table with predefined storage_policy
Browse files Browse the repository at this point in the history
  • Loading branch information
akvlad committed Dec 4, 2023
1 parent c0ef480 commit 1e5825b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
1 change: 0 additions & 1 deletion lib/db/clickhouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,6 @@ const getSettings = async (names, database) => {
argMax(value, inserted_at) as _value
FROM settings${dist} WHERE fingerprint IN (${fps.join(',')}) GROUP BY fingerprint HAVING _name != '' FORMAT JSON`,
null, database)
console.log(settings.data);
return settings.data.data.reduce((sum, cur) => {
sum[cur._name] = cur._value
return sum
Expand Down
7 changes: 3 additions & 4 deletions lib/db/maintain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports.upgrade = async (db) => {
await upgradeSingle(db.name, 2, scripts.traces, db.storage_policy)
if (db.storage_policy) {
await client.addSetting('rotate', 'v3_storage_policy', db.storage_policy, db.name)
await client.addSetting('rotate', 'v1_traces_storage_policy', db.storage_policy, db.name)
}
if (clusterName) {
await upgradeSingle(db.name, 3, scripts.overall_dist, db.storage_policy)
Expand All @@ -46,12 +47,12 @@ const upgradeSingle = async (db, key, scripts, storagePolicy) => {
await _upgradeRequest('CREATE DATABASE IF NOT EXISTS {{DB}} {{{OnCluster}}}')
if (clusterName) {
await _upgradeRequest('CREATE TABLE IF NOT EXISTS {{DB}}._ver {{{OnCluster}}} (k UInt64, ver UInt64) ' +
'ENGINE={{ReplacingMergeTree}}(ver) ORDER BY k {{CREATE_SETTINGS}}', true)
'ENGINE={{ReplacingMergeTree}}(ver) ORDER BY k {{{CREATE_SETTINGS}}}', true)
await _upgradeRequest('CREATE TABLE IF NOT EXISTS {{DB}}.ver {{{OnCluster}}} (k UInt64, ver UInt64) ' +
'ENGINE=Distributed(\'{{CLUSTER}}\',\'{{DB}}\', \'_ver\', rand())', true)
} else {
await _upgradeRequest('CREATE TABLE IF NOT EXISTS {{DB}}.ver {{{OnCluster}}} (k UInt64, ver UInt64) ' +
'ENGINE={{ReplacingMergeTree}}(ver) ORDER BY k {{CREATE_SETTINGS}}', true)
'ENGINE={{ReplacingMergeTree}}(ver) ORDER BY k {{{CREATE_SETTINGS}}}', true)
}
}
let ver = await client.rawRequest(`SELECT max(ver) as ver FROM ver WHERE k = ${key} FORMAT JSON`,
Expand Down Expand Up @@ -82,10 +83,8 @@ const upgradeRequest = async (opts, request) => {
AggregatingMergeTree: clusterName ? 'ReplicatedAggregatingMergeTree' : 'AggregatingMergeTree',
CREATE_SETTINGS: opts.storage_policy ? `SETTINGS storage_policy='${opts.storage_policy}'` : ''
})
console.log(request)
await client.rawRequest(request, null, opts.useDefaultDB ? opts.db : undefined)
if (opts.updateVer) {
console.log(`INSERT INTO ver (k, ver) VALUES (${opts.updateVer.key}, ${opts.updateVer.to})`)
await client.rawRequest(`INSERT INTO ver (k, ver) VALUES (${opts.updateVer.key}, ${opts.updateVer.to})`,
null, opts.db)
}
Expand Down
16 changes: 8 additions & 8 deletions lib/db/maintain/scripts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports.overall = [
`CREATE TABLE IF NOT EXISTS {{DB}}.time_series {{{OnCluster}}} (date Date,fingerprint UInt64,labels String, name String)
ENGINE = {{ReplacingMergeTree}}(date) PARTITION BY date ORDER BY fingerprint {{CREATE_SETTINGS}}`,
ENGINE = {{ReplacingMergeTree}}(date) PARTITION BY date ORDER BY fingerprint {{{CREATE_SETTINGS}}}`,

`CREATE TABLE IF NOT EXISTS {{DB}}.samples_v3 {{{OnCluster}}}
(
Expand All @@ -10,11 +10,11 @@ module.exports.overall = [
string String
) ENGINE = {{MergeTree}}
PARTITION BY toStartOfDay(toDateTime(timestamp_ns / 1000000000))
ORDER BY ({{SAMPLES_ORDER_RUL}}) {{CREATE_SETTINGS}}`,
ORDER BY ({{SAMPLES_ORDER_RUL}}) {{{CREATE_SETTINGS}}}`,

`CREATE TABLE IF NOT EXISTS {{DB}}.settings {{{OnCluster}}}
(fingerprint UInt64, type String, name String, value String, inserted_at DateTime64(9, 'UTC'))
ENGINE = {{ReplacingMergeTree}}(inserted_at) ORDER BY fingerprint {{CREATE_SETTINGS}}`,
ENGINE = {{ReplacingMergeTree}}(inserted_at) ORDER BY fingerprint {{{CREATE_SETTINGS}}}`,

'DROP TABLE IF EXISTS {{DB}}.samples_read {{{OnCluster}}}',

Expand All @@ -34,7 +34,7 @@ module.exports.overall = [
key String,
val String,
fingerprint UInt64
) ENGINE = {{ReplacingMergeTree}}() PARTITION BY date ORDER BY (key, val, fingerprint) {{CREATE_SETTINGS}}`,
) ENGINE = {{ReplacingMergeTree}}() PARTITION BY date ORDER BY (key, val, fingerprint) {{{CREATE_SETTINGS}}}`,

`CREATE MATERIALIZED VIEW IF NOT EXISTS {{DB}}.time_series_gin_view {{{OnCluster}}} TO time_series_gin
AS SELECT date, pairs.1 as key, pairs.2 as val, fingerprint
Expand All @@ -54,7 +54,7 @@ module.exports.overall = [
bytes SimpleAggregateFunction(sum, Float64)
) ENGINE = {{AggregatingMergeTree}}
PARTITION BY toDate(toDateTime(intDiv(timestamp_ns, 1000000000)))
ORDER BY (fingerprint, timestamp_ns) {{CREATE_SETTINGS}};`,
ORDER BY (fingerprint, timestamp_ns) {{{CREATE_SETTINGS}}};`,

`CREATE MATERIALIZED VIEW IF NOT EXISTS {{DB}}.metrics_15s_mv {{{OnCluster}}} TO metrics_15s AS
SELECT fingerprint,
Expand Down Expand Up @@ -85,7 +85,7 @@ module.exports.traces = [
payload_type Int8,
payload String
) Engine = {{MergeTree}}() ORDER BY (oid, trace_id, timestamp_ns)
PARTITION BY (oid, toDate(FROM_UNIXTIME(intDiv(timestamp_ns, 1000000000)))) {{CREATE_SETTINGS}};`,
PARTITION BY (oid, toDate(FROM_UNIXTIME(intDiv(timestamp_ns, 1000000000)))) {{{CREATE_SETTINGS}}};`,

`CREATE TABLE IF NOT EXISTS {{DB}}.tempo_traces_attrs_gin {{{OnCluster}}} (
oid String,
Expand All @@ -98,7 +98,7 @@ module.exports.traces = [
duration Int64
) Engine = {{ReplacingMergeTree}}()
PARTITION BY date
ORDER BY (oid, date, key, val, timestamp_ns, trace_id, span_id) {{CREATE_SETTINGS}};`,
ORDER BY (oid, date, key, val, timestamp_ns, trace_id, span_id) {{{CREATE_SETTINGS}}};`,

`CREATE TABLE IF NOT EXISTS {{DB}}.tempo_traces_kv {{{OnCluster}}} (
oid String,
Expand All @@ -108,7 +108,7 @@ module.exports.traces = [
val String
) Engine = {{ReplacingMergeTree}}()
PARTITION BY (oid, date)
ORDER BY (oid, date, key, val_id) {{CREATE_SETTINGS}}`,
ORDER BY (oid, date, key, val_id) {{{CREATE_SETTINGS}}}`,

`CREATE MATERIALIZED VIEW IF NOT EXISTS {{DB}}.tempo_traces_kv_mv {{{OnCluster}}} TO tempo_traces_kv AS
SELECT oid, date, key, cityHash64(val) % 10000 as val_id, val FROM tempo_traces_attrs_gin`,
Expand Down

0 comments on commit 1e5825b

Please sign in to comment.