Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restart/Logging/Debug tweaks #18

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/java/org/jlab/epics2web/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static Future<?> writeFromBlockingQueue(Session session) {
return writerExecutor.submit(new Runnable() {
@Override
public void run() {
final String id = session.getId() + " / " + session.getUserProperties().get("ip");
final ArrayBlockingQueue<String> writequeue = (ArrayBlockingQueue<String>) session.getUserProperties().get("writequeue");
try {
while (true) {
Expand All @@ -72,7 +73,7 @@ public void run() {
try {
session.getBasicRemote().sendText(msg);
} catch (IllegalStateException | IOException e) { // If session closes between time session.isOpen() and sentText(msg) then you'll get this exception. Not an issue.
LOGGER.log(Level.FINEST, "Unable to send message: ", e);
LOGGER.log(Level.FINEST, "Unable to send message to " + id, e);

if (!session.isOpen()) {
LOGGER.log(Level.FINEST, "Session closed after write exception; shutting down write thread");
Expand All @@ -81,12 +82,12 @@ public void run() {
}
}
} else {
LOGGER.log(Level.FINEST, "Session closed; shutting down write thread");
LOGGER.log(Level.FINEST, "Session {0} closed; shutting down write thread", id);
break;
}
}
} catch (InterruptedException e) {
LOGGER.log(Level.FINEST, "Shutting down writer thread as requested", e);
LOGGER.log(Level.FINEST, "Shutting down {0} writer thread as requested by InterruptException", id);
}
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jlab/epics2web/controller/WebConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.Session;
import org.jlab.epics2web.Application;
import org.jlab.epics2web.epics.ChannelMonitor;
import org.jlab.epics2web.websocket.SessionInfo;
import org.jlab.epics2web.websocket.WebSocketSessionManager;
import org.jlab.epics2web.epics.ChannelManager;

Expand Down Expand Up @@ -40,7 +40,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)


Map<String, ChannelMonitor> monitorMap = channelManager.getMonitorMap();
Map<Session, Set<String>> clientMap = sessionManager.getClientMap();
Map<SessionInfo, Set<String>> clientMap = sessionManager.getClientMap();

request.setAttribute("monitorMap", monitorMap);
request.setAttribute("clientMap", clientMap);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/jlab/epics2web/epics/ChannelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ public static String getDbrValueAsString(DBR dbr) {

public void addValueToJSON(JsonObjectBuilder builder, DBR dbr) {
try {
if (dbr.isDOUBLE()) {
if(dbr == null) {
builder.addNull("value"); // null happens on restart?
} else if (dbr.isDOUBLE()) {
double value = ((gov.aps.jca.dbr.DOUBLE) dbr).getDoubleValue()[0];
if (Double.isFinite(value)) {
builder.add("value", value);
Expand Down Expand Up @@ -380,7 +382,7 @@ public Map<String, ChannelMonitor> getMonitorMap() {
*
* @return The listener to PVs map
*/
public Map<PvListener, Set<String>> getClientMap() {
public Map<PvListener, Set<String>> getListenerMap() {
return Collections.unmodifiableMap(clientMap);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/jlab/epics2web/epics/ChannelMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public void run() {
handleRegularConnectionOrReconnect();
}
} else {
LOGGER.log(Level.FINE, "Notifying clients of disconnect from channel: {0}", pv);
LOGGER.log(Level.FINEST, "Notifying clients of disconnect from channel: {0}", pv);

state.set(MonitorState.DISCONNECTED);
notifyPvInfoAll(false);
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/org/jlab/epics2web/websocket/SessionInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.jlab.epics2web.websocket;

/**
* This class provides a snapshot of websocket information that will not throw Exceptions if you try to interrogate it
* after the session happens to have closed. There is a race condition if you hand a list of javax.websocket.Session
* objects to a debug console for example as if the session happens to close between the time you return it and the
* time the debug console calls the userProperties method for example you get an exception.
*/
public class SessionInfo {
private String id;
private String ip;
private String name;
private String agent;
private long droppedMessageCount;

public SessionInfo(String id, String ip, String name, String agent, long droppedMessageCount) {
this.id = id;
this.ip = ip;
this.name = name;
this.agent = agent;
this.droppedMessageCount = droppedMessageCount;
}

public String getId() {
return id;
}

public String getIp() {
return ip;
}

public String getName() {
return name;
}

public String getAgent() {
return agent;
}

public long getDroppedMessageCount() {
return droppedMessageCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,33 @@ public void removePvs(Session session, Set<String> pvSet) {
*
* @return The map
*/
public Map<Session, Set<String>> getClientMap() {
Map<PvListener, Set<String>> pvMap = Application.channelManager.getClientMap();
Map<Session, Set<String>> clientMap = new HashMap<>();
public Map<SessionInfo, Set<String>> getClientMap() {
Map<PvListener, Set<String>> pvMap = Application.channelManager.getListenerMap();
Map<SessionInfo, Set<String>> clientMap = new HashMap<>();

for (Session session : listenerMap.keySet()) {
WebSocketSessionMonitor listener = listenerMap.get(session);
Set<String> pvSet = pvMap.get(listener);
clientMap.put(session, pvSet);

if(session.isOpen()) {
String id = null;

try {
id = session.getId();
String ip = (String)session.getUserProperties().get("ip");
String name = (String)session.getUserProperties().get("name");
String agent = (String)session.getUserProperties().get("agent");
AtomicLong droppedMessageCount = (AtomicLong)session.getUserProperties().get("droppedMessageCount");

SessionInfo info = new SessionInfo(id, ip, name, agent, droppedMessageCount.get());

clientMap.put(info, pvSet);
} catch(Exception e) {
// Even id may be null if closed before getId() called. Oh well.
LOGGER.log(Level.FINEST, "Session '{0}' closed while preparing info report", id);
// Ignore
}
}
}

return clientMap;
Expand Down Expand Up @@ -316,14 +335,17 @@ public void sendUpdate(Session session, String pv, DBR dbr) {
@SuppressWarnings("unchecked")
public void send(Session session, String pv, String msg) {
if (session.isOpen()) {
String id = session.toString();
if (Application.WRITE_STRATEGY == WriteStrategy.ASYNC_QUEUE) {
ConcurrentLinkedQueue<String> writequeue = (ConcurrentLinkedQueue<String>) session.getUserProperties().get("writequeue");

//System.out.println("Queue Size: " + writequeue.size());

if (writequeue.size() > Application.WRITE_QUEUE_SIZE_LIMIT) {
//LOGGER.log(Level.INFO, "Dropping message: {0}", msg);
AtomicLong dropCount = (AtomicLong)session.getUserProperties().get("droppedMessageCount");
dropCount.getAndIncrement();
long count = dropCount.getAndIncrement() + 1; // getAndIncrement is actually returning previous value, not newly updated, so we add 1.
// Limit log file output by only reporting when thresholds are reached
if(count == 1 || count == 1000 || count == 10000 || count == 100000) {
LOGGER.log(Level.FINEST, "Session {0} queue full (limit={1}); Dropping pv {2} message: {3}; total dropped: {4}", new Object[]{id, Application.WRITE_QUEUE_SIZE_LIMIT, pv, msg, count});
}
} else {
writequeue.offer(msg);
}
Expand All @@ -336,9 +358,12 @@ public void send(Session session, String pv, String msg) {
boolean success = writequeue.offer(msg);

if(!success) {
//LOGGER.log(Level.INFO, "Dropping message: {0}", msg);
AtomicLong dropCount = (AtomicLong)session.getUserProperties().get("droppedMessageCount");
dropCount.getAndIncrement();
long count = dropCount.getAndIncrement() + 1; // getAndIncrement is actually returning previous value, not newly updated, so we add 1.
// Limit log file output by only reporting when thresholds are reached
if(count == 1 || count == 1000 || count == 10000 || count == 100000) {
LOGGER.log(Level.FINEST, "Session {0} queue full (limit={1}); Dropping pv {2} message: {3}; total dropped: {4}", new Object[]{id, Application.WRITE_QUEUE_SIZE_LIMIT, pv, msg, count});
}
}
} else {
try {
Expand Down
8 changes: 4 additions & 4 deletions src/main/webapp/WEB-INF/views/console.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
<c:forEach items="${clientMap}" var="client">
<tr>
<td><c:out value="${client.key.id}"/></td>
<td><c:out value="${client.key.userProperties.ip eq null ? client.key.remoteAddr : client.key.userProperties.ip}"/></td>
<td><c:out value="${client.key.userProperties.agent}"/></td>
<td><c:out value="${client.key.userProperties.name}"/></td>
<td><c:out value="${client.key.ip}"/></td>
<td><c:out value="${client.key.agent}"/></td>
<td><c:out value="${client.key.name}"/></td>
<td>(${client.value == null ? '0' : client.value.size()}) <c:out value="${client.value}"/></td>
<td><fmt:formatNumber value="${client.key.userProperties.droppedMessageCount}"/></td>
<td><fmt:formatNumber value="${client.key.droppedMessageCount}"/></td>
</tr>
</c:forEach>
</tbody>
Expand Down
Loading