Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# Copyright (C) 2018 Bloomberg LP 

# 

# Licensed under the Apache License, Version 2.0 (the "License"); 

# you may not use this file except in compliance with the License. 

# You may obtain a copy of the License at 

# 

# <http://www.apache.org/licenses/LICENSE-2.0> 

# 

# Unless required by applicable law or agreed to in writing, software 

# distributed under the License is distributed on an "AS IS" BASIS, 

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

# See the License for the specific language governing permissions and 

# limitations under the License. 

 

 

""" 

Scheduler 

========= 

Schedules jobs. 

""" 

 

import bisect 

from datetime import timedelta 

import logging 

from threading import Lock 

 

from buildgrid._enums import LeaseState, OperationStage 

from buildgrid._exceptions import NotFoundError 

from buildgrid.server.job import Job 

from buildgrid.utils import BrowserURL 

 

 

class Scheduler: 

 

MAX_N_TRIES = 5 

 

def __init__(self, data_store, action_cache=None, action_browser_url=False, monitor=False): 

self.__logger = logging.getLogger(__name__) 

 

self._instance_name = None 

 

self.__build_metadata_queues = None 

 

self.__queue_time_average = None 

self.__retries_count = 0 

 

self._action_cache = action_cache 

self._action_browser_url = action_browser_url 

 

self.__operation_lock = Lock() # Lock protecting deletion, addition and updating of jobs 

 

self.__operations_by_peer = {} 

self.__peer_message_queues = {} 

 

self.data_store = data_store 

 

self._is_instrumented = False 

if monitor: 

self.activate_monitoring() 

 

# --- Public API --- 

 

@property 

def instance_name(self): 

return self._instance_name 

 

def set_instance_name(self, instance_name): 

if not self._instance_name: 

self._instance_name = instance_name 

 

def list_current_jobs(self): 

"""Returns a list of the :class:`Job` names currently managed.""" 

jobs = self.data_store.get_all_jobs() 

return [job.name for job in jobs] 

 

def list_job_operations(self, job_name): 

"""Returns a list of :class:`Operation` names for a :class:`Job`.""" 

job = self.data_store.get_job_by_name(job_name) 

if job is not None: 

return job.list_operations() 

return [] 

 

# --- Public API: REAPI --- 

 

def register_job_peer(self, job_name, peer, message_queue): 

"""Subscribes to the job's :class:`Operation` stage changes. 

 

Args: 

job_name (str): name of the job to subscribe to. 

peer (str): a unique string identifying the client. 

message_queue (queue.Queue): the event queue to register. 

 

Returns: 

str: The name of the subscribed :class:`Operation`. 

 

Raises: 

NotFoundError: If no job with `job_name` exists. 

""" 

with self.__operation_lock: 

job = self.data_store.get_job_by_name(job_name) 

 

if job is None: 

raise NotFoundError("Job name does not exist: [{}]".format(job_name)) 

 

operation_name = job.register_new_operation_peer( 

peer, message_queue, self.__operations_by_peer, self.__peer_message_queues) 

 

return operation_name 

 

def register_job_operation_peer(self, operation_name, peer, message_queue): 

"""Subscribes to an existing the job's :class:`Operation` stage changes. 

 

Args: 

operation_name (str): name of the operation to subscribe to. 

peer (str): a unique string identifying the client. 

message_queue (queue.Queue): the event queue to register. 

 

Returns: 

str: The name of the subscribed :class:`Operation`. 

 

Raises: 

NotFoundError: If no operation with `operation_name` exists. 

""" 

with self.__operation_lock: 

job = self.data_store.get_job_by_operation(operation_name) 

 

if job is None: 

raise NotFoundError("Operation name does not exist: [{}]" 

.format(operation_name)) 

 

job.register_operation_peer(operation_name, 

peer, 

message_queue, 

self.__operations_by_peer, 

self.__peer_message_queues) 

 

def unregister_job_operation_peer(self, operation_name, peer): 

"""Unsubscribes to one of the job's :class:`Operation` stage change. 

 

Args: 

operation_name (str): name of the operation to unsubscribe from. 

peer (str): a unique string identifying the client. 

 

Raises: 

NotFoundError: If no operation with `operation_name` exists. 

""" 

with self.__operation_lock: 

job = self.data_store.get_job_by_operation(operation_name) 

 

