-
Notifications
You must be signed in to change notification settings - Fork 327
/
pbs_python_restart_settings.py
427 lines (410 loc) · 18.4 KB
/
pbs_python_restart_settings.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
# coding: utf-8
# Copyright (C) 1994-2019 Altair Engineering, Inc.
# For more information, contact Altair at www.altair.com.
#
# This file is part of the PBS Professional ("PBS Pro") software.
#
# Open Source License Information:
#
# PBS Pro is free software. You can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# PBS Pro is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Commercial License Information:
#
# For a copy of the commercial license terms and conditions,
# go to: (http://www.pbspro.com/UserArea/agreement.html)
# or contact the Altair Legal Department.
#
# Altair’s dual-license business model allows companies, individuals, and
# organizations to create proprietary derivative works of PBS Pro and
# distribute them - whether embedded or bundled with other software -
# under a commercial license agreement.
#
# Use of Altair’s trademarks, including but not limited to "PBS™",
# "PBS Professional®", and "PBS Pro™" and Altair’s logos is subject to Altair's
# trademark licensing policies.
from tests.functional import *
class TestPythonRestartSettings(TestFunctional):
"""
For addressing memory leak in server due to python objects python
interpreter needs to be restarted. Previously there were macros in
code to do that. The new design has added attributes in server to
configure how frequently python interpreter should be restarted
This test suite is to validate the server attributes. Actual memory
leak test is still manual
"""
def test_non_integer(self):
"""
This is to test that qmgr will throw error when non-integer
values are provided
"""
exp_err = "Illegal attribute or resource value"
# -1 will throw error
try:
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_hooks': '-1'},
runas=ROOT_USER, logerr=True)
except PbsManagerError, e:
self.assertTrue(exp_err in e.msg[0],
"Error message is not expected")
try:
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_objects': '-1'},
runas=ROOT_USER, logerr=True)
except PbsManagerError, e:
self.assertTrue(exp_err in e.msg[0],
"Error message is not expected")
try:
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_min_interval': '-1'},
runas=ROOT_USER, logerr=True)
except PbsManagerError, e:
self.assertTrue(exp_err in e.msg[0],
"Error message is not expected")
# 0 will also give error
try:
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_hooks': 0},
runas=ROOT_USER, logerr=True)
except PbsManagerError, e:
self.assertTrue(exp_err in e.msg[0],
"Error message is not expected")
try:
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_objects': 0},
runas=ROOT_USER, logerr=True)
except PbsManagerError, e:
self.assertTrue(exp_err in e.msg[0],
"Error message is not expected")
try:
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_min_interval': 0},
runas=ROOT_USER, logerr=True)
except PbsManagerError, e:
self.assertTrue(exp_err in e.msg[0],
"Error message is not expected")
try:
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_min_interval': "00:00:00"},
runas=ROOT_USER, logerr=True)
except PbsManagerError, e:
self.assertTrue(exp_err in e.msg[0],
"Error message is not expected")
try:
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_min_interval': "HH:MM:SS"},
runas=ROOT_USER, logerr=True)
except PbsManagerError, e:
self.assertTrue(exp_err in e.msg[0],
"Error message is not expected")
def test_non_manager(self):
"""
Test that values are not set as operator or users
"""
exp_err = "Unauthorized Request"
try:
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_hooks': 30},
runas=OPER_USER, logerr=True)
except PbsManagerError, e:
self.assertTrue(exp_err in e.msg[0],
"Error message is not expected")
try:
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_objects': 2000},
runas=OPER_USER, logerr=True)
except PbsManagerError, e:
self.assertTrue(exp_err in e.msg[0],
"Error message is not expected")
try:
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_min_interval': 10},
runas=OPER_USER, logerr=True)
except PbsManagerError, e:
self.assertTrue(exp_err in e.msg[0],
"Error message is not expected")
try:
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_hooks': 30},
runas=TEST_USER, logerr=True)
except PbsManagerError, e:
self.assertTrue(exp_err in e.msg[0],
"Error message is not expected")
try:
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_objects': 2000},
runas=TEST_USER, logerr=True)
except PbsManagerError, e:
self.assertTrue(exp_err in e.msg[0],
"Error message is not expected")
try:
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_min_interval': 10},
runas=TEST_USER, logerr=True)
except PbsManagerError, e:
self.assertTrue(exp_err in e.msg[0],
"Error message is not expected")
def test_log_message(self):
"""
Test that message logged in server_logs when values get set
"""
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_hooks': 200},
runas=ROOT_USER, logerr=True)
self.server.log_match("python_restart_max_hooks = 200",
max_attempts=5)
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_objects': 2000},
runas=ROOT_USER, logerr=True)
self.server.log_match("python_restart_max_objects = 2000",
max_attempts=5)
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_min_interval': "00:01:00"},
runas=ROOT_USER, logerr=True)
self.server.log_match("python_restart_min_interval = 00:01:00",
max_attempts=5)
def test_long_values(self):
"""
Test that very long values are accepted
"""
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_hooks': 2147483647},
runas=ROOT_USER, logerr=True)
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_objects': 2147483647},
runas=ROOT_USER, logerr=True)
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_min_interval': 2147483647},
runas=ROOT_USER, logerr=True)
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_min_interval': "596523:00:00"},
runas=ROOT_USER, logerr=True)
def test_set_unset(self):
"""
Test that when unset attribte is not visible in qmgr.
Also values will not change after server restart.
"""
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_hooks': 20},
runas=ROOT_USER, logerr=True)
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_objects': 20},
runas=ROOT_USER, logerr=True)
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_min_interval': "00:00:20"},
runas=ROOT_USER, logerr=True)
# Restart server
self.server.restart()
self.server.expect(SERVER, {'python_restart_max_hooks': 20},
op=SET, runas=ROOT_USER)
self.server.expect(SERVER, {'python_restart_max_objects': 20},
op=SET, runas=ROOT_USER)
self.server.expect(SERVER, {'python_restart_min_interval': 20},
op=SET, runas=ROOT_USER)
self.server.manager(MGR_CMD_UNSET, SERVER,
'python_restart_max_hooks',
runas=ROOT_USER, logerr=True)
self.server.manager(MGR_CMD_UNSET, SERVER,
'python_restart_max_objects',
runas=ROOT_USER, logerr=True)
self.server.manager(MGR_CMD_UNSET, SERVER,
'python_restart_min_interval',
runas=ROOT_USER, logerr=True)
# Restart server again
self.server.restart()
self.server.expect(SERVER, "python_restart_max_hooks",
op=UNSET, runas=ROOT_USER)
self.server.expect(SERVER, "python_restart_max_objects",
op=UNSET, runas=ROOT_USER)
self.server.expect(SERVER, "python_restart_min_interval",
op=UNSET, runas=ROOT_USER)
def test_max_hooks(self):
"""
Test that python restarts at set interval
"""
# create a hook
hook_body = """
import pbs
e = pbs.event()
s = pbs.server()
localnode = pbs.get_local_nodename()
vn = pbs.server().vnode(localnode)
pbs.event().accept()
"""
a = {'event': ["queuejob", "movejob", "modifyjob", "runjob"],
'enabled': "True"}
self.server.create_import_hook("test", a, hook_body, overwrite=True)
return
# Create workq2
a = {'queue_type': 'e', 'started': 't', 'enabled': 't'}
self.server.manager(MGR_CMD_CREATE, QUEUE, a, "workq2")
# Set max_hooks and min_interval so that further changes
# will generate a log message.
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_hooks': 100},
runas=ROOT_USER)
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_min_interval': 30},
runas=ROOT_USER)
# Need to run a job so these new settings are remembered
j = Job()
jid = self.server.submit(j)
# Set server log_events
self.server.manager(MGR_CMD_SET, SERVER, {"log_events": 2047})
# Set time to start scanning logs
time.sleep(1)
stime = int(time.time())
# Set max_hooks to low to hit max_hooks only
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_hooks': 1},
runas=ROOT_USER)
# Set min_interval to 3
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_min_interval': 3},
runas=ROOT_USER)
# Submit multiple jobs
for x in range(6):
j = Job()
j.set_attributes({ATTR_h: None})
j.set_sleep_time(1)
jid = self.server.submit(j)
self.server.expect(JOB, {'job_state': "H"}, id=jid)
self.server.alterjob(jid, {ATTR_N: "yaya"})
self.server.movejob(jid, "workq2")
self.server.rlsjob(jid, None)
time.sleep(1)
# Verify the logs and make sure that python interpreter is restarted
# every 3s
logs = self.server.log_match(
"Restarting Python interpreter to reduce mem usage",
allmatch=True, starttime=stime, max_attempts=8)
self.assertTrue(len(logs) > 1)
log1 = logs[0][1]
log2 = logs[1][1]
pattern = '%m/%d/%Y %H:%M:%S'
tmp = log1.split(';')
# Convert the time into epoch time
time1 = int(time.mktime(time.strptime(tmp[0], pattern)))
tmp = log2.split(';')
time2 = int(time.mktime(time.strptime(tmp[0], pattern)))
# Difference between log message should not be less than 3
diff = time2 - time1
self.logger.info("Time difference between log message is " +
str(diff) + " seconds")
# Leave a little wiggle room for slow systems
self.assertTrue(diff >= 3 and diff <= 5)
# This message only gets printed if /proc/self/statm is present
if os.path.isfile("/proc/self/statm"):
self.server.log_match("Current memory usage:",
starttime=self.server.ctime,
max_attempts=5)
else:
self.server.log_match("unknown", max_attempts=5)
# Verify other log messages
self.server.log_match("python_restart_max_hooks is now 1",
starttime=stime, max_attempts=5)
self.server.log_match("python_restart_min_interval is now 3",
starttime=stime, max_attempts=5)
def test_max_objects(self):
"""
Test that python restarts if max objects limit have met
"""
hook_body = """
import pbs
pbs.event().accept()
"""
a = {'event': ["queuejob", "modifyjob"], 'enabled': 'True'}
self.server.create_import_hook("test", a, hook_body, overwrite=True)
# Set max_objects and min_interval so that further changes
# will generate a log message.
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_objects': 1000},
runas=ROOT_USER)
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_min_interval': 30},
runas=ROOT_USER)
# Need to run a job so these new settings are remembered
j = Job()
jid = self.server.submit(j)
# Set server log_events
self.server.manager(MGR_CMD_SET, SERVER, {"log_events": 2047})
# Set time to start scanning logs
time.sleep(1)
stime = int(time.time())
# Set max_objects only
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_objects': 1},
runas=ROOT_USER)
# Set min_interval to 1
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_min_interval': '00:00:01'},
runas=ROOT_USER)
# Submit held jobs
for x in range(3):
j = Job()
j.set_attributes({ATTR_h: None})
j.set_sleep_time(1)
jid = self.server.submit(j)
self.server.expect(JOB, {'job_state': "H"}, id=jid)
self.server.alterjob(jid, {ATTR_N: "yaya"})
# Verify that python is restarted
self.server.log_match(
"Restarting Python interpreter to reduce mem usage",
starttime=self.server.ctime, max_attempts=5)
# This message only gets printed if
# /proc/self/statm presents
if os.path.isfile("/proc/self/statm"):
self.server.log_match(
"Current memory usage:",
starttime=self.server.ctime, max_attempts=5)
else:
self.server.log_match("unknown", max_attempts=5)
# Verify other log messages
self.server.log_match(
"python_restart_max_objects is now 1",
starttime=stime, max_attempts=5)
self.server.log_match(
"python_restart_min_interval is now 1",
starttime=stime, max_attempts=5)
def test_no_restart(self):
"""
Test that if limit not reached then python interpreter
will not be started
"""
hook_body = """
import pbs
pbs.event().accept()
"""
a = {'event': "queuejob", 'enabled': "True"}
self.server.create_import_hook("test", a, hook_body, overwrite=True)
# Set max_hooks, max_objects, and min_interval to large values
# to avoid restarting the Python interpreter.
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_hooks': 10000},
runas=ROOT_USER)
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_max_objects': 10000},
runas=ROOT_USER)
self.server.manager(MGR_CMD_SET, SERVER,
{'python_restart_min_interval': 10000},
runas=ROOT_USER)
stime = time.time()
# Submit a job
for x in range(10):
j = Job()
j.set_sleep_time(1)
jid = self.server.submit(j)
# Verify no restart message
msg = "Restarting Python interpreter to reduce mem usage"
self.server.log_match(msg, starttime=stime, max_attempts=8,
existence=False)