comparison src/c/driver.c @ 424:b10132434adc

Transactions seem to be working
author Adam Chlipala <adamc@hcoop.net>
date Fri, 24 Oct 2008 16:47:18 -0400
parents cc71fb7e5e54
children 360cbc202756
comparison
equal deleted inserted replaced
423:82067ea6e723 424:b10132434adc
48 48
49 static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER; 49 static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
50 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER; 50 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
51 51
52 #define MAX_RETRIES 5 52 #define MAX_RETRIES 5
53
54 int uw_db_begin(uw_context);
55 int uw_db_commit(uw_context);
56 int uw_db_rollback(uw_context);
57
58 static int try_rollback(uw_context ctx) {
59 int r = uw_db_rollback(ctx);
60
61 if (r) {
62 printf("Error running SQL ROLLBACK\n");
63 uw_reset(ctx);
64 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
65 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
66 uw_write(ctx, "Error running SQL ROLLBACK\n");
67 }
68
69 return r;
70 }
53 71
54 static void *worker(void *data) { 72 static void *worker(void *data) {
55 int me = *(int *)data, retries_left = MAX_RETRIES; 73 int me = *(int *)data, retries_left = MAX_RETRIES;
56 uw_context ctx = uw_init(1024, 0); 74 uw_context ctx = uw_init(1024, 0);
57 75
114 132
115 back += r; 133 back += r;
116 *back = 0; 134 *back = 0;
117 135
118 if (s = strstr(buf, "\r\n\r\n")) { 136 if (s = strstr(buf, "\r\n\r\n")) {
137 failure_kind fk;
119 char *cmd, *path, path_copy[uw_bufsize+1], *inputs; 138 char *cmd, *path, path_copy[uw_bufsize+1], *inputs;
120 139
121 *s = 0; 140 *s = 0;
122 141
123 if (!(s = strstr(buf, "\r\n"))) { 142 if (!(s = strstr(buf, "\r\n"))) {
167 } 186 }
168 187
169 printf("Serving URI %s....\n", path); 188 printf("Serving URI %s....\n", path);
170 189
171 while (1) { 190 while (1) {
172 failure_kind fk; 191 if (uw_db_begin(ctx)) {
192 printf("Error running SQL BEGIN\n");
193 if (retries_left)
194 --retries_left;
195 else {
196 fk = FATAL;
197 uw_reset(ctx);
198 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
199 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
200 uw_write(ctx, "Error running SQL BEGIN\n");
201
202 break;
203 }
204 }
173 205
174 uw_write(ctx, "HTTP/1.1 200 OK\r\n"); 206 uw_write(ctx, "HTTP/1.1 200 OK\r\n");
175 uw_write(ctx, "Content-type: text/html\r\n\r\n"); 207 uw_write(ctx, "Content-type: text/html\r\n\r\n");
176 uw_write(ctx, "<html>"); 208 uw_write(ctx, "<html>");
177 209
178 strcpy(path_copy, path); 210 strcpy(path_copy, path);
179 fk = uw_begin(ctx, path_copy); 211 fk = uw_begin(ctx, path_copy);
180 if (fk == SUCCESS) { 212 if (fk == SUCCESS) {
181 uw_write(ctx, "</html>"); 213 uw_write(ctx, "</html>");
214
215 if (uw_db_commit(ctx)) {
216 fk = FATAL;
217
218 printf("Error running SQL COMMIT\n");
219 uw_reset(ctx);
220 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
221 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
222 uw_write(ctx, "Error running SQL COMMIT\n");
223 }
224
182 break; 225 break;
183 } else if (fk == BOUNDED_RETRY) { 226 } else if (fk == BOUNDED_RETRY) {
184 if (retries_left) { 227 if (retries_left) {
185 printf("Error triggers bounded retry: %s\n", uw_error_message(ctx)); 228 printf("Error triggers bounded retry: %s\n", uw_error_message(ctx));
186 --retries_left; 229 --retries_left;
192 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r"); 235 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
193 uw_write(ctx, "Content-type: text/plain\r\n\r\n"); 236 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
194 uw_write(ctx, "Fatal error (out of retries): "); 237 uw_write(ctx, "Fatal error (out of retries): ");
195 uw_write(ctx, uw_error_message(ctx)); 238 uw_write(ctx, uw_error_message(ctx));
196 uw_write(ctx, "\n"); 239 uw_write(ctx, "\n");
240
241 try_rollback(ctx);
242
243 break;
197 } 244 }
198 } else if (fk == UNLIMITED_RETRY) 245 } else if (fk == UNLIMITED_RETRY)
199 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx)); 246 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
200 else if (fk == FATAL) { 247 else if (fk == FATAL) {
201 printf("Fatal error: %s\n", uw_error_message(ctx)); 248 printf("Fatal error: %s\n", uw_error_message(ctx));
205 uw_write(ctx, "Content-type: text/plain\r\n\r\n"); 252 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
206 uw_write(ctx, "Fatal error: "); 253 uw_write(ctx, "Fatal error: ");
207 uw_write(ctx, uw_error_message(ctx)); 254 uw_write(ctx, uw_error_message(ctx));
208 uw_write(ctx, "\n"); 255 uw_write(ctx, "\n");
209 256
257 try_rollback(ctx);
258
210 break; 259 break;
211 } else { 260 } else {
212 printf("Unknown uw_handle return code!\n"); 261 printf("Unknown uw_handle return code!\n");
213 262
214 uw_reset_keep_request(ctx); 263 uw_reset_keep_request(ctx);
215 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r"); 264 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
216 uw_write(ctx, "Content-type: text/plain\r\n\r\n"); 265 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
217 uw_write(ctx, "Unknown uw_handle return code!\n"); 266 uw_write(ctx, "Unknown uw_handle return code!\n");
218 267
268 try_rollback(ctx);
269
219 break; 270 break;
220 } 271 }
221 272
222 uw_reset_keep_request(ctx); 273 uw_reset_keep_request(ctx);
274
275 if (try_rollback(ctx))
276 break;
223 } 277 }
224 278
225 uw_send(ctx, sock); 279 uw_send(ctx, sock);
226 280
227 printf("Done with client.\n\n"); 281 printf("Done with client.\n\n");