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.HashSet;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.TreeMap;
27  
28  import org.apache.helix.HelixProperty;
29  import org.apache.helix.ZNRecord;
30  import org.apache.helix.model.Message.MessageType;
31  import org.apache.helix.model.builder.ConstraintItemBuilder;
32  import org.apache.log4j.Logger;
33  
34  
35  public class ClusterConstraints extends HelixProperty
36  {
37    private static Logger LOG = Logger.getLogger(ClusterConstraints.class);
38  
39    public enum ConstraintAttribute
40    {
41      STATE, MESSAGE_TYPE, TRANSITION, RESOURCE, INSTANCE, CONSTRAINT_VALUE
42    }
43  
44    public enum ConstraintValue
45    {
46      ANY
47    }
48  
49    public enum ConstraintType
50    {
51      STATE_CONSTRAINT, MESSAGE_CONSTRAINT
52    }
53  
54    // constraint-id -> constraint-item
55    private final Map<String, ConstraintItem> _constraints = new HashMap<String, ConstraintItem>();
56    
57    public ClusterConstraints(ConstraintType type) {
58      super(type.toString());
59    }
60    
61    public ClusterConstraints(ZNRecord record)
62    {
63      super(record);
64  
65      for (String constraintId : _record.getMapFields().keySet())
66      {
67        ConstraintItemBuilder builder = new ConstraintItemBuilder();
68        ConstraintItem item =  builder.addConstraintAttributes(_record.getMapField(constraintId)).build();
69        // ignore item with empty attributes or no constraint-value
70        if (item.getAttributes().size() > 0 && item.getConstraintValue() != null)
71        {
72          addConstraintItem(constraintId, item);
73        } else
74        {
75          LOG.error("Skip invalid constraint. key: " + constraintId + ", value: " 
76                + _record.getMapField(constraintId));
77        }
78      }
79    }
80  
81    /**
82     * add the constraint, overwrite existing one if constraint with same constraint-id already exists
83     * 
84     * @param constraintId
85     * @param item
86     */
87    public void addConstraintItem(String constraintId, ConstraintItem item) {
88      Map<String, String> map = new TreeMap<String, String>();
89      for (ConstraintAttribute attr : item.getAttributes().keySet()) {
90        map.put(attr.toString(), item.getAttributeValue(attr));
91      }
92      map.put(ConstraintAttribute.CONSTRAINT_VALUE.toString(), item.getConstraintValue());
93      _record.setMapField(constraintId, map);
94      _constraints.put(constraintId, item);
95    }
96    
97    public void addConstraintItems(Map<String, ConstraintItem> items) {
98      for (String constraintId : items.keySet()) {
99        addConstraintItem(constraintId, items.get(constraintId));
100     }
101   }
102   
103   /**
104    * remove a constraint-item
105    * 
106    * @param constraintId
107    */
108   public void removeConstraintItem(String constraintId) {
109     _constraints.remove(constraintId);
110     _record.getMapFields().remove(constraintId);
111   }
112   
113   /**
114    * get a constraint-item
115    * 
116    * @param constraintId
117    * @return
118    */
119   public ConstraintItem getConstraintItem(String constraintId) {
120     return _constraints.get(constraintId);
121   }
122   
123   /**
124    * return a set of constraints that match the attribute pairs
125    */
126   public Set<ConstraintItem> match(Map<ConstraintAttribute, String> attributes)
127   {
128     Set<ConstraintItem> matches = new HashSet<ConstraintItem>();
129     for (ConstraintItem item : _constraints.values())
130     {
131       if (item.match(attributes))
132       {
133         matches.add(item);
134       }
135     }
136     return matches;
137   }
138 
139   // convert a message to constraint attribute pairs
140   public static Map<ConstraintAttribute, String> toConstraintAttributes(Message msg)
141   {
142     Map<ConstraintAttribute, String> attributes = new TreeMap<ConstraintAttribute, String>();
143     String msgType = msg.getMsgType();
144     attributes.put(ConstraintAttribute.MESSAGE_TYPE, msgType);
145     if (MessageType.STATE_TRANSITION.toString().equals(msgType))
146     {
147       if (msg.getFromState() != null && msg.getToState() != null)
148       {
149         attributes.put(ConstraintAttribute.TRANSITION,
150             msg.getFromState() + "-" + msg.getToState());
151       }
152       if (msg.getResourceName() != null)
153       {
154         attributes.put(ConstraintAttribute.RESOURCE, msg.getResourceName());
155       }
156       if (msg.getTgtName() != null)
157       {
158         attributes.put(ConstraintAttribute.INSTANCE, msg.getTgtName());
159       }
160     }
161     return attributes;
162   }
163 
164   @Override
165   public boolean isValid()
166   {
167     // TODO Auto-generated method stub
168     return true;
169   }
170 
171 }