refactoring with the port pool, and think about if we should take the

recording script as a service
This commit is contained in:
chentienan 2013-07-20 17:12:00 +08:00
parent e569cb0a0f
commit 020d09177f
5 changed files with 273 additions and 270 deletions

View File

@ -1,7 +1,6 @@
package org.bench4q.master.api; package org.bench4q.master.api;
import org.bench4q.master.api.model.OrganizeRecordPortResponseModel; import org.bench4q.master.api.model.OrganizeRecordPortResponseModel;
import org.bench4q.master.entity.LocalPort;
import org.bench4q.master.service.PortPoolService; import org.bench4q.master.service.PortPoolService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@ -45,14 +44,10 @@ public class RecordPortController extends BaseController {
* .setFailCauseString("you don't hava the power to add port to pool!"); * .setFailCauseString("you don't hava the power to add port to pool!");
* return organizeRecordPortResponseModel; } * return organizeRecordPortResponseModel; }
*/ */
LocalPort localPort = new LocalPort(); this.getPortPoolService().addPortToDBPool(port);
localPort.setPort(port);
localPort.setInUse(false);
this.getPortPoolService().getScriptPortPool().put(port, localPort);
if (!this.getPortPoolService().addPortToDBPool(port)) { if (!this.getPortPoolService().addPortToDBPool(port)) {
return setOrganizePortResponseModel(false, "add to DB pool fails 2"); return setOrganizePortResponseModel(false, "add to DB pool fails");
} }
return setOrganizePortResponseModel(true, ""); return setOrganizePortResponseModel(true, "");
} }
@ -60,10 +55,6 @@ public class RecordPortController extends BaseController {
@RequestMapping(value = "/removePortFromPool", method = RequestMethod.POST) @RequestMapping(value = "/removePortFromPool", method = RequestMethod.POST)
public OrganizeRecordPortResponseModel removePortFromPool( public OrganizeRecordPortResponseModel removePortFromPool(
@RequestParam int port) { @RequestParam int port) {
if (this.getPortPoolService().getScriptPortPool().remove(port) == null) {
return setOrganizePortResponseModel(false,
"fail in add to local pool 1");
}
if (!this.getPortPoolService().removePortFromDBPool(port)) { if (!this.getPortPoolService().removePortFromDBPool(port)) {
return setOrganizePortResponseModel(false, return setOrganizePortResponseModel(false,
"remove from local fails"); "remove from local fails");

View File

@ -2,8 +2,8 @@ package org.bench4q.master.api;
import org.bench4q.master.api.model.OperateScriptServerResponseModel; import org.bench4q.master.api.model.OperateScriptServerResponseModel;
import org.bench4q.master.entity.Constant; import org.bench4q.master.entity.Constant;
import org.bench4q.master.entity.LocalPort;
import org.bench4q.master.entity.ScriptCapturer; import org.bench4q.master.entity.ScriptCapturer;
import org.bench4q.master.entity.db.Port;
import org.bench4q.master.service.PortPoolService; import org.bench4q.master.service.PortPoolService;
import org.bench4q.master.service.ScriptService; import org.bench4q.master.service.ScriptService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -66,12 +66,11 @@ public class RecordScriptController extends BaseController {
*/ */
synchronized (PORT_LOCK) { synchronized (PORT_LOCK) {
// TODO: // TODO:
LocalPort localPort = this.getPortPoolService().getAPortNotInUse(); Port port = this.getPortPoolService().getAPortNotInUse();
if (localPort == null) { if (port == null) {
return returnResponseModel(false, "port is in use!"); return returnResponseModel(false, "port is in use!");
} }
this.setPortForRecord(localPort.getPort()); this.setPortForRecord(port.getPort());
localPort.setInUse(true);
} }
// String pathString = this.getClass().getResource("/").getPath(); // String pathString = this.getClass().getResource("/").getPath();
@ -102,9 +101,10 @@ public class RecordScriptController extends BaseController {
"there is no RecordingServer to stop"); "there is no RecordingServer to stop");
} }
scriptCapturer.stopCurrentRecord(); this.getScriptCapturer().stopCurrentRecord();
this.getPortPoolService().getScriptPortPool() this.getScriptCapturer().shutHttpCaptureProxyServer();
.get(this.getPortForRecord()).setInUse(false);
this.getPortPoolService().backThePortToPool(this.getPortForRecord());
return returnResponseModel(true, "RecordServer stop"); return returnResponseModel(true, "RecordServer stop");
} }

View File

@ -12,6 +12,7 @@ import javax.persistence.Table;
public class Port { public class Port {
private int id; private int id;
private int port; private int port;
private boolean inUse;
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@ -33,4 +34,13 @@ public class Port {
this.port = port; this.port = port;
} }
@Column(name = "inUse", nullable = false)
public boolean isInUse() {
return inUse;
}
public void setInUse(boolean inUse) {
this.inUse = inUse;
}
} }

