/
server_pyro.py
424 lines (366 loc) · 15.9 KB
/
server_pyro.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
# ___________________________________________________________________________
#
# Pyomo: Python Optimization Modeling Objects
# Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
# Under the terms of Contract DE-NA0003525 with National Technology and
# Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
# rights in this software.
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________
__all__ = ("ScenarioTreeServerPyro",
"RegisterWorker")
import os
import sys
import socket
import copy
import argparse
import logging
import traceback
import base64
try:
import cPickle as pickle
except:
import pickle
import pyutilib.misc
from pyutilib.misc import PauseGC
from pyutilib.pyro import (TaskWorker,
TaskWorkerServer,
shutdown_pyro_components,
TaskProcessingError,
using_pyro4)
if using_pyro4:
import Pyro4
from pyomo.util import pyomo_command
from pyomo.opt import (SolverFactory,
PersistentSolver,
TerminationCondition,
SolutionStatus)
from pyomo.opt.parallel.manager import ActionManagerError
from pyomo.pysp.util.misc import (parse_command_line,
launch_command,
load_external_module)
from pyomo.pysp.util.config import (PySPConfigValue,
PySPConfigBlock,
safe_declare_common_option,
safe_register_common_option,
safe_register_unique_option,
_domain_tuple_of_str)
from pyomo.pysp.util.configured_object import PySPConfiguredObject
from pyomo.pysp.scenariotree.instance_factory import \
ScenarioTreeInstanceFactory
from pyomo.pysp.scenariotree.server_pyro_utils import \
(WorkerInitType,
WorkerInit)
import six
from six import iteritems
logger = logging.getLogger('pyomo.pysp')
class ScenarioTreeServerPyro(TaskWorker, PySPConfiguredObject):
# Maps name to a registered worker class to instantiate
_registered_workers = {}
_declared_options = \
PySPConfigBlock("Options declared for the "
"ScenarioTreeServerPyro class")
#
# scenario instance construction
#
safe_declare_common_option(_declared_options,
"model_location")
safe_declare_common_option(_declared_options,
"scenario_tree_location")
#
# scenario tree generation
#
safe_declare_common_option(_declared_options,
"scenario_tree_random_seed")
safe_declare_common_option(_declared_options,
"scenario_tree_downsample_fraction")
#
# various
#
safe_declare_common_option(_declared_options,
"verbose")
@classmethod
def get_registered_worker_type(cls, name):
if name in cls._registered_workers:
return cls._registered_workers[name]
raise KeyError("No worker type has been registered under the name "
"'%s' for ScenarioTreeServerPyro" % (name))
def __init__(self, *args, **kwds):
# add for purposes of diagnostic output.
kwds["name"] = ("ScenarioTreeServerPyro_%d@%s"
% (os.getpid(), socket.gethostname()))
kwds["caller_name"] = kwds["name"]
self._modules_imported = kwds.pop('modules_imported', {})
TaskWorker.__init__(self, **kwds)
assert hasattr(self, "_bulk_task_collection")
self._bulk_task_collection = True
self._contiguous_task_processing = False
# This classes options get updated during the "setup" phase
options = self.register_options()
PySPConfiguredObject.__init__(self, options)
self.type = self.WORKERNAME
self.block = True
self.timeout = None
self._worker_map = {}
#
# These will be used by all subsequent workers created
# by this server. Their creation can eat up a nontrivial
# amount of initialization time when a large number of
# workers are created on this server, so we only create
# them once.
#
self._scenario_instance_factory = None
self._full_scenario_tree = None
def reset(self):
if self._scenario_instance_factory is not None:
self._scenario_instance_factory.close()
self._scenario_instance_factory = None
self._full_scenario_tree = None
for worker_name in list(self._worker_map):
self.remove_worker(worker_name)
def remove_worker(self, name):
self._worker_map[name].close()
del self._worker_map[name]
def process(self, data):
self._worker_task_return_queue = self._current_task_client
try:
# The only reason we are go through this much
# effort to deal with the serpent serializer
# is because it is the default in Pyro4.
if using_pyro4 and \
(Pyro4.config.SERIALIZER == 'serpent'):
if six.PY3:
assert type(data) is dict
assert data['encoding'] == 'base64'
data = base64.b64decode(data['data'])
else:
assert type(data) is unicode
data = str(data)
return pickle.dumps(self._process(pickle.loads(data)))
except:
logger.error(
"Scenario tree server %s caught an exception of type "
"%s while processing a task. Going idle."
% (self.WORKERNAME, sys.exc_info()[0].__name__))
traceback.print_exception(*sys.exc_info())
self._worker_error = True
return pickle.dumps(TaskProcessingError(traceback.format_exc()))
def _process(self, data):
data = pyutilib.misc.Bunch(**data)
result = None
if not data.action.startswith('ScenarioTreeServerPyro_'):
#with PauseGC() as pgc:
result = getattr(self._worker_map[data.worker_name], data.action)\
(*data.args, **data.kwds)
elif data.action == 'ScenarioTreeServerPyro_setup':
options = self.register_options()
for name, val in iteritems(data.options):
options.get(name).set_value(val)
self.set_options(options)
self._options.verbose = self._options.verbose | self._verbose
assert self._scenario_instance_factory is None
assert self._full_scenario_tree is None
if self._options.verbose:
print("Server %s received setup request."
% (self.WORKERNAME))
print("Options:")
self.display_options()
# Make sure these are not archives
assert os.path.exists(self._options.model_location)
assert (self._options.scenario_tree_location is None) or \
os.path.exists(self._options.scenario_tree_location)
self._scenario_instance_factory = \
ScenarioTreeInstanceFactory(
self._options.model_location,
self._options.scenario_tree_location)
#
# Try to prevent unnecessarily re-importing the model module
# if other callbacks are in the same location. Doing so might
# have serious consequences.
#
if self._scenario_instance_factory._model_module is not None:
self._modules_imported[self._scenario_instance_factory.\
_model_filename] = \
self._scenario_instance_factory._model_module
if self._scenario_instance_factory._scenario_tree_module is not None:
self._modules_imported[self._scenario_instance_factory.\
_scenario_tree_filename] = \
self._scenario_instance_factory._scenario_tree_module
self._full_scenario_tree = \
self._scenario_instance_factory.generate_scenario_tree(
downsample_fraction=self._options.scenario_tree_downsample_fraction,
random_seed=self._options.scenario_tree_random_seed,
verbose=self._options.verbose)
if self._full_scenario_tree is None:
raise RuntimeError("Unable to launch scenario tree worker - "
"scenario tree construction failed.")
result = True
elif data.action == "ScenarioTreeServerPyro_initialize":
worker_name = data.worker_name
if self._options.verbose:
print("Server %s received request to initialize "
"scenario tree worker with name %s."
% (self.WORKERNAME, worker_name))
assert self._scenario_instance_factory is not None
assert self._full_scenario_tree is not None
if worker_name in self._worker_map:
raise RuntimeError(
"Server %s Cannot initialize worker with name '%s' "
"because a worker already exists with that name."
% (self.WORKERNAME, worker_name))
worker_type = self._registered_workers[data.worker_type]
options = worker_type.register_options()
for name, val in iteritems(data.options):
options.get(name).set_value(val)
#
# Depending on the Pyro serializer, the namedtuple
# may be been converted to a tuple
#
if not isinstance(data.worker_init, WorkerInit):
assert type(data.worker_init) is tuple
data.worker_init = WorkerInit(type_=data.worker_init[0],
names=data.worker_init[1],
data=data.worker_init[2])
# replace enum string representation with the actual enum
# object now that we've unserialized the Pyro data
worker_init = WorkerInit(type_=getattr(WorkerInitType,
data.worker_init.type_),
names=data.worker_init.names,
data=data.worker_init.data)
self._worker_map[worker_name] = worker_type(options)
self._worker_map[worker_name].initialize(
self.WORKERNAME,
self._full_scenario_tree,
worker_name,
worker_init,
self._modules_imported)
result = True
elif data.action == "ScenarioTreeServerPyro_release":
if self._options.verbose:
print("Server %s releasing worker: %s"
% (self.WORKERNAME, data.worker_name))
self.remove_worker(data.worker_name)
result = True
elif data.action == "ScenarioTreeServerPyro_reset":
if self._options.verbose:
print("Server %s received reset request"
% (self.WORKERNAME))
self.reset()
result = True
elif data.action == "ScenarioTreeServerPyro_shutdown":
if self._options.verbose:
print("Server %s received shutdown request"
% (self.WORKERNAME))
self.reset()
self._worker_shutdown = True
result = True
else:
raise ValueError("Server %s: Invalid command: %s"
% (self.WORKERNAME, data.action))
return result
def RegisterWorker(name, class_type):
assert name not in ScenarioTreeServerPyro._registered_workers, \
("The name %s is already registered for another worker class"
% (name))
ScenarioTreeServerPyro._registered_workers[name] = class_type
#
# Register some known, trusted workers
#
from pyomo.pysp.scenariotree.manager_worker_pyro import \
ScenarioTreeManagerWorkerPyro
RegisterWorker('ScenarioTreeManagerWorkerPyro',
ScenarioTreeManagerWorkerPyro)
from pyomo.pysp.scenariotree.manager_solver_worker_pyro import \
ScenarioTreeManagerSolverWorkerPyro
RegisterWorker('ScenarioTreeManagerSolverWorkerPyro',
ScenarioTreeManagerSolverWorkerPyro)
#
# utility method fill a PySPConfigBlock with options associated
# with the scenariotreeserver command
#
def scenariotreeserver_register_options(options=None):
if options is None:
options = PySPConfigBlock()
safe_register_common_option(options, "disable_gc")
safe_register_common_option(options, "profile")
safe_register_common_option(options, "traceback")
safe_register_common_option(options, "verbose")
safe_register_common_option(options, "pyro_host")
safe_register_common_option(options, "pyro_port")
safe_register_unique_option(
options,
"import_module",
PySPConfigValue(
(),
domain=_domain_tuple_of_str,
description=(
"The name of a user-defined python module to import that, "
"e.g., registers a user-defined scenario tree worker class."
),
doc=None,
visibility=0))
return options
#
# Execute the scenario tree server daemon.
#
def exec_scenariotreeserver(options):
modules_imported = {}
for module_name in options.import_module:
if module_name in sys.modules:
modules_imported[module_name] = sys.modules[module_name]
else:
modules_imported[module_name] = \
load_external_module(module_name, clear_cache=True)[0]
try:
# spawn the daemon
TaskWorkerServer(ScenarioTreeServerPyro,
host=options.pyro_host,
port=options.pyro_port,
verbose=options.verbose,
modules_imported=modules_imported)
except:
# if an exception occurred, then we probably want to shut down
# all Pyro components. otherwise, the PH client may have
# forever while waiting for results that will never
# arrive. there are better ways to handle this at the PH
# client level, but until those are implemented, this will
# suffice for cleanup.
#NOTE: this should perhaps be command-line driven, so it can
# be disabled if desired.
print("ScenarioTreeServerPyro aborted. Sending shutdown request.")
shutdown_pyro_components(host=options.pyro_host,
port=options.pyro_port,
num_retries=0)
raise
@pyomo_command("scenariotreeserver",
"Pyro-based server for scenario tree management")
def main(args=None):
#
# Top-level command that executes the scenario tree server daemon.
#
#
# Import plugins
#
import pyomo.environ
#
# Parse command-line options.
#
try:
options = parse_command_line(
args,
scenariotreeserver_register_options,
prog='scenariotreeserver',
description=(
"""Launches a scenariotreeserver process to manage workers in a
distributed scenario tree."""
))
except SystemExit as _exc:
# the parser throws a system exit if "-h" is specified
# - catch it to exit gracefully.
return _exc.code
return launch_command(exec_scenariotreeserver,
options,
error_label="scenariotreeserver: ",
disable_gc=options.disable_gc,
profile_count=options.profile,
traceback=options.traceback)