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

caolu pushed a commit to branch kylin5
in repository https://gitbox.apache.org/repos/asf/kylin.git


The following commit(s) were added to refs/heads/kylin5 by this push:
     new 1662fc4189 KYLIN-6068 Optimizing spring session cleanup to avoid MySQL 
deadlocks as much as possible
1662fc4189 is described below

commit 1662fc41892ace1987d827bc8061023a4ae5c264
Author: 夏旭晨 <[email protected]>
AuthorDate: Thu Jan 23 17:17:00 2025 +0800

    KYLIN-6068 Optimizing spring session cleanup to avoid MySQL deadlocks as 
much as possible
    
    * Adjust the scheduling time for Spring Session cleanup.
    
    ---------
    
    Co-authored-by: xuchen.xia <[email protected]>
    Co-authored-by: Guoliang.Sun <[email protected]>
---
 pom.xml                                            |  2 +-
 .../config/KylinPropertySourceConfiguration.java   | 17 ++++++
 .../service/task/SpringSessionCleanExpiredJob.java | 67 ++++++++++++++++++++++
 .../service/task/SpringSessionCleanScheduler.java  | 54 +++++++++++++++++
 .../task/SpringSessionCleanExpiredJobTest.java     | 58 +++++++++++++++++++
 .../task/SpringSessionCleanSchedulerTest.java      | 46 +++++++++++++++
 .../apache/kylin/job/execution/JobTypeEnum.java    |  3 +-
 .../kylin/job/factory/JobFactoryConstant.java      |  1 +
 .../kylin/rest/KylinPrepareEnvListenerTest.java    | 11 ++++
 9 files changed, 257 insertions(+), 2 deletions(-)

diff --git a/pom.xml b/pom.xml
index 0fc68408c7..2705a1c494 100644
--- a/pom.xml
+++ b/pom.xml
@@ -231,7 +231,7 @@
         <spring.cloud.version>2021.0.6</spring.cloud.version>
         <spring.boot.version>2.7.18</spring.boot.version>
         <spring-boot-admin.version>2.6.10</spring-boot-admin.version>
-        <spring-session.version>2.6.1-kylin-r4</spring-session.version>
+        <spring-session.version>2.6.1-kylin-r5</spring-session.version>
         
<spring.framework.security.config.version>5.7.12</spring.framework.security.config.version>
         
<spring.framework.security.extensions.version>1.0.10.RELEASE</spring.framework.security.extensions.version>
         <spring-web.version>5.3.33</spring-web.version>
diff --git 
a/src/common-service/src/main/java/org/apache/kylin/rest/config/KylinPropertySourceConfiguration.java
 
b/src/common-service/src/main/java/org/apache/kylin/rest/config/KylinPropertySourceConfiguration.java
index 6c78964085..d3d238e036 100644
--- 
a/src/common-service/src/main/java/org/apache/kylin/rest/config/KylinPropertySourceConfiguration.java
+++ 
b/src/common-service/src/main/java/org/apache/kylin/rest/config/KylinPropertySourceConfiguration.java
@@ -23,6 +23,7 @@ import org.apache.kylin.common.KylinConfig;
 import org.apache.kylin.common.persistence.metadata.jdbc.JdbcUtil;
 import org.apache.kylin.common.util.Unsafe;
 import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.session.StoreType;
 import 
org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
 import org.springframework.boot.env.EnvironmentPostProcessor;
 import org.springframework.core.Ordered;
@@ -36,6 +37,8 @@ import lombok.extern.slf4j.Slf4j;
 public class KylinPropertySourceConfiguration implements 
EnvironmentPostProcessor, Ordered {
 
     private static final String SYSTEM_PROPERTY_PREFIX = 
"kylin.system.property.";
+    private static final String SPRING_SESSION_CLEAN_CRON = 
"spring.session.jdbc.cleanup-cron";
+    public static final String SPRING_SESSION_JDBC_CLEANUP_FLAG = 
"spring.session.jdbc.cleanup-flag";
 
     @Override
     public void postProcessEnvironment(ConfigurableEnvironment environment, 
SpringApplication application) {
@@ -62,6 +65,7 @@ public class KylinPropertySourceConfiguration implements 
EnvironmentPostProcesso
         };
 
         setSystemProperty(kylinConfig.exportToProperties());
+        discardJDBCCleanSessionProperties(kylinConfig);
         propertySources.addAfter("systemProperties", source);
     }
 
