View Javadoc

1   package org.apache.helix.manager.zk;
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.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    // TODO: add TestZkCacheSyncOpSingleThread
42    // TODO: add TestZkCacheAsyncOpMultiThread
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      // init zkCacheDataAccessor
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      // create 10 current states
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      // verify wtCache
86      // TestHelper.printCache(accessor._wtCache);
87      ret = TestHelper.verifyZkCache(cachePaths, accessor._wtCache._cache, _gZkClient, false);
88      Assert.assertTrue(ret, "wtCache doesn't match data on Zk");
89  
90      // update each current state 10 times, single thread
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     // verify cache
106 //    TestHelper.printCache(accessor._wtCache._cache);
107     ret = TestHelper.verifyZkCache(cachePaths, accessor._wtCache._cache, _gZkClient, false);
108     Assert.assertTrue(ret, "wtCache doesn't match data on Zk");
109 
110     // set 10 external views
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     // verify wtCache
120     // accessor.printWtCache();
121     ret = TestHelper.verifyZkCache(cachePaths, accessor._wtCache._cache, _gZkClient, false);
122     Assert.assertTrue(ret, "wtCache doesn't match data on Zk");
123 
124 
125     // get 10 external views
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     // getChildNames
135     List<String> childNames = accessor.getChildNames(extViewPath, 0);
136     // System.out.println(childNames);
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     // exists
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     // init zkCacheDataAccessor
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     // create 10 current states
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     // create same 10 current states again, should fail
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 }