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.ArrayList;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.apache.helix.HelixProperty;
30  import org.apache.helix.ZNRecord;
31  import org.apache.log4j.Logger;
32  
33  
34  /**
35   * Instance configurations
36   */
37  public class InstanceConfig extends HelixProperty
38  {
39    public enum InstanceConfigProperty
40    {
41      HELIX_HOST,
42      HELIX_PORT,
43      HELIX_ENABLED,
44      HELIX_DISABLED_PARTITION,
45      TAG_LIST
46    }
47    private static final Logger _logger = Logger.getLogger(InstanceConfig.class.getName());
48  
49    public InstanceConfig(String instanceId)
50    {
51      super(instanceId);
52    }
53  
54    public InstanceConfig(ZNRecord record)
55    {
56      super(record);
57    }
58  
59    public String getHostName()
60    {
61      return _record.getSimpleField(InstanceConfigProperty.HELIX_HOST.toString());
62    }
63  
64    public void setHostName(String hostName)
65    {
66      _record.setSimpleField(InstanceConfigProperty.HELIX_HOST.toString(), hostName);
67    }
68  
69    public String getPort()
70    {
71      return _record.getSimpleField(InstanceConfigProperty.HELIX_PORT.toString());
72    }
73  
74    public void setPort(String port)
75    {
76      _record.setSimpleField(InstanceConfigProperty.HELIX_PORT.toString(), port);
77    }
78    
79    public List<String> getTags()
80    {
81      List<String> tags = getRecord().getListField(InstanceConfigProperty.TAG_LIST.toString());
82      if(tags == null)
83      {
84        tags = new ArrayList<String>(0);
85      }
86      return tags;
87    }
88    
89    public void addTag(String tag)
90    {
91      List<String> tags = getRecord().getListField(InstanceConfigProperty.TAG_LIST.toString());
92      if(tags == null)
93      {
94        tags = new ArrayList<String>(0);
95      }
96      if(!tags.contains(tag))
97      {
98        tags.add(tag);
99      }
100     getRecord().setListField(InstanceConfigProperty.TAG_LIST.toString(), tags);
101   }
102   
103   public void removeTag(String tag)
104   {
105     List<String> tags = getRecord().getListField(InstanceConfigProperty.TAG_LIST.toString());
106     if(tags == null)
107     {
108       return;
109     }
110     if(tags.contains(tag))
111     {
112       tags.remove(tag);
113     }
114   }
115   
116   public boolean containsTag(String tag)
117   {
118     List<String> tags = getRecord().getListField(InstanceConfigProperty.TAG_LIST.toString());
119     if(tags == null)
120     {
121       return false;
122     }
123     return tags.contains(tag);
124   }
125 
126   public boolean getInstanceEnabled()
127   {
128     String isEnabled = _record.getSimpleField(InstanceConfigProperty.HELIX_ENABLED.toString());
129     return isEnabled == null || Boolean.parseBoolean(isEnabled);
130   }
131 
132   public void setInstanceEnabled(boolean enabled)
133   {
134     _record.setSimpleField(InstanceConfigProperty.HELIX_ENABLED.toString(), Boolean.toString(enabled));
135   }
136 
137 
138   public boolean getInstanceEnabledForPartition(String partition)
139   {
140     // Map<String, String> disabledPartitionMap = _record.getMapField(InstanceConfigProperty.HELIX_DISABLED_PARTITION.toString());
141     List<String> disabledPartitions = _record.getListField(InstanceConfigProperty.HELIX_DISABLED_PARTITION.toString());
142     if (disabledPartitions != null && disabledPartitions.contains(partition))
143     {
144       return false;
145     }
146     else
147     {
148       return true;
149     }
150   }
151 
152   public List<String> getDisabledPartitions()
153   {
154     return _record.getListField(InstanceConfigProperty.HELIX_DISABLED_PARTITION.toString());
155 
156   }
157 
158   public void setInstanceEnabledForPartition(String partitionName, boolean enabled)
159   {
160     List<String> list =
161         _record.getListField(InstanceConfigProperty.HELIX_DISABLED_PARTITION.toString());
162     Set<String> disabledPartitions = new HashSet<String>();
163     if (list != null)
164     {
165       disabledPartitions.addAll(list);
166     }
167 
168     if (enabled)
169     {
170       disabledPartitions.remove(partitionName);
171     }
172     else
173     {
174       disabledPartitions.add(partitionName);
175     }
176 
177     list = new ArrayList<String>(disabledPartitions);
178     Collections.sort(list);
179     _record.setListField(InstanceConfigProperty.HELIX_DISABLED_PARTITION.toString(),
180                              list);
181   }
182 
183   @Override
184   public boolean equals(Object obj)
185   {
186     if (obj instanceof InstanceConfig)
187     {
188       InstanceConfig that = (InstanceConfig) obj;
189 
190       if (this.getId().equals(that.getId()))
191       {
192         return true;
193       }
194     }
195     return false;
196   }
197 
198   @Override
199   public int hashCode()
200   {
201     return getId().hashCode();
202   }
203 
204   public String getInstanceName()
205   {
206     return _record.getId();
207   }
208 
209   @Override
210   public boolean isValid()
211   {
212     // HELIX-65: remove check for hostname/port existence
213     return true;
214   }
215 }