Megatest

Check-in [53f35f5363]
Login
Overview
Comment:Wrap dbfile:open-db call with mutex to minimize over-opening of db's
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | v1.70
Files: files | file ages | folders
SHA1: 53f35f5363775e81c0c5acd22c7b5e64379d17b6
User & Date: matt on 2022-09-18 10:48:56
Other Links: branch diff | manifest | tags
Context
2022-09-18
12:50
Moved close-all-connections! to better spot. check-in: a847a95748 user: matt tags: v1.70
10:48
Wrap dbfile:open-db call with mutex to minimize over-opening of db's check-in: 53f35f5363 user: matt tags: v1.70
2022-09-16
16:21
removed checking for journal file. Moved setting of busy timeout and PRAGMA synchronous inside cautious-open-database check-in: a66ca8ec4f user: mmgraham tags: v1.70
Changes

Modified dbfile.scm from [a29708f170] to [769f7dede8].

80
81
82
83
84
85
86

87

88
89
90
91
92
93
94
80
81
82
83
84
85
86
87

88
89
90
91
92
93
94
95







+
-
+







  (dbfile      #f)
  (dbh         #f)    
  (stmt-cache  (make-hash-table))
  (read-only   #f)
  (birth-sec   (current-seconds)))

(define *dbstruct-dbs* #f)
(define *db-open-mutex* (make-mutex))
(define *db-access-mutex* (make-mutex))
(define *db-access-mutex* (make-mutex)) ;; used in common.scm
(define *no-sync-db*   #f)
(define *db-sync-in-progress* #f)
(define *db-with-db-mutex*    (make-mutex))
(define *max-api-process-requests* 0)
(define *api-process-request-count* 0)
(define *db-write-access*     #t)
(define *db-last-sync*        0)                 ;; last time the sync to megatest.db happened
273
274
275
276
277
278
279

280
281
282
283
284
285
286
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288







+







;;     it to the stack).
;;
(define (dbfile:open-db dbstruct run-id init-proc)
  (if (> *dbfile:num-handles-in-use* 10)
      (let* ((wait-delay (- *dbfile:num-handles-in-use* 9)))
	(dbfile:print-err "INFO: over ten dbfile handle threads in use ("*dbfile:num-handles-in-use*") delaying "wait-delay" second")
	(thread-sleep! wait-delay)))
  
  (let* ((subdb (dbfile:get-subdb dbstruct run-id)))
    (if (not subdb) ;; not yet defined
	(begin
	  (dbfile:init-subdb dbstruct run-id init-proc)
	  (dbfile:open-db dbstruct run-id init-proc))
	(let* ((dbdat (dbfile:get-dbdat dbstruct run-id)))
	  (if dbdat
716
717
718
719
720
721
722
723

724
725
726
727
728
729
730
718
719
720
721
722
723
724

725
726
727
728
729
730
731
732







-
+







(define (db:sync-touched dbstruct run-id keys #!key dbinit (force-sync #f))
  (dbfile:print-err "db:sync-touched Syncing: " (conc (if run-id run-id "main") ".db"))
  (let* (;; the subdb is needed to access the mtdbdat
	 (subdb     (or (dbfile:get-subdb dbstruct run-id)
			(dbfile:init-subdb dbstruct run-id dbinit)))
         (tmpdbfile (dbr:subdb-tmpdbfile subdb))
	 (mtdb      (dbr:subdb-mtdbdat subdb))
         (tmpdb     (dbfile:open-db dbstruct run-id dbinit)) ;; sqlite3-db tmpdbfile #f))
         (tmpdb     (db:open-db dbstruct run-id dbinit)) ;; sqlite3-db tmpdbfile #f))
	 (start-t   (current-seconds)))
    (mutex-lock! *db-multi-sync-mutex*)
    (let ((update_info (cons "last_update" (if force-sync 0 *db-last-sync*) )))
      (mutex-unlock! *db-multi-sync-mutex*)
      (db:sync-tables (db:sync-all-tables-list dbstruct keys) update_info tmpdb mtdb))
    (mutex-lock! *db-multi-sync-mutex*)
    (set! *db-last-sync* start-t)
1145
1146
1147
1148
1149
1150
1151

1152
1153

1154
1155
1156
1157
1158
1159
1160
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164







+


+







;;======================================================================
;; db access stuff
;;======================================================================

;; call with dbinit=db:initialize-main-db
;;
(define (db:open-db dbstruct run-id dbinit)
  (mutex-lock! *db-open-mutex*)
  (let* ((dbdat (dbfile:open-db dbstruct run-id dbinit)))
    (set! *db-write-access* (not (dbr:dbdat-read-only dbdat)))
    (mutex-unlock! *db-open-mutex*)
    dbdat))

(define dbfile:db-init-proc (make-parameter #f))

;; (db:with-db dbstruct run-id sqlite3:exec "select blah fgrom blaz;")
;; r/w is a flag to indicate if the db is modified by this query #t = yes, #f = no
;;