-1

I need to perform some medium load on shutdown but in AWS the machine is giving only 2 or less seconds to the thread to perform and only 45-47% of load is consume.

Is there is a way to increase this? or is nothing i can do!

Thread started.

07:22:09.476 [Thread-61] INFO  com.company.main.Main.lambda$run$0(307) - Shutdown process start with thread: [Thread-61]

And the last line.

07:22:11.026 [Thread-61] DEBUG c.p.w.company.lambda$sort$1(341)

As you can see like 1.5 seconds is giving to thread to finish and i need to do finish the method :(

We are using EC2!

Sorry if the question is plain but this is giving me nightmares.

The code is not mine but is something like this.

public class Main extends Thread {

In public static method we create some Threads.

for (int i = 0; i < threads; i++) {
      Main main = (Main) ctx.getBean("beanName");
      main.setName("main-" + i);
      main.start();
 }

The Main class run method override

@Override
public void run() {
    if (StringUtils.equals("main-0", Thread.currentThread().getName())) {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            doWork();
        }));
    }

As you can see we are creating the shutdown on the 1 Main thread created, i did created the shutdown on the main thread as well but the same thing happens.

chiperortiz
  • 4,751
  • 9
  • 45
  • 79
  • Where are you running it? Lambda? EC2? How are you shutting it down? – Michael Aug 02 '22 at 12:10
  • Hi @Michael EC2! And Amazon is shutting the machine! – chiperortiz Aug 02 '22 at 12:11
  • It would depend on how the runtime is being ended. Certain process signals (e.g. `SIGKILL`) and in-process calls (like `Runtime#halt`) will not reliably call shutdown hooks. You will likely need to look within AWS's documentation regarding how to handle this situation. – Rogue Aug 02 '22 at 12:19
  • According to some other threads I found, it should wait minutes or more, so I suspect this is the JVM rather than anything to do with AWS. Maybe your shutdown task is async so unblocks the shutdown thread? – Michael Aug 02 '22 at 12:20
  • The shutdown is called indeed but is not finish i dont know why! – chiperortiz Aug 02 '22 at 12:20
  • 2
    Include the code for your shutdown hook. – Rogue Aug 02 '22 at 12:40
  • It's important to include the actual code involved here (or at least the same methods in use). If that shutdown hook is merely starting a lot of threads, but never using `Thread#join` on them, then the shutdown hook thread will terminate without waiting for the other threads. – Rogue Aug 04 '22 at 13:02

1 Answers1

-2

Normally projects use "transactions" for this kind of cases. You can't really avoid a machine being shutted down, and sometimes you will not even get a notification. The only solution to reliable deal with this problem is to use ATOMIC operations aka "transactions"

There is no way to guarantee a shutdown hook always finish or even being fired at all, otherwise you could hang the shutdown process indefinitally in the shutdown hook. shutdown hook is to give you chance to shutdown gracefully - eg. notify other components of the system so they will not waste time on trying to connect, or to maybe try to save something, which might be usefull. But this can be used for kind of shutdown optimization to be as gracefull as possible, but it is not for relying on it for data or operation integrity. That's it, and if you will think about it it makes perfect sense.

Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
  • I need to do a code after shutdown is call but is not DB related. – chiperortiz Aug 02 '22 at 12:16
  • But what exactly you need to do after shutdown? – Krzysztof Cichocki Aug 02 '22 at 12:17
  • Not all operations can be made atomic, and this doesn't really apply to multiple operations which need to be performed on shutdown. – Rogue Aug 02 '22 at 12:20
  • Just because you can't always handle a shutdown cleanly (i.e. a crash) doesn't mean you *shouldn't ever try to*. – Michael Aug 02 '22 at 12:23
  • Store some statistics send a JSON which a lambda will consume that's all but there are some between 250-300 reports i need to process. each taking like 0.1 seconds – chiperortiz Aug 02 '22 at 12:31
  • @Rogue which operations can't be made atomic or transactional? – Krzysztof Cichocki Aug 02 '22 at 23:31
  • @Michael what to try? The application running is one big try already. And I didn't say that the code shouldn't do anything when received such event, just that it needs to be written in a way that it will shut down gracefully, and to me it looks like the solution here is to intruduce transcations, that's it. – Krzysztof Cichocki Aug 02 '22 at 23:42
  • @KrzysztofCichocki even if you make an operation atomic, if you have to do 250-300 of them, the overall work will not be atomic. Atomicity will not solve a runtime being cut off in the middle of work. – Rogue Aug 03 '22 at 13:31
  • @Rogue Surely, software cant force the computer to run when it has no power. But you are just talking to talk, you know you are wrong, but you just can't be honest. And if transactions do not solve this problem that a machine might stop working then what will solve this problem in your opinion? How do you think it is resolved in other projects? – Krzysztof Cichocki Aug 04 '22 at 11:20