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 <drizzled/statistics_variables.h>
00050 #include "scoreboard.h"
00051 
00052 #include <math.h>
00053 
00054 using namespace drizzled;
00055 using namespace std;
00056 
00057 Scoreboard::Scoreboard(uint32_t in_number_sessions, uint32_t in_number_buckets)
00058   :
00059     number_sessions(in_number_sessions),
00060     number_buckets(in_number_buckets)
00061 {
00062 
00063   /* calculate the number of elements in each bucket */
00064   number_per_bucket= static_cast<uint32_t> ( ceil( static_cast<double>(number_sessions) / static_cast<double>(number_buckets) ) );
00065 
00066   vector_of_scoreboard_vectors.reserve(number_buckets);
00067   /* populate the vector of scoreboard vectors */
00068   for (uint32_t j= 0; j < number_buckets; ++j)
00069   {
00070     vector<ScoreboardSlot* > *scoreboard_vector= new vector<ScoreboardSlot*>;
00071 
00072     scoreboard_vector->reserve(number_per_bucket);
00073     /* preallocate the individual vectors */
00074     for (uint32_t h= 0; h < number_per_bucket; ++h)
00075       scoreboard_vector->push_back(new ScoreboardSlot);
00076 
00077     /* insert the vector into the vector of scoreboard vectors */
00078     vector_of_scoreboard_vectors.push_back(scoreboard_vector); 
00079   }
00080   
00081   vector_of_scoreboard_locks.reserve(number_buckets);
00082   /* populate the scoreboard locks vector each ScoreboardSlot vector gets a lock */
00083   for (uint32_t k= 0; k < number_buckets; ++k)
00084     vector_of_scoreboard_locks.push_back(new boost::shared_mutex);
00085 
00086   /* calculate the approximate memory allocation of the scoreboard */
00087   size_t statusVarsSize= sizeof(StatusVars) + sizeof(system_status_var);
00088   size_t userCommandsSize= sizeof(UserCommands) + sizeof(uint64_t) * SQLCOM_END;
00089 
00090   scoreboard_size_bytes= (statusVarsSize + userCommandsSize) * number_per_bucket * number_buckets;
00091 }
00092 
00093 Scoreboard::~Scoreboard()
00094 {
00095   BOOST_FOREACH(std::vector<ScoreboardSlot*>* it0, vector_of_scoreboard_vectors)
00096   {
00097     BOOST_FOREACH(ScoreboardSlot* it, *it0)
00098       delete it; 
00099     delete it0;
00100   }
00101   BOOST_FOREACH(boost::shared_mutex* it, vector_of_scoreboard_locks)
00102     delete it;
00103 }
00104 
00105 uint32_t Scoreboard::getBucketNumber(Session *session) const
00106 {
00107   return session->getSessionId() % number_buckets;
00108 }
00109 
00110 ScoreboardSlot* Scoreboard::findScoreboardSlotToLog(Session *session) 
00111 {
00112   /* our bucket */
00113   uint32_t bucket_number= getBucketNumber(session);
00114 
00115   /* our vector corresponding to bucket_number */
00116   vector<ScoreboardSlot* > *scoreboard_vector= vector_of_scoreboard_vectors.at(bucket_number);
00117 
00118   /* Check if this session has already claimed a slot */
00119   int32_t session_scoreboard_slot= session->getScoreboardIndex();
00120 
00121   if (session_scoreboard_slot != -1)
00122     return scoreboard_vector->at(session_scoreboard_slot);
00123 
00124   boost::shared_mutex* LOCK_scoreboard_vector= vector_of_scoreboard_locks.at(bucket_number);
00125   LOCK_scoreboard_vector->lock();
00126 
00127   int32_t slot_index= 0;
00128   for (vector<ScoreboardSlot*>::iterator it= scoreboard_vector->begin(); it != scoreboard_vector->end(); ++it, ++slot_index)
00129   {
00130     ScoreboardSlot& slot= **it;
00131     if (slot.isInUse())
00132       continue;
00133     slot.setInUse(true);
00134     slot.setSessionId(session->getSessionId());
00135     slot.setUser(session->user()->username());
00136     slot.setIp(session->user()->address());
00137     session->setScoreboardIndex(slot_index);
00138     LOCK_scoreboard_vector->unlock();
00139     return &slot; 
00140   }
00141 
00142   LOCK_scoreboard_vector->unlock(); 
00143 
00144   /* its possible we did not claim a slot if the scoreboard size is somehow smaller then the 
00145      active connections */ 
00146   return NULL; 
00147 }
00148 
00149 ScoreboardSlot* Scoreboard::findOurScoreboardSlot(Session *session)
00150 {
00151   /* Check if this session has already claimed a slot */
00152   int32_t session_scoreboard_slot= session->getScoreboardIndex();
00153   if (session_scoreboard_slot == -1) 
00154     return NULL;
00155   return vector_of_scoreboard_vectors.at(getBucketNumber(session))->at(session_scoreboard_slot);
00156 }