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

[H2-1975] Add a TCP_CONNECTIONS table to the INFORMATION_SCHEMA #1990

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions h2/src/main/org/h2/server/TcpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -520,4 +522,11 @@ public boolean isDaemon() {
return isDaemon;
}

public static Map<Integer, TcpServer> listTcpServers() {
return new HashMap<>(SERVERS);
}

public Set<TcpServerThread> listThreads() {
return new HashSet<TcpServerThread>(running);
}
}
16 changes: 16 additions & 0 deletions h2/src/main/org/h2/server/TcpServerThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,22 @@ Thread getThread() {
return thread;
}

public Transfer getTransfer() {
return this.transfer;
}

public Integer getSessionId() {
return (this.session != null ? this.session.getId() : null);
}

public String getTcpSessionId() {
return this.sessionId;
}

public int getThreadId() {
return this.threadId;
}

/**
* Cancel a running statement.
*
Expand Down
74 changes: 73 additions & 1 deletion h2/src/main/org/h2/table/MetaTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.Socket;
import java.net.SocketException;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Types;
Expand All @@ -17,6 +19,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.h2.command.Command;
import org.h2.constraint.Constraint;
Expand Down Expand Up @@ -54,6 +57,8 @@
import org.h2.schema.SchemaObject;
import org.h2.schema.Sequence;
import org.h2.schema.TriggerObject;
import org.h2.server.TcpServer;
import org.h2.server.TcpServerThread;
import org.h2.store.InDoubtTransaction;
import org.h2.tools.Csv;
import org.h2.util.DateTimeUtils;
Expand Down Expand Up @@ -117,7 +122,8 @@ public class MetaTable extends Table {
private static final int TABLE_CONSTRAINTS = 30;
private static final int KEY_COLUMN_USAGE = 31;
private static final int REFERENTIAL_CONSTRAINTS = 32;
private static final int META_TABLE_TYPE_COUNT = REFERENTIAL_CONSTRAINTS + 1;
private static final int TCP_CONNECTIONS = 33;
private static final int META_TABLE_TYPE_COUNT = TCP_CONNECTIONS + 1;

private final int type;
private final int indexColumn;
Expand Down Expand Up @@ -621,6 +627,26 @@ public MetaTable(Schema schema, int id, int type) {
);
break;
}
case TCP_CONNECTIONS: {
setMetaTableName("TCP_CONNECTIONS");
cols = createColumns(
"TCP_SESSION_ID",
"SESSION_ID",
"THREAD_ID",
"REMOTE_IP_ADDRESS",
"REMOTE_PORT",
"LOCAL_IP_ADDRESS",
"LOCAL_PORT",
"RECV_BUFFER_SIZE",
"SEND_BUFFER_SIZE",
"KEEP_ALIVE",
"SO_TIMEOUT",
"SO_LINGER",
"TCP_NO_DELAY",
"IS_CLOSED"
);
break;
}
default:
throw DbException.throwInternalError("type="+type);
}
Expand Down Expand Up @@ -2152,6 +2178,52 @@ public ArrayList<Row> generateRows(Session session, SearchRow first,
}
break;
}
case TCP_CONNECTIONS: {
Map<Integer, TcpServer> tcpServers = TcpServer.listTcpServers();
for(Map.Entry<Integer, TcpServer> tcpServerEntry : tcpServers.entrySet()) {
for(TcpServerThread tcpServerThread : tcpServerEntry.getValue().listThreads()) {
if(tcpServerThread.getTransfer() == null || tcpServerThread.getTransfer().getSocket() == null) {
continue;
}
Socket socket = tcpServerThread.getTransfer().getSocket();

Integer soTimeoutValue = null;
try { soTimeoutValue = socket.getSoTimeout(); } catch (SocketException e) {}

Integer sendBufferSizeValue = null;
try { sendBufferSizeValue = socket.getSendBufferSize(); } catch (SocketException e) {}

Integer recvBufferSizeValue = null;
try { recvBufferSizeValue = socket.getSendBufferSize(); } catch (SocketException e) {}

Boolean keepAliveValue = null;
try { keepAliveValue = socket.getKeepAlive(); } catch (SocketException e) {}

Integer soLingerValue = null;
try { soLingerValue = socket.getSoLinger(); } catch (SocketException e) {}

Boolean tcpNoDelayValue = null;
try { tcpNoDelayValue = socket.getTcpNoDelay(); } catch (SocketException e) {}

add(rows,
tcpServerThread.getTcpSessionId(),
ValueInt.get(tcpServerThread.getSessionId()),
ValueInt.get(tcpServerThread.getThreadId()),
(socket.getInetAddress() != null ? socket.getInetAddress().toString().replace("/","") : null),
ValueInt.get(socket.getPort()),
(socket.getLocalAddress() != null ? socket.getLocalAddress().toString().replace("/","") : null),
ValueInt.get(socket.getLocalPort()),
ValueInt.get(recvBufferSizeValue),
ValueInt.get(sendBufferSizeValue),
ValueBoolean.get(keepAliveValue),
ValueInt.get(soTimeoutValue),
ValueInt.get(soLingerValue),
ValueBoolean.get(tcpNoDelayValue),
ValueBoolean.get(socket.isClosed()));
}
}
break;
}
default:
DbException.throwInternalError("type="+type);
}
Expand Down