IDEMPIERE-4372 Performance: only add log record and sql statement to … (#172)

* IDEMPIERE-4372 Performance: only add log record and sql statement to context if trace/debug level <= debug/info

Minor clean up and thread safe fix for CLogErrorBuffer and MIssue
Only add LogRecord and SQL to context if TraceLevel is <= INFO

* IDEMPIERE-4372 Performance: only add log record and sql statement to context if trace/debug level <= debug/info

add back DB.isConnected check
remove redundant CLogErrorBuffer call from DB.isConnected
more fine grained synchronization
This commit is contained in:
hengsin 2020-07-21 18:25:20 +08:00 committed by GitHub
parent 087b6e62d0
commit cc76e63d85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 50 deletions

View File

@ -424,8 +424,10 @@ public class GridTable extends AbstractTableModel
m_SQL += " ORDER BY " + m_orderClause; m_SQL += " ORDER BY " + m_orderClause;
} }
// //
log.fine(m_SQL_Count); if (log.isLoggable(Level.FINE))
Env.setContext(m_ctx, m_WindowNo, m_TabNo, GridTab.CTX_SQL, m_SQL); log.fine(m_SQL_Count);
if (log.isLoggable(Level.INFO))
Env.setContext(m_ctx, m_WindowNo, m_TabNo, GridTab.CTX_SQL, m_SQL);
return m_SQL; return m_SQL;
} // createSelectSql } // createSelectSql

View File

