At service startup service->rate_limit.last is set back to USERS_REFRESH_TIME

At service startup service->rate_limit.last is set back to
USERS_REFRESH_TIME
This allows reloading users’ table as soon as possible

Fixed mysql_users_alloc instead of users_alloc in replace_mysql_users()
and added MYSQL_USER_MAXLEN where missing
This commit is contained in:
MassimilianoPinto
2014-02-28 18:26:24 +01:00
parent 92d94e16ac
commit c6cfc99ee3
2 changed files with 19 additions and 9 deletions

View File

@ -109,7 +109,7 @@ replace_mysql_users(SERVICE *service)
int i; int i;
struct users *newusers, *oldusers; struct users *newusers, *oldusers;
if ((newusers = users_alloc()) == NULL) if ((newusers = mysql_users_alloc()) == NULL)
return -1; return -1;
i = getUsers(service, newusers); i = getUsers(service, newusers);
@ -120,6 +120,7 @@ struct users *newusers, *oldusers;
spinlock_acquire(&service->spin); spinlock_acquire(&service->spin);
oldusers = service->users; oldusers = service->users;
/* digest compare */
if (memcmp(oldusers->cksum, newusers->cksum, SHA_DIGEST_LENGTH) == 0) { if (memcmp(oldusers->cksum, newusers->cksum, SHA_DIGEST_LENGTH) == 0) {
/* same data, nothing to do */ /* same data, nothing to do */
LOGIF(LD, (skygw_log_write_flush( LOGIF(LD, (skygw_log_write_flush(
@ -170,7 +171,7 @@ getUsers(SERVICE *service, struct users *users)
unsigned char hash[SHA_DIGEST_LENGTH]=""; unsigned char hash[SHA_DIGEST_LENGTH]="";
char *users_data = NULL; char *users_data = NULL;
int nusers = 0; int nusers = 0;
int users_data_row_len = MYSQL_USER_MAXLEN + MYSQL_HOST_MAXLEN + MYSQL_PASSWORD_LEN + 1; int users_data_row_len = MYSQL_USER_MAXLEN + MYSQL_HOST_MAXLEN + MYSQL_PASSWORD_LEN;
struct sockaddr_in serv_addr; struct sockaddr_in serv_addr;
MYSQL_USER_HOST key; MYSQL_USER_HOST key;
@ -303,7 +304,7 @@ getUsers(SERVICE *service, struct users *users)
} }
num_fields = mysql_num_fields(result); num_fields = mysql_num_fields(result);
users_data = (char *)calloc(nusers, users_data_row_len * sizeof(char)); users_data = (char *)malloc(nusers * (users_data_row_len * sizeof(char)) + 1);
if(users_data == NULL) if(users_data == NULL)
return -1; return -1;
@ -350,7 +351,8 @@ getUsers(SERVICE *service, struct users *users)
row[0], row[0],
row[1], row[1],
rc == NULL ? "NULL" : ret_ip))); rc == NULL ? "NULL" : ret_ip)));
/* Append data in the memory area for SHA1 digest */
strncat(users_data, row[3], users_data_row_len); strncat(users_data, row[3], users_data_row_len);
total_users++; total_users++;
@ -377,6 +379,7 @@ getUsers(SERVICE *service, struct users *users)
} }
} }
/* compute SHA1 digest for users' data */
SHA1((const unsigned char *) users_data, strlen(users_data), hash); SHA1((const unsigned char *) users_data, strlen(users_data), hash);
memcpy(users->cksum, hash, SHA_DIGEST_LENGTH); memcpy(users->cksum, hash, SHA_DIGEST_LENGTH);
@ -549,8 +552,8 @@ char *mysql_format_user_entry(void *data)
{ {
MYSQL_USER_HOST *entry; MYSQL_USER_HOST *entry;
char *mysql_user; char *mysql_user;
/* the returned user string is "USER@HOST" */ /* the returned user string is "USER" + "@" + "HOST" + '\0' */
int mysql_user_len = 128 + 1 + INET_ADDRSTRLEN + 1; int mysql_user_len = MYSQL_USER_MAXLEN + 1 + INET_ADDRSTRLEN + 1;
if (data == NULL) if (data == NULL)
return NULL; return NULL;
@ -568,7 +571,7 @@ char *mysql_format_user_entry(void *data)
if (entry->ipv4.sin_addr.s_addr == INADDR_ANY) { if (entry->ipv4.sin_addr.s_addr == INADDR_ANY) {
snprintf(mysql_user, mysql_user_len, "%s@%%", entry->user); snprintf(mysql_user, mysql_user_len, "%s@%%", entry->user);
} else { } else {
snprintf(mysql_user, 128, entry->user); snprintf(mysql_user, MYSQL_USER_MAXLEN, entry->user);
strcat(mysql_user, "@"); strcat(mysql_user, "@");
inet_ntop(AF_INET, &(entry->ipv4).sin_addr, mysql_user+strlen(mysql_user), INET_ADDRSTRLEN); inet_ntop(AF_INET, &(entry->ipv4).sin_addr, mysql_user+strlen(mysql_user), INET_ADDRSTRLEN);
} }

View File

@ -84,9 +84,9 @@ SERVICE *service;
service->enable_root = 0; service->enable_root = 0;
service->routerOptions = NULL; service->routerOptions = NULL;
service->databases = NULL; service->databases = NULL;
memset(&service->rate_limit, 0, sizeof(SERVICE_REFRESH_RATE));
spinlock_init(&service->users_table_spin);
spinlock_init(&service->spin); spinlock_init(&service->spin);
spinlock_init(&service->users_table_spin);
memset(&service->rate_limit, 0, sizeof(SERVICE_REFRESH_RATE));
spinlock_acquire(&service_spin); spinlock_acquire(&service_spin);
service->next = allServices; service->next = allServices;
@ -121,6 +121,13 @@ GWPROTOCOL *funcs;
/* Allocate specific data for MySQL users */ /* Allocate specific data for MySQL users */
service->users = mysql_users_alloc(); service->users = mysql_users_alloc();
loaded = load_mysql_users(service); loaded = load_mysql_users(service);
/* At service start last update is set to USERS_REFRESH_TIME seconds earlier.
* This way MaxScale could try reloading users' just after startup
*/
service->rate_limit.last=time(NULL) - USERS_REFRESH_TIME;
service->rate_limit.nloads=1;
LOGIF(LM, (skygw_log_write( LOGIF(LM, (skygw_log_write(
LOGFILE_MESSAGE, LOGFILE_MESSAGE,
"Loaded %d MySQL Users.", "Loaded %d MySQL Users.",