-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathListener.java
350 lines (315 loc) · 8.84 KB
/
Listener.java
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import java.sql.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class Listener
{
private static String getSQLForVersion() throws SQLException
{
try
{
String strSQL = "SELECT CASE WHEN POSITION ('Greenplum Database 4.3' IN version) > 0 THEN 'gpdb_4_3'\n";
strSQL += "WHEN POSITION ('Greenplum Database 5' IN version) > 0 THEN 'gpdb_5'\n";
strSQL += "ELSE 'OTHER' END\n";
strSQL += "FROM version()";
return strSQL;
}
catch (Exception ex)
{
throw new SQLException(ex.getMessage());
}
}
private static String getVersion(Connection conn) throws SQLException
{
try
{
String version = "gpdb_4_3";
String strSQL = getSQLForVersion();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next())
{
version = rs.getString(1);
}
return version;
}
catch (Exception ex)
{
throw new SQLException(ex.getMessage());
}
}
public static Integer getPort(Connection conn) throws SQLException
{
try
{
Integer port = 0;
//make sure all ports created with GPLink have been started so it isn't re-used
for (int i=GPLink.gplinkPortLower; i<GPLink.gplinkPortUpper+1; i++)
{
if (!(checkPort(i)))
{
startPort(i);
port = i;
break;
}
}
return port;
}
catch (Exception ex)
{
throw new SQLException(ex.getMessage());
}
}
public static void stopOrphanedPorts(Connection conn) throws SQLException
{
try
{
//list of ports currently being used
String strCommand = "ps -ef 2> /dev/null | grep gpfdist | grep -v grep | awk -F ' ' '{print $2 \",\" $12}'";
String strSession = executeShell(strCommand);
String[] sessions = strSession.split("\n", -1);
String[] parts;
Integer pid = 0;
Integer sessionPort = 0;
for (int i = 0; i < sessions.length-1; i++)
{
pid = 0;
sessionPort = 0;
parts = sessions[i].split(",", -1);
if (!parts[0].equals(""))
{
try
{
pid = Integer.parseInt(parts[0]);
}
catch(NumberFormatException nfe)
{
pid = 0;
}
}
if (GPLink.debug)
System.out.println("pid: " + pid);
if (!parts[1].equals(""))
{
try
{
sessionPort = Integer.parseInt(parts[1]);
}
catch(NumberFormatException nfe)
{
sessionPort = 0;
}
}
if (GPLink.debug)
System.out.println("sessionPort: " + sessionPort);
if (sessionPort >= GPLink.gplinkPortLower && sessionPort <= GPLink.gplinkPortUpper && pid > 0)
{
if (!checkPort(conn, sessionPort))
killPid(pid);
}
}
}
catch (Exception ex)
{
throw new SQLException(ex.getMessage());
}
}
public static void startMissingPorts(Connection conn) throws SQLException
{
try
{
Integer portCheck = 0;
String version = getVersion(conn);
if (GPLink.debug)
System.out.println("version:" + version);
String strSQL = getSQLForPorts(version, portCheck);
if (GPLink.debug)
System.out.println("strSQL: " + strSQL);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next())
{
portCheck = rs.getInt(1);
if (!checkPort(portCheck))
{
startPort(portCheck);
}
}
}
catch (SQLException ex)
{
String exceptionMessage = ex.getMessage();
SQLException nextException = ex.getNextException();
while (nextException != null)
{
exceptionMessage = exceptionMessage + " " + nextException.getMessage();
nextException = nextException.getNextException();
}
throw new SQLException(exceptionMessage);
}
}
private static String getSQLForPorts(String version, Integer myPort) throws SQLException
{
try
{
if (GPLink.debug)
System.out.println("MyPort: " + myPort);
String strSQL = "";
if (version.equals("gpdb_4_3"))
{
strSQL = "select (split_part(split_part(e.location[1], '/', 3), ':', 2))::int as port\n";
strSQL += "from pg_class c\n";
strSQL += "join pg_namespace n on c.relnamespace = n.oid\n";
strSQL += "join pg_exttable e on c.oid = e.reloid\n";
strSQL += "where e.location is not null\n";
strSQL += "and writable is false\n";
strSQL += "and lower(location[1]) like 'gpfdist%'\n";
strSQL += "and split_part(split_part(e.location[1], '/', 3), ':', 1) = '" + GPLink.gplinkHostName + "'\n";
strSQL += "and (split_part(split_part(e.location[1], '/', 3), ':', 2))::int >= " + GPLink.gplinkPortLower + "\n";
strSQL += "and (split_part(split_part(e.location[1], '/', 3), ':', 2))::int <= " + GPLink.gplinkPortUpper + "\n";
if (myPort > 0)
{
strSQL += "and (split_part(split_part(e.location[1], '/', 3), ':', 2))::int = " + myPort + "\n";
}
strSQL += "order by 1";
} else if (version.equals("gpdb_5"))
{
strSQL = "select (split_part(split_part(e.urilocation[1], '/', 3), ':', 2))::int as port\n";
strSQL += "from pg_class c\n";
strSQL += "join pg_namespace n on c.relnamespace = n.oid\n";
strSQL += "join pg_exttable e on c.oid = e.reloid\n";
strSQL += "where e.urilocation is not null\n";
strSQL += "and writable is false\n";
strSQL += "and lower(urilocation[1]) like 'gpfdist%'\n";
strSQL += "and split_part(split_part(e.urilocation[1], '/', 3), ':', 1) = '" + GPLink.gplinkHostName + "'\n";
strSQL += "and (split_part(split_part(e.urilocation[1], '/', 3), ':', 2))::int >= " + GPLink.gplinkPortLower + "\n";
strSQL += "and (split_part(split_part(e.urilocation[1], '/', 3), ':', 2))::int <= " + GPLink.gplinkPortUpper + "\n";
if (myPort > 0)
{
strSQL += "and (split_part(split_part(e.urilocation[1], '/', 3), ':', 2))::int = " + myPort + "\n";
}
strSQL += "order by 1";
}
return strSQL;
}
catch (Exception ex)
{
throw new SQLException(ex.getMessage());
}
}
private static void startPort(Integer myPort) throws SQLException
{
try
{
GPLink.gplinkLog += "_" + myPort;
String strCommand="gpfdist -d " + GPLink.gplinkHome + " -p " + myPort + " -c " + GPLink.gplinkYml + " -m " + GPLink.gpfdistMaxLength + " -t " + GPLink.gpfdistTimeout + " >> " + GPLink.gplinkLog+ " 2>&1 < " + GPLink.gplinkLog + " &";
String result = executeShell(strCommand);
}
catch (Exception ex)
{
throw new SQLException(ex.getMessage());
}
}
private static boolean checkPort(Integer myPort) throws SQLException
{
try
{
boolean result = false;
String strCommand = "ps -ef 2>/dev/null | grep gpfdist | grep -v grep | grep " + myPort + " | wc -l";
String count = executeShell(strCommand);
count = count.trim();
if (count.equals("1"))
result = true;
else if (count.equals("0"))
result = false;
if (GPLink.debug)
System.out.println("Result: " + result);
return result;
}
catch (Exception ex)
{
throw new SQLException(ex.getMessage());
}
}
private static boolean checkPort(Connection conn, Integer myPort) throws SQLException
{
try
{
boolean result = false;
String version = getVersion(conn);
String strSQL = getSQLForPorts(version, myPort);
if (GPLink.debug)
System.out.println("strSQL: " + strSQL);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next())
{
result = true;
}
if (GPLink.debug)
System.out.println("Result: " + result);
return result;
}
catch (Exception ex)
{
throw new SQLException(ex.getMessage());
}
}
private static void killPid(Integer pid) throws SQLException
{
try
{
String strCommand = "kill " + pid;
executeShell(strCommand);
strCommand = "sleep .4";
executeShell(strCommand);
}
catch (Exception ex)
{
throw new SQLException(ex.getMessage());
}
}
private static String executeShell(String strCommand) throws SQLException
{
try
{
InputStream stderr = null;
InputStream stdout = null;
if (GPLink.debug)
System.out.println("strCommand: " + strCommand);
String[] cmds = {"/bin/bash", "-c", strCommand};
Runtime run = Runtime.getRuntime();
Process pr = run.exec(cmds);
stdout = pr.getInputStream();
stderr = pr.getErrorStream();
pr.waitFor();
//BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
BufferedReader buf = new BufferedReader(new InputStreamReader(stdout));
String line = "";
String output = "";
while ((line = buf.readLine()) != null)
{
output = output + line + "\n";
}
if (GPLink.debug)
System.out.println("Output: " + output);
buf.close();
String stdErrOutput = "";
buf = new BufferedReader (new InputStreamReader (stderr));
while ((line = buf.readLine()) != null)
{
stdErrOutput += stdErrOutput;
}
buf.close();
if (!(stdErrOutput.equals("")))
throw new SQLException(stdErrOutput);
return output;
}
catch (Exception ex)
{
throw new SQLException(ex.getMessage());
}
}
}