Copilot commented on code in PR #18331:
URL:
https://github.com/apache/dolphinscheduler/pull/18331#discussion_r3380769344
##########
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-hive/src/main/java/org/apache/dolphinscheduler/plugin/datasource/hive/security/UserGroupInformationFactory.java:
##########
@@ -39,7 +40,7 @@ public class UserGroupInformationFactory {
private static final Map<String, Integer> currentLoginTimesMap = new
HashMap<>();
- private static final Map<String, UserGroupInformation>
userGroupInformationMap = new HashMap<>();
+ private static final Map<String, UserGroupInformation>
userGroupInformationMap = new ConcurrentHashMap<>();
Review Comment:
PR description says this is "code cleanup without any test coverage", but
this change adds a new concurrency test. Please update the PR
description/verify section to match the actual changes (or remove the test if
it wasn't intended to be included).
##########
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-hive/src/test/java/org/apache/dolphinscheduler/plugin/datasource/hive/security/UserGroupInformationFactoryTest.java:
##########
@@ -0,0 +1,125 @@
+/*
+ * 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
+ *
+ * http://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.dolphinscheduler.plugin.datasource.hive.security;
+
+import org.apache.dolphinscheduler.common.constants.Constants;
+import org.apache.dolphinscheduler.common.utils.PropertyUtils;
+
+import org.apache.hadoop.security.UserGroupInformation;
+
+import java.lang.reflect.Field;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class UserGroupInformationFactoryTest {
+
+ private MockedStatic<PropertyUtils> mockedPropertyUtils;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ mockedPropertyUtils = Mockito.mockStatic(PropertyUtils.class);
+ mockedPropertyUtils
+ .when(() ->
PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE))
+ .thenReturn("LOCAL");
+ clearInternalMaps();
+ }
+
+ @AfterEach
+ void tearDown() {
+ mockedPropertyUtils.close();
+ }
+
+ @Test
+ void testForEachConcurrentWithModification() throws Exception {
+ for (int i = 0; i < 10; i++) {
+ UserGroupInformationFactory.login("user_" + i);
+ }
+
+ CountDownLatch startLatch = new CountDownLatch(1);
+ CountDownLatch doneLatch = new CountDownLatch(2);
+ AtomicInteger errorCount = new AtomicInteger(0);
+
+ Thread iteratorThread = new Thread(() -> {
+ try {
+ startLatch.await();
+ for (int round = 0; round < 10000; round++) {
+ getUserGroupInformationMap().forEach((key, ugi) -> {});
+ }
+ } catch (Exception e) {
+ errorCount.incrementAndGet();
+ } finally {
+ doneLatch.countDown();
+ }
+ });
+ iteratorThread.start();
+
+ Thread modifierPool = new Thread(() -> {
+ try {
+ startLatch.await();
+ for (int i = 0; i < 500; i++) {
+ for (int j = 0; i < 10; i++) {
Review Comment:
Inner loop mistakenly uses and increments the outer-loop variable `i` (`for
(int j = 0; i < 10; i++)`), so `j` never changes and only `user_0` is
exercised; this also reduces the amount of concurrent modification and makes
the test ineffective.
--
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]