~ubuntu-branches/ubuntu/vivid/slurm-llnl/vivid

« back to all changes in this revision

Viewing changes to src/database/mysql_common.c

  • Committer: Bazaar Package Importer
  • Author(s): Gennaro Oliva
  • Date: 2009-09-24 23:28:15 UTC
  • mfrom: (1.1.11 upstream) (3.2.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090924232815-enh65jn32q1ebg07
Tags: 2.0.5-1
* New upstream release 
* Changed dependecy from lib-mysqlclient15 to lib-mysqlclient 
* Added Default-Start for runlevel 2 and 4 and $remote_fs requirement in
  init.d scripts (Closes: #541252)
* Postinst checks for wrong runlevels 2 and 4 links
* Upgraded to standard version 3.8.3
* Add lintian overrides for missing slurm-llnl-configurator.html in doc
  base registration
* modified postrm scripts to ignore pkill return value in order to avoid
  postrm failure when no slurm process is running
* Checking for slurmctld.pid before cancelling running and pending
  jobs during package removal 

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 *  Written by Danny Auble <da@llnl.gov>
8
8
 *  
9
9
 *  This file is part of SLURM, a resource management program.
10
 
 *  For details, see <http://www.llnl.gov/linux/slurm/>.
 
10
 *  For details, see <https://computing.llnl.gov/linux/slurm/>.
 
11
 *  Please also read the included file: DISCLAIMER.
11
12
 *  
12
13
 *  SLURM is free software; you can redistribute it and/or modify it under
13
14
 *  the terms of the GNU General Public License as published by the Free
47
48
pthread_mutex_t mysql_lock = PTHREAD_MUTEX_INITIALIZER;
48
49
#endif
49
50
 
50
 
#ifdef HAVE_MYSQL
51
 
 
52
51
static char *table_defs_table = "table_defs_table";
53
52
 
54
 
static int _clear_results(MYSQL *mysql_db)
55
 
{
56
 
        MYSQL_RES *result = NULL;
57
 
        int rc = 0;
58
 
        do {
59
 
                /* did current statement return data? */
60
 
                if((result = mysql_store_result(mysql_db)))
61
 
                        mysql_free_result(result);
62
 
                
63
 
                /* more results? -1 = no, >0 = error, 0 = yes (keep looping) */
64
 
                if ((rc = mysql_next_result(mysql_db)) > 0)
65
 
                        error("Could not execute statement %d %s\n",
66
 
                              mysql_errno(mysql_db),
67
 
                              mysql_error(mysql_db));
68
 
        } while (rc == 0);
69
 
 
70
 
        if(rc > 0) {
71
 
                errno = rc;
72
 
                return SLURM_ERROR;
73
 
        } 
74
 
 
75
 
        return SLURM_SUCCESS;
76
 
}
77
 
 
78
53
static MYSQL_RES *_get_first_result(MYSQL *mysql_db)
79
54
{
80
55
        MYSQL_RES *result = NULL;
346
321
        char create_line[50];
347
322
        MYSQL *mysql_db = NULL;
348
323
        int rc = SLURM_ERROR;
 
324
        
 
325
        MYSQL *db_ptr = NULL;
 
326
        char *db_host = NULL;
349
327
 
350
328
        while(rc == SLURM_ERROR) {
351
329
                rc = SLURM_SUCCESS;
355
333
                if(!(mysql_db = mysql_init(mysql_db)))
356
334
                        fatal("mysql_init failed: %s", mysql_error(mysql_db));
357
335
                
358
 
                if(mysql_real_connect(mysql_db, 
359
 
                                      db_info->host, db_info->user,
360
 
                                      db_info->pass, NULL, 
361
 
                                      db_info->port, NULL, 0)) {
 
336
                db_host = db_info->host;
 
337
                db_ptr = mysql_real_connect(mysql_db,
 
338
                                            db_host, db_info->user,
 
339
                                            db_info->pass, NULL,
 
340
                                            db_info->port, NULL, 0);
 
341
 
 
342
                if (!db_ptr && db_info->backup) {
 
343
                        info("Connection failed to host = %s "
 
344
                             "user = %s port = %u",
 
345
                             db_host, db_info->user,
 
346
                             db_info->port);  
 
347
                        db_host = db_info->backup;
 
348
                        db_ptr = mysql_real_connect(mysql_db, db_host,
 
349
                                                    db_info->user,
 
350
                                                    db_info->pass, NULL,
 
351
                                                    db_info->port, NULL, 0);
 
352
                }
 
353
 
 
354
                if (db_ptr) {
362
355
                        snprintf(create_line, sizeof(create_line),
363
356
                                 "create database %s", db_name);
364
357
                        if(mysql_query(mysql_db, create_line)) {
369
362
                        mysql_close_db_connection(&mysql_db);
370
363
                } else {
371
364
                        info("Connection failed to host = %s "
372
 
                             "user = %s pass = %s port = %u",
373
 
                             db_info->host, db_info->user,
374
 
                             db_info->pass, db_info->port);
 
365
                             "user = %s port = %u",
 
366
                             db_host, db_info->user,
 
367
                             db_info->port);
375
368
#ifdef MYSQL_NOT_THREAD_SAFE
376
369
                        slurm_mutex_unlock(&mysql_lock);
377
370
#endif
392
385
extern int *destroy_mysql_db_info(mysql_db_info_t *db_info)
393
386
{
394
387
        if(db_info) {
 
388
                xfree(db_info->backup);
395
389
                xfree(db_info->host);
396
390
                xfree(db_info->user);
397
391
                xfree(db_info->pass);
406
400
        int rc = SLURM_SUCCESS;
407
401
        bool storage_init = false;
408
402
        
 
403
        char *db_host = db_info->host;
 
404
 
409
405
        if(!(*mysql_db = mysql_init(*mysql_db)))
410
406
                fatal("mysql_init failed: %s", mysql_error(*mysql_db));
411
407
        else {
418
414
                mysql_options(*mysql_db, MYSQL_OPT_CONNECT_TIMEOUT,
419
415
                              (char *)&my_timeout);
420
416
                while(!storage_init) {
421
 
                        if(!mysql_real_connect(*mysql_db, db_info->host,
 
417
                        if(!mysql_real_connect(*mysql_db, db_host,
422
418
                                               db_info->user, db_info->pass,
423
419
                                               db_name, db_info->port,
424
420
                                               NULL, CLIENT_MULTI_STATEMENTS)) {
431
427
                                              "%d %s",
432
428
                                              mysql_errno(*mysql_db),
433
429
                                              mysql_error(*mysql_db));
 
430
                                        if ((db_host == db_info->host)
 
431
                                            && db_info->backup) {
 
432
                                                db_host = db_info->backup;
 
433
                                                continue;
 
434
                                        }
434
435
                                        rc = SLURM_ERROR;
435
436
                                        break;
436
437
                                }
468
469
        return SLURM_SUCCESS;
469
470
}
470
471
 
 
472
extern int mysql_clear_results(MYSQL *mysql_db)
 
473
{
 
474
        MYSQL_RES *result = NULL;
 
475
        int rc = 0;
 
476
        do {
 
477
                /* did current statement return data? */
 
478
                if((result = mysql_store_result(mysql_db)))
 
479
                        mysql_free_result(result);
 
480
                
 
481
                /* more results? -1 = no, >0 = error, 0 = yes (keep looping) */
 
482
                if ((rc = mysql_next_result(mysql_db)) > 0)
 
483
                        error("Could not execute statement %d %s\n",
 
484
                              mysql_errno(mysql_db),
 
485
                              mysql_error(mysql_db));
 
486
        } while (rc == 0);
 
487
 
 
488
        if(rc > 0) {
 
489
                errno = rc;
 
490
                return SLURM_ERROR;
 
491
        } 
 
492
 
 
493
        return SLURM_SUCCESS;
 
494
}
 
495
 
471
496
extern int mysql_db_query(MYSQL *mysql_db, char *query)
472
497
{
473
498
        if(!mysql_db)
476
501
        slurm_mutex_lock(&mysql_lock);
477
502
#endif
478
503
        /* clear out the old results so we don't get a 2014 error */
479
 
        _clear_results(mysql_db);               
 
504
        mysql_clear_results(mysql_db);          
480
505
//try_again:
481
506
        if(mysql_query(mysql_db, query)) {
482
 
                /* if(mysql_errno(mysql_db) == CR_SERVER_GONE_ERROR) { */
483
 
/*                      /\* FIX ME: this means the connection went away *\/ */
484
 
/*              } */
485
 
 
486
507
                error("mysql_query failed: %d %s\n%s",
487
508
                      mysql_errno(mysql_db),
488
509
                      mysql_error(mysql_db), query);
490
511
#ifdef MYSQL_NOT_THREAD_SAFE
491
512
                slurm_mutex_unlock(&mysql_lock);
492
513
#endif
 
514
                /* FIXME: If we get ER_LOCK_WAIT_TIMEOUT here we need
 
515
                to restart the connections, but it appears restarting
 
516
                the calling program is the only way to handle this.
 
517
                If anyone in the future figures out a way to handle
 
518
                this, super.  Until then we will need to restart the
 
519
                calling program if you ever get this error. 
 
520
                */
 
521
 
493
522
                return SLURM_ERROR;
494
523
        }
495
524
 
502
531
extern int mysql_db_ping(MYSQL *mysql_db)
503
532
{
504
533
        /* clear out the old results so we don't get a 2014 error */
505
 
        _clear_results(mysql_db);               
 
534
        mysql_clear_results(mysql_db);          
506
535
        return mysql_ping(mysql_db);
507
536
}
508
537
 
512
541
        slurm_mutex_lock(&mysql_lock);
513
542
#endif
514
543
        /* clear out the old results so we don't get a 2014 error */
515
 
        _clear_results(mysql_db);               
 
544
        mysql_clear_results(mysql_db);          
516
545
        if(mysql_commit(mysql_db)) {
517
546
                error("mysql_commit failed: %d %s",
518
547
                      mysql_errno(mysql_db),
535
564
        slurm_mutex_lock(&mysql_lock);
536
565
#endif
537
566
        /* clear out the old results so we don't get a 2014 error */
538
 
        _clear_results(mysql_db);               
 
567
        mysql_clear_results(mysql_db);          
539
568
        if(mysql_rollback(mysql_db)) {
540
569
                error("mysql_commit failed: %d %s",
541
570
                      mysql_errno(mysql_db),
578
607
        int rc = SLURM_SUCCESS;
579
608
                
580
609
        if((rc = mysql_db_query(mysql_db, query)) != SLURM_ERROR)  
581
 
                rc = _clear_results(mysql_db);
 
610
                rc = mysql_clear_results(mysql_db);
582
611
        
583
612
        return rc;
584
613
}
655
684
        return _mysql_make_table_current(mysql_db, table_name,
656
685
                                         first_field, ending);
657
686
}
658
 
 
659
 
 
660
 
#endif
661