kasakrisz commented on code in PR #6168:
URL: https://github.com/apache/hive/pull/6168#discussion_r2497740051


##########
service/src/test/org/apache/hive/service/cli/TestCLIServiceConnectionLimits.java:
##########
@@ -345,6 +347,83 @@ public void testConnectionForwardedIpAddresses() throws 
HiveSQLException {
     }
   }
 
+  @Test
+  public void testConnectionLimitPerUserWhenSessionCreationFails() throws 
HiveSQLException {
+    conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER, 
2);
+    
conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_IPADDRESS, 
0);
+    
conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER_IPADDRESS,
 0);
+    CLIService service = getService(conf);
+    HashMap<String, String> sessionConf = new HashMap<>(1);
+    // modifying this property at runtime is not allowed, it will throw 
exception and session will not be created
+    sessionConf.put("hive.in.test", "true");
+    try {
+      // Here limit is set as 2 but if session creation fails it shouldn't 
increment the count
+      for (int i = 0; i < 3 + 1; i++) {

Review Comment:
   Could you please elaborate why is this expression needed `3 + 1` instead of 
`4`?
   
   If you want to highlight that `n + 1` attempts is needed to test that the 
limit is not exceeded in case of subsequent session open failure how about 
extracting the magic number `2` to a constant?



##########
service/src/test/org/apache/hive/service/cli/TestCLIServiceConnectionLimits.java:
##########
@@ -345,6 +347,83 @@ public void testConnectionForwardedIpAddresses() throws 
HiveSQLException {
     }
   }
 
+  @Test
+  public void testConnectionLimitPerUserWhenSessionCreationFails() throws 
HiveSQLException {
+    conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER, 
2);
+    
conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_IPADDRESS, 
0);
+    
conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER_IPADDRESS,
 0);
+    CLIService service = getService(conf);
+    HashMap<String, String> sessionConf = new HashMap<>(1);
+    // modifying this property at runtime is not allowed, it will throw 
exception and session will not be created
+    sessionConf.put("hive.in.test", "true");

Review Comment:
   Could you please use another restricted config? IMHO the presence of the 
`hive.in.test` setting is wrong, as it indicates that test-specific code is 
being shipped to the production.



##########
service/src/test/org/apache/hive/service/cli/session/TestSessionManagerMetrics.java:
##########
@@ -396,4 +397,45 @@ public void testAbandonedSessionMetrics() throws Exception 
{
     } while (!expectedValue.equals(currentValue) && --count > 0);
     Assert.assertEquals(expectedValue, currentValue);
   }
+
+  @Test
+  public void testSessionLimitsPerUserWhenSessionCreationFails() throws 
Exception {
+    sm.start();
+    HashMap<String, String> sessionConf = new HashMap<>(1);
+    // modifying this property at runtime is not allowed, it will throw 
exception and session will not be created
+    sessionConf.put("hive.in.test", "true");
+    for (int i = 0; i < 3; i++) {
+      try {
+        sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", 
"passw", "127.0.0.1", sessionConf);
+      } catch (HiveSQLException e) {
+        // Here 'hive.server2.limit.connections.per.user' property value set 
as 2 so when we create 3 sessions, for all 3 sessions it should throw exception 
with failure reason
+        // It should not throw exception with 'Connection limit per user 
reached (user: user limit: 2)' message
+        if (i == 0 || i == 1) {
+          Assert.assertEquals(
+              "Failed to open new session: Cannot modify hive.in.test at 
runtime. It is in the list of parameters that can't be modified at runtime or 
is prefixed by a restricted variable",
+              e.getMessage());
+        }
+        // for the 3rd session creation also it should throw exception with 
actual failure reason, should not throw exception with limit reason.
+        if (i == 2) {
+          Assert.assertNotEquals("Connection limit per user reached (user: 
user limit: 2)", e.getMessage());
+          Assert.assertEquals(
+              "Failed to open new session: Cannot modify hive.in.test at 
runtime. It is in the list of parameters that can't be modified at runtime or 
is prefixed by a restricted variable",
+              e.getMessage());

Review Comment:
   Why both of these asserts are needed? Aren't we expecting the same message 
like in the first n attempt?



##########
service/src/test/org/apache/hive/service/cli/TestCLIServiceConnectionLimits.java:
##########
@@ -345,6 +347,83 @@ public void testConnectionForwardedIpAddresses() throws 
HiveSQLException {
     }
   }
 
+  @Test
+  public void testConnectionLimitPerUserWhenSessionCreationFails() throws 
HiveSQLException {
+    conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER, 
2);
+    
conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_IPADDRESS, 
0);
+    
conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER_IPADDRESS,
 0);
