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.Arrays;
23 import java.util.Date;
24 import java.util.List;
25
26 import org.apache.helix.manager.zk.ZKHelixAdmin;
27 import org.apache.helix.model.ConfigScope;
28 import org.apache.helix.model.HelixConfigScope.ConfigScopeProperty;
29 import org.apache.helix.model.InstanceConfig;
30 import org.apache.helix.model.builder.ConfigScopeBuilder;
31 import org.testng.Assert;
32 import org.testng.annotations.Test;
33
34
35 public class TestConfigAccessor extends ZkUnitTestBase
36 {
37 @Test
38 public void testBasic() throws Exception
39 {
40 String className = TestHelper.getTestClassName();
41 String methodName = TestHelper.getTestMethodName();
42 String clusterName = className + "_" + methodName;
43
44 System.out.println("START " + clusterName + " at "
45 + new Date(System.currentTimeMillis()));
46
47 TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, "localhost", "TestDB", 1, 10, 5, 3,
48 "MasterSlave", true);
49
50 ConfigAccessor configAccessor = new ConfigAccessor(_gZkClient);
51 ConfigScope clusterScope = new ConfigScopeBuilder().forCluster(clusterName).build();
52
53
54 String clusterConfigValue = configAccessor.get(clusterScope, "clusterConfigKey");
55 Assert.assertNull(clusterConfigValue);
56
57 configAccessor.set(clusterScope, "clusterConfigKey", "clusterConfigValue");
58 clusterConfigValue = configAccessor.get(clusterScope, "clusterConfigKey");
59 Assert.assertEquals(clusterConfigValue, "clusterConfigValue");
60
61
62 ConfigScope resourceScope = new ConfigScopeBuilder().forCluster(clusterName)
63 .forResource("testResource").build();
64 configAccessor.set(resourceScope, "resourceConfigKey", "resourceConfigValue");
65 String resourceConfigValue = configAccessor.get(resourceScope, "resourceConfigKey");
66 Assert.assertEquals(resourceConfigValue, "resourceConfigValue");
67
68
69 ConfigScope partitionScope = new ConfigScopeBuilder().forCluster(clusterName)
70 .forResource("testResource").forPartition("testPartition").build();
71 configAccessor.set(partitionScope, "partitionConfigKey", "partitionConfigValue");
72 String partitionConfigValue = configAccessor.get(partitionScope, "partitionConfigKey");
73 Assert.assertEquals(partitionConfigValue, "partitionConfigValue");
74
75
76 ConfigScope participantScope = new ConfigScopeBuilder().forCluster(clusterName)
77 .forParticipant("localhost_12918").build();
78 configAccessor.set(participantScope, "participantConfigKey", "participantConfigValue");
79 String participantConfigValue = configAccessor.get(participantScope, "participantConfigKey");
80 Assert.assertEquals(participantConfigValue, "participantConfigValue");
81
82 List<String> keys = configAccessor.getKeys(ConfigScopeProperty.RESOURCE, clusterName);
83 Assert.assertEquals(keys.size(), 1, "should be [testResource]");
84 Assert.assertEquals(keys.get(0), "testResource");
85
86 keys = configAccessor.getKeys(ConfigScopeProperty.CLUSTER, clusterName);
87 Assert.assertEquals(keys.size(), 1, "should be [" + clusterName + "]");
88 Assert.assertEquals(keys.get(0), clusterName);
89
90 keys = configAccessor.getKeys(ConfigScopeProperty.PARTICIPANT, clusterName);
91 Assert.assertEquals(keys.size(), 5, "should be [localhost_12918~22] sorted");
92 Assert.assertEquals(keys.get(0), "localhost_12918");
93 Assert.assertEquals(keys.get(4), "localhost_12922");
94
95 keys = configAccessor.getKeys(ConfigScopeProperty.PARTITION, clusterName, "testResource");
96 Assert.assertEquals(keys.size(), 1, "should be [testPartition]");
97 Assert.assertEquals(keys.get(0), "testPartition");
98
99 keys = configAccessor.getKeys(ConfigScopeProperty.RESOURCE, clusterName, "testResource");
100 Assert.assertEquals(keys.size(), 1, "should be [resourceConfigKey]");
101 Assert.assertEquals(keys.get(0), "resourceConfigKey");
102
103 keys = configAccessor.getKeys(ConfigScopeProperty.CLUSTER, clusterName, clusterName);
104 Assert.assertEquals(keys.size(), 1, "should be [clusterConfigKey]");
105 Assert.assertEquals(keys.get(0), "clusterConfigKey");
106
107 keys = configAccessor.getKeys(ConfigScopeProperty.PARTICIPANT, clusterName, "localhost_12918");
108 System.out.println((keys));
109 Assert.assertEquals(keys.size(), 4, "should be [HELIX_ENABLED, HELIX_HOST, HELIX_PORT, participantConfigKey]");
110 Assert.assertEquals(keys.get(3), "participantConfigKey");
111
112 keys = configAccessor.getKeys(ConfigScopeProperty.PARTITION, clusterName, "testResource", "testPartition");
113 Assert.assertEquals(keys.size(), 1, "should be [partitionConfigKey]");
114 Assert.assertEquals(keys.get(0), "partitionConfigKey");
115
116
117 configAccessor.remove(clusterScope, "clusterConfigKey");
118 clusterConfigValue = configAccessor.get(clusterScope, "clusterConfigKey");
119 Assert.assertNull(clusterConfigValue, "Should be null since it's removed");
120
121 configAccessor.remove(resourceScope, "resourceConfigKey");
122 resourceConfigValue = configAccessor.get(resourceScope, "resourceConfigKey");
123 Assert.assertNull(resourceConfigValue, "Should be null since it's removed");
124
125 configAccessor.remove(partitionScope, "partitionConfigKey");
126 partitionConfigValue = configAccessor.get(partitionScope, "partitionConfigKey");
127 Assert.assertNull(partitionConfigValue, "Should be null since it's removed");
128
129 configAccessor.remove(participantScope, "participantConfigKey");
130 participantConfigValue = configAccessor.get(partitionScope, "participantConfigKey");
131 Assert.assertNull(participantConfigValue, "Should be null since it's removed");
132
133
134 try
135 {
136 new ConfigScopeBuilder().forPartition("testPartition").build();
137 Assert.fail("Should fail since cluster name is not set");
138 } catch (Exception e)
139 {
140
141 }
142
143 try
144 {
145 new ConfigScopeBuilder().forCluster("testCluster").forPartition("testPartition").build();
146 Assert.fail("Should fail since resource name is not set");
147 } catch (Exception e)
148 {
149
150 }
151
152 try
153 {
154 new ConfigScopeBuilder().forParticipant("testParticipant").build();
155 Assert.fail("Should fail since cluster name is not set");
156 } catch (Exception e)
157 {
158
159 }
160
161 System.out.println("END " + clusterName + " at "
162 + new Date(System.currentTimeMillis()));
163
164 }
165
166
167 @Test
168 public void testSetNonexistentParticipantConfig() throws Exception
169 {
170 String className = TestHelper.getTestClassName();
171 String methodName = TestHelper.getTestMethodName();
172 String clusterName = className + "_" + methodName;
173
174 System.out.println("START " + clusterName + " at "
175 + new Date(System.currentTimeMillis()));
176
177 ZKHelixAdmin admin = new ZKHelixAdmin(_gZkClient);
178 admin.addCluster(clusterName, true);
179 ConfigAccessor configAccessor = new ConfigAccessor(_gZkClient);
180 ConfigScope participantScope = new ConfigScopeBuilder().forCluster(clusterName)
181 .forParticipant("localhost_12918").build();
182
183 try {
184 configAccessor.set(participantScope, "participantConfigKey", "participantConfigValue");
185 Assert.fail("Except fail to set participant-config because participant: localhost_12918 is not added to cluster yet");
186 } catch (HelixException e) {
187
188 }
189 admin.addInstance(clusterName, new InstanceConfig("localhost_12918"));
190
191 try {
192 configAccessor.set(participantScope, "participantConfigKey", "participantConfigValue");
193 } catch (Exception e) {
194 Assert.fail("Except succeed to set participant-config because participant: localhost_12918 has been added to cluster");
195 }
196
197 String participantConfigValue = configAccessor.get(participantScope, "participantConfigKey");
198 Assert.assertEquals(participantConfigValue, "participantConfigValue");
199
200 System.out.println("END " + clusterName + " at "
201 + new Date(System.currentTimeMillis()));
202 }
203 }