-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_client.py
383 lines (346 loc) · 14 KB
/
sql_client.py
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# Source:
#https://github.com/GoogleCloudPlatform/getting-started-python/blob/master/2-structured-data/bookshelf/model_cloudsql.py
# And:
#https://cloud.google.com/python/getting-started/using-cloud-sql
# SOURCE: for only the redis part
#https://kubernetes.io/docs/tasks/job/fine-parallel-processing-work-queue/
####################################################################################################
# SCRIPTS:
from config import *
import utilities
import rediswq
import argparse
# PACKAGES
from copy import deepcopy
import time
import random
from datetime import datetime
from sqlalchemy import Column, ForeignKey, Integer, String, Float, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy import create_engine
from sqlalchemy.sql import exists
####################
### Input arguments
parser = argparse.ArgumentParser(description='Grab Master flag')
parser.add_argument('-m', '--main', type=int, default=False,
help='if nonzero, it strictly deals with adding items from main-sql to main queue')
parser.add_argument('-r', '--run', type=int, default=False,
help='if nonzero, it strictly deals with grabbing items from run queue')
# ^'run' phased out...see closed issue on GitHub
parser.add_argument('-c', '--complete', type=int, default=False,
help='if nonzero, it strictly deals with grabbing items from complete queue')
parser.add_argument('-R', '--reset', type=int, default=False,
help='if nonzero, delete existing tables and create new')
parser.add_argument('-C', '--create', type=int, default=False,
help='if nonzero, assume no tables exist and create new')
args = parser.parse_args()
####################
### Create SQL Table
Base = declarative_base()
dbFilePath = 'mysql+pymysql://%s:%s@%s:3306/%s' % (CLOUDSQL_SERVER_USER,
CLOUDSQL_SERVER_PASSWORD,
CLOUDSQL_SERVER_IP,
CLOUDSQL_DATABASE)
engine = create_engine(dbFilePath, echo=False)
DBSession = sessionmaker(bind=engine)
session = DBSession()
class ParameterSpace(Base):
"""
"O2" : 0.21,
"N2" : 0.7772982887,
"H2O" : 0.0123,
"CH4" : 0.00000163,
"CO2" : 0.0004,
"H2" : 0.0000000813,
"""
__tablename__ = 'parameterspace'
id = Column(Integer, primary_key=True)
hash = Column(String(256))
previous_hash = Column(String(256))
code = String(256)
O2 = Column(Float)
N2 = Column(Float)
H2O = Column(Float)
CH4 = Column(Float)
CO2 = Column(Float)
H2 = Column(Float)
state = Column(String(256))
session_start_time = Column(DateTime)
session_end_time = Column(DateTime)
bucket_path = Column(String(256))
# atmos metadata
stable = Column(String(256))
atmos_start_time = Column(String(256))
photochem_duration = Column(String(256))
photochem_iterations = Column(String(256))
clima_duration = Column(String(256))
atmos_run_duration = Column(String(256))
run_iteration_call = Column(String(256))
input_max_clima_iterations = Column(String(256))
input_max_photochem_iterations = Column(String(256))
temperature = Column(String(256))
pressure = Column(String(256))
flux_CO2 = Column(String(256))
flux_CH4 = Column(String(256))
flux_CO = Column(String(256))
flux_H2O = Column(String(256))
flux_NH3 = Column(String(256))
flux_O3 = Column(String(256))
#
def __init__(self, parameter_dict, prev_hash):
self.hash = utilities.param_hash(parameter_dict)
self.code = utilities.param_encode(parameter_dict)
self.O2 = parameter_dict['O2']
self.N2 = parameter_dict['N2']
self.H2O = parameter_dict['H2O']
self.CH4 = parameter_dict['CH4']
self.CO2 = parameter_dict['CO2']
self.H2 = parameter_dict['H2']
self.state = "queue"
self.previous_hash = prev_hash
self.session_start_time = datetime.utcnow()
if args.reset:
print("Deleting old table...")
# delete any old table there
ParameterSpace.__table__.drop(engine)
else:
pass
if args.create or args.reset:
print("Creating table...")
# Create all tables in the engine. This is equivalent to "Create Table" statements in raw SQL.
Base.metadata.create_all(engine)
else:
print("Grabbing existing table...")
pass
####################
### SQL Read + Write Functions
def add_db(data, dtype="dict", prev_hash=None):
# Insert a Person in the person table
if dtype == "dict":
dicted = data
elif dtype == "code":
dicted = utilities.param_decode(data)
else:
return "didn't recognize 'dtype'"
point = ParameterSpace(dicted, prev_hash)
hashed = point.hash
session.add(point)
session.commit()
# check that the point now exists
forgive = 0
while forgive < 5:
ret = session.query(exists().where(ParameterSpace.hash==hashed)).scalar()
if not ret:
forgive += 1
time.sleep(5)
else:
break
if not ret:
return "tried to add; couldn't find [%s] in database table" % (hashed)
else:
pass
return "added: %s" % point.hash
def run_db(data, dtype="dict"):
if dtype == "dict":
hashed = utilities.param_hash(data)
elif dtype == "code":
dicted = utilities.param_decode(data)
hashed = utilities.param_hash(dicted)
else:
return "didn't recognize 'dtype'"
forgive = 0
while forgive < 5: #add forgiveness buffer to make sure it has the time to get added to sql db
point = session.query(ParameterSpace).filter_by(hash=hashed).first()
if point is None:
forgive += 1
time.sleep(5)
else:
break
if point is None:
return "tried to run; couldn't find [%s] in database table" % (hashed)
else:
pass
point.state = "running"
session.commit()
return "running: %s" % point.hash
# def error_db(msg, data, dtype="dict"):
# if dtype == "dict":
# hashed = utilities.param_hash(data)
# elif dtype == "code":
# dicted = utilities.param_decode(data)
# hashed = utilities.param_hash(dicted)
# else:
# return "didn't recognize 'dtype'"
# point = session.query(ParameterSpace).filter_by(hash=hashed).first()
# point.state = "Error"
# point.error_msg = str(msg) #<-exapnd on this
# point.end_time = datetime.utcnow()
# session.commit()
# return "errored: %s - %s" % (point.hash, msg)
def complete_db(data, run_status, stability, metadata_dict, dtype="dict"):
if dtype == "dict":
hashed = utilities.param_hash(data)
elif dtype == "code":
dicted = utilities.param_decode(data)
hashed = utilities.param_hash(dicted)
else:
return "didn't recognize 'dtype'"
forgive = 0
while forgive < 5: #add forgiveness buffer to make sure it has the time to get added to sql db
point = session.query(ParameterSpace).filter_by(hash=hashed).first()
if point is None:
forgive += 1
time.sleep(5)
else:
break
if point is None:
return "tried to complete; couldn't find [%s] in database table" % (hashed)
else:
pass
point.state = run_status
point.stable = stability
point.session_end_time = datetime.utcnow()
point.bucket_path = JOB_STORAGE_PATH
# metadata
# session.commit doesn't acknowledge changes like this: point.__dict__[key] = metadata_dict[key]
# so hard code the updates for each attribute
point.previous_hash = metadata_dict['previous_hash']
point.run_iteration_call = metadata_dict['run_iteration_call']
point.atmos_start_time = metadata_dict['atmos_start_time']
point.photochem_duration = metadata_dict['photochem_duration']
point.photochem_iterations = metadata_dict['photochem_iterations']
point.clima_duration = metadata_dict['clima_duration']
point.atmos_run_duration = metadata_dict['atmos_run_duration']
point.input_max_clima_iterations = metadata_dict['input_max_clima_iterations']
point.input_max_photochem_iterations = metadata_dict['input_max_photochem_iterations']
point.temperature = metadata_dict['temperature']
point.pressure = metadata_dict['pressure']
point.flux_CO2 = metadata_dict['flux_CO2']
point.flux_CH4 = metadata_dict['flux_CH4']
point.flux_CO = metadata_dict['flux_CO']
point.flux_H2O = metadata_dict['flux_H2O']
point.flux_NH3 = metadata_dict['flux_NH3']
point.flux_O3 = metadata_dict['flux_O3']
session.commit()
return "completed: %s - %s" % (point.hash, run_status)
def delete_db(data, dtype="dict"):
if dtype == "dict":
hashed = utilities.param_hash(data)
elif dtype == "code":
dicted = utilities.param_decode(data)
hashed = utilities.param_hash(dicted)
else:
return "didn't recognize 'dtype'"
point = session.query(ParameterSpace).filter_by(hash=hashed).first()
session.delete(point)
session.commit()
return "deleted: %s" % point.hash
def exists_db(data, dtype="dict"):
if dtype == "dict":
hashed = utilities.param_hash(data)
elif dtype == "code":
dicted = utilities.param_decode(data)
hashed = utilities.param_hash(dicted)
else:
return "didn't recognize 'dtype'"
ret = session.query(exists().where(ParameterSpace.hash==hashed)).scalar()
return ret
####################
### Pull items from Redis Queue for Read + Write
q = rediswq.RedisWQ(name=REDIS_SERVER_NAME, host=REDIS_SERVER_IP)
# default to main if nothing else:
if (not args.run) and (not args.complete):
args.main = True
else:
pass
if args.complete:
print("Created Write SQL Client for 'Complete Queue'")
while not q.kill():
packed_code = q.get("complete", block=True, timeout=30)
if packed_code is not None:
unpacked_list = utilities.unpack_items(packed_code)
param_code = unpacked_list[0]
atmos_output = unpacked_list[1]
stable_atmosphere = unpacked_list[2]
metadata_dict = utilities.metadata_decode(unpacked_list[3])
msg = complete_db(data=param_code,
dtype="code",
run_status=atmos_output,
stability=stable_atmosphere,
metadata_dict=metadata_dict)
print(msg)
if msg[:5] == "tried":
# failed to add, add back to queue
q.put(value=packed_code, queue="complete")
else:
pass
else:
pass
elif args.main: #master True
print("Created Read/Write SQL Client for 'Main Queue'")
checkpoint_time = time.time() + random.random()*RECHECK_QUEUE #<- add some random buffer if launching multiple sql client simultaneously
while not q.kill():
#if q.size("complete")+q.size("run")+q.size("main sql")+q.size("main") == 0:
# instead, check every .25hour
if time.time() >= checkpoint_time + RECHECK_QUEUE*60*60:
points = session.query(ParameterSpace).filter_by(state='queue')
for point in points:
timedelta = datetime.utcnow() - point.session_start_time
timedelta = timedelta.days * 24 * 3600 + timedelta.seconds
if timedelta > MAX_JOB_QUEUE_TIME:
#add points back to queue
print("re-queueing: %s - was on for %d seconds" % (point.hash, timedelta))
point.state = "queue"
point.session_start_time = datetime.utcnow()
run_dict = deepcopy(start)
for mol in start.keys():
run_dict[mol] = point.__dict__[mol]
packed_items = utilities.pack_items( [utilities.param_encode(run_dict), point.previous_hash, "0"] )
session.commit()
q.put(packed_items, "main")
else:
pass
checkpoint_time = time.time()
else:
pass
packed_items = q.get("main sql", block=True, timeout=30)
if packed_items is not None:
next_param_code, prev_param_hash, explore_count = utilities.unpack_items(packed_items)
if not exists_db(next_param_code, dtype="code"): #check if item in DB already
explore_count = 0
packed_items = utilities.pack_items( [next_param_code, prev_param_hash, str(explore_count)] )
msg = add_db(data=next_param_code, dtype="code", prev_hash=prev_param_hash)
q.put(packed_items, "main")
print(msg)
else:
# already in the DB...check if it also completed
dicted = utilities.param_decode(next_param_code)
hashed = utilities.param_hash(dicted)
point = session.query(ParameterSpace).filter_by(hash=hashed).first()
if point.temperature is None: # didn't complete, ignore
print("repeat: %s" % hashed)
pass
elif int(explore_count) < EXPLORE_LIMIT: #did complete, and didn't already pass limit
explore_count = 1 + int(explore_count)
# add point to queue only so it explores
packed_items = utilities.pack_items( [next_param_code, "explore only", str(explore_count)] )
q.put(packed_items, "main")
print("re-explore: %s" % hashed)
else: #did complete, but passed limit
pass
else:
pass
''' # removed because the sql server didn't update fast enough...see issue on GitHub
elif args.run:
print("Created Write SQL Client for 'Run Queue'")
while not q.kill():
param_code = q.get("run", block=True, timeout=30)
if param_code is not None:
msg = run_db(data=param_code, dtype="code")
print(msg)
else:
pass
'''
else:
pass