source: trunk/pbs_drmaa/session.c @ 85

Revision 85, 12.0 KB checked in by mmamonski, 11 years ago (diff)

PBS DRMAA autoclose connection

  • Property svn:keywords set to Id
Line 
1/* $Id$ */
2/*
3 *  FedStage DRMAA for PBS Pro
4 *  Copyright (C) 2006-2009  FedStage Systems
5 *
6 *  This program is free software: you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation, either version 3 of the License, or
9 *  (at your option) any later version.
10 *
11 *  This program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifdef HAVE_CONFIG_H
21#       include <config.h>
22#endif
23
24#include <pwd.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28#include <sys/select.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31#include <fcntl.h>
32#include <signal.h>
33
34#include <pbs_ifl.h>
35#include <pbs_error.h>
36
37#include <drmaa_utils/datetime.h>
38#include <drmaa_utils/drmaa.h>
39#include <drmaa_utils/iter.h>
40#include <drmaa_utils/conf.h>
41#include <drmaa_utils/session.h>
42#include <drmaa_utils/datetime.h>
43
44#include <pbs_drmaa/job.h>
45#include <pbs_drmaa/log_reader.h>
46#include <pbs_drmaa/session.h>
47#include <pbs_drmaa/submit.h>
48#include <pbs_drmaa/util.h>
49
50#include <errno.h>
51
52#ifndef lint
53static char rcsid[]
54#       ifdef __GNUC__
55                __attribute__ ((unused))
56#       endif
57        = "$Id$";
58#endif
59
60static void
61pbsdrmaa_session_destroy( fsd_drmaa_session_t *self );
62
63static void
64pbsdrmaa_session_apply_configuration( fsd_drmaa_session_t *self );
65
66static fsd_job_t *
67pbsdrmaa_session_new_job( fsd_drmaa_session_t *self, const char *job_id );
68
69static void
70pbsdrmaa_session_update_all_jobs_status( fsd_drmaa_session_t *self );
71
72static void
73*pbsdrmaa_session_wait_thread( fsd_drmaa_session_t *self );
74
75static char *
76pbsdrmaa_session_run_impl(
77                fsd_drmaa_session_t *self,
78                const fsd_template_t *jt,
79                int bulk_idx
80                );
81
82static struct attrl *
83pbsdrmaa_create_status_attrl(void);
84
85
86fsd_drmaa_session_t *
87pbsdrmaa_session_new( const char *contact )
88{
89        pbsdrmaa_session_t *volatile self = NULL;
90
91        if( contact == NULL )
92                contact = "";
93        TRY
94         {
95                self = (pbsdrmaa_session_t*)fsd_drmaa_session_new(contact);
96                fsd_realloc( self, 1, pbsdrmaa_session_t );
97                self->super_wait_thread = NULL;
98
99                self->log_file_initial_size = 0;
100                self->pbs_home = NULL;
101
102                self->wait_thread_log = false;
103                self->status_attrl = NULL;
104               
105                self->super_destroy = self->super.destroy;
106                self->super.destroy = pbsdrmaa_session_destroy;
107                self->super.new_job = pbsdrmaa_session_new_job;
108                self->super.update_all_jobs_status
109                                = pbsdrmaa_session_update_all_jobs_status;
110                self->super.run_impl = pbsdrmaa_session_run_impl;
111
112                self->super_apply_configuration = self->super.apply_configuration;
113                self->super.apply_configuration = pbsdrmaa_session_apply_configuration;
114
115                self->status_attrl = pbsdrmaa_create_status_attrl();
116                self->max_retries_count = 3;
117                self->wait_thread_sleep_time = 1;
118                self->job_exit_status_file_prefix = NULL;
119
120                self->super.load_configuration( &self->super, "pbs_drmaa" );
121
122                self->super.missing_jobs = FSD_IGNORE_MISSING_JOBS;
123
124                self->pbs_connection = pbsdrmaa_pbs_conn_new( (fsd_drmaa_session_t *)self, contact );
125                self->connection_max_lifetime =  30; /* 30 seconds */
126
127         }
128        EXCEPT_DEFAULT
129         {
130                if( self )
131                  {
132                        self->super.destroy( &self->super );
133                        self = NULL;
134                  }
135
136                fsd_exc_reraise();
137         }
138        END_TRY
139        return (fsd_drmaa_session_t*)self;
140}
141
142
143void
144pbsdrmaa_session_destroy( fsd_drmaa_session_t *self )
145{
146        pbsdrmaa_session_t *pbsself = (pbsdrmaa_session_t*)self;
147        self->stop_wait_thread( self );
148        pbsdrmaa_pbs_conn_destroy(pbsself->pbs_connection);
149        fsd_free( pbsself->status_attrl );
150        fsd_free( pbsself->job_exit_status_file_prefix );
151
152        pbsself->super_destroy( self );
153}
154
155
156static char *
157pbsdrmaa_session_run_impl(
158                fsd_drmaa_session_t *self,
159                const fsd_template_t *jt,
160                int bulk_idx
161                )
162{
163        char *volatile job_id = NULL;
164        fsd_job_t *volatile job = NULL;
165        pbsdrmaa_submit_t *volatile submit = NULL;
166
167        fsd_log_enter(( "(jt=%p, bulk_idx=%d)", (void*)jt, bulk_idx ));
168        TRY
169         {
170                submit = pbsdrmaa_submit_new( self, jt, bulk_idx );
171                submit->eval( submit );
172                job_id = submit->submit( submit );
173                job = self->new_job( self, job_id );
174                job->submit_time = time(NULL);
175                job->flags |= FSD_JOB_CURRENT_SESSION;
176                self->jobs->add( self->jobs, job );
177                job->release( job );  job = NULL;
178         }
179        EXCEPT_DEFAULT
180         {
181                fsd_free( job_id );
182                fsd_exc_reraise();
183         }
184        FINALLY
185         {
186                if( submit )
187                        submit->destroy( submit );
188                if( job )
189                        job->release( job );
190         }
191        END_TRY
192        fsd_log_return(( " =%s", job_id ));
193        return job_id;
194}
195
196
197static fsd_job_t *
198pbsdrmaa_session_new_job( fsd_drmaa_session_t *self, const char *job_id )
199{
200        fsd_job_t *job;
201        job = pbsdrmaa_job_new( fsd_strdup(job_id) );
202        job->session = self;
203        return job;
204}
205
206void
207pbsdrmaa_session_apply_configuration( fsd_drmaa_session_t *self )
208{
209        pbsdrmaa_session_t *pbsself = (pbsdrmaa_session_t*)self;
210        fsd_conf_option_t *pbs_home = NULL;
211        fsd_conf_option_t *wait_thread_sleep_time = NULL;
212        fsd_conf_option_t *max_retries_count = NULL;
213        fsd_conf_option_t *user_state_dir = NULL;
214        fsd_conf_option_t *connection_max_lifetime = NULL;
215
216
217        pbs_home = fsd_conf_dict_get(self->configuration, "pbs_home" );
218        wait_thread_sleep_time = fsd_conf_dict_get(self->configuration, "wait_thread_sleep_time" );
219        max_retries_count = fsd_conf_dict_get(self->configuration, "max_retries_count" );
220        user_state_dir = fsd_conf_dict_get(self->configuration, "user_state_dir" );
221        connection_max_lifetime = fsd_conf_dict_get(self->configuration, "connection_max_lifetime");
222
223        if( pbs_home && pbs_home->type == FSD_CONF_STRING )
224          {
225                        struct stat statbuf;
226                        char * volatile log_path;
227                        struct tm tm;
228                       
229                        pbsself->pbs_home = pbs_home->val.string;
230                        fsd_log_info(("pbs_home: %s",pbsself->pbs_home));
231                        pbsself->super_wait_thread = pbsself->super.wait_thread;
232                        pbsself->super.wait_thread = pbsdrmaa_session_wait_thread;             
233                        pbsself->wait_thread_log = true;
234       
235                        time(&pbsself->log_file_initial_time); 
236                        localtime_r(&pbsself->log_file_initial_time,&tm);
237
238                        log_path = fsd_asprintf("%s/server_logs/%04d%02d%02d",
239                                        pbsself->pbs_home,
240                                        tm.tm_year + 1900,
241                                        tm.tm_mon + 1,
242                                        tm.tm_mday);
243
244                        if(stat(log_path,&statbuf) == -1)
245                          {
246                                char errbuf[256] = "InternalError";
247                                (void)strerror_r(errno, errbuf, sizeof(errbuf));
248                                fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"stat error on file %s: %s", log_path, errbuf);
249                          }
250       
251                        fsd_log_debug(("Log file %s size %d",log_path,(int) statbuf.st_size));
252                        pbsself->log_file_initial_size = statbuf.st_size;
253                        fsd_free(log_path);
254          }
255
256        if ( max_retries_count && max_retries_count->type == FSD_CONF_INTEGER)
257          {
258                pbsself->max_retries_count = max_retries_count->val.integer;
259                fsd_log_info(("Max retries count: %d", pbsself->max_retries_count));
260          }
261
262        if ( connection_max_lifetime && connection_max_lifetime->type == FSD_CONF_INTEGER)
263          {
264                pbsself->connection_max_lifetime = connection_max_lifetime->val.integer;
265                fsd_log_info(("Max connection lifetime: %d", pbsself->connection_max_lifetime));
266          }
267
268        if ( wait_thread_sleep_time && wait_thread_sleep_time->type == FSD_CONF_INTEGER)
269          {
270                pbsself->wait_thread_sleep_time = wait_thread_sleep_time->val.integer;
271                fsd_log_info(("Wait thread sleep time: %d", pbsself->wait_thread_sleep_time));
272          }
273
274        if( user_state_dir && user_state_dir->type == FSD_CONF_STRING )
275          {
276                struct passwd *pw = NULL;
277                uid_t uid;
278
279                uid = geteuid();
280                pw = getpwuid(uid); /* drmaa_init is always called in thread safely fashion */
281
282                if (!pw)
283                        fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"Failed to get pw_name of the user %d", uid);
284
285                pbsself->job_exit_status_file_prefix = fsd_asprintf(user_state_dir->val.string, pw->pw_name);
286          }
287        else
288          {
289                pbsself->job_exit_status_file_prefix = fsd_asprintf("%s/.drmaa", getenv("HOME"));
290          }
291
292        fsd_log_debug(("Trying to create state directory: %s", pbsself->job_exit_status_file_prefix));
293
294        if (mkdir(pbsself->job_exit_status_file_prefix, 0700) == -1 && errno != EEXIST) /* TODO it would be much better to do stat before */
295          {
296                fsd_log_warning(("Failed to create job state directory: %s. Valid job exit status may not be available in some cases.", pbsself->job_exit_status_file_prefix));
297          }
298
299
300        /* TODO purge old exit statuses files */
301
302        pbsself->super_apply_configuration(self); /* call method from the superclass */
303}
304
305
306void
307pbsdrmaa_session_update_all_jobs_status( fsd_drmaa_session_t *self )
308{
309        volatile bool jobs_lock = false;
310        pbsdrmaa_session_t *pbsself = (pbsdrmaa_session_t*)self;
311        fsd_job_set_t *jobs = self->jobs;
312        struct batch_status *volatile status = NULL;
313
314        fsd_log_enter((""));
315
316        TRY
317         {
318
319/* TODO: query only for user's jobs pbs_selstat + ATTR_u */
320#ifdef PBS_PROFESSIONAL
321                status = pbsself->pbs_connection->statjob(pbsself->pbs_connection, NULL, NULL);
322#else
323                status = pbsself->pbs_connection->statjob(pbsself->pbs_connection, NULL, pbsself->status_attrl);
324#endif
325
326                 {
327                        size_t i;
328                        fsd_job_t *job;
329                        jobs_lock = fsd_mutex_lock( &jobs->mutex );
330                        for( i = 0;  i < jobs->tab_size;  i++ )
331                                for( job = jobs->tab[i];  job != NULL;  job = job->next )
332                                 {
333                                        fsd_mutex_lock( &job->mutex );
334                                        job->flags |= FSD_JOB_MISSING;
335                                        fsd_mutex_unlock( &job->mutex );
336                                 }
337                        jobs_lock = fsd_mutex_unlock( &jobs->mutex );
338                 }
339
340                 {
341                        struct batch_status *volatile i;
342                        for( i = status;  i != NULL;  i = i->next )
343                         {
344                                fsd_job_t *job = NULL;
345                                fsd_log_debug(( "job_id=%s", i->name ));
346                                job = self->get_job( self, i->name );
347                                if( job != NULL )
348                                 {
349                                        job->flags &= ~FSD_JOB_MISSING;
350                                        TRY
351                                         {
352                                                ((pbsdrmaa_job_t*)job)->update( job, i );
353                                         }
354                                        FINALLY
355                                         {
356                                                job->release( job );
357                                         }
358                                        END_TRY
359                                 }
360                         }
361                 }
362
363                 {
364                        size_t volatile i;
365                        fsd_job_t *volatile job;
366                        jobs_lock = fsd_mutex_lock( &jobs->mutex );
367                        for( i = 0;  i < jobs->tab_size;  i++ )
368                                for( job = jobs->tab[i];  job != NULL;  job = job->next )
369                                 {
370                                        fsd_mutex_lock( &job->mutex );
371                                        TRY
372                                         {
373                                                if( job->flags & FSD_JOB_MISSING )
374                                                        job->on_missing( job );
375                                         }
376                                        FINALLY{ fsd_mutex_unlock( &job->mutex ); }
377                                        END_TRY
378                                 }
379                        jobs_lock = fsd_mutex_unlock( &jobs->mutex );
380                 }
381         }
382        FINALLY
383         {
384                if( status != NULL )
385                         pbsself->pbs_connection->statjob_free(pbsself->pbs_connection, status );
386                if( jobs_lock )
387                        jobs_lock = fsd_mutex_unlock( &jobs->mutex );
388         }
389        END_TRY
390
391        fsd_log_return((""));
392}
393
394
395
396struct attrl *
397pbsdrmaa_create_status_attrl(void)
398{
399        struct attrl *result = NULL;
400        struct attrl *i;
401        const int max_attribs = 16;
402        int n_attribs;
403        int j = 0;
404
405        fsd_log_enter((""));
406        fsd_calloc( result, max_attribs, struct attrl );
407        result[j++].name="job_state";
408        result[j++].name="exit_status";
409        result[j++].name="resources_used";
410        result[j++].name="ctime";
411        result[j++].name="mtime";
412        result[j++].name="qtime";
413        result[j++].name="etime";
414
415        result[j++].name="queue";
416        result[j++].name="Account_Name";
417        result[j++].name="exec_host";
418        result[j++].name="start_time";
419        result[j++].name="mtime";
420#if 0
421        result[j].name="resources_used";  result[j].resource="walltime";  j++;
422        result[j].name="resources_used";  result[j].resource="cput";  j++;
423        result[j].name="resources_used";  result[j].resource="mem";  j++;
424        result[j].name="resources_used";  result[j].resource="vmem";  j++;
425        result[j].name="Resource_List";  result[j].resource="walltime";  j++;
426        result[j].name="Resource_List";  result[j].resource="cput";  j++;
427        result[j].name="Resource_List";  result[j].resource="mem";  j++;
428        result[j].name="Resource_List";  result[j].resource="vmem";  j++;
429#endif
430        n_attribs = j;
431        for( i = result;  true;  i++ )
432                if( i+1 < result + n_attribs )
433                        i->next = i+1;
434                else
435                 {
436                        i->next = NULL;
437                        break;
438                 }
439
440#ifdef DEBUGGING
441        fsd_log_return((":"));
442        pbsdrmaa_dump_attrl( result, NULL );
443#endif
444        return result;
445}
446
447void *
448pbsdrmaa_session_wait_thread( fsd_drmaa_session_t *self )
449{
450        pbsdrmaa_log_reader_t *log_reader = NULL;
451       
452        fsd_log_enter(( "" ));
453       
454        TRY
455        {       
456                log_reader = pbsdrmaa_log_reader_new( self );
457                log_reader->read_log( log_reader );
458        }
459        FINALLY
460        {
461                pbsdrmaa_log_reader_destroy( log_reader );
462        }
463        END_TRY
464       
465        fsd_log_return(( " =NULL" ));
466        return NULL;
467}
Note: See TracBrowser for help on using the repository browser.