You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cloudbrain.go 20 kB

2 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. package cloudbrain
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "os"
  6. "strconv"
  7. "code.gitea.io/gitea/modules/timeutil"
  8. "code.gitea.io/gitea/modules/storage"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/notification"
  13. "code.gitea.io/gitea/modules/setting"
  14. )
  15. const (
  16. //Command = `pip3 install jupyterlab==2.2.5 -i https://pypi.tuna.tsinghua.edu.cn/simple;service ssh stop;jupyter lab --no-browser --ip=0.0.0.0 --allow-root --notebook-dir="/code" --port=80 --LabApp.token="" --LabApp.allow_origin="self https://cloudbrain.pcl.ac.cn"`
  17. //CommandBenchmark = `echo "start benchmark";python /code/test.py;echo "end benchmark"`
  18. CommandBenchmark = `echo "start benchmark";cd /benchmark && bash run_bk.sh;echo "end benchmark"`
  19. CodeMountPath = "/code"
  20. DataSetMountPath = "/dataset"
  21. ModelMountPath = "/model"
  22. LogFile = "log.txt"
  23. BenchMarkMountPath = "/benchmark"
  24. BenchMarkResourceID = 1
  25. Snn4imagenetMountPath = "/snn4imagenet"
  26. BrainScoreMountPath = "/brainscore"
  27. TaskInfoName = "/taskInfo"
  28. Snn4imagenetCommand = `/opt/conda/bin/python /snn4imagenet/testSNN_script.py --modelname '%s' --modelpath '/dataset' --modeldescription '%s'`
  29. BrainScoreCommand = `bash /brainscore/brainscore_test_par4shSrcipt.sh -b '%s' -n '%s' -p '/dataset' -d '%s'`
  30. SubTaskName = "task1"
  31. Success = "S000"
  32. DefaultBranchName = "master"
  33. ResultPath = "/result"
  34. )
  35. var (
  36. ResourceSpecs *models.ResourceSpecs
  37. TrainResourceSpecs *models.ResourceSpecs
  38. InferenceResourceSpecs *models.ResourceSpecs
  39. SpecialPools *models.SpecialPools
  40. )
  41. type GenerateCloudBrainTaskReq struct {
  42. Ctx *context.Context
  43. DisplayJobName string
  44. JobName string
  45. Image string
  46. Command string
  47. CodePath string
  48. ModelPath string
  49. BenchmarkPath string
  50. Snn4ImageNetPath string
  51. BrainScorePath string
  52. JobType string
  53. GpuQueue string
  54. Description string
  55. BranchName string
  56. BootFile string
  57. Params string
  58. CommitID string
  59. Uuids string
  60. DatasetNames string
  61. DatasetInfos map[string]models.DatasetInfo
  62. BenchmarkTypeID int
  63. BenchmarkChildTypeID int
  64. ResourceSpecId int
  65. ResultPath string
  66. TrainUrl string
  67. ModelName string
  68. ModelVersion string
  69. CkptName string
  70. }
  71. func GetCloudbrainDebugCommand() string {
  72. var command = `pip3 install jupyterlab==3 -i https://pypi.tuna.tsinghua.edu.cn/simple;service ssh stop;/usr/local/bin/python /usr/local/bin/jupyter-lab --ServerApp.shutdown_no_activity_timeout=` + setting.CullIdleTimeout + ` --TerminalManager.cull_inactive_timeout=` + setting.CullIdleTimeout + ` --TerminalManager.cull_interval=` + setting.CullInterval + ` --MappingKernelManager.cull_idle_timeout=` + setting.CullIdleTimeout + ` --MappingKernelManager.cull_interval=` + setting.CullInterval + ` --MappingKernelManager.cull_connected=True --MappingKernelManager.cull_busy=True --no-browser --ip=0.0.0.0 --allow-root --notebook-dir="/code" --port=80 --ServerApp.token="" --ServerApp.allow_origin="self https://cloudbrain.pcl.ac.cn" `
  73. return command
  74. }
  75. func isAdminOrOwnerOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  76. if !ctx.IsSigned {
  77. return false
  78. }
  79. if err != nil {
  80. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin()
  81. } else {
  82. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  83. }
  84. }
  85. func CanDeleteJob(ctx *context.Context, job *models.Cloudbrain) bool {
  86. return isAdminOrOwnerOrJobCreater(ctx, job, nil)
  87. }
  88. func CanCreateOrDebugJob(ctx *context.Context) bool {
  89. if !ctx.IsSigned {
  90. return false
  91. }
  92. return ctx.Repo.CanWrite(models.UnitTypeCloudBrain)
  93. }
  94. func CanModifyJob(ctx *context.Context, job *models.Cloudbrain) bool {
  95. return isAdminOrJobCreater(ctx, job, nil)
  96. }
  97. func isAdminOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  98. if !ctx.IsSigned {
  99. return false
  100. }
  101. if err != nil {
  102. return ctx.IsUserSiteAdmin()
  103. } else {
  104. return ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  105. }
  106. }
  107. func isAdminOrImageCreater(ctx *context.Context, image *models.Image, err error) bool {
  108. if !ctx.IsSigned {
  109. return false
  110. }
  111. if err != nil {
  112. return ctx.IsUserSiteAdmin()
  113. } else {
  114. return ctx.IsUserSiteAdmin() || ctx.User.ID == image.UID
  115. }
  116. }
  117. func AdminOrOwnerOrJobCreaterRight(ctx *context.Context) {
  118. var ID = ctx.Params(":id")
  119. job, err := models.GetCloudbrainByID(ID)
  120. if err != nil {
  121. log.Error("GetCloudbrainByID failed:%v", err.Error())
  122. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  123. }
  124. ctx.Cloudbrain = job
  125. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  126. log.Error("!isAdminOrOwnerOrJobCreater error:%v", err.Error())
  127. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  128. }
  129. }
  130. func AdminOrJobCreaterRight(ctx *context.Context) {
  131. var ID = ctx.Params(":id")
  132. job, err := models.GetCloudbrainByID(ID)
  133. if err != nil {
  134. log.Error("GetCloudbrainByID failed:%v", err.Error())
  135. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  136. }
  137. ctx.Cloudbrain = job
  138. if !isAdminOrJobCreater(ctx, job, err) {
  139. log.Error("!isAdminOrJobCreater error:%v", err.Error())
  140. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  141. }
  142. }
  143. func AdminOrOwnerOrJobCreaterRightForTrain(ctx *context.Context) {
  144. var jobID = ctx.Params(":jobid")
  145. job, err := models.GetCloudbrainByJobID(jobID)
  146. if err != nil {
  147. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  148. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  149. }
  150. ctx.Cloudbrain = job
  151. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  152. log.Error("!isAdminOrOwnerOrJobCreater failed:%v", err.Error())
  153. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  154. }
  155. }
  156. func AdminOrJobCreaterRightForTrain(ctx *context.Context) {
  157. var jobID = ctx.Params(":jobid")
  158. job, err := models.GetCloudbrainByJobID(jobID)
  159. if err != nil {
  160. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  161. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  162. }
  163. ctx.Cloudbrain = job
  164. if !isAdminOrJobCreater(ctx, job, err) {
  165. log.Error("!isAdminOrJobCreater errot:%v", err.Error())
  166. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  167. }
  168. }
  169. func AdminOrImageCreaterRight(ctx *context.Context) {
  170. id, err := strconv.ParseInt(ctx.Params(":id"), 10, 64)
  171. var image *models.Image
  172. if err != nil {
  173. log.Error("Get Image by ID failed:%v", err.Error())
  174. } else {
  175. image, err = models.GetImageByID(id)
  176. if err != nil {
  177. log.Error("Get Image by ID failed:%v", err.Error())
  178. return
  179. }
  180. }
  181. if !isAdminOrImageCreater(ctx, image, err) {
  182. log.Error("!isAdminOrImageCreater error:%v", err.Error())
  183. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  184. }
  185. }
  186. func GenerateTask(req GenerateCloudBrainTaskReq) error {
  187. var resourceSpec *models.ResourceSpec
  188. var versionCount int
  189. if req.JobType == string(models.JobTypeTrain) {
  190. versionCount = 1
  191. if TrainResourceSpecs == nil {
  192. json.Unmarshal([]byte(setting.TrainResourceSpecs), &TrainResourceSpecs)
  193. }
  194. for _, spec := range TrainResourceSpecs.ResourceSpec {
  195. if req.ResourceSpecId == spec.Id {
  196. resourceSpec = spec
  197. break
  198. }
  199. }
  200. } else if req.JobType == string(models.JobTypeInference) {
  201. if InferenceResourceSpecs == nil {
  202. json.Unmarshal([]byte(setting.InferenceResourceSpecs), &InferenceResourceSpecs)
  203. }
  204. for _, spec := range InferenceResourceSpecs.ResourceSpec {
  205. if req.ResourceSpecId == spec.Id {
  206. resourceSpec = spec
  207. break
  208. }
  209. }
  210. } else {
  211. if ResourceSpecs == nil {
  212. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  213. }
  214. for _, spec := range ResourceSpecs.ResourceSpec {
  215. if req.ResourceSpecId == spec.Id {
  216. resourceSpec = spec
  217. break
  218. }
  219. }
  220. }
  221. //如果没有匹配到spec信息,尝试从专属资源池获取
  222. if resourceSpec == nil && SpecialPools != nil {
  223. resourceSpec = geMatchResourceSpec(req.JobType, req.GpuQueue, req.ResourceSpecId)
  224. }
  225. if resourceSpec == nil {
  226. log.Error("no such resourceSpecId(%d)", req.ResourceSpecId, req.Ctx.Data["MsgID"])
  227. return errors.New("no such resourceSpec")
  228. }
  229. volumes := []models.Volume{
  230. {
  231. HostPath: models.StHostPath{
  232. Path: req.CodePath,
  233. MountPath: CodeMountPath,
  234. ReadOnly: false,
  235. },
  236. },
  237. {
  238. HostPath: models.StHostPath{
  239. Path: req.ModelPath,
  240. MountPath: ModelMountPath,
  241. ReadOnly: false,
  242. },
  243. },
  244. {
  245. HostPath: models.StHostPath{
  246. Path: req.BenchmarkPath,
  247. MountPath: BenchMarkMountPath,
  248. ReadOnly: true,
  249. },
  250. },
  251. {
  252. HostPath: models.StHostPath{
  253. Path: req.Snn4ImageNetPath,
  254. MountPath: Snn4imagenetMountPath,
  255. ReadOnly: true,
  256. },
  257. },
  258. {
  259. HostPath: models.StHostPath{
  260. Path: req.BrainScorePath,
  261. MountPath: BrainScoreMountPath,
  262. ReadOnly: true,
  263. },
  264. },
  265. {
  266. HostPath: models.StHostPath{
  267. Path: req.ResultPath,
  268. MountPath: ResultPath,
  269. ReadOnly: false,
  270. },
  271. },
  272. }
  273. if len(req.DatasetInfos) == 1 {
  274. volumes = append(volumes, models.Volume{
  275. HostPath: models.StHostPath{
  276. Path: req.DatasetInfos[req.Uuids].DataLocalPath,
  277. MountPath: DataSetMountPath,
  278. ReadOnly: true,
  279. },
  280. })
  281. } else {
  282. for _, dataset := range req.DatasetInfos {
  283. volumes = append(volumes, models.Volume{
  284. HostPath: models.StHostPath{
  285. Path: dataset.DataLocalPath,
  286. MountPath: DataSetMountPath + "/" + dataset.Name,
  287. ReadOnly: true,
  288. },
  289. })
  290. }
  291. }
  292. createTime := timeutil.TimeStampNow()
  293. jobResult, err := CreateJob(req.JobName, models.CreateJobParams{
  294. JobName: req.JobName,
  295. RetryCount: 1,
  296. GpuType: req.GpuQueue,
  297. Image: req.Image,
  298. TaskRoles: []models.TaskRole{
  299. {
  300. Name: SubTaskName,
  301. TaskNumber: 1,
  302. MinSucceededTaskCount: 1,
  303. MinFailedTaskCount: 1,
  304. CPUNumber: resourceSpec.CpuNum,
  305. GPUNumber: resourceSpec.GpuNum,
  306. MemoryMB: resourceSpec.MemMiB,
  307. ShmMB: resourceSpec.ShareMemMiB,
  308. Command: req.Command,
  309. NeedIBDevice: false,
  310. IsMainRole: false,
  311. UseNNI: false,
  312. },
  313. },
  314. Volumes: volumes,
  315. })
  316. if err != nil {
  317. log.Error("CreateJob failed:", err.Error(), req.Ctx.Data["MsgID"])
  318. return err
  319. }
  320. if jobResult.Code != Success {
  321. log.Error("CreateJob(%s) failed:%s", req.JobName, jobResult.Msg, req.Ctx.Data["MsgID"])
  322. return errors.New(jobResult.Msg)
  323. }
  324. var jobID = jobResult.Payload["jobId"].(string)
  325. err = models.CreateCloudbrain(&models.Cloudbrain{
  326. Status: string(models.JobWaiting),
  327. UserID: req.Ctx.User.ID,
  328. RepoID: req.Ctx.Repo.Repository.ID,
  329. JobID: jobID,
  330. JobName: req.JobName,
  331. DisplayJobName: req.DisplayJobName,
  332. SubTaskName: SubTaskName,
  333. JobType: req.JobType,
  334. Type: models.TypeCloudBrainOne,
  335. Uuid: req.Uuids,
  336. Image: req.Image,
  337. GpuQueue: req.GpuQueue,
  338. ResourceSpecId: req.ResourceSpecId,
  339. ComputeResource: models.GPUResource,
  340. BenchmarkTypeID: req.BenchmarkTypeID,
  341. BenchmarkChildTypeID: req.BenchmarkChildTypeID,
  342. Description: req.Description,
  343. IsLatestVersion: "1",
  344. VersionCount: versionCount,
  345. BranchName: req.BranchName,
  346. BootFile: req.BootFile,
  347. DatasetName: req.DatasetNames,
  348. Parameters: req.Params,
  349. TrainUrl: req.TrainUrl,
  350. ModelName: req.ModelName,
  351. ModelVersion: req.ModelVersion,
  352. CkptName: req.CkptName,
  353. ResultUrl: req.ResultPath,
  354. CreatedUnix: createTime,
  355. UpdatedUnix: createTime,
  356. CommitID: req.CommitID,
  357. })
  358. if err != nil {
  359. return err
  360. }
  361. task, err := models.GetCloudbrainByJobID(jobID)
  362. if err != nil {
  363. log.Error("GetCloudbrainByJobID failed: %v", err.Error())
  364. return err
  365. }
  366. stringId := strconv.FormatInt(task.ID, 10)
  367. if IsBenchmarkJob(req.JobType) {
  368. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, stringId, req.DisplayJobName, models.ActionCreateBenchMarkTask)
  369. } else if string(models.JobTypeTrain) == req.JobType {
  370. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, jobID, req.DisplayJobName, models.ActionCreateGPUTrainTask)
  371. } else if string(models.JobTypeInference) == req.JobType {
  372. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, jobID, req.DisplayJobName, models.ActionCreateInferenceTask)
  373. } else {
  374. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, stringId, req.DisplayJobName, models.ActionCreateDebugGPUTask)
  375. }
  376. return nil
  377. }
  378. func IsBenchmarkJob(jobType string) bool {
  379. return string(models.JobTypeBenchmark) == jobType || string(models.JobTypeBrainScore) == jobType || string(models.JobTypeSnn4imagenet) == jobType
  380. }
  381. func GetWaitingCloudbrainCount(cloudbrainType int, computeResource string, jobTypes ...models.JobType) int64 {
  382. num, err := models.GetWaitingCloudbrainCount(cloudbrainType, computeResource, jobTypes...)
  383. if err != nil {
  384. log.Warn("Get waiting count err", err)
  385. num = 0
  386. }
  387. return num
  388. }
  389. func RestartTask(ctx *context.Context, task *models.Cloudbrain, newID *string) error {
  390. jobName := task.JobName
  391. var resourceSpec *models.ResourceSpec
  392. if ResourceSpecs == nil {
  393. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  394. }
  395. for _, spec := range ResourceSpecs.ResourceSpec {
  396. if task.ResourceSpecId == spec.Id {
  397. resourceSpec = spec
  398. }
  399. }
  400. //如果没有匹配到spec信息,尝试从专属资源池获取
  401. if resourceSpec == nil && SpecialPools != nil {
  402. resourceSpec = geMatchResourceSpec(task.JobType, task.GpuQueue, task.ResourceSpecId)
  403. }
  404. if resourceSpec == nil {
  405. log.Error("no such resourceSpecId(%d)", task.ResourceSpecId, ctx.Data["MsgID"])
  406. return errors.New("no such resourceSpec")
  407. }
  408. datasetInfos, _, err := models.GetDatasetInfo(task.Uuid)
  409. if err != nil {
  410. log.Error("GetDatasetInfo failed:%v", err, ctx.Data["MsgID"])
  411. return err
  412. }
  413. volumes := []models.Volume{
  414. {
  415. HostPath: models.StHostPath{
  416. Path: storage.GetMinioPath(jobName, CodeMountPath+"/"),
  417. MountPath: CodeMountPath,
  418. ReadOnly: false,
  419. },
  420. },
  421. {
  422. HostPath: models.StHostPath{
  423. Path: storage.GetMinioPath(jobName, ModelMountPath+"/"),
  424. MountPath: ModelMountPath,
  425. ReadOnly: false,
  426. },
  427. },
  428. {
  429. HostPath: models.StHostPath{
  430. Path: storage.GetMinioPath(jobName, BenchMarkMountPath+"/"),
  431. MountPath: BenchMarkMountPath,
  432. ReadOnly: true,
  433. },
  434. },
  435. {
  436. HostPath: models.StHostPath{
  437. Path: storage.GetMinioPath(jobName, Snn4imagenetMountPath+"/"),
  438. MountPath: Snn4imagenetMountPath,
  439. ReadOnly: true,
  440. },
  441. },
  442. {
  443. HostPath: models.StHostPath{
  444. Path: storage.GetMinioPath(jobName, BrainScoreMountPath+"/"),
  445. MountPath: BrainScoreMountPath,
  446. ReadOnly: true,
  447. },
  448. },
  449. }
  450. if len(datasetInfos) == 1 {
  451. volumes = append(volumes, models.Volume{
  452. HostPath: models.StHostPath{
  453. Path: datasetInfos[task.Uuid].DataLocalPath,
  454. MountPath: DataSetMountPath,
  455. ReadOnly: true,
  456. },
  457. })
  458. } else {
  459. for _, dataset := range datasetInfos {
  460. volumes = append(volumes, models.Volume{
  461. HostPath: models.StHostPath{
  462. Path: dataset.DataLocalPath,
  463. MountPath: DataSetMountPath + "/" + dataset.Name,
  464. ReadOnly: true,
  465. },
  466. })
  467. }
  468. }
  469. createTime := timeutil.TimeStampNow()
  470. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  471. JobName: jobName,
  472. RetryCount: 1,
  473. GpuType: task.GpuQueue,
  474. Image: task.Image,
  475. TaskRoles: []models.TaskRole{
  476. {
  477. Name: SubTaskName,
  478. TaskNumber: 1,
  479. MinSucceededTaskCount: 1,
  480. MinFailedTaskCount: 1,
  481. CPUNumber: resourceSpec.CpuNum,
  482. GPUNumber: resourceSpec.GpuNum,
  483. MemoryMB: resourceSpec.MemMiB,
  484. ShmMB: resourceSpec.ShareMemMiB,
  485. Command: GetCloudbrainDebugCommand(),//Command,
  486. NeedIBDevice: false,
  487. IsMainRole: false,
  488. UseNNI: false,
  489. },
  490. },
  491. Volumes: volumes,
  492. })
  493. if err != nil {
  494. log.Error("CreateJob failed:%v", err.Error(), ctx.Data["MsgID"])
  495. return err
  496. }
  497. if jobResult.Code != Success {
  498. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  499. return errors.New(jobResult.Msg)
  500. }
  501. var jobID = jobResult.Payload["jobId"].(string)
  502. newTask := &models.Cloudbrain{
  503. Status: string(models.JobWaiting),
  504. UserID: task.UserID,
  505. RepoID: task.RepoID,
  506. JobID: jobID,
  507. JobName: task.JobName,
  508. DisplayJobName: task.DisplayJobName,
  509. SubTaskName: task.SubTaskName,
  510. JobType: task.JobType,
  511. Type: task.Type,
  512. Uuid: task.Uuid,
  513. DatasetName: task.DatasetName,
  514. Image: task.Image,
  515. GpuQueue: task.GpuQueue,
  516. ResourceSpecId: task.ResourceSpecId,
  517. ComputeResource: task.ComputeResource,
  518. CreatedUnix: createTime,
  519. UpdatedUnix: createTime,
  520. BranchName: task.BranchName,
  521. }
  522. err = models.RestartCloudbrain(task, newTask)
  523. if err != nil {
  524. log.Error("RestartCloudbrain(%s) failed:%v", jobName, err.Error(), ctx.Data["MsgID"])
  525. return err
  526. }
  527. stringId := strconv.FormatInt(newTask.ID, 10)
  528. *newID = stringId
  529. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, task.DisplayJobName, models.ActionCreateDebugGPUTask)
  530. return nil
  531. }
  532. func geMatchResourceSpec(jobType string, gpuQueue string, resourceSpecId int) *models.ResourceSpec {
  533. for _, specialPool := range SpecialPools.Pools {
  534. if specialPool.ResourceSpec != nil {
  535. if IsElementExist(specialPool.JobType, jobType) && IsQueueInSpecialtPool(specialPool.Pool, gpuQueue) {
  536. for _, spec := range specialPool.ResourceSpec {
  537. if resourceSpecId == spec.Id {
  538. return spec
  539. }
  540. }
  541. }
  542. }
  543. }
  544. return nil
  545. }
  546. func DelCloudBrainJob(jobId string) string {
  547. task, err := models.GetCloudbrainByJobID(jobId)
  548. if err != nil {
  549. log.Error("get cloud brain err:", err)
  550. return "cloudbrain.Delete_failed"
  551. }
  552. if task.Status != string(models.JobStopped) && task.Status != string(models.JobFailed) && task.Status != string(models.JobSucceeded) {
  553. log.Error("the job(%s) has not been stopped", task.JobName)
  554. return "cloudbrain.Not_Stopped"
  555. }
  556. err = models.DeleteJob(task)
  557. if err != nil {
  558. log.Error("DeleteJob failed:", err)
  559. return "cloudbrain.Delete_failed"
  560. }
  561. deleteJobStorage(task.JobName)
  562. return ""
  563. }
  564. func deleteJobStorage(jobName string) error {
  565. //delete local
  566. localJobPath := setting.JobPath + jobName
  567. err := os.RemoveAll(localJobPath)
  568. if err != nil {
  569. log.Error("RemoveAll(%s) failed:%v", localJobPath, err)
  570. }
  571. dirPath := setting.CBCodePathPrefix + jobName + "/"
  572. err = storage.Attachments.DeleteDir(dirPath)
  573. if err != nil {
  574. log.Error("DeleteDir(%s) failed:%v", localJobPath, err)
  575. }
  576. return nil
  577. }
  578. func InitSpecialPool() {
  579. if SpecialPools == nil && setting.SpecialPools != "" {
  580. json.Unmarshal([]byte(setting.SpecialPools), &SpecialPools)
  581. }
  582. }
  583. func IsResourceSpecInSpecialPool(resourceSpecs []*models.ResourceSpec, resourceSpecId int) bool {
  584. if resourceSpecs == nil || len(resourceSpecs) == 0 {
  585. return true
  586. }
  587. for _, v := range resourceSpecs {
  588. if v.Id == resourceSpecId {
  589. return true
  590. }
  591. }
  592. return false
  593. }
  594. func IsQueueInSpecialtPool(pool []*models.GpuInfo, queue string) bool {
  595. for _, v := range pool {
  596. if v.Queue == queue {
  597. return true
  598. }
  599. }
  600. return false
  601. }
  602. func IsElementExist(s []string, str string) bool {
  603. for _, v := range s {
  604. if v == str {
  605. return true
  606. }
  607. }
  608. return false
  609. }