Clean up service.h

Most of the service header functions now contain the relevant
documentation. Cleaned up small parts of it and renamed functions to be
more consistent.
This commit is contained in:
Markus Makela
2016-11-25 12:15:52 +02:00
parent ff3173d588
commit 6ea4e50f2c
6 changed files with 208 additions and 144 deletions

View File

@ -142,25 +142,24 @@ typedef struct service
int max_connections; /**< Maximum client connections */ int max_connections; /**< Maximum client connections */
QUEUE_CONFIG *queued_connections; /**< Queued connections, if set */ QUEUE_CONFIG *queued_connections; /**< Queued connections, if set */
SERV_LISTENER *ports; /**< Linked list of ports and protocols SERV_LISTENER *ports; /**< Linked list of ports and protocols
* that this service will listen on. * that this service will listen on */
*/
char *routerModule; /**< Name of router module to use */ char *routerModule; /**< Name of router module to use */
char **routerOptions; /**< Router specific option strings */ char **routerOptions; /**< Router specific option strings */
struct router_object *router; /**< The router we are using */ struct router_object *router; /**< The router we are using */
void *router_instance; /**< The router instance for this service */ void *router_instance; /**< The router instance for this service */
char *version_string; /** version string for this service listeners */ char *version_string; /**< version string for this service listeners */
SERVER_REF *dbref; /** server references */ SERVER_REF *dbref; /**< server references */
int n_dbref; /** Number of server references */ int n_dbref; /**< Number of server references */
SERVICE_USER credentials; /**< The cedentials of the service user */ SERVICE_USER credentials; /**< The cedentials of the service user */
SPINLOCK spin; /**< The service spinlock */ SPINLOCK spin; /**< The service spinlock */
SERVICE_STATS stats; /**< The service statistics */ SERVICE_STATS stats; /**< The service statistics */
int enable_root; /**< Allow root user access */ int enable_root; /**< Allow root user access */
int localhost_match_wildcard_host; /**< Match localhost against wildcard */ int localhost_match_wildcard_host; /**< Match localhost against wildcard */
CONFIG_PARAMETER* svc_config_param;/*< list of config params and values */ CONFIG_PARAMETER* svc_config_param;/**< list of config params and values */
int svc_config_version; /*< Version number of configuration */ int svc_config_version; /**< Version number of configuration */
bool svc_do_shutdown; /*< tells the service to exit loops etc. */ bool svc_do_shutdown; /**< tells the service to exit loops etc. */
bool users_from_all; /*< Load users from one server or all of them */ bool users_from_all; /**< Load users from one server or all of them */
bool strip_db_esc; /*< Remove the '\' characters from database names bool strip_db_esc; /**< Remove the '\' characters from database names
* when querying them from the server. MySQL Workbench seems * when querying them from the server. MySQL Workbench seems
* to escape at least the underscore character. */ * to escape at least the underscore character. */
SPINLOCK users_table_spin; /**< The spinlock for users data refresh */ SPINLOCK users_table_spin; /**< The spinlock for users data refresh */
@ -168,11 +167,11 @@ typedef struct service
FILTER_DEF **filters; /**< Ordered list of filters */ FILTER_DEF **filters; /**< Ordered list of filters */
int n_filters; /**< Number of filters */ int n_filters; /**< Number of filters */
long conn_idle_timeout; /**< Session timeout in seconds */ long conn_idle_timeout; /**< Session timeout in seconds */
char *weightby; char *weightby; /**< Service weighting parameter name */
struct service *next; /**< The next service in the linked list */ struct service *next; /**< The next service in the linked list */
bool retry_start; /*< If starting of the service should be retried later */ bool retry_start; /**< If starting of the service should be retried later */
bool log_auth_warnings; /*< Log authentication failures and warnings */ bool log_auth_warnings; /**< Log authentication failures and warnings */
uint64_t capabilities; /*< The capabilities of the service. */ uint64_t capabilities; /**< The capabilities of the service. */
} SERVICE; } SERVICE;
typedef enum count_spec_t typedef enum count_spec_t
@ -188,60 +187,144 @@ typedef enum count_spec_t
#define SERVICE_STATE_FAILED 3 /**< The service failed to start */ #define SERVICE_STATE_FAILED 3 /**< The service failed to start */
#define SERVICE_STATE_STOPPED 4 /**< The service has been stopped */ #define SERVICE_STATE_STOPPED 4 /**< The service has been stopped */
extern SERVICE *service_alloc(const char *, const char *); /**
extern int service_free(SERVICE *); * Service life cycle management
extern SERVICE *service_find(const char *); *
extern int service_isvalid(SERVICE *); * These functions should only be called by the MaxScale core.
extern SERV_LISTENER* serviceCreateListener(SERVICE *service, const char *name, const char *protocol, */
const char *address, unsigned short port, const char *authenticator,
/**
* @brief Allocate a new service
*
* @param name The service name
* @param router The router module this service uses
*
* @return The newly created service or NULL if an error occurred
*/
SERVICE* service_alloc(const char *name, const char *router);
/**
* @brief Free the specified service
*
* @param service The service to free
*/
void service_free(SERVICE *service);
/**
* @brief Shut all services down
*
* Stops all services and calls the destroyInstance entry points for all routers
* and filter. This should only be called once by the main shutdown code.
*/
void service_shutdown(void);
/**
* @brief Launch all services
*
* Initialize and start all services. This should only be called once by the
* main initialization code.
*
* @return Number of successfully started services
*/
int service_launch_all(void);
/**
* Creating and adding new components to services
*/
SERV_LISTENER* serviceCreateListener(SERVICE *service, const char *name,
const char *protocol, const char *address,
unsigned short port, const char *authenticator,
const char *options, SSL_LISTENER *ssl); const char *options, SSL_LISTENER *ssl);
extern int serviceHasProtocol(SERVICE *service, const char *protocol, int serviceHasProtocol(SERVICE *service, const char *protocol,
const char* address, unsigned short port); const char* address, unsigned short port);
extern void serviceAddBackend(SERVICE *, SERVER *); void serviceAddBackend(SERVICE *service, SERVER *server);
extern void serviceRemoveBackend(SERVICE *, const SERVER *); void serviceRemoveBackend(SERVICE *service, const SERVER *server);
extern bool serviceHasBackend(SERVICE *, SERVER *); bool serviceHasBackend(SERVICE *service, SERVER *server);
extern void serviceAddRouterOption(SERVICE *, char *);
extern void serviceClearRouterOptions(SERVICE *); /**
extern int serviceStart(SERVICE *); * Starting and stopping services
extern int serviceStartAll(); */
extern bool serviceListen(SERVICE *service, SERV_LISTENER *port);
extern int serviceStop(SERVICE *); /**
extern int serviceRestart(SERVICE *); * @brief Stop a service
extern int serviceSetUser(SERVICE *, char *, char *); *
extern int serviceGetUser(SERVICE *, char **, char **); * @param service Service to stop
extern bool serviceSetFilters(SERVICE *, char *); * @return True if service was stopped
extern int serviceSetSSL(SERVICE *service, char* action); */
extern int serviceSetSSLVersion(SERVICE *service, char* version); bool serviceStop(SERVICE *service);
extern int serviceSetSSLVerifyDepth(SERVICE* service, int depth);
extern void serviceSetCertificates(SERVICE *service, char* cert, char* key, char* ca_cert); /**
extern int serviceEnableRootUser(SERVICE *, int ); * @brief Restart a stopped service
extern int serviceSetTimeout(SERVICE *, int ); *
extern int serviceSetConnectionLimits(SERVICE *, int, int, int); * @param service Service to restart
extern void serviceSetRetryOnFailure(SERVICE *service, char* value); * @return True if service was restarted
extern void serviceWeightBy(SERVICE *, char *); */
extern char *serviceGetWeightingParameter(SERVICE *); bool serviceStart(SERVICE *service);
extern int serviceEnableLocalhostMatchWildcardHost(SERVICE *, int);
extern int serviceStripDbEsc(SERVICE* service, int action); /**
extern int serviceAuthAllServers(SERVICE *service, int action); * @brief Start a listener for a service
extern void service_update(SERVICE *, char *, char *, char *); *
extern int service_refresh_users(SERVICE *); * @param service Service where the listener is linked
extern void printService(SERVICE *); * @param port Listener to start
extern void printAllServices(); * @return True if listener was started
extern void dprintAllServices(DCB *); */
extern bool service_set_param_value(SERVICE* service, bool serviceStartListener(SERVICE *service, SERV_LISTENER *port);
CONFIG_PARAMETER* param,
char* valstr, /**
count_spec_t count_spec, * @brief Stop a listener for a service
config_param_type_t type); *
extern void dprintService(DCB *, SERVICE *); * @param service Service where the listener is linked
extern void dListServices(DCB *); * @param name Name of the listener
extern void dListListeners(DCB *); * @return True if listener was stopped
extern char* service_get_name(SERVICE* svc); */
extern void service_shutdown(); bool serviceStopListener(SERVICE *service, const char *name);
extern int serviceSessionCountAll();
extern RESULTSET *serviceGetList(); /**
extern RESULTSET *serviceGetListenerList(); * Utility functions
extern bool service_all_services_have_listeners(); */
char* service_get_name(SERVICE* service);
bool service_all_services_have_listeners(void);
SERVICE* service_find(const char *name);
int service_isvalid(SERVICE *service);
/**
* Alteration of the service configuration
*/
void serviceAddRouterOption(SERVICE *service, char *option);
void serviceClearRouterOptions(SERVICE *service);
int serviceSetUser(SERVICE *service, char *user, char *auth);
int serviceGetUser(SERVICE *service, char **user, char **auth);
bool serviceSetFilters(SERVICE *service, char *filters);
int serviceSetSSL(SERVICE *service, char* action);
int serviceSetSSLVersion(SERVICE *service, char* version);
int serviceSetSSLVerifyDepth(SERVICE* service, int depth);
void serviceSetCertificates(SERVICE *service, char* cert, char* key, char* ca_cert);
int serviceEnableRootUser(SERVICE *service, int action);
int serviceSetTimeout(SERVICE *service, int val);
int serviceSetConnectionLimits(SERVICE *service, int max, int queued, int timeout);
void serviceSetRetryOnFailure(SERVICE *service, char* value);
void serviceWeightBy(SERVICE *service, char *weightby);
char* serviceGetWeightingParameter(SERVICE *service);
int serviceEnableLocalhostMatchWildcardHost(SERVICE *service, int action);
int serviceStripDbEsc(SERVICE* service, int action);
int serviceAuthAllServers(SERVICE *service, int action);
void service_update(SERVICE *service, char *router, char *user, char *auth);
int service_refresh_users(SERVICE *service);
bool service_set_param_value(SERVICE* service, CONFIG_PARAMETER* param, char* valstr,
count_spec_t count_spec, config_param_type_t type);
/**
* Diagnostics
*/
void printService(SERVICE *service);
void printAllServices(void);
void dprintAllServices(DCB *dcb);
void dprintService(DCB *dcb, SERVICE *service);
void dListServices(DCB *dcb);
void dListListeners(DCB *dcb);
int serviceSessionCountAll(void);
RESULTSET* serviceGetList(void);
RESULTSET* serviceGetListenerList(void);
/** /**
* Get the capabilities of the servive. * Get the capabilities of the servive.

View File

@ -414,7 +414,7 @@ bool runtime_create_listener(SERVICE *service, const char *name, const char *add
SERV_LISTENER *listener = serviceCreateListener(service, name, proto, addr, SERV_LISTENER *listener = serviceCreateListener(service, name, proto, addr,
u_port, auth, auth_opt, ssl); u_port, auth, auth_opt, ssl);
if (listener && listener_serialize(listener) && serviceListen(service, listener)) if (listener && listener_serialize(listener) && serviceStartListener(service, listener))
{ {
MXS_NOTICE("Listener '%s' at %s:%s for service '%s' created", MXS_NOTICE("Listener '%s' at %s:%s for service '%s' created",
name, print_addr, port, service->name); name, print_addr, port, service->name);

View File

@ -1951,7 +1951,7 @@ int main(int argc, char **argv)
monitorStartAll(); monitorStartAll();
/** Start the services that were created above */ /** Start the services that were created above */
n_services = serviceStartAll(); n_services = service_launch_all();
if (n_services == 0) if (n_services == 0)
{ {

View File

@ -103,32 +103,21 @@ static void service_internal_restart(void *data);
static void service_queue_check(void *data); static void service_queue_check(void *data);
static void service_calculate_weights(SERVICE *service); static void service_calculate_weights(SERVICE *service);
/** SERVICE* service_alloc(const char *name, const char *router)
* Allocate a new service for the gateway to support
*
*
* @param servname The service name
* @param router Name of the router module this service uses
*
* @return The newly created service or NULL if an error occurred
*/
SERVICE *
service_alloc(const char *servname, const char *router)
{ {
servname = MXS_STRDUP(servname); char *my_name = MXS_STRDUP(name);
router = MXS_STRDUP(router); char *my_router = MXS_STRDUP(router);
SERVICE *service = (SERVICE *)MXS_CALLOC(1, sizeof(*service)); SERVICE *service = (SERVICE *)MXS_CALLOC(1, sizeof(*service));
if (!servname || !router || !service) if (!my_name || !my_router || !service)
{ {
MXS_FREE((void*)servname); MXS_FREE(my_name);
MXS_FREE((void*)router); MXS_FREE(my_router);
MXS_FREE(service); MXS_FREE(service);
return NULL; return NULL;
} }
if ((service->router = load_module(router, MODULE_ROUTER)) == NULL) if ((service->router = load_module(my_router, MODULE_ROUTER)) == NULL)
{ {
char* home = get_libdir(); char* home = get_libdir();
char* ldpath = getenv("LD_LIBRARY_PATH"); char* ldpath = getenv("LD_LIBRARY_PATH");
@ -138,13 +127,13 @@ service_alloc(const char *servname, const char *router)
"following directories :\n\t\t\t " "following directories :\n\t\t\t "
"- %s\n%s%s", "- %s\n%s%s",
MODULE_ROUTER, MODULE_ROUTER,
router, my_router,
router, my_router,
home, home,
ldpath ? "\t\t\t - " : "", ldpath ? "\t\t\t - " : "",
ldpath ? ldpath : ""); ldpath ? ldpath : "");
MXS_FREE((void*)servname); MXS_FREE(my_name);
MXS_FREE((void*)router); MXS_FREE(my_router);
MXS_FREE(service); MXS_FREE(service);
return NULL; return NULL;
} }
@ -152,8 +141,8 @@ service_alloc(const char *servname, const char *router)
service->capabilities = service->router->getCapabilities(); service->capabilities = service->router->getCapabilities();
service->client_count = 0; service->client_count = 0;
service->n_dbref = 0; service->n_dbref = 0;
service->name = (char*)servname; service->name = my_name;
service->routerModule = (char*)router; service->routerModule = my_router;
service->users_from_all = false; service->users_from_all = false;
service->queued_connections = NULL; service->queued_connections = NULL;
service->localhost_match_wildcard_host = SERVICE_PARAM_UNINIT; service->localhost_match_wildcard_host = SERVICE_PARAM_UNINIT;
@ -478,8 +467,7 @@ static void free_string_array(char** array)
* @param service The Service that should be started * @param service The Service that should be started
* @return Returns the number of listeners created * @return Returns the number of listeners created
*/ */
int int serviceInitialize(SERVICE *service)
serviceStart(SERVICE *service)
{ {
/** Calculate the server weights */ /** Calculate the server weights */
service_calculate_weights(service); service_calculate_weights(service);
@ -502,24 +490,36 @@ serviceStart(SERVICE *service)
return listeners; return listeners;
} }
/** bool serviceStartListener(SERVICE *service, SERV_LISTENER *port)
* Start an individual listener
*
* @param service The service to start the listener for
* @param port The port number
*/
bool serviceListen(SERVICE *service, SERV_LISTENER *port)
{ {
return serviceStartPort(service, port); return serviceStartPort(service, port);
} }
/** bool serviceStopListener(SERVICE *service, const char *name)
* Start all the services {
* bool rval = false;
* @return Return the number of services started
*/ spinlock_acquire(&service->spin);
int
serviceStartAll() for (SERV_LISTENER *port = service->ports; port; port = port->next)
{
if (strcmp(port->name, name) == 0)
{
if (poll_remove_dcb(port->listener) == 0)
{
port->listener->session->state = SESSION_STATE_LISTENER_STOPPED;
rval = true;
}
break;
}
}
spinlock_release(&service->spin);
return rval;
}
int service_launch_all()
{ {
SERVICE *ptr; SERVICE *ptr;
int n = 0, i; int n = 0, i;
@ -530,7 +530,7 @@ serviceStartAll()
ptr = allServices; ptr = allServices;
while (ptr && !ptr->svc_do_shutdown) while (ptr && !ptr->svc_do_shutdown)
{ {
n += (i = serviceStart(ptr)); n += (i = serviceInitialize(ptr));
if (i == 0) if (i == 0)
{ {
@ -543,16 +543,7 @@ serviceStartAll()
return error ? 0 : n; return error ? 0 : n;
} }
/** bool serviceStop(SERVICE *service)
* Stop a service
*
* This function stops the listener for the service
*
* @param service The Service that should be stopped
* @return Returns the number of listeners restarted
*/
int
serviceStop(SERVICE *service)
{ {
SERV_LISTENER *port; SERV_LISTENER *port;
int listeners = 0; int listeners = 0;
@ -572,7 +563,7 @@ serviceStop(SERVICE *service)
} }
service->state = SERVICE_STATE_STOPPED; service->state = SERVICE_STATE_STOPPED;
return listeners; return listeners > 0;
} }
/** /**
@ -583,8 +574,7 @@ serviceStop(SERVICE *service)
* @param service The Service that should be restarted * @param service The Service that should be restarted
* @return Returns the number of listeners restarted * @return Returns the number of listeners restarted
*/ */
int bool serviceStart(SERVICE *service)
serviceRestart(SERVICE *service)
{ {
SERV_LISTENER *port; SERV_LISTENER *port;
int listeners = 0; int listeners = 0;
@ -603,24 +593,16 @@ serviceRestart(SERVICE *service)
port = port->next; port = port->next;
} }
service->state = SERVICE_STATE_STARTED; service->state = SERVICE_STATE_STARTED;
return listeners; return listeners > 0;
} }
void service_free(SERVICE *service)
/**
* Deallocate the specified service
*
* @param service The service to deallocate
* @return Returns true if the service was freed
*/
int
service_free(SERVICE *service)
{ {
SERVICE *ptr; SERVICE *ptr;
SERVER_REF *srv; SERVER_REF *srv;
if (service->stats.n_current) if (service->stats.n_current)
{ {
return 0; return;
} }
/* First of all remove from the linked list */ /* First of all remove from the linked list */
spinlock_acquire(&service_spin); spinlock_acquire(&service_spin);
@ -661,7 +643,6 @@ service_free(SERVICE *service)
serviceClearRouterOptions(service); serviceClearRouterOptions(service);
MXS_FREE(service); MXS_FREE(service);
return 1;
} }
/** /**

View File

@ -1776,7 +1776,7 @@ shutdown_service(DCB *dcb, SERVICE *service)
static void static void
restart_service(DCB *dcb, SERVICE *service) restart_service(DCB *dcb, SERVICE *service)
{ {
serviceRestart(service); serviceStart(service);
} }
/** /**

View File

@ -694,7 +694,7 @@ void exec_restart_service(DCB *dcb, MAXINFO_TREE *tree)
SERVICE* service = service_find(tree->value); SERVICE* service = service_find(tree->value);
if (service) if (service)
{ {
serviceRestart(service); serviceStart(service);
maxinfo_send_ok(dcb); maxinfo_send_ok(dcb);
} }
else else