1 package org.apache.helix.controller;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.TreeMap;
28 import java.util.TreeSet;
29
30 import org.apache.helix.ZNRecord;
31 import org.apache.helix.model.Message;
32 import org.apache.helix.model.CurrentState.CurrentStateProperty;
33 import org.apache.log4j.Logger;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class ExternalViewGenerator
50 {
51 static Logger _logger = Logger.getLogger(ExternalViewGenerator.class);
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public Map<String, Map<String, Set<String>>> getRouterMapFromExternalView(
67 List<ZNRecord> externalViewList)
68 {
69 Map<String, Map<String, Set<String>>> result = new TreeMap<String, Map<String, Set<String>>>();
70
71 for (ZNRecord nodeView : externalViewList)
72 {
73 Map<String, Map<String, String>> partitionNodeStateMap = nodeView
74 .getMapFields();
75 for (String partitionId : partitionNodeStateMap.keySet())
76 {
77 if (!result.containsKey(partitionId))
78 {
79 result.put(partitionId, new TreeMap<String, Set<String>>());
80 }
81 Map<String, String> nodeStateMap = partitionNodeStateMap.get(partitionId);
82 for (String nodeName : nodeStateMap.keySet())
83 {
84 String state = nodeStateMap.get(nodeName);
85 if (!result.get(partitionId).containsKey(state))
86 {
87 result.get(partitionId).put(state, new TreeSet<String>());
88 }
89 result.get(partitionId).get(state).add(nodeName);
90 }
91 }
92 }
93 return result;
94 }
95
96
97
98
99 public List<ZNRecord> computeExternalView(
100 Map<String, List<ZNRecord>> currentStates, List<ZNRecord> idealStates)
101 {
102 List<ZNRecord> resultList = new ArrayList<ZNRecord>();
103 Map<String, ZNRecord> resultRoutingTable = new HashMap<String, ZNRecord>();
104
105
106
107
108
109 if (idealStates != null)
110 {
111 for (ZNRecord idealState : idealStates)
112 {
113 ZNRecord defaultExternalView = new ZNRecord(idealState.getId());
114 resultRoutingTable.put(idealState.getId(), defaultExternalView);
115 }
116 } else
117 {
118 assert (!currentStates.isEmpty());
119 return resultList;
120 }
121 for (String nodeName : currentStates.keySet())
122 {
123 List<ZNRecord> znStates = currentStates.get(nodeName);
124 for (ZNRecord nodeStateRecord : znStates)
125 {
126 Map<String, Map<String, String>> resourceStates = nodeStateRecord
127 .getMapFields();
128 for (String stateUnitKey : resourceStates.keySet())
129 {
130 Map<String, String> partitionStates = resourceStates.get(stateUnitKey);
131 String resourceName = partitionStates
132 .get(Message.Attributes.RESOURCE_NAME.toString());
133 ZNRecord partitionStatus = resultRoutingTable.get(resourceName);
134 if (partitionStatus == null)
135 {
136 partitionStatus = new ZNRecord(resourceName);
137 resultRoutingTable.put(resourceName, partitionStatus);
138 }
139 String currentStateKey = CurrentStateProperty.CURRENT_STATE.toString();
140
141 if (!partitionStatus.getMapFields().containsKey(stateUnitKey))
142 {
143 partitionStatus.setMapField(stateUnitKey,
144 new TreeMap<String, String>());
145 }
146 partitionStatus.getMapField(stateUnitKey).put(nodeName,
147 partitionStates.get(currentStateKey));
148
149 }
150 }
151 }
152 for (ZNRecord record : resultRoutingTable.values())
153 {
154 resultList.add(record);
155 }
156 return resultList;
157 }
158 }