@@ -78,4 +82,17 @@ public class KylinPropertySourceConfiguration implements 
EnvironmentPostProcesso
     public int getOrder() {
         return ConfigDataEnvironmentPostProcessor.ORDER + 1020;
     }
+
+    /**
+     * when spring store type is jdbc, we should do following:
+     * 1. use {@link 
org.apache.kylin.rest.service.task.SpringSessionCleanScheduler} rather than 
spring session cleanup
+     * 2. discard spring session cleanup task
+     */
+    public void discardJDBCCleanSessionProperties(KylinConfig kylinConfig) {
+        if 
(kylinConfig.getSpringStoreType().equalsIgnoreCase(StoreType.JDBC.toString())) {
+            log.info("Discard JDBC clean session properties.");
+            Unsafe.setProperty(SPRING_SESSION_CLEAN_CRON, "-");
+            Unsafe.setProperty(SPRING_SESSION_JDBC_CLEANUP_FLAG, "false");
+        }
+    }
 }
diff --git 
a/src/common-service/src/main/java/org/apache/kylin/rest/service/task/SpringSessionCleanExpiredJob.java
 
b/src/common-service/src/main/java/org/apache/kylin/rest/service/task/SpringSessionCleanExpiredJob.java
new file mode 100644
index 0000000000..0168bfd7f7
--- /dev/null
+++ 
b/src/common-service/src/main/java/org/apache/kylin/rest/service/task/SpringSessionCleanExpiredJob.java
@@ -0,0 +1,67 @@
+/*
+ * 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.kylin.rest.service.task;
+
+import org.apache.kylin.job.JobContext;
+import org.apache.kylin.job.execution.AbstractExecutable;
+import org.apache.kylin.job.execution.DefaultExecutable;
+import org.apache.kylin.job.execution.ExecuteResult;
+import org.apache.kylin.job.factory.JobFactory;
+import org.apache.kylin.rest.util.SpringContext;
+import org.springframework.context.ApplicationContext;
+import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class SpringSessionCleanExpiredJob extends AbstractExecutable {
+
+    public static class Factory extends JobFactory {
+
+        @Override
+        protected DefaultExecutable create(JobBuildParams jobBuildParams) {
+            return SpringSessionCleanExpiredJob.create(jobBuildParams);
+        }
+    }
+
+    public SpringSessionCleanExpiredJob() {
+        super();
+    }
+
+    public SpringSessionCleanExpiredJob(Object notSetId) {
+        super(notSetId);
+    }
+
+    public static DefaultExecutable create(JobFactory.JobBuildParams 
jobBuildParams) {
+        DefaultExecutable job = new DefaultExecutable();
+        SpringSessionCleanExpiredJob innerJob = new 
SpringSessionCleanExpiredJob();
+        innerJob.setParam(PARENT_ID, job.getJobId());
+        job.addTask(innerJob);
+        job.setJobType(jobBuildParams.getJobType());
+        return job;
+    }
+
+    @Override
+    protected ExecuteResult doWork(JobContext context) {
+        ApplicationContext applicationContext = 
SpringContext.getApplicationContext();
+        
applicationContext.getBean(JdbcIndexedSessionRepository.class).cleanUpExpiredSessions();
+        log.debug("Clean up expired sessions successfully.");
+        return ExecuteResult.createSucceed();
+    }
+}
diff --git 
a/src/common-service/src/main/java/org/apache/kylin/rest/service/task/SpringSessionCleanScheduler.java
 
b/src/common-service/src/main/java/org/apache/kylin/rest/service/task/SpringSessionCleanScheduler.java
new file mode 100644
index 0000000000..9ba6aaccd3
--- /dev/null
+++ 
b/src/common-service/src/main/java/org/apache/kylin/rest/service/task/SpringSessionCleanScheduler.java
@@ -0,0 +1,54 @@
+/*
+ * 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.kylin.rest.service.task;
+
+import static 
org.apache.kylin.job.factory.JobFactoryConstant.SPRING_SESSION_CLEAN_EXPIRED_JOB_FACTORY;
+
+import javax.annotation.PostConstruct;
+
+import org.apache.kylin.common.KylinConfig;
+import org.apache.kylin.common.persistence.ResourceStore;
+import org.apache.kylin.job.execution.ExecutableManager;
+import org.apache.kylin.job.execution.JobTypeEnum;
+import org.apache.kylin.job.factory.JobFactory;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Component
+@ConditionalOnProperty(name = "spring.session.store-type", havingValue = 
"JDBC")
+@Slf4j
+public class SpringSessionCleanScheduler {
+
+    @PostConstruct
+    public void init() {
+        JobFactory.register(SPRING_SESSION_CLEAN_EXPIRED_JOB_FACTORY, new 
SpringSessionCleanExpiredJob.Factory());
+    }
+
+    @Scheduled(cron = "${spring.session.jdbc.cleanup-cron-task:0 */30 * * * 
*}")
+    public void submitJob() {
+        ExecutableManager.getInstance(KylinConfig.getInstanceFromEnv(), 
ResourceStore.GLOBAL_PROJECT)
+                
.checkAndSubmitCronJob(SPRING_SESSION_CLEAN_EXPIRED_JOB_FACTORY,
+                        JobTypeEnum.SPRING_SESSION_CLEAN_EXPIRED);
+        log.debug("submit job to clean spring session");
+    }
+
+}
diff --git 
a/src/common-service/src/test/java/org/apache/kylin/rest/service/task/SpringSessionCleanExpiredJobTest.java
 
