google-cloud-php icon indicating copy to clipboard operation
google-cloud-php copied to clipboard

spanner: Keep idle sessions alive by periodically making a trivial query

Open skuruppu opened this issue 7 years ago • 9 comments

Is your feature request related to a problem? Please describe. Customers report that they need to do periodic "select 1" queries on sessions to keep them alive. As stated in the CreateSession documentation, "Cloud Spanner can delete sessions for which no operations are sent for more than an hour". The recommendation is to send "a trivial SQL query periodically". There's no support for this in the PHP client library as far as I can tell.

Describe the solution you'd like Periodically check sessions that are getting close to 1 hour of being idle, and send a trivial query to keep the session alive. This way customers don't have to do this manually. This logic is implemented in the Java client library.

skuruppu avatar Oct 15 '19 05:10 skuruppu

This feature requires us to add a long-running process to do the keep-alive logic, and if we implement it, it will require users to opt-in to use the feature. So it's not a trivial change to make.

@dwsupplee, do you think we should keep this issue open to consider adding this functionality or do you think we should just close it?

skuruppu avatar Oct 25 '19 00:10 skuruppu

This would be great to have, let's go ahead and keep it open 👍 .

dwsupplee avatar Oct 25 '19 01:10 dwsupplee

Adding a couple of public methods to ping a session and to maintain a session pool at any time is easy, but adding a long-running background task is a problem.

Unlike Java or Python, PHP does not have native threading support. There is a PHP extension pthreads allowing multithreading, which requires customized build of PHP. It could be used to create background tasks, but would it be feasible to automatically test this option?

ava12 avatar Feb 20 '20 16:02 ava12

PHP is synchronous and single-threaded by design. Web server creates a separate PHP environment for each request, and this environment is destroyed after response is served. This means that for PHP server creating background tasks like it is done in other languages is impossible. For long-running CLI scripts there are PHP extensions (pthreads, parallel) adding multithreading support, which require customized PHP build. Those extensions are hard to use, I cannot make reliable background task implementation (an advice from PHP guru proficient in multithreading stuff would be great). There are also a bunch of extensions for creating event loops, which don't look very helpful in this case. (None of those extensions are used in GC client lib).

In PHP world periodical tasks are usually performed by cron tasks, this approach is suitable in this case. CacheSessionPool already has support for custom caches and locks, the only thing missing is a method for detecting and refreshing idle sessions. Using this method anyone could easily write a cron task (or implement event loop for long-running script).

@skuruppu, @dwsupplee, what do you think? Would one extra method be enough?

ava12 avatar Feb 27 '20 13:02 ava12

@skuruppu, @dwsupplee, what do you think? Would one extra method be enough?

Sorry for the delay @ava12. I want to understand this properly. Is your proposal to create a Google\Cloud\Spanner\Session\CacheSessionPool::keepAlive() method then get the user to call it periodically, either directly or through a cron? I think we already have this method.

I suppose what you're proposing is more of a Google\Cloud\Spanner\Session\CacheSessionPool::keepSessionsAlive() where it would iterate over all the sessions in the cache and call keepAlive() if necessary.

Let me know if I misunderstood though.

skuruppu avatar Mar 05 '20 02:03 skuruppu

Well, keepAlive() method name is a bit misleading. It does not actually keep a session alive. When a session is acquire()d, it is moved from free session queue to in-use list, and when it is release()d, it is moved back to the queue. acquire() method searches for in-use sessions being idle for too long (20 minutes) - this may happen if someone for some reason forgot to release() a session - and moves those "orphaned" sessions to the queue. keepAlive() method just updates last activity time for used session, so it won't be collected as "orphaned" in next 20 minutes. This method does not send any requests to the server.

And yes, I propose a keepSessionsAlive() method that would check all (or some) sessions and "ping" them if needed. I guess I should describe it in detail in a design doc.

ava12 avatar Mar 05 '20 09:03 ava12

Added some details on possible solution.

ava12 avatar Mar 06 '20 15:03 ava12

Thanks @ava12. I added some comments in the document to discuss.

skuruppu avatar Mar 12 '20 09:03 skuruppu

Added better and more detailed solution.

ava12 avatar May 07 '20 17:05 ava12