View Javadoc

1   package org.apache.helix.model;
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.HashMap;
23  import java.util.Map;
24  import java.util.TreeMap;
25  
26  import org.apache.helix.HelixProperty;
27  import org.apache.helix.ZNRecord;
28  import org.apache.log4j.Logger;
29  
30  
31  /**
32   * Current states of partitions in a resource
33   */
34  public class CurrentState extends HelixProperty
35  {
36    private static Logger LOG = Logger.getLogger(CurrentState.class);
37  
38    public enum CurrentStateProperty
39    {
40      SESSION_ID, CURRENT_STATE, STATE_MODEL_DEF, STATE_MODEL_FACTORY_NAME, RESOURCE // ,
41                                                                                     // BUCKET_SIZE
42    }
43  
44    public CurrentState(String resourceName)
45    {
46      super(resourceName);
47    }
48  
49    public CurrentState(ZNRecord record)
50    {
51      super(record);
52    }
53  
54    public String getResourceName()
55    {
56      return _record.getId();
57    }
58  
59    public Map<String, String> getPartitionStateMap()
60    {
61      Map<String, String> map = new HashMap<String, String>();
62      Map<String, Map<String, String>> mapFields = _record.getMapFields();
63      for (String partitionName : mapFields.keySet())
64      {
65        Map<String, String> tempMap = mapFields.get(partitionName);
66        if (tempMap != null)
67        {
68          map.put(partitionName, tempMap.get(CurrentStateProperty.CURRENT_STATE.toString()));
69        }
70      }
71      return map;
72    }
73  
74    public String getSessionId()
75    {
76      return _record.getSimpleField(CurrentStateProperty.SESSION_ID.toString());
77    }
78  
79    public void setSessionId(String sessionId)
80    {
81      _record.setSimpleField(CurrentStateProperty.SESSION_ID.toString(), sessionId);
82    }
83  
84    public String getState(String partitionName)
85    {
86      Map<String, Map<String, String>> mapFields = _record.getMapFields();
87      Map<String, String> mapField = mapFields.get(partitionName);
88      if (mapField != null)
89      {
90        return mapField.get(CurrentStateProperty.CURRENT_STATE.toString());
91      }
92      return null;
93    }
94  
95    public void setStateModelDefRef(String stateModelName)
96    {
97      _record.setSimpleField(CurrentStateProperty.STATE_MODEL_DEF.toString(),
98                             stateModelName);
99    }
100 
101   public String getStateModelDefRef()
102   {
103     return _record.getSimpleField(CurrentStateProperty.STATE_MODEL_DEF.toString());
104   }
105 
106   public void setState(String partitionName, String state)
107   {
108     Map<String, Map<String, String>> mapFields = _record.getMapFields();
109     if (mapFields.get(partitionName) == null)
110     {
111       mapFields.put(partitionName, new TreeMap<String, String>());
112     }
113     mapFields.get(partitionName)
114              .put(CurrentStateProperty.CURRENT_STATE.toString(), state);
115   }
116 
117   public void setStateModelFactoryName(String factoryName)
118   {
119     _record.setSimpleField(CurrentStateProperty.STATE_MODEL_FACTORY_NAME.toString(),
120                            factoryName);
121   }
122 
123   public String getStateModelFactoryName()
124   {
125     return _record.getSimpleField(CurrentStateProperty.STATE_MODEL_FACTORY_NAME.toString());
126   }
127 
128 //  @Override
129 //  public int getBucketSize()
130 //  {
131 //    String bucketSizeStr =
132 //        _record.getSimpleField(CurrentStateProperty.BUCKET_SIZE.toString());
133 //    int bucketSize = 0;
134 //    if (bucketSizeStr != null)
135 //    {
136 //      try
137 //      {
138 //        bucketSize = Integer.parseInt(bucketSizeStr);
139 //      }
140 //      catch (NumberFormatException e)
141 //      {
142 //        // OK
143 //      }
144 //    }
145 //    return bucketSize;
146 //  }
147 //
148 //  @Override
149 //  public void setBucketSize(int bucketSize)
150 //  {
151 //    if (bucketSize > 0)
152 //    {
153 //      _record.setSimpleField(CurrentStateProperty.BUCKET_SIZE.toString(), "" + bucketSize);
154 //    }
155 //  }
156 
157   @Override
158   public boolean isValid()
159   {
160     if (getStateModelDefRef() == null)
161     {
162       LOG.error("Current state does not contain state model ref. id:" + getResourceName());
163       return false;
164     }
165     if (getSessionId() == null)
166     {
167       LOG.error("CurrentState does not contain session id, id : " + getResourceName());
168       return false;
169     }
170     return true;
171   }
172 
173 }