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

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

# 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. 

 

 

from datetime import datetime 

import logging 

import uuid 

 

from google.protobuf.duration_pb2 import Duration 

from google.protobuf.timestamp_pb2 import Timestamp 

 

from buildgrid._enums import LeaseState, OperationStage, BotStatus 

from buildgrid._exceptions import CancelledError, NotFoundError 

from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2 

from buildgrid._protos.google.devtools.remoteworkers.v1test2 import bots_pb2 

from buildgrid._protos.google.longrunning import operations_pb2 

from buildgrid._protos.google.rpc import code_pb2 

 

 

class Job: 

 

def __init__(self, data_store, do_not_cache, action_digest, platform_requirements=None, priority=0, 

name=None, operations=(), cancelled_operations=set(), lease=None, 

stage=OperationStage.UNKNOWN.value, cancelled=False, 

queued_timestamp=None, queued_time_duration=None, 

worker_start_timestamp=None, worker_completed_timestamp=None, 

done=False, result=None, worker_name=None, n_tries=0): 

self.__logger = logging.getLogger(__name__) 

 

self._name = name or str(uuid.uuid4()) 

self._priority = priority 

self._lease = lease 

 

self.__execute_response = result 

if result is None: 

self.__execute_response = remote_execution_pb2.ExecuteResponse() 

self.__operation_metadata = remote_execution_pb2.ExecuteOperationMetadata() 

 

self.__queued_timestamp = Timestamp() 

if queued_timestamp is not None: 

self.__queued_timestamp.CopyFrom(queued_timestamp) 

 

self.__queued_time_duration = Duration() 

if queued_time_duration is not None: 

self.__queued_time_duration.CopyFrom(queued_time_duration) 

 

self.__worker_start_timestamp = Timestamp() 

if worker_start_timestamp is not None: 

self.__worker_start_timestamp.CopyFrom(worker_start_timestamp) 

 

self.__worker_completed_timestamp = Timestamp() 

if worker_completed_timestamp is not None: 

self.__worker_completed_timestamp.CopyFrom(worker_completed_timestamp) 

 

self.__operations_by_name = {op.name: op for op in operations} # Name to Operation 1:1 mapping 

self.__operations_cancelled = cancelled_operations 

self.__lease_cancelled = cancelled 

self.__job_cancelled = cancelled 

 

self.__operation_metadata.action_digest.CopyFrom(action_digest) 

self.__operation_metadata.stage = stage 

 

self._do_not_cache = do_not_cache 

self._n_tries = n_tries 

 

self._platform_requirements = platform_requirements \ 

if platform_requirements else dict() 

 

self._done = done 

self.worker_name = worker_name 

 

self.data_store = data_store 

 

def __lt__(self, other): 

try: 

return self.priority < other.priority 

except AttributeError: 

return NotImplemented 

 

def __le__(self, other): 

try: 

return self.priority <= other.priority 

except AttributeError: 

return NotImplemented 

 

def __eq__(self, other): 

if isinstance(other, Job): 

return self.name == other.name 

return False 

 

def __ne__(self, other): 

return not self.__eq__(other) 

 

def __gt__(self, other): 

try: 

return self.priority > other.priority 

except AttributeError: 

return NotImplemented 

 

def __ge__(self, other): 

try: 

return self.priority >= other.priority 

except AttributeError: 

return NotImplemented 

 

# --- Public API --- 

 

@property 

def name(self): 

return self._name 

 

@property 

def cancelled(self): 

return self.__job_cancelled 

 

@property 

def priority(self): 

return self._priority 

 

@priority.setter 

def priority(self, new_priority): 

self._priority = new_priority 

self.data_store.update_job(self.name, {'priority': new_priority}) 

 

@property 

def done(self): 

return self._done 

 

# --- Public API: REAPI --- 

 

@property 

def platform_requirements(self): 

return self._platform_requirements 

 

@property 

def do_not_cache(self): 

return self._do_not_cache 

 

@property 

def action_digest(self): 

return self.__operation_metadata.action_digest 

 

@property 

def operation_stage(self): 

