r69658 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69657‎ | r69658 | r69659 >
Date:09:22, 21 July 2010
Author:river
Status:deferred
Tags:
Comment:
fixes
Modified paths:
  • /trunk/tools/trainwreck/Makefile (modified) (history)
  • /trunk/tools/trainwreck/trainwreck.c (modified) (history)

Diff [purge]

Index: trunk/tools/trainwreck/trainwreck.c
@@ -34,6 +34,8 @@
3535
3636 #define BINLOG_NAMELEN 512
3737
 38+#define MASTER_RETRY 30
 39+
3840 static void strdup_free(char **, char const *);
3941 static void usage(void);
4042
@@ -118,6 +120,8 @@
119121 static status_t reader_st = ST_STOPPED;
120122 static int master_thread_stop;
121123
 124+static void slave_process_logs(writer_t *);
 125+static int slave_connect(writer_t *);
122126 static void executed_up_to(writer_t *, char const *, logpos_t);
123127 static void sync_ack(writer_t *);
124128 static int nsyncs;
@@ -318,9 +322,14 @@
319323 reader_st = ST_INITIALISING;
320324 (void) pthread_mutex_unlock(&rst_mtx);
321325
322 - if (master_connect() == -1)
323 - return -1;
 326+ for (;;) {
 327+ if (master_connect() == 0)
 328+ break;
324329
 330+ logmsg("master connect failed; sleeping for retry");
 331+ (void) sleep(MASTER_RETRY);
 332+ }
 333+
325334 (void) pthread_create(&master_thread, NULL, read_master_logs, NULL);
326335
327336 return 0;
@@ -352,7 +361,7 @@
353362 break;
354363
355364 reconnect:
356 - (void) sleep(30);
 365+ (void) sleep(MASTER_RETRY);
357366 logmsg("reconnecting to master...");
358367 mysql_close(master_conn);
359368 if (master_connect() == -1)
@@ -422,11 +431,11 @@
423432 (void) pthread_mutex_unlock(&rst_mtx);
424433
425434 if (master_thread_stop) {
426 - logmsg("shutting down");
427 - (void) pthread_mutex_unlock(&rst_mtx);
428435 mysql_close(master_conn);
429436
430437 (void) pthread_mutex_lock(&rst_mtx);
 438+ logmsg("shutting down; read up to %s,%lu",
 439+ curfile, (unsigned long) curpos);
431440 reader_st = ST_STOPPED;
432441 master_thread_stop = 0;
433442 free(curfile);
@@ -437,7 +446,6 @@
438447 return 0;
439448 }
440449
441 -
442450 if (len == packet_error) {
443451 logmsg("%s,%lu: error retrieving binlogs from server: (%d) %s",
444452 curfile, (unsigned long) curpos,
@@ -445,8 +453,6 @@
446454 return -1;
447455 }
448456
449 - (void) pthread_mutex_unlock(&rst_mtx);
450 -
451457 if ((ent = parse_binlog(master_conn->net.read_pos + 1, len - 1)) == NULL) {
452458 logmsg("failed parsing binlog");
453459 return -1;
@@ -484,8 +490,7 @@
485491 if ((db_regex == NULL || regexec(db_regex, ent->le_database, 0, NULL, 0) == 0) &&
486492 (ignore_regex == NULL || regexec(ignore_regex, ent->le_database, 0, NULL, 0) != 0) &&
487493 (ent->le_type == ET_INTVAR || ent->le_type == ET_QUERY)) {
488 - writer_t *writer;
489 - lq_put(&writer->wr_log_queue, ent);
 494+ lq_put(&writer.wr_log_queue, ent);
490495 } else {
491496 free_log_entry(ent);
492497 }
@@ -664,7 +669,8 @@
665670 if (debug)
666671 logmsg("queue is full, sleeping...");
667672 (void) pthread_mutex_unlock(&q->lq_mtx);
668 - (void) sleep(5);
 673+ /* (void) sleep(5);*/
 674+ (void) usleep(5000);
669675 (void) pthread_mutex_lock(&q->lq_mtx);
670676 }
671677
@@ -731,11 +737,32 @@
732738 return 0;
733739 }
734740
 741+int
 742+slave_connect(self)
 743+ writer_t *self;
 744+{
 745+ if ((self->wr_conn = mysql_init(NULL)) == NULL) {
 746+ logmsg("out of memory in mysql_init");
 747+ return -1;
 748+ }
 749+
 750+ mysql_options(self->wr_conn, MYSQL_READ_DEFAULT_GROUP, "trainwreck-slave");
 751+
 752+ if (mysql_real_connect(self->wr_conn, slave_host, slave_user, slave_pass, NULL,
 753+ slave_port, NULL, 0) == NULL) {
 754+ logmsg("cannot connect to slave %s:%d: %s",
 755+ slave_host, slave_port, mysql_error(self->wr_conn));
 756+ mysql_close(self->wr_conn);
 757+ return -1;
 758+ }
 759+
 760+ return 0;
 761+}
 762+
