/** * test1 Allocate a server and do lots of other things * */ static int test1() { SERVER *server; int result; char *status; /* Server tests */ ss_dfprintf(stderr, "testserver : creating server called MyServer"); server = server_alloc("MyServer", "HTTPD", 9876); skygw_log_sync_all(); //ss_info_dassert(NULL != service, "New server with valid protocol and port must not be null"); //ss_info_dassert(0 != service_isvalid(service), "Service must be valid after creation"); ss_dfprintf(stderr, "\t..done\nTest Parameter for Server."); ss_info_dassert(NULL == serverGetParameter(server, "name"), "Parameter should be null when not set"); serverAddParameter(server, "name", "value"); skygw_log_sync_all(); ss_info_dassert(0 == strcmp("value", serverGetParameter(server, "name")), "Parameter should be returned correctly"); ss_dfprintf(stderr, "\t..done\nTesting Unique Name for Server."); ss_info_dassert(NULL == server_find_by_unique_name("uniquename"), "Should not find non-existent unique name."); server_set_unique_name(server, "uniquename"); skygw_log_sync_all(); ss_info_dassert(server == server_find_by_unique_name("uniquename"), "Should find by unique name."); ss_dfprintf(stderr, "\t..done\nTesting Status Setting for Server."); status = server_status(server); skygw_log_sync_all(); ss_info_dassert(0 == strcmp("Running", status), "Status of Server should be Running by default."); if (NULL != status) free(status); server_set_status(server, SERVER_MASTER); status = server_status(server); skygw_log_sync_all(); ss_info_dassert(0 == strcmp("Master, Running", status), "Should find correct status."); server_clear_status(server, SERVER_MASTER); free(status); status = server_status(server); skygw_log_sync_all(); ss_info_dassert(0 == strcmp("Running", status), "Status of Server should be Running after master status cleared."); if (NULL != status) free(status); ss_dfprintf(stderr, "\t..done\nRun Prints for Server and all Servers."); printServer(server); printAllServers(); skygw_log_sync_all(); ss_dfprintf(stderr, "\t..done\nFreeing Server."); ss_info_dassert(0 != server_free(server), "Free should succeed"); ss_dfprintf(stderr, "\t..done\n"); return 0; }
/** * get candidate master from all nodes * * The current available rule: get the server with MIN(node_id) * node_id comes from 'wsrep_local_index' variable * * @param servers The monitored servers list * @return The candidate master on success, NULL on failure */ static MONITOR_SERVERS *get_candidate_master(MONITOR* mon) { MONITOR_SERVERS *moitor_servers = mon->databases; MONITOR_SERVERS *candidate_master = NULL; GALERA_MONITOR* handle = mon->handle; long min_id = -1; int minval = INT_MAX; int currval; char* value; /* set min_id to the lowest value of moitor_servers->server->node_id */ while (moitor_servers) { if (!SERVER_IN_MAINT(moitor_servers->server) && SERVER_IS_JOINED(moitor_servers->server)) { moitor_servers->server->depth = 0; if (handle->use_priority && (value = serverGetParameter(moitor_servers->server, "priority")) != NULL) { currval = atoi(value); if (currval < minval && currval > 0) { minval = currval; candidate_master = moitor_servers; } } else if (moitor_servers->server->node_id >= 0 && (!handle->use_priority || /** Server priority disabled*/ candidate_master == NULL || /** No candidate chosen */ serverGetParameter(candidate_master->server, "priority") == NULL)) /** Candidate has no priority */ { if (min_id < 0 || moitor_servers->server->node_id < min_id) { min_id = moitor_servers->server->node_id; candidate_master = moitor_servers; } } } moitor_servers = moitor_servers->next; } return candidate_master; }
/** * Create an instance of the router for a particular service * within the gateway. * * @param service The service this router is being create for * @param options An array of options for this query router * * @return The instance data for this new instance */ static ROUTER * createInstance(SERVICE *service, char **options) { ROUTER_INSTANCE *inst; SERVER *server; SERVER_REF *sref; int i, n; BACKEND *backend; char *weightby; if ((inst = calloc(1, sizeof(ROUTER_INSTANCE))) == NULL) { return NULL; } inst->service = service; spinlock_init(&inst->lock); /* * We need an array of the backend servers in the instance structure so * that we can maintain a count of the number of connections to each * backend server. */ for (sref = service->dbref, n = 0; sref; sref = sref->next) n++; inst->servers = (BACKEND **)calloc(n + 1, sizeof(BACKEND *)); if (!inst->servers) { free(inst); return NULL; } for (sref = service->dbref, n = 0; sref; sref = sref->next) { if ((inst->servers[n] = malloc(sizeof(BACKEND))) == NULL) { for (i = 0; i < n; i++) free(inst->servers[i]); free(inst->servers); free(inst); return NULL; } inst->servers[n]->server = sref->server; inst->servers[n]->current_connection_count = 0; inst->servers[n]->weight = 1000; n++; } inst->servers[n] = NULL; if ((weightby = serviceGetWeightingParameter(service)) != NULL) { int total = 0; for (n = 0; inst->servers[n]; n++) { backend = inst->servers[n]; total += atoi(serverGetParameter(backend->server, weightby)); } if (total == 0) { LOGIF(LE, (skygw_log_write(LOGFILE_ERROR, "WARNING: Weighting Parameter for service '%s' " "will be ignored as no servers have values " "for the parameter '%s'.\n", service->name, weightby))); } else { for (n = 0; inst->servers[n]; n++) { int perc, wght; backend = inst->servers[n]; perc = ((wght = atoi(serverGetParameter(backend->server, weightby))) * 1000) / total; if (perc == 0 && wght != 0) perc = 1; backend->weight = perc; if (perc == 0) { LOGIF(LE, (skygw_log_write( LOGFILE_ERROR, "Server '%s' has no value " "for weighting parameter '%s', " "no queries will be routed to " "this server.\n", inst->servers[n]->server->unique_name, weightby))); } } } } /* * Process the options */ inst->bitmask = 0; inst->bitvalue = 0; if (options) { for (i = 0; options[i]; i++) { if (!strcasecmp(options[i], "master")) { inst->bitmask |= (SERVER_MASTER|SERVER_SLAVE); inst->bitvalue |= SERVER_MASTER; } else if (!strcasecmp(options[i], "slave")) { inst->bitmask |= (SERVER_MASTER|SERVER_SLAVE); inst->bitvalue |= SERVER_SLAVE; } else if (!strcasecmp(options[i], "running")) { inst->bitmask |= (SERVER_RUNNING); inst->bitvalue |= SERVER_RUNNING; } else if (!strcasecmp(options[i], "synced")) { inst->bitmask |= (SERVER_JOINED); inst->bitvalue |= SERVER_JOINED; } else if (!strcasecmp(options[i], "ndb")) { inst->bitmask |= (SERVER_NDB); inst->bitvalue |= SERVER_NDB; } else { LOGIF(LM, (skygw_log_write( LOGFILE_MESSAGE, "* Warning : Unsupported router " "option \'%s\' for readconnroute. " "Expected router options are " "[slave|master|synced|ndb]", options[i]))); } } } if(inst->bitmask == 0 && inst->bitvalue == 0) { /** No parameters given, use RUNNING as a valid server */ inst->bitmask |= (SERVER_RUNNING); inst->bitvalue |= SERVER_RUNNING; } /* * We have completed the creation of the instance data, so now * insert this router instance into the linked list of routers * that have been created with this module. */ spinlock_acquire(&instlock); inst->next = instances; instances = inst; spinlock_release(&instlock); return (ROUTER *)inst; }