@ -53,9 +53,10 @@ public class MIssue extends X_AD_Issue
*/ */
public static MIssue create (LogRecord record) public static MIssue create (LogRecord record)
{ {
s_log.config(record.getMessage()); if (s_log.isLoggable(Level.CONFIG))
s_log.config(record.getMessage());
MSystem system = MSystem.get(Env.getCtx()); MSystem system = MSystem.get(Env.getCtx());
if (!DB.isConnected() if (!DB.isConnected(false)
|| system == null || system == null
|| !system.isAutoErrorReport()) || !system.isAutoErrorReport())
return null; return null;
@ -406,7 +407,7 @@ public class MIssue extends X_AD_Issue
public String report() public String report()
{ {
//if (true) //if (true)
return "-"; return null;
/*StringBuilder parameter = new StringBuilder("?"); /*StringBuilder parameter = new StringBuilder("?");
if (getRecord_ID() == 0) // don't report if (getRecord_ID() == 0) // don't report
return "ID=0"; return "ID=0";

View File

@ -73,6 +73,11 @@ public class CLogErrorBuffer extends Handler
setFilter(CLogFilter.get()); setFilter(CLogFilter.get());
} // initialize } // initialize
private boolean isAddLogRecordToContext()
{
return CLogMgt.getLevelAsInt() <= Level.INFO.intValue();
}
/** /**
* Issue Error * Issue Error
* @return true if issue error * @return true if issue error
@ -124,62 +129,83 @@ public class CLogErrorBuffer extends Handler
*/ */
public void publish (LogRecord record) public void publish (LogRecord record)
{ {
if (!isLoggable (record))
return;
checkContext(); checkContext();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
LinkedList<LogRecord> m_logs = (LinkedList<LogRecord>) Env.getCtx().get(LOGS_KEY); LinkedList<LogRecord> m_logs = (LinkedList<LogRecord>) Env.getCtx().get(LOGS_KEY);
if (!isLoggable (record) || m_logs == null) if (m_logs == null)
return; return;
// Output // Output
synchronized (m_logs) if (isAddLogRecordToContext())
{ {
if (m_logs.size() >= LOG_SIZE) synchronized (m_logs)
m_logs.removeFirst(); {
m_logs.add(record); if (m_logs.size() >= LOG_SIZE)
m_logs.removeFirst();
m_logs.add(record);
}
} }
// We have an error // We have an error
if (record.getLevel() == Level.SEVERE) if (record.getLevel() == Level.SEVERE)
{ {
@SuppressWarnings("unchecked") if (isAddLogRecordToContext())
LinkedList<LogRecord> m_errors = (LinkedList<LogRecord>) Env.getCtx().get(ERRORS_KEY);
@SuppressWarnings("unchecked")
LinkedList<LogRecord[]> m_history = (LinkedList<LogRecord[]>) Env.getCtx().get(HISTORY_KEY);
if (m_errors.size() >= ERROR_SIZE)
{ {
m_errors.removeFirst(); @SuppressWarnings("unchecked")
m_history.removeFirst(); LinkedList<LogRecord> m_errors = (LinkedList<LogRecord>) Env.getCtx().get(ERRORS_KEY);
synchronized (m_errors)
{
if (m_errors.size() >= ERROR_SIZE)
{
m_errors.removeFirst();
}
// Add Error
m_errors.add(record);
}
} }
// Add Error
m_errors.add(record);
record.getSourceClassName(); // forces Class Name eval record.getSourceClassName(); // forces Class Name eval
// Create History // Create History
ArrayList<LogRecord> history = new ArrayList<LogRecord>(); if (isAddLogRecordToContext())
for (int i = m_logs.size()-1; i >= 0; i--)
{ {
LogRecord rec = (LogRecord)m_logs.get(i); @SuppressWarnings("unchecked")
if (rec.getLevel() == Level.SEVERE) LinkedList<LogRecord[]> m_history = (LinkedList<LogRecord[]>) Env.getCtx().get(HISTORY_KEY);
ArrayList<LogRecord> history = new ArrayList<LogRecord>();
synchronized (m_history)
{ {
if (history.size() == 0) if (m_history.size() >= ERROR_SIZE)
history.add(rec); {
else m_history.removeFirst();
break; // don't include previous error }
for (int i = m_logs.size()-1; i >= 0; i--)
{
LogRecord rec = (LogRecord)m_logs.get(i);
if (rec.getLevel() == Level.SEVERE)
{
if (history.size() == 0)
history.add(rec);
else
break; // don't include previous error
}
else
{
history.add(rec);
if (history.size() > 10)
break; // no more then 10 history records
}
}
LogRecord[] historyArray = new LogRecord[history.size()];
int no = 0;
for (int i = history.size()-1; i >= 0; i--)
historyArray[no++] = (LogRecord)history.get(i);
m_history.add(historyArray);
} }
else
{
history.add(rec);
if (history.size() > 10)
break; // no more then 10 history records
}
} }
LogRecord[] historyArray = new LogRecord[history.size()];
int no = 0;
for (int i = history.size()-1; i >= 0; i--)
historyArray[no++] = (LogRecord)history.get(i);
m_history.add(historyArray);
// Issue Reporting // Issue Reporting
if (isIssueError()) if (isIssueError())
{ {
@ -199,12 +225,11 @@ public class CLogErrorBuffer extends Handler
&& loggerName.indexOf("CConnection") == -1 && loggerName.indexOf("CConnection") == -1
) )
{ {
setIssueError(false);
try try
{ {
MIssue.create(record); MIssue.create(record);
setIssueError(true); }
} catch (Throwable e) catch (Throwable e)
{ {
//failed to save exception to db, print to console //failed to save exception to db, print to console
System.err.println(getFormatter().format(record)); System.err.println(getFormatter().format(record));

View File

@ -339,11 +339,6 @@ public final class DB
//direct connection //direct connection
boolean success = false; boolean success = false;
CLogErrorBuffer eb = CLogErrorBuffer.get(false);
if (eb != null && eb.isIssueError())
eb.setIssueError(false);
else
eb = null; // don't reset
try try
{ {
Connection conn = getConnectionRW(createNew); // try to get a connection Connection conn = getConnectionRW(createNew); // try to get a connection
@ -358,8 +353,6 @@ public final class DB
{ {
success = false; success = false;
} }
if (eb != null)
eb.setIssueError(true);
return success; return success;
} // isConnected } // isConnected

View File

@ -2356,7 +2356,8 @@ public class FindWindow extends Window implements EventListener<Event>, ValueCha
String finalSQL = MRole.getDefault().addAccessSQL(sql.toString(), String finalSQL = MRole.getDefault().addAccessSQL(sql.toString(),
m_tableName, MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); m_tableName, MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
finalSQL = Env.parseContext(Env.getCtx(), m_targetWindowNo, finalSQL, false); finalSQL = Env.parseContext(Env.getCtx(), m_targetWindowNo, finalSQL, false);
Env.setContext(Env.getCtx(), m_targetWindowNo, TABNO, GridTab.CTX_FindSQL, finalSQL); if (log.isLoggable(Level.INFO))
Env.setContext(Env.getCtx(), m_targetWindowNo, TABNO, GridTab.CTX_FindSQL, finalSQL);
// Execute Qusery // Execute Qusery
m_total = 999999; m_total = 999999;