b/src/common-service/src/test/java/org/apache/kylin/rest/service/task/SpringSessionCleanExpiredJobTest.java
new file mode 100644
index 0000000000..32a492ae89
--- /dev/null
+++ 
b/src/common-service/src/test/java/org/apache/kylin/rest/service/task/SpringSessionCleanExpiredJobTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.kylin.rest.service.task;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.kylin.job.execution.ExecuteResult;
+import org.apache.kylin.rest.util.SpringContext;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PowerMockIgnore;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.springframework.context.ApplicationContext;
+import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(SpringContext.class)
+@PowerMockIgnore("javax.management.*")
+public class SpringSessionCleanExpiredJobTest {
+
+    @Test
+    public void testSpringSessionClean() {
+        JdbcIndexedSessionRepository jdbcIndexedSessionRepository = 
prepareSpringSessionBean();
+        SpringSessionCleanExpiredJob springSessionCleanExpiredJob = new 
SpringSessionCleanExpiredJob();
+        ExecuteResult executeResult = 
springSessionCleanExpiredJob.doWork(null);
+        assertEquals(ExecuteResult.State.SUCCEED, executeResult.state());
+        Mockito.verify(jdbcIndexedSessionRepository).cleanUpExpiredSessions();
+    }
+
+    private static JdbcIndexedSessionRepository prepareSpringSessionBean() {
+        ApplicationContext applicationContext = 
Mockito.mock(ApplicationContext.class);
+        JdbcIndexedSessionRepository jdbcIndexedSessionRepository = 
Mockito.mock(JdbcIndexedSessionRepository.class);
+        
Mockito.when(applicationContext.getBean(JdbcIndexedSessionRepository.class))
+                .thenReturn(jdbcIndexedSessionRepository);
+        PowerMockito.mockStatic(SpringContext.class);
+        
PowerMockito.when(SpringContext.getApplicationContext()).thenReturn(applicationContext);
+        return jdbcIndexedSessionRepository;
+    }
+}
diff --git 
a/src/common-service/src/test/java/org/apache/kylin/rest/service/task/SpringSessionCleanSchedulerTest.java
 
