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

Custom ("user") session handler using database (ie MySQL) instead of "files" #1279

Closed
robocoder opened this Issue Apr 3, 2010 · 25 comments

Comments

Projects
None yet
3 participants
@robocoder
Copy link
Contributor

robocoder commented Apr 3, 2010

Use Zend_Session_SaveHandler_DbTable.

see: http://zendframework.com/manual/1.10/en/zend.session.savehandler.dbtable.html

For Installation, we would store session information in a signed cookie, and delete the cookie when the installation is complete.

Pros:

  • reduce number of inodes, eliminating tmp/sessions
  • move the session handling logic out of ./index.php (to FrontController init?)
  • potentially simplifies the session handling logic
  • no longer second guessing security of user's configuration
  • works with load balanced web servers that don't share session files
  • works when user doesn't have write access to shared session folder (technically, considered to be an improperly configured web server)

Cons:

  • cookies subject to 4K limit (only applies to installer and custom dashboard layout for anonymous)
  • slower (especially with garbage collection)
  • would have to rework LanguagesManager/API.php's use of Zend_Session_Namespace in getLanguageForSession() and setLanguageForSession()

Note: Piwik still requires write access to ./tmp (i.e., templates_c, cache/tracker, latest).

While you're at it, add a systemCheck for the 'session' extension, in case php was compiled with --disable-session.

And look into handling disabled ini_set() -- used by Zend_Session.

Oh, and make sure we handle the case where session.use_cookies = 0 (ref: http://forum.piwik.org/index.php?showtopic=11381)

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented Apr 25, 2010

(In [2121]) refs #1279 - refactor session initialization code out of index.php

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented Apr 25, 2010

(In [2122]) refs #1279 - fallback to system default

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented Jun 6, 2010

(In [2280]) refs #1279 - test that session.save_path is also readable, otherwise
session file cleanup may fail, e.g.,

Zend_Session_Exception: Zend_Session::start() - /PATH/libs/Zend/Session.php(Line:480): Error #8 session_start(): ps_files_cleanup_dir: opendir(/var/lib/php5) failed: Permission denied

@anonymous-piwik-user

This comment has been minimized.

Copy link

anonymous-piwik-user commented Jun 19, 2010

I've confirmed on my fresh install that this patch fixes the exception output. Thank you.

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented Aug 11, 2010

Reprioritizing. On the demo server, I noticed we have 285000+ session files in piwik/tmp/sessions -- most being empty files. That's a lot of inodes, and could be a problem for some users with shared hosting accounts.

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented Aug 14, 2010

Nice to have:

  • eliminate dependency on libs/Zend/Session and the session extension. (No more workarounds.)
@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented Dec 22, 2010

(In [3514]) refs #1279, refs #1910 - language preference for anonymous users is now stored in a cookie; we can't use this workaround for the dashboard layout because of cookie size limits

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented Dec 22, 2010

(In [3515]) refs #1279, refs #1910 - fix typo

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented Dec 27, 2010

Other suggestions:

  • not every request requires session data; propose not calling Zend_Session::start() in Piwik_Session; ie lazy init
  • Dashboard: set expiry for the anonymous user's non-persistent dashboard layout, eg $ns->setExpirationSeconds(1800);
@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented Apr 11, 2011

Using a DB table should avoid the potential race condition experienced with locked session files (see #2296).

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented May 22, 2011

(In [4761]) refs #1279 - add session table

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented May 22, 2011

(In [4762]) refs #1279 - add update script and bump version

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented May 22, 2011

(In [4763]) refs #1279 - fix typo

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented May 22, 2011

(In [4764]) refs #1279 - typo

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented May 22, 2011

(In [4765]) fixes #1279

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented May 22, 2011

CI shows the update fails:

[22-May-2011 19:05:51] PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'piwik_qa.canoo_session' doesn't exist' in /home/www/data/root/jenkins.private/jobs/Piwik/workspace/build/libs/Zend/Db/Statement/Pdo.php:228
Stack trace:
#0 /home/www/data/root/jenkins.private/jobs/Piwik/workspace/build/libs/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
#1 /home/www/data/root/jenkins.private/jobs/Piwik/workspace/build/libs/Zend/Db/Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)
#2 /home/www/data/root/jenkins.private/jobs/Piwik/workspace/build/libs/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#3 /home/www/data/root/jenkins.private/jobs/Piwik/workspace/build/libs/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('DESCRIBE `canoo...', Array)
#4 /home/www/data/root/jenkins.private/jobs/Piwik/workspace/build/core/Db/Adapter/Pdo/Mysql.php(211): Zend_Db_Adapter_Pdo_Abstract->query('DESCRIBE `canoo...', Array)
#5 /home/www/dat in /home/www/data/root/jenkins.private/jobs/Piwik/workspace/build/libs/Zend/Db/Statement/Pdo.php on line 234
@mattab

This comment has been minimized.

Copy link
Member

mattab commented May 22, 2011

Interesting!!
Questions:

  • Why move Piwik_Session::start to frontController instead of previous location in index.php?
  • Session purge: what is the default timeout after which sessions will be deleted from the DB to ensure the table doesn't grow out of control?

This is great feature because it will ensure that load balanced Piwik Reporting UI will work fine without requiring to "stick" same users to the same backend server!

@mattab

This comment has been minimized.

Copy link
Member

mattab commented May 22, 2011

(In [4766]) Refs #1279 Important to copy paste the table definitions in update files: this ensures that subsequent "ALTER" on these tables will work fine (otherwise, if we change table definition in myisam file in 1.8, and users upgrade from 1.4 to 1.9, the 1.5 upgrade will install the newest version of 1.8 tables which will then fail the 1.8 ALTER table on this table..)

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented May 22, 2011

I had to move the Piwik_Session::start() to front controller because the save handler needs the $db handle.

Purge: the frequency of cleanup and lifetime is defined in php.ini (e.g., session.gc_maxlifetime)

re: r4767 Good point. I'll go back and fix those cases in 0.2.10, 0.2.13, and 0.2.27, so we don't set a bad example. ;)

I'm just installing 1.0 now, and going through the auto-update to find out what's causing the problem in comment:30.

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented May 22, 2011

(In [4768]) refs #1279

@mattab

This comment has been minimized.

Copy link
Member

mattab commented May 22, 2011

on the update screen I see an error at the bottom, not important but if easy to hide would be nice :) (see attached file)

@mattab

This comment has been minimized.

Copy link
Member

mattab commented May 22, 2011

Attachment:
Piwik Update.png

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented May 23, 2011

(In [4772]) refs #1279

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented May 23, 2011

Yes, I saw the exception too. This is fixed in r4772. I'm just waiting for confirmation from Jenkins before I close this ticket.

@robocoder

This comment has been minimized.

Copy link
Contributor Author

robocoder commented May 23, 2011

Thank you, Jenkins.

@robocoder robocoder added this to the 1.5 - Piwik 1.5 milestone Jul 8, 2014

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment