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

Support queries automatically timing out after X seconds #1657

Closed
otoolep opened this issue Jan 31, 2024 · 11 comments · Fixed by #1666 or #1669
Closed

Support queries automatically timing out after X seconds #1657

otoolep opened this issue Jan 31, 2024 · 11 comments · Fixed by #1666 or #1669

Comments

@otoolep
Copy link
Member

otoolep commented Jan 31, 2024

Offer this as a query parameter. I think the following C call could work:

https://sqlite.org/c3ref/interrupt.html

Start a timer in Go, and if it fires, call this C function on the connection.

@otoolep
Copy link
Member Author

otoolep commented Jan 31, 2024

Ha, the underlying SQLite Go library already supports setting a timeout, and uses this method.

https://github.com/mattn/go-sqlite3/blob/4702d9b5d640f42488752f5cf70a195b748ffe96/sqlite3.go#L2071

Could be worth trying this out.

@mauri870
Copy link
Contributor

mauri870 commented Feb 2, 2024

I tried working on this and failed miserably. Maybe I'm trying to approach this the wrong way.

I implemented it in such a way that I can invoke QueryContext and ExecContext with a context.WithTimeout with the duration specified by the sql_timeout query parameter. Even tho the context reached the deadline those functions fail to return an error for it.

Any clues on what should be the proper way to implement this?

@otoolep
Copy link
Member Author

otoolep commented Feb 2, 2024

Can you show me your code?

@mauri870
Copy link
Contributor

mauri870 commented Feb 2, 2024

Sorry, I forgot to add the link to my reply, here it is

master...mauri870:rqlite:feature/sql-timeout

@otoolep
Copy link
Member Author

otoolep commented Feb 2, 2024

Even tho the context reached the deadline those functions fail to return an error for it.

You mean the deadline doesn't expire? If I was you I would focus on writing a db-level unit test first, to prove to yourself it works/doesn't work and debug that. You are on the right track, as I too believe it'll be a new field in the command.Request proto.

But focus on getting it working at the DB level solely via unit tests. And perhaps just focus on db.Query to start. Show me that failing.

@mauri870
Copy link
Contributor

mauri870 commented Feb 2, 2024

Thanks, I'll try that

@mauri870
Copy link
Contributor

mauri870 commented Feb 2, 2024

I added a test here master...mauri870:rqlite:feature/sql-timeout. I can confirm with the debugger that the context is indeed set properly with the deadline from context, but ExecContext does not respect that. I would expect a context.DeadlineExceeded to be returned.

@otoolep
Copy link
Member Author

otoolep commented Feb 2, 2024

I'm not sure I'm convinced by that test. That creation will run so fast, maybe it will return before runtime checks the timer, I don't know. Not saying the creation will take shorter than 1 nanosecond, just questioning if the runtime is good enough. Maybe you know.

I think a better test is to create 10,000 rows in the database, perhaps with a fair chunk of data in each row, and perform some slow query (say a few hundred milliseconds) and also set your 1 nanosecond time -- perhaps SELECT *. Then I think we've got something if that test still fails.

Have you checked the tests in the SQLite driver itself (mattn), to see if they test this?

@mauri870
Copy link
Contributor

mauri870 commented Feb 2, 2024

I tried with a couple thousand entries as well as a time.Sleep before the ExecContext (to make sure the context is indeed expired) but got the same results.

I found this test, which is pretty much what I want to write https://github.com/mattn/go-sqlite3/blob/4702d9b5d640f42488752f5cf70a195b748ffe96/sqlite3_go18_test.go#L162-L182.

I'll dig deeper into it later today.

@otoolep
Copy link
Member Author

otoolep commented Feb 3, 2024

Re-opening until I figure out why the integration test for this functionality is failing.

#1669

@otoolep otoolep reopened this Feb 3, 2024
@otoolep otoolep linked a pull request Feb 3, 2024 that will close this issue
@otoolep
Copy link
Member Author

otoolep commented Feb 3, 2024

The system testing helped improve this feature, I think we're good shape now. Thanks again @mauri870

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