34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
-
-
+
+
|
;; else return #f to let the calling proc know that there is no server available
;;
(define (rmt:get-connection-info areapath) ;; TODO: push areapath down.
(let ((cinfo (remote-conndat *runremote*))
(run-id 0))
(if cinfo
cinfo
(if (tasks:server-running-or-starting? (db:delay-if-busy (tasks:open-db)) run-id)
(client:setup run-id)
(if (server:check-if-running areapath)
(client:setup areapath)
#f))))
(define *send-receive-mutex* (make-mutex)) ;; should have separate mutex per run-id
;; RA => e.g. usage (rmt:send-receive 'get-var #f (list varname))
;;
(define (rmt:send-receive cmd rid params #!key (attemptnum 1)) ;; start attemptnum at 1 so the modulo below works as expected
|
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
|
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
|
-
-
+
+
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
|
(debug:print-info 12 *default-log-port* "rmt:send-receive, case 3")
(rmt:open-qry-close-locally cmd 0 params))
;; on homehost and this is a write, we already have a server, but server has died
((and (cdr (remote-hh-dat *runremote*)) ;; on homehost
(not (member cmd api:read-only-queries)) ;; this is a write
(remote-server-url *runremote*) ;; have a server
(not (server:read-dotserver *toppath*))) ;; server has died.
(set! *runremote* #f)
(not (server:check-if-running *toppath*))) ;; server has died.
(set! *runremote* (make-remote))
(mutex-unlock! *rmt-mutex*)
(debug:print-info 12 *default-log-port* "rmt:send-receive, case 4.1")
(rmt:send-receive cmd rid params attemptnum: attemptnum))
;; on homehost and this is a write, we already have a server
((and (cdr (remote-hh-dat *runremote*)) ;; on homehost
(not (member cmd api:read-only-queries)) ;; this is a write
(remote-server-url *runremote*)) ;; have a server
(mutex-unlock! *rmt-mutex*)
(debug:print-info 12 *default-log-port* "rmt:send-receive, case 4")
(rmt:open-qry-close-locally cmd 0 params))
;; commented by bb; this was blocking server passive start on write on homehost (case 5)
;; ;; on homehost and this is a write, we have a server (we know because case 4 checked)
;; ((and (cdr (remote-hh-dat *runremote*)) ;; on homehost
;; (not (member cmd api:read-only-queries)))
;; (mutex-unlock! *rmt-mutex*)
;; (debug:print-info 12 *default-log-port* "rmt:send-receive, case 4.1")
;; (rmt:open-qry-close-locally cmd 0 params))
;; on homehost, no server contact made and this is a write, passively start a server
((and (cdr (remote-hh-dat *runremote*)) ; new
(not (remote-server-url *runremote*))
(not (member cmd api:read-only-queries)))
(debug:print-info 12 *default-log-port* "rmt:send-receive, case 5")
(let ((server-url (server:read-dotserver->url *toppath*))) ;; (server:check-if-running *toppath*))) ;; Do NOT want to run server:check-if-running - very expensive to do for every write call
(let ((server-url (server:check-if-running *toppath*))) ;; (server:read-dotserver->url *toppath*))) ;; (server:check-if-running *toppath*))) ;; Do NOT want to run server:check-if-running - very expensive to do for every write call
(if server-url
(remote-server-url-set! *runremote* server-url) ;; the string can be consumed by the client setup if needed
(if (not (server:start-attempted? *toppath*))
(server:kind-run *toppath*))))
(mutex-unlock! *rmt-mutex*)
(debug:print-info 12 *default-log-port* "rmt:send-receive, case 5.1")
(rmt:open-qry-close-locally cmd 0 params))
(server:kind-run *toppath*)))
(mutex-unlock! *rmt-mutex*)
(debug:print-info 12 *default-log-port* "rmt:send-receive, case 5.1")
(rmt:open-qry-close-locally cmd 0 params))
;;;
;; (begin ;; not on homehost, start server and wait
;; (mutex-unlock! *rmt-mutex*)
;; (debug:print-info 12 *default-log-port* "rmt:send-receive, case 5.2")
;; (tasks:start-and-wait-for-server (tasks:open-db) 0 15)
;; (rmt:send-receive cmd rid params attemptnum: attemptnum)) ;) ;)
;;;;
;; if not on homehost ensure we have a connection to a live server
;; NOTE: we *have* a homehost record by now
;; ((and (not (cdr (remote-hh-dat *runremote*))) ;; not on a homehost
;; (not (remote-conndat *runremote*)) ;; and no connection
;; (server:read-dotserver *toppath*)) ;; .server file exists
;; ;; something caused the server entry in tdb to disappear, but the server is still running
;; (server:remove-dotserver-file *toppath* ".*")
;; (mutex-unlock! *rmt-mutex*)
;; (debug:print-info 12 *default-log-port* "rmt:send-receive, case 20")
;; (rmt:send-receive cmd rid params attemptnum: (add1 attemptnum)))
((and (not (cdr (remote-hh-dat *runremote*))) ;; not on a homehost
(not (remote-conndat *runremote*))) ;; and no connection
(debug:print-info 12 *default-log-port* "rmt:send-receive, case 6 hh-dat: " (remote-hh-dat *runremote*) " conndat: " (remote-conndat *runremote*))
(mutex-unlock! *rmt-mutex*)
(tasks:start-and-wait-for-server (tasks:open-db) 0 15)
(server:start-and-wait *toppath*)
(remote-conndat-set! *runremote* (rmt:get-connection-info *toppath*)) ;; calls client:setup which calls client:setup-http
(rmt:send-receive cmd rid params attemptnum: attemptnum)) ;; TODO: add back-off timeout as
;; all set up if get this far, dispatch the query
((cdr (remote-hh-dat *runremote*)) ;; we are on homehost
(mutex-unlock! *rmt-mutex*)
(debug:print-info 12 *default-log-port* "rmt:send-receive, case 7")
(rmt:open-qry-close-locally cmd (if rid rid 0) params))
|
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
-
+
|
(exit 1)))
(begin
(debug:print 0 *default-log-port* "WARNING: communication failed. Trying again, try num: " attemptnum)
(remote-conndat-set! *runremote* #f)
(remote-server-url-set! *runremote* #f)
(debug:print-info 12 *default-log-port* "rmt:send-receive, case 9.1")
(mutex-unlock! *rmt-mutex*)
(tasks:start-and-wait-for-server (tasks:open-db) 0 15)
(server:start-and-wait *toppath*)
(rmt:send-receive cmd rid params attemptnum: (+ attemptnum 1)))))))))
;; (define (rmt:update-db-stats run-id rawcmd params duration)
;; (mutex-lock! *db-stats-mutex*)
;; (handle-exceptions
;; exn
;; (begin
|