]> git.sesse.net Git - freerainbowtables/blob - BOINC software/BOINC server apps/chain_checker_assimilator/assimilator.cpp
initial
[freerainbowtables] / BOINC software / BOINC server apps / chain_checker_assimilator / assimilator.cpp
1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
4 //
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
9 //
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
17
18 // This is a framework for an assimilator.
19 // You need to link this with an (application-specific) function
20 // assimilate_handler()
21 // in order to make a complete program.
22 //
23
24 #include "config.h"
25 #include <cstring>
26 #include <cstdlib>
27 #include <unistd.h>
28 #include <ctime>
29 #include <vector>
30
31 #include "boinc_db.h"
32 #include "parse.h"
33 #include "util.h"
34 #include "error_numbers.h"
35 #include "str_util.h"
36
37 #include "sched_config.h"
38 #include "sched_util.h"
39 #include "sched_msgs.h"
40 #include "assimilate_handler.h"
41
42 using std::vector;
43
44 #define LOCKFILE "assimilator.out"
45 #define PIDFILE  "assimilator.pid"
46 #define SLEEP_INTERVAL 10
47
48 bool update_db = true;
49 bool noinsert = false;
50 int wu_id_modulus=0, wu_id_remainder=0;
51 int sleep_interval = SLEEP_INTERVAL;
52 int one_pass_N_WU=0;
53 int g_argc;
54 char** g_argv;
55
56 void usage(char** argv) {
57     fprintf(stderr,
58         "This program is an 'assimilator'; it handles completed jobs.\n"
59         "Normally it is run as a daemon from config.xml.\n"
60         "See: http://boinc.berkeley.edu/trac/wiki/BackendPrograms\n\n"
61     );
62
63     fprintf(stderr, "usage: %s [options]\n"
64         "    Options:\n"
65         "    -app name            Process jobs for the given application\n"
66         "    [-sleep_interval X]  Sleep X seconds if no jobs to process (default 10)\n"
67         "    [-mod N R]           Process jobs with mod(ID, N) == R\n"
68         "    [-one_pass]          Do one DB enumeration, then exit\n"
69         "    [-one_pass_N_WU N]   Process at most N jobs\n"
70         "    [-d N]               Set verbosity level (1, 2, 3=most verbose)\n"
71         "    [-dont_update_db]    Don't update DB (for testing)\n"
72         "    [-noinsert]          Don't insert records in app-specific DB\n",
73         argv[0]
74     );
75     exit(0);
76 }
77
78 // assimilate all WUs that need it
79 // return nonzero if did anything
80 //
81 bool do_pass(APP& app) {
82     DB_WORKUNIT wu;
83     DB_RESULT canonical_result, result;
84     bool did_something = false;
85     char buf[256];
86     char mod_clause[256];
87     int retval;
88     int num_assimilated=0;
89
90     check_stop_daemons();
91
92     if (wu_id_modulus) {
93         sprintf(mod_clause, " and workunit.id %% %d = %d ",
94                 wu_id_modulus, wu_id_remainder
95         );
96     } else {
97         strcpy(mod_clause, "");
98     }
99
100     sprintf(buf,
101         "where appid=%d and assimilate_state=%d %s limit %d",
102         app.id, ASSIMILATE_READY, mod_clause,
103         one_pass_N_WU ? one_pass_N_WU : 1000
104     );
105     while (1) {
106         retval = wu.enumerate(buf);
107         if (retval) {
108             if (retval != ERR_DB_NOT_FOUND) {
109                 log_messages.printf(MSG_DEBUG,
110                     "DB connection lost, exiting\n"
111                 );
112                 exit(0);
113             }
114             break;
115         }
116         vector<RESULT> results;     // must be inside while()!
117
118         // for testing purposes, pretend we did nothing
119         //
120         if (update_db) {
121             did_something = true;
122         }
123
124         log_messages.printf(MSG_DEBUG,
125             "[%s] assimilating WU %d; state=%d\n", wu.name, wu.id, wu.assimilate_state
126         );
127
128         sprintf(buf, "where workunitid=%d", wu.id);
129         canonical_result.clear();
130         bool found = false;
131         while (!result.enumerate(buf)) {
132             results.push_back(result);
133             if (result.id == wu.canonical_resultid) {
134                 canonical_result = result;
135                 found = true;
136             }
137         }
138
139         // If no canonical result found and WU had no other errors,
140         // something is wrong, e.g. result records got deleted prematurely.
141         // This is probably unrecoverable, so mark the WU as having
142         // an assimilation error and keep going.
143         //
144         if (!found && !wu.error_mask) {
145             log_messages.printf(MSG_CRITICAL,
146                 "[%s] no canonical result\n", wu.name
147             );
148             wu.error_mask = WU_ERROR_NO_CANONICAL_RESULT;
149             sprintf(buf, "error_mask=%d", wu.error_mask);
150             wu.update_field(buf);
151         }
152
153         retval = assimilate_handler(wu, results, canonical_result);
154         if (retval && retval != DEFER_ASSIMILATION) {
155             log_messages.printf(MSG_CRITICAL,
156                 "[%s] handler returned error %d; exiting\n", wu.name, retval
157             );
158             exit(retval);
159         }
160
161         if (update_db) {
162             // Defer assimilation until next result is returned
163             int assimilate_state = ASSIMILATE_DONE;
164             if (retval == DEFER_ASSIMILATION) {
165                 assimilate_state = ASSIMILATE_INIT;
166             }
167             sprintf(
168                 buf, "assimilate_state=%d, transition_time=%d", 
169                 assimilate_state, (int)time(0)
170             );
171             retval = wu.update_field(buf);
172             if (retval) {
173                 log_messages.printf(MSG_CRITICAL,
174                     "[%s] update failed: %d\n", wu.name, retval
175                 );
176                 exit(1);
177             }
178         }
179
180         num_assimilated++;
181
182     }
183
184     if (did_something) {
185         boinc_db.commit_transaction();
186     }
187
188     if (num_assimilated)  {
189         log_messages.printf(MSG_NORMAL,
190             "Assimilated %d workunits.\n", num_assimilated
191         );
192     }
193
194     return did_something;
195 }
196
197 int main(int argc, char** argv) {
198     int retval;
199     bool one_pass = false;
200     DB_APP app;
201     int i;
202     char buf[256];
203
204     strcpy(app.name, "");
205     check_stop_daemons();
206     g_argc = argc;
207     g_argv = argv;
208     for (i=1; i<argc; i++) {
209         if (!strcmp(argv[i], "-one_pass_N_WU")) {
210             one_pass_N_WU = atoi(argv[++i]);
211             one_pass = true;
212         } else if (!strcmp(argv[i], "-sleep_interval")) {
213             sleep_interval = atoi(argv[++i]);
214         } else if (!strcmp(argv[i], "-one_pass")) {
215             one_pass = true;
216         } else if (!strcmp(argv[i], "-d")) {
217             log_messages.set_debug_level(atoi(argv[++i]));
218         } else if (!strcmp(argv[i], "-app")) {
219             strcpy(app.name, argv[++i]);
220         } else if (!strcmp(argv[i], "-dont_update_db")) {
221             // This option is for testing your assimilator.  When set,
222             // it ensures that the assimilator does not actually modify
223             // the assimilate_state of the workunits, so you can run
224             // your assimilator over and over again without affecting
225             // your project.
226             update_db = false;
227         } else if (!strcmp(argv[i], "-noinsert")) {
228             // This option is also for testing and is used to 
229             // prevent the inserting of results into the *backend*
230             // (as opposed to the boinc) DB.
231             noinsert = true;
232         } else if (!strcmp(argv[i], "-mod")) {
233             wu_id_modulus   = atoi(argv[++i]);
234             wu_id_remainder = atoi(argv[++i]);
235         } else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
236             usage(argv);
237         } else {
238             log_messages.printf(MSG_CRITICAL, "Unrecognized arg: %s\n", argv[i]);
239             usage(argv);
240         }
241     }
242
243     if (!strlen(app.name)) {
244         usage(argv);
245     }
246
247     if (wu_id_modulus) {
248         log_messages.printf(MSG_DEBUG,
249             "Using mod'ed WU enumeration.  modulus = %d  remainder = %d\n",
250             wu_id_modulus, wu_id_remainder
251         );
252     }
253
254     retval = config.parse_file("..");
255     if (retval) {
256         log_messages.printf(MSG_CRITICAL,
257             "Can't parse ../config.xml: %s\n", boincerror(retval)
258         );
259         exit(1);
260     }
261
262     log_messages.printf(MSG_NORMAL, "Starting\n");
263
264     retval = boinc_db.open(config.db_name, config.db_host, config.db_user, config.db_passwd);
265     if (retval) {
266         log_messages.printf(MSG_CRITICAL, "Can't open DB\n");
267         exit(1);
268     }
269     sprintf(buf, "where name='%s'", app.name);
270     retval = app.lookup(buf);
271     if (retval) {
272         log_messages.printf(MSG_CRITICAL, "Can't find app\n");
273         exit(1);
274     }
275     install_stop_signal_handler();
276     while (1) {
277         if (!do_pass(app)) {
278             if (one_pass) break;
279             sleep(sleep_interval);
280         }
281     }
282 }
283
284
285 const char *BOINC_RCSID_7841370789 = "$Id: assimilator.cpp 16121 2008-10-03 19:31:56Z davea $";