-
Notifications
You must be signed in to change notification settings - Fork 0
/
outer_wrapper.py
executable file
·796 lines (691 loc) · 30.7 KB
/
outer_wrapper.py
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
# Copyright 2020 The Johns Hopkins University Applied Physics Laboratory LLC
# All rights reserved.
# Distributed under the terms of the MIT License.
import zmq
import json
from jsonschema import validate, ValidationError
import time
import glob
from threading import Thread, Event
from queue import Queue, Empty
from abc import ABC, abstractmethod
import os
import sys
import logging
import networkx as nx
from collections import defaultdict
class Graph(nx.DiGraph):
def __init__(self, filename):
"""
constructor for the granularity graph
builds a graph by loading it from a JSON file
:param filename: path to the JSON file
"""
# call the networkx parent constructor
super().__init__()
# map the translation function names to the functions
self.functions = {
"simple_sum": self.simple_sum,
"simple_average": self.simple_average,
"weighted_average": self.weighted_average,
"distribute_uniformly": self.distribute_uniformly,
"distribute_by_area": self.distribute_by_area,
"distribute_identically": self.distribute_identically,
}
# build the graph by loading it from the JSON file
with open(filename, mode="r") as json_file:
data = json.load(json_file)
for node in data["nodes"]:
self.add_node(
node["id"],
name=node.get("name"),
type=node.get("type"),
shape=node.get("shape"),
area=node.get("area"),
)
for edge in data["links"]:
self.add_edge(edge["source"], edge["target"])
def simple_sum(self, values, *args):
"""
aggregator for an instance graph
:param values: a list of value tuples: (instance node name, node value)
:return: the sum of the values
"""
return sum([value[1] for value in values])
def simple_average(self, values, *args):
"""
aggregator for an instance graph
:param values: a list of value tuples: (instance node name, node value)
:return: the mean of the values
"""
return sum([value[1] for value in values]) / len(values)
def weighted_average(self, values, parent_granularity, *args):
"""
aggregator for an instance graph
:param values: a list of value tuples: (instance node name, node value)
:return: the area-weighted mean of the values
"""
# get the parents of the first node (each node should have the same parents)
ancestors = nx.ancestors(self, values[0][0])
# get the area of the parent instance node
for ancestor in ancestors:
if self.nodes[ancestor]["type"] == parent_granularity:
parent_area = self.nodes[ancestor]["area"]
break
else:
logging.error(
f"none of the nodes in {ancestors} have granularity {parent_granularity}"
)
parent_area = sum(
[self.nodes[value[0]]["area"] for value in values]
)
return (
sum([value[1] * self.nodes[value[0]]["area"] for value in values])
/ parent_area
)
def distribute_uniformly(self, value, instance, child_granularity):
"""
disaggregator for an instance graph
:param value: the value of the parent node
:param instance: the parent node to disaggregate
:param child_granularity: the intended granularity of the transformation (the child node of the instance in the abstract graph)
:return: a dict mapping each child node to its equal share of the parent value
"""
children = [
child
for child in self.successors(instance)
if self.nodes[child]["type"] == child_granularity
]
mean = value / len(children) if children else 0
distributed = {child: mean for child in children}
return distributed
def distribute_identically(self, value, instance, child_granularity):
"""
disaggregator for an instance graph
:param value: the value of the parent node
:param instance: the parent node to disaggregate
:param child_granularity: the intended granularity of the transformation (the child node of the instance in the abstract graph)
:return: a dict mapping each child node to the parent value
"""
children = [
child
for child in self.successors(instance)
if self.nodes[child]["type"] == child_granularity
]
distributed = {child: value for child in children}
return distributed
def distribute_by_area(self, value, instance, child_granularity):
"""
disaggregator for an instance graph
:param value: the value of the parent node
:param instance: the parent node to disaggregate
:param child_granularity: the intended granularity of the transformation (the child node of the instance in the abstract graph)
:return: a dict mapping ecah child node to its area-proportionate share of the parent value
"""
children = [
child
for child in self.successors(instance)
if self.nodes[child]["type"] == child_granularity
]
parent_area = self.nodes[instance]["area"]
distributed = {
child: value * self.nodes[child]["area"] / parent_area
for child in children
}
return distributed
class OuterWrapper(ABC):
def __init__(self, model_id, num_expected_inputs):
"""
constructor for the outer wrapper, an abstract base class inherited by the inner wrapper
:param model_id: the ID / unique name of the model, as defined in the inner wrapper
:param num_expected_inputs: the number of unique types of input data messages the model needs in order to perform
an increment, as defined in the inner wrapper. Should equal the number of input schemas,
that is, the number of .json files in the model's schemas/input/ directory
"""
self.model_id = model_id
self.num_expected_inputs = num_expected_inputs
self.status = "booting"
self.incstep = 1
self.initial_year = -1
self.increment_flag = False
self.connected_to_broker = False
self.pub_queue = Queue()
self.broker_queue = Queue()
self.action_queue = Queue()
self.abstract_graph = Graph("/abstract-graph.geojson")
self.instance_graph = Graph("/instance-graph.geojson")
self.default_agg = "simple_sum"
self.default_dagg = "distribute_by_area"
self.input_schemas = None
self.output_schemas = None
self.validated_messages = {"this_incstep": {}, "next_incstep": {}}
self.generic_output_schema = (
"{"
' "type": "object",'
' "patternProperties": {'
' ".*": {'
' "type": "object", '
' "properties": {'
' "data": {"type": "object"}, "granularity": {"type": ["string", "null"]}'
" },"
' "required": ["data", "granularity"]'
" }"
" }"
"}"
)
logging.basicConfig(
level=logging.INFO,
stream=sys.stdout,
format="%(asctime)s - %(levelname)s - %(filename)s:%(funcName)s:%(lineno)d - %(message)s",
)
def meet(self, a, b):
sort = sorted((a, b))
return f"{sort[0]}^{sort[1]}"
def aggregate(self, data, src, dest, agg_name=None):
if not agg_name:
agg_name = self.default_agg
# no translation needed
if src == dest:
return data
# get path across the granularity graph
path = nx.shortest_path(self.abstract_graph, dest, src)
path.reverse()
if path:
assert path[0] == src
assert path[-1] == dest
# group the instances by their parent
parents = defaultdict(list)
for instance, value in data.items():
if instance not in self.instance_graph.nodes:
logging.warning(
f"instance {instance} not in instance graph"
)
continue
parent = [
parent
for parent in self.instance_graph.predecessors(instance)
if self.instance_graph.nodes[parent]["type"] == path[1]
]
assert len(parent) == 1
parents[parent[0]].append((instance, value))
# aggregate each parent's child values
translated = {}
for instance, values in parents.items():
trans_func = self.instance_graph.functions.get(agg_name)
translated[instance] = trans_func(values)
# translate to the next granularity in the path
return self.aggregate(translated, path[1], dest, agg_name)
else:
raise Exception(
f"error aggregating from {src} to {dest}, no path found"
)
def disaggregate(self, data, src, dest, disagg_name=None):
if not disagg_name:
disagg_name = self.default_dagg
# no translation needed
if src == dest:
return data
# get path across the granularity graph
path = nx.shortest_path(self.abstract_graph, src, dest)
if path:
assert path[0] == src
assert path[-1] == dest
# iterate over each parent instance
translated = {}
for instance, value in data.items():
if instance not in self.instance_graph.nodes:
logging.warning(
f"instance {instance} not in instance graph"
)
else:
trans_func = self.instance_graph.functions.get(disagg_name)
# for this parent, create a dict of child instances mapped to disaggregated values
children = trans_func(value, instance, path[1])
# add this parent's dict of children to the flat dict
translated = {**translated, **children}
# translate to the next granularity in the path
return self.disaggregate(translated, path[1], dest, disagg_name)
else:
raise Exception(
f"error disaggregating from {src} to {dest}, no path found"
)
def translate(
self, data, src, dest, variable, agg_name=None, disagg_name=None
):
"""
:param data: dictionary mapping instance nodes (of src granularity) to their values
:param src: granularity of the data (the abstract graph must have a node with the same name)
:param dest: granularity to translate the data to (the abstract graph must have a node with the same name)
:return: dictionary mapping instance nodes (of dest granularity) to their values
"""
# default aggregator and disaggregator
if not agg_name:
agg_name = self.default_agg
if not disagg_name:
disagg_name = self.default_dagg
# no translation necessary
if src == dest:
return data
# disaggregate straight down a branch of the granularity graph
elif nx.has_path(self.abstract_graph, src, dest):
return self.disaggregate(data, src, dest, disagg_name)
# aggregate straight up a branch of the granularity graph
elif nx.has_path(self.abstract_graph, dest, src):
return self.aggregate(data, src, dest, agg_name)
# translate between branches of the granularity graph: disaggregate down, then aggregate back up
elif nx.has_path(
self.abstract_graph, src, self.meet(src, dest)
) and nx.has_path(self.abstract_graph, dest, self.meet(src, dest)):
disaggregated = self.disaggregate(
data, src, self.meet(src, dest), disagg_name
)
aggregated = self.aggregate(
disaggregated, self.meet(src, dest), dest, agg_name
)
return aggregated
else:
raise Exception(
f"error translating {variable} from {src} to {dest}, no path found"
)
def load_json_objects(self, dir_path):
"""
load JSON objects from .json files
:param dir_path: path to a directory with .json files
:return: a dictionary that a maps a schema's filename (without the .json extension) to the corresponding JSON
object / schema
"""
schemas = {}
for schema in glob.glob(f"{dir_path}/*.json"):
with open(schema) as schema_file:
file_name = os.path.splitext(os.path.basename(schema))[0]
schemas[file_name] = json.load(schema_file)
return schemas
@abstractmethod
def configure(self, **kwargs):
"""
implemented in the inner wrapper. Initializes the model with its parameters, needed before any increments
:param kwargs:
:return:
"""
raise NotImplementedError(
f"configure() has to be implemented in the {self.model_id} inner wrapper"
)
@abstractmethod
def increment(self, **kwargs):
"""
implemented in the inner wrapper
:param kwargs:
:return:
"""
raise NotImplementedError(
f"increment() has to be implemented in the {self.model_id} inner wrapper"
)
def increment_handler(self, event, incstep):
"""
Calls increment() after validating inputs, then validates the results of the increment
:param event: the shutdown event for managing threads
:param incstep: the increment number that is currently being performed, as understood by the broker
:return: if the increment is successful, returns its results in a dictionary
"""
self.increment_flag = True
logging.info(
f"about to increment, incstep {incstep}, year {self.initial_year + incstep}"
)
self.incstep = incstep
# validate against input schemas
if (
incstep > 1
and len(self.validated_messages["this_incstep"])
!= self.num_expected_inputs
):
logging.critical(
f"number of validated messages {len(self.validated_messages['this_incstep'])} != num_expected_inputs {self.num_expected_inputs}"
)
event.set()
raise RuntimeError
# call the inner wrapper
payloads = {}
for schema, message in self.validated_messages["this_incstep"].items():
payloads[schema] = message["payload"]
results = self.increment(**payloads)
# validate against output schemas
for schema_name, data_msg in results.items():
try:
validate(data_msg, json.loads(self.generic_output_schema))
validate(data_msg, self.output_schemas[schema_name])
except Exception as e:
logging.critical(
f"message {data_msg} failed to validate schema {schema_name}"
)
event.set()
raise RuntimeError
if len(results) != len(self.output_schemas):
logging.critical("didn't validate against every output schema")
event.set()
raise RuntimeError
self.increment_flag = False
for schema, data in results.items():
data_msg = {}
data_msg["schema"] = schema
data_msg["payload"] = data
data_msg["signal"] = "data"
data_msg["source"] = self.model_id
data_msg["incstep"] = self.incstep
data_msg["year"] = self.incstep + self.initial_year
self.pub_queue.put(data_msg)
logging.info(
f"finished increment {self.incstep}, year {self.incstep + self.initial_year}"
)
self.incstep += 1
def send_status(self, event):
"""
creates a status message and puts it into the publish queue. A status message must include the model's ID,
a signal / message type of "status", the increment step, and the current status:
booting: model is waiting for the broker to boot (receive status messages from all models)
waiting: model is waiting for its needed input data messages from other models
ready: model can begin incrementing if it receives an increment pulse; it is at the first increment step,
or it has received all input messages and has validated them against input schemas
incrementing: model has received an increment pulse and is performing the increment in the handler
:param event: the shutdown event for managing threads
:return: runs continuously until the shutdown event is set
"""
count = 0
while not event.is_set():
count += 1
time.sleep(1)
if self.connected_to_broker:
if self.increment_flag:
# waiting for the increment to finish
self.status = "incrementing"
else:
if self.incstep == 1:
# kickstart the model for the first increment
self.status = "ready"
elif (
len(self.validated_messages["next_incstep"])
== self.num_expected_inputs
):
# ready for an increment
self.validated_messages[
"this_incstep"
] = self.validated_messages["next_incstep"].copy()
self.validated_messages["next_incstep"].clear()
self.status = "ready"
elif (
len(self.validated_messages["this_incstep"])
== self.num_expected_inputs
):
# ready for an increment
self.status = "ready"
else:
# still waiting for messages from other models
self.status = "waiting"
else:
self.status = "booting"
message = {}
message["source"] = self.model_id
message["id"] = count
message["time"] = time.time()
message["date"] = time.ctime()
message["signal"] = "status"
message["incstep"] = self.incstep
message["year"] = self.incstep + self.initial_year
message["status"] = self.status
self.pub_queue.put(message)
logging.debug(json.dumps(message))
def pub(self, event, context):
"""
publishes messages to the broker, including status messages and data messages
Sets the shutdown event if an outgoing data message matches more than one output schema.
:param event: the shutdown event for managing threads
:return: runs continuously until shutdown event is set, then closes its zmq socket
"""
# connect to zmq
sock = context.socket(zmq.PUB)
sock.setsockopt(zmq.LINGER, 1000)
sock.connect("tcp://broker:5555")
while not event.is_set():
# get message from the queue
try:
message = self.pub_queue.get(timeout=0.1)
except Empty:
continue
# send status messages
if message.get("signal") == "status":
sock.send_json(message)
continue
# validate data messages
matched = []
for name, schema in self.output_schemas.items():
try:
validate(message["payload"], schema)
logging.info(
f"validated outgoing message from {message['source']}"
)
matched.append(schema)
# translate each data variable to output schema's granularity
for item in message["payload"]:
# get current granularity from the data message
src_gran = message["payload"][item]["granularity"]
# get granularity and translation functions from the schema
dest_gran = schema["properties"][item]["properties"][
"granularity"
].get("value", src_gran)
agg = (
schema["properties"][item]["properties"]
.get("agg", {})
.get("value")
)
dagg = (
schema["properties"][item]["properties"]
.get("dagg", {})
.get("value")
)
# translate the data and update the data message
logging.info(
f"validating output: message from {name}, translating variable {item}, {src_gran} -> {dest_gran}"
)
data = self.translate(
message["payload"][item]["data"],
src_gran,
dest_gran,
item,
agg_name=agg,
disagg_name=dagg,
)
message["payload"][item]["data"] = data
message["payload"][item]["unit"] = schema[
"properties"
][item]["properties"]["data"].get("unit", "")
message["payload"][item]["granularity"] = dest_gran
except ValidationError:
logging.debug("validation error")
except json.JSONDecodeError:
logging.warning("json decode error")
if len(matched) == 0:
logging.debug(
f"message didn't match any output schemas: {message['source']}"
)
elif len(matched) == 1:
logging.info(
f"message matched an output schema: {message['source']}"
)
sock.send_json(message)
else:
logging.critical(
f"more than one output schema was matched: {message['source']}"
)
event.set()
sock.close()
def sub(self, event, context):
"""
connects to the broker's PUB as a subscriber and receives all messages sent from the broker,
and all messages sent by other models and forwarded by the broker.
Status messages from the broker go into the broker queue, for the watchdog.
:param event: the shutdown event for managing threads
:return: runs continuously until shutdown event is set, then closes its zmq socket
"""
# connect to zmq
sock = context.socket(zmq.SUB)
sock.setsockopt(zmq.SUBSCRIBE, b"")
sock.setsockopt(zmq.RCVTIMEO, 0)
sock.setsockopt(zmq.LINGER, 1000)
sock.connect("tcp://broker:5556")
while not event.is_set():
try:
message = sock.recv_json()
except zmq.ZMQError:
continue
logging.debug(json.dumps(message))
signal = message.get("signal")
if signal == "status" and message.get("source") == "broker":
self.broker_queue.put(message)
elif signal == "data":
if not self.insert_data_message(message):
event.set()
else:
self.action_queue.put(message)
sock.close()
def insert_data_message(self, message):
"""
validates a data message against the input schemas
:param message: the data message to insert into the queue
:return: False if message insertion throws an error, otherwise True.
returns True if the message is validated by 0 or 1 schemas
returns False if the data message is a duplicate
(was validated by a schema already used since the last increment)
"""
# validate data messages
matched = []
for name, schema in self.input_schemas.items():
try:
validate(message["payload"], schema)
logging.info(
f"schema {name} validated incoming message from {message['source']}"
)
if name in self.validated_messages["next_incstep"]:
logging.error(
f"schema {name} already validated a message: {self.validated_messages['next_incstep'][name]}"
)
logging.error(f"new message: {message}")
return False
else:
matched.append(schema)
# translate each data variable to input schema's granularity
for item in message["payload"]:
# get current granularity from the data message
src_gran = message["payload"][item]["granularity"]
# get granularity and translation functions from the schema
dest_gran = schema["properties"][item]["properties"][
"granularity"
].get("value", src_gran)
agg = (
schema["properties"][item]["properties"]
.get("agg", {})
.get("value")
)
dagg = (
schema["properties"][item]["properties"]
.get("dagg", {})
.get("value")
)
# translate the data and update the data message
logging.info(
f"validating input: message from {name}, translating variable {item}, {src_gran} -> {dest_gran}"
)
data = self.translate(
message["payload"][item]["data"],
src_gran,
dest_gran,
item,
agg_name=agg,
disagg_name=dagg,
)
message["payload"][item]["data"] = data
message["payload"][item]["unit"] = schema[
"properties"
][item]["properties"]["data"].get("unit", "")
message["payload"][item]["granularity"] = dest_gran
self.validated_messages["next_incstep"][name] = message
except ValidationError:
logging.debug("validation error")
except json.JSONDecodeError:
logging.warning("json decode error")
if len(matched) == 0:
logging.debug(
f"message didn't match any input schemas: {message['source']}"
)
return True
def action_worker(self, event):
"""
gets messages from the action queue and performs the respective action. currently, just the increment action
:param event: the shutdown event for managing threads
:return: runs continuously until the shutdown event is set
"""
while not event.is_set():
try:
message = self.action_queue.get(timeout=0.1)
except Empty:
continue
if message["signal"] == "increment":
try:
self.increment_handler(event, message["incstep"])
except Exception as e:
logging.critical(e)
event.set()
raise RuntimeError
def watchdog(self, event):
"""
verifies that the broker is running by receiving its status messages. Sets the connected_to_broker flag
if the broker's status is booted, that is, if the broker has received status messages from all models,
so that models can safely receive increment pulses and publish their data.
Will set the shutdown event if a status message has not been received within the timeout interval
:param event: the shutdown event for managing threads
:return: runs continuously until the shutdown event is set
"""
while not event.is_set():
try:
message = self.broker_queue.get(timeout=10)
if message.get("status") == "booted":
self.connected_to_broker = True
self.initial_year = message.get("initial_year")
except Empty:
logging.critical("Timed out waiting for broker message")
event.set()
def run(self):
"""
main thread of the outer wrapper. Launches all sub threads. Called from the inner wrapper
:return: runs continuously until shutdown event is set
"""
# initialize the model
self.input_schemas = self.load_json_objects("/opt/schemas/input")
self.output_schemas = self.load_json_objects("/opt/schemas/output")
initial_conditions = self.load_json_objects("/opt/config")
self.configure(**initial_conditions)
# start the threads
shutdown = Event()
context = zmq.Context()
# listen for messages
subscribe_thread = Thread(target=self.sub, args=(shutdown, context,))
subscribe_thread.start()
# publish messages
publish_thread = Thread(target=self.pub, args=(shutdown, context,))
publish_thread.start()
# handle increments
action_thread = Thread(target=self.action_worker, args=(shutdown,))
action_thread.start()
# check connectivity to broker
watchdog_thread = Thread(target=self.watchdog, args=(shutdown,))
watchdog_thread.start()
# submit updated status to the publish queue
status_thread = Thread(target=self.send_status, args=(shutdown,))
status_thread.start()
try:
while not shutdown.is_set():
time.sleep(1)
except Exception as e:
logging.critical(e)
finally:
context.term()
shutdown.set()
logging.critical(f"{self.model_id} model has shut down")