sort_by_disk.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. 按照磁盘占用率从大到小装箱,即按照磁盘先用完为止进行分配实例到主机。
  5. @Auther :liuyuqi.gov@msn.cn
  6. @Time :2018/7/7 0:43
  7. @File :sort_by_disk.py
  8. """
  9. import matplotlib
  10. matplotlib.use('Agg')
  11. import pandas as pd
  12. from configparser import ConfigParser
  13. import time
  14. import libs.save_result
  15. cf = ConfigParser()
  16. config_path = "../conf/config.ini"
  17. section_name = "data_file_name"
  18. cf.read(config_path)
  19. app_interference = cf.get(section_name, "app_interference")
  20. app_resources = cf.get(section_name, "app_resources")
  21. instance = cf.get(section_name, "instance")
  22. # app
  23. df1 = pd.read_csv(app_resources, encoding="utf-8")
  24. # instance
  25. df3 = pd.read_csv(instance)
  26. # df3 = pd.read_csv("../data/test-instance.csv")
  27. df3["cpu"] = df3["cpu"].astype("float")
  28. df3["disk"] = df3["disk"].astype("float")
  29. df3["mem"] = df3["mem"].astype("float")
  30. df3["M"] = df3["M"].astype("float")
  31. df3["P"] = df3["P"].astype("float")
  32. df3["PM"] = df3["PM"].astype("float")
  33. df3["isdeploy"] = False
  34. # machine
  35. # 其实就两类,所以就不需要导入数据了。
  36. # 限制表
  37. df4 = pd.read_csv(app_interference, header=None,
  38. names=list(["appid1", "appid2", "max_interference"]), encoding="utf-8")
  39. result = pd.DataFrame(columns=list(["instanceid", "machineid"]), data=list())
  40. tem_pre_disk = tem_pre_mem = tem_pre_cpu = tem_pre_P = tem_pre_M = tem_pre_PM = 0
  41. tem_disk = tem_mem = tem_cpu = tem_P = tem_M = tem_PM = 0
  42. tmp_stand_cpu1 = 32
  43. tmp_stand_mem1 = 64
  44. tmp_stand_disk1 = 600
  45. tmp_stand_cpu2 = 92
  46. tmp_stand_mem2 = 288
  47. tmp_stand_disk2 = 1024
  48. tmp_stand_P = 7
  49. tmp_stand_M1 = 3
  50. tmp_stand_M2 = 7
  51. tmp_stand_PM1 = 7
  52. tmp_stand_PM2 = 9
  53. machine_count = 0 # 3000小机器,3000大机器。所以在小机器用完换大机器
  54. j = 1 # j表示主机序号,从1-3000,3001到6000
  55. is_deploy = False # 主机j是否部署了instance
  56. deploy_list = list() # 主机j部署的instanceid实例
  57. # 各app之间的限制
  58. def restrict_apps(instance, deploy_list):
  59. len_list = len(deploy_list)
  60. if len_list == 0:
  61. return True
  62. else:
  63. ct = pd.Series(deploy_list).value_counts()
  64. for k, v in ct.items():
  65. # tmp表示在df4中找到限制条件一行
  66. tmp = df4.loc[(df4["appid1"] == k) & (df4["appid2"] == instance)]
  67. row, col = tmp.shape
  68. if row > 0:
  69. if instance in ct.index:
  70. if k == instance:
  71. # a a 2 表示有一个a前提,还可以放2个a,最多可以放3个a
  72. if ct[instance] > tmp["max_interference"].values[0]:
  73. return False
  74. else:
  75. # a b 2 表示有一个a前提,还可以放2个b,最多可以放2个b
  76. if ct[instance] + 1 > tmp["max_interference"].values[0]:
  77. return False
  78. else:
  79. # a e 2 表示有一个a前提,还可以放2个e,最多可以放2个b ct[instance]=0
  80. if 1 > tmp["max_interference"].values[0]:
  81. return False
  82. return True
  83. # 执行部署方案
  84. def deploy():
  85. global j, is_deploy, tem_mem, tem_cpu, tem_disk, tem_P, tem_M, tem_PM, tem_pre_disk, tem_pre_mem, \
  86. tem_pre_cpu, tem_pre_P, tem_pre_M, tem_pre_PM, result, df3, deploy_list
  87. print("------------开始部署啦--------------")
  88. start = time.time()
  89. row, column = df3.shape
  90. while row > 0:
  91. deployInstance()
  92. # 整个instace都遍历了,第j主机无法再放入一个,所以添加j+1主机
  93. df3 = df3[df3["isdeploy"] == False]
  94. row, column = df3.shape
  95. df3 = df3.reset_index(drop=True)
  96. j = j + 1
  97. # j++之后表示新建主机,所以新主机没有部署任何实例,为false,然后初始化所有其他参数
  98. is_deploy = False
  99. tem_pre_disk = tem_pre_mem = tem_pre_cpu = tem_pre_P = tem_pre_M = tem_pre_PM = 0
  100. tem_disk = tem_mem = tem_cpu = tem_P = tem_M = tem_PM = 0
  101. deploy_list = list()
  102. print("已经部署:", 68219 - row, "剩余部署Instance数据:", row)
  103. print("已经消耗Machine主机数据:", j)
  104. print("已经消耗时间:", time.time() - start, "秒")
  105. # 部署完事
  106. print("------------部署完啦--------------")
  107. end = time.time()
  108. print("总共耗时:", end - start, "秒")
  109. print("总共需要主机数:", j)
  110. print("部署方案前几条示意:", result.head())
  111. libs.save_result.save_result(result)
  112. def deployInstance():
  113. """
  114. 根据限制部署实例到主机上
  115. :return: 暂未定返回值,None
  116. """
  117. global is_deploy, tem_mem, tem_cpu, tem_disk, tem_P, tem_M, tem_PM, tem_pre_disk, \
  118. tem_pre_mem, tem_pre_cpu, tem_pre_P, tem_pre_M, tem_pre_PM, result, j, df3, deploy_list
  119. for row in df3.itertuples():
  120. i = row.Index
  121. tem_pre_cpu = tem_cpu + row.cpu
  122. tem_pre_mem = tem_mem + row.mem
  123. tem_pre_disk = tem_disk + row.disk # 当前磁盘消耗
  124. tem_pre_P = tem_P + row.P
  125. tem_pre_M = tem_M + row.M
  126. tem_pre_PM = tem_PM + row.PM
  127. # if 满足限制表条件,则把当前实例部署到这台主机上。
  128. if j > 3000: # 使用小主机
  129. if is_deploy:
  130. if tem_pre_disk <= tmp_stand_disk1: # 磁盘够
  131. if tem_pre_mem < tmp_stand_mem1: # 内存够
  132. if tem_pre_cpu < tmp_stand_cpu1: # CPU够
  133. if tem_pre_M <= tmp_stand_M1:
  134. if tem_pre_P <= tmp_stand_P:
  135. if tem_pre_PM <= tmp_stand_PM1:
  136. if restrict_apps(instance=row.instanceid, deploy_list=deploy_list):
  137. # 条件都满足,则把instance放入主机,同时df3表中去掉这个部署好的一行
  138. result = result.append(pd.DataFrame(
  139. [{"instanceid": row.instanceid,
  140. "machineid": "machine_" + str(j)}]))
  141. tem_disk = tem_disk + row.disk
  142. tem_mem = tem_mem + row.mem
  143. tem_cpu = tem_cpu + row.cpu
  144. tem_P = tem_P + row.P
  145. tem_M = tem_M + row.M
  146. tem_PM = tem_PM + row.PM
  147. df3.loc[i, "isdeploy"] = True
  148. deploy_list.append(row.instanceid)
  149. else:
  150. # 主机j没有部署实例,则先部署一个
  151. result = result.append(
  152. pd.DataFrame([{"instanceid": row.instanceid, "machineid": "machine_" + str(j)}]))
  153. tem_disk = tem_disk + row.disk
  154. tem_mem = tem_mem + row.mem
  155. tem_cpu = tem_cpu + row.cpu
  156. tem_P = tem_P + row.P
  157. tem_M = tem_M + row.M
  158. tem_PM = tem_PM + row.PM
  159. df3.loc[i, "isdeploy"] = True
  160. deploy_list.append(row.instanceid)
  161. # df3["isdeploy"][i] = True
  162. is_deploy = True
  163. else: # 使用大主机
  164. if is_deploy:
  165. if tem_pre_disk <= tmp_stand_disk2: # 磁盘够
  166. if tem_pre_mem < tmp_stand_mem2: # 内存够
  167. if tem_pre_cpu < tmp_stand_cpu2: # CPU够
  168. if tem_pre_M <= tmp_stand_M2:
  169. if tem_pre_P <= tmp_stand_P:
  170. if tem_pre_PM <= tmp_stand_PM2:
  171. if restrict_apps(instance=row.instanceid, deploy_list=deploy_list):
  172. # 条件都满足,则把instance放入主机
  173. result = result.append(pd.DataFrame(
  174. [{"instanceid": row.instanceid,
  175. "machineid": "machine_" + str(j)}]))
  176. tem_disk = tem_disk + row.disk
  177. tem_mem = tem_mem + row.mem
  178. tem_cpu = tem_cpu + row.cpu
  179. tem_P = tem_P + row.P
  180. tem_M = tem_M + row.M
  181. tem_PM = tem_PM + row.PM
  182. df3.loc[i, "isdeploy"] = True
  183. deploy_list.append(row.instanceid)
  184. else:
  185. # 主机j没有部署实例,则先部署一个
  186. result = result.append(
  187. pd.DataFrame([{"instanceid": row.instanceid, "machineid": "machine_" + str(j)}]))
  188. tem_disk = tem_disk + row.disk
  189. tem_mem = tem_mem + row.mem
  190. tem_cpu = tem_cpu + row.cpu
  191. tem_P = tem_P + row.P
  192. tem_M = tem_M + row.M
  193. tem_PM = tem_PM + row.PM
  194. df3.loc[i, "isdeploy"] = True
  195. deploy_list.append(row.instanceid)
  196. is_deploy = True
  197. deploy()