1 package org.apache.helix.manager.zk;
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.AccessOption;
27 import org.apache.helix.PropertyPathConfig;
28 import org.apache.helix.PropertyType;
29 import org.apache.helix.TestHelper;
30 import org.apache.helix.ZNRecord;
31 import org.apache.helix.ZNRecordUpdater;
32 import org.apache.helix.ZkUnitTestBase;
33 import org.apache.helix.manager.zk.ZkBaseDataAccessor;
34 import org.apache.helix.manager.zk.ZkCacheBaseDataAccessor;
35 import org.testng.Assert;
36 import org.testng.annotations.Test;
37
38
39 public class TestWtCacheSyncOpSingleThread extends ZkUnitTestBase
40 {
41
42
43 @Test
44 public void testHappyPathZkCacheBaseDataAccessor() throws Exception
45 {
46 String className = TestHelper.getTestClassName();
47 String methodName = TestHelper.getTestMethodName();
48 String clusterName = className + "_" + methodName;
49 System.out.println("START " + clusterName + " at "
50 + new Date(System.currentTimeMillis()));
51
52
53 String curStatePath =
54 PropertyPathConfig.getPath(PropertyType.CURRENTSTATES,
55 clusterName,
56 "localhost_8901");
57 String extViewPath =
58 PropertyPathConfig.getPath(PropertyType.EXTERNALVIEW, clusterName);
59
60 ZkBaseDataAccessor<ZNRecord> baseAccessor =
61 new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
62
63 baseAccessor.create(curStatePath, null, AccessOption.PERSISTENT);
64
65 List<String> cachePaths = Arrays.asList(curStatePath, extViewPath);
66 ZkCacheBaseDataAccessor<ZNRecord> accessor =
67 new ZkCacheBaseDataAccessor<ZNRecord>(baseAccessor,
68 null,
69 cachePaths,
70 null);
71
72 boolean ret = TestHelper.verifyZkCache(cachePaths, accessor._wtCache._cache, _gZkClient, true);
73 Assert.assertTrue(ret, "wtCache doesn't match data on Zk");
74
75
76
77 for (int i = 0; i < 10; i++)
78 {
79 String path = curStatePath + "/session_0/TestDB" + i;
80 boolean success =
81 accessor.create(path, new ZNRecord("TestDB" + i), AccessOption.PERSISTENT);
82 Assert.assertTrue(success, "Should succeed in create: " + path);
83 }
84
85
86
87 ret = TestHelper.verifyZkCache(cachePaths, accessor._wtCache._cache, _gZkClient, false);
88 Assert.assertTrue(ret, "wtCache doesn't match data on Zk");
89
90
91 for (int i = 0; i < 10; i++)
92 {
93 String path = curStatePath + "/session_0/TestDB" + i;
94 for (int j = 0; j < 10; j++)
95 {
96 ZNRecord newRecord = new ZNRecord("TestDB" + i);
97 newRecord.setSimpleField("" + j, "" + j);
98 boolean success =
99 accessor.update(path, new ZNRecordUpdater(newRecord), AccessOption.PERSISTENT);
100 Assert.assertTrue(success, "Should succeed in update: " + path);
101
102 }
103 }
104
105
106
107 ret = TestHelper.verifyZkCache(cachePaths, accessor._wtCache._cache, _gZkClient, false);
108 Assert.assertTrue(ret, "wtCache doesn't match data on Zk");
109
110
111 for (int i = 0; i < 10; i++)
112 {
113 String path =
114 PropertyPathConfig.getPath(PropertyType.EXTERNALVIEW, clusterName, "TestDB" + i);
115 boolean success = accessor.set(path, new ZNRecord("TestDB" + i), AccessOption.PERSISTENT);
116 Assert.assertTrue(success, "Should succeed in set: " + path);
117 }
118
119
120
121 ret = TestHelper.verifyZkCache(cachePaths, accessor._wtCache._cache, _gZkClient, false);
122 Assert.assertTrue(ret, "wtCache doesn't match data on Zk");
123
124
125
126 for (int i = 0; i < 10; i++)
127 {
128 String path =
129 PropertyPathConfig.getPath(PropertyType.EXTERNALVIEW, clusterName, "TestDB" + i);
130 ZNRecord record = accessor.get(path, null, 0);
131 Assert.assertEquals(record.getId(), "TestDB" + i);
132 }
133
134
135 List<String> childNames = accessor.getChildNames(extViewPath, 0);
136
137 Assert.assertEquals(childNames.size(), 10, "Should contain only: TestDB0-9");
138 for (int i = 0; i < 10; i++)
139 {
140 Assert.assertTrue(childNames.contains("TestDB" + i));
141 }
142
143
144 for (int i = 0; i < 10; i++)
145 {
146 String path =
147 PropertyPathConfig.getPath(PropertyType.CURRENTSTATES,
148 clusterName,
149 "localhost_8901",
150 "session_0",
151 "TestDB" + i);
152
153 Assert.assertTrue(accessor.exists(path, 0));
154 }
155
156 System.out.println("END " + clusterName + " at "
157 + new Date(System.currentTimeMillis()));
158 }
159
160 @Test
161 public void testCreateFailZkCacheBaseDataAccessor()
162 {
163 String className = TestHelper.getTestClassName();
164 String methodName = TestHelper.getTestMethodName();
165 String clusterName = className + "_" + methodName;
166 System.out.println("START " + clusterName + " at "
167 + new Date(System.currentTimeMillis()));
168
169
170 String curStatePath =
171 PropertyPathConfig.getPath(PropertyType.CURRENTSTATES,
172 clusterName,
173 "localhost_8901");
174
175 ZkBaseDataAccessor<ZNRecord> baseAccessor =
176 new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
177
178 ZkCacheBaseDataAccessor<ZNRecord> accessor =
179 new ZkCacheBaseDataAccessor<ZNRecord>(baseAccessor,
180 null,
181 Arrays.asList(curStatePath),
182 null);
183
184
185 for (int i = 0; i < 10; i++)
186 {
187 String path =
188 PropertyPathConfig.getPath(PropertyType.CURRENTSTATES,
189 clusterName,
190 "localhost_8901",
191 "session_1",
192 "TestDB" + i);
193 boolean success =
194 accessor.create(path, new ZNRecord("TestDB" + i), AccessOption.PERSISTENT);
195 Assert.assertTrue(success, "Should succeed in create: " + path);
196 }
197
198
199 for (int i = 0; i < 10; i++)
200 {
201 String path =
202 PropertyPathConfig.getPath(PropertyType.CURRENTSTATES,
203 clusterName,
204 "localhost_8901",
205 "session_1",
206 "TestDB" + i);
207 boolean success =
208 accessor.create(path, new ZNRecord("TestDB" + i), AccessOption.PERSISTENT);
209 Assert.assertFalse(success, "Should fail in create due to NodeExists: " + path);
210 }
211
212 System.out.println("END " + clusterName + " at "
213 + new Date(System.currentTimeMillis()));
214 }
215 }