Changed evaluators and threads to be initialized only when needed. Phew!

This commit is contained in:
hns 2001-01-18 14:50:04 +00:00
parent e20aaec618
commit b00e421f88
7 changed files with 123 additions and 58 deletions

View file

@ -27,13 +27,14 @@ import FESI.Exceptions.*;
public class Action { public class Action {
String name; String name;
String functionName;
Prototype prototype; Prototype prototype;
Application app; Application app;
File file; File file;
long lastmod; long lastmod;
// this is the parsed function which can be easily applied to RequestEvaluator objects
ParsedFunction pfunc; TypeUpdater pfunc;
public Action (File file, String name, Prototype proto) { public Action (File file, String name, Prototype proto) {
@ -70,15 +71,22 @@ public class Action {
public void update (String content) throws Exception { public void update (String content) throws Exception {
// IServer.getLogger().log ("Reading text template " + name); // IServer.getLogger().log ("Reading text template " + name);
String fname = name+"_hop_action"; functionName = name+"_hop_action";
try { try {
pfunc = parseFunction (fname, "arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10", content); pfunc = parseFunction (functionName, "arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10", content);
} catch (Exception x) { } catch (Exception x) {
String message = x.getMessage (); String message = x.getMessage ();
app.typemgr.generateErrorFeedback (fname, message, prototype.getName ()); pfunc = new ErrorFeedback (functionName, message);
} }
Iterator evals = app.typemgr.getRegisteredRequestEvaluators ();
while (evals.hasNext ()) {
try {
RequestEvaluator reval = (RequestEvaluator) evals.next ();
updateRequestEvaluator (reval);
} catch (Exception ignore) {}
}
} }
@ -87,10 +95,16 @@ public class Action {
} }
public String getFunctionName () { public String getFunctionName () {
return pfunc.functionName; return functionName;
} }
protected ParsedFunction parseFunction (String funcname, String params, String body) throws EcmaScriptException {
public synchronized void updateRequestEvaluator (RequestEvaluator reval) throws EcmaScriptException {
if (pfunc != null)
pfunc.updateRequestEvaluator (reval);
}
protected TypeUpdater parseFunction (String funcname, String params, String body) throws EcmaScriptException {
// ESObject fp = app.eval.evaluator.getFunctionPrototype(); // ESObject fp = app.eval.evaluator.getFunctionPrototype();
// ConstructedFunctionObject function = null; // ConstructedFunctionObject function = null;
@ -142,12 +156,7 @@ public class Action {
return new ParsedFunction (fpl, sl, fes, fulltext, funcname); return new ParsedFunction (fpl, sl, fes, fulltext, funcname);
} }
public void updateRequestEvaluator (RequestEvaluator reval) throws EcmaScriptException { class ParsedFunction implements TypeUpdater {
if (pfunc != null)
pfunc.updateRequestEvaluator (reval);
}
class ParsedFunction {
ASTFormalParameterList fpl = null; ASTFormalParameterList fpl = null;
ASTStatementList sl = null; ASTStatementList sl = null;
@ -178,6 +187,44 @@ public class Action {
} }
} }
class ErrorFeedback implements TypeUpdater {
String functionName, errorMessage;
public ErrorFeedback (String fname, String msg) {
functionName = fname;
errorMessage = msg;
}
public void updateRequestEvaluator (RequestEvaluator reval) throws EcmaScriptException {
ObjectPrototype op = reval.getPrototype (prototype.getName ());
FunctionPrototype fp = (FunctionPrototype) reval.evaluator.getFunctionPrototype ();
FunctionPrototype func = new ThrowException (functionName, reval.evaluator, fp, errorMessage);
op.putHiddenProperty (functionName, func);
}
}
class ThrowException extends BuiltinFunctionObject {
String message;
ThrowException (String name, Evaluator evaluator, FunctionPrototype fp, String message) {
super (fp, evaluator, name, 1);
this.message = message == null ? "No error message available" : message;
}
public ESValue callFunction (ESObject thisObject, ESValue[] arguments) throws EcmaScriptException {
throw new EcmaScriptException (message);
}
public ESObject doConstruct (ESObject thisObject, ESValue[] arguments) throws EcmaScriptException {
throw new EcmaScriptException (message);
}
}
interface TypeUpdater {
public void updateRequestEvaluator (RequestEvaluator reval) throws EcmaScriptException;
}
} }

View file

@ -182,7 +182,7 @@ public class Application extends UnicastRemoteObject implements IRemoteApp, Runn
} }
protected boolean setNumberOfEvaluators (int n) { protected boolean setNumberOfEvaluators (int n) {
if (n < 1 || n > 512) if (n < 2 || n > 511)
return false; return false;
int current = allThreads.size(); int current = allThreads.size();
synchronized (allThreads) { synchronized (allThreads) {
@ -199,6 +199,7 @@ public class Application extends UnicastRemoteObject implements IRemoteApp, Runn
try { try {
RequestEvaluator re = (RequestEvaluator) freeThreads.pop (); RequestEvaluator re = (RequestEvaluator) freeThreads.pop ();
allThreads.removeElement (re); allThreads.removeElement (re);
typemgr.unregisterRequestEvaluator (re);
re.stopThread (); re.stopThread ();
} catch (EmptyStackException empty) { } catch (EmptyStackException empty) {
return false; return false;

View file

@ -23,10 +23,11 @@ public class ESAppNode extends ESNode {
app = eval.app; app = eval.app;
createtime = new DatePrototype (eval.evaluator, node.created()); createtime = new DatePrototype (eval.evaluator, node.created());
FunctionPrototype fp = (FunctionPrototype) eval.evaluator.getFunctionPrototype(); FunctionPrototype fp = (FunctionPrototype) eval.evaluator.getFunctionPrototype();
putHiddenProperty("countEvaluators", new AppCountEvaluators ("countEvaluators", evaluator, fp)); putHiddenProperty("getMaxThreads", new AppCountEvaluators ("getMaxThreads", evaluator, fp));
putHiddenProperty("countFreeEvaluators", new AppCountFreeEvaluators ("countFreeEvaluators", evaluator, fp)); putHiddenProperty("getFreeThreads", new AppCountFreeEvaluators ("getFreeThreads", evaluator, fp));
putHiddenProperty("countBusyEvaluators", new AppCountBusyEvaluators ("countBusyEvaluators", evaluator, fp)); putHiddenProperty("getActiveThreads", new AppCountBusyEvaluators ("getActiveThreads", evaluator, fp));
putHiddenProperty("setNumberOfEvaluators", new AppSetNumberOfEvaluators ("setNumberOfEvaluators", evaluator, fp)); putHiddenProperty("getMaxActiveThreads", new AppCountMaxBusyEvaluators ("getMaxActiveThreads", evaluator, fp));
putHiddenProperty("setMaxThreads", new AppSetNumberOfEvaluators ("setMaxThreads", evaluator, fp));
} }
/** /**
@ -54,7 +55,7 @@ public class ESAppNode extends ESNode {
super (fp, evaluator, name, 0); super (fp, evaluator, name, 0);
} }
public ESValue callFunction (ESObject thisObject, ESValue[] arguments) throws EcmaScriptException { public ESValue callFunction (ESObject thisObject, ESValue[] arguments) throws EcmaScriptException {
return new ESNumber (app.allThreads.size()); return new ESNumber (app.allThreads.size()-1);
} }
} }
@ -72,7 +73,16 @@ public class ESAppNode extends ESNode {
super (fp, evaluator, name, 0); super (fp, evaluator, name, 0);
} }
public ESValue callFunction (ESObject thisObject, ESValue[] arguments) throws EcmaScriptException { public ESValue callFunction (ESObject thisObject, ESValue[] arguments) throws EcmaScriptException {
return new ESNumber (app.allThreads.size() - app.freeThreads.size()); return new ESNumber (app.allThreads.size() - app.freeThreads.size() -1);
}
}
class AppCountMaxBusyEvaluators extends BuiltinFunctionObject {
AppCountMaxBusyEvaluators (String name, Evaluator evaluator, FunctionPrototype fp) {
super (fp, evaluator, name, 0);
}
public ESValue callFunction (ESObject thisObject, ESValue[] arguments) throws EcmaScriptException {
return new ESNumber (app.typemgr.countRegisteredRequestEvaluators () -1);
} }
} }
@ -84,7 +94,7 @@ public class ESAppNode extends ESNode {
RequestEvaluator ev = new RequestEvaluator (app); RequestEvaluator ev = new RequestEvaluator (app);
if (arguments.length != 1) if (arguments.length != 1)
return ESBoolean.makeBoolean (false); return ESBoolean.makeBoolean (false);
return ESBoolean.makeBoolean (app.setNumberOfEvaluators (arguments[0].toInt32())); return ESBoolean.makeBoolean (app.setNumberOfEvaluators (1 + arguments[0].toInt32()));
} }
} }

View file

@ -44,9 +44,17 @@ public class FunctionFile {
lastmod = fmod; lastmod = fmod;
// app.typemgr.readFunctionFile (file, prototype.getName ()); // app.typemgr.readFunctionFile (file, prototype.getName ());
Iterator evals = app.typemgr.getRegisteredRequestEvaluators ();
while (evals.hasNext ()) {
try {
RequestEvaluator reval = (RequestEvaluator) evals.next ();
updateRequestEvaluator (reval);
} catch (Exception ignore) {}
}
} }
public void updateRequestEvaluator (RequestEvaluator reval) { public synchronized void updateRequestEvaluator (RequestEvaluator reval) {
EvaluationSource es = new FileEvaluationSource(file.getPath(), null); EvaluationSource es = new FileEvaluationSource(file.getPath(), null);
FileReader fr = null; FileReader fr = null;

View file

@ -28,6 +28,7 @@ public class RequestEvaluator implements Runnable {
Application app; Application app;
protected boolean initialized;
RequestTrans req; RequestTrans req;
ResponseTrans res; ResponseTrans res;
@ -86,6 +87,7 @@ public class RequestEvaluator implements Runnable {
this.objectcache = new LruHashtable (100, .80f); this.objectcache = new LruHashtable (100, .80f);
this.prototypes = new Hashtable (); this.prototypes = new Hashtable ();
initEvaluator (); initEvaluator ();
initialized = false;
// startThread (); // startThread ();
} }
@ -132,7 +134,7 @@ public class RequestEvaluator implements Runnable {
int txcount = 0; int txcount = 0;
if (prototypes.size() == 0) if (!initialized)
app.typemgr.initRequestEvaluator (this); app.typemgr.initRequestEvaluator (this);
do { do {

View file

@ -23,7 +23,8 @@ import FESI.Exceptions.*;
public class Template extends Action { public class Template extends Action {
ParsedFunction psfunc; // this is the *_as_string function, which is in addition to the normal one
TypeUpdater psfunc;
public Template (File file, String name, Prototype proto) { public Template (File file, String name, Prototype proto) {
@ -132,6 +133,7 @@ public class Template extends Action {
} }
// templateBody.append ("\r\nreturn null;\r\n"); // templateBody.append ("\r\nreturn null;\r\n");
functionName = name;
String fname = name+"_as_string"; String fname = name+"_as_string";
String body = templateBody.toString (); String body = templateBody.toString ();
@ -141,7 +143,7 @@ public class Template extends Action {
body+"\r\nreturn null;\r\n"); body+"\r\nreturn null;\r\n");
} catch (Exception x) { } catch (Exception x) {
String message = x.getMessage (); String message = x.getMessage ();
app.typemgr.generateErrorFeedback (name, message, prototype.getName ()); pfunc = new ErrorFeedback (name, message);
} }
try { try {
psfunc = parseFunction (fname, psfunc = parseFunction (fname,
@ -149,12 +151,19 @@ public class Template extends Action {
"res.pushStringBuffer(); "+body+"\r\nreturn res.popStringBuffer();\r\n"); "res.pushStringBuffer(); "+body+"\r\nreturn res.popStringBuffer();\r\n");
} catch (Exception x) { } catch (Exception x) {
String message = x.getMessage (); String message = x.getMessage ();
app.typemgr.generateErrorFeedback (fname, message, prototype.getName ()); psfunc = new ErrorFeedback (fname, message);
} }
Iterator evals = app.typemgr.getRegisteredRequestEvaluators ();
while (evals.hasNext ()) {
try {
RequestEvaluator reval = (RequestEvaluator) evals.next ();
updateRequestEvaluator (reval);
} catch (Exception ignore) {}
}
} }
public void updateRequestEvaluator (RequestEvaluator reval) throws EcmaScriptException { public synchronized void updateRequestEvaluator (RequestEvaluator reval) throws EcmaScriptException {
if (pfunc != null) if (pfunc != null)
pfunc.updateRequestEvaluator (reval); pfunc.updateRequestEvaluator (reval);
if (psfunc != null) if (psfunc != null)

View file

@ -31,6 +31,10 @@ public class TypeManager implements Runnable, EcmaScriptTreeConstants {
long idleSeconds = 120; // if idle for longer than 5 minutes, slow down long idleSeconds = 120; // if idle for longer than 5 minutes, slow down
boolean rewire; boolean rewire;
// this contains only those evaluatores which have already been initialized
// and thus need to get updates
List registeredEvaluators;
Thread typechecker; Thread typechecker;
// The http broadcaster for pushing out parser output // The http broadcaster for pushing out parser output
@ -55,6 +59,7 @@ public class TypeManager implements Runnable, EcmaScriptTreeConstants {
if (!f.exists()) if (!f.exists())
f.mkdir (); f.mkdir ();
prototypes = new Hashtable (); prototypes = new Hashtable ();
registeredEvaluators = Collections.synchronizedList (new ArrayList (30));
nodeProto = null; nodeProto = null;
} }
@ -277,38 +282,9 @@ public class TypeManager implements Runnable, EcmaScriptTreeConstants {
protected void generateErrorFeedback (String funcname, String message, String protoname)
throws EcmaScriptException {
int size = app.allThreads.size ();
for (int i=0; i<size; i++) {
RequestEvaluator reval = (RequestEvaluator) app.allThreads.elementAt (i);
ObjectPrototype op = reval.getPrototype (protoname);
if (op == null) return;
FunctionPrototype fp = (FunctionPrototype) reval.evaluator.getFunctionPrototype ();
FunctionPrototype func = new ThrowException (funcname, reval.evaluator, fp, message);
op.putHiddenProperty (funcname, func);
}
}
class ThrowException extends BuiltinFunctionObject {
String message;
ThrowException (String name, Evaluator evaluator, FunctionPrototype fp, String message) {
super (fp, evaluator, name, 1);
this.message = message == null ? "No error message available" : message;
}
public ESValue callFunction (ESObject thisObject, ESValue[] arguments) throws EcmaScriptException {
throw new EcmaScriptException (message);
}
public ESObject doConstruct (ESObject thisObject, ESValue[] arguments) throws EcmaScriptException {
throw new EcmaScriptException (message);
}
}
public void initRequestEvaluator (RequestEvaluator reval) { public void initRequestEvaluator (RequestEvaluator reval) {
if (!registeredEvaluators.contains (reval))
registeredEvaluators.add (reval);
for (Enumeration en = prototypes.elements(); en.hasMoreElements(); ) { for (Enumeration en = prototypes.elements(); en.hasMoreElements(); ) {
Prototype p = (Prototype) en.nextElement (); Prototype p = (Prototype) en.nextElement ();
String name = p.getName (); String name = p.getName ();
@ -338,8 +314,20 @@ if (op == null) return;
p.initRequestEvaluator (reval); p.initRequestEvaluator (reval);
} }
reval.initialized = true;
} }
public void unregisterRequestEvaluator (RequestEvaluator reval) {
registeredEvaluators.remove (reval);
}
public Iterator getRegisteredRequestEvaluators () {
return registeredEvaluators.iterator ();
}
public int countRegisteredRequestEvaluators () {
return registeredEvaluators.size ();
}
} }