View Javadoc

1   package org.apache.helix;
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.Map;
23  
24  import org.apache.helix.HelixDataAccessor;
25  import org.apache.helix.ZNRecord;
26  import org.apache.helix.Mocks.MockManager;
27  import org.apache.helix.PropertyKey.Builder;
28  import org.apache.helix.healthcheck.PerformanceHealthReportProvider;
29  import org.apache.helix.model.HealthStat;
30  import org.testng.AssertJUnit;
31  import org.testng.annotations.BeforeMethod;
32  import org.testng.annotations.Test;
33  
34  
35  public class TestPerformanceHealthReportProvider {
36  
37  	 protected static final String CLUSTER_NAME = "TestCluster";
38  	 protected final String STAT_NAME = "Stat_123";
39  	 protected final String PARTITION_NAME = "Partition_456";
40  	 protected final String FAKE_STAT_NAME = "Stat_ABC";
41  	 protected final String FAKE_PARTITION_NAME = "Partition_DEF";
42  	 protected final String STORED_STAT = "789";
43  	 protected final String INSTANCE_NAME = "instance:1";
44  
45  	PerformanceHealthReportProvider _healthProvider;
46  	MockManager _helixManager;
47  
48  	public void incrementPartitionStat() throws Exception
49  	{
50  		_helixManager = new MockManager(CLUSTER_NAME);
51  		_healthProvider.incrementPartitionStat(STAT_NAME, PARTITION_NAME);
52  	}
53  
54  	public void transmitReport() throws Exception
55  	{
56  		_helixManager = new MockManager(CLUSTER_NAME);
57  		 Map<String, Map<String, String>> partitionReport = _healthProvider
58  	                .getRecentPartitionHealthReport();
59  		 ZNRecord record = new ZNRecord(_healthProvider.getReportName());
60  		 if (partitionReport != null) {
61           	record.setMapFields(partitionReport);
62           }
63  		 HelixDataAccessor accessor = _helixManager.getHelixDataAccessor();
64  
65  		 Builder keyBuilder = accessor.keyBuilder();
66  		 accessor.setProperty(keyBuilder.healthReport(INSTANCE_NAME, record.getId()), new HealthStat(record));
67  	}
68  
69  	@BeforeMethod ()
70  	public void setup()
71  	{
72  		_healthProvider = new PerformanceHealthReportProvider();
73  	}
74  
75  	 @Test ()
76  	  public void testGetRecentHealthReports() throws Exception
77  	  {
78  		 _healthProvider.getRecentHealthReport();
79  		 _healthProvider.getRecentPartitionHealthReport();
80  	  }
81  
82  	 @Test ()
83  	 public void testIncrementPartitionStat() throws Exception
84  	 {
85  		 //stat does not exist yet
86  		 _healthProvider.incrementPartitionStat(STAT_NAME, PARTITION_NAME);
87  		 transmitReport();
88  		 //stat does exist
89  		 _healthProvider.incrementPartitionStat(STAT_NAME, PARTITION_NAME);
90  		 transmitReport();
91  		 String retrievedStat = _healthProvider.getPartitionStat(STAT_NAME, PARTITION_NAME);
92  		 AssertJUnit.assertEquals(2.0, Double.parseDouble(retrievedStat));
93  
94  		 //set to some other value
95  		 _healthProvider.submitPartitionStat(STAT_NAME, PARTITION_NAME, STORED_STAT);
96  		 transmitReport();
97  		 _healthProvider.incrementPartitionStat(STAT_NAME, PARTITION_NAME);
98  		 transmitReport();
99  		 retrievedStat = _healthProvider.getPartitionStat(STAT_NAME, PARTITION_NAME);
100 		 AssertJUnit.assertEquals(Double.parseDouble(retrievedStat), Double.parseDouble(STORED_STAT)+1);
101 	 }
102 
103 	 @Test ()
104 	 public void testSetGetPartitionStat() throws Exception
105 	 {
106 		 _healthProvider.submitPartitionStat(STAT_NAME, PARTITION_NAME, STORED_STAT);
107 		 transmitReport();
108 		 String retrievedStat = _healthProvider.getPartitionStat(STAT_NAME, PARTITION_NAME);
109 		 //check on correct retrieval for real stat, real partition
110 		 AssertJUnit.assertEquals(STORED_STAT, retrievedStat);
111 
112 		 //real stat, fake partition
113 		 retrievedStat = _healthProvider.getPartitionStat(STAT_NAME, FAKE_PARTITION_NAME);
114 		 AssertJUnit.assertNull(retrievedStat);
115 
116 		 //fake stat, real partition
117 		 retrievedStat = _healthProvider.getPartitionStat(FAKE_STAT_NAME, PARTITION_NAME);
118 		 AssertJUnit.assertNull(retrievedStat);
119 
120 		 //fake stat, fake partition
121 		 retrievedStat = _healthProvider.getPartitionStat(FAKE_STAT_NAME, FAKE_PARTITION_NAME);
122 		 AssertJUnit.assertNull(retrievedStat);
123 	 }
124 
125 	 @Test ()
126 	 public void testGetPartitionHealthReport() throws Exception
127 	 {
128 		 //test empty map case
129 		 Map<String, Map<String, String>> resultMap = _healthProvider.getRecentPartitionHealthReport();
130 		 AssertJUnit.assertEquals(resultMap.size(), 0);
131 
132 		 //test non-empty case
133 		 testSetGetPartitionStat();
134 		 resultMap = _healthProvider.getRecentPartitionHealthReport();
135 		 //check contains 1 stat
136 		 AssertJUnit.assertEquals(1, resultMap.size());
137 		 //check contains STAT_NAME STAT
138 		 AssertJUnit.assertTrue(resultMap.keySet().contains(STAT_NAME));
139 		 Map<String, String> statMap = resultMap.get(STAT_NAME);
140 		 //check statMap has size 1
141 		 AssertJUnit.assertEquals(1, statMap.size());
142 		 //check contains PARTITION_NAME
143 		 AssertJUnit.assertTrue(statMap.keySet().contains(PARTITION_NAME));
144 		 //check stored val
145 		 String statVal = statMap.get(PARTITION_NAME);
146 		 AssertJUnit.assertEquals(statVal, STORED_STAT);
147 	 }
148 
149 	 @Test ()
150 	 public void testPartitionStatReset() throws Exception
151 	 {
152 		 incrementPartitionStat();
153 		 //ensure stat appears
154 		 String retrievedStat = _healthProvider.getPartitionStat(STAT_NAME, PARTITION_NAME);
155 		 AssertJUnit.assertEquals(1.0, Double.parseDouble(retrievedStat));
156 		 //reset partition stats
157 		 _healthProvider.resetStats();
158 		 transmitReport();
159 		 retrievedStat = _healthProvider.getPartitionStat(STAT_NAME, PARTITION_NAME);
160 		 AssertJUnit.assertEquals(null, retrievedStat);
161 	 }
162 
163 	 @Test ()
164 	  public void testGetReportName() throws Exception
165 	  {
166 		 _healthProvider.getReportName();
167 	  }
168 }