sort_static.py 9.4 KB

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