Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to cancel a task scheduled at fixed rate #5143

Closed
zcxsythenew opened this issue Jul 3, 2023 · 4 comments
Closed

Unable to cancel a task scheduled at fixed rate #5143

zcxsythenew opened this issue Jul 3, 2023 · 4 comments

Comments

@zcxsythenew
Copy link
Contributor

Expected behavior Task should be canceled

Actual behavior future.cancel(true); returns true, but the task still exists in Redis and will be executed again.

Steps to reproduce or test case

Run the following code:

        RScheduledFuture<?> future = redissonClient
                .getExecutorService(TestConstants.NAME)
                .scheduleAtFixedRate(new TestRunnable(), 0, 1, TimeUnit.MINUTES);
        Thread.sleep(TimeUnit.SECONDS.toMillis(1));
        return future.cancel(true);

Where the code of TestRunnable is as the following:

package com.example.test;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@Slf4j
public class TestRunnable implements Runnable {

    @Override
    public void run() {
        log.info("Running!");
        try {
            Thread.sleep(TimeUnit.SECONDS.toMillis(50));
        } catch (Exception e) {
            log.info("Cancel done with Exception {}", e.getClass().getSimpleName());
            return;
        }
        throw new RuntimeException("timeout", new TimeoutException());
    }
}

The first execution is at 11:54:42.296 (and then the task is canceled)

The second execution is at 11:55:42.378

The third execution is at 12:00:42.478

Execution Time
1 11:54:42.296
2 11:55:42.378
3 12:00:42.478
4 12:05:42.582

Redis version 7.0.11 (Docker latest image by July 3, 2023)

Redisson version 3.22.1

Redisson configuration Defaults

The full log of the spring boot (2.7.13) application is pasted below.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::               (v2.7.13)

2023-07-03 11:54:29.440  INFO 39917 --- [           main] .e.t.RedissonSelfCancellationApplication : Starting RedissonSelfCancellationApplication using Java 11.0.19 on wguozhangVJGH6.vmware.com with PID 39917 (/Users/wguozhang/Documents/redisson-self-cancellation/target/classes started by wguozhang in /Users/wguozhang/Documents/redisson-self-cancellation)
2023-07-03 11:54:29.448  INFO 39917 --- [           main] .e.t.RedissonSelfCancellationApplication : No active profile set, falling back to 1 default profile: "default"
2023-07-03 11:54:31.208  INFO 39917 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-07-03 11:54:31.221  INFO 39917 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-07-03 11:54:31.221  INFO 39917 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.76]
2023-07-03 11:54:31.389  INFO 39917 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-07-03 11:54:31.390  INFO 39917 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1857 ms
2023-07-03 11:54:31.635  INFO 39917 --- [           main] org.redisson.Version                     : Redisson 3.22.1
2023-07-03 11:54:31.712  WARN 39917 --- [           main] i.n.r.d.DnsServerAddressStreamProviders  : Can not find io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider in the classpath, fallback to system defaults. This may result in incorrect DNS resolutions on MacOS. Check whether you have a dependency on 'io.netty:netty-resolver-dns-native-macos'
2023-07-03 11:54:31.976  INFO 39917 --- [isson-netty-2-4] o.r.c.pool.MasterPubSubConnectionPool    : 1 connections initialized for 127.0.0.1/127.0.0.1:6379
2023-07-03 11:54:32.286  INFO 39917 --- [sson-netty-2-19] o.r.c.pool.MasterConnectionPool          : 24 connections initialized for 127.0.0.1/127.0.0.1:6379
2023-07-03 11:54:33.000  INFO 39917 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-07-03 11:54:33.016  INFO 39917 --- [           main] .e.t.RedissonSelfCancellationApplication : Started RedissonSelfCancellationApplication in 4.233 seconds (JVM running for 5.679)
2023-07-03 11:54:41.943  INFO 39917 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-07-03 11:54:41.944  INFO 39917 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-07-03 11:54:41.946  INFO 39917 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms
2023-07-03 11:54:42.296  INFO 39917 --- [   redisson-3-4] com.example.test.TestRunnable            : Running!
2023-07-03 11:54:45.334  INFO 39917 --- [   redisson-3-4] com.example.test.TestRunnable            : Cancel done with Exception InterruptedException
2023-07-03 11:55:42.378  INFO 39917 --- [   redisson-3-6] com.example.test.TestRunnable            : Running!
2023-07-03 11:56:32.394 ERROR 39917 --- [   redisson-3-6] o.r.e.RedissonExecutorRemoteService      : Can't execute: RemoteServiceRequest [requestId=c408df3e3c46e5b228fe1d76d4a304cf, methodName=scheduleAtFixedRate, signature=[2954431956196527280, 5708544082777223948], args=[org.redisson.executor.params.ScheduledAtFixedRateParameters@2d01724e], options=RemoteInvocationOptions[ackTimeoutInMillis=null, executionTimeoutInMillis=null], date=1688356485347]