if job is None: 

raise NotFoundError("Operation name does not exist: [{}]" 

.format(operation_name)) 

 

job.unregister_operation_peer(operation_name, peer) 

self.__operations_by_peer[peer].remove(operation_name) 

self.__peer_message_queues[peer].pop(operation_name) 

 

if not job.n_peers_for_operation(operation_name, self.__operations_by_peer): 

self.data_store.delete_operation(operation_name) 

 

if not job.n_peers(self.__operations_by_peer) and job.done and not job.lease: 

self.data_store.delete_job(job.name) 

 

def queue_job_action(self, action, action_digest, platform_requirements=None, 

priority=0, skip_cache_lookup=False): 

"""Inserts a newly created job into the execution queue. 

 

Warning: 

Priority is handle like a POSIX ``nice`` values: a higher value 

means a low priority, 0 being default priority. 

 

Args: 

action (Action): the given action to queue for execution. 

action_digest (Digest): the digest of the given action. 

platform_requirements (dict(set)): platform attributes that a worker 

must satisfy in order to be assigned the job. (Each key can 

have multiple values.) 

priority (int): the execution job's priority. 

skip_cache_lookup (bool): whether or not to look for pre-computed 

result for the given action. 

 

Returns: 

str: the newly created job's name. 

""" 

job = self.data_store.get_job_by_action(action_digest) 

if job is not None and not action.do_not_cache: 

# If existing job has been cancelled or isn't 

# cacheable, create a new one. 

if not job.cancelled and not job.do_not_cache: 

# Reschedule if priority is now greater: 

if priority < job.priority: 

job.priority = priority 

 

if job.operation_stage == OperationStage.QUEUED: 

self.data_store.queue_job(job.name) 

 

self.__logger.debug("Job deduplicated for action [%s]: [%s]", 

action_digest.hash[:8], job.name) 

 

return job.name 

 

job = Job(self.data_store, action.do_not_cache, action_digest, 

platform_requirements=platform_requirements, 

priority=priority) 

self.data_store.create_job(job) 

 

if self._action_browser_url: 

job.set_action_url( 

BrowserURL(self._action_browser_url, self._instance_name)) 

 

self.__logger.debug("Job created for action [%s]: [%s]", 

action_digest.hash[:8], job.name) 

 

operation_stage = None 

 

if self._action_cache is not None and not skip_cache_lookup: 

try: 

action_result = self._action_cache.get_action_result(job.action_digest) 

 

self.__logger.debug("Job cache hit for action [%s]: [%s]", 

action_digest.hash[:8], job.name) 

 

operation_stage = OperationStage.COMPLETED 

job.set_cached_result(action_result) 

 

except NotFoundError: 

operation_stage = OperationStage.QUEUED 

self.data_store.queue_job(job.name) 

 

else: 

operation_stage = OperationStage.QUEUED 

self.data_store.queue_job(job.name) 

 

self._update_job_operation_stage(job.name, operation_stage) 

 

return job.name 

 

def get_job_operation(self, operation_name): 

"""Retrieves a job's :class:`Operation` by name. 

 

Args: 

operation_name (str): name of the operation to query. 

 

Raises: 

NotFoundError: If no operation with `operation_name` exists. 

""" 

job = self.data_store.get_job_by_operation(operation_name) 

 

if job is None: 

raise NotFoundError("Operation name does not exist: [{}]" 

.format(operation_name)) 

 

return job.get_operation(operation_name) 

 

def cancel_job_operation(self, operation_name): 

""""Cancels a job's :class:`Operation` by name. 

 

Args: 

operation_name (str): name of the operation to cancel. 

 

Raises: 

NotFoundError: If no operation with `operation_name` exists. 

""" 

job = self.data_store.get_job_by_operation(operation_name) 

 

if job is None: 

raise NotFoundError("Operation name does not exist: [{}]" 

.format(operation_name)) 

 

job.cancel_operation( 

operation_name, self.__operations_by_peer, self.__peer_message_queues) 

 

def delete_job_operation(self, operation_name): 

""""Removes a job. 

 

Args: 

operation_name (str): name of the operation to delete. 

 

Raises: 

NotFoundError: If no operation with `operation_name` exists. 

""" 

with self.__operation_lock: 

job = self.data_store.get_job_by_operation(operation_name) 

 

if job is None: 

