-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhepop.js
1227 lines (1048 loc) · 37 KB
/
hepop.js
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import parquet from '@dsnp/parquetjs';
import { DuckDBInstance } from '@duckdb/node-api';
import hepjs from 'hep-js';
import { getSIP } from 'parsip';
import path from 'path';
import fs from 'fs';
import duckdb from '@duckdb/node-api';
import QueryClient from './query.js';
import { parse } from './lineproto.js';
class ParquetBufferManager {
constructor(flushInterval = 10000, bufferSize = 1000) {
this.buffers = new Map();
this.flushInterval = flushInterval;
this.bufferSize = bufferSize;
this.baseDir = process.env.PARQUET_DIR || './data';
this.writerId = process.env.WRITER_ID || require('os').hostname();
// Define schema for HEP data
this.schema = new parquet.ParquetSchema({
timestamp: { type: 'TIMESTAMP_MILLIS' },
rcinfo: { type: 'UTF8' },
payload: { type: 'UTF8' }
});
// Add bloom filters for better query performance
this.writerOptions = {
bloomFilters: [
{
column: 'timestamp',
numFilterBytes: 1024
}
]
};
// Add schema for Line Protocol data
this.lpSchema = new parquet.ParquetSchema({
timestamp: { type: 'TIMESTAMP_MILLIS' },
tags: { type: 'UTF8' }, // JSON string of tags
// Dynamic fields will be added based on data
});
}
async initialize() {
// Ensure base directories exist
await this.ensureDirectories();
// Load or create global metadata
await this.initializeMetadata();
// Start flush interval after initialization
this.startFlushInterval();
}
async initializeMetadata() {
const metadataPath = path.join(this.baseDir, this.writerId, 'metadata.json');
try {
const data = await fs.promises.readFile(metadataPath, 'utf8');
this.metadata = JSON.parse(data);
} catch (error) {
if (error.code === 'ENOENT') {
this.metadata = {
writer_id: this.writerId,
next_db_id: 0,
next_table_id: 0
};
await this.writeGlobalMetadata();
} else {
throw error;
}
}
}
async writeGlobalMetadata() {
const metadataPath = path.join(this.baseDir, this.writerId, 'metadata.json');
const tempPath = `${metadataPath}.tmp`;
try {
await fs.promises.writeFile(tempPath, JSON.stringify(this.metadata, null, 2));
await fs.promises.rename(tempPath, metadataPath);
} catch (error) {
try {
await fs.promises.unlink(tempPath);
} catch (e) {
// Ignore cleanup errors
}
throw error;
}
}
async getTypeMetadata(type) {
const metadataPath = this.getTypeMetadataPath(type);
try {
const data = await fs.promises.readFile(metadataPath, 'utf8');
return JSON.parse(data);
} catch (error) {
if (error.code === 'ENOENT') {
// Initialize new type metadata
const metadata = {
type,
parquet_size_bytes: 0,
row_count: 0,
min_time: null,
max_time: null,
wal_sequence: 0,
files: [] // Array of file entries
};
await this.writeTypeMetadata(type, metadata);
return metadata;
}
throw error;
}
}
async getFilePath(type, timestamp) {
const typeMetadata = await this.getTypeMetadata(type);
const date = new Date(timestamp);
const datePath = date.toISOString().split('T')[0];
const hour = date.getHours().toString().padStart(2, '0');
const minute = Math.floor(date.getMinutes() / 10) * 10;
const minutePath = minute.toString().padStart(2, '0');
return path.join(
this.baseDir,
this.writerId,
'dbs',
`hep-${this.metadata.next_db_id}`,
`hep_${type}-${this.metadata.next_table_id}`,
datePath,
`${hour}-${minutePath}`,
`${typeMetadata.wal_sequence.toString().padStart(10, '0')}.parquet`
);
}
add(type, data) {
if (!this.buffers.has(type)) {
this.buffers.set(type, {
rows: [],
schema: this.schema, // Use HEP schema
isLineProtocol: false // Mark as HEP data
});
}
const buffer = this.buffers.get(type);
buffer.rows.push(data);
if (buffer.rows.length >= this.bufferSize) {
this.flush(type);
}
}
startFlushInterval() {
setInterval(() => {
for (const type of this.buffers.keys()) {
this.flush(type);
}
}, this.flushInterval);
}
async flush(type) {
const buffer = this.buffers.get(type);
if (!buffer?.rows.length) return;
try {
const filePath = await this.getFilePath(type, buffer.isLineProtocol ?
buffer.rows[0].timestamp : buffer.rows[0].create_date);
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
// Create writer with appropriate schema
const writer = await parquet.ParquetWriter.openFile(
buffer.schema,
filePath,
this.writerOptions
);
// Write rows based on type
for (const data of buffer.rows) {
if (buffer.isLineProtocol) {
await writer.appendRow(data);
} else {
await writer.appendRow({
timestamp: new Date(data.create_date),
rcinfo: JSON.stringify(data.protocol_header),
payload: data.raw || ''
});
}
}
await writer.close();
// Get file stats
const stats = await fs.promises.stat(filePath);
// Update metadata
await this.updateMetadata(
type,
filePath,
stats.size,
buffer.rows.length,
buffer.rows.map(d => buffer.isLineProtocol ?
d.timestamp : new Date(d.create_date))
);
// Clear buffer
this.buffers.set(type, {
rows: [],
schema: buffer.schema,
isLineProtocol: buffer.isLineProtocol
});
console.log(`Wrote ${buffer.rows.length} records to ${filePath}`);
} catch (error) {
console.error(`Parquet flush error:`, error);
}
}
getTypeMetadataPath(type) {
return path.join(
this.baseDir,
this.writerId,
'dbs',
`hep-${this.metadata.next_db_id}`,
`hep_${type}-${this.metadata.next_table_id}`,
'metadata.json'
);
}
async writeTypeMetadata(type, metadata) {
const metadataPath = this.getTypeMetadataPath(type);
await fs.promises.mkdir(path.dirname(metadataPath), { recursive: true });
const tempPath = `${metadataPath}.tmp`;
try {
await fs.promises.writeFile(tempPath, JSON.stringify(metadata, null, 2));
await fs.promises.rename(tempPath, metadataPath);
} catch (error) {
try {
await fs.promises.unlink(tempPath);
} catch (e) {
// Ignore cleanup errors
}
throw error;
}
}
async updateMetadata(type, filePath, sizeBytes, rowCount, timestamps) {
const minTime = Math.min(...timestamps.map(t => t.getTime() * 1000000));
const maxTime = Math.max(...timestamps.map(t => t.getTime() * 1000000));
const chunkTime = Math.floor(minTime / 600000000000) * 600000000000;
// Get current type metadata
const typeMetadata = await this.getTypeMetadata(type);
const fileInfo = {
id: typeMetadata.files.length,
path: filePath,
size_bytes: sizeBytes,
row_count: rowCount,
chunk_time: chunkTime,
min_time: minTime,
max_time: maxTime,
type: 'raw'
};
// Update type metadata
typeMetadata.files.push(fileInfo);
typeMetadata.parquet_size_bytes += sizeBytes;
typeMetadata.row_count += rowCount;
typeMetadata.min_time = typeMetadata.min_time ?
Math.min(typeMetadata.min_time, minTime) : minTime;
typeMetadata.max_time = typeMetadata.max_time ?
Math.max(typeMetadata.max_time, maxTime) : maxTime;
typeMetadata.wal_sequence++;
// Write updated metadata
await this.writeTypeMetadata(type, typeMetadata);
}
async close() {
for (const type of this.buffers.keys()) {
await this.flush(type);
}
}
async ensureDirectories() {
const metadataDir = path.join(this.baseDir, this.writerId);
await fs.promises.mkdir(metadataDir, { recursive: true });
// Write initial metadata file if it doesn't exist
const metadataPath = path.join(metadataDir, 'metadata.json');
if (!fs.existsSync(metadataPath)) {
const initialMetadata = {
writer_id: this.writerId,
next_db_id: 0,
next_table_id: 0
};
await fs.promises.writeFile(
metadataPath,
JSON.stringify(initialMetadata, null, 2)
);
}
}
async addLineProtocol(data) {
const measurement = data.measurement;
if (!this.buffers.has(measurement)) {
// Create new schema for this measurement including its fields
const schema = new parquet.ParquetSchema({
timestamp: { type: 'TIMESTAMP_MILLIS' },
tags: { type: 'UTF8' },
...Object.entries(data.fields).reduce((acc, [key, value]) => {
acc[key] = {
type: typeof value === 'number' ? 'DOUBLE' :
typeof value === 'boolean' ? 'BOOLEAN' : 'UTF8'
};
return acc;
}, {})
});
this.buffers.set(measurement, {
rows: [],
schema
});
}
const buffer = this.buffers.get(measurement);
buffer.rows.push({
timestamp: new Date(data.timestamp),
tags: JSON.stringify(data.tags),
...data.fields
});
if (buffer.rows.length >= this.bufferSize) {
await this.flushLineProtocol(measurement);
}
}
async flushLineProtocol(measurement) {
const buffer = this.buffers.get(measurement);
if (!buffer?.rows.length) return;
try {
const filePath = await this.getFilePath(measurement, buffer.rows[0].timestamp);
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
// Create writer with measurement's schema
const writer = await parquet.ParquetWriter.openFile(
buffer.schema,
filePath,
this.writerOptions
);
// Write rows
for (const row of buffer.rows) {
await writer.appendRow(row);
}
await writer.close();
// Update metadata similar to HEP data
const stats = await fs.promises.stat(filePath);
await this.updateMetadata(
measurement,
filePath,
stats.size,
buffer.rows.length,
buffer.rows.map(r => r.timestamp)
);
buffer.rows = [];
} catch (error) {
console.error(`Line Protocol flush error:`, error);
}
}
async addLineProtocolBulk(measurement, rows) {
// Use measurement directly as type (like HEP types)
const type = measurement;
if (!this.buffers.has(type)) {
// Create new schema for this measurement including its fields
const schema = new parquet.ParquetSchema({
timestamp: { type: 'TIMESTAMP_MILLIS' },
tags: { type: 'UTF8' },
...Object.entries(rows[0]).reduce((acc, [key, value]) => {
if (key !== 'timestamp' && key !== 'tags') {
acc[key] = {
type: typeof value === 'number' ? 'DOUBLE' :
typeof value === 'boolean' ? 'BOOLEAN' : 'UTF8'
};
}
return acc;
}, {})
});
this.buffers.set(type, {
rows: [],
schema,
isLineProtocol: true // Mark as Line Protocol data
});
}
const buffer = this.buffers.get(type);
buffer.rows.push(...rows);
if (buffer.rows.length >= this.bufferSize) {
await this.flush(type); // Use the same flush method as HEP
}
}
}
class CompactionManager {
constructor(bufferManager) {
this.bufferManager = bufferManager;
this.compactionIntervals = {
'10m': 10 * 60 * 1000,
'1h': 60 * 60 * 1000,
'24h': 24 * 60 * 60 * 1000
};
this.compactionLock = new Map();
}
async initialize() {
try {
// Initialize DuckDB
this.db = await DuckDBInstance.create(':memory:');
console.log(`Initialized DuckDB for parquet compaction`);
// Run initial compaction check
await this.checkAndCompact();
// Start compaction jobs after initialization
this.startCompactionJobs();
} catch (error) {
console.error('Failed to initialize CompactionManager:', error);
throw error;
}
}
startCompactionJobs() {
// Run compaction checks every minute
this.compactionInterval = setInterval(async () => {
try {
console.log('Running scheduled compaction check...');
await this.checkAndCompact();
} catch (error) {
console.error('Compaction job error:', error);
}
}, 60 * 1000);
}
async verifyAndCleanMetadata(type, metadata) {
const existingFiles = [];
const removedFiles = [];
for (const file of metadata.files) {
try {
await fs.promises.access(file.path);
existingFiles.push(file);
} catch (error) {
if (error.code === 'ENOENT') {
console.log(`Removing missing file from metadata: ${file.path}`);
removedFiles.push(file);
} else {
throw error;
}
}
}
if (removedFiles.length > 0) {
// Update metadata to remove missing files
metadata.files = existingFiles;
// Recalculate totals
metadata.parquet_size_bytes = existingFiles.reduce((sum, f) => sum + f.size_bytes, 0);
metadata.row_count = existingFiles.reduce((sum, f) => sum + f.row_count, 0);
if (existingFiles.length > 0) {
metadata.min_time = Math.min(...existingFiles.map(f => f.min_time));
metadata.max_time = Math.max(...existingFiles.map(f => f.max_time));
} else {
metadata.min_time = null;
metadata.max_time = null;
}
// Write updated metadata
await this.bufferManager.writeTypeMetadata(type, metadata);
console.log(`Cleaned up ${removedFiles.length} missing files from metadata`);
}
return metadata;
}
async checkAndCompact() {
const typeDirs = await this.getTypeDirectories();
console.log('Found types for compaction:', typeDirs);
for (const type of typeDirs) {
if (this.compactionLock.get(type)) {
console.log(`Skipping compaction for type ${type} - already running`);
continue;
}
try {
this.compactionLock.set(type, true);
let metadata = await this.bufferManager.getTypeMetadata(type);
if (!metadata.files || !metadata.files.length) {
console.log(`No files found in metadata for type ${type}`);
continue;
}
// Verify and clean metadata before compaction
metadata = await this.verifyAndCleanMetadata(type, metadata);
if (!metadata.files.length) {
console.log(`No valid files remain after metadata cleanup for type ${type}`);
continue;
}
console.log(`Type ${type} has ${metadata.files.length} files to consider for compaction`);
console.log('Files:', metadata.files.map(f => ({
path: f.path,
type: f.type,
min_time: new Date(f.min_time / 1000000).toISOString(),
max_time: new Date(f.max_time / 1000000).toISOString()
})));
await this.compactTimeRange(type, metadata.files, '10m', '1h');
await this.compactTimeRange(type, metadata.files, '1h', '24h');
} catch (error) {
console.error(`Error during compaction for type ${type}:`, error);
} finally {
this.compactionLock.set(type, false);
}
}
}
async getTypeDirectories() {
const baseDir = path.join(
this.bufferManager.baseDir,
this.bufferManager.writerId,
'dbs'
);
try {
const entries = await fs.promises.readdir(baseDir, { withFileTypes: true });
const types = new Set();
for (const entry of entries) {
if (entry.isDirectory() && entry.name.startsWith('hep-')) {
const subEntries = await fs.promises.readdir(path.join(baseDir, entry.name));
for (const subEntry of subEntries) {
if (subEntry.startsWith('hep_')) {
// Extract type from hep_TYPE-ID format
const match = subEntry.match(/hep_([^-]+)-/);
if (match) {
types.add(match[1]); // Store the full type/measurement name
}
}
}
}
}
console.log('Found directories:', Array.from(types).map(type => ({
type,
path: path.join(baseDir, `hep-${this.bufferManager.metadata.next_db_id}`, `hep_${type}-${this.bufferManager.metadata.next_table_id}`)
})));
return Array.from(types);
} catch (error) {
if (error.code === 'ENOENT') {
console.log('No dbs directory found at:', baseDir);
return [];
}
console.error('Error reading type directories:', error);
throw error;
}
}
async compactTimeRange(type, files, fromRange, toRange) {
const now = Date.now() * 1000000;
const interval = this.compactionIntervals[fromRange];
// Group all files (including compacted) by hour
const groups = new Map();
console.log(`Checking ${files.length} files for ${fromRange} compaction...`);
files.forEach(file => {
const isCompacted = path.basename(file.path).startsWith('c_');
const fileAge = now - file.max_time;
// Different rules for raw vs compacted files
if (isCompacted) {
// Compacted files are never too new, they're consolidation targets
console.log(`Found compacted file ${path.basename(file.path)} as potential merge target`);
} else {
// Only raw files have age restrictions
if (fileAge <= interval) {
console.log(`File ${path.basename(file.path)} is too new for compaction (age: ${fileAge / 1000000}s)`);
return;
}
if (fileAge > (interval * 2)) {
console.log(`Found orphaned raw file ${path.basename(file.path)} (age: ${fileAge / 1000000}s)`);
}
}
// Calculate target hour timestamp (floor to hour)
const timestamp = new Date(file.chunk_time / 1000000);
const hourTime = new Date(
timestamp.getFullYear(),
timestamp.getMonth(),
timestamp.getDate(),
timestamp.getHours()
).getTime();
if (!groups.has(hourTime)) {
groups.set(hourTime, {
raw: [],
compacted: []
});
}
// Separate raw and compacted files
if (isCompacted) {
groups.get(hourTime).compacted.push(file);
} else {
groups.get(hourTime).raw.push(file);
}
});
// Log grouping results
for (const [hourTime, { raw, compacted }] of groups) {
console.log(`Hour ${new Date(hourTime).toISOString()}: ${raw.length} raw files, ${compacted.length} compacted files`);
}
// Process each hour group
for (const [hourTime, { raw, compacted }] of groups) {
try {
let filesToCompact = [];
let targetCompacted = null;
// Verify files exist before including them
for (const file of raw) {
try {
await fs.promises.access(file.path);
filesToCompact.push(file);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
}
// Find the most recent compacted file as merge target
for (const file of compacted.sort((a, b) => b.max_time - a.max_time)) {
try {
await fs.promises.access(file.path);
targetCompacted = file;
break;
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
}
// Decide whether to compact based on files available
if (filesToCompact.length >= 2 || (filesToCompact.length > 0 && targetCompacted)) {
if (targetCompacted) {
filesToCompact.push(targetCompacted);
console.log(`Merging ${filesToCompact.length - 1} raw files into existing compacted file ${path.basename(targetCompacted.path)}`);
} else {
console.log(`Creating new compacted file from ${filesToCompact.length} raw files`);
}
await this.compactFiles(type, filesToCompact, toRange);
} else {
console.log(`Not enough files to compact for hour ${new Date(hourTime).toISOString()}`);
}
} catch (error) {
console.error(`Error compacting files for hour ${new Date(hourTime).toISOString()}:`, error);
}
}
}
async getCompactedFilePath(type, timestamp, typeMetadata) {
const date = timestamp.toISOString().split('T')[0];
const hour = timestamp.getHours().toString().padStart(2, '0');
return path.join(
this.bufferManager.baseDir,
this.bufferManager.writerId,
'dbs',
`hep-${this.bufferManager.metadata.next_db_id}`,
`hep_${type}-${this.bufferManager.metadata.next_table_id}`,
date,
`${hour}-00`, // Always use top of the hour for compacted files
`c_${typeMetadata.wal_sequence.toString().padStart(10, '0')}.parquet`
);
}
async compactFiles(type, files, targetRange) {
// Sort files by timestamp to ensure consistent ordering
files.sort((a, b) => a.min_time - b.min_time);
const timestamp = new Date(files[0].chunk_time / 1000000);
const typeMetadata = await this.bufferManager.getTypeMetadata(type);
const newPath = await this.getCompactedFilePath(type, timestamp, typeMetadata);
try {
// Check if all source files exist before starting
for (const file of files) {
await fs.promises.access(file.path);
}
// Create directory for new file
await fs.promises.mkdir(path.dirname(newPath), { recursive: true });
// Determine if this is Line Protocol data by checking first file's schema
const firstReader = await parquet.ParquetReader.openFile(files[0].path);
const schema = firstReader.getSchema();
await firstReader.close();
// Create new writer with appropriate schema
const writer = await parquet.ParquetWriter.openFile(
schema, // Use the schema from the source files
newPath,
this.bufferManager.writerOptions
);
// Track total rows for logging
let totalRows = 0;
// Read and merge all files
for (const file of files) {
const reader = await parquet.ParquetReader.openFile(file.path);
const cursor = reader.getCursor();
let record = null;
while (record = await cursor.next()) {
await writer.appendRow(record);
totalRows++;
}
await reader.close();
}
// Close writer and ensure file is written
await writer.close();
await fs.promises.access(newPath);
// Get stats from new file
const stats = await this.getFileStats(newPath);
// Update metadata first
await this.updateCompactionMetadata(type, files, {
path: newPath,
size_bytes: stats.size_bytes,
row_count: stats.row_count,
chunk_time: files[0].chunk_time,
min_time: stats.min_time,
max_time: stats.max_time,
range: targetRange
});
// Write metadata
await this.writeMetadata();
// Only after metadata is written, clean up old files
await this.cleanupCompactedFiles(files);
const fileTypes = files.map(f => path.basename(f.path).startsWith('c_') ? 'compacted' : 'raw');
const summary = `${fileTypes.filter(t => t === 'raw').length} raw, ${fileTypes.filter(t => t === 'compacted').length} compacted`;
console.log(`Compacted ${files.length} files (${summary}) into ${newPath} (${totalRows} rows)`);
} catch (error) {
console.error(`Compaction error for type ${type}:`, error);
try {
await fs.promises.unlink(newPath);
} catch (e) {
// Ignore cleanup errors
}
throw error;
}
}
async getFileStats(filePath) {
const reader = await parquet.ParquetReader.openFile(filePath);
const cursor = reader.getCursor();
let rowCount = 0;
let minTime = Infinity;
let maxTime = -Infinity;
let record = null;
while (record = await cursor.next()) {
rowCount++;
const timestamp = record.timestamp.getTime();
minTime = Math.min(minTime, timestamp);
maxTime = Math.max(maxTime, timestamp);
}
await reader.close();
const { size: sizeBytes } = await fs.promises.stat(filePath);
return {
size_bytes: sizeBytes,
row_count: rowCount,
min_time: minTime * 1000000, // Convert to nanoseconds
max_time: maxTime * 1000000
};
}
async updateCompactionMetadata(type, oldFiles, newFile) {
const typeMetadata = await this.bufferManager.getTypeMetadata(type);
// Remove old files from metadata
oldFiles.forEach(oldFile => {
const index = typeMetadata.files.findIndex(f => f.path === oldFile.path);
if (index !== -1) {
typeMetadata.parquet_size_bytes -= oldFile.size_bytes;
typeMetadata.row_count -= oldFile.row_count;
typeMetadata.files.splice(index, 1);
}
});
// Add new compacted file
const newFileEntry = {
id: typeMetadata.files.length,
...newFile,
type: path.basename(newFile.path).startsWith('c_') ? 'compacted' : 'raw'
};
typeMetadata.files.push(newFileEntry);
// Update global metadata
typeMetadata.parquet_size_bytes += newFile.size_bytes;
typeMetadata.row_count += newFile.row_count;
typeMetadata.min_time = typeMetadata.min_time ?
Math.min(typeMetadata.min_time, newFile.min_time) : newFile.min_time;
typeMetadata.max_time = typeMetadata.max_time ?
Math.max(typeMetadata.max_time, newFile.max_time) : newFile.max_time;
// Write updated metadata
await this.bufferManager.writeTypeMetadata(type, typeMetadata);
}
async writeMetadata() {
const metadataDir = path.join(
this.bufferManager.baseDir,
this.bufferManager.writerId
);
await fs.promises.mkdir(metadataDir, { recursive: true });
const metadataPath = path.join(metadataDir, 'metadata.json');
const tempPath = `${metadataPath}.tmp`;
try {
// Write metadata to temp file
await fs.promises.writeFile(
tempPath,
JSON.stringify(this.bufferManager.metadata, null, 2)
);
// Ensure temp file exists before rename
await fs.promises.access(tempPath);
// Atomic rename
await fs.promises.rename(tempPath, metadataPath);
// Verify metadata file exists
await fs.promises.access(metadataPath);
} catch (error) {
// Cleanup temp file if it exists
try {
await fs.promises.unlink(tempPath);
} catch (e) {
// Ignore cleanup errors
}
throw error;
}
}
async cleanupCompactedFiles(files) {
// Delete files first
for (const file of files) {
try {
await fs.promises.access(file.path);
await fs.promises.unlink(file.path);
} catch (error) {
if (error.code !== 'ENOENT') {
console.error(`Error deleting file ${file.path}:`, error);
}
}
}
// Collect directories to check
const dirsToCheck = new Set();
files.forEach(file => {
let dirPath = path.dirname(file.path);
while (dirPath.startsWith(this.bufferManager.baseDir)) {
dirsToCheck.add(dirPath);
dirPath = path.dirname(dirPath);
}
});
// Get current hour for comparison
const now = new Date();
const currentDate = now.toISOString().split('T')[0];
const currentHour = now.getHours().toString().padStart(2, '0');
const currentHourPath = `${currentDate}/${currentHour}`;
// Clean up empty directories from deepest to shallowest
const sortedDirs = Array.from(dirsToCheck)
.sort((a, b) => b.split(path.sep).length - a.split(path.sep).length);
for (const dir of sortedDirs) {
try {
// Skip if this is the current hour's directory
if (dir.includes(currentHourPath)) {
console.log(`Skipping cleanup of current hour directory: ${dir}`);
continue;
}
const files = await fs.promises.readdir(dir);
// For hour directories, also check if it's from a past hour
if (dir.match(/\d{4}-\d{2}-\d{2}\/\d{2}-\d{2}/)) {
const dirDate = path.basename(path.dirname(dir));
const dirHour = dir.split('/').pop().split('-')[0];
const dirPath = `${dirDate}/${dirHour}`;
if (dirPath >= currentHourPath) {
console.log(`Skipping cleanup of current or future hour directory: ${dir}`);
continue;
}
}
if (files.length === 0) {
await fs.promises.rmdir(dir);
console.log(`Cleaned up empty directory: ${dir}`);
} else {
console.log(`Directory not empty, skipping cleanup: ${dir} (${files.length} files)`);
}
} catch (error) {
if (error.code !== 'ENOENT') {
console.error(`Error cleaning up directory ${dir}:`, error);
}
}
}
}
async close() {
if (this.compactionInterval) {
clearInterval(this.compactionInterval);
}
}
}
class HEPServer {
constructor(config = {}) {
this.debug = config.debug || false;
this.queryClient = null; // Add queryClient property
}
async initialize() {
try {
this.buffer = new ParquetBufferManager();
await this.buffer.initialize();
this.compaction = new CompactionManager(this.buffer);
await this.compaction.initialize();
// Initialize query client
this.queryClient = new QueryClient(this.buffer.baseDir);
await this.queryClient.initialize();
await this.startServers();
} catch (error) {
console.error('Failed to initialize HEPServer:', error);
throw error;
}
}
async startServers() {
const port = parseInt(process.env.PORT) || 9069;
const httpPort = parseInt(process.env.HTTP_PORT) || (port + 1);
const host = process.env.HOST || "0.0.0.0";
const retryAttempts = 3;
const retryDelay = 1000;
for (let attempt = 1; attempt <= retryAttempts; attempt++) {
try {
// Create TCP Server
const tcpServer = Bun.listen({
hostname: host,
port: port,
socket: {
data: (socket, data) => this.handleData(data, socket),