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.

resource_specification.go 12 kB

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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. package models
  2. import (
  3. "code.gitea.io/gitea/modules/timeutil"
  4. "xorm.io/builder"
  5. )
  6. const (
  7. SpecNotVerified int = iota + 1
  8. SpecOnShelf
  9. SpecOffShelf
  10. )
  11. type ResourceSpecification struct {
  12. ID int64 `xorm:"pk autoincr"`
  13. QueueId int64 `xorm:"INDEX"`
  14. SourceSpecId string `xorm:"INDEX"`
  15. AccCardsNum int
  16. CpuCores int
  17. MemGiB float32
  18. GPUMemGiB float32
  19. ShareMemGiB float32
  20. UnitPrice int
  21. Status int
  22. IsAutomaticSync bool
  23. CreatedTime timeutil.TimeStamp `xorm:"created"`
  24. CreatedBy int64
  25. UpdatedTime timeutil.TimeStamp `xorm:"updated"`
  26. UpdatedBy int64
  27. }
  28. func (r ResourceSpecification) ConvertToRes() *ResourceSpecificationRes {
  29. return &ResourceSpecificationRes{
  30. ID: r.ID,
  31. SourceSpecId: r.SourceSpecId,
  32. AccCardsNum: r.AccCardsNum,
  33. CpuCores: r.CpuCores,
  34. MemGiB: r.MemGiB,
  35. ShareMemGiB: r.ShareMemGiB,
  36. GPUMemGiB: r.GPUMemGiB,
  37. UnitPrice: r.UnitPrice,
  38. Status: r.Status,
  39. UpdatedTime: r.UpdatedTime,
  40. }
  41. }
  42. type ResourceSpecificationReq struct {
  43. QueueId int64 `binding:"Required"`
  44. SourceSpecId string
  45. AccCardsNum int
  46. CpuCores int
  47. MemGiB float32
  48. GPUMemGiB float32
  49. ShareMemGiB float32
  50. UnitPrice int
  51. Status int
  52. IsAutomaticSync bool
  53. CreatorId int64
  54. }
  55. func (r ResourceSpecificationReq) ToDTO() ResourceSpecification {
  56. return ResourceSpecification{
  57. QueueId: r.QueueId,
  58. SourceSpecId: r.SourceSpecId,
  59. AccCardsNum: r.AccCardsNum,
  60. CpuCores: r.CpuCores,
  61. MemGiB: r.MemGiB,
  62. GPUMemGiB: r.GPUMemGiB,
  63. ShareMemGiB: r.ShareMemGiB,
  64. UnitPrice: r.UnitPrice,
  65. Status: r.Status,
  66. IsAutomaticSync: r.IsAutomaticSync,
  67. CreatedBy: r.CreatorId,
  68. UpdatedBy: r.CreatorId,
  69. }
  70. }
  71. type SearchResourceSpecificationOptions struct {
  72. ListOptions
  73. QueueId int64
  74. Status int
  75. Cluster string
  76. }
  77. type SearchResourceBriefSpecificationOptions struct {
  78. QueueId int64
  79. Cluster string
  80. }
  81. type ResourceSpecAndQueueListRes struct {
  82. TotalSize int64
  83. List []*ResourceSpecAndQueueRes
  84. }
  85. func NewResourceSpecAndQueueListRes(totalSize int64, list []ResourceSpecAndQueue) *ResourceSpecAndQueueListRes {
  86. resList := make([]*ResourceSpecAndQueueRes, len(list))
  87. for i, v := range list {
  88. resList[i] = v.ConvertToRes()
  89. }
  90. return &ResourceSpecAndQueueListRes{
  91. TotalSize: totalSize,
  92. List: resList,
  93. }
  94. }
  95. type ResourceSpecificationRes struct {
  96. ID int64
  97. SourceSpecId string
  98. AccCardsNum int
  99. CpuCores int
  100. MemGiB float32
  101. GPUMemGiB float32
  102. ShareMemGiB float32
  103. UnitPrice int
  104. Status int
  105. UpdatedTime timeutil.TimeStamp
  106. }
  107. func (ResourceSpecificationRes) TableName() string {
  108. return "resource_specification"
  109. }
  110. type ResourceSpecAndQueueRes struct {
  111. Spec *ResourceSpecificationRes
  112. Queue *ResourceQueueRes
  113. }
  114. type ResourceSpecAndQueue struct {
  115. ResourceSpecification `xorm:"extends"`
  116. ResourceQueue `xorm:"extends"`
  117. }
  118. func (*ResourceSpecAndQueue) TableName() string {
  119. return "resource_specification"
  120. }
  121. func (r ResourceSpecAndQueue) ConvertToRes() *ResourceSpecAndQueueRes {
  122. return &ResourceSpecAndQueueRes{
  123. Spec: r.ResourceSpecification.ConvertToRes(),
  124. Queue: r.ResourceQueue.ConvertToRes(),
  125. }
  126. }
  127. type FindSpecsOptions struct {
  128. JobType JobType
  129. ComputeResource string
  130. Cluster string
  131. AiCenterCode string
  132. SpecId int64
  133. QueueCode string
  134. SourceSpecId string
  135. AccCardsNum int
  136. UseAccCardsNum bool
  137. AccCardType string
  138. CpuCores int
  139. UseCpuCores bool
  140. MemGiB float32
  141. UseMemGiB bool
  142. GPUMemGiB float32
  143. UseGPUMemGiB bool
  144. ShareMemGiB float32
  145. UseShareMemGiB bool
  146. //if true,find specs no matter used or not used in scene. if false,only find specs used in scene
  147. RequestAll bool
  148. }
  149. type Specification struct {
  150. ID int64
  151. SourceSpecId string
  152. AccCardsNum int
  153. AccCardType string
  154. CpuCores int
  155. MemGiB float32
  156. GPUMemGiB float32
  157. ShareMemGiB float32
  158. ComputeResource string
  159. UnitPrice int
  160. QueueId int64
  161. QueueCode string
  162. Cluster string
  163. AiCenterCode string
  164. AiCenterName string
  165. IsExclusive bool
  166. ExclusiveOrg string
  167. }
  168. func (Specification) TableName() string {
  169. return "resource_specification"
  170. }
  171. func InsertResourceSpecification(r ResourceSpecification) (int64, error) {
  172. return x.Insert(&r)
  173. }
  174. func UpdateResourceSpecificationById(queueId int64, spec ResourceSpecification) (int64, error) {
  175. return x.ID(queueId).Update(&spec)
  176. }
  177. func UpdateSpecUnitPriceById(id int64, unitPrice int) error {
  178. _, err := x.Exec("update resource_specification set unit_price = ? ,updated_time = ? where id = ?", unitPrice, timeutil.TimeStampNow(), id)
  179. return err
  180. }
  181. func SearchResourceSpecification(opts SearchResourceSpecificationOptions) (int64, []ResourceSpecAndQueue, error) {
  182. var cond = builder.NewCond()
  183. if opts.Page <= 0 {
  184. opts.Page = 1
  185. }
  186. if opts.QueueId > 0 {
  187. cond = cond.And(builder.Eq{"resource_specification.queue_id": opts.QueueId})
  188. }
  189. if opts.Status > 0 {
  190. cond = cond.And(builder.Eq{"resource_specification.status": opts.Status})
  191. }
  192. if opts.Cluster != "" {
  193. cond = cond.And(builder.Eq{"resource_queue.cluster": opts.Cluster})
  194. }
  195. //cond = cond.And(builder.Or(builder.Eq{"resource_queue.deleted_time": 0}).Or(builder.IsNull{"resource_queue.deleted_time"}))
  196. n, err := x.Where(cond).Join("INNER", "resource_queue", "resource_queue.ID = resource_specification.queue_id").
  197. Unscoped().Count(&ResourceSpecAndQueue{})
  198. if err != nil {
  199. return 0, nil, err
  200. }
  201. r := make([]ResourceSpecAndQueue, 0)
  202. err = x.Where(cond).
  203. Join("INNER", "resource_queue", "resource_queue.ID = resource_specification.queue_id").
  204. Desc("resource_specification.id").
  205. Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
  206. Unscoped().Find(&r)
  207. if err != nil {
  208. return 0, nil, err
  209. }
  210. return n, r, nil
  211. }
  212. func GetSpecScenes(specId int64) ([]ResourceSceneBriefRes, error) {
  213. r := make([]ResourceSceneBriefRes, 0)
  214. err := x.Where("resource_scene_spec.spec_id = ?", specId).
  215. Join("INNER", "resource_scene_spec", "resource_scene_spec.scene_id = resource_scene.id").
  216. Find(&r)
  217. if err != nil {
  218. return nil, err
  219. }
  220. return r, nil
  221. }
  222. func ResourceSpecOnShelf(id int64, unitPrice int) error {
  223. _, err := x.Exec("update resource_specification set unit_price = ?,updated_time = ?,status = ? where id = ?", unitPrice, timeutil.TimeStampNow(), SpecOnShelf, id)
  224. return err
  225. }
  226. func ResourceSpecOffShelf(id int64) (int64, error) {
  227. sess := x.NewSession()
  228. var err error
  229. defer func() {
  230. if err != nil {
  231. sess.Rollback()
  232. }
  233. sess.Close()
  234. }()
  235. //delete scene spec relation
  236. if _, err = sess.Where("spec_id = ?", id).Delete(&ResourceSceneSpec{}); err != nil {
  237. return 0, err
  238. }
  239. param := ResourceSpecification{
  240. Status: SpecOffShelf,
  241. }
  242. n, err := sess.Where("id = ? and status = ?", id, SpecOnShelf).Update(&param)
  243. if err != nil {
  244. return 0, err
  245. }
  246. sess.Commit()
  247. return n, err
  248. }
  249. func GetResourceSpecification(r *ResourceSpecification) (*ResourceSpecification, error) {
  250. has, err := x.Get(r)
  251. if err != nil {
  252. return nil, err
  253. } else if !has {
  254. return nil, nil
  255. }
  256. return r, nil
  257. }
  258. func SyncGrampusSpecs(updateList []ResourceSpecification, insertList []ResourceSpecification, existIds []int64) error {
  259. sess := x.NewSession()
  260. var err error
  261. defer func() {
  262. if err != nil {
  263. sess.Rollback()
  264. }
  265. sess.Close()
  266. }()
  267. //delete specs and scene that no longer exists
  268. deleteIds := make([]int64, 0)
  269. cond := builder.NewCond()
  270. cond = cond.And(builder.NotIn("resource_specification.id", existIds)).And(builder.Eq{"resource_queue.cluster": C2NetCluster})
  271. if err := sess.Cols("resource_specification.id").Table("resource_specification").
  272. Where(cond).Join("INNER", "resource_queue", "resource_queue.id = resource_specification.queue_id").
  273. Find(&deleteIds); err != nil {
  274. return err
  275. }
  276. if len(deleteIds) > 0 {
  277. if _, err = sess.In("id", deleteIds).Update(&ResourceSpecification{Status: SpecOffShelf}); err != nil {
  278. return err
  279. }
  280. if _, err = sess.In("spec_id", deleteIds).Delete(&ResourceSceneSpec{}); err != nil {
  281. return err
  282. }
  283. }
  284. //update exists specs
  285. if len(updateList) > 0 {
  286. for _, v := range updateList {
  287. if _, err = sess.ID(v.ID).Update(&v); err != nil {
  288. return err
  289. }
  290. }
  291. }
  292. //insert new specs
  293. if len(insertList) > 0 {
  294. if _, err = sess.Insert(insertList); err != nil {
  295. return err
  296. }
  297. }
  298. return sess.Commit()
  299. }
  300. //FindSpecs
  301. func FindSpecs(opts FindSpecsOptions) ([]*Specification, error) {
  302. var cond = builder.NewCond()
  303. if !opts.RequestAll && opts.JobType != "" {
  304. cond = cond.And(builder.Eq{"resource_scene.job_type": opts.JobType})
  305. }
  306. if opts.ComputeResource != "" {
  307. cond = cond.And(builder.Eq{"resource_queue.compute_resource": opts.ComputeResource})
  308. }
  309. if opts.ComputeResource != "" {
  310. cond = cond.And(builder.Eq{"resource_queue.cluster": opts.Cluster})
  311. }
  312. if opts.AiCenterCode != "" {
  313. cond = cond.And(builder.Eq{"resource_queue.ai_center_code": opts.AiCenterCode})
  314. }
  315. if opts.SpecId > 0 {
  316. cond = cond.And(builder.Eq{"resource_specification.id": opts.SpecId})
  317. }
  318. if opts.QueueCode != "" {
  319. cond = cond.And(builder.Eq{"resource_queue.queue_code": opts.QueueCode})
  320. }
  321. if opts.SourceSpecId != "" {
  322. cond = cond.And(builder.Eq{"resource_specification.source_spec_id": opts.SourceSpecId})
  323. }
  324. if opts.UseAccCardsNum {
  325. cond = cond.And(builder.Eq{"resource_specification.acc_cards_num": opts.AccCardsNum})
  326. }
  327. if opts.AccCardType != "" {
  328. cond = cond.And(builder.Eq{"resource_queue.acc_card_type": opts.AccCardType})
  329. }
  330. if opts.UseCpuCores {
  331. cond = cond.And(builder.Eq{"resource_specification.cpu_cores": opts.CpuCores})
  332. }
  333. if opts.UseMemGiB {
  334. cond = cond.And(builder.Eq{"resource_specification.mem_gi_b": opts.MemGiB})
  335. }
  336. if opts.UseGPUMemGiB {
  337. cond = cond.And(builder.Eq{"resource_specification.gpu_mem_gi_b": opts.GPUMemGiB})
  338. }
  339. if opts.UseShareMemGiB {
  340. cond = cond.And(builder.Eq{"resource_specification.share_mem_gi_b": opts.ShareMemGiB})
  341. }
  342. r := make([]*Specification, 0)
  343. s := x.Where(cond).
  344. Join("INNER", "resource_queue", "resource_queue.id = resource_specification.queue_id")
  345. if !opts.RequestAll {
  346. s = s.Join("INNER", "resource_scene_spec", "resource_scene_spec.spec_id = resource_specification.id").
  347. Join("INNER", "resource_scene", "resource_scene_spec.scene_id = resource_scene.id")
  348. }
  349. err := s.OrderBy("resource_queue.compute_resource asc,resource_queue.acc_card_type asc,resource_specification.acc_cards_num asc").
  350. Unscoped().Find(&r)
  351. if err != nil {
  352. return nil, err
  353. }
  354. return r, nil
  355. }
  356. func InitQueueAndSpec(queue ResourceQueue, spec ResourceSpecification) (*Specification, error) {
  357. sess := x.NewSession()
  358. defer sess.Close()
  359. sess.Begin()
  360. param := ResourceQueue{
  361. QueueCode: queue.QueueCode,
  362. Cluster: queue.Cluster,
  363. AiCenterCode: queue.AiCenterCode,
  364. ComputeResource: queue.ComputeResource,
  365. AccCardType: queue.AccCardType,
  366. }
  367. _, err := sess.Get(&param)
  368. if err != nil {
  369. sess.Rollback()
  370. return nil, err
  371. }
  372. if param.ID == 0 {
  373. _, err = sess.InsertOne(&queue)
  374. if err != nil {
  375. sess.Rollback()
  376. return nil, err
  377. }
  378. } else {
  379. queue = param
  380. }
  381. spec.QueueId = queue.ID
  382. _, err = sess.InsertOne(&spec)
  383. if err != nil {
  384. sess.Rollback()
  385. return nil, err
  386. }
  387. sess.Commit()
  388. return &Specification{
  389. ID: spec.ID,
  390. SourceSpecId: spec.SourceSpecId,
  391. AccCardsNum: spec.AccCardsNum,
  392. AccCardType: queue.AccCardType,
  393. CpuCores: spec.CpuCores,
  394. MemGiB: spec.MemGiB,
  395. GPUMemGiB: spec.GPUMemGiB,
  396. ShareMemGiB: spec.ShareMemGiB,
  397. ComputeResource: queue.ComputeResource,
  398. UnitPrice: spec.UnitPrice,
  399. QueueId: queue.ID,
  400. QueueCode: queue.QueueCode,
  401. Cluster: queue.Cluster,
  402. AiCenterCode: queue.AiCenterCode,
  403. AiCenterName: queue.AiCenterName,
  404. }, nil
  405. }
  406. func GetCloudbrainOneAccCardType(queueCode string) string {
  407. switch queueCode {
  408. case "a100":
  409. return "A100"
  410. case "openidebug":
  411. return "T4"
  412. case "openidgx":
  413. return "V100"
  414. }
  415. return ""
  416. }