Browse Source

Issue #10 - Add caching to quota totals calculation.

-> Merge branch '10'
Moonchild 3 years ago
parent
commit
908e9c3311
4 changed files with 81 additions and 12 deletions
  1. 3 0
      index.php
  2. 5 1
      setup.php
  3. 68 6
      weave_storage.php
  4. 5 5
      weave_utils.php

+ 3 - 0
index.php

@@ -280,6 +280,7 @@
 			if ($id)
 			{
 				$db->delete_object($collection, $id);
+				$db->clear_quota_usage($username);
 			}
 			else if ($collection)
 			{
@@ -293,12 +294,14 @@
 								$params['ids'],
 								$params['index_above'], $params['index_below']
 							);
+				$db->clear_quota_usage($username);
 			}
             else if($function == 'storage') // ich vermute mal storage reinigen
             {
                 if (!array_key_exists('HTTP_X_CONFIRM_DELETE', $_SERVER))
                      report_problem(WEAVE_ERROR_NO_OVERWRITE, 412);
                 $db->delete_storage($username);
+                $db->clear_quota_usage($username);
             }
 			else
 			{ 

+ 5 - 1
setup.php

@@ -165,6 +165,9 @@ function write_config_file($dbt, $dbh, $dbn, $dbu, $dbp, $fsRoot) {
     $cfg_content .= "    // set MinQuota and MaxQuota\n";
     $cfg_content .= "    define(\"MINQUOTA\", 30000);\n";
     $cfg_content .= "    define(\"MAXQUOTA\", 35000);\n";
+    $cfg_content .= "    // The setting below determines the time to live for quota totals\n";
+    $cfg_content .= "    // before recalculating how much database space has been used.\n";
+    $cfg_content .= "    define(\"QUOTA_TTL\", 3600);\n";
 
     $cfg_content .= "\n?>\n";
 
@@ -357,7 +360,8 @@ if ( $action == "step2" ) {
             $create_statement = " create table wbo ( username varchar(100), id varchar(65), collection varchar(100),
                  parentid  varchar(65), predecessorid int, modified real, sortindex int,
                  payload text, payload_size int, ttl int, primary key (username,collection,id))";
-            $create_statement2 = " create table users ( username varchar(255), md5 varchar(124), login int, 
+            $create_statement2 = " create table users ( username varchar(255), md5 varchar(124), login int,
+                 quota_usage int, usage_time int,
                  primary key (username)) ";
             $index1 = 'create index parentindex on wbo (username, parentid)';
             $index2 = 'create index predecessorindex on wbo (username, predecessorid)';

+ 68 - 6
weave_storage.php

@@ -629,21 +629,65 @@ class WeaveStorage
 
     function get_storage_total()
     {
+        $username = $this->_username;
+        $time = time();
+        
         try
         {
-            $select_stmt = 'select round(sum(length(payload))/1024) from wbo where username = :username';
+            $select_stmt = 'select quota_usage, usage_time from users where username = :username';
             $sth = $this->_dbh->prepare($select_stmt);
-            $username = $this->_username;
             $sth->bindParam(':username', $username);
             $sth->execute();
         }
         catch( PDOException $exception )
         {
-            error_log("get_storage_total: " . $exception->getMessage());
+            error_log("get_storage_total (user field): " . $exception->getMessage());
             throw new Exception("Database unavailable", 503);
         }
+        $result = $sth->fetch(PDO::FETCH_ASSOC);
+        if ($result['quota_usage'] != NULL &&
+            $result['usage_time'] != NULL &&
+            ((int)$result['quota_usage'] != 0) &&
+            ($time - (int)$result['usage_time'] < QUOTA_TTL)) {
+            # We have a usage size and it's recent enough; use cached value
+            return (int)$result['quota_usage'];
+        }
+        else
+        {
+            # We don't have a current cached value. Retrieve and store.
+            try
+            {
+                $select_stmt = 'select round(sum(length(payload))/1024) from wbo where username = :username';
+                $sth = $this->_dbh->prepare($select_stmt);
+                $sth->bindParam(':username', $username);
+                $sth->execute();
+            }
+            catch( PDOException $exception )
+            {
+                error_log("get_storage_total: " . $exception->getMessage());
+                throw new Exception("Database unavailable", 503);
+            }
+
+            $usage = (int)$sth->fetchColumn();
+
+            try
+            {
+                $update_stmt = 'update users set quota_usage = :usage, usage_time = :usage_time where username = :username';
+                $sth = $this->_dbh->prepare($update_stmt);
+                $sth->bindParam(':username', $username);
+                $sth->bindParam(':usage', $usage);
+                $sth->bindParam(':usage_time', $time);
+                // error_log("Store query: update users set quota_usage = ".$usage.", usage_time = ".$time." where username = ".$username);
+                $sth->execute();
+            }
+            catch( PDOException $exception )
+            {
+                error_log("get_storage_total (store): " . $exception->getMessage());
+                throw new Exception("Database unavailable", 503);
+            }
 
-        return (int)$sth->fetchColumn();
+            return $usage;
+        }
     }
 
     function get_collection_storage_totals()
@@ -750,15 +794,33 @@ class WeaveStorage
             return 0;
         }
         return 1;
-    }        
+    }       
 
+    function clear_quota_usage($username)
+    {
+        try
+        {
+            $update_statement = "update users set quota_usage = 0 where username = :username";
+            $sth = $this->_dbh->prepare($update_statement);
+            $sth->bindParam(':username', $username);
+            $sth->execute();
+        }
+        catch( PDOException $exception )
+        {
+            log_error("clear quota usage:" . $exception->getMessage());
+            return 0;
+        }
+        return 1;
+    }
+    
     function create_user($username, $password)
     {
         log_error("Create User - Username: ".$username."|".$password);
 
         try
         {
-            $create_statement = "insert into users (username, md5, login) values (:username, :md5, null)";
+            $create_statement = "insert into users (username, md5, login, quota_usage, usage_time)
+                                 values (:username, :md5, null, 0, 0)";
 
             $sth = $this->_dbh->prepare($create_statement);
             $hash = WeaveHashFactory::factory();

+ 5 - 5
weave_utils.php

@@ -55,15 +55,15 @@
 
 
     define ('LOG_THE_ERROR', 0);
-    define ('LOG_QUOTAS', 1);
+    define ('LOG_QUOTAS', 0);
 
     function log_quota($msg) 
     {   
         if ( LOG_QUOTAS == 1 ) 
         {
             $datei = fopen("/tmp/FSyncMS-quota.log","a");
-            $fmsg = sprintf("$msg\n");
-            fputs($datei,$fmsg);
+            // $fmsg = sprintf("$msg\n");
+            fputs($datei,"$msg\n");
             // fputs($datei,"Server ".print_r( $_SERVER, true));
             fclose($datei);
         }
@@ -74,8 +74,8 @@
         if ( LOG_THE_ERROR == 1 ) 
         {
             $datei = fopen("/tmp/FSyncMS-error.txt","a");
-            $fmsg = sprintf("$msg\n");
-            fputs($datei,$fmsg);
+            // $fmsg = sprintf("$msg\n");
+            fputs($datei,"$msg\n");
             // fputs($datei,"Server ".print_r( $_SERVER, true));
             fclose($datei);
         }