source: trunk/pbs_drmaa/session.c @ 45

Revision 45, 11.6 KB checked in by mmamonski, 12 years ago (diff)

version 1.0.11 - ready to try

  • 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 <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27#include <sys/select.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <fcntl.h>
31
32#include <pbs_ifl.h>
33#include <pbs_error.h>
34
35#include <drmaa_utils/datetime.h>
36#include <drmaa_utils/drmaa.h>
37#include <drmaa_utils/iter.h>
38#include <drmaa_utils/conf.h>
39#include <drmaa_utils/session.h>
40#include <drmaa_utils/datetime.h>
41
42#include <pbs_drmaa/job.h>
43#include <pbs_drmaa/log_reader.h>
44#include <pbs_drmaa/session.h>
45#include <pbs_drmaa/submit.h>
46#include <pbs_drmaa/util.h>
47
48#include <errno.h>
49
50#ifndef lint
51static char rcsid[]
52#       ifdef __GNUC__
53                __attribute__ ((unused))
54#       endif
55        = "$Id$";
56#endif
57
58static void
59pbsdrmaa_session_destroy( fsd_drmaa_session_t *self );
60
61static void
62pbsdrmaa_session_apply_configuration( fsd_drmaa_session_t *self );
63
64static fsd_job_t *
65pbsdrmaa_session_new_job( fsd_drmaa_session_t *self, const char *job_id );
66
67static void
68pbsdrmaa_session_update_all_jobs_status( fsd_drmaa_session_t *self );
69
70static void
71*pbsdrmaa_session_wait_thread( fsd_drmaa_session_t *self );
72
73static char *
74pbsdrmaa_session_run_impl(
75                fsd_drmaa_session_t *self,
76                const fsd_template_t *jt,
77                int bulk_idx
78                );
79
80static struct attrl *
81pbsdrmaa_create_status_attrl(void);
82
83
84fsd_drmaa_session_t *
85pbsdrmaa_session_new( const char *contact )
86{
87        pbsdrmaa_session_t *volatile self = NULL;
88
89        if( contact == NULL )
90                contact = "";
91        TRY
92         {
93                self = (pbsdrmaa_session_t*)fsd_drmaa_session_new(contact);
94                fsd_realloc( self, 1, pbsdrmaa_session_t );
95                self->super_wait_thread = NULL;
96
97                self->log_file_initial_size = 0;
98                self->pbs_conn = -1;
99                self->pbs_home = NULL;
100
101                self->wait_thread_log = false;
102                self->status_attrl = NULL;
103               
104                self->super_destroy = self->super.destroy;
105                self->super.destroy = pbsdrmaa_session_destroy;
106                self->super.new_job = pbsdrmaa_session_new_job;
107                self->super.update_all_jobs_status
108                                = pbsdrmaa_session_update_all_jobs_status;
109                self->super.run_impl = pbsdrmaa_session_run_impl;
110
111                self->super_apply_configuration = self->super.apply_configuration;
112                self->super.apply_configuration = pbsdrmaa_session_apply_configuration;
113
114                self->status_attrl = pbsdrmaa_create_status_attrl();
115                self->max_retries_count = 3;
116                self->wait_thread_sleep_time = 1;
117
118                self->super.load_configuration( &self->super, "pbs_drmaa" );
119
120                self->super.missing_jobs = FSD_IGNORE_MISSING_JOBS;
121
122                 {
123                        int tries_left = self->max_retries_count;
124                        int sleep_time = 1;
125retry_connect: /* Life... */
126                        self->pbs_conn = pbs_connect( self->super.contact );
127                        fsd_log_info(( "pbs_connect(%s) =%d", self->super.contact, self->pbs_conn ));
128                        if( self->pbs_conn < 0 && tries_left-- )
129                         {
130                                sleep(sleep_time++);
131                                goto retry_connect;
132                         }
133
134                        if( self->pbs_conn < 0 )
135                                pbsdrmaa_exc_raise_pbs( "pbs_connect" );
136                 }
137         }
138        EXCEPT_DEFAULT
139         {
140                if( self )
141                  {
142                        self->super.destroy( &self->super );
143                        self = NULL;
144                  }
145
146                fsd_exc_reraise();
147         }
148        END_TRY
149        return (fsd_drmaa_session_t*)self;
150}
151
152
153void
154pbsdrmaa_session_destroy( fsd_drmaa_session_t *self )
155{
156        pbsdrmaa_session_t *pbsself = (pbsdrmaa_session_t*)self;
157        self->stop_wait_thread( self );
158        if( pbsself->pbs_conn >= 0 )
159                pbs_disconnect( pbsself->pbs_conn );
160        fsd_free( pbsself->status_attrl );
161        pbsself->super_destroy( self );
162}
163
164
165static char *
166pbsdrmaa_session_run_impl(
167                fsd_drmaa_session_t *self,
168                const fsd_template_t *jt,
169                int bulk_idx
170                )
171{
172        char *volatile job_id = NULL;
173        fsd_job_t *volatile job = NULL;
174        pbsdrmaa_submit_t *volatile submit = NULL;
175
176        fsd_log_enter(( "(jt=%p, bulk_idx=%d)", (void*)jt, bulk_idx ));
177        TRY
178         {
179                submit = pbsdrmaa_submit_new( self, jt, bulk_idx );
180                submit->eval( submit );
181                job_id = submit->submit( submit );
182                job = self->new_job( self, job_id );
183                job->submit_time = time(NULL);
184                job->flags |= FSD_JOB_CURRENT_SESSION;
185                self->jobs->add( self->jobs, job );
186                job->release( job );  job = NULL;
187         }
188        EXCEPT_DEFAULT
189         {
190                fsd_free( job_id );
191                fsd_exc_reraise();
192         }
193        FINALLY
194         {
195                if( submit )
196                        submit->destroy( submit );
197                if( job )
198                        job->release( job );
199         }
200        END_TRY
201        fsd_log_return(( " =%s", job_id ));
202        return job_id;
203}
204
205
206static fsd_job_t *
207pbsdrmaa_session_new_job( fsd_drmaa_session_t *self, const char *job_id )
208{
209        fsd_job_t *job;
210        job = pbsdrmaa_job_new( fsd_strdup(job_id) );
211        job->session = self;
212        return job;
213}
214
215void
216pbsdrmaa_session_apply_configuration( fsd_drmaa_session_t *self )
217{
218        pbsdrmaa_session_t *pbsself = (pbsdrmaa_session_t*)self;
219        fsd_conf_option_t *pbs_home = NULL;
220        fsd_conf_option_t *wait_thread_sleep_time = NULL;
221        fsd_conf_option_t *max_retries_count = NULL;
222
223        pbs_home = fsd_conf_dict_get(self->configuration, "pbs_home" );
224        wait_thread_sleep_time = fsd_conf_dict_get(self->configuration, "wait_thread_sleep_time" );
225        max_retries_count = fsd_conf_dict_get(self->configuration, "max_retries_count" );
226
227        if( pbs_home && pbs_home->type == FSD_CONF_STRING )
228          {
229                        struct stat statbuf;
230                        char * volatile log_path;
231                        struct tm tm;
232                       
233                        pbsself->pbs_home = pbs_home->val.string;
234                        fsd_log_info(("pbs_home: %s",pbsself->pbs_home));
235                        pbsself->super_wait_thread = pbsself->super.wait_thread;
236                        pbsself->super.wait_thread = pbsdrmaa_session_wait_thread;             
237                        pbsself->wait_thread_log = true;
238       
239                        time(&pbsself->log_file_initial_time); 
240                        localtime_r(&pbsself->log_file_initial_time,&tm);
241
242                        log_path = fsd_asprintf("%s/server_logs/%04d%02d%02d",
243                                        pbsself->pbs_home,
244                                        tm.tm_year + 1900,
245                                        tm.tm_mon + 1,
246                                        tm.tm_mday);
247
248                        if(stat(log_path,&statbuf) == -1)
249                          {
250                                char errbuf[256] = "InternalError";
251                                (void)strerror_r(errno, errbuf, sizeof(errbuf));
252                                fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"stat error on file %s: %s", log_path, errbuf);
253                          }
254       
255                        fsd_log_debug(("Log file %s size %d",log_path,(int) statbuf.st_size));
256                        pbsself->log_file_initial_size = statbuf.st_size;
257                        fsd_free(log_path);
258          }
259
260        if ( max_retries_count && max_retries_count->type == FSD_CONF_INTEGER)
261          {
262                pbsself->max_retries_count = max_retries_count->val.integer;
263                fsd_log_info(("Max retries count: %d", pbsself->max_retries_count));
264          }
265
266        if ( wait_thread_sleep_time && wait_thread_sleep_time->type == FSD_CONF_INTEGER)
267          {
268                pbsself->wait_thread_sleep_time = wait_thread_sleep_time->val.integer;
269                fsd_log_info(("Wait thread sleep time: %d", pbsself->wait_thread_sleep_time));
270          }
271
272        pbsself->super_apply_configuration(self); /* call method from the superclass */
273}
274
275
276void
277pbsdrmaa_session_update_all_jobs_status( fsd_drmaa_session_t *self )
278{
279        volatile bool conn_lock = false;
280        volatile bool jobs_lock = false;
281        pbsdrmaa_session_t *pbsself = (pbsdrmaa_session_t*)self;
282        fsd_job_set_t *jobs = self->jobs;
283        struct batch_status *volatile status = NULL;
284        volatile int tries_left = pbsself->max_retries_count;
285        volatile int sleep_time = 1;
286
287        fsd_log_enter((""));
288
289        TRY
290         {
291                conn_lock = fsd_mutex_lock( &self->drm_connection_mutex );
292retry:
293/* TODO: query only for user's jobs pbs_selstat + ATTR_u */
294#ifdef PBS_PROFESSIONAL
295                status = pbs_statjob( pbsself->pbs_conn, NULL, NULL, NULL );
296#else
297                status = pbs_statjob( pbsself->pbs_conn, NULL, pbsself->status_attrl, NULL );
298#endif
299                fsd_log_info(( "pbs_statjob( fd=%d, job_id=NULL, attribs={...} ) =%p", pbsself->pbs_conn, (void*)status ));
300                if( status == NULL  &&  pbs_errno != 0 )
301                 {
302                        if (pbs_errno == PBSE_PROTOCOL || pbs_errno == PBSE_EXPIRED)
303                         {
304                                if ( pbsself->pbs_conn >= 0)
305                                        pbs_disconnect( pbsself->pbs_conn );
306retry_connect:
307                                sleep(sleep_time++);
308                                pbsself->pbs_conn = pbs_connect( pbsself->super.contact );
309                                if( pbsself->pbs_conn < 0)
310                                 {
311                                        if (tries_left--)
312                                                goto retry_connect;
313                                        else
314                                                pbsdrmaa_exc_raise_pbs( "pbs_connect" );
315                                 }
316                                else
317                                        goto retry;
318                         }
319                        else
320                         {
321                                pbsdrmaa_exc_raise_pbs( "pbs_statjob" );
322                         }
323                 }
324                conn_lock = fsd_mutex_unlock( &self->drm_connection_mutex );
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                        pbs_statfree( status );
386                if( conn_lock )
387                        conn_lock = fsd_mutex_unlock( &self->drm_connection_mutex );
388                if( jobs_lock )
389                        jobs_lock = fsd_mutex_unlock( &jobs->mutex );
390         }
391        END_TRY
392
393        fsd_log_return((""));
394}
395
396
397
398struct attrl *
399pbsdrmaa_create_status_attrl(void)
400{
401        struct attrl *result = NULL;
402        struct attrl *i;
403        const int max_attribs = 16;
404        int n_attribs;
405        int j = 0;
406
407        fsd_log_enter((""));
408        fsd_calloc( result, max_attribs, struct attrl );
409        result[j++].name="job_state";
410        result[j++].name="exit_status";
411        result[j++].name="resources_used";
412        result[j++].name="ctime";
413        result[j++].name="mtime";
414        result[j++].name="qtime";
415        result[j++].name="etime";
416
417        result[j++].name="queue";
418        result[j++].name="Account_Name";
419        result[j++].name="exec_host";
420        result[j++].name="start_time";
421        result[j++].name="mtime";
422#if 0
423        result[j].name="resources_used";  result[j].resource="walltime";  j++;
424        result[j].name="resources_used";  result[j].resource="cput";  j++;
425        result[j].name="resources_used";  result[j].resource="mem";  j++;
426        result[j].name="resources_used";  result[j].resource="vmem";  j++;
427        result[j].name="Resource_List";  result[j].resource="walltime";  j++;
428        result[j].name="Resource_List";  result[j].resource="cput";  j++;
429        result[j].name="Resource_List";  result[j].resource="mem";  j++;
430        result[j].name="Resource_List";  result[j].resource="vmem";  j++;
431#endif
432        n_attribs = j;
433        for( i = result;  true;  i++ )
434                if( i+1 < result + n_attribs )
435                        i->next = i+1;
436                else
437                 {
438                        i->next = NULL;
439                        break;
440                 }
441
442#ifdef DEBUGGING
443        fsd_log_return((":"));
444        pbsdrmaa_dump_attrl( result, NULL );
445#endif
446        return result;
447}
448
449void *
450pbsdrmaa_session_wait_thread( fsd_drmaa_session_t *self )
451{
452        pbsdrmaa_log_reader_t *log_reader = NULL;
453       
454        fsd_log_enter(( "" ));
455       
456        TRY
457        {       
458                log_reader = pbsdrmaa_log_reader_new( self );
459                log_reader->read_log( log_reader );
460        }
461        FINALLY
462        {
463                pbsdrmaa_log_reader_destroy( log_reader );
464        }
465        END_TRY
466       
467        fsd_log_return(( " =NULL" ));
468        return NULL;
469}
Note: See TracBrowser for help on using the repository browser.