overlord.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "strconv"
  7. "go-common/app/admin/main/cache/model"
  8. "go-common/library/ecode"
  9. "github.com/BurntSushi/toml"
  10. )
  11. // OpsClusterNames .
  12. func (s *Service) OpsClusterNames(c context.Context, arg *model.OverlordReq) (resp *model.OverlordResp, err error) {
  13. resp = &model.OverlordResp{}
  14. if arg.Type == "memcache" {
  15. for _, opsmc := range s.opsMcs {
  16. resp.Names = append(resp.Names, opsmc.Labels.Name)
  17. }
  18. } else if arg.Type == "redis" {
  19. for _, opsrd := range s.opsRds {
  20. if opsrd.Type == "redis_standalone" {
  21. resp.Names = append(resp.Names, opsrd.Labels.Name)
  22. }
  23. }
  24. } else if arg.Type == "redis_cluster" {
  25. for _, opsrd := range s.opsRds {
  26. if opsrd.Type == "redis_cluster" {
  27. resp.Names = append(resp.Names, opsrd.Labels.Name)
  28. }
  29. }
  30. } else {
  31. err = fmt.Errorf("unsupport type:%s", arg.Type)
  32. }
  33. return
  34. }
  35. // OpsClusterNodes .
  36. func (s *Service) OpsClusterNodes(c context.Context, arg *model.OverlordReq) (resp *model.OverlordResp, err error) {
  37. resp = &model.OverlordResp{}
  38. if arg.Type == "memcache" {
  39. for _, opsmc := range s.opsMcs {
  40. if arg.Name == opsmc.Labels.Name {
  41. resp.Addrs = opsmc.Targets
  42. return
  43. }
  44. }
  45. } else if arg.Type == "redis" {
  46. for _, opsrd := range s.opsRds {
  47. if opsrd.Type == "redis_standalone" && arg.Name == opsrd.Labels.Name {
  48. resp.Addrs = opsrd.Targets
  49. return
  50. }
  51. }
  52. } else if arg.Type == "redis_cluster" {
  53. for _, opsrd := range s.opsRds {
  54. if opsrd.Type == "redis_cluster" && arg.Name == opsrd.Labels.Name {
  55. resp.Addrs = opsrd.Targets
  56. return
  57. }
  58. }
  59. } else {
  60. err = fmt.Errorf("unsupport type:%s", arg.Type)
  61. }
  62. return
  63. }
  64. // ImportOpsCluster .
  65. func (s *Service) ImportOpsCluster(c context.Context, arg *model.OverlordReq) (resp *model.EmpResp, err error) {
  66. exist := 0
  67. if err = s.dao.DB.Model(&model.OverlordCluster{}).Where("name=?", arg.Name).Count(&exist).Error; err != nil {
  68. return
  69. }
  70. if exist > 0 {
  71. return
  72. }
  73. var targets []string
  74. if arg.Type == "memcache" {
  75. for _, opsmc := range s.opsMcs {
  76. if arg.Name == opsmc.Labels.Name {
  77. targets = opsmc.Targets
  78. break
  79. }
  80. }
  81. } else if arg.Type == "redis" {
  82. for _, opsrd := range s.opsRds {
  83. if opsrd.Type == "redis_standalone" && arg.Name == opsrd.Labels.Name {
  84. targets = opsrd.Targets
  85. break
  86. }
  87. }
  88. } else if arg.Type == "redis_cluster" {
  89. for _, opsrd := range s.opsRds {
  90. if opsrd.Type == "redis_cluster" && arg.Name == opsrd.Labels.Name {
  91. targets = opsrd.Targets
  92. break
  93. }
  94. }
  95. } else {
  96. err = fmt.Errorf("unsupport type:%s", arg.Type)
  97. return
  98. }
  99. port := 0
  100. if err = s.dao.DB.Model(&model.OverlordCluster{}).Where("type=?", arg.Type).Count(&port).Error; err != nil {
  101. return
  102. }
  103. if arg.Type == "memcache" {
  104. port += 11211
  105. } else {
  106. port += 26379
  107. }
  108. tranDB := s.dao.DB.Begin()
  109. oc := &model.OverlordCluster{
  110. Name: arg.Name,
  111. Type: arg.Type,
  112. Zone: arg.Zone,
  113. HashMethod: "fnv1a_64",
  114. HashDistribution: "ketama",
  115. HashTag: "",
  116. ListenProto: "tcp",
  117. ListenAddr: "0.0.0.0:" + strconv.Itoa(port),
  118. DailTimeout: 1000,
  119. ReadTimeout: 1000,
  120. WriteTimeout: 1000,
  121. NodeConn: 2,
  122. PingFailLimit: 3,
  123. PingAutoEject: true,
  124. }
  125. if err = tranDB.Create(oc).Error; err != nil {
  126. tranDB.Rollback()
  127. return
  128. }
  129. for i, target := range targets {
  130. ocn := &model.OverlordNode{
  131. Cid: oc.ID,
  132. Alias: arg.Name + strconv.Itoa(i+1),
  133. Addr: target,
  134. Weight: 1,
  135. }
  136. if err = tranDB.Create(ocn).Error; err != nil {
  137. tranDB.Rollback()
  138. return
  139. }
  140. }
  141. err = tranDB.Commit().Error
  142. return
  143. }
  144. // OverlordClusters .
  145. func (s *Service) OverlordClusters(c context.Context, arg *model.OverlordReq) (resp *model.OverlordResp, err error) {
  146. resp = &model.OverlordResp{}
  147. if arg.Name != "" {
  148. err = s.dao.DB.Where("zone=? AND type=? AND name like ?", arg.Zone, arg.Type, "%"+arg.Name+"%").Order("id desc").Offset((arg.PN - 1) * arg.PS).Limit(arg.PS).Find(&resp.Clusters).Error
  149. s.dao.DB.Model(&model.OverlordCluster{}).Where("zone=? AND type=? AND name like ?", arg.Zone, arg.Type, arg.Name).Count(&resp.Total)
  150. } else {
  151. err = s.dao.DB.Where("zone=? AND type=?", arg.Zone, arg.Type).Order("id desc").Offset((arg.PN - 1) * arg.PS).Limit(arg.PS).Find(&resp.Clusters).Error
  152. s.dao.DB.Model(&model.OverlordCluster{}).Where("zone=? AND type=?", arg.Zone, arg.Type).Count(&resp.Total)
  153. }
  154. if err != nil {
  155. return
  156. }
  157. for _, cluster := range resp.Clusters {
  158. var ens *model.OverlordResp
  159. if ens, err = s.ExistOverlordNodes(c, &model.OverlordReq{Name: cluster.Name}); err != nil {
  160. return
  161. }
  162. cluster.Nodes = ens.Nodes
  163. }
  164. return
  165. }
  166. // ExistOverlordNodes .
  167. func (s *Service) ExistOverlordNodes(c context.Context, arg *model.OverlordReq) (resp *model.OverlordResp, err error) {
  168. cluster := &model.OverlordCluster{}
  169. if err = s.dao.DB.Model(cluster).Where("name=?", arg.Name).First(cluster).Error; err != nil {
  170. fmt.Printf("get cluster err %v\n", err)
  171. return
  172. }
  173. if cluster.ID == 0 {
  174. err = fmt.Errorf("cluster not exist:%s", arg.Name)
  175. return
  176. }
  177. var exists []*model.OverlordNode
  178. if err = s.dao.DB.Where("cid=?", cluster.ID).Order("id").Find(&exists).Error; err != nil {
  179. return
  180. }
  181. resp = &model.OverlordResp{}
  182. resp.Cluster = cluster
  183. resp.Nodes = exists
  184. return
  185. }
  186. // NotExistOverlordAddrs .
  187. func (s *Service) NotExistOverlordAddrs(c context.Context, arg *model.OverlordReq) (resp *model.OverlordResp, err error) {
  188. cluster := &model.OverlordCluster{}
  189. if err = s.dao.DB.Model(cluster).Where("name=?", arg.Name).First(cluster).Error; err != nil {
  190. return
  191. }
  192. if cluster.ID == 0 {
  193. err = fmt.Errorf("cluster not exist:%s", arg.Name)
  194. return
  195. }
  196. var targets []string
  197. tp := arg.Type
  198. if tp == "memcache" {
  199. for _, opsmc := range s.opsMcs {
  200. if arg.Name == opsmc.Labels.Name {
  201. targets = opsmc.Targets
  202. break
  203. }
  204. }
  205. } else if tp == "redis" {
  206. for _, opsrd := range s.opsRds {
  207. if arg.Name == opsrd.Labels.Name {
  208. targets = opsrd.Targets
  209. if opsrd.Type == "redis_cluster" {
  210. tp = "redis_cluster"
  211. }
  212. break
  213. }
  214. }
  215. } else {
  216. err = fmt.Errorf("unsupport type:%s", arg.Type)
  217. return
  218. }
  219. var exists []*model.OverlordNode
  220. if err = s.dao.DB.Where("cid=?", cluster.ID).Order("id").Find(&exists).Error; err != nil {
  221. return
  222. }
  223. resp = &model.OverlordResp{}
  224. NEXT:
  225. for _, target := range targets {
  226. for _, exist := range exists {
  227. if target == exist.Addr {
  228. continue NEXT
  229. }
  230. }
  231. resp.Addrs = append(resp.Addrs, target)
  232. }
  233. return
  234. }
  235. // ImportOpsNode .
  236. func (s *Service) ImportOpsNode(c context.Context, arg *model.OverlordReq) (resp *model.EmpResp, err error) {
  237. nen, err := s.NotExistOverlordAddrs(c, arg)
  238. if err != nil {
  239. return
  240. }
  241. en, err := s.ExistOverlordNodes(c, arg)
  242. if err != nil {
  243. return
  244. }
  245. i := len(en.Nodes)
  246. tranDB := s.dao.DB.Begin()
  247. for _, target := range nen.Addrs {
  248. ocn := &model.OverlordNode{
  249. Cid: en.Cluster.ID,
  250. Alias: arg.Name + strconv.Itoa(i+1),
  251. Addr: target,
  252. Weight: 1,
  253. }
  254. if err = tranDB.Create(ocn).Error; err != nil {
  255. tranDB.Rollback()
  256. return
  257. }
  258. i++
  259. }
  260. err = tranDB.Commit().Error
  261. return
  262. }
  263. // ReplaceOpsNode .
  264. func (s *Service) ReplaceOpsNode(c context.Context, arg *model.OverlordReq) (resp *model.EmpResp, err error) {
  265. nen, err := s.NotExistOverlordAddrs(c, arg)
  266. if err != nil {
  267. return
  268. }
  269. if len(nen.Addrs) == 0 {
  270. err = fmt.Errorf("cluster have not new node:%s", arg.Name)
  271. return
  272. }
  273. en, err := s.ExistOverlordNodes(c, arg)
  274. if err != nil {
  275. return
  276. }
  277. for _, n := range en.Nodes {
  278. if n.Alias != arg.Alias && n.Addr == arg.Addr {
  279. err = fmt.Errorf("cluster:%s node:%s used by other node:%s ", arg.Name, arg.Addr, n.Alias)
  280. return
  281. }
  282. }
  283. node := &model.OverlordNode{}
  284. if err = s.dao.DB.Model(node).Where("cid=? AND alias=?", en.Cluster.ID, arg.Alias).First(node).Error; err != nil {
  285. return
  286. }
  287. if node.Addr == arg.Addr {
  288. return
  289. }
  290. err = s.dao.DB.Model(node).Where("alias=? AND addr=?", node.Alias, node.Addr).Update("addr", arg.Addr).Error
  291. return
  292. }
  293. // DelOverlordCluster .
  294. func (s *Service) DelOverlordCluster(c context.Context, arg *model.OverlordReq) (resp *model.EmpResp, err error) {
  295. en, err := s.ExistOverlordNodes(c, arg)
  296. if err != nil {
  297. return
  298. }
  299. if err = s.dao.DB.Delete(en.Cluster).Error; err != nil {
  300. return
  301. }
  302. err = s.dao.DB.Delete(&model.OverlordNode{}, "cid=?", en.Cluster.ID).Error
  303. return
  304. }
  305. // DelOverlordNode .
  306. func (s *Service) DelOverlordNode(c context.Context, arg *model.OverlordReq) (resp *model.EmpResp, err error) {
  307. en, err := s.ExistOverlordNodes(c, arg)
  308. if err != nil {
  309. return
  310. }
  311. err = s.dao.DB.Delete(&model.OverlordNode{}, "cid=? AND alias=? AND addr=?", en.Cluster.ID, arg.Alias, arg.Addr).Error
  312. return
  313. }
  314. // OverlordAppClusters .
  315. func (s *Service) OverlordAppClusters(c context.Context, arg *model.OverlordReq) (resp *model.OverlordResp, err error) {
  316. appids, err := s.appids(c, arg.Cookie, arg.AppID)
  317. if err != nil {
  318. err = ecode.AccessDenied
  319. return
  320. }
  321. resp = &model.OverlordResp{}
  322. if len(appids) <= 1 {
  323. err = s.dao.DB.Where("app_id like ?", "%"+arg.AppID+"%").Order("id desc").Offset((arg.PN - 1) * arg.PS).Limit(arg.PS).Find(&resp.Apps).Error
  324. s.dao.DB.Model(&model.OverlordApp{}).Where("app_id like ?", arg.AppID).Count(&resp.Total)
  325. } else if len(appids) > 1 {
  326. err = s.dao.DB.Where("app_id in (?)", appids).Order("id desc").Offset((arg.PN - 1) * arg.PS).Limit(arg.PS).Find(&resp.Apps).Error
  327. s.dao.DB.Model(&model.OverlordApp{}).Where("app_id in (?)", appids).Count(&resp.Total)
  328. }
  329. if err != nil || len(resp.Apps) == 0 {
  330. return
  331. }
  332. var cids []int64
  333. for _, app := range resp.Apps {
  334. cids = append(cids, app.Cid)
  335. }
  336. var clusters []*model.OverlordCluster
  337. if err = s.dao.DB.Find(&clusters, "id in (?)", cids).Error; err != nil {
  338. return
  339. }
  340. for _, cluster := range clusters {
  341. var ens *model.OverlordResp
  342. if ens, err = s.ExistOverlordNodes(c, &model.OverlordReq{Name: cluster.Name}); err != nil {
  343. return
  344. }
  345. cluster.Nodes = ens.Nodes
  346. for _, app := range resp.Apps {
  347. if cluster.ID == app.Cid {
  348. app.Cluster = cluster
  349. }
  350. }
  351. }
  352. if len(appids) <= 1 {
  353. // 当使用appid查询时,填充overlord-mesos的数据
  354. if ocs, err := s.dao.OverlordClusters(c, "", arg.AppID); err == nil {
  355. clusters = append(clusters, ocs...)
  356. }
  357. }
  358. return
  359. }
  360. // OverlordAppCanBindClusters .
  361. func (s *Service) OverlordAppCanBindClusters(c context.Context, arg *model.OverlordReq) (resp *model.OverlordResp, err error) {
  362. resp = &model.OverlordResp{}
  363. err = s.dao.DB.Where("zone=? AND type=?", arg.Zone, arg.Type).Find(&resp.Clusters).Error
  364. if err != nil {
  365. return
  366. }
  367. for _, cluster := range resp.Clusters {
  368. resp.Names = append(resp.Names, cluster.Name)
  369. }
  370. resp.Clusters = nil
  371. return
  372. }
  373. // OverlordAppClusterBind .
  374. func (s *Service) OverlordAppClusterBind(c context.Context, arg *model.OverlordReq) (resp *model.EmpResp, err error) {
  375. treeid, err := s.treeid(c, arg.Cookie, arg.AppID)
  376. if err != nil || treeid == 0 {
  377. err = ecode.AccessDenied
  378. return
  379. }
  380. cluster := &model.OverlordCluster{}
  381. if err = s.dao.DB.Model(cluster).Where("zone=? AND type=? AND name=?", arg.Zone, arg.Type, arg.Name).First(cluster).Error; err != nil {
  382. return
  383. }
  384. app := &model.OverlordApp{
  385. TreeID: treeid,
  386. AppID: arg.AppID,
  387. Cid: cluster.ID,
  388. }
  389. err = s.dao.DB.Create(app).Error
  390. return
  391. }
  392. // OverlordAppClusterDel .
  393. func (s *Service) OverlordAppClusterDel(c context.Context, arg *model.OverlordReq) (resp *model.EmpResp, err error) {
  394. treeid, err := s.treeid(c, arg.Cookie, arg.AppID)
  395. if err != nil || treeid == 0 {
  396. err = ecode.AccessDenied
  397. return
  398. }
  399. cluster := &model.OverlordCluster{}
  400. if err = s.dao.DB.Model(cluster).Where("zone=? AND type=? AND name=?", arg.Zone, arg.Type, arg.Name).First(cluster).Error; err != nil {
  401. return
  402. }
  403. app := &model.OverlordApp{}
  404. if err = s.dao.DB.Model(app).Where("app_id=? AND cid=?", arg.AppID, cluster.ID).First(app).Error; err != nil {
  405. return
  406. }
  407. err = s.dao.DB.Table(app.TableName()).Delete(app).Error
  408. return
  409. }
  410. // OverlordAppAppIDs .
  411. func (s *Service) OverlordAppAppIDs(c context.Context, arg *model.OverlordReq) (resp *model.OverlordResp, err error) {
  412. appids, err := s.appids(c, arg.Cookie, "")
  413. if err != nil {
  414. err = ecode.AccessDenied
  415. return
  416. }
  417. resp = &model.OverlordResp{}
  418. resp.AppIDs = appids
  419. return
  420. }
  421. // OverlordToml return a toml file of cluster infos.
  422. func (s *Service) OverlordToml(c context.Context, arg *model.OverlordReq) (resp []byte, err error) {
  423. var apps []*model.OverlordApp
  424. if err = s.dao.DB.Where("app_id=?", arg.AppID).Find(&apps).Error; err != nil {
  425. return
  426. }
  427. var cids []int64
  428. for _, app := range apps {
  429. cids = append(cids, app.Cid)
  430. }
  431. var clusters []*model.OverlordCluster
  432. // TODO(felix): 待都走overlord-mesos后干掉
  433. if err = s.dao.DB.Where("zone=? AND id in (?)", arg.Zone, cids).Find(&clusters).Error; err != nil {
  434. return
  435. }
  436. if len(clusters) == 0 {
  437. // TODO(felix): 待都走overlord-mesos后干掉
  438. if err = s.dao.DB.Where("zone='sh001' AND id in (?)", cids).Find(&clusters).Error; err != nil {
  439. return
  440. }
  441. }
  442. var ocs []*model.OverlordCluster
  443. if ocs, err = s.dao.OverlordClusters(c, arg.Zone, arg.AppID); err == nil {
  444. if len(ocs) == 0 {
  445. ocs, err = s.dao.OverlordClusters(c, "sh001", arg.AppID)
  446. }
  447. if len(ocs) > 0 {
  448. clusters = append(clusters, ocs...)
  449. }
  450. }
  451. t := struct {
  452. Clusters []*model.OverlordToml `toml:"clusters"`
  453. }{}
  454. for _, cluster := range clusters {
  455. ot := &model.OverlordToml{
  456. Name: cluster.Name,
  457. Type: cluster.Type,
  458. HashMethod: cluster.HashMethod,
  459. HashDistribution: cluster.HashDistribution,
  460. HashTag: cluster.HashTag,
  461. ListenProto: cluster.ListenProto,
  462. ListenAddr: cluster.ListenAddr,
  463. DailTimeout: cluster.DailTimeout,
  464. ReadTimeout: cluster.ReadTimeout,
  465. WriteTimeout: cluster.WriteTimeout,
  466. NodeConn: cluster.NodeConn,
  467. PingFailLimit: cluster.PingFailLimit,
  468. PingAutoEject: cluster.PingAutoEject,
  469. }
  470. var nodes []*model.OverlordNode
  471. if len(cluster.Nodes) == 0 {
  472. if err = s.dao.DB.Where("cid=?", cluster.ID).Order("id").Find(&nodes).Error; err != nil {
  473. return
  474. }
  475. } else {
  476. nodes = cluster.Nodes
  477. }
  478. var servers []string
  479. for _, node := range nodes {
  480. var server string
  481. if cluster.Type == "redis_cluster" {
  482. server = node.Addr
  483. } else {
  484. server = fmt.Sprintf("%s:%d %s", node.Addr, node.Weight, node.Alias)
  485. }
  486. servers = append(servers, server)
  487. }
  488. ot.Servers = servers
  489. t.Clusters = append(t.Clusters, ot)
  490. }
  491. buf := bytes.NewBuffer(resp)
  492. err = toml.NewEncoder(buf).Encode(t)
  493. resp = buf.Bytes()
  494. return
  495. }