revise_mariadb.py 6.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
#!/usr/bin/env python

from __future__ import print_function

import argparse
import os
#import mysql.connector as mariadb
import pymysql as mariadb

def getPartitionCoreNumber(partition):
  if 'haswell' in partition: return 24
  if 'west' in partition: return 12
  if 'sandy' in partition: return 16
  if 'broadwell' in partition: return 28
  if 'gpu1' in partition: return 16
  if 'gpu2' in partition: return 24
  if 'smp1' in partition: return 32
  if 'smp2' in partition: return 64
  if 'knl' in partition: return 64
  if 'ml' in partition: return 176
  else: return 0

def getStatus(state):
  statusdict = {}
  statusdict[0] = 'pending'
  statusdict[1] = 'running'
  statusdict[2] = 'suspended'
  statusdict[3] = 'completed'
  statusdict[4] = 'cancelled'
  statusdict[5] = 'failed'
  statusdict[6] = 'timeout'
  statusdict[7] = 'failed'
  statusdict[8] = 'failed'
  statusdict[9] = 'failed'
  statusdict[10] = 'failed'
  statusdict[11] = 'OOM'

  if state >= 0 and state <= 11:
    return statusdict[state]
  else:
    return statusdict[1]


def connect_to_mariadb_slurm():
  #connection data for MariaDB 
  HOST = os.environ["MARIADB_SLURM_HOST"]
  PORT = os.environ["MARIADB_SLURM_PORT"]
  USER = os.environ["MARIADB_SLURM_USER"]
  PASSWORD = os.environ["MARIADB_SLURM_PASSWORD"]
  DATABASE = os.environ["MARIADB_SLURM_DATABASE"]

  mariadb_connection = mariadb.connect(host=HOST, port=int(PORT),
                                       user=USER, passwd=PASSWORD,
                                       db=DATABASE, connect_timeout=10)
  return mariadb_connection

def connect_to_mariadb_prope():
  #connection data for MariaDB 
  HOST = os.environ["MARIADB_HOST"]
  PORT = os.environ["MARIADB_PORT"]
  USER = os.environ["MARIADB_USER"]
  PASSWORD = os.environ["MARIADB_PASSWORD"]
  DATABASE = os.environ["MARIADB_DATABASE"]

  mariadb_connection = mariadb.connect(host=HOST, port=int(PORT),
                                       user=USER, passwd=PASSWORD,
                                       db=DATABASE, connect_timeout=10)
  return mariadb_connection

def get_slurm_metadata(mariadb_connection, start, end):
  cursor = mariadb_connection.cursor()

  sql_cmd = """select
                id_job as JID,
                account as PROJECT,
                state,
                nodes_alloc as NUM_NODES,
                nodelist as NODELIST,
                cpus_req as NUM_CORES,
                time_start as START,
                time_end as END,
                job_name as NAME,
                (timelimit * 60) as WALLTIME,
                time_submit as SUBMIT,
                `partition` as P_PARTITION,
                id_array_job as ARRAY_ID
                from taurus_job_table
                where time_start >= """ + str(start) + """
                and time_start <= """ + str(end) #+ """ limit 10"""

  #print(sql_cmd)
  try:
    cursor.execute(sql_cmd)
  except mariadb.Error as error:
    print("Error: {}".format(error))

  results = cursor.fetchall()
  all_meta_data = []
  for job_data in results:
    meta_data = {}
    meta_data['JID'] = job_data[0]
    meta_data['PROJECT'] = job_data[1]
    meta_data['state'] = job_data[2]
    meta_data['NUM_NODES'] = job_data[3]
    meta_data['NODELIST'] = job_data[4]
    meta_data['NUM_CORES'] = job_data[5]
    meta_data['START'] = job_data[6]
    meta_data['END'] = job_data[7]
    meta_data['NAME'] = job_data[8]
    meta_data['WALLTIME'] = job_data[9]
    meta_data['SUBMIT'] = job_data[10]
    meta_data['P_PARTITION'] = job_data[11]
    meta_data['ARRAY_ID'] = job_data[12]

    meta_data['STATUS'] = getStatus(meta_data['state'])
    core_num = meta_data['NUM_NODES'] * getPartitionCoreNumber(meta_data['P_PARTITION'])
    if core_num == meta_data['NUM_CORES']:
      meta_data['EXCLUSIVE'] = 1
    else:
      meta_data['EXCLUSIVE'] = 0
    
    all_meta_data.append(meta_data)

  cursor.close()

  return all_meta_data

