-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0MQ load balancer. randomized, threaded test client.
- Loading branch information
Showing
12 changed files
with
363 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
CC=gcc | ||
CFLAGS=-g -Wall -std=gnu99 -o3 | ||
LIBS=-lfcgi | ||
CFLAGS=-g -Wall -std=gnu99 -o2 | ||
LIBS=-lfcgi -lzmq -lczmq | ||
SOURCES=$(wildcard *.c) | ||
OBJECTS=$(SOURCES:.c=.o) | ||
EXECUTABLE=rrrr | ||
|
||
all: $(SOURCES) $(EXECUTABLE) | ||
|
||
$(EXECUTABLE): $(OBJECTS) | ||
$(CC) $(OBJECTS) $(LIBS) -o $@ | ||
all: loadbalancer rrrr test | ||
|
||
%.o: %.c | ||
$(CC) -c $(CFLAGS) $^ -o $@ | ||
|
||
loadbalancer: loadbalancer.o | ||
$(CC) $(CFLAGS) $^ $(LIBS) -o $@ | ||
|
||
rrrr: qstring.o router.o transitdata.o util.o rrrr.o | ||
$(CC) $(CFLAGS) $^ $(LIBS) -o $@ | ||
|
||
test: qstring.o router.o transitdata.o util.o test.o | ||
$(CC) $(CFLAGS) $^ $(LIBS) -o $@ | ||
|
||
clean: | ||
rm -rf $(OBJECTS) $(EXECUTABLE) | ||
rm -f *.o *~ core rrrr | ||
|
||
|
||
test: $(SOURCES) | ||
$(CC) -o $@ $^ $(CFLAGS) $(LIBS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/* config.h */ | ||
|
||
#define CONFIG_PROGRAM_NAME "rrrr" | ||
#define CONFIG_INPUT_FILE "/tmp/timetable.dat" | ||
#define CONFIG_MAX_ROUNDS 4 | ||
#define RRRR_TEST_CONCURRENCY 4 | ||
#define RRRR_TEST_REQUESTS 500 | ||
#define RRRR_INPUT_FILE "/tmp/timetable.dat" | ||
#define RRRR_MAX_ROUNDS 6 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* iter.c : iterators */ | ||
#include "iter.h" | ||
#include <stdbool.h> | ||
|
||
inline void iter_init(iter it, int *ary, int ilo, int ihi) { | ||
it.lo = ary + ilo; | ||
it.hi = ary + ihi; | ||
it.cur = it.lo; | ||
} | ||
|
||
inline bool iter_has_next(iter it) { | ||
return it.cur < it.hi; | ||
} | ||
|
||
inline int iter_index(iter it) { | ||
return it.cur - it.lo; | ||
} | ||
|
||
inline int iter_value(iter it) { | ||
return *(it.cur); | ||
} | ||
|
||
inline int iter_next(iter it) { | ||
return *((it.cur)++); | ||
} | ||
|
||
inline void iter_reset(iter it) { | ||
it.cur = it.lo; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* iter.h */ | ||
#include <stdbool.h> | ||
|
||
typedef struct iter iter; | ||
struct iter { | ||
int *lo; | ||
int *cur; | ||
int *hi; | ||
}; | ||
|
||
inline bool iter_has_next(iter); | ||
|
||
inline int iter_index(iter); | ||
|
||
inline int iter_value(iter); | ||
|
||
inline int iter_next(iter); | ||
|
||
inline void iter_reset(iter); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* Load-balancing broker using CZMQ API. Borrows heavily from load balancer pattern in 0MQ Guide. */ | ||
|
||
#include <syslog.h> | ||
#include <czmq.h> | ||
#include "rrrr.h" | ||
|
||
int main (void) { | ||
|
||
// initialize logging | ||
setlogmask(LOG_UPTO(LOG_DEBUG)); | ||
openlog(PROGRAM_NAME, LOG_CONS | LOG_PID | LOG_PERROR, LOG_USER); | ||
syslog(LOG_INFO, "broker starting up"); | ||
|
||
zctx_t *ctx = zctx_new (); | ||
void *frontend = zsocket_new (ctx, ZMQ_ROUTER); | ||
void *backend = zsocket_new (ctx, ZMQ_ROUTER); | ||
//zsocket_bind (frontend, "ipc://frontend.ipc"); | ||
//zsocket_bind (backend, "ipc://backend.ipc"); | ||
zsocket_bind (frontend, "tcp://*:9292"); | ||
zsocket_bind (backend, "tcp://*:9293"); | ||
int frx = 0, ftx = 0, brx = 0, btx = 0, nworkers = 0, npoll = 0; | ||
|
||
// Queue of available workers | ||
zlist_t *workers = zlist_new (); | ||
|
||
while (true) { | ||
if (++npoll % 72 == 0) | ||
syslog(LOG_INFO, "broker: frx %04d ftx %04d brx %04d btx %04d / %d workers\n", | ||
frx, ftx, brx, btx, nworkers); | ||
zmq_pollitem_t items [] = { | ||
{ backend, 0, ZMQ_POLLIN, 0 }, | ||
{ frontend, 0, ZMQ_POLLIN, 0 } | ||
}; | ||
// Poll frontend only if we have available workers | ||
int rc = zmq_poll (items, zlist_size (workers)? 2: 1, -1); | ||
if (rc == -1) | ||
break; // Interrupted | ||
// Handle worker activity on backend | ||
if (items [0].revents & ZMQ_POLLIN) { | ||
// Use worker identity for load-balancing | ||
zmsg_t *msg = zmsg_recv (backend); | ||
if (!msg) | ||
break; // Interrupted | ||
zframe_t *identity = zmsg_unwrap (msg); | ||
zlist_append (workers, identity); | ||
|
||
// Forward message to client if it's not a READY | ||
zframe_t *frame = zmsg_first (msg); | ||
if (memcmp (zframe_data (frame), WORKER_READY, 1) == 0) { | ||
zmsg_destroy (&msg); | ||
nworkers++; | ||
} else { | ||
brx++; | ||
zmsg_send (&msg, frontend); | ||
ftx++; | ||
} | ||
} | ||
if (items [1].revents & ZMQ_POLLIN) { | ||
// Get client request, route to first available worker | ||
zmsg_t *msg = zmsg_recv (frontend); | ||
frx++; | ||
if (msg) { | ||
zmsg_wrap (msg, (zframe_t *) zlist_pop (workers)); | ||
zmsg_send (&msg, backend); | ||
btx++; | ||
} | ||
} | ||
} | ||
|
||
// When we're done, clean up properly | ||
syslog(LOG_INFO, "broker terminating"); | ||
while (zlist_size (workers)) { | ||
zframe_t *frame = (zframe_t *) zlist_pop (workers); | ||
zframe_destroy (&frame); | ||
} | ||
zlist_destroy (&workers); | ||
zctx_destroy (&ctx); | ||
return 0; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
#!/bin/bash | ||
NUM_HANDLERS=8 | ||
echo killing old processes | ||
killall loadbalancer | ||
killall rrrr | ||
cgi-fcgi -start -connect /tmp/fastcgi.socket ./rrrr $NUM_HANDLERS | ||
chmod a+rw /tmp/fastcgi.socket | ||
sleep 1 | ||
echo starting new processes | ||
./loadbalancer& | ||
# 8 workers is not significantly faster than 4 on a 4-core machine (confirmed twice) | ||
for i in {1..4} ; do ./rrrr & done | ||
sleep 1 | ||
echo done | ||
|
||
# old fcgi: | ||
# cgi-fcgi -start -connect /tmp/fastcgi.socket ./rrrr $NUM_HANDLERS | ||
# chmod a+rw /tmp/fastcgi.socket | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#define PROGRAM_NAME "rrrr" | ||
#define WORKER_READY "\001" // Signals worker is ready | ||
#define WORKER_LEAVE "\002" // Signals worker is shutting down | ||
|
Oops, something went wrong.