Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 10-26-2008, 01:06 PM
zanis zanis is offline
 
Join Date: Nov 2005
Posts: 9
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Difference between $vbulletin->db and $db->

Hello all,

I think the code below which is within a plugin crashed my server! I was wondering if it was due to the use of $vbulletin->db and $db in the same function?

For example:

Code:
$search_sql = "SELECT threadid, title, lastpost, forumid,
 replycount, dateline, postuserid, visible, open
FROM " . TABLE_PREFIX . "thread AS thread
WHERE NOT ISNULL(threadid) AND visible = '1' AND replycount > '0' AND open!='10' AND forumid IN (".$forums_to_search.") $mp_stats_timecut_activethreads
ORDER BY replycount DESC
LIMIT 0, $num_of_threads_to_display";
				
$active_threads_result = $vbulletin->db->query_read($search_sql);
		
$threadcount = $db->num_rows($active_threads_result);
		
# now get the results and add to an array
while ($thread_result = $db->fetch_array($active_threads_result)){
......
		{
mp_stats_timecut_activethreads ends up being 1 day back in time stamp - again the code worked well and returned the correct results. The forum is also new so in total we are looking at 200 posts and 10 new ones in 10 days.

I copied the sql and code bits from a well known plugin mentioned in this forum and it worked well when I was the only one calling the plugin however when I added the plugin to the forum home template and accessible to the public my server used all its memory and swap and had to be rebooted. There are two code blocks in the plugin which execute SQL for stats (the other is along the same lines).

I am unsure of why there are two access points used for the db calls ($vbulletin->db, $db) and I wonder if this is actually quite bad?

For example wouldn't you go:

Quote:
while ($thread_result = $vbulletin->db->fetch_array($active_threads_result)){
instead of:

Quote:
while ($thread_result = $db->fetch_array($active_threads_result)){
As I assume $vbulletin-> is actually controlling the access to the db for this instance of my code running? And I wouldn't be supprised that this in a site with say a few hundred visitors would cause issues with db sql execution etc...

Cheers

Marc
Reply With Quote
  #2  
Old 10-27-2008, 05:30 AM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
$db =& $vbulletin->db
Reply With Quote
  #3  
Old 10-27-2008, 05:52 PM
zanis zanis is offline
 
Join Date: Nov 2005
Posts: 9
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hello Dismounted,

Thank you for your reply.

What I failed to mention in my first post is that the code is within a function and I have declared the following:

Quote:
function get_results(){
global $vbulletin,$db
...
Maybe its due to the code using two database objects that my server crashed trying to execute this code?

Cheers

Marc
Reply With Quote
  #4  
Old 10-27-2008, 09:01 PM
Guest190829
Guest
 
Posts: n/a
Default

No, that code bit would not crash your server [necessarily]. We need to see the full plugin to evaluate it, there is also the possibility that the function is being run to many times causing the crash. There are a lot of possibilities.
Reply With Quote
  #5  
Old 10-27-2008, 09:25 PM
zanis zanis is offline
 
Join Date: Nov 2005
Posts: 9
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hello Danny.VBT,

The plugin consists of code added to the plugin manager and a set of files stored outside the document root of the web site. The plugin code instantiates a class which does a few fews - one of them is to execute SQL against the forum db. Its a large class file so I have highlighted the main points below:

1. Uses pears cache_lite class - so when the plugin is called and the class loads it checks to see if the data is cached already. if so return the cache value if not continue with the data collection. I used cache_lite as I am told it has good file management (locking) etc..

2) The class returns HTML data that the plugin then echos out.

Below is the function in full that when added to the class called by the plugin the server crashed. Not sure if this did the damage but it was the last update. I also killed the cache files as well by deleting them. And the plugin code.

Quote:
function mp_getMostActiveThreads(){

global $vbulletin,$db;

########## NOTES ###########
# There is no caching on getting the thread starter name and forum title via function calls fetch_userinfo() and fetch_foruminfo() however both seem to be using caching anyway.
##########################

$this->the_logger->write_log("debug","mp_getMostActiveThreads() called");

if($this->mp_isActiveThreadCache() == TRUE){
$this->the_logger->write_log("debug","mp_getMostActiveThreads:: the threads are in the caching so using that copy.");
return true;
}else{
$this->the_logger->write_log("debug","mp_getMostActiveThreads:: the threads are NOT in the cache so executing database search.");
}

$forums_to_search = "1,2,3,4,9,10,11,18,22,23,24,25,26,27,28,29";
$num_of_threads_to_display = 10;
$num_days_to_search_back = 0; # since BT is new we will go back 5 days to locate new threads
$date_format = "d-m";
$trimthreadtitle = "60"; //cut title off at 60 chars

if(!is_object($vbulletin)){
$this->the_logger->write_log("error","mp_getMostActiveThreads:: vbulletin is not an object. Terminating this functions work.");
return false;
}

if($num_days_to_search_back > 0){
$mp_stats_timecut_start = TIMENOW - ($num_days_to_search_back * 86400);
$mp_stats_timecut_activethreads = "AND dateline > $mp_stats_timecut_start";
}else{
# Note this means the entire db will be searched!!
$mp_stats_timecut_activethreads = "";
}

$search_sql = "SELECT threadid, title, lastpost, forumid, replycount, dateline, postuserid, visible, open
FROM " . TABLE_PREFIX . "thread AS thread
WHERE NOT ISNULL(threadid) AND visible = '1' AND replycount > '0' AND open!='10' AND forumid IN (".$forums_to_search.") $mp_stats_timecut_activethreads
ORDER BY replycount DESC
LIMIT 0, $num_of_threads_to_display";

$this->the_logger->write_log("debug","mp_getMostActiveThreads:: SQL to execute:: ".$search_sql);

$get_stats_hottest = $vbulletin->db->query_read($search_sql);

$threadcount = $db->num_rows($get_stats_hottest);

$this->the_logger->write_log("debug","mp_getMostActiveThreads:: Number of threads:: ".$threadcount);

# now get the results and add to an array
while ($get_hottest = $db->fetch_array($get_stats_hottest))
{
$get_hottest[fullthreadtitle] = strip_tags($get_hottest[title]);
if ($trimthreadtitle > 0)
{
$get_hottest[titletrimmed] = fetch_trimmed_title($get_hottest[fullthreadtitle], $trimthreadtitle,true);
}
else
{
$get_hottest[titletrimmed] = $get_hottest[fullthreadtitle];
}
if ($get_hottest[lastpost] > $vbulletin->userinfo['lastvisit'])
{
$get_hottest[newpost] = true;
}

$userinfo = fetch_userinfo($get_hottest[postuserid]);
$get_news_postdate = vbdate($date_format, $get_hottest[dateline]);
$foruminfo = fetch_foruminfo($get_hottest[forumid]);

$get_hottest[postusername] = $userinfo['username'];
$get_hottest[forumname] = $foruminfo['title'];
$get_hottest[threaddate] = $get_news_postdate;

$this->the_logger->write_log("debug","mp_getMostActiveThreads:: Thread trimed title:: ".$get_hottest[titletrimmed]);
$this->the_logger->write_log("debug","mp_getMostActiveThreads:: In forum:: ".$foruminfo['title']);
$this->the_logger->write_log("debug","mp_getMostActiveThreads:: Reply count:: ".$get_hottest['replycount']);

$this->mostActiveThreads[] = $get_hottest;

}//end while


$this->mp_cacheActive();
$this->the_logger->write_log("debug","mp_getMostActiveThreads:: Have cache the threads");


}
Below is the plugin code that was last used before the crash. Note that I have commented out to only have certain users trigger this. Instead the plugin executed for all forum visitors. I made it public after uploading the changes (code above) then once a saved the plugin code with comments about 20 seconds latter the server ran out of memory and had to be rebooted.

Quote:
$testusers = array(1, 2, 10, 58);

//turn to true if you want to ignore caching rules
$nocache = false;

#if (in_array($vbulletin->userinfo['userid'],$testusers))
#{
$mp_cache_life_value = 3600; //1 hour
$mp_cache_life_text = "Hour";
$MMtitle = 'Information Centre - Refreshed every '.$mp_cache_life_text;

require_once '../biztalk_code/mediamanager/class.MMBuilder.php';
$builder = new MM_builder($mp_cache_life_value);

$MMinitDisplay = $builder->getDefaultStyle();
$MMinitButton = $builder->getDefaultButton();

$builder->fetch();
$builder->mp_getLatestForumNews();
$builder->mp_getMostActiveThreads();

$MMPanels = $builder->fetchPanels();

$MMjavascript = $builder->fetchJavascript();
$MMstyles = $builder->fetchStyles();

//this is at the very bottom
eval('$mediamanager = "' . fetch_template('MediaManager') . '";');
#}
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:06 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.06060 seconds
  • Memory Usage 2,219KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (1)bbcode_php
  • (5)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (5)post_thanks_box
  • (5)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (5)post_thanks_postbit_info
  • (5)postbit
  • (4)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete