2013-04-17 00:00:59 -04:00
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
2013-04-17 00:00:03 -04:00
# ident "$Id$"
2013-04-17 00:01:35 -04:00
/*
COPYING CONDITIONS NOTICE :
This program is free software ; you can redistribute it and / or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation , and provided that the
following conditions are met :
* Redistributions of source code must retain this COPYING
CONDITIONS NOTICE , the COPYRIGHT NOTICE ( below ) , the
DISCLAIMER ( below ) , the UNIVERSITY PATENT NOTICE ( below ) , the
PATENT MARKING NOTICE ( below ) , and the PATENT RIGHTS
GRANT ( below ) .
* Redistributions in binary form must reproduce this COPYING
CONDITIONS NOTICE , the COPYRIGHT NOTICE ( below ) , the
DISCLAIMER ( below ) , the UNIVERSITY PATENT NOTICE ( below ) , the
PATENT MARKING NOTICE ( below ) , and the PATENT RIGHTS
GRANT ( below ) in the documentation and / or other materials
provided with the distribution .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA
02110 - 1301 , USA .
COPYRIGHT NOTICE :
TokuDB , Tokutek Fractal Tree Indexing Library .
Copyright ( C ) 2007 - 2013 Tokutek , Inc .
DISCLAIMER :
This program is distributed in the hope that it will be useful , but
WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
General Public License for more details .
UNIVERSITY PATENT NOTICE :
The technology is licensed by the Massachusetts Institute of
Technology , Rutgers State University of New Jersey , and the Research
Foundation of State University of New York at Stony Brook under
United States of America Serial No . 11 / 760379 and to the patents
and / or patent applications resulting from it .
PATENT MARKING NOTICE :
This software is covered by US Patent No . 8 , 185 , 551.
2013-10-04 16:49:53 -04:00
This software is covered by US Patent No . 8 , 489 , 638.
2013-04-17 00:01:35 -04:00
PATENT RIGHTS GRANT :
2013-04-17 00:01:36 -04:00
" THIS IMPLEMENTATION " means the copyrightable works distributed by
2013-04-17 00:01:35 -04:00
Tokutek as part of the Fractal Tree project .
" PATENT CLAIMS " means the claims of patents that are owned or
licensable by Tokutek , both currently or in the future ; and that in
the absence of this license would be infringed by THIS
IMPLEMENTATION or by using or running THIS IMPLEMENTATION .
" PATENT CHALLENGE " shall mean a challenge to the validity ,
patentability , enforceability and / or non - infringement of any of the
PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS .
Tokutek hereby grants to you , for the term and geographical scope of
the PATENT CLAIMS , a non - exclusive , no - charge , royalty - free ,
irrevocable ( except as stated in this section ) patent license to
make , have made , use , offer to sell , sell , import , transfer , and
otherwise run , modify , and propagate the contents of THIS
IMPLEMENTATION , where such license applies only to the PATENT
CLAIMS . This grant does not include claims that would be infringed
only as a consequence of further modifications of THIS
IMPLEMENTATION . If you or your agent or licensee institute or order
or agree to the institution of patent litigation against any entity
( including a cross - claim or counterclaim in a lawsuit ) alleging that
THIS IMPLEMENTATION constitutes direct or contributory patent
infringement , or inducement of patent infringement , then any rights
granted to you under this License shall terminate as of the date
such litigation is filed . If you or your agent or exclusive
licensee institute or order or agree to the institution of a PATENT
CHALLENGE , then Tokutek may terminate any rights granted to you
under this License .
*/
2013-04-17 00:01:35 -04:00
# ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
2013-04-17 00:00:03 -04:00
# ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11 / 760379 and to the patents and / or patent applications resulting from it."
2013-04-17 00:00:35 -04:00
# include <ft-flusher.h>
# include <ft-flusher-internal.h>
# include <ft-cachetable-wrappers.h>
# include <ft-internal.h>
# include <ft.h>
2013-04-17 00:01:14 -04:00
# include <portability/toku_atomic.h>
2013-04-17 00:01:31 -04:00
# include <util/status.h>
2013-04-17 00:00:03 -04:00
// Member Descirption:
// 1. highest_pivot_key - this is the key that corresponds to the
// most recently flushed leaf entry.
// 2. max_current_key - this is the pivot/key that we inherit as
// we descend down the tree. We use this to set the highest_pivot_key.
// 3. sub_tree_size - this is the percentage of the entire tree that our
// current position (in a sub-tree) encompasses.
// 4. percentage_done - this is the percentage of leaf nodes that have
// been flushed into.
// 5. rightmost_leaf_seen - this is a boolean we use to determine if
// if we have flushed to every leaf node.
struct hot_flusher_extra {
DBT highest_pivot_key ;
DBT max_current_key ;
float sub_tree_size ;
float percentage_done ;
bool rightmost_leaf_seen ;
} ;
2013-04-17 00:00:58 -04:00
static FT_HOT_STATUS_S hot_status ;
2013-04-17 00:00:08 -04:00
2013-04-17 00:01:31 -04:00
# define STATUS_INIT(k,c,t,l,inc) TOKUDB_STATUS_INIT(hot_status, k, c, t, "hot: " l, inc)
2013-04-17 00:00:08 -04:00
# define STATUS_VALUE(x) hot_status.status[x].value.num
2013-04-17 00:00:03 -04:00
2013-04-17 00:00:04 -04:00
void
2013-04-17 00:00:35 -04:00
toku_ft_hot_status_init ( void )
2013-04-17 00:00:04 -04:00
{
2013-04-17 00:01:31 -04:00
STATUS_INIT ( FT_HOT_NUM_STARTED , nullptr , UINT64 , " operations ever started " , TOKU_ENGINE_STATUS ) ;
STATUS_INIT ( FT_HOT_NUM_COMPLETED , nullptr , UINT64 , " operations successfully completed " , TOKU_ENGINE_STATUS ) ;
STATUS_INIT ( FT_HOT_NUM_ABORTED , nullptr , UINT64 , " operations aborted " , TOKU_ENGINE_STATUS ) ;
STATUS_INIT ( FT_HOT_MAX_ROOT_FLUSH_COUNT , nullptr , UINT64 , " max number of flushes from root ever required to optimize a tree " , TOKU_ENGINE_STATUS ) ;
2013-04-17 00:00:08 -04:00
hot_status . initialized = true ;
2013-04-17 00:00:04 -04:00
}
2013-04-17 00:00:08 -04:00
# undef STATUS_INIT
2013-04-17 00:00:04 -04:00
void
2013-04-17 00:00:35 -04:00
toku_ft_hot_get_status ( FT_HOT_STATUS s ) {
2013-04-17 00:00:08 -04:00
if ( ! hot_status . initialized ) {
2013-04-17 00:00:35 -04:00
toku_ft_hot_status_init ( ) ;
2013-04-17 00:00:08 -04:00
}
2013-04-17 00:00:03 -04:00
* s = hot_status ;
}
// Copies the max current key to the highest pivot key seen.
static void
hot_set_highest_key ( struct hot_flusher_extra * flusher )
{
// The max current key will be NULL if we are traversing in the
// rightmost subtree of a given parent. As such, we don't want to
// allocate memory for this case.
2013-04-17 00:01:25 -04:00
toku_destroy_dbt ( & flusher - > highest_pivot_key ) ;
if ( flusher - > max_current_key . data ! = NULL ) {
2013-04-17 00:00:03 -04:00
// Otherwise, let's copy all the contents from one key to the other.
2013-04-17 00:01:25 -04:00
toku_clone_dbt ( & flusher - > highest_pivot_key , flusher - > max_current_key ) ;
2013-04-17 00:00:03 -04:00
}
}
2013-08-13 15:01:27 -04:00
static void
hot_set_start_key ( struct hot_flusher_extra * flusher , const DBT * start )
{
toku_destroy_dbt ( & flusher - > highest_pivot_key ) ;
if ( start ! = NULL ) {
// Otherwise, let's copy all the contents from one key to the other.
toku_clone_dbt ( & flusher - > highest_pivot_key , * start ) ;
}
}
2013-04-17 00:00:03 -04:00
static int
2013-04-17 00:00:35 -04:00
hot_just_pick_child ( FT h ,
FTNODE parent ,
2013-04-17 00:00:03 -04:00
struct hot_flusher_extra * flusher )
{
int childnum = 0 ;
// Search through Parents pivots, see which one is greater than
// the highest_pivot_key seen so far.
if ( flusher - > highest_pivot_key . data = = NULL )
{
// Special case of the first child of the root node.
// Also known as, NEGATIVE INFINITY....
childnum = 0 ;
} else {
// Find the pivot boundary.
2013-04-17 00:00:35 -04:00
childnum = toku_ftnode_hot_next_child ( parent ,
2013-04-17 00:00:03 -04:00
& flusher - > highest_pivot_key ,
2013-04-17 00:00:15 -04:00
& h - > cmp_descriptor ,
2013-04-17 00:00:03 -04:00
h - > compare_fun ) ;
}
return childnum ;
}
static void
2013-04-17 00:00:35 -04:00
hot_update_flusher_keys ( FTNODE parent ,
2013-04-17 00:00:03 -04:00
int childnum ,
struct hot_flusher_extra * flusher )
{
// Update maximum current key if the child is NOT the rightmost
// child node.
if ( childnum < ( parent - > n_children - 1 ) ) {
2013-04-17 00:01:25 -04:00
toku_destroy_dbt ( & flusher - > max_current_key ) ;
toku_clone_dbt ( & flusher - > max_current_key , parent - > childkeys [ childnum ] ) ;
2013-04-17 00:00:03 -04:00
}
}
2013-04-17 00:01:21 -04:00
// Picks which child toku_ft_flush_some_child will use for flushing and
2013-04-17 00:00:03 -04:00
// recursion.
static int
2013-04-17 00:00:35 -04:00
hot_pick_child ( FT h ,
FTNODE parent ,
2013-04-17 00:00:03 -04:00
void * extra )
{
2013-04-17 00:00:58 -04:00
struct hot_flusher_extra * flusher = ( struct hot_flusher_extra * ) extra ;
2013-04-17 00:00:03 -04:00
int childnum = hot_just_pick_child ( h , parent , flusher ) ;
// Now we determine the percentage of the tree flushed so far.
// Whichever subtree we choose to recurse into, it is a fraction
// of the current parent.
flusher - > sub_tree_size / = parent - > n_children ;
// Update the precentage complete, using our new sub tree size AND
// the number of children we have already flushed.
flusher - > percentage_done + = ( flusher - > sub_tree_size * childnum ) ;
hot_update_flusher_keys ( parent , childnum , flusher ) ;
return childnum ;
}
// Does nothing for now.
static void
2013-04-17 00:00:35 -04:00
hot_update_status ( FTNODE UU ( child ) ,
2013-04-17 00:00:03 -04:00
int UU ( dirtied ) ,
void * UU ( extra ) )
{
return ;
}
2013-04-17 00:00:12 -04:00
// If we've just split a node, HOT needs another chance to decide which
// one to flush into. This gives it a chance to do that, and update the
// keys it maintains.
2013-04-17 00:00:03 -04:00
static int
2013-04-17 00:00:35 -04:00
hot_pick_child_after_split ( FT h ,
FTNODE parent ,
2013-04-17 00:00:03 -04:00
int childnuma ,
int childnumb ,
void * extra )
{
2013-04-17 00:00:58 -04:00
struct hot_flusher_extra * flusher = ( struct hot_flusher_extra * ) extra ;
2013-04-17 00:00:03 -04:00
int childnum = hot_just_pick_child ( h , parent , flusher ) ;
assert ( childnum = = childnuma | | childnum = = childnumb ) ;
hot_update_flusher_keys ( parent , childnum , flusher ) ;
if ( parent - > height = = 1 ) {
2013-04-17 00:00:12 -04:00
// We don't want to recurse into a leaf node, but if we return
2013-04-17 00:00:35 -04:00
// anything valid, ft_split_child will try to go there, so we
// return -1 to allow ft_split_child to have its default
2013-04-17 00:00:12 -04:00
// behavior, which will be to stop recursing.
2013-04-17 00:00:03 -04:00
childnum = - 1 ;
}
return childnum ;
}
// Basic constructor/initializer for the hot flusher struct.
static void
hot_flusher_init ( struct flusher_advice * advice ,
struct hot_flusher_extra * flusher )
{
// Initialize the highest pivot key seen to NULL. This represents
// NEGATIVE INFINITY and is used to cover the special case of our
// first traversal of the tree.
toku_init_dbt ( & ( flusher - > highest_pivot_key ) ) ;
toku_init_dbt ( & ( flusher - > max_current_key ) ) ;
flusher - > rightmost_leaf_seen = 0 ;
flusher - > sub_tree_size = 1.0 ;
flusher - > percentage_done = 0.0 ;
flusher_advice_init ( advice ,
hot_pick_child ,
dont_destroy_basement_nodes ,
always_recursively_flush ,
default_merge_child ,
hot_update_status ,
hot_pick_child_after_split ,
flusher
) ;
}
// Erases any DBT keys we have copied from a traversal.
static void
hot_flusher_destroy ( struct hot_flusher_extra * flusher )
{
2013-04-17 00:01:25 -04:00
toku_destroy_dbt ( & flusher - > highest_pivot_key ) ;
toku_destroy_dbt ( & flusher - > max_current_key ) ;
2013-04-17 00:00:03 -04:00
}
// Entry point for Hot Optimize Table (HOT). Note, this function is
// not recursive. It iterates over root-to-leaf paths.
int
2013-08-13 15:01:27 -04:00
toku_ft_hot_optimize ( FT_HANDLE brt , DBT * left , DBT * right ,
2013-04-17 00:00:03 -04:00
int ( * progress_callback ) ( void * extra , float progress ) ,
2013-10-21 16:02:14 -04:00
void * progress_extra , uint64_t * loops_run )
2013-04-17 00:00:03 -04:00
{
int r = 0 ;
struct hot_flusher_extra flusher ;
struct flusher_advice advice ;
hot_flusher_init ( & advice , & flusher ) ;
2013-08-13 15:01:27 -04:00
hot_set_start_key ( & flusher , left ) ;
2013-04-17 00:00:03 -04:00
uint64_t loop_count = 0 ;
MSN msn_at_start_of_hot = ZERO_MSN ; // capture msn from root at
// start of HOT operation
2013-04-17 00:01:14 -04:00
( void ) toku_sync_fetch_and_add ( & STATUS_VALUE ( FT_HOT_NUM_STARTED ) , 1 ) ;
2013-04-17 00:00:03 -04:00
2013-04-17 00:01:25 -04:00
toku_ft_note_hot_begin ( brt ) ;
2013-04-17 00:00:04 -04:00
// Higher level logic prevents a dictionary from being deleted or
// truncated during a hot optimize operation. Doing so would violate
// the hot optimize contract.
2013-04-17 00:00:03 -04:00
do {
2013-04-17 00:00:35 -04:00
FTNODE root ;
2013-04-17 00:00:29 -04:00
CACHEKEY root_key ;
2013-04-17 00:01:01 -04:00
uint32_t fullhash ;
2013-04-17 00:00:03 -04:00
2013-04-17 00:00:04 -04:00
{
// Get root node (the first parent of each successive HOT
// call.)
2013-04-17 00:00:36 -04:00
toku_calculate_root_offset_pointer ( brt - > ft , & root_key , & fullhash ) ;
2013-04-17 00:00:35 -04:00
struct ftnode_fetch_extra bfe ;
2013-04-17 00:00:36 -04:00
fill_bfe_for_full_read ( & bfe , brt - > ft ) ;
toku_pin_ftnode_off_client_thread ( brt - > ft ,
2013-04-17 00:00:29 -04:00
( BLOCKNUM ) root_key ,
2013-04-17 00:00:04 -04:00
fullhash ,
& bfe ,
2013-04-17 00:01:03 -04:00
PL_WRITE_EXPENSIVE ,
2013-04-17 00:00:04 -04:00
0 ,
NULL ,
& root ) ;
toku_assert_entire_node_in_memory ( root ) ;
}
2013-04-17 00:00:03 -04:00
// Prepare HOT diagnostics.
if ( loop_count = = 0 ) {
2013-04-17 00:00:04 -04:00
// The first time through, capture msn from root
2013-04-17 00:00:03 -04:00
msn_at_start_of_hot = root - > max_msn_applied_to_node_on_disk ;
}
loop_count + + ;
2013-04-17 00:00:35 -04:00
if ( loop_count > STATUS_VALUE ( FT_HOT_MAX_ROOT_FLUSH_COUNT ) ) {
STATUS_VALUE ( FT_HOT_MAX_ROOT_FLUSH_COUNT ) = loop_count ;
2013-04-17 00:00:03 -04:00
}
// Initialize the maximum current key. We need to do this for
// every traversal.
2013-04-17 00:01:25 -04:00
toku_destroy_dbt ( & flusher . max_current_key ) ;
2013-04-17 00:00:03 -04:00
flusher . sub_tree_size = 1.0 ;
flusher . percentage_done = 0.0 ;
// This should recurse to the bottom of the tree and then
// return.
if ( root - > height > 0 ) {
2013-04-17 00:01:21 -04:00
toku_ft_flush_some_child ( brt - > ft , root , & advice ) ;
2013-04-17 00:00:03 -04:00
} else {
// Since there are no children to flush, we should abort
// the HOT call.
flusher . rightmost_leaf_seen = 1 ;
2013-04-17 00:00:36 -04:00
toku_unpin_ftnode_off_client_thread ( brt - > ft , root ) ;
2013-04-17 00:00:03 -04:00
}
// Set the highest pivot key seen here, since the parent may
// be unlocked and NULL'd later in our caller:
2013-04-17 00:01:21 -04:00
// toku_ft_flush_some_child().
2013-04-17 00:00:03 -04:00
hot_set_highest_key ( & flusher ) ;
// This is where we determine if the traversal is finished or
// not.
if ( flusher . max_current_key . data = = NULL ) {
flusher . rightmost_leaf_seen = 1 ;
}
2013-08-13 15:01:27 -04:00
else if ( right ) {
// if we have flushed past the bounds set for us,
// set rightmost_leaf_seen so we exit
FAKE_DB ( db , & brt - > ft - > cmp_descriptor ) ;
int cmp = brt - > ft - > compare_fun ( & db , & flusher . max_current_key , right ) ;
if ( cmp > 0 ) {
flusher . rightmost_leaf_seen = 1 ;
}
}
2013-04-17 00:00:03 -04:00
// Update HOT's progress.
if ( progress_callback ! = NULL ) {
r = progress_callback ( progress_extra , flusher . percentage_done ) ;
// Check if the callback wants us to stop running HOT.
if ( r ! = 0 ) {
flusher . rightmost_leaf_seen = 1 ;
}
}
// Loop until the max key has been updated to positive
// infinity.
} while ( ! flusher . rightmost_leaf_seen ) ;
2013-10-21 16:02:14 -04:00
* loops_run = loop_count ;
2013-04-17 00:00:03 -04:00
// Cleanup.
hot_flusher_destroy ( & flusher ) ;
// More diagnostics.
{
2013-04-17 00:01:01 -04:00
bool success = false ;
2013-04-17 00:00:04 -04:00
if ( r = = 0 ) { success = true ; }
{
2013-04-17 00:00:35 -04:00
toku_ft_note_hot_complete ( brt , success , msn_at_start_of_hot ) ;
2013-04-17 00:00:04 -04:00
}
if ( success ) {
2013-04-17 00:01:14 -04:00
( void ) toku_sync_fetch_and_add ( & STATUS_VALUE ( FT_HOT_NUM_COMPLETED ) , 1 ) ;
2013-04-17 00:00:04 -04:00
} else {
2013-04-17 00:01:14 -04:00
( void ) toku_sync_fetch_and_add ( & STATUS_VALUE ( FT_HOT_NUM_ABORTED ) , 1 ) ;
2013-04-17 00:00:04 -04:00
}
2013-04-17 00:00:03 -04:00
}
return r ;
}
2013-04-17 00:00:08 -04:00
2013-04-17 00:01:11 -04:00
# include <toku_race_tools.h>
2013-04-17 00:00:13 -04:00
void __attribute__ ( ( __constructor__ ) ) toku_hot_helgrind_ignore ( void ) ;
2013-04-17 00:00:11 -04:00
void
2013-04-17 00:00:13 -04:00
toku_hot_helgrind_ignore ( void ) {
2013-04-17 00:00:11 -04:00
// incremented only while lock is held, but read by engine status asynchronously.
2013-04-17 00:01:11 -04:00
TOKU_VALGRIND_HG_DISABLE_CHECKING ( & hot_status , sizeof hot_status ) ;
2013-04-17 00:00:11 -04:00
}
2013-04-17 00:00:08 -04:00
# undef STATUS_VALUE