-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcluster.py
175 lines (157 loc) · 6.97 KB
/
cluster.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
# !/usr/bin/env python
# coding: utf-8
import os
import re
import sys
import json
import string
import logging
from random import sample
from batchcompute import CN_BEIJING
from batchcompute import Client, ClientError
from batchcompute.resources import (
Configs, Networks, VPC, AutoCluster,
JobDescription, TaskDescription, DAG, Mounts,
)
REGION = {
'BEIJING': 'batchcompute.cn-beijing.aliyuncs.com',
'HANGZHOU': 'batchcompute.cn-hangzhou.aliyuncs.com',
'HUHEHAOTE': 'batchcompute.cn-huhehaote.aliyuncs.com',
'SHANGHAI': 'batchcompute.cn-shanghai.aliyuncs.com',
'ZHANGJIAKOU': 'batchcompute.cn-zhangjiakou.aliyuncs.com',
'CHENGDU': 'batchcompute.cn-chengdu.aliyuncs.com',
'HONGKONG': 'batchcompute.cn-hongkong.aliyuncs.com',
'QINGDAO': 'batchcompute.cn-qingdao.aliyuncs.com',
'SHENZHEN': 'batchcompute.cn-shenzhen.aliyuncs.com'
}
class RClient(object):
def __init__(self, config=None):
self.conf = config
self.client = config.client
class Cluster(RClient):
def __init__(self, config=None):
super(Cluster, self).__init__(config)
cluster = AutoCluster()
cluster.ImageId = self.conf.get("Cluster", "ImageId")
cluster.ReserveOnFail = False
cluster.ResourceType = "OnDemand"
cluster.InstanceType = self.conf.get("Cluster", "InstanceType")
configs = Configs()
if self.conf.get("Cluster", "CidrBlock") is not None and self.conf.get("Cluster", "VpcId") is not None:
networks = Networks()
vpc = VPC()
vpc.CidrBlock = self.conf.get("Cluster", "CidrBlock")
vpc.VpcId = self.conf.get("Cluster", "VpcId")
networks.VPC = vpc
configs.Networks = networks
if self.conf.get("Cluster", "system_disk") is not None:
configs.add_system_disk(size=int(self.conf.get(
"Cluster", "system_disk")), type_='cloud_efficiency')
else:
configs.add_system_disk(size=40, type_='cloud_efficiency')
configs.InstanceCount = 1
cluster.Configs = configs
self.cluster = cluster
def AddClusterMount(self, writeable=True):
self.oss_mount_entry = {
"Source": self.conf.get("OSS", "ossSrc"),
"Destination": self.conf.get("OSS", "mountPath"),
"WriteSupport": writeable,
}
self.cluster.Configs.Mounts.add_entry(self.oss_mount_entry)
self.cluster.Configs.Mounts.Locale = "utf-8"
self.cluster.Configs.Mounts.Lock = False
class Task(object):
def __init__(self, cluster=None):
self.job_desc = JobDescription()
self.task_dag = DAG()
self.stdout = cluster.conf.get("Task", "stdoutlog")
self.stderr = cluster.conf.get("Task", "stderrlog")
if not self.stdout.endswith("/"):
self.stdout += "/"
if not self.stderr.endswith("/"):
self.stderr += "/"
self.timeout = int(cluster.conf.get("Task", "timeout"))
self.cluster = cluster
def AddOneTask(self, job=None, outdir=""):
jobcpu = job.cpu if job.cpu else self.cluster.conf.get("args", "cpu")
jobmem = job.mem if job.mem else self.cluster.conf.get(
"args", "memory")
insType = self.cluster.conf.get("Cluster", "InstanceType")
if insType is not None:
if self.cluster.conf.it_conf[insType]["cpu"] < int(jobcpu) or self.cluster.conf.it_conf[insType]["memory"] < int(jobmem):
insType = self.get_instance_type(int(jobcpu), int(jobmem))
self.cluster.conf.logger.info("job %s %s InstanceType not match cpu or memory necessary, used %s insteaded",
job.name, self.cluster.conf.get("Cluster", "InstanceType"), insType)
else:
insType = self.get_instance_type(int(jobcpu), int(jobmem))
self.cluster.cluster.InstanceType = insType
task = TaskDescription()
task.Parameters.Command.CommandLine = "sh -c \"%s\"" % job.cmd
task.Parameters.StdoutRedirectPath = self.stdout
task.Parameters.StderrRedirectPath = self.stderr
task.InstanceCount = 1
task.Timeout = self.timeout
task.MaxRetryCount = 0
task.AutoCluster = self.cluster.cluster
if self.cluster.conf.get("Task", "DOCKER_IMAGE"):
task.Parameters.Command.EnvVars["BATCH_COMPUTE_DOCKER_IMAGE"] = self.cluster.conf.get(
"Task", "DOCKER_IMAGE")
task.Parameters.Command.EnvVars["BATCH_COMPUTE_DOCKER_REGISTRY_OSS_PATH"] = self.cluster.conf.get(
"Task", "DOCKER_REGISTRY_OSS_PATH")
task.Mounts = self.cluster.cluster.Configs.Mounts
self.cluster.cluster.Configs.Mounts = Mounts()
if outdir is not None:
if self.cluster.oss_mount_entry["Destination"] in outdir:
if not outdir.endswith("/"):
outdir += "/"
des = self.cluster.oss_mount_entry["Destination"]
src = self.cluster.oss_mount_entry["Source"]
task.OutputMapping[outdir] = re.sub("^"+des, src, outdir)
else:
self.cluster.conf.logger.debug(
"Only oss mount path support(%s), %s", outdir, job.name)
self.task_dag.add_task(task_name=job.name, task=task)
self.name = job.name
self.client = self.cluster.client
job.client = self.client
def modifyTaskOutMapping(self, job=None, mapping=""):
if mapping:
if self.cluster.oss_mount_entry["Destination"] in mapping:
# if not mapping.endswith("/"):
# mapping += "/"
des = self.cluster.oss_mount_entry["Destination"]
src = self.cluster.oss_mount_entry["Source"]
self.task_dag.Tasks[self.name].OutputMapping.clear()
self.task_dag.Tasks[self.name].OutputMapping[mapping] = re.sub(
"^"+des, src, mapping)
else:
self.cluster.conf.logger.debug(
"Only oss mount path support(%s), %s", mapping, job.name)
def get_instance_type(self, cpu=2, mem=4):
e = []
c, m = 0, 0
for tp in self.cluster.conf.availableTypes:
cc = self.cluster.conf.it_conf[tp]["cpu"]
cm = self.cluster.conf.it_conf[tp]['memory']
if cc >= cpu and cm >= mem:
if len(e):
if cc != c or cm != m:
break
c, m = cc, cm
e.append(tp)
if not e:
return "ecs.c5.large"
return sample(e, 1)[0]
def Submit(self):
self.job_desc.DAG = self.task_dag
self.job_desc.Priority = 1
self.job_desc.Name = self.name
self.job_desc.Description = self.name
self.job_desc.JobFailOnInstanceFail = True
self.job_desc.AutoRelease = False
clientjob = self.client.create_job(self.job_desc)
self.id = clientjob.Id
@property
def loger(self):
return logging.getLogger()