java.lang.reflect.InvocationTargetException: null
	... <omitted by issue reporter>
Caused by: java.lang.RuntimeException: timeout
	at com.example.test.TestRunnable.run(TestRunnable.java:20) ~[classes/:na]
	at org.redisson.executor.TasksRunnerService.executeRunnable(TasksRunnerService.java:341) ~[redisson-3.22.1.jar:3.22.1]
	at org.redisson.executor.TasksRunnerService.scheduleAtFixedRate(TasksRunnerService.java:126) ~[redisson-3.22.1.jar:3.22.1]
	... 12 common frames omitted
Caused by: java.util.concurrent.TimeoutException: null
	... 15 common frames omitted

2023-07-03 12:00:42.478  INFO 39917 --- [   redisson-3-8] com.example.test.TestRunnable            : Running!
2023-07-03 12:01:32.483 ERROR 39917 --- [   redisson-3-8] o.r.e.RedissonExecutorRemoteService      : Can't execute: RemoteServiceRequest [requestId=c408df3e3c46e5b228fe1d76d4a304cf, methodName=scheduleAtFixedRate, signature=[2954431956196527280, 5708544082777223948], args=[org.redisson.executor.params.ScheduledAtFixedRateParameters@332303f5], options=RemoteInvocationOptions[ackTimeoutInMillis=null, executionTimeoutInMillis=null], date=1688356485347]

java.lang.reflect.InvocationTargetException: null
	... <omitted by issue reporter>
Caused by: java.lang.RuntimeException: timeout
	at com.example.test.TestRunnable.run(TestRunnable.java:20) ~[classes/:na]
	at org.redisson.executor.TasksRunnerService.executeRunnable(TasksRunnerService.java:341) ~[redisson-3.22.1.jar:3.22.1]
	at org.redisson.executor.TasksRunnerService.scheduleAtFixedRate(TasksRunnerService.java:126) ~[redisson-3.22.1.jar:3.22.1]
	... 12 common frames omitted
Caused by: java.util.concurrent.TimeoutException: null
	... 15 common frames omitted

2023-07-03 12:05:42.582  INFO 39917 --- [  redisson-3-10] com.example.test.TestRunnable            : Running!
2023-07-03 12:06:32.588 ERROR 39917 --- [  redisson-3-10] o.r.e.RedissonExecutorRemoteService      : Can't execute: RemoteServiceRequest [requestId=c408df3e3c46e5b228fe1d76d4a304cf, methodName=scheduleAtFixedRate, signature=[2954431956196527280, 5708544082777223948], args=[org.redisson.executor.params.ScheduledAtFixedRateParameters@705664bb], options=RemoteInvocationOptions[ackTimeoutInMillis=null, executionTimeoutInMillis=null], date=1688356485347]

java.lang.reflect.InvocationTargetException: null
	... <omitted by issue reporter>
Caused by: java.lang.RuntimeException: timeout
	at com.example.test.TestRunnable.run(TestRunnable.java:20) ~[classes/:na]
	at org.redisson.executor.TasksRunnerService.executeRunnable(TasksRunnerService.java:341) ~[redisson-3.22.1.jar:3.22.1]
	at org.redisson.executor.TasksRunnerService.scheduleAtFixedRate(TasksRunnerService.java:126) ~[redisson-3.22.1.jar:3.22.1]
	... 12 common frames omitted
Caused by: java.util.concurrent.TimeoutException: null
	... 15 common frames omitted
@zcxsythenew
Copy link
Contributor Author

I find out that if I re-throw the exception in TestRunnable, the task can be successfully canceled, which means I have to add annotation @SneakyThrows above the run function. Is it necessary to re-throw the InterruptedException in a Runnable in order to cancel a task?

@mrniko
Copy link
Member

mrniko commented Jul 3, 2023

That's correct InterruptedException should be thrown.

@mrniko mrniko closed this as completed Jul 3, 2023
@zcxsythenew
Copy link
Contributor Author

Why don't we need to re-throw the exception when the task is scheduled with fixed delay?

@zcxsythenew
Copy link
Contributor Author

@mrniko

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants