View Javadoc

1   package org.apache.helix.controller.stages;
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.Collections;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.helix.model.CurrentState;
27  import org.apache.helix.model.Partition;
28  
29  
30  public class CurrentStateOutput
31  {
32    private final Map<String, Map<Partition, Map<String, String>>> _currentStateMap;
33    private final Map<String, Map<Partition, Map<String, String>>> _pendingStateMap;
34    private final Map<String, String>                              _resourceStateModelMap;
35    private final Map<String, CurrentState>                        _curStateMetaMap;
36  
37    public CurrentStateOutput()
38    {
39      _currentStateMap = new HashMap<String, Map<Partition, Map<String, String>>>();
40      _pendingStateMap = new HashMap<String, Map<Partition, Map<String, String>>>();
41      _resourceStateModelMap = new HashMap<String, String>();
42      _curStateMetaMap = new HashMap<String, CurrentState>();
43  
44    }
45  
46    public void setResourceStateModelDef(String resourceName, String stateModelDefName)
47    {
48      _resourceStateModelMap.put(resourceName, stateModelDefName);
49    }
50  
51    public String getResourceStateModelDef(String resourceName)
52    {
53      return _resourceStateModelMap.get(resourceName);
54    }
55  
56    public void setBucketSize(String resource, int bucketSize)
57    {
58      CurrentState curStateMeta = _curStateMetaMap.get(resource);
59      if (curStateMeta == null)
60      {
61        curStateMeta = new CurrentState(resource);
62        _curStateMetaMap.put(resource, curStateMeta);
63      }
64      curStateMeta.setBucketSize(bucketSize);
65    }
66    
67    public int getBucketSize(String resource)
68    {
69      int bucketSize = 0;
70      CurrentState curStateMeta = _curStateMetaMap.get(resource);
71      if (curStateMeta != null)
72      {
73        bucketSize = curStateMeta.getBucketSize();  
74      }
75      
76      return bucketSize;
77    }
78    
79    public void setCurrentState(String resourceName,
80                                Partition partition,
81                                String instanceName,
82                                String state)
83    {
84      if (!_currentStateMap.containsKey(resourceName))
85      {
86        _currentStateMap.put(resourceName, new HashMap<Partition, Map<String, String>>());
87      }
88      if (!_currentStateMap.get(resourceName).containsKey(partition))
89      {
90        _currentStateMap.get(resourceName).put(partition, new HashMap<String, String>());
91      }
92      _currentStateMap.get(resourceName).get(partition).put(instanceName, state);
93    }
94  
95    public void setPendingState(String resourceName,
96                                Partition partition,
97                                String instanceName,
98                                String state)
99    {
100     if (!_pendingStateMap.containsKey(resourceName))
101     {
102       _pendingStateMap.put(resourceName, new HashMap<Partition, Map<String, String>>());
103     }
104     if (!_pendingStateMap.get(resourceName).containsKey(partition))
105     {
106       _pendingStateMap.get(resourceName).put(partition, new HashMap<String, String>());
107     }
108     _pendingStateMap.get(resourceName).get(partition).put(instanceName, state);
109   }
110 
111   /**
112    * given (resource, partition, instance), returns currentState
113    * 
114    * @param resourceName
115    * @param partition
116    * @param instanceName
117    * @return
118    */
119   public String getCurrentState(String resourceName,
120                                 Partition partition,
121                                 String instanceName)
122   {
123     Map<Partition, Map<String, String>> map = _currentStateMap.get(resourceName);
124     if (map != null)
125     {
126       Map<String, String> instanceStateMap = map.get(partition);
127       if (instanceStateMap != null)
128       {
129         return instanceStateMap.get(instanceName);
130       }
131     }
132     return null;
133   }
134 
135   /**
136    * given (resource, partition, instance), returns toState
137    * 
138    * @param resourceName
139    * @param partition
140    * @param instanceName
141    * @return
142    */
143   public String getPendingState(String resourceName,
144                                 Partition partition,
145                                 String instanceName)
146   {
147     Map<Partition, Map<String, String>> map = _pendingStateMap.get(resourceName);
148     if (map != null)
149     {
150       Map<String, String> instanceStateMap = map.get(partition);
151       if (instanceStateMap != null)
152       {
153         return instanceStateMap.get(instanceName);
154       }
155     }
156     return null;
157   }
158 
159   /**
160    * given (resource, partition), returns (instance->currentState) map
161    * 
162    * @param resourceName
163    * @param partition
164    * @return
165    */
166   public Map<String, String> getCurrentStateMap(String resourceName, Partition partition)
167   {
168     if (_currentStateMap.containsKey(resourceName))
169     {
170       Map<Partition, Map<String, String>> map = _currentStateMap.get(resourceName);
171       if (map.containsKey(partition))
172       {
173         return map.get(partition);
174       }
175     }
176     return Collections.emptyMap();
177   }
178 
179   /**
180    * given (resource, partition), returns (instance->toState) map
181    * 
182    * @param resourceName
183    * @param partition
184    * @return
185    */
186   public Map<String, String> getPendingStateMap(String resourceName, Partition partition)
187   {
188     if (_pendingStateMap.containsKey(resourceName))
189     {
190       Map<Partition, Map<String, String>> map = _pendingStateMap.get(resourceName);
191       if (map.containsKey(partition))
192       {
193         return map.get(partition);
194       }
195     }
196     return Collections.emptyMap();
197   }
198 
199   @Override
200   public String toString()
201   {
202     StringBuilder sb = new StringBuilder();
203     sb.append("current state= ").append(_currentStateMap);
204     sb.append(", pending state= ").append(_pendingStateMap);
205     return sb.toString();
206 
207   }
208 
209 }