summaryrefslogtreecommitdiff
path: root/databases/pgcluster/files/patch-main.c
blob: 791b8d460168b475dad5ed1b712f0b88e26f6547 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
--- src/pgcluster/pgrp/main.c.orig	Fri Feb 20 11:38:40 2004
+++ src/pgcluster/pgrp/main.c	Mon Feb 23 12:45:51 2004
@@ -148,7 +148,7 @@
 	char * query;
 
 	/* set function name */
-	set_function("read_packet");
+	set_function("main::read_packet");
 
 	if (header == NULL)
 	{
@@ -171,7 +171,7 @@
 		}
 		if (r == 0)
 		{
-			show_debug("connection closed");
+			show_debug("connection closed (sock=%d, header=%d, read=%d)", sock, header_size, read_size);
 			return NULL;
 		}
 		read_size += r;
@@ -258,18 +258,18 @@
 	/* set function name */
 	set_function("replicate_packet_send");
 
-	show_debug("cmdSts=%c\n",header->cmdSts);
-	show_debug("cmdType=%c\n",header->cmdType);
-	show_debug("port=%d\n",header->port);
-	show_debug("pid=%d\n",header->pid);
-	show_debug("except_host=%s\n",header->except_host);
-	show_debug("from_host=%s\n",header->from_host);
-	show_debug("dbName=%s\n",header->dbName);
-	show_debug("userName=%s\n",header->userName);
-	show_debug("recieve sec=%u\n",header->tv.tv_sec);
-	show_debug("recieve usec=%u\n",header->tv.tv_usec);
-	show_debug("query_size=%d\n",header->query_size);
-	show_debug("query=%s\n",query);
+	show_debug("cmdSts=%c",header->cmdSts);
+	show_debug("cmdType=%c",header->cmdType);
+	show_debug("port=%d",header->port);
+	show_debug("pid=%d",header->pid);
+	show_debug("except_host=%s",header->except_host);
+	show_debug("from_host=%s",header->from_host);
+	show_debug("dbName=%s",header->dbName);
+	show_debug("userName=%s",header->userName);
+	show_debug("recieve sec=%u",header->tv.tv_sec);
+	show_debug("recieve usec=%u",header->tv.tv_usec);
+	show_debug("query_size=%d",header->query_size);
+	show_debug("query=%s",query);
 
 	/*
 	 * loop while registrated cluster DB exist 
@@ -277,7 +277,9 @@
 	ptr = Host_Tbl_Begin;
 	while(ptr->useFlag != DB_TBL_END)
 	{
+		set_function("replicate_packet_send");
 		sem_num = ptr->hostNum;
+		show_debug("loop: cnt=%d, hostNum=%d, hostName=%s, port=%d, recoveryPort=%d, useFlag=%d", cnt, ptr->hostNum, ptr->hostName, ptr->port, ptr->recoveryPort, ptr->useFlag);
 		/*
 		 * check the status of the cluster DB
 		 */
@@ -291,7 +293,7 @@
 		 */
 		if (PGRis_master_in_recovery(ptr->hostName, ptr->port) == true)
 		{
-			show_debug("%s skipped\n",ptr->hostName);
+			show_debug("%s skipped",ptr->hostName);
 			ptr ++;
 			continue;
 		}
@@ -368,6 +370,8 @@
 		if (sem_num > 0)
 			PGRsem_lock(SemID,sem_num);
 		rtn = replicate_packet_send_each_server( ptr, cnt,header, query, dest);
+		set_function("replicate_packet_send");
+		show_debug("replicate_packet_send_each_server returns %d (useFlag=%d", rtn, ptr->useFlag);
 		show_debug("sem_unlock[%d]",sem_num);
 		if (sem_num > 0)
 			PGRsem_unlock(SemID,sem_num);
@@ -382,7 +386,8 @@
 			cnt ++;
 		}
 		ptr ++;
-	}	
+	}
+	show_debug("replicate_packet_send() end");
 	return STATUS_OK;
 }
 
@@ -412,10 +417,10 @@
 	set_function("replicate_packet_send_each_server");
 
 	host = ptr->hostName;
