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-16 23:59:03 -04:00
# ident "$Id$"
2013-04-17 00:00:59 -04:00
# ident "Copyright (c) 2007-2012 Tokutek Inc. All rights reserved."
2013-04-16 23:57:55 -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:01:15 -04:00
# include <toku_stdint.h>
# include "ft.h"
# include "log-internal.h"
2013-04-17 00:00:42 -04:00
# include "rollback-ct-callbacks.h"
2013-04-16 23:59:05 -04:00
2013-04-17 00:01:01 -04:00
static void rollback_unpin_remove_callback ( CACHEKEY * cachekey , bool for_checkpoint , void * extra ) {
2013-04-17 00:00:59 -04:00
FT CAST_FROM_VOIDP ( h , extra ) ;
2013-04-17 00:00:06 -04:00
toku_free_blocknum (
2013-04-17 00:01:13 -04:00
h - > blocktable ,
2013-04-17 00:00:06 -04:00
cachekey ,
h ,
for_checkpoint
) ;
}
2013-04-17 00:00:15 -04:00
void toku_rollback_log_unpin_and_remove ( TOKUTXN txn , ROLLBACK_LOG_NODE log ) {
2013-04-16 23:59:05 -04:00
int r ;
CACHEFILE cf = txn - > logger - > rollback_cachefile ;
2013-04-17 00:00:59 -04:00
FT CAST_FROM_VOIDP ( h , toku_cachefile_get_userdata ( cf ) ) ;
2013-04-17 00:01:03 -04:00
r = toku_cachetable_unpin_and_remove ( cf , log - > ct_pair , rollback_unpin_remove_callback , h ) ;
2013-04-17 00:00:15 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:59:05 -04:00
}
2013-04-16 23:59:22 -04:00
int
2013-04-17 00:00:59 -04:00
toku_find_xid_by_xid ( const TXNID & xid , const TXNID & xidfind ) {
2013-04-16 23:59:50 -04:00
if ( xid < xidfind ) return - 1 ;
if ( xid > xidfind ) return + 1 ;
2013-04-16 23:59:22 -04:00
return 0 ;
}
2013-04-17 00:00:15 -04:00
void * toku_malloc_in_rollback ( ROLLBACK_LOG_NODE log , size_t size ) {
2013-04-16 23:59:05 -04:00
return malloc_in_memarena ( log - > rollentry_arena , size ) ;
2013-04-16 23:57:55 -04:00
}
2013-04-16 23:59:05 -04:00
void * toku_memdup_in_rollback ( ROLLBACK_LOG_NODE log , const void * v , size_t len ) {
void * r = toku_malloc_in_rollback ( log , len ) ;
2013-04-16 23:57:55 -04:00
memcpy ( r , v , len ) ;
return r ;
}
2013-04-16 23:59:54 -04:00
static inline PAIR_ATTR make_rollback_pair_attr ( long size ) {
PAIR_ATTR result = {
. size = size ,
2013-04-17 00:01:13 -04:00
. nonleaf_size = 0 ,
. leaf_size = 0 ,
. rollback_size = size ,
2013-04-17 00:00:11 -04:00
. cache_pressure_size = 0 ,
2013-04-17 00:01:01 -04:00
. is_valid = true
2013-04-17 00:01:13 -04:00
} ;
return result ;
2013-04-16 23:59:54 -04:00
}
2013-04-17 00:00:42 -04:00
PAIR_ATTR
2013-04-16 23:59:05 -04:00
rollback_memory_size ( ROLLBACK_LOG_NODE log ) {
size_t size = sizeof ( * log ) ;
2013-04-17 00:01:13 -04:00
if ( log - > rollentry_arena ) {
size + = memarena_total_memory_size ( log - > rollentry_arena ) ;
}
2013-04-16 23:59:54 -04:00
return make_rollback_pair_attr ( size ) ;
2013-04-16 23:59:05 -04:00
}
2013-04-17 00:01:14 -04:00
static void toku_rollback_node_save_ct_pair ( CACHEKEY UU ( key ) , void * value_data , PAIR p ) {
2013-04-17 00:01:03 -04:00
ROLLBACK_LOG_NODE CAST_FROM_VOIDP ( log , value_data ) ;
log - > ct_pair = p ;
}
2013-04-17 00:01:13 -04:00
//
// initializes an empty rollback log node
// Does not touch the blocknum or hash, that is the
// responsibility of the caller
//
void rollback_empty_log_init ( ROLLBACK_LOG_NODE log ) {
// Having a txnid set to TXNID_NONE is how we determine if the
// rollback log node is empty or in use.
log - > txnid = TXNID_NONE ;
2013-04-17 00:00:35 -04:00
log - > layout_version = FT_LAYOUT_VERSION ;
log - > layout_version_original = FT_LAYOUT_VERSION ;
log - > layout_version_read_from_disk = FT_LAYOUT_VERSION ;
2013-04-17 00:01:01 -04:00
log - > dirty = true ;
2013-04-17 00:01:13 -04:00
log - > sequence = 0 ;
2013-04-17 00:01:14 -04:00
log - > previous = make_blocknum ( 0 ) ;
2013-04-17 00:01:13 -04:00
log - > previous_hash = 0 ;
log - > oldest_logentry = NULL ;
log - > newest_logentry = NULL ;
log - > rollentry_arena = NULL ;
log - > rollentry_resident_bytecount = 0 ;
}
static void rollback_initialize_for_txn (
ROLLBACK_LOG_NODE log ,
TOKUTXN txn ,
BLOCKNUM previous ,
uint32_t previous_hash
)
{
2013-04-16 23:59:05 -04:00
log - > txnid = txn - > txnid64 ;
2013-04-17 00:00:42 -04:00
log - > sequence = txn - > roll_info . num_rollback_nodes + + ;
2013-04-17 00:01:13 -04:00
log - > previous = previous ;
2013-04-17 00:00:15 -04:00
log - > previous_hash = previous_hash ;
2013-04-16 23:59:05 -04:00
log - > oldest_logentry = NULL ;
log - > newest_logentry = NULL ;
log - > rollentry_arena = memarena_create ( ) ;
log - > rollentry_resident_bytecount = 0 ;
2013-04-17 00:01:16 -04:00
log - > dirty = true ;
2013-04-17 00:01:13 -04:00
}
void make_rollback_log_empty ( ROLLBACK_LOG_NODE log ) {
memarena_close ( & log - > rollentry_arena ) ;
rollback_empty_log_init ( log ) ;
}
// create and pin a new rollback log node. chain it to the other rollback nodes
// by providing a previous blocknum/ hash and assigning the new rollback log
// node the next sequence number
static void rollback_log_create (
TOKUTXN txn ,
BLOCKNUM previous ,
uint32_t previous_hash ,
ROLLBACK_LOG_NODE * result
)
{
ROLLBACK_LOG_NODE XMALLOC ( log ) ;
rollback_empty_log_init ( log ) ;
CACHEFILE cf = txn - > logger - > rollback_cachefile ;
FT CAST_FROM_VOIDP ( ft , toku_cachefile_get_userdata ( cf ) ) ;
rollback_initialize_for_txn ( log , txn , previous , previous_hash ) ;
toku_allocate_blocknum ( ft - > blocktable , & log - > blocknum , ft ) ;
log - > hash = toku_cachetable_hash ( ft - > cf , log - > blocknum ) ;
2013-04-16 23:59:05 -04:00
* result = log ;
2013-04-17 00:01:08 -04:00
toku_cachetable_put ( cf , log - > blocknum , log - > hash ,
log , rollback_memory_size ( log ) ,
2013-04-17 00:01:13 -04:00
get_write_callbacks_for_rollback_log ( ft ) ,
2013-04-17 00:01:08 -04:00
toku_rollback_node_save_ct_pair ) ;
2013-04-17 00:01:13 -04:00
txn - > roll_info . current_rollback = log - > blocknum ;
2013-04-17 00:00:42 -04:00
txn - > roll_info . current_rollback_hash = log - > hash ;
2013-04-16 23:59:50 -04:00
}
2013-04-17 00:00:15 -04:00
void toku_rollback_log_unpin ( TOKUTXN txn , ROLLBACK_LOG_NODE log ) {
2013-04-16 23:59:05 -04:00
int r ;
CACHEFILE cf = txn - > logger - > rollback_cachefile ;
2013-04-17 00:01:03 -04:00
r = toku_cachetable_unpin (
cf ,
log - > ct_pair ,
( enum cachetable_dirty ) log - > dirty ,
rollback_memory_size ( log )
) ;
2013-04-17 00:00:15 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:59:05 -04:00
}
//Requires: log is pinned
// log is current
//After:
// Maybe there is no current after (if it spilled)
2013-04-17 00:00:15 -04:00
void toku_maybe_spill_rollbacks ( TOKUTXN txn , ROLLBACK_LOG_NODE log ) {
2013-04-16 23:59:05 -04:00
if ( log - > rollentry_resident_bytecount > txn - > logger - > write_block_size ) {
2013-04-17 00:00:42 -04:00
assert ( log - > blocknum . b = = txn - > roll_info . current_rollback . b ) ;
2013-04-16 23:59:05 -04:00
//spill
if ( ! txn_has_spilled_rollback_logs ( txn ) ) {
//First spilled. Copy to head.
2013-04-17 00:00:42 -04:00
txn - > roll_info . spilled_rollback_head = txn - > roll_info . current_rollback ;
txn - > roll_info . spilled_rollback_head_hash = txn - > roll_info . current_rollback_hash ;
2013-04-16 23:59:05 -04:00
}
//Unconditionally copy to tail. Old tail does not need to be cached anymore.
2013-04-17 00:00:42 -04:00
txn - > roll_info . spilled_rollback_tail = txn - > roll_info . current_rollback ;
txn - > roll_info . spilled_rollback_tail_hash = txn - > roll_info . current_rollback_hash ;
2013-04-16 23:59:05 -04:00
2013-04-17 00:00:42 -04:00
txn - > roll_info . current_rollback = ROLLBACK_NONE ;
txn - > roll_info . current_rollback_hash = 0 ;
2013-04-16 23:59:05 -04:00
}
2013-04-16 23:57:55 -04:00
}
2013-04-17 00:01:04 -04:00
int find_filenum ( const FT & h , const FT & hfind ) ;
int find_filenum ( const FT & h , const FT & hfind ) {
2013-04-17 00:00:27 -04:00
FILENUM fnum = toku_cachefile_filenum ( h - > cf ) ;
FILENUM fnumfind = toku_cachefile_filenum ( hfind - > cf ) ;
2013-04-16 23:57:55 -04:00
if ( fnum . fileid < fnumfind . fileid ) return - 1 ;
if ( fnum . fileid > fnumfind . fileid ) return + 1 ;
return 0 ;
}
//Notify a transaction that it has touched a brt.
2013-04-17 00:00:42 -04:00
void toku_txn_maybe_note_ft ( TOKUTXN txn , FT ft ) {
toku_txn_lock ( txn ) ;
2013-04-17 00:01:02 -04:00
FT ftv ;
2013-04-17 00:00:42 -04:00
uint32_t idx ;
2013-04-17 00:01:02 -04:00
int r = txn - > open_fts . find_zero < FT , find_filenum > ( ft , & ftv , & idx ) ;
2013-04-17 00:00:42 -04:00
if ( r = = 0 ) {
// already there
2013-04-17 00:01:02 -04:00
assert ( ftv = = ft ) ;
2013-04-17 00:00:42 -04:00
goto exit ;
}
2013-04-17 00:01:02 -04:00
r = txn - > open_fts . insert_at ( ft , idx ) ;
2013-04-17 00:00:42 -04:00
assert_zero ( r ) ;
// TODO(leif): if there's anything that locks the reflock and then
// the txn lock, this may deadlock, because it grabs the reflock.
2013-04-17 00:00:52 -04:00
toku_ft_add_txn_ref ( ft ) ;
2013-04-17 00:00:42 -04:00
exit :
toku_txn_unlock ( txn ) ;
2013-04-16 23:57:55 -04:00
}
// Return the number of bytes that went into the rollback data structure (the uncompressed count if there is compression)
2013-04-17 00:01:17 -04:00
int toku_logger_txn_rollback_stats ( TOKUTXN txn , struct txn_stat * txn_stat )
2013-04-16 23:57:55 -04:00
{
2013-04-17 00:00:42 -04:00
toku_txn_lock ( txn ) ;
2013-04-17 00:01:17 -04:00
txn_stat - > rollback_raw_count = txn - > roll_info . rollentry_raw_count ;
txn_stat - > rollback_num_entries = txn - > roll_info . num_rollentries ;
2013-04-17 00:00:42 -04:00
toku_txn_unlock ( txn ) ;
2013-04-16 23:57:55 -04:00
return 0 ;
}
2013-04-17 00:00:15 -04:00
void toku_maybe_prefetch_previous_rollback_log ( TOKUTXN txn , ROLLBACK_LOG_NODE log ) {
//Currently processing 'log'. Prefetch the next (previous) log node.
2013-04-16 23:59:41 -04:00
2013-04-17 00:00:15 -04:00
BLOCKNUM name = log - > previous ;
2013-04-16 23:59:06 -04:00
int r = 0 ;
if ( name . b ! = ROLLBACK_NONE . b ) {
2013-04-17 00:00:15 -04:00
uint32_t hash = log - > previous_hash ;
2013-04-16 23:59:06 -04:00
CACHEFILE cf = txn - > logger - > rollback_cachefile ;
2013-04-17 00:00:59 -04:00
FT CAST_FROM_VOIDP ( h , toku_cachefile_get_userdata ( cf ) ) ;
2013-04-17 00:01:01 -04:00
bool doing_prefetch = false ;
2013-04-16 23:59:06 -04:00
r = toku_cachefile_prefetch ( cf , name , hash ,
2013-04-17 00:00:09 -04:00
get_write_callbacks_for_rollback_log ( h ) ,
2013-04-17 00:00:42 -04:00
toku_rollback_fetch_callback ,
toku_rollback_pf_req_callback ,
toku_rollback_pf_callback ,
2013-04-16 23:59:46 -04:00
h ,
& doing_prefetch ) ;
2013-04-17 00:00:15 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:59:06 -04:00
}
}
2013-04-17 00:00:15 -04:00
void toku_rollback_verify_contents ( ROLLBACK_LOG_NODE log ,
TXNID txnid , uint64_t sequence )
{
assert ( log - > txnid = = txnid ) ;
assert ( log - > sequence = = sequence ) ;
}
2013-04-16 23:59:05 -04:00
2013-04-17 00:00:15 -04:00
void toku_get_and_pin_rollback_log ( TOKUTXN txn , BLOCKNUM blocknum , uint32_t hash , ROLLBACK_LOG_NODE * log ) {
void * value ;
CACHEFILE cf = txn - > logger - > rollback_cachefile ;
2013-04-17 00:00:59 -04:00
FT CAST_FROM_VOIDP ( h , toku_cachefile_get_userdata ( cf ) ) ;
2013-04-17 00:01:03 -04:00
int r = toku_cachetable_get_and_pin_with_dep_pairs ( cf , blocknum , hash ,
2013-04-17 00:00:15 -04:00
& value , NULL ,
2013-04-17 00:00:09 -04:00
get_write_callbacks_for_rollback_log ( h ) ,
2013-04-17 00:00:42 -04:00
toku_rollback_fetch_callback ,
toku_rollback_pf_req_callback ,
toku_rollback_pf_callback ,
2013-04-17 00:01:13 -04:00
PL_WRITE_CHEAP , // lock_type
2013-04-17 00:01:03 -04:00
h ,
2013-04-17 00:01:14 -04:00
0 , NULL , NULL
2013-04-16 23:59:50 -04:00
) ;
2013-04-17 00:00:15 -04:00
assert ( r = = 0 ) ;
2013-04-17 00:00:59 -04:00
ROLLBACK_LOG_NODE CAST_FROM_VOIDP ( pinned_log , value ) ;
2013-04-17 00:00:15 -04:00
assert ( pinned_log - > blocknum . b = = blocknum . b ) ;
2013-04-17 00:01:13 -04:00
assert ( pinned_log - > hash = = hash ) ;
2013-04-17 00:00:15 -04:00
* log = pinned_log ;
2013-04-16 23:59:05 -04:00
}
2013-04-17 00:00:15 -04:00
void toku_get_and_pin_rollback_log_for_new_entry ( TOKUTXN txn , ROLLBACK_LOG_NODE * log ) {
2013-04-17 00:01:13 -04:00
ROLLBACK_LOG_NODE pinned_log = NULL ;
2013-04-17 00:00:49 -04:00
invariant ( txn - > state = = TOKUTXN_LIVE | | txn - > state = = TOKUTXN_PREPARING ) ; // hot indexing may call this function for prepared transactions
2013-04-17 00:00:15 -04:00
if ( txn_has_current_rollback_log ( txn ) ) {
2013-04-17 00:00:42 -04:00
toku_get_and_pin_rollback_log ( txn , txn - > roll_info . current_rollback , txn - > roll_info . current_rollback_hash , & pinned_log ) ;
toku_rollback_verify_contents ( pinned_log , txn - > txnid64 , txn - > roll_info . num_rollback_nodes - 1 ) ;
2013-04-17 00:00:15 -04:00
} else {
2013-04-17 00:01:13 -04:00
// For each transaction, we try to acquire the first rollback log
// from the rollback log node cache, so that we avoid
// putting something new into the cachetable. However,
// if transaction has spilled rollbacks, that means we
// have already done a lot of work for this transaction,
// and subsequent rollback log nodes are created
// and put into the cachetable. The idea is for
// transactions that don't do a lot of work to (hopefully)
// get a rollback log node from a cache, as opposed to
// taking the more expensive route of creating a new one.
if ( ! txn_has_spilled_rollback_logs ( txn ) ) {
txn - > logger - > rollback_cache . get_rollback_log_node ( txn , & pinned_log ) ;
if ( pinned_log ! = NULL ) {
rollback_initialize_for_txn (
pinned_log ,
txn ,
txn - > roll_info . spilled_rollback_tail ,
txn - > roll_info . spilled_rollback_tail_hash
) ;
txn - > roll_info . current_rollback = pinned_log - > blocknum ;
txn - > roll_info . current_rollback_hash = pinned_log - > hash ;
}
}
if ( pinned_log = = NULL ) {
rollback_log_create ( txn , txn - > roll_info . spilled_rollback_tail , txn - > roll_info . spilled_rollback_tail_hash , & pinned_log ) ;
}
2013-04-16 23:59:05 -04:00
}
2013-04-17 00:00:15 -04:00
assert ( pinned_log - > txnid = = txn - > txnid64 ) ;
2013-04-17 00:00:42 -04:00
assert ( pinned_log - > blocknum . b ! = ROLLBACK_NONE . b ) ;
2013-04-17 00:00:15 -04:00
* log = pinned_log ;
2013-04-16 23:59:05 -04:00
}