-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage-manager.js
191 lines (170 loc) · 7.24 KB
/
storage-manager.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
const { Readable } = require('stream');
const { Encoding } = require('@hkube/encoding');
const prettyBytes = require('pretty-bytes');
const unitsConverter = require('@hkube/units-converter');
const { STORAGE_PREFIX } = require('../consts/storage-prefix');
const StorageBase = require('./storage/storage-base');
const Hkube = require('./storage/hkube');
const HkubeIndex = require('./storage/hkube-index');
const HkubeResults = require('./storage/hkube-results');
const HkubeStore = require('./storage/hkube-store');
const HkubeMetadata = require('./storage/hkube-metadata');
const HkubeExecutions = require('./storage/hkube-execution');
const HkubeBuilds = require('./storage/hkube-builds');
const HkubeAlgoMetrics = require('./storage/hkube-algo-metrics');
const DataSource = require('./storage/hkube-datasource');
class StorageManager {
constructor() {
this._wasInit = false;
}
async init(config, log, bootstrap = false, prefixes = Object.values(STORAGE_PREFIX)) {
if (!this._wasInit) {
const storage = config.storageAdapters[config.defaultStorage];
this._info = {
type: config.defaultStorage,
encoding: storage.encoding,
clusterName: config.clusterName,
moduleName: storage.moduleName,
storageResultsThreshold: config.storageResultsThreshold
};
this.moduleName = storage.moduleName;
this._adapterPackage = require(storage.moduleName); // eslint-disable-line
this._adapter = new this._adapterPackage();
this._storageResultsThreshold = config.storageResultsThreshold && unitsConverter.getMemoryInBytes(config.storageResultsThreshold);
this.prefixesTypes = prefixes.map(x => `${config.clusterName}-${x}`);
await this._adapter.init(storage.connection, this.prefixesTypes, bootstrap);
this._wasInit = true;
this.encoding = new Encoding({ type: storage.encoding });
this.storage = new StorageBase(this._adapter, log, this.encoding);
this.hkube = new Hkube(this._adapter, log, config, this.encoding);
this.hkubeResults = new HkubeResults(this._adapter, log, config, this.encoding);
this.hkubeStore = new HkubeStore(this._adapter, log, config, this.encoding);
this.hkubeMetadata = new HkubeMetadata(this._adapter, log, config, this.encoding);
this.hkubeExecutions = new HkubeExecutions(this._adapter, log, config, this.encoding);
this.hkubeIndex = new HkubeIndex(this._adapter, log, config, this.encoding);
this.hkubeBuilds = new HkubeBuilds(this._adapter, log, config, this.encoding);
this.hkubeAlgoMetrics = new HkubeAlgoMetrics(this._adapter, log, config, this.encoding);
this.hkubeDataSource = new DataSource(this._adapter, log, config, this.encoding);
}
}
getInfo() {
return this._info;
}
checkDataSize(size) {
const result = {};
if (size >= this._storageResultsThreshold) {
result.error = `data too large (${prettyBytes(size)}), use the stream api`;
}
return result;
}
async getCustomStream(options) {
const { path } = options;
const { size: totalLength, metadata, headerInData } = await this.getMetadata({ path });
const { header } = metadata || {};
const { headerLength } = this.encoding;
let stream;
if (!header) {
stream = await this.getStream({ path, totalLength });
}
else {
const { isCustomEncoding, isDataTypeEncoded } = this._readHeader(header);
if (isCustomEncoding) { // check if hkube encoding is here
const start = headerInData ? headerLength : 0;
const end = totalLength;
if (isDataTypeEncoded) { // check if this data is encoded
const result = this.checkDataSize(totalLength);
if (result.error) {
// currently we are not supporting large decoding
stream = await this.getStream({ path, start, end, totalLength });
}
else {
const { payload } = await this.getCustomData(options);
stream = new Readable();
stream.push(JSON.stringify(payload));
stream.push(null);
}
}
else {
stream = await this.getStream({ path, start, end, totalLength }); // this data is not encoded, we should stream it (without header)
}
}
else {
stream = await this.getStream({ path, totalLength });
}
}
return stream;
}
_readHeader(header) {
const { magicNumber } = this.encoding;
const magicNum = header.slice(header.length - magicNumber.length, header.length).toString();
const isCustomEncoding = magicNum === magicNumber;
let isDataTypeEncoded = false;
if (isCustomEncoding) {
isDataTypeEncoded = this.encoding.isDataTypeEncoded(header[2]);
}
return { isCustomEncoding, isDataTypeEncoded };
}
async _createBytesRange(options) {
const { start, end, totalLength } = options;
if (!start && !end) {
return options;
}
if (start < 0 || (start > 0 && end === 0)) {
throw new Error('invalid range');
}
let from = start || 0;
let to = end || 0;
const absEnd = Math.abs(to);
let totalSize;
if (totalLength == null) {
const stats = await this.getMetadata(options);
totalSize = stats.size;
}
if (start > totalSize || absEnd > totalSize) {
throw new Error('invalid range');
}
if (start === 0 && end < 0) {
to = totalSize - absEnd;
}
else if (start == null && end < 0) {
from = totalSize - absEnd;
to = totalSize;
}
else if (start >= 0 && end == null) {
from = start;
to = totalSize;
}
return { start: from, end: to };
}
async put(options, tracer) {
return this.storage.put(options, tracer);
}
async get(options, tracer) {
return this.storage.get(options, tracer);
}
async getCustomData(options) {
return this.storage.getCustomData(options);
}
async getMetadata(options, tracer) {
return this.storage.getMetadata(options, tracer);
}
async seek(options, tracer) {
const { start, end } = await this._createBytesRange(options);
return this.storage.seek({ ...options, start, end }, tracer);
}
async list(options) {
return this.storage.list(options);
}
async delete(options) {
return this.storage.delete(options);
}
async getStream(options) {
const { start, end } = await this._createBytesRange(options);
return this.storage.getStream({ ...options, start, end });
}
async putStream(options) {
return this.storage.putStream(options);
}
}
module.exports = new StorageManager();
module.exports.StorageManager = StorageManager;