Rename serve->has_control to serve->success.

This makes the use of this variable to signal an unexpected SIGTERM
while migrating less confusing.
This commit is contained in:
Alex Young
2012-10-09 17:20:39 +01:00
parent a039ceffcb
commit 161d2fccf1
9 changed files with 128 additions and 104 deletions

View File

@@ -235,7 +235,7 @@ void * control_runner( void * control_uncast )
control_serve( control ); control_serve( control );
control_cleanup( control, 0 ); control_cleanup( control, 0 );
return NULL; pthread_exit( NULL );
} }

View File

@@ -150,8 +150,10 @@ void flexnbd_stop_control( struct flexnbd * flexnbd )
NULLCHECK( flexnbd->control ); NULLCHECK( flexnbd->control );
control_signal_close( flexnbd->control ); control_signal_close( flexnbd->control );
FATAL_UNLESS( 0 == pthread_join( flexnbd->control->thread, NULL ), pthread_t tid = flexnbd->control->thread;
FATAL_UNLESS( 0 == pthread_join( tid, NULL ),
"Failed joining the control thread" ); "Failed joining the control thread" );
debug( "Control thread %p pthread_join returned", tid );
} }
@@ -240,6 +242,7 @@ int flexnbd_serve( struct flexnbd * flexnbd )
} }
success = do_serve( flexnbd->serve ); success = do_serve( flexnbd->serve );
debug("do_serve success is %d", success );
if ( flexnbd->control ) { if ( flexnbd->control ) {
debug( "Stopping control thread" ); debug( "Stopping control thread" );

View File

@@ -11,6 +11,7 @@
#include "util.h" #include "util.h"
#include "bitset.h" #include "bitset.h"
int build_allocation_map(struct bitset_mapping* allocation_map, int fd) int build_allocation_map(struct bitset_mapping* allocation_map, int fd)
{ {
/* break blocking ioctls down */ /* break blocking ioctls down */
@@ -31,9 +32,12 @@ int build_allocation_map(struct bitset_mapping* allocation_map, int fd)
unsigned int i; unsigned int i;
fiemap->fm_start = offset; fiemap->fm_start = offset;
fiemap->fm_length = max_length; fiemap->fm_length = max_length;
if (offset + max_length > allocation_map->size) if ( offset + max_length > allocation_map->size ) {
fiemap->fm_length = allocation_map->size-offset; fiemap->fm_length = allocation_map->size-offset;
}
fiemap->fm_flags = FIEMAP_FLAG_SYNC; fiemap->fm_flags = FIEMAP_FLAG_SYNC;
fiemap->fm_extent_count = max_extents; fiemap->fm_extent_count = max_extents;
fiemap->fm_mapped_extents = 0; fiemap->fm_mapped_extents = 0;
@@ -41,21 +45,20 @@ int build_allocation_map(struct bitset_mapping* allocation_map, int fd)
if ( ioctl( fd, FS_IOC_FIEMAP, fiemap ) < 0 ) { if ( ioctl( fd, FS_IOC_FIEMAP, fiemap ) < 0 ) {
debug( "Couldn't get fiemap, returning no allocation_map" ); debug( "Couldn't get fiemap, returning no allocation_map" );
free(allocation_map); free(allocation_map);
return NULL; allocation_map = NULL;
break;
} }
else {
for ( i = 0; i < fiemap->fm_mapped_extents; i++ ) { for ( i = 0; i < fiemap->fm_mapped_extents; i++ ) {
bitset_set_range( bitset_set_range( allocation_map,
allocation_map,
fiemap->fm_extents[i].fe_logical, fiemap->fm_extents[i].fe_logical,
fiemap->fm_extents[i].fe_length fiemap->fm_extents[i].fe_length );
); }
//debug("range from %ld + %ld", fiemap->fm_extents[i].fe_logical, fiemap->fm_extents[i].fe_length);
} }
} }
debug("Successfully built allocation map"); debug("Successfully built allocation map");
return allocation_map; return NULL != allocation_map;
} }

View File

@@ -406,6 +406,8 @@ int mode_serve( int argc, char *argv[] )
int default_deny = 0; // not on by default int default_deny = 0; // not on by default
int err = 0; int err = 0;
int success;
struct flexnbd * flexnbd; struct flexnbd * flexnbd;
while (1) { while (1) {
@@ -427,10 +429,10 @@ int mode_serve( int argc, char *argv[] )
flexnbd = flexnbd_create_serving( ip_addr, ip_port, file, sock, default_deny, argc - optind, argv + optind, MAX_NBD_CLIENTS ); flexnbd = flexnbd_create_serving( ip_addr, ip_port, file, sock, default_deny, argc - optind, argv + optind, MAX_NBD_CLIENTS );
info( "Serving file %s", file ); info( "Serving file %s", file );
flexnbd_serve( flexnbd ); success = flexnbd_serve( flexnbd );
flexnbd_destroy( flexnbd ); flexnbd_destroy( flexnbd );
return 0; return success ? 0 : 1;
} }