-	show_debug("except:%d@%s host:%d@%s\n",
+	show_debug("except:%d@%s host:%d@%s",
 		header->port,header->except_host,
 		ptr->port,ptr->hostName);
-	show_debug("send replicate to:%s\n",host);
+	show_debug("send replicate to:%s",host);
 	/*
 	 * send query to cluster DB
 	 */
@@ -427,6 +432,8 @@
 	memset(PGR_Result,0,PGR_MESSAGE_BUFSIZE);
 
 	rtn = PGRsend_replicate_packet_to_server( ptr, header,query,PGR_Result);
+	set_function("replicate_packet_send_each_server");
+	show_debug("PGRsend_replicate_packet_to_server() returns %d", rtn);
 	if ((header->cmdSts == CMD_STS_QUERY ) &&
 		((header->cmdType == CMD_TYPE_INSERT) || 
 		(header->cmdType == CMD_TYPE_UPDATE)))
@@ -476,7 +483,7 @@
 	}
 	send_ptr = PGR_Result;
 	buf_size = PGR_MESSAGE_BUFSIZE;
-	show_debug("%d[%s]",buf_size,send_ptr);
+	show_debug("buf_size=%d[send_ptr=%s]",buf_size,send_ptr);
 	if (buf_size < 1)
 		buf_size = 1;
 
