-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhypercore.ts
More file actions
581 lines (519 loc) · 17.8 KB
/
Copy pathhypercore.ts
File metadata and controls
581 lines (519 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
import type { Db } from "./db/db.ts";
import { sql } from "drizzle-orm";
import {
createErrorString,
createFail,
createSuccess,
isSuccess,
type Result,
} from "@joyautomation/dark-matter";
import { log } from "./log.ts";
import type { getBuilder } from "@joyautomation/conch";
import { GraphQLError } from "graphql";
const historianEnabled = Deno.env.get("MANTLE_HISTORIAN_ENABLED") !== "false";
const retentionDays = parseInt(
Deno.env.get("MANTLE_RETENTION_DAYS") || "30",
10,
);
// Types for storage stats
type TableStorageStats = {
tableName: string;
totalBytes: number;
compressedBytes: number | null;
uncompressedBytes: number | null;
compressionRatio: number | null;
};
type StorageStats = {
hypercoreAvailable: boolean;
compressionEnabled: boolean;
tables: TableStorageStats[];
totalStorageBytes: number;
totalCompressedBytes: number | null;
totalUncompressedBytes: number | null;
overallCompressionRatio: number | null;
};
type CompressionStatus = {
tableName: string;
compressionEnabled: boolean;
policyExists: boolean;
};
/**
* Check if TimescaleDB compression (hypercore) is available
*/
export async function isHypercoreAvailable(db: Db): Promise<Result<boolean>> {
try {
// Check if timescaledb extension is installed and has compression support
const result = await db.execute(sql`
SELECT EXISTS (
SELECT 1 FROM pg_extension WHERE extname = 'timescaledb'
) as has_timescaledb
`);
if (!result.rows[0]?.has_timescaledb) {
return createSuccess(false);
}
// Check if compression is available (it's part of TimescaleDB)
// Try to query compression-related catalog
try {
await db.execute(sql`
SELECT 1 FROM timescaledb_information.compression_settings LIMIT 0
`);
return createSuccess(true);
} catch {
// Compression catalog doesn't exist - older version without compression
return createSuccess(false);
}
} catch (error) {
return createFail(createErrorString(error));
}
}
/**
* Check compression status for a specific table
*/
export async function getCompressionStatus(
db: Db,
tableName: string,
): Promise<Result<CompressionStatus>> {
try {
// Check if compression is enabled on the hypertable
const compressionResult = await db.execute(sql.raw(`
SELECT compression_enabled
FROM timescaledb_information.hypertables
WHERE hypertable_name = '${tableName}'
`));
const compressionEnabled = compressionResult.rows[0]?.compression_enabled === true;
// Check if compression policy exists
const policyResult = await db.execute(sql.raw(`
SELECT 1
FROM timescaledb_information.jobs j
JOIN timescaledb_information.job_stats js ON j.job_id = js.job_id
WHERE j.proc_name = 'policy_compression'
AND j.hypertable_name = '${tableName}'
LIMIT 1
`));
const policyExists = (policyResult.rows?.length ?? 0) > 0;
return createSuccess({
tableName,
compressionEnabled,
policyExists,
});
} catch (error) {
return createFail(createErrorString(error));
}
}
/**
* Enable compression on the history table
*/
export async function enableHistoryCompression(db: Db): Promise<Result<void>> {
try {
log.info("Enabling compression on history table...");
await db.execute(sql`
ALTER TABLE history SET (
timescaledb.compress,
timescaledb.compress_segmentby = 'group_id, node_id, device_id, metric_id',
timescaledb.compress_orderby = 'timestamp DESC NULLS FIRST'
)
`);
log.info("Compression enabled on history table");
return createSuccess(undefined);
} catch (error) {
return createFail(createErrorString(error));
}
}
/**
* Add compression policy for history table (compress chunks older than 1 hour)
*/
export async function addHistoryCompressionPolicy(db: Db): Promise<Result<void>> {
try {
log.info("Adding compression policy for history table...");
await db.execute(sql`
SELECT add_compression_policy('history', INTERVAL '1 hour')
`);
log.info("Compression policy added for history table");
return createSuccess(undefined);
} catch (error) {
// Policy might already exist
const errorStr = createErrorString(error);
if (errorStr.includes("already exists")) {
log.info("Compression policy already exists for history table");
return createSuccess(undefined);
}
return createFail(errorStr);
}
}
/**
* Compress all eligible chunks for a hypertable.
* Compresses any uncompressed chunk whose time range has fully elapsed
* (range_end is in the past), plus the configured grace period.
*/
export async function compressEligibleChunks(
db: Db,
tableName: string,
olderThan: string = "1 hour",
): Promise<Result<number>> {
try {
const result = await db.execute(sql.raw(`
SELECT chunk_schema, chunk_name
FROM timescaledb_information.chunks
WHERE hypertable_name = '${tableName}'
AND NOT is_compressed
AND range_end < NOW() - INTERVAL '${olderThan}'
ORDER BY range_start
`));
let compressed = 0;
for (const row of result.rows) {
const chunkFqn = `${row.chunk_schema}.${row.chunk_name}`;
try {
await db.execute(sql.raw(`SELECT compress_chunk('${chunkFqn}')`));
compressed++;
log.info(`Compressed chunk ${chunkFqn}`);
} catch (error) {
log.warn(`Failed to compress chunk ${chunkFqn}: ${createErrorString(error)}`);
}
}
return createSuccess(compressed);
} catch (error) {
return createFail(createErrorString(error));
}
}
/**
* Initialize hypercore compression on all tables if available
* This should be called on startup after migrations
*/
export async function initializeHypercore(db: Db): Promise<Result<void>> {
log.info("Checking hypercore (TimescaleDB compression) availability...");
const availableResult = await isHypercoreAvailable(db);
if (!isSuccess(availableResult)) {
log.warn(`Could not check hypercore availability: ${availableResult.error}`);
return createFail(availableResult.error);
}
if (!availableResult.output) {
log.info("Hypercore (TimescaleDB compression) is not available");
return createSuccess(undefined);
}
log.info("Hypercore is available, checking compression status...");
// Check and enable compression on history table
const historyStatus = await getCompressionStatus(db, "history");
if (isSuccess(historyStatus)) {
if (!historyStatus.output.compressionEnabled) {
const enableResult = await enableHistoryCompression(db);
if (!isSuccess(enableResult)) {
log.error(`Failed to enable compression on history: ${enableResult.error}`);
}
} else {
log.info("Compression already enabled on history table");
}
if (!historyStatus.output.policyExists) {
const policyResult = await addHistoryCompressionPolicy(db);
if (!isSuccess(policyResult)) {
log.error(`Failed to add compression policy for history: ${policyResult.error}`);
}
} else {
log.info("Compression policy already exists for history table");
}
}
// Directly compress any eligible chunks on startup.
// The bgw scheduler policies are unreliable, so this ensures
// compression actually happens on every restart/deploy.
log.info("Compressing eligible chunks...");
const historyCompressed = await compressEligibleChunks(db, "history", "1 hour");
if (isSuccess(historyCompressed)) {
log.info(`Compressed ${historyCompressed.output} history chunk(s)`);
}
log.info("Hypercore initialization complete");
return createSuccess(undefined);
}
/**
* Get the current retention policy for a hypertable, if any.
*/
export async function getRetentionPolicy(
db: Db,
tableName: string,
): Promise<Result<{ exists: boolean; intervalDays: number | null }>> {
try {
const result = await db.execute(sql.raw(`
SELECT config->>'drop_after' as drop_after
FROM timescaledb_information.jobs
WHERE proc_name = 'policy_retention'
AND hypertable_name = '${tableName}'
LIMIT 1
`));
if (result.rows.length > 0) {
const dropAfter = String(result.rows[0].drop_after);
const daysMatch = dropAfter.match(/(\d+)\s*days?/i);
return createSuccess({
exists: true,
intervalDays: daysMatch ? parseInt(daysMatch[1], 10) : null,
});
}
return createSuccess({ exists: false, intervalDays: null });
} catch (error) {
return createFail(createErrorString(error));
}
}
/**
* Set the retention policy for a hypertable.
* Removes any existing policy first, then adds one with the specified interval.
*/
export async function setRetentionPolicy(
db: Db,
tableName: string,
days: number,
): Promise<Result<void>> {
try {
// Remove existing policy if any
try {
await db.execute(
sql.raw(`SELECT remove_retention_policy('${tableName}')`),
);
log.info(`Removed existing retention policy for ${tableName}`);
} catch {
// No existing policy — that's fine
}
// Add new retention policy
await db.execute(
sql.raw(
`SELECT add_retention_policy('${tableName}', INTERVAL '${days} days')`,
),
);
log.info(
`Added retention policy for ${tableName}: drop data older than ${days} days`,
);
return createSuccess(undefined);
} catch (error) {
return createFail(createErrorString(error));
}
}
/**
* Initialize retention policy on the history table.
* Should be called on startup regardless of historian status.
*/
export async function initializeRetention(db: Db): Promise<Result<void>> {
log.info(`Checking retention policy (configured: ${retentionDays} days)...`);
const availableResult = await isHypercoreAvailable(db);
if (!isSuccess(availableResult)) {
log.warn(
`Could not check TimescaleDB availability: ${availableResult.error}`,
);
return createFail(availableResult.error);
}
if (!availableResult.output) {
log.info("TimescaleDB not available, skipping retention policy");
return createSuccess(undefined);
}
const currentPolicy = await getRetentionPolicy(db, "history");
if (!isSuccess(currentPolicy)) {
log.error(`Failed to get retention policy: ${currentPolicy.error}`);
return createFail(currentPolicy.error);
}
const needsUpdate =
!currentPolicy.output.exists ||
currentPolicy.output.intervalDays !== retentionDays;
if (needsUpdate) {
const result = await setRetentionPolicy(db, "history", retentionDays);
if (!isSuccess(result)) {
log.error(`Failed to set retention policy: ${result.error}`);
return createFail(result.error);
}
} else {
log.info(
`Retention policy already set to ${retentionDays} days, no change needed`,
);
}
return createSuccess(undefined);
}
/**
* Get storage statistics including compression info
*/
export async function getStorageStats(db: Db): Promise<Result<StorageStats>> {
try {
const availableResult = await isHypercoreAvailable(db);
const hypercoreAvailable = isSuccess(availableResult) && availableResult.output;
const tables: TableStorageStats[] = [];
let compressionEnabled = false;
if (hypercoreAvailable) {
// Get hypertable info and sizes using TimescaleDB functions
const hypertablesResult = await db.execute(sql`
SELECT
hypertable_name as table_name,
compression_enabled
FROM timescaledb_information.hypertables
WHERE hypertable_name IN ('history', 'history_properties')
`);
for (const row of hypertablesResult.rows) {
const tableName = String(row.table_name);
// Get total size using hypertable_size function
const sizeResult = await db.execute(sql.raw(`
SELECT hypertable_size('${tableName}') as total_bytes
`));
const totalBytes = Number(sizeResult.rows[0]?.total_bytes) || 0;
// Get compression stats using hypertable_compression_stats function
let compressedBytes: number | null = null;
let uncompressedBytes: number | null = null;
let compressionRatio: number | null = null;
if (row.compression_enabled) {
compressionEnabled = true;
const compressionResult = await db.execute(sql.raw(`
SELECT
COALESCE(SUM(after_compression_total_bytes), 0) as compressed_bytes,
COALESCE(SUM(before_compression_total_bytes), 0) as uncompressed_bytes
FROM hypertable_compression_stats('${tableName}')
`));
if (compressionResult.rows[0]) {
compressedBytes = Number(compressionResult.rows[0].compressed_bytes) || 0;
uncompressedBytes = Number(compressionResult.rows[0].uncompressed_bytes) || 0;
if (compressedBytes > 0 && uncompressedBytes > 0) {
compressionRatio = uncompressedBytes / compressedBytes;
}
}
}
tables.push({
tableName,
totalBytes,
compressedBytes,
uncompressedBytes,
compressionRatio,
});
}
} else {
// Fall back to basic pg_total_relation_size for non-TimescaleDB
const sizeResult = await db.execute(sql`
SELECT
'history' as table_name,
pg_total_relation_size('history') as total_bytes
UNION ALL
SELECT
'history_properties' as table_name,
pg_total_relation_size('history_properties') as total_bytes
`);
for (const row of sizeResult.rows) {
tables.push({
tableName: String(row.table_name),
totalBytes: Number(row.total_bytes) || 0,
compressedBytes: null,
uncompressedBytes: null,
compressionRatio: null,
});
}
}
// Calculate totals
const totalStorageBytes = tables.reduce((sum, t) => sum + t.totalBytes, 0);
const totalCompressedBytes = tables.every(t => t.compressedBytes !== null)
? tables.reduce((sum, t) => sum + (t.compressedBytes ?? 0), 0)
: null;
const totalUncompressedBytes = tables.every(t => t.uncompressedBytes !== null)
? tables.reduce((sum, t) => sum + (t.uncompressedBytes ?? 0), 0)
: null;
let overallCompressionRatio: number | null = null;
if (totalCompressedBytes !== null && totalUncompressedBytes !== null && totalCompressedBytes > 0) {
overallCompressionRatio = totalUncompressedBytes / totalCompressedBytes;
}
return createSuccess({
hypercoreAvailable,
compressionEnabled,
tables,
totalStorageBytes,
totalCompressedBytes,
totalUncompressedBytes,
overallCompressionRatio,
});
} catch (error) {
return createFail(createErrorString(error));
}
}
/**
* Add hypercore/storage GraphQL schema
*/
export function addHypercoreToSchema(
builder: ReturnType<typeof getBuilder>,
db: Db,
) {
// Table storage stats type
const TableStorageStatsRef = builder.objectRef<TableStorageStats>("TableStorageStats");
TableStorageStatsRef.implement({
fields: (t) => ({
tableName: t.exposeString("tableName"),
totalBytes: t.exposeFloat("totalBytes"),
compressedBytes: t.exposeFloat("compressedBytes", { nullable: true }),
uncompressedBytes: t.exposeFloat("uncompressedBytes", { nullable: true }),
compressionRatio: t.exposeFloat("compressionRatio", { nullable: true }),
}),
});
// Storage stats type
const StorageStatsRef = builder.objectRef<StorageStats>("StorageStats");
StorageStatsRef.implement({
fields: (t) => ({
hypercoreAvailable: t.exposeBoolean("hypercoreAvailable"),
compressionEnabled: t.exposeBoolean("compressionEnabled"),
tables: t.field({
type: [TableStorageStatsRef],
resolve: (parent) => parent.tables,
}),
totalStorageBytes: t.exposeFloat("totalStorageBytes"),
totalCompressedBytes: t.exposeFloat("totalCompressedBytes", { nullable: true }),
totalUncompressedBytes: t.exposeFloat("totalUncompressedBytes", { nullable: true }),
overallCompressionRatio: t.exposeFloat("overallCompressionRatio", { nullable: true }),
}),
});
// Query: hypercoreAvailable
builder.queryField("hypercoreAvailable", (t) =>
t.field({
type: "Boolean",
description: "Check if TimescaleDB compression (hypercore) is available",
resolve: async () => {
const result = await isHypercoreAvailable(db);
if (isSuccess(result)) {
return result.output;
} else {
throw new GraphQLError(result.error);
}
},
})
);
// Query: storageStats
builder.queryField("storageStats", (t) =>
t.field({
type: StorageStatsRef,
description: "Get storage statistics including compression info",
resolve: async () => {
if (!historianEnabled) {
throw new GraphQLError(
"Historian is not enabled for this space.",
);
}
const result = await getStorageStats(db);
if (isSuccess(result)) {
return result.output;
} else {
throw new GraphQLError(result.error);
}
},
})
);
// Retention policy type
const RetentionPolicyRef = builder.objectRef<{
exists: boolean;
intervalDays: number | null;
configuredDays: number;
}>("RetentionPolicy");
RetentionPolicyRef.implement({
fields: (t) => ({
exists: t.exposeBoolean("exists"),
intervalDays: t.exposeInt("intervalDays", { nullable: true }),
configuredDays: t.exposeInt("configuredDays"),
}),
});
// Query: retentionPolicy
builder.queryField("retentionPolicy", (t) =>
t.field({
type: RetentionPolicyRef,
description: "Get the current data retention policy configuration",
resolve: async () => {
const result = await getRetentionPolicy(db, "history");
if (isSuccess(result)) {
return { ...result.output, configuredDays: retentionDays };
}
throw new GraphQLError(result.error);
},
})
);
}