You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by wu...@apache.org on 2023/06/14 01:49:55 UTC
[shardingsphere] branch master updated: Fix stop job before task starting (#26325)
This is an automated email from the ASF dual-hosted git repository.
wuweijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 939b91f6978 Fix stop job before task starting (#26325)
939b91f6978 is described below
commit 939b91f69789a0f82dc822b34d5924ba278c2946
Author: Hongsheng Zhong <zh...@apache.org>
AuthorDate: Wed Jun 14 09:49:48 2023 +0800
Fix stop job before task starting (#26325)
---
kernel/data-pipeline/api/pom.xml | 20 +++--
.../api/executor/AbstractLifecycleExecutor.java | 29 ++++---
.../executor/AbstractLifecycleExecutorTest.java | 91 ++++++++++++++++++++++
3 files changed, 121 insertions(+), 19 deletions(-)
diff --git a/kernel/data-pipeline/api/pom.xml b/kernel/data-pipeline/api/pom.xml
index fabff7433c8..2f3ebb900ee 100644
--- a/kernel/data-pipeline/api/pom.xml
+++ b/kernel/data-pipeline/api/pom.xml
@@ -34,6 +34,15 @@
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ </dependency>
+
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-test-fixture-jdbc</artifactId>
@@ -46,14 +55,11 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
-
<dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- </dependency>
- <dependency>
- <groupId>commons-codec</groupId>
- <artifactId>commons-codec</artifactId>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-test-util</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
</dependency>
</dependencies>
</project>
diff --git a/kernel/data-pipeline/api/src/main/java/org/apache/shardingsphere/data/pipeline/api/executor/AbstractLifecycleExecutor.java b/kernel/data-pipeline/api/src/main/java/org/apache/shardingsphere/data/pipeline/api/executor/AbstractLifecycleExecutor.java
index ac008c426aa..fbefb1b9f8a 100644
--- a/kernel/data-pipeline/api/src/main/java/org/apache/shardingsphere/data/pipeline/api/executor/AbstractLifecycleExecutor.java
+++ b/kernel/data-pipeline/api/src/main/java/org/apache/shardingsphere/data/pipeline/api/executor/AbstractLifecycleExecutor.java
@@ -17,9 +17,6 @@
package org.apache.shardingsphere.data.pipeline.api.executor;
-import lombok.AccessLevel;
-import lombok.Getter;
-import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import java.sql.SQLException;
@@ -27,6 +24,7 @@ import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
+import java.util.concurrent.atomic.AtomicReference;
/**
* Abstract lifecycle executor.
@@ -36,17 +34,20 @@ public abstract class AbstractLifecycleExecutor implements LifecycleExecutor {
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- @Getter(AccessLevel.PROTECTED)
- @Setter(AccessLevel.PROTECTED)
- private volatile boolean running;
-
- private volatile boolean stopped;
+ private final AtomicReference<Boolean> running = new AtomicReference<>(null);
private volatile long startTimeMillis;
+ protected boolean isRunning() {
+ Boolean running = this.running.get();
+ return null != running && running;
+ }
+
@Override
public final void start() {
- running = true;
+ if (null != running.get() || !running.compareAndSet(null, true)) {
+ return;
+ }
startTimeMillis = System.currentTimeMillis();
runBlocking();
}
@@ -58,7 +59,12 @@ public abstract class AbstractLifecycleExecutor implements LifecycleExecutor {
@Override
public final void stop() {
- if (stopped) {
+ Boolean running = this.running.get();
+ if (null == running) {
+ this.running.set(false);
+ return;
+ }
+ if (!running) {
return;
}
LocalDateTime startTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(startTimeMillis), ZoneId.systemDefault());
@@ -70,8 +76,7 @@ public abstract class AbstractLifecycleExecutor implements LifecycleExecutor {
// CHECKSTYLE:ON
log.warn("doStop failed", ex);
}
- running = false;
- stopped = true;
+ this.running.set(false);
}
protected abstract void doStop() throws SQLException;
diff --git a/kernel/data-pipeline/api/src/test/java/org/apache/shardingsphere/data/pipeline/api/executor/AbstractLifecycleExecutorTest.java b/kernel/data-pipeline/api/src/test/java/org/apache/shardingsphere/data/pipeline/api/executor/AbstractLifecycleExecutorTest.java
new file mode 100644
index 00000000000..c4aa4e05c19
--- /dev/null
+++ b/kernel/data-pipeline/api/src/test/java/org/apache/shardingsphere/data/pipeline/api/executor/AbstractLifecycleExecutorTest.java
@@ -0,0 +1,91 @@
+/*
+ * 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.shardingsphere.data.pipeline.api.executor;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class AbstractLifecycleExecutorTest {
+
+ @Test
+ void assertRunning() {
+ FixtureLifecycleExecutor executor = new FixtureLifecycleExecutor();
+ assertFalse(executor.isRunning());
+ executor.start();
+ assertTrue(executor.isRunning());
+ executor.stop();
+ assertFalse(executor.isRunning());
+ }
+
+ @Test
+ void assertStartRunOnce() {
+ FixtureLifecycleExecutor executor = new FixtureLifecycleExecutor();
+ executor.start();
+ executor.start();
+ assertThat(executor.runBlockingCount.get(), is(1));
+ }
+
+ @Test
+ void assertStopRunOnce() {
+ FixtureLifecycleExecutor executor = new FixtureLifecycleExecutor();
+ executor.start();
+ executor.stop();
+ executor.stop();
+ assertThat(executor.doStopCount.get(), is(1));
+ }
+
+ @Test
+ void assertNoStopBeforeStarting() {
+ FixtureLifecycleExecutor executor = new FixtureLifecycleExecutor();
+ executor.stop();
+ executor.stop();
+ assertThat(executor.doStopCount.get(), is(0));
+ }
+
+ @Test
+ void assertStopStart() {
+ FixtureLifecycleExecutor executor = new FixtureLifecycleExecutor();
+ executor.stop();
+ executor.start();
+ assertThat(executor.doStopCount.get(), is(0));
+ assertThat(executor.runBlockingCount.get(), is(0));
+ }
+
+ private static class FixtureLifecycleExecutor extends AbstractLifecycleExecutor {
+
+ private final AtomicInteger runBlockingCount = new AtomicInteger();
+
+ private final AtomicInteger doStopCount = new AtomicInteger();
+
+ @Override
+ protected void runBlocking() {
+ runBlockingCount.addAndGet(1);
+ }
+
+ @Override
+ protected void doStop() {
+ doStopCount.addAndGet(1);
+ }
+ }
+}