PageRenderTime 46ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/cn/com/sina/rd/dsp/model/Cluster.java

https://gitlab.com/zxinrong3/smartbalance
Java | 139 lines | 106 code | 25 blank | 8 comment | 7 complexity | 5beeea64d848b088a3d8e735b053f253 MD5 | raw file
  1. package cn.com.sina.rd.dsp.model;
  2. import cn.com.sina.rd.dsp.tools.MoveOnce;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.google.common.collect.Lists;
  6. import com.google.common.collect.Maps;
  7. import java.util.*;
  8. /**
  9. * Created with IntelliJ IDEA.
  10. * User: xinrong3
  11. * Date: 14-1-10
  12. * Time: 下午3:36
  13. */
  14. public class Cluster implements Weight{
  15. public static final String CLUSTER_NAME="cluster_name";
  16. public static final String HIGH_GATE ="high_gate";
  17. public static final String LOW_GATE ="low_gate";
  18. public static final String SERVERS="servers";
  19. private String name;
  20. private long highgate;
  21. private long lowgate;
  22. private final boolean acend;
  23. private TreeMap<Server,Long> maps= Maps.newTreeMap();
  24. public Cluster(){
  25. acend=true;
  26. }
  27. public Cluster(boolean acend){
  28. this.acend=acend;
  29. }
  30. public TreeMap<Server, Long> getMaps() {
  31. return maps;
  32. }
  33. public String getName() {
  34. return name;
  35. }
  36. public boolean isAcend() {
  37. return acend;
  38. }
  39. public void setName(String name) {
  40. this.name = name;
  41. }
  42. public long getHighgate() {
  43. return highgate;
  44. }
  45. public long getLowgate() {
  46. return highgate/10;
  47. }
  48. public void setLowgate(long lowgate) {
  49. this.lowgate = lowgate;
  50. }
  51. public void setHighgate(long highgate) {
  52. this.highgate = highgate;
  53. setLowgate(highgate/10);
  54. }
  55. public void add2Maps(Server ship) {
  56. maps.put(ship,ship.getWeight());
  57. }
  58. public static Cluster createByJSON(JSONObject json,boolean acend){
  59. Cluster cluster=new Cluster(acend);
  60. cluster.setName(json.getString(CLUSTER_NAME));
  61. cluster.setHighgate(json.getLong(HIGH_GATE));
  62. JSONArray array=json.getJSONArray(SERVERS);
  63. for(int i=0;i<array.size();i++){
  64. Server ship= Server.createByJson(array.getJSONObject(i), cluster.acend);
  65. cluster.add2Maps(ship);
  66. }
  67. return cluster;
  68. }
  69. public static JSONObject switch2JSON(Cluster cluster){
  70. JSONObject josn=new JSONObject();
  71. josn.put(CLUSTER_NAME,cluster.getName());
  72. josn.put(HIGH_GATE,cluster.getHighgate());
  73. JSONArray servers=new JSONArray();
  74. josn.put(SERVERS,servers);
  75. //many server
  76. for(Server server:cluster.getMaps().keySet()){
  77. JSONObject server_arr=new JSONObject();
  78. JSONArray region_arr=new JSONArray();
  79. //many load
  80. List<Load> loadList= Lists.newArrayList(server.getLoadMap().keySet());
  81. for(Load load:loadList){
  82. JSONObject single_load=new JSONObject();
  83. single_load.put(Load.LOAD_NANE, load.getName());
  84. single_load.put(Load.WEIGHT, load.getWeight());
  85. region_arr.add(single_load);
  86. }
  87. server_arr.put(server.getServerName(), region_arr);
  88. servers.add(server_arr);
  89. }
  90. return josn;
  91. }
  92. @Override
  93. public Long getWeight() {
  94. long total=0l;
  95. for(Long value:maps.values()){
  96. total+=value;
  97. }
  98. return total;
  99. }
  100. public static void main(String ...args){
  101. }
  102. public void move(MoveOnce moveOnce) {
  103. Server from=moveOnce.getFrom();
  104. Load load=moveOnce.getName();
  105. Server to=moveOnce.getTo();
  106. for(Server server:maps.keySet()){
  107. if(server.equals(from)){
  108. server.getLoadMap().remove(load);
  109. }else if(server.equals(to)){
  110. server.addLoadMap(load);
  111. }
  112. }
  113. }
  114. }