return OperationStage(self.__operation_metadata.stage) 

 

@property 

def action_result(self): 

if self.__execute_response is not None: 

return self.__execute_response.result 

else: 

return None 

 

@property 

def execute_response(self): 

return self.__execute_response 

 

@execute_response.setter 

def execute_response(self, response): 

self.__execute_response = response 

for operation in self.__operations_by_name.values(): 

operation.response.Pack(self.__execute_response) 

 

@property 

def holds_cached_result(self): 

if self.__execute_response is not None: 

return self.__execute_response.cached_result 

else: 

return False 

 

@property 

def queued_timestamp(self): 

return self.__queued_timestamp 

 

@property 

def queued_time_duration(self): 

return self.__queued_time_duration 

 

@property 

def worker_start_timestamp(self): 

return self.__worker_start_timestamp 

 

@property 

def worker_completed_timestamp(self): 

return self.__worker_completed_timestamp 

 

def mark_worker_started(self): 

self.__worker_start_timestamp.GetCurrentTime() 

 

def set_action_url(self, url): 

"""Generates a CAS browser URL for the job's action.""" 

if url.for_message('action', self.__operation_metadata.action_digest): 

self.__execute_response.message = url.generate() 

 

def set_cached_result(self, action_result): 

"""Allows specifying an action result form the action cache for the job. 

 

Note: 

This won't trigger any :class:`Operation` stage transition. 

 

Args: 

action_result (ActionResult): The result from cache. 

""" 

self.__execute_response.result.CopyFrom(action_result) 

self.__execute_response.cached_result = True 

 

def n_peers(self, operations_by_peer): 

return len([peer for peer, names in operations_by_peer.items() 

if any(name in self.__operations_by_name for name in names)]) 

 

def n_peers_for_operation(self, operation_name, operations_by_peer): 

return len([peer for peer, names in operations_by_peer.items() 

if any(name == operation_name for name in names)]) 

 

def register_new_operation_peer(self, peer, message_queue, operations_by_peer, peer_message_queues): 

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

 

