2006-11-28 09:56:10 +03:00
/*
ctdb utility code
Copyright ( C ) Andrew Tridgell 2006
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation ; either
version 2 of the License , or ( at your option ) any later version .
This library 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
Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public
License along with this library ; if not , write to the Free Software
Foundation , Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
*/
# include "includes.h"
# include "lib/events/events.h"
2007-04-16 04:21:44 +04:00
# include "lib/tdb/include/tdb.h"
2006-11-28 09:56:10 +03:00
# include "system/network.h"
# include "system/filesys.h"
2007-04-16 04:21:44 +04:00
# include "../include/ctdb_private.h"
2006-11-28 09:56:10 +03:00
2007-04-17 16:13:06 +04:00
int LogLevel ;
2006-11-28 09:56:10 +03:00
/*
return error string for last error
*/
const char * ctdb_errstr ( struct ctdb_context * ctdb )
{
return ctdb - > err_msg ;
}
/*
remember an error message
*/
void ctdb_set_error ( struct ctdb_context * ctdb , const char * fmt , . . . )
{
va_list ap ;
talloc_free ( ctdb - > err_msg ) ;
va_start ( ap , fmt ) ;
ctdb - > err_msg = talloc_vasprintf ( ctdb , fmt , ap ) ;
2007-04-16 04:31:39 +04:00
DEBUG ( 0 , ( " ctdb error: %s \n " , ctdb - > err_msg ) ) ;
2006-11-28 09:56:10 +03:00
va_end ( ap ) ;
}
2006-12-18 08:01:11 +03:00
/*
a fatal internal error occurred - no hope for recovery
*/
void ctdb_fatal ( struct ctdb_context * ctdb , const char * msg )
{
2007-04-16 04:31:39 +04:00
DEBUG ( 0 , ( " ctdb fatal error: %s \n " , msg ) ) ;
2006-12-18 08:01:11 +03:00
abort ( ) ;
}
2006-11-28 09:56:10 +03:00
/*
parse a IP : port pair
*/
int ctdb_parse_address ( struct ctdb_context * ctdb ,
TALLOC_CTX * mem_ctx , const char * str ,
struct ctdb_address * address )
{
char * p ;
p = strchr ( str , ' : ' ) ;
if ( p = = NULL ) {
ctdb_set_error ( ctdb , " Badly formed node '%s' \n " , str ) ;
return - 1 ;
}
address - > address = talloc_strndup ( mem_ctx , str , p - str ) ;
address - > port = strtoul ( p + 1 , NULL , 0 ) ;
return 0 ;
}
/*
check if two addresses are the same
*/
bool ctdb_same_address ( struct ctdb_address * a1 , struct ctdb_address * a2 )
{
return strcmp ( a1 - > address , a2 - > address ) = = 0 & & a1 - > port = = a2 - > port ;
}
/*
hash function for mapping data to a VNN - taken from tdb
*/
2006-12-18 08:01:11 +03:00
uint32_t ctdb_hash ( const TDB_DATA * key )
2006-11-28 09:56:10 +03:00
{
uint32_t value ; /* Used to compute the hash value. */
uint32_t i ; /* Used to cycle through random values. */
/* Set the initial value from the key size. */
for ( value = 0x238F13AF * key - > dsize , i = 0 ; i < key - > dsize ; i + + )
value = ( value + ( key - > dptr [ i ] < < ( i * 5 % 24 ) ) ) ;
return ( 1103515243 * value + 12345 ) ;
}
2007-04-18 12:39:02 +04:00
2007-04-30 17:31:40 +04:00
/*
hash function for a string
*/
uint32_t ctdb_hash_string ( const char * str )
{
TDB_DATA data ;
data . dptr = ( uint8_t * ) discard_const ( str ) ;
data . dsize = strlen ( str ) + 1 ;
return ctdb_hash ( & data ) ;
}
2007-04-19 05:28:01 +04:00
/*
a type checking varient of idr_find
*/
2007-04-23 12:19:50 +04:00
static void * _idr_find_type ( struct idr_context * idp , int id , const char * type , const char * location )
2007-04-19 05:28:01 +04:00
{
void * p = idr_find ( idp , id ) ;
if ( p & & talloc_check_name ( p , type ) = = NULL ) {
DEBUG ( 0 , ( " %s idr_find_type expected type %s but got %s \n " ,
location , type , talloc_get_name ( p ) ) ) ;
return NULL ;
}
return p ;
}
2007-04-20 14:07:47 +04:00
/*
update a max latency number
*/
void ctdb_latency ( double * latency , struct timeval t )
{
double l = timeval_elapsed ( & t ) ;
if ( l > * latency ) {
* latency = l ;
}
}
2007-04-23 12:19:50 +04:00
uint32_t ctdb_reqid_new ( struct ctdb_context * ctdb , void * state )
{
uint32_t id ;
id = ctdb - > idr_cnt + + & 0xFFFF ;
id | = ( idr_get_new ( ctdb - > idr , state , 0xFFFF ) < < 16 ) ;
return id ;
}
void * _ctdb_reqid_find ( struct ctdb_context * ctdb , uint32_t reqid , const char * type , const char * location )
{
void * p ;
p = _idr_find_type ( ctdb - > idr , ( reqid > > 16 ) & 0xFFFF , type , location ) ;
if ( p = = NULL ) {
DEBUG ( 0 , ( " Could not find idr:%d \n " , reqid ) ) ;
}
return p ;
}
void ctdb_reqid_remove ( struct ctdb_context * ctdb , uint32_t reqid )
{
int ret ;
ret = idr_remove ( ctdb - > idr , ( reqid > > 16 ) & 0xFFFF ) ;
if ( ret ! = 0 ) {
DEBUG ( 0 , ( " Removing idr that does not exist \n " ) ) ;
}
}