Skip to content

Commit

Permalink
TRITON-2413 hagfish should notice new instances sooner (#33)
Browse files Browse the repository at this point in the history
TRITON-2413 update PR with requested changes from CR.
TRITON-2413 update PR with further requested changes from CR.
Reviewed by: Brian Bennett
Reviewed by: Dan McDonald <danmcd@mnx.io>
  • Loading branch information
marsell committed Nov 15, 2023
1 parent d4fedaf commit 00491a7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
19 changes: 13 additions & 6 deletions lib/service.js
Expand Up @@ -6,6 +6,7 @@

/*
* Copyright (c) 2014, Joyent, Inc.
* Copyright 2023 Spearhead Systems SRL.
*/

var mod_assert = require('assert-plus');
Expand Down Expand Up @@ -46,7 +47,7 @@ function Service(config) {
}
mod_util.inherits(Service, mod_events.EventEmitter);

Service.prototype._trigger = function (hour, minute) {
Service.prototype._trigger = function (hour, minute, doWrite) {
var self = this;

for (var i = 0; i < self.s_services.length; i++) {
Expand All @@ -63,7 +64,7 @@ Service.prototype._trigger = function (hour, minute) {
continue;
}

srv.srv_service.trigger();
srv.srv_service.trigger(doWrite);
}
};

Expand All @@ -73,15 +74,21 @@ Service.prototype._resched = function () {
var dt = new Date();
dt.setUTCMilliseconds(0);
/*
* Advance to the next minute:
* Advance to the next check, which happens four times per minute:
*/
do {
dt.setUTCSeconds(dt.getUTCSeconds() + 1);
} while (dt.getUTCSeconds() !== 0);
} while (dt.getUTCSeconds() % 15 !== 0);

/*
* Although we check four times a minute, we only write out the accumulated
* results once a minute.
*/
var writeOut = (dt.getUTCSeconds() === 0);

setTimeout(function () {
var dtt = new Date();
self._trigger(dtt.getUTCHours(), dtt.getUTCMinutes());
self._trigger(dtt.getUTCHours(), dtt.getUTCMinutes(), writeOut);
self._resched();
}, dt.valueOf() - Date.now());
};
Expand All @@ -97,7 +104,7 @@ Service.prototype.start = function () {
* Run tasks that should fire once at startup:
*/
setImmediate(function () {
self._trigger(false, false);
self._trigger(false, false, true);
});
/*
* Schedule first periodic execution:
Expand Down
9 changes: 7 additions & 2 deletions lib/service_gzip.js
Expand Up @@ -6,6 +6,7 @@

/*
* Copyright (c) 2014, Joyent, Inc.
* Copyright 2023 Spearhead Systems SRL.
*/

var mod_path = require('path');
Expand Down Expand Up @@ -50,9 +51,13 @@ function GzipService(log, dirname) {
}
mod_util.inherits(GzipService, mod_events.EventEmitter);

GzipService.prototype.trigger = function () {
GzipService.prototype.trigger = function (doWrite) {
var self = this;

if (!doWrite) {
return;
}

if (self.gs_running) {
self.gs_queued = true;
return;
Expand Down Expand Up @@ -103,7 +108,7 @@ GzipService.prototype._work = function () {
if (self.gs_queued) {
self.gs_log.debug('queued, rescheduling immediately');
self.gs_queued = false;
self.trigger();
self.trigger(true);
}
return;
}
Expand Down
40 changes: 35 additions & 5 deletions lib/service_usage.js
Expand Up @@ -6,6 +6,7 @@

/*
* Copyright (c) 2014, Joyent, Inc.
* Copyright 2023 Spearhead Systems SRL.
*/

var mod_path = require('path');
Expand All @@ -28,6 +29,8 @@ function UsageService(log, dirname) {
self.us_dirname = dirname;
self.us_running = false;

self.us_vm_cache = {};

self.us_base_record = null;

lib_common.sysinfo(function (err, sysinfo) {
Expand All @@ -45,7 +48,7 @@ function UsageService(log, dirname) {
}
mod_util.inherits(UsageService, mod_events.EventEmitter);

UsageService.prototype.trigger = function () {
UsageService.prototype.trigger = function (doWrite) {
var self = this;

if (!self.us_base_record) {
Expand All @@ -61,18 +64,18 @@ UsageService.prototype.trigger = function () {

self.us_log.debug('triggered');

self._resched();
self._resched(doWrite);
};

UsageService.prototype._resched = function () {
UsageService.prototype._resched = function (doWrite) {
var self = this;

setImmediate(function () {
self._work();
self._work(doWrite);
});
};

UsageService.prototype._work = function () {
UsageService.prototype._work = function (doWrite) {
var self = this;

var now = new Date();
Expand All @@ -88,6 +91,33 @@ UsageService.prototype._work = function () {
throw (err);
}

/*
* Update cache every _work() call.
*/
vms.forEach(function updateVm(_vm) {
self.us_vm_cache[_vm.uuid] = _vm;
});

/*
* If we're not writing, updating the cache was enough.
*/
if (!doWrite) {
self.us_running = false;
self.us_log.debug({
summary: summary
}, 'caching run complete');

return;
}

/*
* Replace vms with the updated cache, then empty cache.
*/
vms = Object.keys(self.us_vm_cache).map(function getVm(vmUuid) {
return self.us_vm_cache[vmUuid];
});
self.us_vm_cache = {};

var ufw = new lib_ufw.UsageFileWriter(self.us_dirname, now);
ufw.on('error', function (_err) {
self.us_log.fatal({
Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -1,14 +1,14 @@
{
"name": "hagfish-watcher",
"description": "SmartDataCenter Usage Watcher Agent",
"version": "1.0.1",
"version": "1.1.0",
"author": "Joyent (joyent.com)",
"private": true,
"dependencies": {
"bunyan": "0.21.3",
"sax": "0.5.4",
"mkdirp": "0.3.5",
"kstat": "^1.0.2",
"kstat": "1.0.2",
"zutil": "1.0.0",
"extsprintf": "1.0.3",
"vasync": "1.5.0",
Expand Down

0 comments on commit 00491a7

Please sign in to comment.