Skip to content

Commit

Permalink
router: fixup split regex PR #273
Browse files Browse the repository at this point in the history
  • Loading branch information
grobian committed Jun 6, 2017
1 parent 2c621d4 commit 4d2f10e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
2 changes: 1 addition & 1 deletion conffile.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ typedef struct _destinations {

typedef struct _route {
char *pattern; /* original regex input, used for printing only */
regex_t *rule; /* regex on metric, only if type == REGEX */
regex_t *rule; /* regex per worker on metric, only if type == REGEX */
size_t nmatch; /* number of match groups */
char *strmatch; /* string to search for if type not REGEX or MATCHALL */
destinations *dests; /* where matches should go */
Expand Down
12 changes: 9 additions & 3 deletions dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ dispatch_connection(connection *conn, dispatcher *self, struct timeval start)
self->id, conn->sock, conn->metric);
__sync_add_and_fetch(&(self->blackholes),
router_route(self->rtr,
conn->dests, &conn->destlen, CONN_DESTS_SIZE,
conn->srcaddr,
conn->metric, firstspace, self->id));
conn->dests, &conn->destlen, CONN_DESTS_SIZE,
conn->srcaddr,
conn->metric, firstspace, self->id));
tracef("dispatcher %d, connfd %d, destinations %zd\n",
self->id, conn->sock, conn->destlen);

Expand Down Expand Up @@ -717,6 +717,12 @@ dispatch_new_connection(router *r, char *allowed_chars)
return dispatch_new(id, CONNECTION, r, allowed_chars);
}

inline char
dispatch_last_id(void)
{
return globalid;
}

/**
* Signals this dispatcher to stop whatever it's doing.
*/
Expand Down
1 change: 1 addition & 0 deletions dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ int dispatch_addconnection_aggr(int sock);
void dispatch_set_bufsize(unsigned int sockbufsize);
dispatcher *dispatch_new_listener(void);
dispatcher *dispatch_new_connection(router *r, char *allowed_chars);
char dispatch_last_id(void);
void dispatch_stop(dispatcher *d);
void dispatch_shutdown(dispatcher *d);
void dispatch_free(dispatcher *d);
Expand Down
2 changes: 1 addition & 1 deletion relay.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ unsigned char keep_running = 1;
int pending_signal = -1;
char relay_hostname[256];
unsigned char mode = 0;
char workercnt = 0;

static char *config = NULL;
static int batchsize = 2500;
Expand All @@ -58,6 +57,7 @@ static int sockbufsize = 0;
static int collector_interval = 60;
static col_mode smode = CUM;
static dispatcher **workers = NULL;
static char workercnt = 0;
static router *rtr = NULL;
static server *internal_submission = NULL;
static char *relay_logfile = NULL;
Expand Down
1 change: 0 additions & 1 deletion relay.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ extern unsigned char mode;
#endif

extern char relay_hostname[];
extern char workercnt;

enum logdst { LOGOUT, LOGERR };

Expand Down
61 changes: 33 additions & 28 deletions router.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "allocator.h"
#include "relay.h"
#include "router.h"
#include "dispatcher.h"
#include "conffile.h"
#include "conffile.tab.h"

