DependableSystemsLab / ThingsJS

ThingsJS is a framework for running JavaScript applications on IoT devices such as Raspberry PIs. For more details, see below:
http://thingsjs.io
MIT License
16 stars 5 forks source link

Scheduler: Exception thrown when CodeEngine device stats are not available #28

Closed marusshi closed 6 years ago

marusshi commented 6 years ago

When a new Code Engine instance begins to run, it publishes its existence to the topic engine-registry. Dispatcher listens to this topic to keep a mapping of all existing engines in this.engines.

Scheduler, who inherits from Dispatcher, takes this mapping when finding available devices (Scheduler.js:136 in Scheduler.prototype._assess):

        Object.values(self.engines).forEach(function(engine){
            if (engine.status !== 'dead'){
                var stat = engine.getStat();
                mapping[engine.id] = {
                    available_memory: (stat.device_memory / 1000000),
                    processes: {}
                };
            }
        });

If a Code Engine instance exits the network after it publishes its existence to engine-registry but before it publishes its status in a setInterval reportStatus() call, the following exception will be thrown:

[Scheduler] Invoke start
(node:1124) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'device_memory' of undefined
    at ThingsJS\lib\core\Scheduler.js:140:30
    at Array.forEach (<anonymous>)
    at ThingsJS\lib\core\Scheduler.js:136:31
jungkumseok commented 6 years ago

Cool, I'm gonna paraphrase this problem for clarity:

Scheduler wants to use current device stats for its calculation, but it could happen that a newly launched CodeEngine has not reported its stats (e.g. available memory) yet. Scheduler will throw an error complaining it does not have stats about this new CodeEngine.

I think we should change this so that if a data point is not available yet, we approach it conservatively and say that the device has 0 available memory. Let's discuss this in the meeting.

jungkumseok commented 6 years ago

Patched in 17a704056d4afa5d760af5d756b63e8be4929471