Chat Logs

  1. enoqso I asked about virtual threads (spring) and parallel http requests a couple days ago; turns out by default it's not parallelized (which is what I expected) https://dpaste.com/EXBWBMSAN
  2. enoqwhen I curl the endpoint, it waits for 20s
  3. enoqwhat's the recommended way to deal with that? wrap in CompletableFuture.supplyAsync?
  4. enoq(not 100% sure if that approach loses ThreadLocal context)
  5. enoqthere's also @Async btw
  6. Parahuh, my firewall says dpaste is a dangerous website
  7. enoq~pastebin list options
  8. javabotenoq, what does that even *mean*?
  9. enoq~pastebin
  10. javabotPlease paste your code and any errors online. For runnable classes, try https://ideone.com/ . For general code and errors, use https://gist.github.com or https://dpaste.org/
  11. nevetDiscover gists
  12. enoqdpaste.org is down
  13. enoqhttps://gist.github.com/BernhardPosselt/a5638b26987519fdb60b2fdd414f5443
  14. nevetProductController.kt
  15. ParaSo, it does exactly what it should?
  16. enoqright, I think we had another misunderstanding back then
  17. enoqvirtual threads don't parallelize IO in the same thread
  18. ParaI mean... you've literally written "wait 10 seconds, then wait 10 seconds" :)
  19. enoqsince I'll be aggregating a lot of different rest endpoints, this makes me wonder if I should go reactive and maybe Kotlin coroutines
  20. ParaThat sounds like quite a leap to conclusion. You've written synchronous code, that will not magically turn into asynchronous.
  21. enoqspring.threads.virtual.enabled=true btw
  22. enoqso it will spin off a virtual thread per request
  23. enoqso it should be async, just not parallel
  24. ParaYou're solving the wrong problem. You need to change your programming pattern if you want async/parallel execution.
  25. enoqright, so this is how I understand it you could do it using servlet tech https://gist.github.com/BernhardPosselt/a5638b26987519fdb60b2fdd414f5443
  26. nevetProductController.kt
  27. enoqbasically use CompletableFuture.supplyAsync
  28. enoqjust not 100% sure if this makes sense
  29. ParaI don't see how "servlet tech" relates to this at all.
  30. ParaBut yeah, that's what futures are for.
  31. enoqservlet as in: not reactive style
  32. dreamrealenoq: did you turn on virtual threads, and you're seeing *subsequent* invocation?
  33. enoqI did turn it on, yes
  34. enoqThread.currentThread().isVirtual() is true
  35. enoqand they're happening one after another
  36. dreamrealokay, so let me make sure what's being described: you have AN ENDPOINT, and you're calling it twice, AND it's executing sequentially such that a 10s call is taking 2(10)s, not 10s twice
  37. dreamrealyou would expect *some* deviation (the time between the "two calls" being started plus a slight deviation for wire transfer time) but those would be pretty small
  38. enoqI tried to simulate 2 REST endpoints that each take 10s to respond; I want both endpoints to be called in parallel to reduce it from 20s to 10s; later on I'll need to aggregate data from various different services
  39. enoqif I used Thread.sleep() wrong, let me know
  40. dreamrealenoq: You shouldn't see sequential behavior from those servlets *even without* virtual threads
  41. dreamrealsomething else is going on
  42. enoqI'm calling the /testwait endpoint btw
  43. enoqfrom my understanding, Thread.sleep in a virtual thread should suspend, correct?
  44. dreamrealshould suspend THAT THREAD, yes
  45. dreamrealbut even without virtual threads you shouldn't be seeing one-after-the-other execution
  46. dreamrealOH
  47. dreamrealtestwait() is what's blocking, yeah?
  48. enoqyes
  49. dreamrealthose ARE sequential for that thread
  50. dreamrealyou're not executing two calls at the same time
  51. enoqright, that's what I was trying to communicate, we probably talked by each other
  52. dreamrealno worries, I'm in a meeting and attention is spread
  53. dreamrealso this behavior is *correct* for that code
  54. enoqI don't care too much about being able to serve 100k requests at once, I'm more interested in cutting down on sequential async calls
  55. dreamrealyou run one, then you run the other: 10s twice. That's correct and expected.
  56. dreamrealYou're not running those calls at the same time.
  57. dreamrealYour *code* doesn't run them at the same time.
  58. dreamrealcreate a list of completion results, then resolve them.
  59. enoqright, so would you still stick with servlet if that was the main thing your application did and sprinkle in CompletableFutures?
  60. enoqmy guess is that many requests can't be parallel because they'll depend on previous requests, but there might a good chunk that could
  61. dreamrealyeah, you're literally needing to spread out the calls, this is where you actually DO get to multiplex requests
  62. dreamrealIt's not DIFFICULT: create a set of requests and start them, then collect the results
  63. sambalala -+\
  64. dreamrealI actually disagree
  65. ernimril^~!
  66. dreamrealno, no, you *!@
  67. dreamrealThou offendsth me
  68. NeXeNconcurrency != parallel
  69. NeXeNenoq: and don't fall into the forkjoinpool trap. always lean on your StructuredTaskScope and ExecutorService https://download.java.net/java/early_access/loom/docs/api/java.base/java/util/concurrent/StructuredTaskScope.html and https://download.java.net/java/early_access/loom/docs/api/java.base/java/util/concurrent/ExecutorService.html btw that's loom docs. don't try and use a spliterator or something or you'll end up waiting 10 seconds blocking
  70. NeXeNthe rest of the world
  71. javabotNeXeN's titles: "StructuredTaskScope (Java SE 25 & JDK 25 [build 1])" | "ExecutorService (Java SE 25 & JDK 25 [build 1])"
  72. nevethttps://download.java.net/java/early_access/loom/docs/api/java.base/java/util/concurrent/StructuredTaskScope.html "StructuredTaskScope (Java SE 25 & JDK 25 [build 1])" || https://download.java.net/java/early_access/loom/docs/api/java.base/java/util/concurrent/ExecutorService.html "ExecutorService (Java SE 25 & JDK 25 [build 1])"