keith-turner commented on code in PR #5765:
URL: https://github.com/apache/accumulo/pull/5765#discussion_r2237403667


##########
test/src/main/java/org/apache/accumulo/test/zookeeper/ZooCacheIT.java:
##########
@@ -228,4 +238,105 @@ public void testGetChildren() throws Exception {
           && zooCache.childrenCached(base + "/test3") == false;
     });
   }
+
+  @Test
+  public void testZookeeperRestart() throws Exception {
+    final String root = Constants.ZROOT + UUID.randomUUID();
+    final String base = root + Constants.ZTSERVERS;
+    TestZooCache zooCache = new TestZooCache(zk, Set.of(base));
+
+    zrw.mkdirs(base);
+    zrw.putPersistentData(base + "/test2", new byte[] {1, 2, 3, 4}, 
ZooUtil.NodeExistsPolicy.FAIL);
+
+    assertArrayEquals(new byte[] {1, 2, 3, 4}, zooCache.get(base + "/test2"));
+    assertEquals(List.of("test2"), zooCache.getChildren(base));
+
+    long uc1 = zooCache.getUpdateCount();
+
+    assertTrue(zooCache.dataCached(base + "/test2"));
+    assertTrue(zooCache.childrenCached(base));
+
+    assertArrayEquals(new byte[] {1, 2, 3, 4}, zooCache.get(base + "/test2"));
+    assertEquals(List.of("test2"), zooCache.getChildren(base));
+
+    assertEquals(uc1, zooCache.getUpdateCount());
+
+    // restarting zookeeper should cause the cache to be cleared
+    szk.restart();
+
+    // clearing the cache should increment the update count
+    Wait.waitFor(() -> uc1 != zooCache.getUpdateCount());
+    // The data and children previously cached should no longer be cached
+    assertFalse(zooCache.dataCached(base + "/test2"));
+    assertFalse(zooCache.childrenCached(base));
+
+    assertArrayEquals(new byte[] {1, 2, 3, 4}, zooCache.get(base + "/test2"));
+    assertEquals(List.of("test2"), zooCache.getChildren(base));
+
+    // after the event, ensure that zoocache will still eventually see 
updates. May have
+    // reregistered watchers.
+    zrw.putPersistentData(base + "/test2", new byte[] {4, 3, 2, 1},
+        ZooUtil.NodeExistsPolicy.OVERWRITE);
+    Wait.waitFor(() -> Arrays.equals(new byte[] {4, 3, 2, 1}, 
zooCache.get(base + "/test2")));
+  }
+
+  @SuppressWarnings("deprecation")
+  @Test
+  public void testDisconnect() throws Exception {
+    final String root = Constants.ZROOT + UUID.randomUUID();
+    final String base = root + Constants.ZTSERVERS;
+    TestZooCache zooCache = new TestZooCache(zk, Set.of(base));
+
+    zrw.mkdirs(base);
+    zrw.putPersistentData(base + "/test2", new byte[] {1, 2, 3, 4}, 
ZooUtil.NodeExistsPolicy.FAIL);
+
+    assertArrayEquals(new byte[] {1, 2, 3, 4}, zooCache.get(base + "/test2"));
+    assertEquals(List.of("test2"), zooCache.getChildren(base));
+
+    long uc1 = zooCache.getUpdateCount();
+
+    assertTrue(zooCache.dataCached(base + "/test2"));
+    assertTrue(zooCache.childrenCached(base));
+
+    assertArrayEquals(new byte[] {1, 2, 3, 4}, zooCache.get(base + "/test2"));
+    assertEquals(List.of("test2"), zooCache.getChildren(base));
+
+    assertEquals(uc1, zooCache.getUpdateCount());
+
+    // Find the zookeeper thread that sends stuff to the server and pause it 
for longer than the
+    // session timeout. This should cause the server to disconnect the session 
which should cause
+    // the cache to clear.
+    Thread sendThread = findZookeeperSendThread();
+    sendThread.suspend();
+    UtilWaitThread.sleep(SESSION_TIMEOUT.plusSeconds(1).toMillis());
+    sendThread.resume();
+
+    // clearing the cache should increment the update count
+    Wait.waitFor(() -> uc1 != zooCache.getUpdateCount());
+    // The data and children previously cached should no longer be cached
+    assertFalse(zooCache.dataCached(base + "/test2"));
+    assertFalse(zooCache.childrenCached(base));
+
+    assertArrayEquals(new byte[] {1, 2, 3, 4}, zooCache.get(base + "/test2"));
+    assertEquals(List.of("test2"), zooCache.getChildren(base));
+
+    // after the event, ensure that zoocache will still eventually see updates.
+    zrw.putPersistentData(base + "/test2", new byte[] {4, 3, 2, 1},
+        ZooUtil.NodeExistsPolicy.OVERWRITE);
+    Wait.waitFor(() -> Arrays.equals(new byte[] {4, 3, 2, 1}, 
zooCache.get(base + "/test2")));
+  }
+
+  private Thread findZookeeperSendThread() {
+    Map<Thread,StackTraceElement[]> traces = Thread.getAllStackTraces();
+    for (var entry : traces.entrySet()) {
+      Thread thread = entry.getKey();
+      StackTraceElement[] stackTrace = entry.getValue();
+      for (var ste : stackTrace) {
+        if (ste.getClassName().contains(ClientCnxn.class.getSimpleName() + 
"$SendThread")) {

Review Comment:
   what do you mean by get it dynamically?  this code does find the thread at 
runtime.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to