735763 static void *
736764 slave_write_thread(p)
737765 void *p;
738766 {
739 -logentry_t *e;
740767 writer_t *self = p;
741768 char namebuf[16];
742769
@@ -753,30 +780,41 @@
754781
755782 self->wr_status = ST_INITIALISING;
756783
757 - if ((self->wr_conn = mysql_init(NULL)) == NULL) {
758 - logmsg("out of memory in mysql_init");
759 - return 0;
 784+ for (;;) {
 785+ if (slave_connect(self) == 0)
 786+ break;
 787+ sleep(30);
760788 }
761789
762 - mysql_options(self->wr_conn, MYSQL_READ_DEFAULT_GROUP, "trainwreck-slave");
763 -
764 - if (mysql_real_connect(self->wr_conn, slave_host, slave_user, slave_pass, NULL,
765 - slave_port, NULL, 0) == NULL) {
766 - logmsg("cannot connect to slave %s:%d: %s",
767 - slave_host, slave_port, mysql_error(self->wr_conn));
768 - return 0;
769 - }
770 -
771790 if (retrieve_binlog_position(self) != 0) {
772791 logmsg("could not retrieve binlog position");
773 - exit(1);
 792+ mysql_close(self->wr_conn);
 793+ return NULL;
774794 }
775795
776796 (void) pthread_mutex_lock(&wi_mtx);
777797 writers_initialising--;
778798 (void) pthread_cond_signal(&wi_cond);
779799 (void) pthread_mutex_unlock(&wi_mtx);
780 -
 800+
 801+ for (;;) {
 802+ slave_process_logs(self);
 803+ mysql_close(self->wr_conn);
 804+ sleep(30);
 805+
 806+ for (;;) {
 807+ if (slave_connect(self) == 0)
 808+ break;
 809+ sleep(30);
 810+ }
 811+ }
 812+}
 813+
 814+void
 815+slave_process_logs(self)
 816+ writer_t *self;
 817+{
 818+logentry_t *e;
781819 self->wr_status = ST_WAIT_FOR_ENTRY;
782820 while ((e = lq_get(&self->wr_log_queue)) != NULL) {
783821 if (debug)
@@ -791,7 +829,8 @@
792830 logmsg("%s,%lu: cannot select \"%s\": %s",
793831 e->le_file, (unsigned long) e->le_pos,
794832 e->le_database, mysql_error(self->wr_conn));
795 - exit(1);
 833+ mysql_close(self->wr_conn);
 834+ return;
796835 }
797836
798837 switch (e->le_type) {
@@ -805,7 +844,7 @@
806845 e->le_database,
807846 mysql_errno(self->wr_conn), mysql_error(self->wr_conn),
808847 query);
809 - exit(1);
 848+ return;
810849 }
811850
812851 break;
@@ -820,7 +859,7 @@
821860 e->le_database,
822861 mysql_errno(self->wr_conn), mysql_error(self->wr_conn),
823862 query);
824 - exit(1);
 863+ return;
825864 }
826865 executed_up_to(self, e->le_file, e->le_pos);
827866
@@ -830,7 +869,7 @@
831870 free_log_entry(e);
832871 self->wr_status = ST_WAIT_FOR_ENTRY;
833872 }
834 - return NULL;
 873+ return;
835874 }
836875
837876 /*ARGSUSED*/
Index: trunk/tools/trainwreck/Makefile
@@ -1,13 +1,8 @@
2 -#CC = gcc
32 CC = cc
43 CFLAGS = -g -mt -xc99=none -D_FILE_OFFSET_BITS=64
5 -#CFLAGS = -g -D_REENTRANT -m64 -DBIG_TABLES -DHAVE_RWLOCK_T
6 -#CFLAGS = -g -D_REENTRANT -D_FILE_OFFSET_BITS=64
74 LDFLAGS =
85 MYSQL_CFLAGS:sh = mysql_config --include
9 -#MYSQL_LIBS:sh = mysql_config --libs_r
10 -#MYSQL_LIBS = -L/usr/local/mysql/lib/mysql -R/usr/local/mysql/lib/mysql -lmysqlclient
11 -MYSQL_LIBS = -L/opt/mysql/lib/mysql -R/opt/mysql/lib/mysql -lmysqlclient
 6+MYSQL_LIBS:sh = mysql_config --libs_r
127 INCLUDES =
138 LIBS = -lrt -ldoor
149 PROG = trainwreck

Status & tagging log