raise NotFoundError("Operation name does not exist: [{}]" 

.format(operation_name)) 

if not job.n_peers(self.__operations_by_peer) and job.done and not job.lease: 

self.data_store.delete_job(job.name) 

 

# --- Public API: RWAPI --- 

 

def request_job_leases(self, worker_capabilities, timeout=None, worker_name=None, bot_id=None): 

"""Generates a list of the highest priority leases to be run. 

 

Args: 

worker_capabilities (dict): a set of key-value pairs describing the 

worker properties, configuration and state at the time of the 

request. 

timeout (int): time to block waiting on job queue, caps if longer 

than MAX_JOB_BLOCK_TIME 

worker_name (string): name of the worker requesting the leases. 

""" 

def assign_lease(job): 

self.__logger.info("Job scheduled to run: [%s]", job.name) 

 

lease = job.lease 

 

if not lease: 

# For now, one lease at a time: 

lease = job.create_lease(worker_name, bot_id) 

 

if lease: 

job.mark_worker_started() 

return [lease] 

return [] 

 

leases = self.data_store.assign_lease_for_next_job( 

worker_capabilities, assign_lease, timeout=timeout) 

if leases: 

# Update the leases outside of the callback to avoid nested data_store operations 

for lease in leases: 

# The lease id and job names are the same, so use that as the job name 

self._update_job_operation_stage(lease.id, OperationStage.EXECUTING) 

return leases 

 

def update_job_lease_state(self, job_name, lease): 

"""Requests a state transition for a job's current :class:Lease. 

 

Note: 

This may trigger a job's :class:`Operation` stage transition. 

 

Args: 

job_name (str): name of the job to update lease state from. 

lease (Lease): the lease holding the new state. 

 

Raises: 

NotFoundError: If no job with `job_name` exists. 

""" 

job = self.data_store.get_job_by_name(job_name) 

 

if job is None: 

raise NotFoundError("Job name does not exist: [{}]".format(job_name)) 

 

lease_state = LeaseState(lease.state) 

 

operation_stage = None 

if lease_state == LeaseState.PENDING: 

job.update_lease_state(LeaseState.PENDING) 

operation_stage = OperationStage.QUEUED 

 

elif lease_state == LeaseState.ACTIVE: 

job.update_lease_state(LeaseState.ACTIVE) 

operation_stage = OperationStage.EXECUTING 

 

elif lease_state == LeaseState.COMPLETED: 

job.update_lease_state(LeaseState.COMPLETED, 

status=lease.status, result=lease.result) 

 

if (self._action_cache is not None and 

self._action_cache.allow_updates and not job.do_not_cache): 

self._action_cache.update_action_result(job.action_digest, job.action_result) 

self.data_store.store_response(job) 

 

operation_stage = OperationStage.COMPLETED 

 

self._update_job_operation_stage(job_name, operation_stage) 

 

def retry_job_lease(self, job_name): 

"""Re-queues a job on lease execution failure. 

 

Note: 

This may trigger a job's :class:`Operation` stage transition. 

 

Args: 

job_name (str): name of the job to retry the lease from. 

 

Raises: 

NotFoundError: If no job with `job_name` exists. 

""" 

job = self.data_store.get_job_by_name(job_name) 

 

if job is None: 

raise NotFoundError("Job name does not exist: [{}]".format(job_name)) 

 

operation_stage = None 

if job.n_tries >= self.MAX_N_TRIES: 

# TODO: Decide what to do with these jobs 

operation_stage = OperationStage.COMPLETED 

# TODO: Mark these jobs as done 

 

else: 

operation_stage = OperationStage.QUEUED 

self.data_store.queue_job(job.name) 

 

job.update_lease_state(LeaseState.PENDING) 

 

if self._is_instrumented: 

self.__retries_count += 1 

 

self._update_job_operation_stage(job_name, operation_stage) 

 

def get_job_lease(self, job_name): 

"""Returns the lease associated to job, if any have been emitted yet. 

 

Args: 

job_name (str): name of the job to query the lease from. 

 

Raises: 

NotFoundError: If no job with `job_name` exists. 

""" 

job = self.data_store.get_job_by_name(job_name) 

 

if job is None: 

raise NotFoundError("Job name does not exist: [{}]".format(job_name)) 

 

return job.lease 

 

def delete_job_lease(self, job_name): 

