Skip to content

Commit 766302a

Browse files
committed
system store - endpoints load from core instead of db
Signed-off-by: Amit Prinz Setter <[email protected]>
1 parent 2727e34 commit 766302a

File tree

5 files changed

+65
-8
lines changed

5 files changed

+65
-8
lines changed

src/api/system_api.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,13 @@ module.exports = {
459459
auth: {
460460
system: 'admin'
461461
}
462+
},
463+
464+
get_system_store: {
465+
method: 'GET',
466+
auth: {
467+
system: false
468+
}
462469
}
463470
},
464471

src/endpoint/endpoint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ async function main(options = {}) {
171171
// Load a system store instance for the current process and register for changes.
172172
// We do not wait for it in becasue the result or errors are not relevent at
173173
// this point (and should not kill the process);
174-
system_store.get_instance().load();
174+
system_store.get_instance({source: system_store.SOURCE.CORE}).load();
175175
// Register the process as an md_server.
176176
await md_server.register_rpc();
177177
}

src/server/system_services/system_server.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const config = require('../../../config');
1919
const { BucketStatsStore } = require('../analytic_services/bucket_stats_store');
2020
const { EndpointStatsStore } = require('../analytic_services/endpoint_stats_store');
2121
const os_utils = require('../../util/os_utils');
22-
const { RpcError } = require('../../rpc');
22+
const { RpcError, RPC_BUFFERS } = require('../../rpc');
2323
const nb_native = require('../../util/nb_native');
2424
const Dispatcher = require('../notifications/dispatcher');
2525
const size_utils = require('../../util/size_utils');
@@ -303,6 +303,11 @@ function get_system_status(req) {
303303
};
304304
}
305305

306+
async function get_system_store() {
307+
return {
308+
[RPC_BUFFERS]: {data: Buffer.from(JSON.stringify(await system_store.recent_db_data()))},
309+
};
310+
}
306311