View File

@@ -45,13 +45,13 @@ struct server * server_create (
int acl_entries, int acl_entries,
char** s_acl_entries, char** s_acl_entries,
int max_nbd_clients, int max_nbd_clients,
int has_control) int success)
{ {
NULLCHECK( flexnbd ); NULLCHECK( flexnbd );
struct server * out; struct server * out;
out = xmalloc( sizeof( struct server ) ); out = xmalloc( sizeof( struct server ) );
out->flexnbd = flexnbd; out->flexnbd = flexnbd;
out->has_control = has_control; out->success = success;
out->max_nbd_clients = max_nbd_clients; out->max_nbd_clients = max_nbd_clients;
out->nbd_client = xmalloc( max_nbd_clients * sizeof( struct client_tbl_entry ) ); out->nbd_client = xmalloc( max_nbd_clients * sizeof( struct client_tbl_entry ) );
@@ -678,23 +678,24 @@ int server_mirror_can_start( struct server *serve )
} }
void serve_handle_signal( struct server *params ) /* Queries to see if we are currently mirroring. If we are, we need
* to communicate that via the process exit status. because otherwise
* the supervisor will assume the migration completed.
*/
int serve_shutdown_is_graceful( struct server *params )
{ {
int should_die = 0; int is_mirroring = 0;
server_lock_start_mirror( params ); server_lock_start_mirror( params );
{ {
if ( server_is_mirroring( params ) ) { if ( server_is_mirroring( params ) ) {
should_die = 1; is_mirroring = 1;
warn( "Stop signal received while mirroring." );
server_prevent_mirror_start( params ); server_prevent_mirror_start( params );
} }
} }
server_unlock_start_mirror( params ); server_unlock_start_mirror( params );
if ( should_die ){ return !is_mirroring;
fatal( "Stop signal received while mirroring." );
} else {
server_close_clients( params );
}
} }
@@ -710,6 +711,7 @@ int server_accept( struct server * params )
/* We select on this fd to receive OS signals (only a few of /* We select on this fd to receive OS signals (only a few of
* which we're interested in, see flexnbd.c */ * which we're interested in, see flexnbd.c */
int signal_fd = flexnbd_signal_fd( params->flexnbd ); int signal_fd = flexnbd_signal_fd( params->flexnbd );
int should_continue = 1;
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(params->server_fd, &fds); FD_SET(params->server_fd, &fds);
@@ -722,17 +724,15 @@ int server_accept( struct server * params )
if ( self_pipe_fd_isset( params->close_signal, &fds ) ){ if ( self_pipe_fd_isset( params->close_signal, &fds ) ){
server_close_clients( params ); server_close_clients( params );
return 0; should_continue = 0;
} }
if ( 0 < signal_fd && FD_ISSET( signal_fd, &fds ) ){ if ( 0 < signal_fd && FD_ISSET( signal_fd, &fds ) ){
debug( "Stop signal received." ); debug( "Stop signal received." );
serve_handle_signal( params ); server_close_clients( params );
params->success = serve_shutdown_is_graceful( params );
/* serve_handle_signal will fatal() if it has to, so it should_continue = 0;
* might not return at all.
*/
return 0;
} }
@@ -747,7 +747,7 @@ int server_accept( struct server * params )
accept_nbd_client(params, client_fd, &client_address); accept_nbd_client(params, client_fd, &client_address);
} }
return 1; return should_continue;
} }
@@ -825,13 +825,15 @@ void server_control_arrived( struct server *serve )
{ {
NULLCHECK( serve ); NULLCHECK( serve );
if ( !serve->has_control ) { if ( !serve->success ) {
serve->has_control = 1; serve->success = 1;
serve_signal_close( serve ); serve_signal_close( serve );
} }
} }
void flexnbd_stop_control( struct flexnbd * flexnbd );
/** Closes sockets, frees memory and waits for all client threads to finish */ /** Closes sockets, frees memory and waits for all client threads to finish */
void serve_cleanup(struct server* params, void serve_cleanup(struct server* params,
int fatal __attribute__ ((unused)) ) int fatal __attribute__ ((unused)) )
@@ -864,7 +866,6 @@ void serve_cleanup(struct server* params,
} }
if ( need_mirror_lock ) { server_unlock_start_mirror( params ); } if ( need_mirror_lock ) { server_unlock_start_mirror( params ); }
for (i=0; i < params->max_nbd_clients; i++) { for (i=0; i < params->max_nbd_clients; i++) {
pthread_t thread_id = params->nbd_client[i].thread; pthread_t thread_id = params->nbd_client[i].thread;
@@ -882,6 +883,15 @@ void serve_cleanup(struct server* params,
server_unlock_acl( params ); server_unlock_acl( params );
} }
/* if( params->flexnbd ) { */
/* if ( params->flexnbd->control ) { */
/* flexnbd_stop_control( params->flexnbd ); */
/* } */
/* flexnbd_destroy( params->flexnbd ); */
/* } */
/* server_destroy( params ); */
debug( "Cleanup done"); debug( "Cleanup done");
} }
@@ -889,7 +899,7 @@ void serve_cleanup(struct server* params,
int server_is_in_control( struct server *serve ) int server_is_in_control( struct server *serve )
{ {
NULLCHECK( serve ); NULLCHECK( serve );
return serve->has_control; return serve->success;
} }
int server_is_mirroring( struct server * serve ) int server_is_mirroring( struct server * serve )
@@ -911,7 +921,9 @@ void server_abandon_mirror( struct server * serve )
* the next pass. * the next pass.
* */ * */
serve->mirror->signal_abandon = 1; serve->mirror->signal_abandon = 1;
pthread_join( serve->mirror_super->thread, NULL ); pthread_t tid = serve->mirror_super->thread;
pthread_join( tid, NULL );
debug( "Mirror thread %p pthread_join returned", tid );
} }
} }
@@ -926,15 +938,15 @@ int do_serve(struct server* params)
{ {
NULLCHECK( params ); NULLCHECK( params );
int has_control; int success;
error_set_handler((cleanup_handler*) serve_cleanup, params); error_set_handler((cleanup_handler*) serve_cleanup, params);
serve_open_server_socket(params); serve_open_server_socket(params);
serve_init_allocation_map(params); serve_init_allocation_map(params);
serve_accept_loop(params); serve_accept_loop(params);
has_control = params->has_control; success = params->success;
serve_cleanup(params, 0); serve_cleanup(params, 0);
return has_control; return success;
} }

