Skip to content

Commit

Permalink
新增 SQL / HIGH / SLOW / TABLE 指令 clear 参数 true表示清除cache, 如: show @@Sql
Browse files Browse the repository at this point in the history
…true; show @@sql.high false;
  • Loading branch information
zhuam committed Jan 22, 2016
1 parent 5abc912 commit afb62d5
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 30 deletions.
12 changes: 8 additions & 4 deletions src/main/java/org/opencloudb/handler/ShowHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ public static void handle(String stmt, ManagerConnection c, int offset) {
ShowWhiteHost.setHost(c,ParseUtil.parseString(stmt));
break;
case ManagerParseShow.SQL:
ShowSQL.execute(c, ParseUtil.getSQLId(stmt));
boolean isClearSql = Boolean.valueOf( stmt.substring(rs >>> 8).trim() );
ShowSQL.execute(c, isClearSql);
break;
case ManagerParseShow.SQL_DETAIL:
ShowSQLDetail.execute(c, ParseUtil.getSQLId(stmt));
Expand All @@ -154,10 +155,12 @@ public static void handle(String stmt, ManagerConnection c, int offset) {
ShowSQLExecute.execute(c);
break;
case ManagerParseShow.SQL_SLOW:
ShowSQLSlow.execute(c);
boolean isClearSlow = Boolean.valueOf( stmt.substring(rs >>> 8).trim() );
ShowSQLSlow.execute(c, isClearSlow);
break;
case ManagerParseShow.SQL_HIGH:
ShowSQLHigh.execute(c);
boolean isClearHigh = Boolean.valueOf( stmt.substring(rs >>> 8).trim() );
ShowSQLHigh.execute(c, isClearHigh);
break;
case ManagerParseShow.SQL_CONDITION:
ShowSQLCondition.execute(c);
Expand All @@ -166,7 +169,8 @@ public static void handle(String stmt, ManagerConnection c, int offset) {
ShowSQLSumUser.execute(c);
break;
case ManagerParseShow.SQL_SUM_TABLE:
ShowSQLSumTable.execute(c);
boolean isClearTable = Boolean.valueOf( stmt.substring(rs >>> 8).trim() );
ShowSQLSumTable.execute(c, isClearTable);
break;
case ManagerParseShow.SLOW_DATANODE: {
String name = stmt.substring(rs >>> 8).trim();
Expand Down
50 changes: 37 additions & 13 deletions src/main/java/org/opencloudb/parser/ManagerParseShow.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public final class ManagerParseShow {

public static final int WHITE_HOST=42;
public static final int WHITE_HOST_SET=43;

public static int parse(String stmt, int offset) {
int i = offset;
for (; i < stmt.length(); i++) {
Expand Down Expand Up @@ -1183,13 +1184,12 @@ static int show2SqlCheck(String stmt, int offset) {
return show2SqlDotCheck(stmt, offset);
case ' ':
return show2SqlBlankCheck(stmt, offset);
default:
return OTHER;
default:
return SQL;
}
}
else
} else {
return SQL;

}
}

// SHOW @@SQL.
Expand Down Expand Up @@ -1240,7 +1240,7 @@ static int show2SqlBlankCheck(String stmt, int offset) {
return OTHER;
}
default:
return OTHER;
return (offset << 8) | SQL;
}
}

Expand Down Expand Up @@ -1303,9 +1303,16 @@ static int show2SqlSLCheck(String stmt, int offset) {
char c1 = stmt.charAt(++offset);
char c2 = stmt.charAt(++offset);
if ((c1 == 'O' || c1 == 'o') && (c2 == 'W' || c2 == 'w')) {
if (stmt.length() > ++offset && stmt.charAt(offset) != ' ') {
return OTHER;
}

while (stmt.length() > ++offset) {
switch (stmt.charAt(offset)) {
case ' ':
continue;
default:
return (offset << 8) | SQL_SLOW;
}
}

return SQL_SLOW;
}
}
Expand All @@ -1320,9 +1327,16 @@ static int show2SqlHCheck(String stmt, int offset) {
char c2 = stmt.charAt(++offset);
char c3 = stmt.charAt(++offset);
if ((c1 == 'I' || c1 == 'i') && (c2 == 'G' || c2 == 'g') && (c3 == 'H' || c3 == 'h') ) {
if (stmt.length() > ++offset && stmt.charAt(offset) != ' ') {
return OTHER;
}

while (stmt.length() > ++offset) {
switch (stmt.charAt(offset)) {
case ' ':
continue;
default:
return (offset << 8) | SQL_HIGH;
}
}

return SQL_HIGH;
}
}
Expand Down Expand Up @@ -1382,7 +1396,17 @@ static int show2SqlSUCheck(String stmt, int offset) {
if ( stmt.length() > (offset+1) ) {
char c6 = stmt.charAt(++offset);
if ( c6 == 'E' || c6 == 'e') {
return SQL_SUM_TABLE;

while (stmt.length() > ++offset) {
switch (stmt.charAt(offset)) {
case ' ':
continue;
default:
return (offset << 8) | SQL_SUM_TABLE;
}
}

return SQL_SUM_TABLE;
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/opencloudb/response/ShowSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public final class ShowSQL {
eof.packetId = ++packetId;
}

public static void execute(ManagerConnection c, long sql) {
public static void execute(ManagerConnection c, boolean isClear) {
ByteBuffer buffer = c.allocate();

// write header
Expand All @@ -103,7 +103,11 @@ public static void execute(ManagerConnection c, long sql) {
buffer = row.write(buffer, c,true);
}
}
userStat.getSqlStat().clear();//读取SQL监控后清理

//读取SQL监控后清理
if ( isClear ) {
userStat.getSqlStat().clear();
}
}


Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/opencloudb/response/ShowSQLHigh.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public final class ShowSQLHigh {
eof.packetId = ++packetId;
}

public static void execute(ManagerConnection c) {
public static void execute(ManagerConnection c, boolean isClear) {
ByteBuffer buffer = c.allocate();

// write header
Expand All @@ -74,7 +74,7 @@ public static void execute(ManagerConnection c) {

// write rows
byte packetId = eof.packetId;
List<Map.Entry<String, SqlFrequency>> list = HighFrequencySqlAnalyzer.getInstance().getSqlFrequency();
List<Map.Entry<String, SqlFrequency>> list = HighFrequencySqlAnalyzer.getInstance().getSqlFrequency( isClear );
if ( list != null ) {
for (int i = 0; i < list.size(); i++) {
SqlFrequency sqlFrequency = list.get(i).getValue();
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/opencloudb/response/ShowSQLSlow.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public final class ShowSQLSlow {
eof.packetId = ++packetId;
}

public static void execute(ManagerConnection c) {
public static void execute(ManagerConnection c, boolean isClear) {
ByteBuffer buffer = c.allocate();

// write header
Expand All @@ -102,7 +102,10 @@ public static void execute(ManagerConnection c) {
buffer = row.write(buffer, c,true);
}
}
userStat.getSqlRecorder().clear();//读取慢SQL后,清理

if ( isClear ) {
userStat.getSqlRecorder().clear();//读取慢SQL后,清理
}
}

// write last eof
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/opencloudb/response/ShowSQLSumTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class ShowSQLSumTable {
eof.packetId = ++packetId;
}

public static void execute(ManagerConnection c) {
public static void execute(ManagerConnection c, boolean isClear) {
ByteBuffer buffer = c.allocate();

// write header
Expand All @@ -84,7 +84,7 @@ public static void execute(ManagerConnection c) {
buffer = row.write(buffer, c,true);
}
*/
List<Map.Entry<String, TableStat>> list =TableStatAnalyzer.getInstance().getTableStats();
List<Map.Entry<String, TableStat>> list = TableStatAnalyzer.getInstance().getTableStats(isClear);
if ( list != null ) {
for (int i = 0; i < list.size(); i++) {
TableStat tableStat=list.get(i).getValue();
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/org/opencloudb/stat/HighFrequencySqlAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void onQueryResult(QueryResult queryResult) {
/**
* 获取 SQL 访问频率
*/
public List<Map.Entry<String, SqlFrequency>> getSqlFrequency() {
public List<Map.Entry<String, SqlFrequency>> getSqlFrequency(boolean isClear) {

List<Map.Entry<String, SqlFrequency>> list = null;

Expand All @@ -97,12 +97,25 @@ public List<Map.Entry<String, SqlFrequency>> getSqlFrequency() {
} finally {
lock.readLock().unlock();
}
ClearSqlFrequency(); // 获取 高频SQL后清理

if ( isClear ) {
clearSqlFrequency(); // 获取 高频SQL后清理
}

return list;
}
public void ClearSqlFrequency() {
sqlFrequencyMap.clear();


private void clearSqlFrequency() {

lock.readLock().lock();
try {
sqlFrequencyMap.clear();
} finally {
lock.readLock().unlock();
}
}

/**
* 排序
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/opencloudb/stat/TableStatAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public Map<String, TableStat> getTableStatMap() {
/**
* 获取 table 访问排序统计
*/
public List<Map.Entry<String, TableStat>> getTableStats() {
public List<Map.Entry<String, TableStat>> getTableStats(boolean isClear) {

List<Map.Entry<String, TableStat>> list = null;

Expand All @@ -123,8 +123,10 @@ public List<Map.Entry<String, TableStat>> getTableStats() {
} finally {
lock.readLock().unlock();
}

//先不清理
//ClearTable();//获取 table 访问排序统计后清理

return list;
}

Expand Down

0 comments on commit afb62d5

Please sign in to comment.