"""Discards the lease associated with a job. 

 

Args: 

job_name (str): name of the job to delete the lease from. 

 

Raises: 

NotFoundError: If no job with `job_name` exists. 

""" 

with self.__operation_lock: 

job = self.data_store.get_job_by_name(job_name) 

 

if job is None: 

raise NotFoundError("Job name does not exist: [{}]".format(job_name)) 

 

job.delete_lease() 

 

if not job.n_peers(self.__operations_by_peer) and job.done: 

self.data_store.delete_job(job.name) 

 

def get_job_lease_cancelled(self, job_name): 

"""Returns true if the lease is cancelled. 

 

Args: 

job_name (str): name of the job to query the lease state from. 

 

Raises: 

NotFoundError: If no job with `job_name` exists. 

""" 

job = self.data_store.get_job_by_name(job_name) 

 

if job is None: 

raise NotFoundError("Job name does not exist: [{}]".format(job_name)) 

 

return job.lease_cancelled 

 

# --- Public API: Monitoring --- 

 

@property 

def is_instrumented(self): 

return self._is_instrumented 

 

def activate_monitoring(self): 

"""Activated jobs monitoring.""" 

if self._is_instrumented: 

return 

 

self.__build_metadata_queues = [] 

 

self.__queue_time_average = 0, timedelta() 

self.__retries_count = 0 

 

self._is_instrumented = True 

 

def deactivate_monitoring(self): 

"""Deactivated jobs monitoring.""" 

if not self._is_instrumented: 

return 

 

self._is_instrumented = False 

 

self.__build_metadata_queues = None 

 

self.__queue_time_average = None 

self.__retries_count = 0 

 

def register_build_metadata_watcher(self, message_queue): 

if self.__build_metadata_queues is not None: 

self.__build_metadata_queues.append(message_queue) 

 

def query_n_jobs(self): 

return len(self.data_store.get_all_jobs()) 

 

def query_n_operations(self): 

# For now n_operations == n_jobs: 

return len(self.data_store.get_all_operations()) 

 

def query_n_operations_by_stage(self, operation_stage): 

return len(self.data_store.get_operations_by_stage(operation_stage)) 

 

def query_n_leases(self): 

return len(self.data_store.get_all_jobs()) 

 

def query_n_leases_by_state(self, lease_state): 

return len(self.data_store.get_leases_by_state(lease_state)) 

 

def query_n_retries(self): 

return self.__retries_count 

 

def query_am_queue_time(self): 

if self.__queue_time_average is not None: 

return self.__queue_time_average[1] 

return timedelta() 

 

# --- Private API --- 

 

def _update_job_operation_stage(self, job_name, operation_stage): 

"""Requests a stage transition for the job's :class:Operations. 

 

Args: 

job_name (str): name of the job to query. 

operation_stage (OperationStage): the stage to transition to. 

""" 

with self.__operation_lock: 

job = self.data_store.get_job_by_name(job_name) 

 

if operation_stage == OperationStage.CACHE_CHECK: 

job.update_operation_stage(OperationStage.CACHE_CHECK, 

self.__operations_by_peer, 

self.__peer_message_queues) 

 

elif operation_stage == OperationStage.QUEUED: 

job.update_operation_stage(OperationStage.QUEUED, 

self.__operations_by_peer, 

self.__peer_message_queues) 

 

elif operation_stage == OperationStage.EXECUTING: 

job.update_operation_stage(OperationStage.EXECUTING, 

self.__operations_by_peer, 

self.__peer_message_queues) 

 

elif operation_stage == OperationStage.COMPLETED: 

job.update_operation_stage(OperationStage.COMPLETED, 

self.__operations_by_peer, 

self.__peer_message_queues) 

 

if self._is_instrumented: 

average_order, average_time = self.__queue_time_average 

 

average_order += 1 

if average_order <= 1: 

average_time = job.query_queue_time() 

else: 

queue_time = job.query_queue_time() 

average_time = average_time + ((queue_time - average_time) / average_order) 

 

self.__queue_time_average = average_order, average_time 

 

if not job.holds_cached_result: 

execution_metadata = job.action_result.execution_metadata 

context_metadata = {'job-is': job.name} 

 

message = (execution_metadata, context_metadata,) 

 

for message_queue in self.__build_metadata_queues: 

message_queue.put(message)