From 990a29333e6a2000b1de2b4e14c6ff9b60210754 Mon Sep 17 00:00:00 2001 From: Leigh B Stoller Date: Mon, 17 Apr 2017 08:52:54 -0600 Subject: [PATCH] Start using the new general/export_active sitevar to control when we need to call exports_setup. --- db/Project.pm.in | 29 ++++++++++++++++----------- db/User.pm.in | 35 +++++++++++++++++++-------------- www/tbauth.php3 | 30 ++++++++++++++++------------ xmlrpc/sslxmlrpc_server.py.in | 37 +++++++++++++++++++++++++---------- 4 files changed, 82 insertions(+), 49 deletions(-) diff --git a/db/Project.pm.in b/db/Project.pm.in index 48e99f2ad..2a3949ea3 100644 --- a/db/Project.pm.in +++ b/db/Project.pm.in @@ -1,6 +1,6 @@ #!/usr/bin/perl -wT # -# Copyright (c) 2005-2016 University of Utah and the Flux Group. +# Copyright (c) 2005-2017 University of Utah and the Flux Group. # # {{{EMULAB-LICENSE # @@ -33,6 +33,7 @@ use vars qw(@ISA @EXPORT); use libdb; use libtestbed; use Brand; +use libEmulab; use User; use Group; use English; @@ -950,6 +951,9 @@ sub UpdateExports($) return 0 if (! ($WITHZFS && ($ZFS_NOEXPORT || !$WITHAMD))); + my $exports_limit = GetSiteVar("general/export_active"); + return 0 + if (!$exports_limit); my $query_result = DBQueryWarn("select UNIX_TIMESTAMP(last_activity) from project_stats ". @@ -959,14 +963,15 @@ sub UpdateExports($) if (!$query_result->numrows); my ($lastactivity) = $query_result->fetchrow_array(); - - if (!defined($lastactivity) || - time() - $lastactivity > (24 * 3600)) { - # Update last_activity first so exports_setup will do something. - DBQueryWarn("update project_stats set last_activity=now() ". - "where pid_idx='$pid_idx'") - or return -1; + # Update last_activity first so exports_setup will do something + # and to mark the project as active, to keep the mount active. + DBQueryWarn("update project_stats set last_activity=now() ". + "where pid_idx='$pid_idx'") + or return -1; + + if (!defined($lastactivity) || + time() - $lastactivity > ((($exports_limit * 24) - 12) * 3600)) { if ($ZFS_NOEXPORT) { mysystem($EXPORTS_SETUP); } @@ -975,9 +980,11 @@ sub UpdateExports($) } # failed, reset the timestamp - if ($? && defined($lastactivity)) { - DBQueryWarn("update project_stats set ". - " last_activity=FROM_UNIXTIME($lastactivity) ". + if ($?) { + my $set = (defined($lastactivity) ? + "FROM_UNIXTIME($lastactivity)" : "null"); + + DBQueryWarn("update project_stats set last_activity=$set ". "where pid_idx='$pid_idx'"); return -1; } diff --git a/db/User.pm.in b/db/User.pm.in index f5b3c9ec3..c9545f7ac 100644 --- a/db/User.pm.in +++ b/db/User.pm.in @@ -31,6 +31,7 @@ use vars qw(@ISA); use emdb; use EmulabConstants; use libtestbed; +use libEmulab; use Brand; use English; use Data::Dumper; @@ -2270,25 +2271,27 @@ sub UpdateExports($) return 0 if (! ($WITHZFS && ($ZFS_NOEXPORT || !$WITHAMD))); + my $exports_limit = GetSiteVar("general/export_active"); + return 0 + if (!$exports_limit); my $query_result = - DBQueryWarn("select UNIX_TIMESTAMP(weblogin_last) from users as u ". - "left join user_stats as s on s.uid_idx=u.uid_idx ". - "where u.uid_idx='$uid_idx'"); + DBQueryWarn("select UNIX_TIMESTAMP(last_activity) from user_stats ". + "where uid_idx='$uid_idx'"); # Hmm. return 0 if (!$query_result->numrows); - my ($lastlogin) = $query_result->fetchrow_array(); - - if (!defined($lastlogin) || - time() - $lastlogin > (24 * 3600)) { - # Update weblogin_last first so exports_setup will do something. - DBQueryWarn("update user_stats set ". - " weblogin_last=now() ". - "where uid_idx='$uid_idx'") - or return -1; + my ($last_activity) = $query_result->fetchrow_array(); + # Always update database so exports_setup will do something, + # and to mark activity to keep mount active. + DBQueryWarn("update user_stats set last_activity=now() ". + "where uid_idx='$uid_idx'") + or return -1; + + if (!defined($last_activity) || + time() - $last_activity > ((($exports_limit * 24) - 12) * 3600)) { if ($ZFS_NOEXPORT) { mysystem($EXPORTS_SETUP); } @@ -2296,9 +2299,11 @@ sub UpdateExports($) mysystem($EXPORTS_SETUP . " -B"); } # failed, reset the timestamp - if ($? && defined($lastlogin)) { - DBQueryWarn("update user_stats set ". - " weblogin_last=FROM_UNIXTIME($lastlogin) ". + if ($?) { + my $set = (defined($last_activity) ? + "FROM_UNIXTIME($last_activity)" : "null"); + + DBQueryWarn("update user_stats set last_activity=$set ". "where uid_idx='$uid_idx'"); return -1; } diff --git a/www/tbauth.php3 b/www/tbauth.php3 index 97e9fb433..0be75decd 100644 --- a/www/tbauth.php3 +++ b/www/tbauth.php3 @@ -1042,32 +1042,36 @@ function DOLOGIN_MAGIC($uid, $uid_idx, $email = null, # every time the user logs in of course, and since exports_setup is # using one week as its threshold, we can use that as the limit. # - if ($WITHZFS && $ZFS_NOEXPORT) { + $exports_active = TBGetSiteVar("general/export_active"); + + if ($WITHZFS && $ZFS_NOEXPORT && $exports_active) { + $limit = (($exports_active * 24) - 12) * 3600; + $query_result = - DBQueryFatal("select UNIX_TIMESTAMP(weblogin_last),weblogin_last ". + DBQueryFatal("select UNIX_TIMESTAMP(last_activity),last_activity ". " from users as u ". "left join user_stats as s on s.uid_idx=u.uid_idx ". "where u.uid_idx='$uid_idx' and ". " u.nonlocal_id is null"); + + # Update last_activity first so exports_setup will do something + # and to mark activity to keep the mount active. + DBQueryFatal("update user_stats set last_activity=now() ". + "where uid_idx='$uid_idx'"); + if (mysql_num_rows($query_result)) { - $lastrow = mysql_fetch_row($query_result); - $lastlogin = $lastrow[0]; - $lastloginstr = $lastrow[1]; + $lastrow = mysql_fetch_row($query_result); + $lastactive = $lastrow[0]; + $lastactivestr = $lastrow[1]; - if (time() - $lastlogin > (24 * 3600 * 6)) { - # Update weblogin_last first so exports_setup - # will do something. - DBQueryFatal("update user_stats set ". - " weblogin_last=now() ". - "where uid_idx='$uid_idx'"); - + if (time() - $lastactive > $limit) { $rv = SUEXEC("nobody", "nobody", "webexports_setup", SUEXEC_ACTION_IGNORE); # failed, reset the timestamp if ($rv) { DBQueryFatal("update user_stats set ". - " weblogin_last='$lastloginstr' ". + " last_activity='$lastactivestr' ". "where uid_idx='$uid_idx'"); SUEXECERROR(SUEXEC_ACTION_DIE); return; diff --git a/xmlrpc/sslxmlrpc_server.py.in b/xmlrpc/sslxmlrpc_server.py.in index a7bdd18ce..7bc648df9 100755 --- a/xmlrpc/sslxmlrpc_server.py.in +++ b/xmlrpc/sslxmlrpc_server.py.in @@ -1,6 +1,6 @@ #!/usr/local/bin/python # -# Copyright (c) 2005-2016 University of Utah and the Flux Group. +# Copyright (c) 2005-2017 University of Utah and the Flux Group. # # {{{EMULAB-LICENSE # @@ -24,6 +24,7 @@ import sys import getopt import os, os.path +import time import pwd import traceback import syslog @@ -312,6 +313,8 @@ class MyServer(SSL.ForkingSSLServer, SimpleXMLRPCDispatcher): pass def setupuser(self, request, client): + exports_active = TBGetSiteVar("general/export_active"); + subject = request.get_peer_cert().get_subject() if self.debug: self.logit(str(subject)) @@ -372,16 +375,30 @@ class MyServer(SSL.ForkingSSLServer, SimpleXMLRPCDispatcher): # We have to make sure the exports are done, since the user might # not be using the web interface at all. # - if WITHZFS and ZFS_NOEXPORT: - res = DBQueryFatal("select last_activity from user_stats as s " - "where uid_idx=%s and " - " last_activity < now() - (24 * 3600) ", + if WITHZFS and ZFS_NOEXPORT and exports_active: + limit = ((int(exports_active) * 24) - 12) * 3600 + + res = DBQueryFatal("select UNIX_TIMESTAMP(last_activity) " + " from user_stats " + "where uid_idx=%s", (str(self.uid_idx),)) - # Do not do this every time. - if len(res): - DBQueryFatal("update user_stats set last_activity=now() " - "where uid_idx=%s ", - (str(self.uid_idx),)) + + last_activity = int(res[0][0]) + + self.logit("%s: limit,last_activity for %s,%d: %d,%d,%d" % + (client[0], self.uid, self.uid_idx, + limit, last_activity, int(time.time()))) + + # Always update weblogin_last so exports_setup will do something, + # and to mark activity to keep mount active. + DBQueryFatal("update user_stats set last_activity=now() " + "where uid_idx=%s", + (str(self.uid_idx),)) + + if time.time() - last_activity > limit: + self.logit("%s: calling exports_setup for %s,%d" % + (client[0], self.uid, self.uid_idx)) + if os.system(TBDIR + "/sbin/exports_setup"): raise Exception("exports_setup failed") pass -- GitLab