TD-978: adjust kill command

This commit is contained in:
Ping Xiao 2020-07-22 14:38:06 +08:00
parent 73c3446e22
commit ec47ae9cca
3 changed files with 62 additions and 49 deletions

View File

@ -1,6 +1,8 @@
package com.taosdata.jdbc.utils; package com.taosdata.jdbc.utils;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.InputStreamReader;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -31,6 +33,10 @@ public class TDNode {
this.testCluster = testCluster; this.testCluster = testCluster;
} }
public void setRunning(int running) {
this.running = running;
}
public void searchTaosd(File dir, ArrayList<String> taosdPath) { public void searchTaosd(File dir, ArrayList<String> taosdPath) {
File[] fileList = dir.listFiles(); File[] fileList = dir.listFiles();
@ -102,15 +108,46 @@ public class TDNode {
this.running = 1; this.running = 1;
} }
public Integer getTaosdPid() {
String cmd = "ps -ef|grep -w taosd| grep -v grep | awk '{print $2}'";
String[] cmds = {"sh", "-c", cmd};
try {
Process process = Runtime.getRuntime().exec(cmds);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
Integer res = null;
while((line = reader.readLine()) != null) {
if(!line.isEmpty()) {
res = Integer.valueOf(line);
break;
}
}
return res;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void stop() { public void stop() {
String toBeKilled = "taosd";
if (this.running != 0) { if (this.running != 0) {
String killCmd = "pkill -kill -x " + toBeKilled; Integer pid = null;
String[] killCmds = {"sh", "-c", killCmd}; while((pid = getTaosdPid()) != null) {
try {
Runtime.getRuntime().exec(killCmds).waitFor();
String killCmd = "kill -term " + pid;
String[] killCmds = {"sh", "-c", killCmd};
try {
Runtime.getRuntime().exec(killCmds).waitFor();
TimeUnit.SECONDS.sleep(2);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
for(int port = 6030; port < 6041; port ++) { for(int port = 6030; port < 6041; port ++) {
String fuserCmd = "fuser -k -n tcp " + port; String fuserCmd = "fuser -k -n tcp " + port;
Runtime.getRuntime().exec(fuserCmd).waitFor(); Runtime.getRuntime().exec(fuserCmd).waitFor();
@ -120,7 +157,7 @@ public class TDNode {
} }
this.running = 0; this.running = 0;
System.out.println("dnode:" + this.index + " is stopped by pkill"); System.out.println("dnode:" + this.index + " is stopped by kill -term");
} }
} }

View File

@ -14,33 +14,6 @@ public class TDNodes {
} }
} }
public void setPath(String path) {
try {
String killCmd = "pkill -kill -x taosd";
String[] killCmds = {"sh", "-c", killCmd};
Runtime.getRuntime().exec(killCmds).waitFor();
String binPath = System.getProperty("user.dir");
binPath += "/../../../debug";
System.out.println("binPath: " + binPath);
File file = new File(path);
binPath = file.getCanonicalPath();
System.out.println("binPath real path: " + binPath);
if(path.isEmpty()){
file = new File(path + "/../../");
path = file.getCanonicalPath();
}
for(int i = 0; i < tdNodes.size(); i++) {
tdNodes.get(i).setPath(path);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void setTestCluster(boolean testCluster) { public void setTestCluster(boolean testCluster) {
this.testCluster = testCluster; this.testCluster = testCluster;
} }
@ -71,6 +44,11 @@ public class TDNodes {
tdNodes.get(index - 1).setCfgConfig(option, value); tdNodes.get(index - 1).setCfgConfig(option, value);
} }
public TDNode getTDNode(int index) {
check(index);
return tdNodes.get(index - 1);
}
public void start(int index) { public void start(int index) {
check(index); check(index);
tdNodes.get(index - 1).start(); tdNodes.get(index - 1).start();

View File

@ -1,6 +1,5 @@
package com.taosdata.jdbc; package com.taosdata.jdbc;
import java.io.File;
import com.taosdata.jdbc.utils.TDNodes; import com.taosdata.jdbc.utils.TDNodes;
import org.junit.AfterClass; import org.junit.AfterClass;
@ -9,30 +8,29 @@ import org.junit.BeforeClass;
public class BaseTest { public class BaseTest {
private static boolean testCluster = false; private static boolean testCluster = false;
private static String deployPath = System.getProperty("user.dir"); private static TDNodes nodes = new TDNodes();
private static TDNodes tdNodes = new TDNodes();
@BeforeClass @BeforeClass
public static void setupEnv() { public static void setupEnv() {
try{ try{
File file = new File(deployPath + "/../../../"); if (nodes.getTDNode(1).getTaosdPid() != null) {
String rootPath = file.getCanonicalPath(); System.out.println("Kill taosd before running JDBC test");
nodes.getTDNode(1).setRunning(1);
nodes.stop(1);
}
tdNodes.setPath(rootPath); nodes.setTestCluster(testCluster);
tdNodes.setTestCluster(testCluster); nodes.deploy(1);
nodes.start(1);
tdNodes.deploy(1);
tdNodes.start(1);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
System.out.println("Base Test Exception");
} }
} }
@AfterClass @AfterClass
public static void cleanUpEnv() { public static void cleanUpEnv() {
tdNodes.stop(1); nodes.stop(1);
} }
} }