Commit 6a4af76e authored by Axel Brand's avatar Axel Brand Committed by daeva

Gojara

- batch logfiles before writing to db, lets try this out with 20. is synchronized, probably needs further testing with higher load
- refactor session iteration in transportsessionmanager
- dont pass null, pass empty arraylist instead for session getter (if no sessions for user found)
- changed concurrency level for hashmaps to 1, need to try this out under higher load, 16 may seem a bit high as its not a highly concurritive environment
- deleted duplicated processor

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@13704 b35dd754-fafc-0310-a699-88a17e54d16e
parent fd7bfe7f
...@@ -7,6 +7,7 @@ import java.sql.SQLException; ...@@ -7,6 +7,7 @@ import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
...@@ -22,6 +23,7 @@ import org.jivesoftware.util.JiveGlobals; ...@@ -22,6 +23,7 @@ import org.jivesoftware.util.JiveGlobals;
public class DatabaseManager { public class DatabaseManager {
private static Logger Log = Logger.getLogger(DatabaseManager.class); private static Logger Log = Logger.getLogger(DatabaseManager.class);
private List<LogEntry> logbuffer;
private static volatile DatabaseManager _myself; private static volatile DatabaseManager _myself;
// Logging // Logging
...@@ -51,7 +53,7 @@ public class DatabaseManager { ...@@ -51,7 +53,7 @@ public class DatabaseManager {
// TODO: Use PropertyEventListener to check if cleaner.minutes have // TODO: Use PropertyEventListener to check if cleaner.minutes have
// changed // changed
_dbCleanMinutes = JiveGlobals.getIntProperty("plugin.remoteroster.log.cleaner.minutes", 60); _dbCleanMinutes = JiveGlobals.getIntProperty("plugin.remoteroster.log.cleaner.minutes", 60);
logbuffer = Collections.synchronizedList(new ArrayList<LogEntry>(20));
startDatabaseCleanLoop(); startDatabaseCleanLoop();
} }
...@@ -112,7 +114,7 @@ public class DatabaseManager { ...@@ -112,7 +114,7 @@ public class DatabaseManager {
String to = rs.getString(5); String to = rs.getString(5);
String type = rs.getString(3); String type = rs.getString(3);
long date = rs.getLong(2); long date = rs.getLong(2);
LogEntry res = new LogEntry(from, to, type, date); LogEntry res = new LogEntry(from, to, type, date, component);
result.add(res); result.add(res);
} }
...@@ -146,7 +148,7 @@ public class DatabaseManager { ...@@ -146,7 +148,7 @@ public class DatabaseManager {
} }
/** /**
* Adds new log entry for specified component. * Adds new log entry for specified component. Buffers upto 20 Logs, then writes in batch.
* *
* @param component * @param component
* subdomain of the external component. e.g. icq.myjabberserver.com * subdomain of the external component. e.g. icq.myjabberserver.com
...@@ -158,23 +160,31 @@ public class DatabaseManager { ...@@ -158,23 +160,31 @@ public class DatabaseManager {
* full qualified JID of user or component this packet was adressed to * full qualified JID of user or component this packet was adressed to
*/ */
public void addNewLogEntry(String component, String type, String from, String to) { public void addNewLogEntry(String component, String type, String from, String to) {
Connection con = null; if (logbuffer.size() < 20)
PreparedStatement pstmt = null; logbuffer.add(new LogEntry(from, to, type, System.currentTimeMillis(), component));
try { else {
con = DbConnectionManager.getConnection(); synchronized (logbuffer) {
Connection con = null;
pstmt = con.prepareStatement(ADD_NEW_LOG); PreparedStatement pstmt = null;
pstmt.setLong(1, System.currentTimeMillis()); try {
pstmt.setString(2, type); con = DbConnectionManager.getConnection();
pstmt.setString(3, from); for (LogEntry log : logbuffer) {
pstmt.setString(4, to); pstmt = con.prepareStatement(ADD_NEW_LOG);
pstmt.setString(5, component); pstmt.setLong(1, log.getDate());
pstmt.executeUpdate(); pstmt.setString(2, log.getType());
pstmt.close(); pstmt.setString(3, log.getFrom());
} catch (SQLException sqle) { pstmt.setString(4, log.getTo());
Log.error(sqle); pstmt.setString(5, log.getComponent());
} finally { pstmt.addBatch();
DbConnectionManager.closeConnection(pstmt, con); }
pstmt.executeBatch();
} catch (SQLException sqle) {
Log.error(sqle);
} finally {
DbConnectionManager.closeConnection(pstmt, con);
logbuffer.clear();
}
}
} }
} }
...@@ -410,7 +420,7 @@ public class DatabaseManager { ...@@ -410,7 +420,7 @@ public class DatabaseManager {
} }
return result; return result;
} }
public int getNumberOfRegistrationsForTransport(String transport) { public int getNumberOfRegistrationsForTransport(String transport) {
int result = 0; int result = 0;
Connection con = null; Connection con = null;
......
...@@ -11,6 +11,7 @@ public class LogEntry { ...@@ -11,6 +11,7 @@ public class LogEntry {
private String _to; private String _to;
private String _type; private String _type;
private long _date; private long _date;
private String _component;
/** /**
* Constructs a log entry * Constructs a log entry
...@@ -24,11 +25,12 @@ public class LogEntry { ...@@ -24,11 +25,12 @@ public class LogEntry {
* @param date * @param date
* date of the packet in unixtimestamp miliseconds * date of the packet in unixtimestamp miliseconds
*/ */
public LogEntry(String from, String to, String type, long date) { public LogEntry(String from, String to, String type, long date, String component) {
_from = from; _from = from;
_to = to; _to = to;
_type = type; _type = type;
_date = date; _date = date;
_component = component;
} }
/** /**
...@@ -67,4 +69,7 @@ public class LogEntry { ...@@ -67,4 +69,7 @@ public class LogEntry {
return _date; return _date;
} }
public String getComponent() {
return _component;
}
} }
package org.jivesoftware.openfire.plugin.gojara.messagefilter.processors;
import org.jivesoftware.openfire.interceptor.PacketRejectedException;
import org.jivesoftware.openfire.plugin.gojara.database.DatabaseManager;
import org.xmpp.packet.Packet;
/**
*
* This class is only for logging messages between the gateway and the
* registered clients. It uses the database to save the packets from the past 60
* (configurable) minutes.
*
* @author Holger Bergunde
* @author axel.frederik.brand
*/
public class TransportSessionProcessor extends AbstractRemoteRosterProcessor{
private DatabaseManager _db;
public TransportSessionProcessor() {
Log.info("Created StatisticsProcessor");
_db = DatabaseManager.getInstance();
}
/**
* At this Point we Already know:
* neither of both JIDS is malformed (Package wouldn't have been intercepted)
* Package is incoming & processed
*
* Either From or To contains the watched,passed subdomain
* From does not Equal To (This way we exclude PING sent by spectrum To spectrum
* From AND To are NOT empty (null), this way we exclude packets sent to server itself...change Maininterceptor if we want to change this
*
*/
@Override
public void process(Packet packet, String subdomain, String to, String from)
throws PacketRejectedException {
String type = packet.getClass().getName();
_db.addNewLogEntry(subdomain, type, from, to);
}
}
...@@ -23,7 +23,7 @@ public class TransportSessionManager { ...@@ -23,7 +23,7 @@ public class TransportSessionManager {
private static TransportSessionManager myself; private static TransportSessionManager myself;
private DatabaseManager db; private DatabaseManager db;
private GojaraAdminManager adminManager; private GojaraAdminManager adminManager;
private Map<String, Map<String, Long>> transportSessions = new ConcurrentHashMap<String, Map<String, Long>>(); private Map<String, Map<String, Long>> transportSessions = new ConcurrentHashMap<String, Map<String, Long>>(16, 0.75f, 1);
private static final Logger Log = LoggerFactory.getLogger(TransportSessionManager.class); private static final Logger Log = LoggerFactory.getLogger(TransportSessionManager.class);
private TransportSessionManager() { private TransportSessionManager() {
...@@ -32,12 +32,9 @@ public class TransportSessionManager { ...@@ -32,12 +32,9 @@ public class TransportSessionManager {
Log.info(" Created TransportSessionManager"); Log.info(" Created TransportSessionManager");
} }
public static TransportSessionManager getInstance() { public static synchronized TransportSessionManager getInstance() {
if (myself == null) { if (myself == null) {
synchronized (TransportSessionManager.class) { myself = new TransportSessionManager();
if (myself == null)
myself = new TransportSessionManager();
}
} }
return myself; return myself;
} }
...@@ -48,7 +45,7 @@ public class TransportSessionManager { ...@@ -48,7 +45,7 @@ public class TransportSessionManager {
* @param subdomain * @param subdomain
*/ */
public void addTransport(String subdomain) { public void addTransport(String subdomain) {
transportSessions.put(subdomain, new ConcurrentHashMap<String, Long>()); transportSessions.put(subdomain, new ConcurrentHashMap<String, Long>(64, 0.75f, 1));
Log.info("Added key to transportSessionMap: " + subdomain); Log.info("Added key to transportSessionMap: " + subdomain);
} }
...@@ -168,12 +165,12 @@ public class TransportSessionManager { ...@@ -168,12 +165,12 @@ public class TransportSessionManager {
* @return Sorted/Unsorted ArrayList of GatewaySession Objects * @return Sorted/Unsorted ArrayList of GatewaySession Objects
*/ */
public ArrayList<GatewaySession> getSessionsSorted(String sortby, String sortorder) { public ArrayList<GatewaySession> getSessionsSorted(String sortby, String sortorder) {
ArrayList<GatewaySession> result = new ArrayList<GatewaySession>(); ArrayList<GatewaySession> result = new ArrayList<GatewaySession>(getNumberOfActiveSessions());
for (String key : transportSessions.keySet()) { for (Map.Entry<String, Map<String,Long>> gateway : transportSessions.entrySet()) {
for (String user : transportSessions.get(key).keySet()) { for (Map.Entry<String, Long> entry : gateway.getValue().entrySet()) {
Timestamp stamp = new Timestamp(transportSessions.get(key).get(user)); Timestamp stamp = new Timestamp(entry.getValue());
Date date = new Date(stamp.getTime()); Date date = new Date(stamp.getTime());
result.add(new GatewaySession(user, key, date)); result.add(new GatewaySession(entry.getKey(), gateway.getKey(), date));
} }
} }
...@@ -195,8 +192,8 @@ public class TransportSessionManager { ...@@ -195,8 +192,8 @@ public class TransportSessionManager {
*/ */
public int getNumberOfActiveSessions() { public int getNumberOfActiveSessions() {
int result = 0; int result = 0;
for (String key : transportSessions.keySet()) { for (Map.Entry<String, Map<String,Long>> gateway : transportSessions.entrySet()) {
result += transportSessions.get(key).size(); result += gateway.getValue().size();
} }
return result; return result;
} }
...@@ -219,14 +216,12 @@ public class TransportSessionManager { ...@@ -219,14 +216,12 @@ public class TransportSessionManager {
* @return * @return
*/ */
public ArrayList<GatewaySession> getConnectionsFor(String username) { public ArrayList<GatewaySession> getConnectionsFor(String username) {
ArrayList<GatewaySession> userconnections = null; ArrayList<GatewaySession> userconnections = new ArrayList<GatewaySession>();
for (String transport : transportSessions.keySet()) { for (Map.Entry<String, Map<String, Long>> transport : transportSessions.entrySet()) {
if (transportSessions.get(transport).containsKey(username)) { if (transport.getValue().containsKey(username)) {
if (userconnections == null) Timestamp stamp = new Timestamp(transport.getValue().get(username));
userconnections = new ArrayList<GatewaySession>();
Timestamp stamp = new Timestamp(transportSessions.get(transport).get(username));
Date date = new Date(stamp.getTime()); Date date = new Date(stamp.getTime());
userconnections.add(new GatewaySession(username, transport, date)); userconnections.add(new GatewaySession(username, transport.getKey(), date));
} }
} }
return userconnections; return userconnections;
......
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
<body> <body>
<% if (!gojaraAdminManager.areGatewaysConfigured()) {%> <% if (!gojaraAdminManager.areGatewaysConfigured()) {%>
<center><h2 style="color:red">Warning: Not all Gateways are configured for admin usage. This means session details may be inaccurate or not logged at all.<br/> <center><h2 style="color:red">Warning: Not all Gateways are configured for admin usage. This means session details may be inaccurate or not logged at all.<br/>
Please configure admin_jid = gojaraadmin@yourdomain in Spectrum2 transport configuration.</h2></center> Please configure admin_jid = gojaraadmin@yourdomain in Spectrum2 transport configuration.</h2></center><br/>
<% } %> <% } %>
<h4> <h4>
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
<% <%
} }
%> %>
<br/><br/> <br/><br/>
<div class="jive-table"> <div class="jive-table">
<table cellpadding="0" cellspacing="0" border="0" width="100%"> <table cellpadding="0" cellspacing="0" border="0" width="100%">
......
...@@ -58,7 +58,7 @@ ...@@ -58,7 +58,7 @@
<% <%
} }
ArrayList<GatewaySession> userconnections = transportManager.getConnectionsFor(username); ArrayList<GatewaySession> userconnections = transportManager.getConnectionsFor(username);
if (userconnections == null) { if (userconnections.isEmpty()) {
%> %>
<h2> <h2>
<center>User has no active sessions</center> <center>User has no active sessions</center>
...@@ -111,7 +111,8 @@ ...@@ -111,7 +111,8 @@
<tr> <tr>
<th nowrap>User Name:</th> <th nowrap>User Name:</th>
<th nowrap>Resource:</th> <th nowrap>Resource:</th>
<th nowrap>Resource active?</th> <th nowrap>Active?</th>
<th nowrap>Admin Configured?</th>
<th nowrap>Last login was at:</th> <th nowrap>Last login was at:</th>
<th nowrap>Unregister?</th> <th nowrap>Unregister?</th>
</tr> </tr>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment