Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: appengine/swarming/server/lease_management.py

Issue 2402543002: Create a BotEvent when a bot is leased from Machine Provider (Closed)
Patch Set: Fix tests Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « appengine/swarming/server/bot_management_test.py ('k') | appengine/swarming/swarming_rpcs.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2016 The LUCI Authors. All rights reserved. 1 # Copyright 2016 The LUCI Authors. All rights reserved.
2 # Use of this source code is governed under the Apache License, Version 2.0 2 # Use of this source code is governed under the Apache License, Version 2.0
3 # that can be found in the LICENSE file. 3 # that can be found in the LICENSE file.
4 4
5 """Lease management for machines leased from the Machine Provider. 5 """Lease management for machines leased from the Machine Provider.
6 6
7 Keeps a list of machine types which should be leased from the Machine Provider 7 Keeps a list of machine types which should be leased from the Machine Provider
8 and the list of machines of each type currently leased. 8 and the list of machines of each type currently leased.
9 9
10 Swarming integration with Machine Provider 10 Swarming integration with Machine Provider
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 # Request ID used to generate this request. 64 # Request ID used to generate this request.
65 client_request_id = ndb.StringProperty(indexed=True) 65 client_request_id = ndb.StringProperty(indexed=True)
66 # Whether or not this MachineLease should issue lease requests. 66 # Whether or not this MachineLease should issue lease requests.
67 drained = ndb.BooleanProperty(indexed=True) 67 drained = ndb.BooleanProperty(indexed=True)
68 # Hostname of the machine currently allocated for this request. 68 # Hostname of the machine currently allocated for this request.
69 hostname = ndb.StringProperty() 69 hostname = ndb.StringProperty()
70 # Duration to lease for. 70 # Duration to lease for.
71 lease_duration_secs = ndb.IntegerProperty(indexed=False) 71 lease_duration_secs = ndb.IntegerProperty(indexed=False)
72 # DateTime indicating lease expiration time. 72 # DateTime indicating lease expiration time.
73 lease_expiration_ts = ndb.DateTimeProperty() 73 lease_expiration_ts = ndb.DateTimeProperty()
74 # Lease ID assigned by Machine Provider.
75 lease_id = ndb.StringProperty(indexed=False)
74 # ndb.Key for the MachineType this MachineLease is created for. 76 # ndb.Key for the MachineType this MachineLease is created for.
75 machine_type = ndb.KeyProperty() 77 machine_type = ndb.KeyProperty()
76 # machine_provider.Dimensions describing the machine. 78 # machine_provider.Dimensions describing the machine.
77 mp_dimensions = msgprop.MessageProperty( 79 mp_dimensions = msgprop.MessageProperty(
78 machine_provider.Dimensions, indexed=False) 80 machine_provider.Dimensions, indexed=False)
79 # Last request number used. 81 # Last request number used.
80 request_count = ndb.IntegerProperty(default=0, required=True) 82 request_count = ndb.IntegerProperty(default=0, required=True)
81 83
82 84
83 class MachineType(ndb.Model): 85 class MachineType(ndb.Model):
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 ) 226 )
225 return 227 return
226 228
227 machine_lease.client_request_id = None 229 machine_lease.client_request_id = None
228 machine_lease.hostname = None 230 machine_lease.hostname = None
229 machine_lease.lease_expiration_ts = None 231 machine_lease.lease_expiration_ts = None
230 machine_lease.put() 232 machine_lease.put()
231 233
232 234
233 @ndb.transactional 235 @ndb.transactional
234 def log_lease_fulfillment(key, request_id, hostname, lease_expiration_ts): 236 def log_lease_fulfillment(
237 key, request_id, hostname, lease_expiration_ts, lease_id):
235 """Logs lease fulfillment. 238 """Logs lease fulfillment.
236 239
237 Args: 240 Args:
238 request_id: ID of the request being fulfilled. 241 request_id: ID of the request being fulfilled.
239 hostname: Hostname of the machine fulfilling the request. 242 hostname: Hostname of the machine fulfilling the request.
240 lease_expiration_ts: UTC seconds since epoch when the lease expires. 243 lease_expiration_ts: UTC seconds since epoch when the lease expires.
244 lease_id: ID of the lease assigned by Machine Provider.
241 """ 245 """
242 machine_lease = key.get() 246 machine_lease = key.get()
243 if not machine_lease: 247 if not machine_lease:
244 logging.error('MachineLease does not exist\nKey: %s', key) 248 logging.error('MachineLease does not exist\nKey: %s', key)
245 return 249 return
246 250
247 if request_id != machine_lease.client_request_id: 251 if request_id != machine_lease.client_request_id:
248 logging.error( 252 logging.error(
249 'Request ID mismatch\nKey: %s\nExpected: %s\nActual: %s', 253 'Request ID mismatch\nKey: %s\nExpected: %s\nActual: %s',
250 key, 254 key,
251 machine_lease.client_request_id, 255 machine_lease.client_request_id,
252 request_id, 256 request_id,
253 ) 257 )
254 return 258 return
255 259
256 if (hostname == machine_lease.hostname 260 if (hostname == machine_lease.hostname
257 and lease_expiration_ts == machine_lease.lease_expiration_ts): 261 and lease_expiration_ts == machine_lease.lease_expiration_ts
262 and lease_id == machine_lease.lease_id):
258 return 263 return
259 264
260 machine_lease.hostname = hostname 265 machine_lease.hostname = hostname
261 machine_lease.lease_expiration_ts = datetime.datetime.utcfromtimestamp( 266 machine_lease.lease_expiration_ts = datetime.datetime.utcfromtimestamp(
262 lease_expiration_ts) 267 lease_expiration_ts)
268 machine_lease.lease_id = lease_id
263 machine_lease.put() 269 machine_lease.put()
264 270
265 271
266 @ndb.transactional 272 @ndb.transactional
267 def update_client_request_id(key): 273 def update_client_request_id(key):
268 """Sets the client request ID used to lease a machine. 274 """Sets the client request ID used to lease a machine.
269 275
270 Args: 276 Args:
271 key: ndb.Key for a MachineLease entity. 277 key: ndb.Key for a MachineLease entity.
272 """ 278 """
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 machine_lease.key, 369 machine_lease.key,
364 machine_lease.client_request_id, 370 machine_lease.client_request_id,
365 response['hostname'], 371 response['hostname'],
366 response['lease_expiration_ts'], 372 response['lease_expiration_ts'],
367 ) 373 )
368 log_lease_fulfillment( 374 log_lease_fulfillment(
369 machine_lease.key, 375 machine_lease.key,
370 machine_lease.client_request_id, 376 machine_lease.client_request_id,
371 response['hostname'], 377 response['hostname'],
372 int(response['lease_expiration_ts']), 378 int(response['lease_expiration_ts']),
379 response['request_hash'],
373 ) 380 )
374 # TODO(smut): Create BotInfo, associate lease information.
375 elif state == machine_provider.LeaseRequestState.DENIED: 381 elif state == machine_provider.LeaseRequestState.DENIED:
376 logging.warning( 382 logging.warning(
377 'Request denied: %s\nRequest ID: %s', 383 'Request denied: %s\nRequest ID: %s',
378 machine_lease.key, 384 machine_lease.key,
379 machine_lease.client_request_id, 385 machine_lease.client_request_id,
380 ) 386 )
381 clear_lease_request(machine_lease.key, machine_lease.client_request_id) 387 clear_lease_request(machine_lease.key, machine_lease.client_request_id)
382 388
383 389
384 def manage_pending_lease_request(machine_lease): 390 def manage_pending_lease_request(machine_lease):
(...skipping 28 matching lines...) Expand all
413 419
414 Args: 420 Args:
415 key: ndb.Key for a MachineLease entity. 421 key: ndb.Key for a MachineLease entity.
416 """ 422 """
417 machine_lease = key.get() 423 machine_lease = key.get()
418 if not machine_lease: 424 if not machine_lease:
419 return 425 return
420 426
421 # Manage a leased machine. 427 # Manage a leased machine.
422 if machine_lease.lease_expiration_ts: 428 if machine_lease.lease_expiration_ts:
429 assert machine_lease.hostname, key
430 bot_info = bot_management.get_info_key(machine_lease.hostname).get()
431 if not (bot_info and bot_info.lease_id and bot_info.lease_expiration_ts):
432 bot_management.bot_event(
433 event_type='bot_leased',
434 bot_id=machine_lease.hostname,
435 external_ip=None,
436 authenticated_as=None,
437 dimensions=None,
438 state=None,
439 version=None,
440 quarantined=False,
441 task_id='',
442 task_name=None,
443 lease_id=machine_lease.lease_id,
444 lease_expiration_ts=machine_lease.lease_expiration_ts,
445 )
446
423 if machine_lease.lease_expiration_ts <= utils.utcnow(): 447 if machine_lease.lease_expiration_ts <= utils.utcnow():
424 logging.info('MachineLease expired: %s', key) 448 logging.info('MachineLease expired: %s', key)
425 assert machine_lease.hostname, key
426 clear_lease_request(key, machine_lease.client_request_id) 449 clear_lease_request(key, machine_lease.client_request_id)
427 return 450 return
428 451
429 # Lease expiration time is unknown, so there must be no leased machine. 452 # Lease expiration time is unknown, so there must be no leased machine.
430 assert not machine_lease.hostname, key 453 assert not machine_lease.hostname, key
431 454
432 # Manage a pending lease request. 455 # Manage a pending lease request.
433 if machine_lease.client_request_id: 456 if machine_lease.client_request_id:
434 manage_pending_lease_request(machine_lease) 457 manage_pending_lease_request(machine_lease)
435 return 458 return
436 459
437 # Manage an uninitiated lease request. 460 # Manage an uninitiated lease request.
438 if not machine_lease.drained: 461 if not machine_lease.drained:
439 update_client_request_id(key) 462 update_client_request_id(key)
440 return 463 return
441 464
442 # Manage an uninitiated, drained lease request. 465 # Manage an uninitiated, drained lease request.
443 delete_machine_lease(key) 466 delete_machine_lease(key)
OLDNEW
« no previous file with comments | « appengine/swarming/server/bot_management_test.py ('k') | appengine/swarming/swarming_rpcs.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698