+    CLIService service = getService(conf);
+    HashMap<String, String> sessionConf = new HashMap<>(1);
+    // modifying this property at runtime is not allowed, it will throw 
exception and session will not be created
+    sessionConf.put("hive.in.test", "true");
+    try {
+      // Here limit is set as 2 but if session creation fails it shouldn't 
increment the count
+      for (int i = 0; i < 3 + 1; i++) {
+        try {
+          service.openSession(CLIService.SERVER_VERSION, "foo", "bar", 
IPStackUtils.resolveLoopbackAddress(),
+              sessionConf);
+        } catch (HiveSQLException he) {
+          Assert.assertEquals(
+              "Failed to open new session: Cannot modify hive.in.test at 
runtime. It is in the list of parameters that can't be modified at runtime or 
is prefixed by a restricted variable",
+              he.getMessage());
+        }
+      }
+    } finally {
+      service.stop();
+    }
+  }
+
+  @Test
+  public void testConnectionLimitPerIpAddressWhenSessionCreationFails() throws 
HiveSQLException {
+    conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER, 
0);
+    
conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_IPADDRESS, 
2);
+    
conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER_IPADDRESS,
 0);
+    CLIService service = getService(conf);
+    HashMap<String, String> sessionConf = new HashMap<>(1);
+    // modifying this property at runtime is not allowed, it will throw 
exception and session will not be created
+    sessionConf.put("hive.in.test", "true");
+    try {
+      // Here limit is set as 2 but if session creation fails it shouldn't 
increment the count
+      for (int i = 0; i < 3 + 1; i++) {
+        try {
+          service.openSession(CLIService.SERVER_VERSION, "foo", "bar", 
IPStackUtils.resolveLoopbackAddress(),
+              sessionConf);
+        } catch (HiveSQLException he) {
+          Assert.assertEquals(
+              "Failed to open new session: Cannot modify hive.in.test at 
runtime. It is in the list of parameters that can't be modified at runtime or 
is prefixed by a restricted variable",
+              he.getMessage());
+        }
+      }
+    } finally {
+      service.stop();
+    }
+  }
+
+  @Test
+  public void testConnectionLimitPerUserIpAddressWhenSessionCreationFails() 
throws HiveSQLException {
+    conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER, 
0);
+    
conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_IPADDRESS, 
0);
+    
conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER_IPADDRESS,
 2);
+    CLIService service = getService(conf);
+    HashMap<String, String> sessionConf = new HashMap<>(1);
+    // modifying this property at runtime is not allowed, it will throw 
exception and session will not be created
+    sessionConf.put("hive.in.test", "true");
+    try {
+      // Here limit is set as 2 but if session creation fails it shouldn't 
increment the count
+      for (int i = 0; i < 3 + 1; i++) {
+        try {
+          service.openSession(CLIService.SERVER_VERSION, "foo", "bar", 
IPStackUtils.resolveLoopbackAddress(), sessionConf);
+        } catch (HiveSQLException he) {
+          Assert.assertEquals(
+              "Failed to open new session: Cannot modify hive.in.test at 
runtime. It is in the list of parameters that can't be modified at runtime or 
is prefixed by a restricted variable",
+              he.getMessage());
+        }
+      }
+    } finally {
+      service.stop();
+    }
+  }

Review Comment:
   Is it possible to extract this to a method? It seems that 3 test are using 
it.



##########
service/src/test/org/apache/hive/service/cli/session/TestSessionManagerMetrics.java:
##########
@@ -396,4 +397,45 @@ public void testAbandonedSessionMetrics() throws Exception 
{
     } while (!expectedValue.equals(currentValue) && --count > 0);
     Assert.assertEquals(expectedValue, currentValue);
   }
+
+  @Test
+  public void testSessionLimitsPerUserWhenSessionCreationFails() throws 
Exception {
+    sm.start();
+    HashMap<String, String> sessionConf = new HashMap<>(1);
+    // modifying this property at runtime is not allowed, it will throw 
exception and session will not be created
+    sessionConf.put("hive.in.test", "true");
+    for (int i = 0; i < 3; i++) {
+      try {
+        sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", 
"passw", "127.0.0.1", sessionConf);
+      } catch (HiveSQLException e) {
+        // Here 'hive.server2.limit.connections.per.user' property value set 
as 2 so when we create 3 sessions, for all 3 sessions it should throw exception 
with failure reason
+        // It should not throw exception with 'Connection limit per user 
reached (user: user limit: 2)' message
+        if (i == 0 || i == 1) {
+          Assert.assertEquals(
+              "Failed to open new session: Cannot modify hive.in.test at 
runtime. It is in the list of parameters that can't be modified at runtime or 
is prefixed by a restricted variable",
+              e.getMessage());
+        }
+        // for the 3rd session creation also it should throw exception with 
actual failure reason, should not throw exception with limit reason.
+        if (i == 2) {
+          Assert.assertNotEquals("Connection limit per user reached (user: 
user limit: 2)", e.getMessage());
+          Assert.assertEquals(
+              "Failed to open new session: Cannot modify hive.in.test at 
runtime. It is in the list of parameters that can't be modified at runtime or 
is prefixed by a restricted variable",
+              e.getMessage());
+        }
+      }
+    }
+    SessionHandle handle = null;
+    // After 3 unsuccessful session creation now with session creation with 
proper details should be able to create the session
+    try {
+      handle = sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, 
"user", "passw", "127.0.0.1",
+          new HashMap<String, String>());
+    } catch (Exception e) {
+      Assert.fail("Should not throw exception, session should be created 
successfully. " + e.getMessage());
+    }

Review Comment:
   IMHO this catch and assert is not necessary. Let the exception be thrown by 
the test method.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to