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.Arrays;
23  import java.util.List;
24  
25  import org.apache.helix.Mocks;
26  import org.apache.helix.ZNRecord;
27  import org.apache.helix.PropertyKey.Builder;
28  import org.apache.helix.controller.pipeline.StageContext;
29  import org.apache.helix.controller.stages.CompatibilityCheckStage;
30  import org.apache.helix.controller.stages.ReadClusterDataStage;
31  import org.apache.helix.model.IdealState;
32  import org.apache.helix.model.LiveInstance;
33  import org.apache.helix.model.LiveInstance.LiveInstanceProperty;
34  import org.apache.helix.tools.DefaultIdealStateCalculator;
35  import org.testng.Assert;
36  import org.testng.annotations.Test;
37  
38  
39  public class TestCompatibilityCheckStage extends BaseStageTest
40  {
41    private void prepare(String controllerVersion, String participantVersion)
42    {
43      prepare(controllerVersion, participantVersion, null);
44    }
45    
46    private void prepare(String controllerVersion, String participantVersion, 
47        String minSupportedParticipantVersion)
48    {
49      List<String> instances = Arrays.asList("localhost_0", "localhost_1",
50                                             "localhost_2", "localhost_3", "localhost_4");
51      int partitions = 10;
52      int replicas = 1;
53  
54      // set ideal state
55      String resourceName = "testResource";
56      ZNRecord record = DefaultIdealStateCalculator.calculateIdealState(
57          instances, partitions, replicas, resourceName, "MASTER", "SLAVE");
58      IdealState idealState = new IdealState(record);
59      idealState.setStateModelDefRef("MasterSlave");
60      
61      Builder keyBuilder = accessor.keyBuilder();
62      accessor.setProperty(keyBuilder.idealStates(resourceName), idealState);
63  
64      // set live instances
65      record = new ZNRecord("localhost_0");
66      if (participantVersion != null)
67      {
68        record.setSimpleField(LiveInstanceProperty.HELIX_VERSION.toString(), participantVersion);
69      }
70      LiveInstance liveInstance = new LiveInstance(record);
71      liveInstance.setSessionId("session_0");
72      accessor.setProperty(keyBuilder.liveInstance("localhost_0"), liveInstance);
73  
74      if (controllerVersion != null)
75      {
76        ((Mocks.MockManager)manager).setVersion(controllerVersion);
77      }
78      
79      if (minSupportedParticipantVersion != null) {
80        manager.getProperties().getProperties()
81                        .put("minimum_supported_version.participant", 
82                            minSupportedParticipantVersion);
83      }
84      event.addAttribute("helixmanager", manager);
85      runStage(event, new ReadClusterDataStage());
86    }
87  
88    @Test
89    public void testCompatible()
90    {
91      prepare("0.4.0", "0.4.0");
92      CompatibilityCheckStage stage = new CompatibilityCheckStage();
93      StageContext context = new StageContext();
94      stage.init(context);
95      stage.preProcess();
96      try
97      {
98        stage.process(event);
99      }
100     catch (Exception e)
101     {
102       Assert.fail("Should not fail since versions are compatible", e);
103     }
104     stage.postProcess();
105   }
106 
107   @Test
108   public void testNullParticipantVersion()
109   {
110     prepare("0.4.0", null);
111     CompatibilityCheckStage stage = new CompatibilityCheckStage();
112     StageContext context = new StageContext();
113     stage.init(context);
114     stage.preProcess();
115     try
116     {
117       stage.process(event);
118     }
119     catch (Exception e)
120     {
121       Assert.fail("Should not fail since compatibility check will be skipped if participant version is null");
122     }
123     stage.postProcess();
124   }
125 
126   @Test
127   public void testNullControllerVersion()
128   {
129     prepare(null, "0.4.0");
130     CompatibilityCheckStage stage = new CompatibilityCheckStage();
131     StageContext context = new StageContext();
132     stage.init(context);
133     stage.preProcess();
134     try
135     {
136       stage.process(event);
137     }
138     catch (Exception e)
139     {
140       Assert.fail("Should not fail since compatibility check will be skipped if controller version is null");
141     }
142     stage.postProcess();
143   }
144 
145   @Test
146   public void testIncompatible()
147   {
148     prepare("0.6.1-incubating-SNAPSHOT", "0.3.4", "0.4");
149     CompatibilityCheckStage stage = new CompatibilityCheckStage();
150     StageContext context = new StageContext();
151     stage.init(context);
152     stage.preProcess();
153     try
154     {
155       stage.process(event);
156       Assert.fail("Should fail since participant version is less than the minimum participant version supported by controller");
157     }
158     catch (Exception e)
159     {
160       // OK
161     }
162     stage.postProcess();
163   }
164 
165 }