Skip to content

Instantly share code, notes, and snippets.

@wendelicious
Created May 2, 2014 00:33
Show Gist options
  • Save wendelicious/a98922d5844cf442e130 to your computer and use it in GitHub Desktop.
Save wendelicious/a98922d5844cf442e130 to your computer and use it in GitHub Desktop.
Decompiled Java from Joseph
mport com.infusion.databridge.BridgeException;
import com.infusion.databridge.ConInfo;
import com.infusion.databridge.Rst;
import com.infusion.util.Joiner;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class ConnectionStabilizer
{
private static final int minutes = 60000;
public static void main(String[] args)
throws FileNotFoundException
{
if (args.length < 6)
{
System.out.println("Usage: ConnectionStabilizer {host} {slot} {port} {user} {pass} {minutes between checks} {max connections allowed}");
System.exit(1);
}
String host = args[0];
String slot = args[1];
String port = args[2];
String user = args[3];
String pass = args[4];
int interval = Integer.parseInt(args[5]);
int max = Integer.parseInt(args[6]);
ConInfo con = new ConInfo(host, port, user, pass);
PrintWriter largeLog = new PrintWriter(new FileOutputStream("/var/log/connectionStabilizerLarge-" + slot + ".log", true));
for (;;)
{
System.out.println("\nInspecting process list at " + DateFormat.getDateTimeInstance().format(new Date()) + "...");
Map<String, Set<MysqlProcess>> userMap = new HashMap();
Map<String, Set<MysqlProcess>> dbMap = new HashMap();
Map<String, Set<MysqlProcess>> commandMap = new HashMap();
Set<MysqlProcess> processes = new HashSet();
Rst rst = con.getRst("SHOW FULL PROCESSLIST");
try
{
while (rst.next())
{
MysqlProcess process = new MysqlProcess(rst);
addToMap(userMap, process.user, process);
addToMap(dbMap, process.db, process);
addToMap(commandMap, process.command, process);
processes.add(process);
}
}
catch (BridgeException e)
{
e.printStackTrace();
}
if (processes.size() > 10)
{
System.out.println("Total processes: " + processes.size());
Joiner states = new Joiner();
for (String state : commandMap.keySet()) {
states.add(state + ": " + ((Set)commandMap.get(state)).size());
}
System.out.println("(By Command) " + states);
Joiner users = new Joiner();
for (String user : userMap.keySet()) {
users.add(user + ": " + ((Set)userMap.get(user)).size());
}
System.out.println("(By User) " + users);
Joiner dbs = new Joiner();
for (String db : dbMap.keySet())
{
Set<MysqlProcess> dbProcesses = (Set)dbMap.get(db);
int queryCount = 0;
int longQueryCount = 0;
for (MysqlProcess process : dbProcesses) {
if ((process.command != null) && (!process.command.equalsIgnoreCase("Sleep")))
{
queryCount++;
if (process.time.intValue() > 2) {
longQueryCount++;
}
}
}
if (queryCount > 0) {
dbs.add(db + ": " + queryCount);
}
if ((queryCount > 5) && (db != null) && (longQueryCount > 0))
{
largeLog.println("\n" + DateFormat.getDateTimeInstance().format(new Date()) + ": Large stack found for db: " + db);
for (MysqlProcess process : dbProcesses) {
if (process != null)
{
if (!"Sleep".equalsIgnoreCase(process.command)) {
largeLog.println("\tstate: " + process.state + " time: " + process.time + " command: " + process.command + " query: " + process.info);
}
if ((!"Locked".equalsIgnoreCase(process.state)) && ("Query".equalsIgnoreCase(process.command)) && (process.time.intValue() > 60)) {
try
{
process.kill();
System.out.println("Killed process " + process.id + " because it was causing slow query stackup");
}
catch (BridgeException e)
{
System.out.println("Exception while trying to kill " + process.id + ": " + e.getMessage());
}
}
}
}
largeLog.flush();
}
}
System.out.println("(By Db) " + dbs);
}
Set<MysqlProcess> crmmaster = (Set)dbMap.get("crmmaster");
if (crmmaster == null) {
crmmaster = new HashSet();
}
Set<MysqlProcess> util = (Set)userMap.get("util");
if (util == null) {
util = new HashSet();
}
Set<MysqlProcess> sleeping = (Set)commandMap.get("Sleep");
if (sleeping == null) {
sleeping = new HashSet();
}
int crmmasterkilled = 0;
int utilkilled = 0;
int sleepingkilled = 0;
for (;;)
{
if (processes.size() <= max) {
break label1509;
}
boolean success = false;
if ((!success) && (util.size() > 5))
{
Iterator i$ = util.iterator();
for (;;)
{
if (!i$.hasNext()) {
break label1295;
}
MysqlProcess process = (MysqlProcess)i$.next();
try
{
if ((process.command.equalsIgnoreCase("Sleep")) && (process.time.intValue() > 60))
{
killProcess(process, util, processes);
success = true;
utilkilled++;
break label1295;
}
}
catch (BridgeException e)
{
e.printStackTrace();
}
break;
}
}
label1295:
if ((!success) && (crmmaster.size() > 0))
{
Iterator i$ = crmmaster.iterator();
for (;;)
{
if (!i$.hasNext()) {
break label1388;
}
MysqlProcess process = (MysqlProcess)i$.next();
try
{
if (process.command.equalsIgnoreCase("Sleep"))
{
killProcess(process, crmmaster, processes);
success = true;
crmmasterkilled++;
break label1388;
}
}
catch (BridgeException e)
{
e.printStackTrace();
}
break;
}
}
label1388:
if ((!success) && (sleeping.size() > 0))
{
Iterator i$ = sleeping.iterator();
if (i$.hasNext())
{
MysqlProcess process = (MysqlProcess)i$.next();
try
{
killProcess(process, sleeping, processes);
success = true;
sleepingkilled++;
}
catch (BridgeException e)
{
e.printStackTrace();
}
break;
}
}
if (!success)
{
System.out.println("Couldn't kill enough processes! " + processes.size() + " remain.");
break label1509;
}
}
label1509:
if (crmmasterkilled + sleepingkilled > 0)
{
Joiner j = new Joiner(" and ");
if (utilkilled > 0) {
j.add(utilkilled + " util");
}
if (crmmasterkilled > 0) {
j.add(crmmasterkilled + " crmmaster");
}
if (sleepingkilled > 0) {
j.add(sleepingkilled + " sleeping");
}
System.out.println("Killed " + j + " processes");
}
try
{
Thread.sleep(interval * 60000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
private static void killProcess(MysqlProcess process, Set<MysqlProcess> set, Set<MysqlProcess> processes)
throws BridgeException
{
process.kill();
set.remove(process);
processes.remove(process);
}
private static void addToMap(Map<String, Set<MysqlProcess>> map, String key, MysqlProcess process)
{
Set<MysqlProcess> processes = (Set)map.get(key);
if (processes == null)
{
processes = new HashSet();
map.put(key, processes);
}
processes.add(process);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment