This is an automated email from the ASF dual-hosted git repository.

kturner pushed a commit to branch 2.1
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/2.1 by this push:
     new 5282088890 adds unit test for deferred session cleanup (#3581)
5282088890 is described below

commit 5282088890fbb6db4ee2a9ca9bdb9b9c08334ebc
Author: Keith Turner <ktur...@apache.org>
AuthorDate: Wed Jul 12 12:55:25 2023 -0400

    adds unit test for deferred session cleanup (#3581)
---
 .../accumulo/tserver/session/SessionManager.java   |   6 +-
 .../tserver/session/SessionManagerTest.java        | 109 +++++++++++++++++++++
 2 files changed, 114 insertions(+), 1 deletion(-)

diff --git 
a/server/tserver/src/main/java/org/apache/accumulo/tserver/session/SessionManager.java
 
b/server/tserver/src/main/java/org/apache/accumulo/tserver/session/SessionManager.java
index f0f8a5de2a..5041b5c66f 100644
--- 
a/server/tserver/src/main/java/org/apache/accumulo/tserver/session/SessionManager.java
+++ 
b/server/tserver/src/main/java/org/apache/accumulo/tserver/session/SessionManager.java
@@ -224,7 +224,7 @@ public class SessionManager {
     return session;
   }
 
-  private void cleanup(Session session) {
+  static void cleanup(BlockingQueue<Session> deferredCleanupQueue, Session 
session) {
     if (!session.cleanup()) {
       var retry = Retry.builder().infiniteRetries().retryAfter(25, 
MILLISECONDS)
           .incrementBy(25, MILLISECONDS).maxWait(5, SECONDS).backOffFactor(1.5)
@@ -248,6 +248,10 @@ public class SessionManager {
     }
   }
 
+  private void cleanup(Session session) {
+    cleanup(deferredCleanupQueue, session);
+  }
+
   private void sweep(final long maxIdle, final long maxUpdateIdle) {
     List<Session> sessionsToCleanup = new LinkedList<>();
     Iterator<Session> iter = sessions.values().iterator();
diff --git 
a/server/tserver/src/test/java/org/apache/accumulo/tserver/session/SessionManagerTest.java
 
b/server/tserver/src/test/java/org/apache/accumulo/tserver/session/SessionManagerTest.java
new file mode 100644
index 0000000000..be3626ea71
--- /dev/null
+++ 
b/server/tserver/src/test/java/org/apache/accumulo/tserver/session/SessionManagerTest.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.accumulo.tserver.session;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.concurrent.ArrayBlockingQueue;
+
+import org.junit.jupiter.api.Test;
+
+public class SessionManagerTest {
+
+  private static class TestSession extends Session {
+
+    int cleanupCount;
+
+    TestSession(int cleanupCount) {
+      super(null);
+      this.cleanupCount = cleanupCount;
+    }
+
+    @Override
+    public boolean cleanup() {
+      return cleanupCount-- <= 0;
+    }
+  }
+
+  @Test
+  public void testTestcode() {
+    // test behavior of test class
+    TestSession session = new TestSession(2);
+    assertFalse(session.cleanup());
+    assertFalse(session.cleanup());
+    assertTrue(session.cleanup());
+    assertTrue(session.cleanup());
+  }
+
+  @Test
+  public void testFullDeferredCleanupQueue() {
+    ArrayBlockingQueue<Session> deferredCleanupQeue = new 
ArrayBlockingQueue<>(3);
+
+    deferredCleanupQeue.add(new TestSession(2));
+    deferredCleanupQeue.add(new TestSession(2));
+    deferredCleanupQeue.add(new TestSession(2));
+
+    // the queue is full, so cleanup method should repeatedly call cleanup 
instead of queuing
+    TestSession session = new TestSession(5);
+    SessionManager.cleanup(deferredCleanupQeue, session);
+    assertEquals(-1, session.cleanupCount);
+    assertEquals(3, deferredCleanupQeue.size());
+    assertTrue(deferredCleanupQeue.stream().allMatch(s -> ((TestSession) 
s).cleanupCount == 2));
+  }
+
+  @Test
+  public void testDefersCleanup() {
+    ArrayBlockingQueue<Session> deferredCleanupQeue = new 
ArrayBlockingQueue<>(3);
+
+    deferredCleanupQeue.add(new TestSession(2));
+    deferredCleanupQeue.add(new TestSession(2));
+
+    TestSession session = new TestSession(5);
+
+    // the queue is not full so expect the session to be queued after cleanup
+    SessionManager.cleanup(deferredCleanupQeue, session);
+
+    assertEquals(4, session.cleanupCount);
+    assertEquals(3, deferredCleanupQeue.size());
+    assertEquals(2,
+        deferredCleanupQeue.stream().filter(s -> ((TestSession) 
s).cleanupCount == 2).count());
+    assertEquals(1,
+        deferredCleanupQeue.stream().filter(s -> ((TestSession) 
s).cleanupCount == 4).count());
+  }
+
+  @Test
+  public void testDeferNotNeeded() {
+    ArrayBlockingQueue<Session> deferredCleanupQeue = new 
ArrayBlockingQueue<>(3);
+
+    deferredCleanupQeue.add(new TestSession(2));
+    deferredCleanupQeue.add(new TestSession(2));
+
+    TestSession session = new TestSession(0);
+
+    // the queue is not full, but the session will cleanup in one call so it 
should not be queued
+    SessionManager.cleanup(deferredCleanupQeue, session);
+
+    assertEquals(-1, session.cleanupCount);
+    assertEquals(2, deferredCleanupQeue.size());
+    assertEquals(2,
+        deferredCleanupQeue.stream().filter(s -> ((TestSession) 
s).cleanupCount == 2).count());
+  }
+}

Reply via email to