Args: 

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`. 

""" 

new_operation = operations_pb2.Operation() 

# Copy state from first existing and non cancelled operation: 

for operation in self.__operations_by_name.values(): 

if operation.name not in self.__operations_cancelled: 

new_operation.CopyFrom(operation) 

break 

 

new_operation.name = str(uuid.uuid4()) 

 

self.__logger.debug("Operation created for job [%s]: [%s]", 

self._name, new_operation.name) 

 

self.__operations_by_name[new_operation.name] = new_operation 

if peer in operations_by_peer: 

operations_by_peer[peer].add(new_operation.name) 

else: 

operations_by_peer[peer] = set([new_operation.name]) 

 

if peer in peer_message_queues: 

peer_message_queues[peer][new_operation.name] = message_queue 

else: 

peer_message_queues[peer] = {new_operation.name: message_queue} 

 

self.data_store.create_operation(new_operation, self._name) 

 

self._send_operations_updates(peers=[peer], 

operations_by_peer=operations_by_peer, 

peer_message_queues=peer_message_queues) 

 

return new_operation.name 

 

def register_operation_peer(self, operation_name, peer, message_queue, operations_by_peer, peer_message_queues): 

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

 

Args: 

operation_name (str): an existing operation's name 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. 

""" 

if operation_name not in self.__operations_by_name: 

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

.format(operation_name)) 

 

if peer in operations_by_peer: 

operations_by_peer[peer].add(operation_name) 

else: 

operations_by_peer[peer] = set([operation_name]) 

 

if peer in peer_message_queues: 

peer_message_queues[peer][operation_name] = message_queue 

else: 

peer_message_queues[peer] = {operation_name: message_queue} 

 

self._send_operations_updates(peers=[peer], 

operations_by_peer=operations_by_peer, 

peer_message_queues=peer_message_queues) 

 

def unregister_operation_peer(self, operation_name, peer): 

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

 

Args: 

operation_name (str): an existing operation's name to unsubscribe from. 

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

 

Raises: 

NotFoundError: If no operation with `operation_name` exists. 

""" 

if operation_name not in self.__operations_by_name: 

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

.format(operation_name)) 

 

def list_operations(self): 

"""Lists the :class:`Operation` related to a job. 

 

Returns: 

list: A list of :class:`Operation` names. 

""" 

return list(self.__operations_by_name.keys()) 

 

def get_operation(self, operation_name): 

"""Returns a copy of the the job's :class:`Operation`. 

 

Args: 

operation_name (str): the operation's name. 

 

Raises: 

NotFoundError: If no operation with `operation_name` exists. 

""" 

try: 

operation = self.__operations_by_name[operation_name] 

 

except KeyError: 

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

.format(operation_name)) 

 

return self._copy_operation(operation) 

 

def update_operation_stage(self, stage, operations_by_peer, peer_message_queues): 

"""Operates a stage transition for the job's :class:`Operation`. 

 

Args: 

stage (OperationStage): the operation stage to transition to. 

""" 

if stage.value == self.__operation_metadata.stage: 

return 

 

changes = {} 

 

self.__operation_metadata.stage = stage.value 

changes["stage"] = stage.value 

 

self.__logger.debug("Stage changed for job [%s]: [%s] (operation)", 

self._name, stage.name) 

 

if self.__operation_metadata.stage == OperationStage.QUEUED.value: 

if self.__queued_timestamp.ByteSize() == 0: 

self.__queued_timestamp.GetCurrentTime() 

changes["queued_timestamp"] = self.__queued_timestamp.ToDatetime() 

self._n_tries += 1 

changes["n_tries"] = self._n_tries 

 

elif self.__operation_metadata.stage == OperationStage.EXECUTING.value: 

queue_in, queue_out = self.__queued_timestamp.ToDatetime(), datetime.utcnow() 

self.__queued_time_duration.FromTimedelta(queue_out - queue_in) 

changes["queued_time_duration"] = self.__queued_time_duration.seconds 

 

elif self.__operation_metadata.stage == OperationStage.COMPLETED.value: 

self._done = True 

 

self.data_store.update_job(self.name, changes) 

 

self._send_operations_updates(operations_by_peer=operations_by_peer, 

peer_message_queues=peer_message_queues) 

 

def cancel_operation(self, operation_name, operations_by_peer, peer_message_queues): 

"""Triggers a job's :class:`Operation` cancellation. 

 

This may cancel any job's :class:`Lease` that may have been issued. 

 

Args: 

operation_name (str): the operation's name. 

 

Raises: 

NotFoundError: If no operation with `operation_name` exists. 

""" 

try: 

operation = self.__operations_by_name[operation_name] 

 

except KeyError: 

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

.format(operation_name)) 

 

self.__operations_cancelled.add(operation.name) 

 

self.__logger.debug("Operation cancelled for job [%s]: [%s]", 

self._name, operation.name) 

 

ongoing_operations = set(self.__operations_by_name.keys()) 

# Job is cancelled if all the operation are: 

self.__job_cancelled = ongoing_operations.issubset(self.__operations_cancelled) 

 

if self.__job_cancelled: 

self.__operation_metadata.stage = OperationStage.COMPLETED.value 

changes = { 

"stage": OperationStage.COMPLETED.value, 

"cancelled": True 

} 

self.data_store.update_job(self.name, changes) 

if self._lease is not None: 

self.cancel_lease() 

 

peers_to_notify = set() 

# If the job is not cancelled, notify all the peers watching the given 

# operation; if the job is cancelled, only notify the peers for which 

# the operation status changed. 

for peer, names in operations_by_peer.items(): 

relevant_names = [n for n in names if n in self.__operations_by_name] 

if self.__job_cancelled: 

if not any(name in self.__operations_cancelled for name in relevant_names): 

peers_to_notify.add(peer) 

elif operation_name in relevant_names: 

peers_to_notify.add(peer) 

 

else: 

if operation_name in relevant_names: 

peers_to_notify.add(peer) 

 

self._send_operations_updates(peers=peers_to_notify, 

notify_cancelled=True, 

operations_by_peer=operations_by_peer, 

peer_message_queues=peer_message_queues) 

 

# --- Public API: RWAPI --- 

 

@property 

def lease(self): 

return self._lease 

 

@property 

def lease_state(self): 

if self._lease is not None: 

return LeaseState(self._lease.state) 

else: 

return None 

 

@property 

def lease_cancelled(self): 

return self.__lease_cancelled 

 

@property 

def n_tries(self): 

return self._n_tries 

 

def create_lease(self, worker_name, bot_id=None): 

"""Emits a new :class:`Lease` for the job. 

 

Only one :class:`Lease` can be emitted for a given job. This method 

should only be used once, any further calls are ignored. 

 

Args: 

worker_name (string): The name of the worker this lease is for. 

bot_id (string): The name of the corresponding bot for this job's worker. 

""" 

if self._lease is not None: 

return self._lease 

elif self.__job_cancelled: 

return None 

 

self._lease = bots_pb2.Lease() 

self._lease.id = self._name 

self._lease.payload.Pack(self.__operation_metadata.action_digest) 

self._lease.state = LeaseState.UNSPECIFIED.value 

 

if bot_id is None: 

bot_id = "UNKNOWN" 

self.__logger.debug("Lease created for job [%s]: [%s] (assigned to bot [%s])", 

self._name, self._lease.id, bot_id) 

 

self.update_lease_state(LeaseState.PENDING, skip_lease_persistence=True) 

 

self.worker_name = worker_name 

 

return self._lease 

 

def update_lease_state(self, state, status=None, result=None, skip_lease_persistence=False): 

"""Operates a state transition for the job's current :class:`Lease`. 

 

Args: 

state (LeaseState): the lease state to transition to. 

status (google.rpc.Status, optional): the lease execution status, 

only required if `state` is `COMPLETED`. 

result (google.protobuf.Any, optional): the lease execution result, 

only required if `state` is `COMPLETED`. 

""" 

if state.value == self._lease.state: 

return 

 

job_changes = {} 

lease_changes = {} 

 

self._lease.state = state.value 

lease_changes["state"] = state.value 

 

self.__logger.debug("State changed for job [%s]: [%s] (lease)", 

self._name, state.name) 

 

if self._lease.state == LeaseState.PENDING.value: 

self.__worker_start_timestamp.Clear() 

self.__worker_completed_timestamp.Clear() 

job_changes["worker_start_timestamp"] = self.__worker_start_timestamp.ToDatetime() 

job_changes["worker_completed_timestamp"] = self.__worker_completed_timestamp.ToDatetime() 

 

self._lease.status.Clear() 

self._lease.result.Clear() 

lease_changes["status"] = self._lease.status.code 

 

elif self._lease.state == LeaseState.COMPLETED.value: 

self.__worker_completed_timestamp.GetCurrentTime() 

job_changes["worker_completed_timestamp"] = self.__worker_completed_timestamp.ToDatetime() 

 

action_result = remote_execution_pb2.ActionResult() 

 

# TODO: Make a distinction between build and bot failures! 

if status.code != code_pb2.OK: 

self._do_not_cache = True 

job_changes["do_not_cache"] = True 

 

lease_changes["status"] = status.code 

 

if result is not None and result.Is(action_result.DESCRIPTOR): 

result.Unpack(action_result) 

 

action_metadata = action_result.execution_metadata 

action_metadata.queued_timestamp.CopyFrom(self.__queued_timestamp) 

action_metadata.worker_start_timestamp.CopyFrom(self.__worker_start_timestamp) 

action_metadata.worker_completed_timestamp.CopyFrom(self.__worker_completed_timestamp) 

 

self.__execute_response.result.CopyFrom(action_result) 

self.__execute_response.cached_result = False 

self.__execute_response.status.CopyFrom(status) 

 

self.data_store.update_job(self.name, job_changes) 

if not skip_lease_persistence: 

self.data_store.update_lease(self.name, lease_changes) 

 

def cancel_lease(self): 

"""Triggers a job's :class:`Lease` cancellation. 

 

Note: 

This will not cancel the job's :class:`Operation`. 

""" 

self.__lease_cancelled = True 

 

self.__logger.debug("Lease cancelled for job [%s]: [%s]", 

self._name, self._lease.id) 

 

if self._lease is not None: 

self.update_lease_state(LeaseState.CANCELLED) 

 

def delete_lease(self): 

"""Discard the job's :class:`Lease`. 

 

Note: 

This will not cancel the job's :class:`Operation`. 

""" 

if self._lease is not None: 

self.__worker_start_timestamp.Clear() 

self.__worker_completed_timestamp.Clear() 

 

self.__logger.debug("Lease deleted for job [%s]: [%s]", 

self._name, self._lease.id) 

 

self._lease = None 

 

# --- Public API: Monitoring --- 

 

def query_queue_time(self): 

return self.__queued_time_duration.ToTimedelta() 

 

def query_n_retries(self): 

return self._n_tries - 1 if self._n_tries > 0 else 0 

 

# --- Private API --- 

 

def _copy_operation(self, operation): 

"""Simply duplicates a given :class:`Lease` object.""" 

new_operation = operations_pb2.Operation() 

new_operation.CopyFrom(operation) 

 

new_operation.metadata.Pack(self.__operation_metadata) 

new_operation.response.Pack(self.__execute_response) 

 

return new_operation 

 

def _update_operation(self, operation, operation_metadata, execute_response=None, done=False): 

"""Forges a :class:`Operation` message given input data.""" 

operation.metadata.Pack(operation_metadata) 

 

if execute_response is not None: 

operation.response.Pack(execute_response) 

 

operation.done = done 

changes = {"done": done} 

self.data_store.update_operation(operation.name, changes) 

 

def _update_cancelled_operation(self, operation, operation_metadata, execute_response=None): 

"""Forges a cancelled :class:`Operation` message given input data.""" 

cancelled_operation_metadata = remote_execution_pb2.ExecuteOperationMetadata() 

cancelled_operation_metadata.CopyFrom(operation_metadata) 

cancelled_operation_metadata.stage = OperationStage.COMPLETED.value 

 

operation.metadata.Pack(cancelled_operation_metadata) 

 

cancelled_execute_response = remote_execution_pb2.ExecuteResponse() 

if execute_response is not None: 

cancelled_execute_response.CopyFrom(self.__execute_response) 

cancelled_execute_response.status.code = code_pb2.CANCELLED 

cancelled_execute_response.status.message = "Operation cancelled by client." 

 

operation.response.Pack(cancelled_execute_response) 

 

operation.done = True 

changes = {"done": True, "cancelled": True} 

self.data_store.update_operation(operation.name, changes) 

 

def _send_operations_updates(self, peers=None, notify_cancelled=False, 

operations_by_peer=None, peer_message_queues=None): 

"""Sends :class:`Operation` stage change messages to watchers.""" 

if operations_by_peer is None: 

operations_by_peer = {} 

if peer_message_queues is None: 

peer_message_queues = {} 

 

for operation in self.__operations_by_name.values(): 

if operation.name in self.__operations_cancelled: 

self._update_cancelled_operation(operation, self.__operation_metadata, 

execute_response=self.__execute_response) 

 

else: 

self._update_operation(operation, self.__operation_metadata, 

execute_response=self.__execute_response, 

done=self._done) 

 

relevant_queues = {peer: mqs for peer, mqs in peer_message_queues.items() 

if any(name in self.__operations_by_name 

for name in mqs)} 

 

for peer, message_queues in relevant_queues.items(): 

if peer not in operations_by_peer: 

continue 

elif peers and peer not in peers: 

continue 

 

operations = [self.__operations_by_name[name] for name in operations_by_peer[peer] 

if name in self.__operations_by_name] 

for operation in operations: 

# Messages are pairs of (Exception, Operation,): 

if not notify_cancelled and operation.name in self.__operations_cancelled: 

continue 

elif operation.name not in self.__operations_cancelled: 

message = (None, self._copy_operation(operation),) 

else: 

message = (CancelledError("Operation has been cancelled"), 

self._copy_operation(operation),) 

 

message_queue = message_queues[operation.name] 

message_queue.put(message)