@@ -491,7 +498,7 @@
 	rtn = select(dest+1, (fd_set *)NULL, &wmask, (fd_set *)NULL, &timeout);
 	if (rtn && FD_ISSET(dest, &wmask))
 	{
-		show_debug("return_result[%s]",send_ptr);
+		show_debug("select() and FD_ISSET() [send_ptr=%s]",send_ptr);
 		for (;;)
 		{
 			s = send(dest,send_ptr + send_size,buf_size - send_size ,0);
@@ -513,7 +520,7 @@
 			send_size += s;
 			if (send_size == buf_size)
 			{
-				show_debug("%d send",send_size);
+				show_debug("%d bytes sent", send_size);
 				status = STATUS_OK;
 				if (wait == PGR_WAIT_ANSWER)
 				{
@@ -560,7 +567,7 @@
 		rtn = select(dest+1, &rmask, (fd_set *)NULL, (fd_set *)NULL, &timeout);
 		if (rtn && FD_ISSET(dest, &rmask))
 		{
-			show_debug("read_answer selected\n");
+			show_debug("read_answer selected (dest=%d)", dest);
 			answer = NULL;
 			answer = read_packet(dest,&header);
 			show_debug("answer[%s]",answer);
@@ -630,6 +637,43 @@
 	return NULL;
 }
 
+static void
+dump_status(int fd)
+{
+  HostTbl *host = Host_Tbl_Begin;
+  while (host->useFlag != DB_TBL_END) {
+    char buf[256];
+    const char *flag = buf;
+    if (host->useFlag == DB_TBL_FREE) {
+      flag = "FREE";
+    } else if (host->useFlag == DB_TBL_INIT) {
+      flag = "INIT";
+    } else if (host->useFlag == DB_TBL_USE) {
+      flag = "USE";
+    } else if (host->useFlag == DB_TBL_ERROR) {
+      flag = "ERROR";
+    } else {
+      snprintf(buf, sizeof(buf), "UNKNOWN(%d)", host->useFlag);
+    }
+    show_debug("%s:%d flag=%s, recoveryPort=%d",
+	       host->hostName, host->port, flag, host->recoveryPort);
+
+    snprintf(buf, sizeof(buf), "hostNum=%d\n", host->hostNum);
+    write(fd, buf, strlen(buf));
+    snprintf(buf, sizeof(buf), "hostName=%s\n", host->hostName);
+    write(fd, buf, strlen(buf));
+    snprintf(buf, sizeof(buf), "port=%d\n", host->port);
+    write(fd, buf, strlen(buf));
+    snprintf(buf, sizeof(buf), "useFlag=%d\n", host->useFlag);
+    write(fd, buf, strlen(buf));
+    snprintf(buf, sizeof(buf), "recoveryPort=%d\n", host->recoveryPort);
+    write(fd, buf, strlen(buf));
+    write(fd, "\n", strlen("\n"));
+
+    host++;
+  }
+}
+
 /*--------------------------------------------------------------------
  * SYMBOL
  *    replicate_loop()
@@ -665,11 +709,11 @@
 	}
 	if (pid == 0)
 	{
-		signal(SIGHUP, quick_exit);	
-		signal(SIGINT, quick_exit);	
-		signal(SIGQUIT, quick_exit);	
-		signal(SIGTERM, quick_exit);	
-		signal(SIGALRM, quick_exit); 
+		signal(SIGHUP, quick_exit);
+		signal(SIGINT, quick_exit);
+		signal(SIGQUIT, quick_exit);
+		signal(SIGTERM, quick_exit);
+		signal(SIGALRM, quick_exit);
 		setpgid(0,pgid);
 
 		/* child loop */
@@ -687,14 +731,21 @@
 			FD_ZERO(&rmask);
 			FD_SET(sock,&rmask);
 			rtn = select(sock+1, &rmask, (fd_set *)NULL, (fd_set *)NULL, &timeout);
+			if (rtn < 0) {
+				show_debug("select() = %d (%s)", rtn, strerror(errno));
+			}
 			if (rtn && FD_ISSET(sock, &rmask))
 			{
-				show_debug("replicate_loop selected\n");
+				show_debug("replicate_loop selected (sock=%d)", sock);
 				query = NULL;
 				query = read_packet(sock,&header);
-				if (query == NULL)
+				if (query == NULL && errno != 0)
 				{
-					show_error("read_packet failed");
+					show_error("read_packet failed (errno=%d)", errno);
+					break;
+				}
+				if (header.cmdType == 'o') {
+					dump_status(sock);
 					break;
 				}
 				if (header.cmdSts == 0)
@@ -708,7 +759,7 @@
 					}
 				}
 				gettimeofday(&(header.tv),NULL);
-				show_debug("query :: %s\n",query);
+				show_debug("query :: %s",query);
 
 				RecoveryStatus = PGRget_recovery_status();
 				PGRcheck_recovered_host();
@@ -781,7 +832,7 @@
 		show_error("fopen failed: (%s)",strerror(errno));
 		return STATUS_ERROR;
 	}
-	show_debug("%s open ok\n",fname);
+	show_debug("%s open ok",fname);
 
 	/*
 	 * read configuration file
@@ -915,10 +966,10 @@
 	/* set load balance table */
 	for ( i = 0 ; i <= lb_cnt ; i ++)
 	{
+		memset((LoadBalanceTbl + i),0,sizeof(RecoveryTbl));
 		(LoadBalanceTbl + i)->port = -1;
 		(LoadBalanceTbl + i)->sock = -1;
 	}
-	memset((LoadBalanceTbl + i),0,sizeof(RecoveryTbl));
 	PGR_Free_Conf_Data();
 
 	/* allocate result buffer of query */
@@ -954,13 +1005,13 @@
 	/* set function name */
 	set_function("replicate_main");
 
-	show_debug("replicate mail %d port bind\n",Port_Number);
+	show_debug("replicate mail %d port bind",Port_Number);
 	status = PGR_Create_Socket_Bind(&fd, "", Port_Number);
 	if (status != STATUS_OK)
 	{
 		PGRreplicate_exit(0);
 	}
-	show_debug("replicate mail %d port bind OK \n",Port_Number);
+	show_debug("replicate mail %d port bind OK",Port_Number);
 	
 	for (;;)
 	{
@@ -976,11 +1027,14 @@
 		 */
 		FD_ZERO(&rmask);
 		FD_SET(fd,&rmask);
-		show_debug("wait replicate\n");
+		show_debug("wait replicate");
 		rtn = select(fd+1, &rmask, (fd_set *)NULL, (fd_set *)NULL, &timeout);
+		if (rtn < 0) {
+			show_debug("select() = %d (%s)", rtn, strerror(errno));
+		}
 		if (rtn && FD_ISSET(fd, &rmask))
 		{
-			show_debug("replicate main: selected\n");
+			show_debug("replicate main: selected (fd=%d)", fd);
 			/*
 			 * get recovery status.
 			 */
@@ -1315,12 +1369,12 @@
 
 	if (init_server_tbl(path) != STATUS_OK)
 	{
-		show_debug("init_server_tbl error\n");
+		show_debug("init_server_tbl error");
 		PGRreplicate_exit(0);
 	}
 	if (PGRinit_recovery() != STATUS_OK)
 	{
-		show_debug("PGRinit_recovery error\n");
+		show_debug("PGRinit_recovery error");
 		PGRreplicate_exit(0);
 	}
 	pgid = getpgid(0);
@@ -1332,10 +1386,10 @@
 	}
 	if (pid == 0)
 	{
-		signal(SIGHUP, quick_exit);	
-		signal(SIGINT, quick_exit);	
-		signal(SIGQUIT, quick_exit);	
-		signal(SIGTERM, quick_exit);	
+		signal(SIGHUP, quick_exit);
+		signal(SIGINT, quick_exit);
+		signal(SIGQUIT, quick_exit);
+		signal(SIGTERM, quick_exit);
 		/*
 		 * in child process,
 		 * call recovery module