View Javadoc

1   package org.apache.helix.controller;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * ZKRoutingInfoProvider keeps a copy of the routing table. Given a partition id,
38   * it will return
39   *
40   * 1. The list of partition that can be read
41   * 2. the master partition, for write operation
42   *
43   * The routing table is constructed from the currentState of each storage nodes.
44   * The current state is a list of following pairs: partition-id:State(MASTER / SLAVE)
45   *
46   * TODO: move the code as part of router process
47   * TODO: add listeners to node current state changes
48   * */
49  public class ExternalViewGenerator
50  {
51    static Logger _logger = Logger.getLogger(ExternalViewGenerator.class);
52  
53    /*
54     * Given a list of external view ZNRecord nodes(one for each cluster),
55     * calculate the routing map.
56     *
57     * The format of the routing map is like this:
58     *
59     * Map<String, Map<String, Set<String>>> maps from a partitionName to its
60     * states Map<String, List<String>> The second Map maps from a state
61     * ("MASTER", "SLAVE"...) to a list of nodeNames
62     *
63     * So that the we can query the map for the list of nodes by providing the
64     * partition name and the expected state.
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     * The parameter is a map that maps the nodeName to a list of ZNRecords.
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     // maps from resourceName to another map : partition -> map <nodename,
105     // master/slave>;
106     // Fill the routing table with "empty" default state according to ideals
107     // states
108     // in the cluster
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 }