def update_prope(mariadb_connection, slurm_data):
  cursor = mariadb_connection.cursor()

  sql_filter = " WHERE JID=" + str(slurm_data['JID']) + " AND START BETWEEN " + str(slurm_data['START']) + "-600 AND " + str(slurm_data['START']) + "+600"

  sql_cmd = "SELECT JID,EXCLUSIVE FROM Job_Data" + str(sql_filter) + " LIMIT 1"

  try:
    cursor.execute(sql_cmd)
  except mariadb.Error as error:
    print("Error: {}".format(error))

  results = cursor.fetchone()
  if results is not None:
    #print('Correct JID:',results[0])
    exclusive = results[1]
    sql_cmd = "UPDATE Job_Data SET "
    sql_cmd += "PROJECT=\"" + str(slurm_data['PROJECT']) + "\","
    sql_cmd += "STATUS=\"" + str(slurm_data['STATUS']) + "\","
    sql_cmd += "NUM_CORES=" + str(slurm_data['NUM_CORES']) + ","
    sql_cmd += "END=" + str(slurm_data['END']) + ","
    try:
      sql_cmd += "NAME=\"" + str(slurm_data['NAME']) + "\","
    except:
      sql_cmd += "NAME=\"unreadable\","
    sql_cmd += "WALLTIME=" + str(slurm_data['WALLTIME']) + ","
    sql_cmd += "SUBMIT=" + str(slurm_data['SUBMIT']) + ","
    sql_cmd += "P_PARTITION=\"" + str(slurm_data['P_PARTITION']) + "\","
    if exclusive == 0:
      sql_cmd += "EXCLUSIVE="+ str(slurm_data['EXCLUSIVE']) + ","
    sql_cmd += "ARRAY_ID=" + str(slurm_data['ARRAY_ID']) + str(sql_filter)

    try:
      cursor.execute(sql_cmd)
      mariadb_connection.commit()
    except mariadb.Error as error:
      print("Error: {}".format(error))

  cursor.close()


def main(start, end):
  mariadb_slurm_connection = connect_to_mariadb_slurm()
  mariadb_prope_connection = connect_to_mariadb_prope()


  # get slurm data
  slurm_meta_data = get_slurm_metadata(mariadb_slurm_connection, start, end)

  #if no data found exit
  if len(slurm_meta_data) == 0:
    exit(1)

  for slurm_data in slurm_meta_data:
    # print("--")
    # print('JID from Backup:',slurm_data['JID'])
    # print('PROJECT:',slurm_data['PROJECT'])
    # print('STATUS:',slurm_data['STATUS'])
    # print('NUM_NODES:',slurm_data['NUM_NODES'])
    # print('NODELIST:',slurm_data['NODELIST'])
    # print('NUM_CORES:',slurm_data['NUM_CORES'])
    # print('START:',slurm_data['START'])
    # print('END:',slurm_data['END'])
    # print('NAME:',slurm_data['NAME'])
    # print('WALLTIME:',slurm_data['WALLTIME'])
    # print('P_PARTITION:',slurm_data['P_PARTITION'])
    # print('EXCLUSIVE:',slurm_data['EXCLUSIVE'])
    # print('ARRAY_ID:',slurm_data['ARRAY_ID'])
    update_prope(mariadb_prope_connection, slurm_data)


  mariadb_slurm_connection.close()
  mariadb_prope_connection.close()
  print("Data correction finished sucessfully.")


def parse_args():
  parser = argparse.ArgumentParser()
  parser.add_argument('--start', type=int, required=True)
  parser.add_argument('--end', type=int, required=True)
  return parser.parse_args()


if __name__ == '__main__':
  args = parse_args()
  main(start=args.start,
       end=args.end)