annotate src/main/java/alice/topology/fix/FixTopology.java @ 492:24e535123df7 dispose

add PrepareToClose CodeSegment
author sugi
date Thu, 11 Dec 2014 06:29:31 +0900 (2014-12-10)
parents 9a7dd7591ddc
children f82f259ea93b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
474
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
1 package alice.topology.fix;
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
2
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
3 import java.util.HashMap;
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
4 import java.util.LinkedList;
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
5
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
6 import alice.codesegment.CodeSegment;
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
7 import alice.daemon.ConnectionInfo;
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
8 import alice.datasegment.CommandType;
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
9 import alice.datasegment.Receiver;
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
10 import alice.topology.HostMessage;
489
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
11 import alice.topology.manager.Parent;
476
8968b24c1ce3 TODO is to fix ParentManager information
sugi
parents: 474
diff changeset
12 import alice.topology.manager.ParentManager;
474
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
13
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
14 public class FixTopology extends CodeSegment {
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
15
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
16 private Receiver info = ids.create(CommandType.TAKE); // disconnection nodeInfo
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
17 private Receiver info1 = ids.create(CommandType.TAKE); // all connection Info
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
18 private Receiver info2 = ids.create(CommandType.TAKE); // node count
476
8968b24c1ce3 TODO is to fix ParentManager information
sugi
parents: 474
diff changeset
19 private Receiver info3 = ids.create(CommandType.TAKE); // IP Table
8968b24c1ce3 TODO is to fix ParentManager information
sugi
parents: 474
diff changeset
20 private Receiver info4 = ids.create(CommandType.TAKE); // parentManager
8968b24c1ce3 TODO is to fix ParentManager information
sugi
parents: 474
diff changeset
21
474
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
22 public FixTopology() {
478
cf345b10a21a bug fix
sugi
parents: 477
diff changeset
23 info.setKey("_DISCONNECTNODE");
474
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
24 info1.setKey("topology");
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
25 info2.setKey("hostCount");
476
8968b24c1ce3 TODO is to fix ParentManager information
sugi
parents: 474
diff changeset
26 info3.setKey("nameTable");
8968b24c1ce3 TODO is to fix ParentManager information
sugi
parents: 474
diff changeset
27 info4.setKey("parentManager");
474
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
28 }
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
29
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
30 @SuppressWarnings("unchecked")
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
31 @Override
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
32 public void run() {
484
sugi
parents: 482
diff changeset
33 ConnectionInfo lost = info.asClass(ConnectionInfo.class);
sugi
parents: 482
diff changeset
34 System.out.println(lost.port+" "+lost.nodeName);
476
8968b24c1ce3 TODO is to fix ParentManager information
sugi
parents: 474
diff changeset
35 HashMap<String, HostMessage> nameTable = info3.asClass(HashMap.class);
477
f644dc9e0589 TODO : add ReceiveCloseOrder CodeSegment in alice.topology.node package
sugi
parents: 476
diff changeset
36 ParentManager manager = info4.asClass(ParentManager.class);
f644dc9e0589 TODO : add ReceiveCloseOrder CodeSegment in alice.topology.node package
sugi
parents: 476
diff changeset
37 HashMap<String, LinkedList<HostMessage>> topology = info1.asClass(HashMap.class);
484
sugi
parents: 482
diff changeset
38 HostMessage lostNode = null;
sugi
parents: 482
diff changeset
39 String lostNodeName = null;
482
8227a2fd5a55 bug fix
sugi
parents: 478
diff changeset
40 for (String name : nameTable.keySet()) {
484
sugi
parents: 482
diff changeset
41 lostNode = nameTable.get(name);
sugi
parents: 482
diff changeset
42 if ((lostNode.name.equals(lost.addr)||lostNode.name.equals(lost.hostname))
sugi
parents: 482
diff changeset
43 && lostNode.port == lost.port) {
sugi
parents: 482
diff changeset
44 lostNodeName = name;
482
8227a2fd5a55 bug fix
sugi
parents: 478
diff changeset
45 break;
8227a2fd5a55 bug fix
sugi
parents: 478
diff changeset
46 }
484
sugi
parents: 482
diff changeset
47 lostNode = null;
482
8227a2fd5a55 bug fix
sugi
parents: 478
diff changeset
48 }
477
f644dc9e0589 TODO : add ReceiveCloseOrder CodeSegment in alice.topology.node package
sugi
parents: 476
diff changeset
49
484
sugi
parents: 482
diff changeset
50 if (lostNode!=null && lostNode.isAlive()) {
476
8968b24c1ce3 TODO is to fix ParentManager information
sugi
parents: 474
diff changeset
51 // change state not Alive
484
sugi
parents: 482
diff changeset
52 lostNode.alive = false;
476
8968b24c1ce3 TODO is to fix ParentManager information
sugi
parents: 474
diff changeset
53 // get lastJoinedNode
8968b24c1ce3 TODO is to fix ParentManager information
sugi
parents: 474
diff changeset
54 int last = info2.asInteger()-1;
482
8227a2fd5a55 bug fix
sugi
parents: 478
diff changeset
55 String lastJoinNodeName = "node"+last;
8227a2fd5a55 bug fix
sugi
parents: 478
diff changeset
56 LinkedList<HostMessage> connectionList = topology.get(lastJoinNodeName);
477
f644dc9e0589 TODO : add ReceiveCloseOrder CodeSegment in alice.topology.node package
sugi
parents: 476
diff changeset
57 // lastJoinedNode has one connection
482
8227a2fd5a55 bug fix
sugi
parents: 478
diff changeset
58 HostMessage parentNode = connectionList.poll();
492
24e535123df7 add PrepareToClose CodeSegment
sugi
parents: 489
diff changeset
59 Parent p = manager.find(parentNode.absName);
24e535123df7 add PrepareToClose CodeSegment
sugi
parents: 489
diff changeset
60 p.decrement();
474
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
61
482
8227a2fd5a55 bug fix
sugi
parents: 478
diff changeset
62 connectionList = topology.get(parentNode.absName);
8227a2fd5a55 bug fix
sugi
parents: 478
diff changeset
63 for (HostMessage node1 : connectionList) {
8227a2fd5a55 bug fix
sugi
parents: 478
diff changeset
64 if (node1.absName.equals(lastJoinNodeName)) {
8227a2fd5a55 bug fix
sugi
parents: 478
diff changeset
65 connectionList.remove(node1);
477
f644dc9e0589 TODO : add ReceiveCloseOrder CodeSegment in alice.topology.node package
sugi
parents: 476
diff changeset
66 break;
f644dc9e0589 TODO : add ReceiveCloseOrder CodeSegment in alice.topology.node package
sugi
parents: 476
diff changeset
67 }
f644dc9e0589 TODO : add ReceiveCloseOrder CodeSegment in alice.topology.node package
sugi
parents: 476
diff changeset
68 }
f644dc9e0589 TODO : add ReceiveCloseOrder CodeSegment in alice.topology.node package
sugi
parents: 476
diff changeset
69
484
sugi
parents: 482
diff changeset
70 if (!lastJoinNodeName.equals(lostNodeName)) {
489
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
71 if (!lostNodeName.equals(parentNode.remoteAbsName)) {
484
sugi
parents: 482
diff changeset
72 // send close message to lastJoinedNode
sugi
parents: 482
diff changeset
73 ods.put(lastJoinNodeName, "_CLOSEMESSEAGE", parentNode.reverseName);
sugi
parents: 482
diff changeset
74 // send close message to lastJoinedNode's parent
sugi
parents: 482
diff changeset
75 ods.put(parentNode.absName, "_CLOSEMESSEAGE", parentNode.connectionName);
sugi
parents: 482
diff changeset
76 }
474
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
77
482
8227a2fd5a55 bug fix
sugi
parents: 478
diff changeset
78 HostMessage lastJoinNode = nameTable.get(lastJoinNodeName);
8227a2fd5a55 bug fix
sugi
parents: 478
diff changeset
79 connectionList = topology.get(lastJoinNodeName);
484
sugi
parents: 482
diff changeset
80 LinkedList<HostMessage> disconnectionList = topology.get(lostNodeName);
476
8968b24c1ce3 TODO is to fix ParentManager information
sugi
parents: 474
diff changeset
81 // remove NodeInformation from disconnection node's children and parent
8968b24c1ce3 TODO is to fix ParentManager information
sugi
parents: 474
diff changeset
82 for (HostMessage node :disconnectionList) {
8968b24c1ce3 TODO is to fix ParentManager information
sugi
parents: 474
diff changeset
83 LinkedList<HostMessage> list = topology.get(node.absName);
484
sugi
parents: 482
diff changeset
84 if (!node.absName.equals(lastJoinNodeName)) {
sugi
parents: 482
diff changeset
85 for (HostMessage node1 : list) {
sugi
parents: 482
diff changeset
86 if (node1.absName.equals(lostNodeName)) {
sugi
parents: 482
diff changeset
87 node1.name = lastJoinNode.name;
sugi
parents: 482
diff changeset
88 node1.port = lastJoinNode.port;
sugi
parents: 482
diff changeset
89 node1.absName = lastJoinNodeName;
sugi
parents: 482
diff changeset
90 ods.put(node1.remoteAbsName, node1);
492
24e535123df7 add PrepareToClose CodeSegment
sugi
parents: 489
diff changeset
91 p = manager.find(lastJoinNodeName);
24e535123df7 add PrepareToClose CodeSegment
sugi
parents: 489
diff changeset
92 p.increment();
484
sugi
parents: 482
diff changeset
93 break;
sugi
parents: 482
diff changeset
94 }
sugi
parents: 482
diff changeset
95 }
sugi
parents: 482
diff changeset
96 node.remoteAbsName = lastJoinNodeName;
sugi
parents: 482
diff changeset
97 ods.put(node.remoteAbsName, node);
sugi
parents: 482
diff changeset
98 connectionList.add(node);
sugi
parents: 482
diff changeset
99 } else {
sugi
parents: 482
diff changeset
100 for (HostMessage node1 :disconnectionList) {
sugi
parents: 482
diff changeset
101 if ("parent".equals(node1.connectionName)) {
sugi
parents: 482
diff changeset
102 node1.reverseName = lastJoinNodeName;
sugi
parents: 482
diff changeset
103 list.add(node1);
sugi
parents: 482
diff changeset
104 ods.put(node1.remoteAbsName, node1);
sugi
parents: 482
diff changeset
105 break;
sugi
parents: 482
diff changeset
106 }
474
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
107 }
489
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
108
474
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
109 }
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
110 }
482
8227a2fd5a55 bug fix
sugi
parents: 478
diff changeset
111 disconnectionList.clear();
484
sugi
parents: 482
diff changeset
112 manager.replaceAndRemove(lostNodeName, lastJoinNodeName);
474
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
113 } else {
476
8968b24c1ce3 TODO is to fix ParentManager information
sugi
parents: 474
diff changeset
114 // disconnection node is lastJoinedNode
484
sugi
parents: 482
diff changeset
115 manager.remove(lostNodeName);
474
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
116 }
489
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
117
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
118 // need debug option
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
119 for (LinkedList<HostMessage> list :topology.values()){
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
120 if (list.size() !=0) {
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
121 System.out.print(list.get(0).remoteAbsName+" : ");
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
122 for (HostMessage host : list){
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
123 System.out.print("[ "+host.absName+" "+host.name+" "+host.port+" "+host.connectionName+" "+host.reverseName+" "+host.remoteAbsName+" ]");
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
124 }
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
125 System.out.println();
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
126 }
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
127 }
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
128
9a7dd7591ddc bug fix
sugi
parents: 484
diff changeset
129 manager.show();
474
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
130 } else {
476
8968b24c1ce3 TODO is to fix ParentManager information
sugi
parents: 474
diff changeset
131 // disconnect message already received.
474
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
132 }
478
cf345b10a21a bug fix
sugi
parents: 477
diff changeset
133
477
f644dc9e0589 TODO : add ReceiveCloseOrder CodeSegment in alice.topology.node package
sugi
parents: 476
diff changeset
134 ods.put(info1.key, topology);
f644dc9e0589 TODO : add ReceiveCloseOrder CodeSegment in alice.topology.node package
sugi
parents: 476
diff changeset
135 ods.put(info2.key, info2.getReceiveData());
f644dc9e0589 TODO : add ReceiveCloseOrder CodeSegment in alice.topology.node package
sugi
parents: 476
diff changeset
136 ods.put(info3.key, nameTable);
f644dc9e0589 TODO : add ReceiveCloseOrder CodeSegment in alice.topology.node package
sugi
parents: 476
diff changeset
137 ods.put(info4.key, manager);
474
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
138 }
6a71732cd3ee add topology fix package
sugi
parents:
diff changeset
139 }