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

TimeoutTimer thread pool max #755

Closed
shayaantx opened this issue Jul 21, 2018 · 6 comments
Closed

TimeoutTimer thread pool max #755

shayaantx opened this issue Jul 21, 2018 · 6 comments
Labels
Enhancement An enhancement to the driver. Lower priority than bugs.

Comments

@shayaantx
Copy link
Contributor

Hello,

I noticed the TimeoutTimer in older versions started a thread for every query that had a query timeout greater than 0. Then I saw in #150 someone had reported it. #162 fixed it.

Not sure if I'm missing something, but how is this problem fixed? In TimeoutTimer the executor service is using a cached thread pool via java's helper methods in ExecutorService. This creates a thread pool with a core pool size of 0 and a max pool size of Integer.MAX. If I'm reading that correctly, if I spawn 1k queries at once with timeouts, I will have 1k additional timeout threads (since no max on the thread pool exists). Seems like the problem still exists, you've just capped the max number of threads that could get created from unlimited to Integer.MAX by using a thread pool.

How come we can't do something like what the oracle jdbc driver does? They have one thread that all queries (that have a timeout) register with and that thread polls every second checking if they should interrupt each thread running the query?

Forgive me if I'm missing something with the original proposed fix

@ulvii
Copy link
Contributor

ulvii commented Jul 28, 2018

Hi @shayaantx ,

Please provide a repro code, we will investigate and get back to you.

@shayaantx
Copy link
Contributor Author

I can attempt to write some reproducible code, but I mean I think the latest code highlights the issue I described.

In the TDSCommand and TDSReader objects, you guys use the class TimeoutTimer (if users of the driver set a query timeout greater than 0). This class has two methods that use a static final ExecutorService that is created once via Executors.newCachedThreadPool(new ThreadFactory()). That specific implementation makes a thread pool that has a max size of Integer.MAX_VALUE, this type of thread pool will reuse threads if they are available, but since the max is such a large number you can run the pool up as many threads as you need.

So if I run 5k queries at once with 5k timeouts configured, depending on how fast each query executes, you could have 10k threads in use, instead of 5k. Obviously this is an extreme case, but the same problem would appear with 500 simultaneous queries configured with timeouts (which in my use cases, is normal amount of queries to run in parallel).

The design here of the query timeout logic seems inefficient. You are creating one timer thread for every query being run at any given time with a query timeout. Oracle doesn't have this problem, mysql has the same issue (https://github.com/mysql/mysql-connector-j/blob/1f61b0b0270d9844b006572ba4e77f19c0f230d4/src/com/mysql/jdbc/StatementImpl.java#L67).

I understand timeouts may be rarely used, but still seems like something worth fixing. The only workaround we implemented was just setting keep alive timeouts at the OS level.

@ulvii
Copy link
Contributor

ulvii commented Jul 31, 2018

Hi @shayaantx ,

I looked into this a bit and agree that in a multi-threaded environment (where every thread has its own connection) each query with timeout creates a new thread. I will label this as an enhancement, we will investigate what needs to be done to improve.

@ulvii ulvii added the Enhancement An enhancement to the driver. Lower priority than bugs. label Jul 31, 2018
@shayaantx
Copy link
Contributor Author

@ulvii Appreciate it

If I wanted to enhance it myself would you guys consider adding it to the main src?

@ulvii
Copy link
Contributor

ulvii commented Jul 31, 2018

Hi @shayaantx ,

Definitely, we appreciate contributions from the community. Please create a PR against the dev branch, the team will get it reviewed.

@ulvii
Copy link
Contributor

ulvii commented Nov 23, 2018

#782 is merged.

@ulvii ulvii closed this as completed Nov 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement An enhancement to the driver. Lower priority than bugs.
Projects
None yet
Development

No branches or pull requests

2 participants