b/src/common-service/src/test/java/org/apache/kylin/rest/service/task/SpringSessionCleanSchedulerTest.java
new file mode 100644
index 0000000000..90cd3f8959
--- /dev/null
+++ 
b/src/common-service/src/test/java/org/apache/kylin/rest/service/task/SpringSessionCleanSchedulerTest.java
@@ -0,0 +1,46 @@
+/*
+ * 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.kylin.rest.service.task;
+
+import org.apache.kylin.common.KylinConfig;
+import org.apache.kylin.common.persistence.ResourceStore;
+import org.apache.kylin.job.execution.ExecutableManager;
+import org.apache.kylin.job.execution.JobTypeEnum;
+import org.apache.kylin.junit.annotation.MetadataInfo;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+@MetadataInfo
+class SpringSessionCleanSchedulerTest {
+
+    @Test
+    void testTaskSubmit() {
+        SpringSessionCleanScheduler springSessionCleanScheduler = new 
SpringSessionCleanScheduler();
+        springSessionCleanScheduler.init();
+        Assertions.assertEquals(0, cleanJobCount());
+        springSessionCleanScheduler.submitJob();
+        Assertions.assertEquals(1, cleanJobCount());
+    }
+
+    private static long cleanJobCount() {
+        return ExecutableManager.getInstance(KylinConfig.getInstanceFromEnv(), 
ResourceStore.GLOBAL_PROJECT)
+                .getAllJobs().stream().filter(job -> job.getJobType() == 
JobTypeEnum.SPRING_SESSION_CLEAN_EXPIRED)
+                .count();
+    }
+}
diff --git 
a/src/core-job/src/main/java/org/apache/kylin/job/execution/JobTypeEnum.java 
b/src/core-job/src/main/java/org/apache/kylin/job/execution/JobTypeEnum.java
index 6c8a942624..f0cefe2ea1 100644
--- a/src/core-job/src/main/java/org/apache/kylin/job/execution/JobTypeEnum.java
+++ b/src/core-job/src/main/java/org/apache/kylin/job/execution/JobTypeEnum.java
@@ -54,7 +54,8 @@ public enum JobTypeEnum {
     ROUTINE(Category.CRON), //
     META(Category.CRON), //
     SOURCE_USAGE(Category.CRON), //
-    AUTO_REFRESH(Category.CRON);
+    AUTO_REFRESH(Category.CRON), //
+    SPRING_SESSION_CLEAN_EXPIRED(Category.CRON);
 
     private final String category;
 
diff --git 
a/src/core-job/src/main/java/org/apache/kylin/job/factory/JobFactoryConstant.java
 
b/src/core-job/src/main/java/org/apache/kylin/job/factory/JobFactoryConstant.java
index a9382e95f4..39b59da0ea 100644
--- 
a/src/core-job/src/main/java/org/apache/kylin/job/factory/JobFactoryConstant.java
+++ 
b/src/core-job/src/main/java/org/apache/kylin/job/factory/JobFactoryConstant.java
@@ -28,6 +28,7 @@ public class JobFactoryConstant {
     public static final String META_JOB_FACTORY = "META_JOB_FACTORY";
     public static final String SOURCE_USAGE_JOB_FACTORY = 
"SOURCE_USAGE_JOB_FACTORY";
     public static final String AUTO_REFRESH_JOB_FACTORY = 
"AUTO_REFRESH_JOB_FACTORY";
+    public static final String SPRING_SESSION_CLEAN_EXPIRED_JOB_FACTORY = 
"SPRING_SESSION_CLEAN_EXPIRED_JOB_FACTORY";
 
     public static final String CUBE_JOB_FACTORY = "CUBE_JOB_FACTORY";
     public static final String MERGE_JOB_FACTORY = "MERGE_JOB_FACTORY";
diff --git 
a/src/server/src/test/java/org/apache/kylin/rest/KylinPrepareEnvListenerTest.java
 
b/src/server/src/test/java/org/apache/kylin/rest/KylinPrepareEnvListenerTest.java
index 6c923420ce..4608dce9f8 100644
--- 
a/src/server/src/test/java/org/apache/kylin/rest/KylinPrepareEnvListenerTest.java
+++ 
b/src/server/src/test/java/org/apache/kylin/rest/KylinPrepareEnvListenerTest.java
@@ -17,11 +17,14 @@
  */
 package org.apache.kylin.rest;
 
+import static org.apache.kylin.common.util.TestUtils.getTestConfig;
+
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.stream.Collectors;
 
+import org.apache.kylin.junit.annotation.MetadataInfo;
 import org.apache.kylin.rest.config.KylinPropertySourceConfiguration;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
@@ -37,6 +40,9 @@ import 
org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.core.env.Profiles;
 
+import lombok.val;
+
+@MetadataInfo(onlyProps = true)
 class KylinPrepareEnvListenerTest {
 
     private SpringApplication application;
@@ -84,6 +90,11 @@ class KylinPrepareEnvListenerTest {
         configuration.setSystemProperty(properties);
         String property = 
System.getProperty("org.springframework.ldap.core.keyCaseFold");
         Assertions.assertEquals("none", property);
+        val kylinConfig = getTestConfig();
+        kylinConfig.setProperty("spring.session.store-type", "jdbc");
+        configuration.discardJDBCCleanSessionProperties(kylinConfig);
+        String storeType = 
System.getProperty("spring.session.jdbc.cleanup-cron");
+        Assertions.assertEquals("-", storeType);
     }
 
     @Configuration

Reply via email to