307312
async function _update_system_state(system_id, mode) {
308313
const update = {
@@ -1600,3 +1605,5 @@ exports.rotate_master_key = rotate_master_key;
16001605
exports.disable_master_key = disable_master_key;
16011606
exports.enable_master_key = enable_master_key;
16021607
exports.upgrade_master_keys = upgrade_master_keys;
1608+
1609+
exports.get_system_store = get_system_store;

src/server/system_services/system_store.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ const size_utils = require('../../util/size_utils');
3838
const os_utils = require('../../util/os_utils');
3939
const config = require('../../../config');
4040
const db_client = require('../../util/db_client');
41+
const { decode_json } = require('../../util/postgres_client');
4142

42-
const { RpcError } = require('../../rpc');
43+
const { RpcError, RPC_BUFFERS } = require('../../rpc');
4344
const master_key_manager = require('./master_key_manager');
4445

4546
const COLLECTIONS = [{
@@ -152,6 +153,10 @@ const COLLECTIONS_BY_NAME = _.keyBy(COLLECTIONS, 'name');
152153

153154
const accounts_by_email_lowercase = [];
154155

156+
const SOURCE = Object.freeze({
157+
DB: 'DB',
158+
CORE: 'CORE',
159+
});
155160

156161
/**
157162
*
@@ -352,6 +357,8 @@ class SystemStore extends EventEmitter {
352357
this.START_REFRESH_THRESHOLD = 10 * 60 * 1000;
353358
this.FORCE_REFRESH_THRESHOLD = 60 * 60 * 1000;
354359
this.SYSTEM_STORE_LOAD_CONCURRENCY = config.SYSTEM_STORE_LOAD_CONCURRENCY || 5;
360+
this.source = (process.env.HOSTNAME && process.env.HOSTNAME.indexOf("endpoint") === -1) ? SOURCE.DB : SOURCE.CORE;
361+
dbg.log0("system store source is", this.source);
355362
this._load_serial = new semaphore.Semaphore(1, { warning_timeout: this.START_REFRESH_THRESHOLD });
356363
for (const col of COLLECTIONS) {
357364
db_client.instance().define_collection(col);
@@ -414,18 +421,23 @@ class SystemStore extends EventEmitter {
414421
try {
415422
dbg.log3('SystemStore: loading ... this.last_update_time =', this.last_update_time, ", since =", since);
416423

417-
const new_data = new SystemStoreData();
418-
419424
// If we get a load request with an timestamp older then our last update time
420425
// we ensure we load everyting from that timestamp by updating our last_update_time.
421426
if (!_.isUndefined(since) && since < this.last_update_time) {
422427
dbg.log0('SystemStore.load: Got load request with a timestamp', since, 'older than my last update time', this.last_update_time);
423428
this.last_update_time = since;
424429
}
425430
this.master_key_manager.load_root_key();
431+
const new_data = new SystemStoreData();
426432
let millistamp = time_utils.millistamp();
427433
await this._register_for_changes();
428-
await this._read_new_data_from_db(new_data);
434+
if (this.source === SOURCE.DB) {
435+
await this._read_new_data_from_db(new_data);
436+
} else {
437+
this.data = new SystemStoreData();
438+
await this._read_new_data_from_core(this.data);
439+
}
440+
429441
const secret = await os_utils.read_server_secret();
430442
this._server_secret = secret;
431443
if (dbg.should_log(1)) { //param should match below logs' level
@@ -435,8 +447,10 @@ class SystemStore extends EventEmitter {
435447
depth: 4
436448
}));
437449
}
438-
this.old_db_data = this._update_data_from_new(this.old_db_data || {}, new_data);
439-
this.data = _.cloneDeep(this.old_db_data);
450+
if (this.source === SOURCE.DB) {
451+
this.old_db_data = this._update_data_from_new(this.old_db_data || {}, new_data);
452+
this.data = _.cloneDeep(this.old_db_data);
453+
}
440454
millistamp = time_utils.millistamp();
441455
this.data.rebuild();
442456
dbg.log1('SystemStore: rebuild took', time_utils.millitook(millistamp));
@@ -458,6 +472,11 @@ class SystemStore extends EventEmitter {
458472
});
459473
}
460474

475+
//return the latest copy of in-memory data
476+
async recent_db_data() {
477+
return this._load_serial.surround(async () => this.old_db_data);
478+
}
479+
461480
_update_data_from_new(data, new_data) {
462481
COLLECTIONS.forEach(col => {
463482
const old_items = data[col.name];
@@ -523,6 +542,28 @@ class SystemStore extends EventEmitter {
523542
this.last_update_time = now;
524543
}
525544

545+
async _read_new_data_from_core(target) {
546+
dbg.log3("_read_new_data_from_core begins");
547+
const res = await server_rpc.client.system.get_system_store();
548+
const ss = JSON.parse(res[RPC_BUFFERS].data.toString());
549+
dbg.log3("_read_new_data_from_core new system store", ss);
550+
for (const key of Object.keys(ss)) {
551+
const collection = COLLECTIONS_BY_NAME[key];
552+
if (collection) {
553+
target[key] = [];
554+
_.each(ss[key], item => {
555+
//these two lines will transform string values into appropriately typed objects
556+
//(SensitiveString, ObjectId) according to schema
557+
const after = decode_json(collection.schema, item);
558+
db_client.instance().validate(key, after);
559+
target[key].push(after);
560+
});
561+
} else {
562+
target[key] = ss[key];
563+
}
564+
}
565+
}
566+
526567
_check_schema(col, item, warn) {
527568
return db_client.instance().validate(col.name, item, warn);
528569
}
@@ -851,3 +892,4 @@ SystemStore._instance = undefined;
851892
// EXPORTS
852893
exports.SystemStore = SystemStore;
853894
exports.get_instance = SystemStore.get_instance;
895+
exports.SOURCE = SOURCE;

src/util/postgres_client.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,3 +1951,4 @@ PostgresClient._instance = undefined;
19511951
// EXPORTS
19521952
exports.PostgresClient = PostgresClient;
19531953
exports.instance = PostgresClient.instance;
1954+
exports.decode_json = decode_json;

0 commit comments

Comments
 (0)