View File

@ -21,280 +21,249 @@ import org.python.util.PythonInterpreter;
public class Config { public class Config {
private static Config config = null; private static Config config = null;
private static final Log log = LogFactory.getLog(Config.class); private static final Log log = LogFactory.getLog(Config.class);
private static Properties props; private static Properties props;
private String pythonPath; private String pythonPath;
private int port = 8090; private int port = 8090;
private boolean debug; private boolean debug;
private boolean quiet; private boolean quiet;
private String exist; private String exist;
private String replace; private String replace;
private List<String> includeList; private List<String> includeList;
private List<String> excludeList; private List<String> excludeList;
private List<Pattern> includePatterns = new ArrayList<Pattern>(); private List<Pattern> includePatterns = new ArrayList<Pattern>();
private List<Pattern> excludePatterns = new ArrayList<Pattern>(); private List<Pattern> excludePatterns = new ArrayList<Pattern>();
private String scriptArg; private String scriptArg;
private ProxySettings proxySettings; private ProxySettings proxySettings;
private static String defDriverPkgName = "com.bitmechanic.maxq"; private static String defDriverPkgName = "com.bitmechanic.maxq";
private static String defValidatorPkgName = "com.bitmechanic.maxq"; private static String defValidatorPkgName = "com.bitmechanic.maxq";
static static {
{ Layout layout = new PatternLayout("%d [%t] %-5p %c - %m%n");
Layout layout = new PatternLayout("%d [%t] %-5p %c - %m%n"); Logger.getRootLogger().addAppender(
Logger.getRootLogger().addAppender(new ConsoleAppender(layout, "System.err")); new ConsoleAppender(layout, "System.err"));
} }
public static Config getConfig() public static Config getConfig() {
{ if (config == null)
if (config == null) initConfig();
initConfig(); return config;
return config; }
}
public static void initConfig() public static void initConfig() {
{ config = new Config();
config = new Config(); String maxqDir = System.getProperty("maxq.dir");
String maxqDir = System.getProperty("maxq.dir"); String pathSep = System.getProperty("file.separator");
String pathSep = System.getProperty("file.separator");
String propertiesFileName = System.getProperty("properties.file", "maxq.properties"); String propertiesFileName = System.getProperty("properties.file",
InputStream propertiesStream = Config.class.getClassLoader().getResourceAsStream(propertiesFileName); "maxq.properties");
props = new Properties(); InputStream propertiesStream = Config.class.getClassLoader()
if (propertiesStream != null) .getResourceAsStream(propertiesFileName);
{ props = new Properties();
try if (propertiesStream != null) {
{ try {
props.load(propertiesStream); props.load(propertiesStream);
log.debug("Generator: " + props.getProperty("generator.classnames")); log.debug("Generator: "
} + props.getProperty("generator.classnames"));
catch (IOException e) } catch (IOException e) {
{ throw new ExceptionInInitializerError(e);
throw new ExceptionInInitializerError(e); }
}
} }
config.addPythonPath(maxqDir + pathSep + "jython"); config.addPythonPath(maxqDir + pathSep + "jython");
String path = config.getProperty("python.path"); String path = config.getProperty("python.path");
if (path != null) { if (path != null) {
config.addPythonPath(path); config.addPythonPath(path);
} }
String portStr = props.getProperty("local.proxy.port"); String portStr = props.getProperty("local.proxy.port");
if (portStr != null) if (portStr != null)
config.port = Integer.parseInt(portStr); config.port = Integer.parseInt(portStr);
} }
public void completeInit() public void completeInit() throws Utils.UserException {
throws Utils.UserException Level level = this.quiet ? Level.WARN : this.debug ? Level.ALL
{ : Level.INFO;
Level level = this.quiet ? Level.WARN : this.debug ? Level.ALL : Level.INFO; Logger.getRootLogger().setLevel(level);
Logger.getRootLogger().setLevel(level);
System.setProperty("org.apache.commons.logging.simplelog.defaultlog", "warn"); System.setProperty("org.apache.commons.logging.simplelog.defaultlog",
"warn");
Properties ppref = new Properties(); Properties ppref = new Properties();
if (getPythonPath() != null) if (getPythonPath() != null)
ppref.put("python.path", getPythonPath()); ppref.put("python.path", getPythonPath());
PythonInterpreter.initialize(System.getProperties(), ppref, new String[0]); PythonInterpreter.initialize(System.getProperties(), ppref,
new String[0]);
String host = getProperty("remote.proxy.host"); String host = getProperty("remote.proxy.host");
Integer port = getPropertyInt("remote.proxy.port"); Integer port = getPropertyInt("remote.proxy.port");
if ((host == null ? 1 : 0) != (port == null ? 1 : 0)) if ((host == null ? 1 : 0) != (port == null ? 1 : 0))
throw new Utils.UserException("Not using proxy server. You must set both remote.proxy.host and remote.proxy.port."); throw new Utils.UserException(
if ((host != null) && (port != null)) { "Not using proxy server. You must set both remote.proxy.host and remote.proxy.port.");
this.proxySettings = new ProxySettings(); if ((host != null) && (port != null)) {
this.proxySettings.host = host; this.proxySettings = new ProxySettings();
this.proxySettings.port = port.intValue(); this.proxySettings.host = host;
log.info("Proxying requests via " + this.proxySettings.host + ":" + Integer.toString(this.proxySettings.port)); this.proxySettings.port = port.intValue();
} log.info("Proxying requests via " + this.proxySettings.host + ":"
} + Integer.toString(this.proxySettings.port));
}
}
public String getProperty(String prop) public String getProperty(String prop) {
{ return (String) props.get(prop);
return (String)props.get(prop); }
}
public String getProperty(String prop, String def) public String getProperty(String prop, String def) {
{ String val = getProperty(prop);
String val = getProperty(prop); return (val == null) || (val.equals("")) ? def : val;
return (val == null) || (val.equals("")) ? def : val; }
}
public void setProperty(String property, String value) public void setProperty(String property, String value) {
{ props.setProperty(property, value);
props.setProperty(property, value); }
}
public Integer getPropertyInt(String prop) public Integer getPropertyInt(String prop) throws Utils.UserException {
throws Utils.UserException String s = getProperty(prop);
{ if (s == null)
String s = getProperty(prop); return null;
if (s == null) try {
return null; return new Integer(s);
try { } catch (NumberFormatException e) {
return new Integer(s); } catch (NumberFormatException e) { }
} throw new Utils.UserException(prop + " property must be an integer");
throw new Utils.UserException(prop + " property must be an integer"); }
}
public void addPythonPath(String path) public void addPythonPath(String path) {
{ if (this.pythonPath == null)
if (this.pythonPath == null) this.pythonPath = path;
this.pythonPath = path; else
else this.pythonPath = (this.pythonPath
this.pythonPath = (this.pythonPath + System.getProperty("path.separator") + path); + System.getProperty("path.separator") + path);
} }
public String getPythonPath() public String getPythonPath() {
{ return this.pythonPath;
return this.pythonPath; }
}
public void setPort(int pt) public void setPort(int pt) {
{ this.port = pt;
this.port = pt; }
}
public int getPort() public int getPort() {
{ return this.port;
return this.port; }
}
public void setDebug(boolean d) public void setDebug(boolean d) {
{ this.debug = d;
this.debug = d; }
}
public boolean isDebug() public boolean isDebug() {
{ return this.debug;
return this.debug; }
}
public void setQuiet(boolean q) public void setQuiet(boolean q) {
{ this.quiet = q;
this.quiet = q; }
}
public boolean isQuiet() public boolean isQuiet() {
{ return this.quiet;
return this.quiet; }
}
public void setExist(String e) public void setExist(String e) {
{ this.exist = e;
this.exist = e; }
}
public String getExist() public String getExist() {
{ return this.exist;
return this.exist; }
}
public void setReplace(String e) public void setReplace(String e) {
{ this.replace = e;
this.replace = e; }
}
public String getReplace() public String getReplace() {
{ return this.replace;
return this.replace; }
}
public void setIncludePatterns(String[] pats) public void setIncludePatterns(String[] pats) {
{ this.includeList = Arrays.asList(pats);
this.includeList = Arrays.asList(pats); createPatterns(this.includeList, this.includePatterns);
createPatterns(this.includeList, this.includePatterns); }
}
public void setExcludePatterns(String[] pats) public void setExcludePatterns(String[] pats) {
{ this.excludeList = Arrays.asList(pats);
this.excludeList = Arrays.asList(pats); createPatterns(this.excludeList, this.excludePatterns);
createPatterns(this.excludeList, this.excludePatterns); }
}
public List<Pattern> getIncludePatterns() public List<Pattern> getIncludePatterns() {
{ return this.includePatterns;
return this.includePatterns; }
}
public List<Pattern> getExcludePatterns() public List<Pattern> getExcludePatterns() {
{ return this.excludePatterns;
return this.excludePatterns; }
}
public void setScriptArg(String arg) public void setScriptArg(String arg) {
{ this.scriptArg = arg;
this.scriptArg = arg; }
}
public String getScriptArg() public String getScriptArg() {
{ return this.scriptArg;
return this.scriptArg; }
}
public static String getDriverPkgName() public static String getDriverPkgName() {
{ return System.getProperty("maxq.driverpkgname", defDriverPkgName);
return System.getProperty("maxq.driverpkgname", defDriverPkgName); }
}
public static String getValidatorPkgName() public static String getValidatorPkgName() {
{ return System.getProperty("maxq.validatorpkgname", defValidatorPkgName);
return System.getProperty("maxq.validatorpkgname", defValidatorPkgName); }
}
public static Log getTestLogger() public static Log getTestLogger() {
{ return LogFactory.getLog("com.bitmechanic.maxq.testrun");
return LogFactory.getLog("com.bitmechanic.maxq.testrun"); }
}
public ProxySettings getProxySettings() public ProxySettings getProxySettings() {
{ return this.proxySettings;
return this.proxySettings; }
}
public void setProxySettings(ProxySettings proxySettings) public void setProxySettings(ProxySettings proxySettings) {
{ this.proxySettings = proxySettings;
this.proxySettings = proxySettings; }
}
private void createPatterns(List<String> list, List<Pattern> excludePatterns2) private void createPatterns(List<String> list,
{ List<Pattern> excludePatterns2) {
Iterator<String> iter = list.iterator(); Iterator<String> iter = list.iterator();
excludePatterns2.clear(); excludePatterns2.clear();
while (iter.hasNext()) while (iter.hasNext())
try { try {
Pattern p = Pattern.compile(iter.next().toString()); Pattern p = Pattern.compile(iter.next().toString());
excludePatterns2.add(p); excludePatterns2.add(p);
} catch (PatternSyntaxException e) { } catch (PatternSyntaxException e) {
log.error(e); log.error(e);
} }
} }
public static class ProxySettings public static class ProxySettings {
{ String host;
String host; int port;
int port;
public String getHost() public String getHost() {
{ return this.host;
return this.host; }
}
public void setHost(String host) public void setHost(String host) {
{ this.host = host;
this.host = host; }
}
public int getPort() public int getPort() {
{ return this.port;
return this.port; }
}
public void setPort(int port) public void setPort(int port) {
{ this.port = port;
this.port = port; }
} }
}
} }

