Drizzled Public API Documentation

scoreboard.cc

00001 /*
00002  * Copyright (C) 2010 Joseph Daly <skinny.moey@gmail.com>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *   * Redistributions of source code must retain the above copyright notice,
00009  *     this list of conditions and the following disclaimer.
00010  *   * Redistributions in binary form must reproduce the above copyright notice,
00011  *     this list of conditions and the following disclaimer in the documentation
00012  *     and/or other materials provided with the distribution.
00013  *   * Neither the name of Joseph Daly nor the names of its contributors
00014  *     may be used to endorse or promote products derived from this software
00015  *     without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00027  * THE POSSIBILITY OF SUCH DAMAGE.
00028  *
00029  */
00030 
00047 #include <config.h>
00048 #include <drizzled/plugin.h>
00049 #include "scoreboard.h"
00050 
00051 #include <math.h>
00052 
00053 using namespace drizzled;
00054 using namespace std;
00055 
00056 Scoreboard::Scoreboard(uint32_t in_number_sessions, uint32_t in_number_buckets)
00057   :
00058     number_sessions(in_number_sessions),
00059     number_buckets(in_number_buckets)
00060 {
00061 
00062   /* calculate the number of elements in each bucket */
00063   number_per_bucket= static_cast<uint32_t> ( ceil( static_cast<double>(number_sessions) / static_cast<double>(number_buckets) ) );
00064 
00065   /* populate the vector of scoreboard vectors */
00066   for (uint32_t j= 0; j < number_buckets; ++j)
00067   {
00068     vector<ScoreboardSlot* > *scoreboard_vector= new vector<ScoreboardSlot* >();
00069 
00070     /* preallocate the individual vectors */
00071     vector<ScoreboardSlot* >::iterator scoreboard_vector_iterator= scoreboard_vector->begin();
00072     for (uint32_t h= 0; h < number_per_bucket; ++h)
00073     {
00074       ScoreboardSlot *scoreboard_slot= new ScoreboardSlot();
00075       scoreboard_vector_iterator= scoreboard_vector->insert(scoreboard_vector_iterator, scoreboard_slot);
00076     }  
00077     scoreboard_vector->resize(number_per_bucket);
00078 
00079 
00080     /* insert the vector into the vector of scoreboard vectors */
00081     vector<vector<ScoreboardSlot* >* >::iterator vector_of_scoreboard_vectors_iterator= 
00082       vector_of_scoreboard_vectors.begin();
00083 
00084     vector_of_scoreboard_vectors_iterator= 
00085       vector_of_scoreboard_vectors.insert(vector_of_scoreboard_vectors_iterator, scoreboard_vector); 
00086   }
00087   vector_of_scoreboard_vectors.resize(number_buckets);
00088   
00089   /* populate the scoreboard locks vector each ScoreboardSlot vector gets a lock */
00090   vector<boost::shared_mutex* >::iterator vector_of_scoreboard_locks_iterator= vector_of_scoreboard_locks.begin();
00091   for (uint32_t k= 0; k < number_buckets; ++k)
00092   {
00093     boost::shared_mutex* lock= new boost::shared_mutex();
00094     vector_of_scoreboard_locks_iterator= 
00095       vector_of_scoreboard_locks.insert(vector_of_scoreboard_locks_iterator, lock);   
00096   } 
00097   vector_of_scoreboard_locks.resize(number_buckets);
00098 
00099   /* calculate the approximate memory allocation of the scoreboard */
00100   size_t statusVarsSize= sizeof(StatusVars) + sizeof(system_status_var);
00101   size_t userCommandsSize= sizeof(UserCommands) + sizeof(uint64_t) * SQLCOM_END;
00102 
00103   scoreboard_size_bytes= (statusVarsSize + userCommandsSize) * number_per_bucket * number_buckets;
00104 }
00105 
00106 Scoreboard::~Scoreboard()
00107 {
00108   vector<vector<ScoreboardSlot* >* >::iterator v_of_scoreboard_v_begin_it= vector_of_scoreboard_vectors.begin();
00109   vector<vector<ScoreboardSlot* >* >::iterator v_of_scoreboard_v_end_it= vector_of_scoreboard_vectors.end();
00110 
00111   for (; v_of_scoreboard_v_begin_it != v_of_scoreboard_v_end_it; ++v_of_scoreboard_v_begin_it)
00112   {
00113     vector<ScoreboardSlot* > *scoreboard_vector= *v_of_scoreboard_v_begin_it; 
00114 
00115     vector<ScoreboardSlot* >::iterator scoreboard_vector_it= scoreboard_vector->begin();
00116     vector<ScoreboardSlot* >::iterator scoreboard_vector_end= scoreboard_vector->end();
00117     for (; scoreboard_vector_it != scoreboard_vector_end; ++scoreboard_vector_it)
00118     {
00119       delete *scoreboard_vector_it; 
00120     }
00121     
00122     scoreboard_vector->clear();
00123     delete scoreboard_vector;
00124   } // vector_of_scoreboard_vectors is not on the stack and does not deletion
00125   
00126   vector<boost::shared_mutex* >::iterator vector_of_scoreboard_locks_it= vector_of_scoreboard_locks.begin();
00127   vector<boost::shared_mutex* >::iterator vector_of_scoreboard_locks_end= vector_of_scoreboard_locks.end();
00128 
00129   for (; vector_of_scoreboard_locks_it != vector_of_scoreboard_locks_end; ++vector_of_scoreboard_locks_it)
00130   {
00131     boost::shared_mutex* lock= *vector_of_scoreboard_locks_it;
00132     delete lock;
00133   }
00134 }
00135 
00136 uint32_t Scoreboard::getBucketNumber(Session *session)
00137 {
00138   return (session->getSessionId() % number_buckets);
00139 }
00140 
00141 ScoreboardSlot* Scoreboard::findScoreboardSlotToLog(Session *session) 
00142 {
00143   /* our bucket */
00144   uint32_t bucket_number= getBucketNumber(session);
00145 
00146   /* our vector corresponding to bucket_number */
00147   vector<ScoreboardSlot* > *scoreboard_vector= vector_of_scoreboard_vectors.at(bucket_number);
00148 
00149   /* Check if this session has already claimed a slot */
00150   int32_t session_scoreboard_slot= session->getScoreboardIndex();
00151 
00152   if (session_scoreboard_slot == -1)
00153   {
00154     boost::shared_mutex* LOCK_scoreboard_vector= vector_of_scoreboard_locks.at(bucket_number);
00155     LOCK_scoreboard_vector->lock();
00156  
00157     ScoreboardSlot *scoreboard_slot= NULL;
00158 
00159     int32_t slot_index= 0;
00160     for (vector<ScoreboardSlot *>::iterator it= scoreboard_vector->begin();
00161          it != scoreboard_vector->end(); ++it, ++slot_index)
00162     {
00163       scoreboard_slot= *it;
00164 
00165       if (scoreboard_slot->isInUse() == false)
00166       {
00167         scoreboard_slot->setInUse(true);
00168         scoreboard_slot->setSessionId(session->getSessionId());
00169         scoreboard_slot->setUser(session->user()->username());
00170         scoreboard_slot->setIp(session->user()->address());
00171         session->setScoreboardIndex(slot_index);
00172         LOCK_scoreboard_vector->unlock();
00173         return scoreboard_slot; 
00174       }
00175     }
00176   
00177     LOCK_scoreboard_vector->unlock(); 
00178   } 
00179   else // already claimed a slot just do a lookup
00180   {
00181     ScoreboardSlot *scoreboard_slot= scoreboard_vector->at(session_scoreboard_slot);
00182     return scoreboard_slot;  
00183   }
00184 
00185   /* its possible we did not claim a slot if the scoreboard size is somehow smaller then the 
00186      active connections */ 
00187   return NULL; 
00188 }
00189 
00190 ScoreboardSlot* Scoreboard::findOurScoreboardSlot(Session *session)
00191 {
00192   /* our bucket */
00193   uint32_t bucket_number= getBucketNumber(session);
00194 
00195   /* our vector corresponding to bucket_number */
00196   vector<ScoreboardSlot* > *scoreboard_vector= vector_of_scoreboard_vectors.at(bucket_number);
00197 
00198   /* Check if this session has already claimed a slot */
00199   int32_t session_scoreboard_slot= session->getScoreboardIndex();
00200 
00201   if (session_scoreboard_slot == -1) 
00202   {
00203     return NULL;
00204   }
00205 
00206   ScoreboardSlot *scoreboard_slot= scoreboard_vector->at(session_scoreboard_slot);
00207   return scoreboard_slot;
00208 }