diff options
author | Christopher Baines <mail@cbaines.net> | 2020-06-20 12:37:06 +0100 |
---|---|---|
committer | Christopher Baines <mail@cbaines.net> | 2020-06-20 12:37:06 +0100 |
commit | 9f0f1210b0aba5657dc32b0cd6e3a4ee9440e54b (patch) | |
tree | f3c376516f26ec21ee328de2ef3f0775e690f7d1 /guix-build-coordinator/datastore | |
parent | 0f947d683d6b3de552b563f785082fa7923fbb53 (diff) | |
download | build-coordinator-9f0f1210b0aba5657dc32b0cd6e3a4ee9440e54b.tar build-coordinator-9f0f1210b0aba5657dc32b0cd6e3a4ee9440e54b.tar.gz |
Simplify transaction handling in the sqlite datastore
Diffstat (limited to 'guix-build-coordinator/datastore')
-rw-r--r-- | guix-build-coordinator/datastore/sqlite.scm | 146 |
1 files changed, 37 insertions, 109 deletions
diff --git a/guix-build-coordinator/datastore/sqlite.scm b/guix-build-coordinator/datastore/sqlite.scm index 6afa872..f90fc1d 100644 --- a/guix-build-coordinator/datastore/sqlite.scm +++ b/guix-build-coordinator/datastore/sqlite.scm @@ -255,21 +255,10 @@ WHERE agent_id = :agent_id AND password = :password"))) (define-method (datastore-store-derivation (datastore <sqlite-datastore>) derivation) - (call-with-worker-thread - (slot-ref datastore 'worker-writer-thread-channel) + (datastore-call-with-transaction + datastore (lambda (db) - (sqlite-exec db "BEGIN TRANSACTION;") - (with-exception-handler - (lambda (exn) - (simple-format (current-error-port) - "error: sqlite: ~A\n" - exn) - (sqlite-exec db "ROLLBACK TRANSACTION;") - (raise-exception exn)) - (lambda () - (insert-derivation-and-return-outputs db derivation) - (sqlite-exec db "COMMIT TRANSACTION;")) - #:unwind? #t))) + (insert-derivation-and-return-outputs db derivation))) #t) (define-method (datastore-list-related-derivations-with-no-build-for-outputs @@ -464,28 +453,12 @@ INSERT INTO build_tags (build_id, tag_id) VALUES (:build_id, :tag_id)" (sqlite-step build-tags-statement))) tags))) - (call-with-worker-thread - (slot-ref datastore 'worker-writer-thread-channel) + (datastore-call-with-transaction + datastore (lambda (db) - (sqlite-exec db "BEGIN TRANSACTION;") - (with-exception-handler - (lambda (exn) - (simple-format (current-error-port) - "error: sqlite: ~A\n" - exn) - (sqlite-exec db "ROLLBACK TRANSACTION;") - (raise-exception exn)) - (lambda () - (with-exception-handler - (lambda (exn) - (backtrace) - (raise-exception exn)) - (lambda () - (insert-build db uuid derivation-name priority) - (unless (null? tags) - (insert-tags db uuid tags)) - (sqlite-exec db "COMMIT TRANSACTION;")))) - #:unwind? #t))) + (insert-build db uuid derivation-name priority) + (unless (null? tags) + (insert-tags db uuid tags)))) #t) (define-method (datastore-count-build-results @@ -606,28 +579,17 @@ VALUES " "build-failure") (list build-id))) - (call-with-worker-thread - (slot-ref datastore 'worker-writer-thread-channel) + (datastore-call-with-transaction + datastore (lambda (db) - (sqlite-exec db "BEGIN TRANSACTION;") - (with-exception-handler - (lambda (exn) - (simple-format (current-error-port) - "error: sqlite: ~A\n" - exn) - (sqlite-exec db "ROLLBACK TRANSACTION;") - (raise-exception exn)) - (lambda () - (insert-build-result db build-id agent-id result failure-reason) - (remove-build-allocation db build-id agent-id) - (mark-build-as-processed db build-id) - ;; This logic should be part of the coordinator, but it's here to be - ;; inside the transaction - (handle-inserting-unprocessed-hook-event db build-id result) - (when output-metadata - (store-output-metadata db build-id output-metadata)) - (sqlite-exec db "COMMIT TRANSACTION;")) - #:unwind? #t))) + (insert-build-result db build-id agent-id result failure-reason) + (remove-build-allocation db build-id agent-id) + (mark-build-as-processed db build-id) + ;; This logic should be part of the coordinator, but it's here to be + ;; inside the transaction + (handle-inserting-unprocessed-hook-event db build-id result) + (when output-metadata + (store-output-metadata db build-id output-metadata)))) #t) (define (insert-setup-failure-and-remove-allocation @@ -686,29 +648,18 @@ INSERT INTO setup_failure_missing_inputs ( (list build-id missing-inputs))) - (call-with-worker-thread - (slot-ref datastore 'worker-writer-thread-channel) + (datastore-call-with-transaction + datastore (lambda (db) - (sqlite-exec db "BEGIN TRANSACTION;") - (with-exception-handler - (lambda (exn) - (simple-format (current-error-port) - "error: sqlite: ~A\n" - exn) - (sqlite-exec db "ROLLBACK TRANSACTION;") - (raise-exception exn)) - (lambda () - (let ((setup-failure-id - (insert-setup-failure-and-remove-allocation db - build-id - agent-id - "missing_inputs"))) - (insert-missing-inputs db setup-failure-id missing-inputs)) - ;; This logic should be part of the coordinator, but it's here to be - ;; inside the transaction - (handle-inserting-unprocessed-hook-event db build-id missing-inputs) - (sqlite-exec db "COMMIT TRANSACTION;")) - #:unwind? #t))) + (let ((setup-failure-id + (insert-setup-failure-and-remove-allocation db + build-id + agent-id + "missing_inputs"))) + (insert-missing-inputs db setup-failure-id missing-inputs)) + ;; This logic should be part of the coordinator, but it's here to be + ;; inside the transaction + (handle-inserting-unprocessed-hook-event db build-id missing-inputs))) #t) (define-method (datastore-list-setup-failure-missing-inputs @@ -1277,23 +1228,12 @@ INSERT INTO build_allocation_plan (build_id, agent_id, ordering) VALUES " datastore "replace_build_allocation_plan_duration_seconds" (lambda () - (call-with-worker-thread - (slot-ref datastore 'worker-writer-thread-channel) + (datastore-call-with-transaction + datastore (lambda (db) - (sqlite-exec db "BEGIN TRANSACTION;") - (with-exception-handler - (lambda (exn) - (simple-format (current-error-port) - "error: sqlite: ~A\n" - exn) - (sqlite-exec db "ROLLBACK TRANSACTION;") - (raise-exception exn)) - (lambda () - (clear-current-plan db) - (unless (null? planned-builds) - (insert-new-plan db planned-builds)) - (sqlite-exec db "COMMIT TRANSACTION;")) - #:unwind? #t))))) + (clear-current-plan db) + (unless (null? planned-builds) + (insert-new-plan db planned-builds)))))) #t) (define-method (datastore-count-allocated-builds @@ -1422,22 +1362,10 @@ WHERE build_id IN (" (loop (cons build-details builds)) builds))))) - (call-with-worker-thread - (slot-ref datastore 'worker-writer-thread-channel) + (datastore-call-with-transaction + datastore (lambda (db) - (sqlite-exec db "BEGIN TRANSACTION;") - (with-exception-handler - (lambda (exn) - (simple-format (current-error-port) - "error: sqlite: ~A\n" - exn) - (sqlite-exec db "ROLLBACK TRANSACTION;") - (raise-exception exn)) - (lambda () - (let ((builds (allocate-several-builds db agent-id count))) - (sqlite-exec db "COMMIT TRANSACTION;") - builds)) - #:unwind? #t)))) + (allocate-several-builds db agent-id count)))) (define-method (datastore-list-allocation-plan-builds (datastore <sqlite-datastore>) |