View File

@@ -91,8 +91,11 @@ struct server {
/* Marker for whether this server has control over the data in /* Marker for whether this server has control over the data in
* the file, or if we're waiting to receive it from an inbound * the file, or if we're waiting to receive it from an inbound
* migration which hasn't yet finished. * migration which hasn't yet finished.
*
* It's the value which controls the exit status of a serve or
* listen process.
*/ */
int has_control; int success;
}; };
struct server * server_create( struct server * server_create(
@@ -104,7 +107,7 @@ struct server * server_create(
int acl_entries, int acl_entries,
char** s_acl_entries, char** s_acl_entries,
int max_nbd_clients, int max_nbd_clients,
int has_control ); int success );
void server_destroy( struct server * ); void server_destroy( struct server * );
int server_is_closed(struct server* serve); int server_is_closed(struct server* serve);
void server_dirty(struct server *serve, off64_t from, int len); void server_dirty(struct server *serve, off64_t from, int len);

View File

@@ -9,7 +9,7 @@ struct status * status_create( struct server * serve )
status = xmalloc( sizeof( struct status ) ); status = xmalloc( sizeof( struct status ) );
status->pid = getpid(); status->pid = getpid();
status->has_control = serve->has_control; status->has_control = server_is_in_control( serve );
status->is_mirroring = NULL != serve->mirror; status->is_mirroring = NULL != serve->mirror;
return status; return status;

View File

@@ -310,13 +310,14 @@ module FlexNBD
debug( cmd ) debug( cmd )
@pid = @executor.run( cmd ) @pid = @executor.run( cmd )
start_wait_thread( @pid )
while !File.socket?(ctrl) while !File.socket?(ctrl)
pid, status = Process.wait2(@pid, Process::WNOHANG) pid, status = Process.wait2(@pid, Process::WNOHANG)
raise "server did not start (#{cmd})" if pid raise "server did not start (#{cmd})" if pid
sleep 0.1 sleep 0.1
end end
start_wait_thread( @pid )
at_exit { kill } at_exit { kill }
end end
private :run_serve_cmd private :run_serve_cmd

View File

@@ -20,7 +20,7 @@ class TestSourceErrorHandling < Test::Unit::TestCase
def expect_term_during_migration def expect_term_during_migration
@env.nbd1.can_die(6,9) @env.nbd1.can_die(1,9)
end end