1 package org.apache.helix;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
86 _healthProvider.incrementPartitionStat(STAT_NAME, PARTITION_NAME);
87 transmitReport();
88
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
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
110 AssertJUnit.assertEquals(STORED_STAT, retrievedStat);
111
112
113 retrievedStat = _healthProvider.getPartitionStat(STAT_NAME, FAKE_PARTITION_NAME);
114 AssertJUnit.assertNull(retrievedStat);
115
116
117 retrievedStat = _healthProvider.getPartitionStat(FAKE_STAT_NAME, PARTITION_NAME);
118 AssertJUnit.assertNull(retrievedStat);
119
120
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
129 Map<String, Map<String, String>> resultMap = _healthProvider.getRecentPartitionHealthReport();
130 AssertJUnit.assertEquals(resultMap.size(), 0);
131
132
133 testSetGetPartitionStat();
134 resultMap = _healthProvider.getRecentPartitionHealthReport();
135
136 AssertJUnit.assertEquals(1, resultMap.size());
137
138 AssertJUnit.assertTrue(resultMap.keySet().contains(STAT_NAME));
139 Map<String, String> statMap = resultMap.get(STAT_NAME);
140
141 AssertJUnit.assertEquals(1, statMap.size());
142
143 AssertJUnit.assertTrue(statMap.keySet().contains(PARTITION_NAME));
144
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
154 String retrievedStat = _healthProvider.getPartitionStat(STAT_NAME, PARTITION_NAME);
155 AssertJUnit.assertEquals(1.0, Double.parseDouble(retrievedStat));
156
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 }