View File

@ -1,9 +1,7 @@
package org.bench4q.master.service; package org.bench4q.master.service;
import java.util.HashMap; import java.util.List;
import java.util.Iterator;
import java.util.Map;
import org.bench4q.master.entity.LocalPort;
import org.bench4q.master.entity.db.Port; import org.bench4q.master.entity.db.Port;
import org.bench4q.master.helper.SessionHelper; import org.bench4q.master.helper.SessionHelper;
import org.hibernate.Session; import org.hibernate.Session;
@ -15,14 +13,8 @@ import org.springframework.stereotype.Component;
@Component @Component
public class PortPoolService { public class PortPoolService {
private static Map<Integer, LocalPort> ScriptPortPool = new HashMap<Integer, LocalPort>();
private SessionHelper sessionHelper = new SessionHelper(); private SessionHelper sessionHelper = new SessionHelper();
public Map<Integer, LocalPort> getScriptPortPool() {
return PortPoolService.ScriptPortPool;
}
public SessionHelper getSessionHelper() { public SessionHelper getSessionHelper() {
return sessionHelper; return sessionHelper;
} }
@ -99,17 +91,58 @@ public class PortPoolService {
} }
} }
public LocalPort getAPortNotInUse() { @SuppressWarnings("unchecked")
Iterator<LocalPort> iterator = ScriptPortPool.values().iterator(); public Port getAPortNotInUse() {
LocalPort localPort = new LocalPort(); // If i do the HA in the pool, shall i lock this change for this port
while (iterator.hasNext()) { Session session = this.getSessionHelper().openSession();
localPort = iterator.next(); Transaction transaction = session.beginTransaction();
if (localPort.isInUse()) { Port port = null;
continue; try {
} else { List<Port> portList = (List<Port>) session
return localPort; .createCriteria(Port.class)
.add(Restrictions.eq("inUse", false)).list();
if (portList == null) {
return null;
}
port = portList.get(0);
port.setInUse(true);
transaction.commit();
return port;
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return null;
} finally {
if (session != null) {
session.close();
}
}
}
public boolean backThePortToPool(int port) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Port portEntity = (Port) session.createCriteria(Port.class)
.add(Restrictions.eq("port", port)).uniqueResult();
if (portEntity == null) {
return false;
}
portEntity.setInUse(false);
transaction.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return true;
} finally {
if (session != null) {
session.close();
} }
} }
return null;
} }
} }