-
Notifications
You must be signed in to change notification settings - Fork 704
Expand file tree
/
Copy pathclient.py
More file actions
270 lines (227 loc) · 8.8 KB
/
Copy pathclient.py
File metadata and controls
270 lines (227 loc) · 8.8 KB
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
# Copyright 2020 The SQLFlow Authors. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License
import ast
import json
import random
import string
import time
from enum import Enum
from runtime.dbapi.pyalisa.pop import Pop
class AlisaTaksStatus(Enum):
ALISA_TASK_WAITING = 1
ALISA_TASK_RUNNING = 2
ALISA_TASK_COMPLETED = 3
ALISA_TASK_ERROR = 4
ALISA_TASK_FAILOVER = 5
ALISA_TASK_KILLED = 6
ALISA_TASK_RERUN = 8
ALISA_TASK_EXPIRED = 9
ALISA_TASK_ALISA_RERUN = 10
ALISA_TASK_ALLOCATE = 11
# used to deal with too many logs
MAX_LOG_NUM = 2000
class Client(object):
"""Client for building kinds of tasks and submitting them to alisa gateway
Args:
config(Config): the Config(runtime.dbapi.pyalisa.config)
"""
def __init__(self, config):
self.config = config # noqa F841
def _base_params(self):
# use gmtime(UTC+0) here instead of localtime
ts = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
nonce = "".join(random.sample(string.ascii_letters, 32))
return {
"Timestamp": ts,
"AccessKeyId": self.config.pop_access_id,
"SignatureMethod": "HMAC-SHA1",
"SignatureVersion": "1.0",
"SignatureNonce": nonce,
"Format": "JSON",
"Product": "dataworks",
"Version": "2017-12-12",
}
def create_sql_task(self, code):
"""Create a SQL task and return the result
Args:
code(string): the SQL program to run
Returns:
a (taskId, statu) tuple
"""
params = self._base_params()
params["ExecCode"] = code
params["PluginName"] = self.config.withs["PluginName"]
params["Exec"] = self.config.withs["Exec"]
return self._create_task(params)
def create_pyodps_task(self, code, args):
"""Create a pyodps task and return the result
Args:
code(string): the pyodps program to run
Returns:
a (taskId, status) tuple
"""
params = self._base_params()
params["ExecCode"] = code
params["PluginName"] = self.config.withs["PluginName4PyODPS"]
params["Exec"] = self.config.withs["Exec4PyODPS"]
if len(args) > 0:
params["Args"] = args
return self._create_task(params)
def _create_task(self, params):
""" Create returns a task id and it's status
Args:
params(dict): kinds of request params, both keys and values
should be strings
Returns:
a (taskId, status) tuple
"""
params["CustomerId"] = self.config.withs["CustomerId"]
params["UniqueKey"] = str(time.time())
params["ExecTarget"] = self.config.env["ALISA_TASK_EXEC_TARGET"]
nenv = dict(self.config.env)
# display column type, for feature derivation.
nenv["SHOW_COLUMN_TYPE"] = "true"
params["Envs"] = json.dumps(nenv)
val = self._request_and_parse_response("CreateAlisaTask", params)
return val["alisaTaskId"], val["status"]
def get_status(self, task_id):
"""Get the status of given task
Args:
task_id(string): the task id returned by create_task
Returns:
an AlisaTaksStatus indicating the status
"""
params = self._base_params()
params["AlisaTaskId"] = task_id
val = self._request_and_parse_response("GetAlisaTask", params)
return AlisaTaksStatus(int(val["status"]))
def completed(self, status):
"""Check whether the given status is a finish status
Args:
status(AlisaTaksStatus|int): the status to check
Returns:
True for finish status, Flase otherwise
"""
if isinstance(status, int):
status = AlisaTaksStatus(status)
return status in [
AlisaTaksStatus.ALISA_TASK_COMPLETED,
AlisaTaksStatus.ALISA_TASK_ERROR,
AlisaTaksStatus.ALISA_TASK_KILLED,
AlisaTaksStatus.ALISA_TASK_RERUN,
AlisaTaksStatus.ALISA_TASK_EXPIRED
]
def read_logs(self, task_id, offset, w):
"""Read logs for given task
Args:
task_id(string): the task to retrival logs
offset(int): the log offset from where to read
w(writer): the output stream to write the log
Returns:
an integer: -1 if the log is read completely, or,
a positive integer for the end of current reading
"""
for _ in range(MAX_LOG_NUM):
params = self._base_params()
params["AlisaTaskId"] = task_id
params["Offset"] = str(offset)
log = self._request_and_parse_response("GetAlisaTaskLog", params)
rlen = int(log["readLength"])
if rlen == 0:
return offset
offset += rlen
w.write(log["logMsg"])
if bool(log["isEnd"]):
return -1
return offset
def count_results(self, task_id):
"""Retrival the result count for given task
Args:
task_id(string): the task to query
Returns:
an integer indicating the result count
"""
params = self._base_params()
params["AlisaTaskId"] = task_id
res = self._request_and_parse_response("GetAlisaTaskResultCount",
params)
return int(res)
def get_results(self, task_id, batch):
"""Reads the task's results
Args:
task_id(string): the task to read
batch(int): batch size for retrival the result
Returns:
a list of query results
"""
if batch <= 0:
raise ValueError("batch should greater than 0")
count = self.count_results(task_id)
columns, body = [], []
for i in range(0, count, batch):
params = self._base_params()
params["AlisaTaskId"] = task_id
params["Start"] = str(i)
params["Limit"] = str(batch)
val = self._request_and_parse_response("GetAlisaTaskResult",
params)
header, rows = self._parse_alisa_value(val)
if len(columns) == 0:
columns = header
body.extend(rows)
return {"columns": columns, "body": body}
def stop(self, task_id):
"""Stop given task.
NOTE(weiguoz): need to be tested.
Args:
task_id(string): the task to stop
Returns:
True if the task is stopped, False otherwise
"""
params = self._base_params()
params["AlisaTaskId"] = task_id
res = self._request_and_parse_response("StopAlisaTask", params)
return bool(res)
def _parse_alisa_value(self, val):
"""Parse 'returnValue' in alisa response
https://github.com/sql-machine-learning/goalisa/blob/68d3aad1344c9e5c0cd35c6556e1f3f2b6ca9db7/alisa.go#L190
Args:
val: [{u'resultMsg': u'[["Alice","23.8","56000"]]',
u'dataHeader': u'["name::string","age::double","salary::bigint"]'}]
"""
jsval = ast.literal_eval(json.dumps(val))
columns = []
for h in json.loads(jsval['dataHeader']):
nt = h.split("::")
name, typ = (nt[0], nt[1]) if len(nt) == 2 else (h, "string")
columns.append({"name": str(name), "typ": str(typ)})
body = []
for m in json.loads(jsval['resultMsg']):
row = []
for i in ast.literal_eval(json.dumps(m)):
row.append(i)
body.append(row)
return columns, body
def _request_and_parse_response(self, action, params):
params["Action"] = action
params["ProjectEnv"] = self.config.env["SKYNET_SYSTEM_ENV"]
url = self.config.pop_scheme + "://" + self.config.pop_url
code, buf = Pop.request(url, params, self.config.pop_access_secret)
resp = json.loads(buf)
if code != 200:
raise RuntimeError("%s got a bad result, request=%s, response=%s" %
(code, params, buf))
if resp['returnCode'] != '0':
raise Exception("returned an error request={}, response={}".format(
params, resp))
return resp["returnValue"]