Expand Down Expand Up @@ -88,13 +89,12 @@ router_free_intern(route *routes)
{
/* the only thing that isn't allocated using the allocators are the
* regexes */
int i;
while (routes != NULL) {
if (routes->matchtype == REGEX) {
int regex_count = workercnt + 1;
for(i = 0; i < regex_count; ++i)
int i;
char cnt = dispatch_last_id();
for(i = 0; i < cnt; i++)
regfree(&routes->rule[i]);
free(routes->rule);
}

if (routes->next == NULL || routes->next->dests != routes->dests) {
Expand Down Expand Up @@ -191,29 +191,40 @@ determine_if_regex(allocator *a, route *r, char *pat)
r->pattern = ra_strdup(a, pat);
} else {
int i;
int regex_count = workercnt + 1;
r->rule = malloc(sizeof(*r->rule) * regex_count);
char wcnt = dispatch_last_id();
int ret;

r->rule = ra_malloc(a, sizeof(*r->rule) * wcnt);
if (r->rule == NULL) {
logerr("determine_if_regex: malloc failed for regular expressions\n");
return 0;
logerr("determine_if_regex: malloc failed for "
"regular expressions\n");
return REG_ESPACE; /* lie closest to the truth */
}
for(i = 0; i < regex_count; ++i) {
int ret = regcomp(&r->rule[i], pat, REG_EXTENDED);
if (ret != 0)
return ret; /* allow use of regerror */
}
r->strmatch = NULL;
r->pattern = ra_strdup(a, pat);
ret = regcomp(&r->rule[0], pat, REG_EXTENDED);
if (ret != 0)
return ret; /* allow use of regerror */
if (r->rule[0].re_nsub > 0) {
/* we need +1 because position 0 contains the entire
* expression */
r->nmatch = r->rule[0].re_nsub + 1;
if (r->nmatch > RE_MAX_MATCHES) {
logerr("determine_if_regex: too many match groups, "
"please increase RE_MAX_MATCHES in router.h\n");
regfree(&r->rule[0]);
return REG_ESPACE; /* lie closest to the truth */
}
}
for (i = 1; i < wcnt; i++) {
if (regcomp(&r->rule[i], pat, REG_EXTENDED) != 0) {
while (--i >= 0)
regfree(&r->rule[i]);
logerr("determine_if_regex: out of memory allocating "
"regexes\n");
return REG_ESPACE; /* lie closest to the truth */
}
}
r->strmatch = NULL;
r->pattern = ra_strdup(a, pat);
}

return 0;
Expand Down Expand Up @@ -478,9 +489,6 @@ router_validate_expression(router *rtr, route **retr, char *pat)
r->matchtype = MATCHALL;
} else {
int err = determine_if_regex(rtr->a, r, pat);
if (err == 0 && r->matchtype == REGEX && r->rule == NULL) {
return ra_strdup(rtr->a, "Unable to allocate space for compiled regular expressions");
}
if (err != 0) {
char ebuf[512];
size_t s = snprintf(ebuf, sizeof(ebuf),
Expand Down Expand Up @@ -2661,7 +2669,7 @@ router_route(
* triggered. Useful for testing regular expressions.
*/
static char
router_test_intern(char *metric, char *firstspace, route *routes, int dispatcher_id)
router_test_intern(char *metric, char *firstspace, route *routes)
{
route *w;
destinations *d;
Expand All @@ -2679,11 +2687,10 @@ router_test_intern(char *metric, char *firstspace, route *routes, int dispatcher
gotmatch |= router_test_intern(
metric,
firstspace,
w->dests->cl->members.routes,
dispatcher_id);
w->dests->cl->members.routes);
if (gotmatch & 2)
break;
} else if (router_metric_matches(w, metric, firstspace, pmatch, dispatcher_id)) {
} else if (router_metric_matches(w, metric, firstspace, pmatch, 0)) {
gotmatch = 1;
switch (w->dests->cl->type) {
case AGGREGATION:
Expand All @@ -2697,8 +2704,7 @@ router_test_intern(char *metric, char *firstspace, route *routes, int dispatcher
gotmatch |= router_test_intern(
metric + strlen(w->pattern),
firstspace,
w->dests->cl->members.routes,
dispatcher_id);
w->dests->cl->members.routes);
return gotmatch;
} break;
default:
Expand Down Expand Up @@ -2797,8 +2803,7 @@ router_test_intern(char *metric, char *firstspace, route *routes, int dispatcher
gotmatch |= router_test_intern(
newmetric,
newfirstspace,
routes,
dispatcher_id);
routes);
}
}
if (mode & MODE_DEBUG) {
Expand Down Expand Up @@ -2930,7 +2935,7 @@ router_test_intern(char *metric, char *firstspace, route *routes, int dispatcher
firstspace + 1,
lastspc,
pmatch,
dispatcher_id))
0))
{
fprintf(stdout, " match\n");
} else {
Expand Down Expand Up @@ -2965,7 +2970,7 @@ router_test(router *rtr, char *metric)
for (firstspace = metric; *firstspace != '\0'; firstspace++)
if (*firstspace == ' ')
break;
if (!router_test_intern(metric, firstspace, rtr->routes, 0)) {
if (!router_test_intern(metric, firstspace, rtr->routes)) {
*firstspace = '\0';
fprintf(stdout, "nothing matched %s\n", metric);
}
Expand Down

0 comments on commit 4d2f10e

Please sign in to comment.