db 自动清除过期历史消息数据

This commit is contained in:
heavyrain2012
2021-01-23 18:01:28 +08:00
parent e5a4aa4741
commit 7923a3e0c4
6 changed files with 365 additions and 140 deletions
+5
View File
@@ -35,6 +35,11 @@ local_port 80
##是否使用内置DB。0使用mysql;1使用h2db;2使用mysql+mongodb。社区版只支持0和1,专业版支持2 ##是否使用内置DB。0使用mysql;1使用h2db;2使用mysql+mongodb。社区版只支持0和1,专业版支持2
embed.db 1 embed.db 1
## 是否自动清理历史消息记录,当为true时,IM服务会定时扫描t_messages_x和t_user_messages_x表,清除掉3年前的历史消息。
## 如果不开启自动清理,需要手动清理t_messages_x和t_user_messages_x表内的历史数据,保存数据不能超过3年。
## 使用mongodb时次开关无效(因为mongodb设置了消息过期时间,会自动清理)。
db.auto_clean_history_messages true
##服务器管理接口密钥 ##服务器管理接口密钥
http.admin.secret_key 123456 http.admin.secret_key 123456
@@ -117,6 +117,7 @@ public final class BrokerConstants {
public static final String TOKEN_EXPIRE_TIME = "token.expire_time"; public static final String TOKEN_EXPIRE_TIME = "token.expire_time";
public static final String EMBED_DB_PROPERTY_NAME = "embed.db"; public static final String EMBED_DB_PROPERTY_NAME = "embed.db";
public static final String DB_AUTO_CLEAN_HISTORY_MESSAGES = "db.auto_clean_history_messages";
public static final String SENSITIVE_Filter_Type = "sensitive.filter.type"; public static final String SENSITIVE_Filter_Type = "sensitive.filter.type";
@@ -436,6 +436,7 @@ public class Server {
System.out.println("Server will flush data to db before shutting down, please wait 5 seconds!"); System.out.println("Server will flush data to db before shutting down, please wait 5 seconds!");
LOG.info("Unbinding server from the configured ports"); LOG.info("Unbinding server from the configured ports");
m_shutdowning = true; m_shutdowning = true;
DBUtil.SystemExiting = true;
m_acceptor.close(); m_acceptor.close();
LOG.trace("Stopping MQTT protocol processor"); LOG.trace("Stopping MQTT protocol processor");
+333 -140
View File
@@ -14,6 +14,8 @@ import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.sql.*; import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List; import java.util.List;
import com.hazelcast.util.StringUtil; import com.hazelcast.util.StringUtil;
@@ -27,6 +29,8 @@ import org.slf4j.LoggerFactory;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import static cn.wildfirechat.common.IMExceptionEvent.EventType.RDBS_Exception;
public class DBUtil { public class DBUtil {
private static final Logger LOG = LoggerFactory.getLogger(DBUtil.class); private static final Logger LOG = LoggerFactory.getLogger(DBUtil.class);
private static ComboPooledDataSource comboPooledDataSource = null; private static ComboPooledDataSource comboPooledDataSource = null;
@@ -39,118 +43,307 @@ public class DBUtil {
} }
}; };
public static boolean IsEmbedDB = false; public static boolean IsEmbedDB = false;
public static boolean SystemExiting = false;
public static boolean ClearDBDebugMode = false;
public static void init(IConfig config) { public static void init(IConfig config) {
String embedDB = config.getProperty(BrokerConstants.EMBED_DB_PROPERTY_NAME); String embedDB = config.getProperty(BrokerConstants.EMBED_DB_PROPERTY_NAME);
if (embedDB != null && embedDB.equals("1")) { boolean autoCleanMsgs = "true".equals(config.getProperty(BrokerConstants.DB_AUTO_CLEAN_HISTORY_MESSAGES));
IsEmbedDB = true; if (embedDB != null && embedDB.equals("1")) {
LOG.info("Use h2 database"); IsEmbedDB = true;
LOG.info("Use h2 database");
} else {
IsEmbedDB = false;
LOG.info("Use mysql database");
}
if (comboPooledDataSource == null) {
String migrateLocation;
if (IsEmbedDB) {
migrateLocation = "filesystem:./migrate/h2";
comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setJdbcUrl( "jdbc:h2:./h2db/wfchat;AUTO_SERVER=TRUE;MODE=MySQL" );
comboPooledDataSource.setUser("SA");
comboPooledDataSource.setPassword("SA");
comboPooledDataSource.setMinPoolSize(5);
comboPooledDataSource.setAcquireIncrement(5);
comboPooledDataSource.setMaxPoolSize(20);
comboPooledDataSource.setIdleConnectionTestPeriod(60 * 5);
comboPooledDataSource.setMinPoolSize(3);
comboPooledDataSource.setInitialPoolSize(3);
try {
comboPooledDataSource.setDriverClass( "org.h2.Driver" ); //loads the jdbc driver
} catch (PropertyVetoException e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
System.exit(-1);
}
} else { } else {
IsEmbedDB = false; migrateLocation = "filesystem:./migrate/mysql";
LOG.info("Use mysql database"); comboPooledDataSource = new ComboPooledDataSource("mysql");
try {
String url01 = comboPooledDataSource.getJdbcUrl().substring(0,comboPooledDataSource.getJdbcUrl().indexOf("?"));
String datasourceName = url01.substring(url01.lastIndexOf("/")+1);
// 连接已经存在的数据库,如:mysql
String jdbc = comboPooledDataSource.getJdbcUrl().replace(datasourceName, "");
Connection connection = DriverManager.getConnection(jdbc, comboPooledDataSource.getUser(), comboPooledDataSource.getPassword());
Statement statement = connection.createStatement();
// 创建数据库
statement.executeUpdate("CREATE DATABASE IF NOT EXISTS `" + datasourceName + "` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;");
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
Flyway flyway = Flyway.configure().dataSource(comboPooledDataSource).locations(migrateLocation).baselineOnMigrate(true).load();
flyway.migrate();
if(autoCleanMsgs) {
cleanHistoryMsg();
}
}
}
private static void cleanHistoryMsg() {
new Thread(()->{
long usedTime = 0;
while (true) {
if(SystemExiting) {
break;
}
try {
long sleepTime;
if(ClearDBDebugMode) {
sleepTime = 60 * 1000 - usedTime;
} else {
sleepTime = 60 * 60 * 1000 - usedTime;
}
if(sleepTime < 0) {
sleepTime = 5 * 1000;
}
Thread.sleep(sleepTime);
LOG.info("Start clean history messages");
if(SystemExiting) {
break;
}
long start = System.currentTimeMillis();
if(IsEmbedDB) {
clearH2DB();
} else {
clearMySQL();
}
usedTime = System.currentTimeMillis() - start;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
private static boolean sleep() {
if(SystemExiting)
return true;
try {
if(ClearDBDebugMode) {
Thread.sleep(500);
} else {
Thread.sleep(10000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return SystemExiting;
}
private static void clearH2DB() {
boolean hasMore = true;
while (hasMore) {
hasMore = clearOneTable("t_user_messages");
if(sleep()) {
return;
}
}
hasMore = true;
while (hasMore) {
hasMore = clearOneTable("t_messages");
if(sleep()) {
return;
}
}
}
private static void clearMySQL() {
int i = 0;
do {
if(!clearOneTable("t_user_messages_" + i)) {
i++;
} }
if (comboPooledDataSource == null) { if(sleep()) {
String migrateLocation; return;
if (IsEmbedDB) { }
migrateLocation = "filesystem:./migrate/h2"; } while (i < 128);
comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setJdbcUrl( "jdbc:h2:./h2db/wfchat;AUTO_SERVER=TRUE;MODE=MySQL" ); boolean hasMore = true;
comboPooledDataSource.setUser("SA"); String msgTableName;
comboPooledDataSource.setPassword("SA"); if(ClearDBDebugMode) {
comboPooledDataSource.setMinPoolSize(5); msgTableName = MessageShardingUtil.getMessageTable(MessageShardingUtil.getMsgIdFromTimestamp(System.currentTimeMillis()));
comboPooledDataSource.setAcquireIncrement(5); } else {
comboPooledDataSource.setMaxPoolSize(20); msgTableName = MessageShardingUtil.getMessageTable(MessageShardingUtil.getMsgIdFromTimestamp(System.currentTimeMillis()), 1);
}
while (hasMore) {
hasMore = clearOneTable(msgTableName);
if(sleep()) {
return;
}
}
comboPooledDataSource.setIdleConnectionTestPeriod(60 * 5); Calendar cal = Calendar.getInstance();
comboPooledDataSource.setMinPoolSize(3); cal.setTime(new Date());
comboPooledDataSource.setInitialPoolSize(3); if(cal.get(Calendar.DAY_OF_MONTH) > 25 && !ClearDBDebugMode) {
hasMore = true;
msgTableName = MessageShardingUtil.getMessageTable(MessageShardingUtil.getMsgIdFromTimestamp(System.currentTimeMillis()), 2);
while (hasMore) {
hasMore = clearOneTable(msgTableName);
if(sleep()) {
return;
}
}
}
}
try { private static boolean clearOneTable(String tableName) {
comboPooledDataSource.setDriverClass( "org.h2.Driver" ); //loads the jdbc driver Connection connection = null;
} catch (PropertyVetoException e) { PreparedStatement statement = null;
e.printStackTrace(); ResultSet rs = null;
Utility.printExecption(LOG, e); int clearCount = 999;
System.exit(-1); if(ClearDBDebugMode) {
} clearCount = 19;
}
boolean hasMore = true;
try {
connection = DBUtil.getConnection();
String sql = "select id, _mid from " + tableName + " order by id limit 1 offset " + clearCount;
statement = connection.prepareStatement(sql);
rs = statement.executeQuery();
int id = 0;
long mid = 0;
if (rs.next()) {
id = rs.getInt(1);
mid = rs.getLong(2);
}
if(id == 0) {
hasMore = false;
rs.close();
rs = null;
statement.close();
sql = "select id, _mid from " + tableName + " order by id desc limit 1";
statement = connection.prepareStatement(sql);
rs = statement.executeQuery();
if (rs.next()) {
id = rs.getInt(1);
mid = rs.getLong(2);
}
}
rs.close();
rs = null;
LOG.info("Check history message in table {}, id {}, mid {}, hasMore {}", tableName, id, mid, hasMore);
if(id != 0) {
Calendar tm = MessageShardingUtil.getCalendarFromMessageId(mid);
if(ClearDBDebugMode) {
tm.add(Calendar.HOUR, 1);
} else { } else {
migrateLocation = "filesystem:./migrate/mysql"; tm.add(Calendar.YEAR, 3);
comboPooledDataSource = new ComboPooledDataSource("mysql"); tm.add(Calendar.MONTH, -1);
try { tm.add(Calendar.DATE, -4);
String url01 = comboPooledDataSource.getJdbcUrl().substring(0,comboPooledDataSource.getJdbcUrl().indexOf("?"));
String datasourceName = url01.substring(url01.lastIndexOf("/")+1);
// 连接已经存在的数据库,如:mysql
String jdbc = comboPooledDataSource.getJdbcUrl().replace(datasourceName, "");
Connection connection = DriverManager.getConnection(jdbc, comboPooledDataSource.getUser(), comboPooledDataSource.getPassword());
Statement statement = connection.createStatement();
// 创建数据库
statement.executeUpdate("CREATE DATABASE IF NOT EXISTS `" + datasourceName + "` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;");
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
} }
Flyway flyway = Flyway.configure().dataSource(comboPooledDataSource).locations(migrateLocation).baselineOnMigrate(true).load(); if(tm.getTimeInMillis() < System.currentTimeMillis()) {
flyway.migrate(); statement.close();
} sql = "delete from " + tableName + " where id <= " + id;
} statement = connection.prepareStatement(sql);
int count = statement.executeUpdate();
private static List<String> getCreateSql() { LOG.info("Clear {} rows", count);
List<String> out = new ArrayList<>(); } else {
try{ LOG.info("No history messages need clean");
BufferedReader br = new BufferedReader(new FileReader("h2/create_table.sql"));//构造一个BufferedReader类来读取文件 hasMore = false;
String s = null;
StringBuilder result = new StringBuilder();
while((s = br.readLine())!=null) {
result.append(s);
if (s.contains(";")) {
out.add(result.toString());
result = new StringBuilder();
}
} }
br.close(); } else {
}catch(Exception e){ LOG.info("No messages");
e.printStackTrace();
Utility.printExecption(LOG, e);
} }
return out; } catch (Exception e) {
hasMore = false;
e.printStackTrace();
Utility.printExecption(LOG, e, RDBS_Exception);
} finally {
DBUtil.closeDB(connection, statement, rs);
} }
//从数据源中获取数据库的连接 return hasMore;
public static Connection getConnection() throws SQLException { }
long threadId = Thread.currentThread().getId();
if (map.get(threadId) != null) { private static List<String> getCreateSql() {
LOG.error("error here!!!! DB connection not close correctly"); List<String> out = new ArrayList<>();
} try{
map.put(threadId, Thread.currentThread().getStackTrace().toString()); BufferedReader br = new BufferedReader(new FileReader("h2/create_table.sql"));//构造一个BufferedReader类来读取文件
Connection connection = transactionConnection.get(); String s = null;
if (connection != null) { StringBuilder result = new StringBuilder();
LOG.debug("Thread {} get db connection {}", threadId, connection); while((s = br.readLine())!=null) {
return connection; result.append(s);
if (s.contains(";")) {
out.add(result.toString());
result = new StringBuilder();
}
} }
br.close();
}catch(Exception e){
e.printStackTrace();
Utility.printExecption(LOG, e);
}
return out;
}
//从数据源中获取数据库的连接
public static Connection getConnection() throws SQLException {
long threadId = Thread.currentThread().getId();
connection = comboPooledDataSource.getConnection(); if (map.get(threadId) != null) {
LOG.error("error here!!!! DB connection not close correctly");
}
map.put(threadId, Thread.currentThread().getStackTrace().toString());
Connection connection = transactionConnection.get();
if (connection != null) {
LOG.debug("Thread {} get db connection {}", threadId, connection); LOG.debug("Thread {} get db connection {}", threadId, connection);
return connection; return connection;
} }
public static void beginTransaction() { connection = comboPooledDataSource.getConnection();
try { LOG.debug("Thread {} get db connection {}", threadId, connection);
Connection connection = getConnection(); return connection;
connection.setAutoCommit(false); }
transactionConnection.set(connection);
} catch (SQLException e) { public static void beginTransaction() {
e.printStackTrace(); try {
Utility.printExecption(LOG, e); Connection connection = getConnection();
} connection.setAutoCommit(false);
transactionConnection.set(connection);
} catch (SQLException e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
} }
}
public static void commit() { public static void commit() {
try { try {
@@ -180,57 +373,57 @@ public class DBUtil {
} }
} }
//释放资源,将数据库连接还给数据库连接池 //释放资源,将数据库连接还给数据库连接池
public static void closeDB(Connection conn,PreparedStatement ps,ResultSet rs) { public static void closeDB(Connection conn,PreparedStatement ps,ResultSet rs) {
LOG.debug("Thread {} release db connection {}", Thread.currentThread().getId(), conn); LOG.debug("Thread {} release db connection {}", Thread.currentThread().getId(), conn);
try { try {
if (rs!=null) { if (rs!=null) {
rs.close(); rs.close();
}
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
try {
if (ps!=null) {
ps.close();
}
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
try {
if (conn!=null && transactionConnection.get() != conn) {
conn.close();
map.remove(Thread.currentThread().getId());
}
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
} }
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
} }
//释放资源,将数据库连接还给数据库连接池
public static void closeDB(Connection conn, PreparedStatement ps) {
LOG.debug("Thread {} release db connection {}", Thread.currentThread().getId(), conn);
try {
if (ps!=null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
try { try {
if (conn!=null && transactionConnection.get() != conn) { if (ps!=null) {
conn.close(); ps.close();
map.remove(Thread.currentThread().getId());
}
} catch (SQLException e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
} }
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
} }
try {
if (conn!=null && transactionConnection.get() != conn) {
conn.close();
map.remove(Thread.currentThread().getId());
}
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
}
//释放资源,将数据库连接还给数据库连接池
public static void closeDB(Connection conn, PreparedStatement ps) {
LOG.debug("Thread {} release db connection {}", Thread.currentThread().getId(), conn);
try {
if (ps!=null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
try {
if (conn!=null && transactionConnection.get() != conn) {
conn.close();
map.remove(Thread.currentThread().getId());
}
} catch (SQLException e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
}
} }
@@ -101,6 +101,26 @@ public class MessageShardingUtil {
year %= 3; year %= 3;
return "t_messages_" + (year * 12 + month); return "t_messages_" + (year * 12 + month);
} }
static Calendar getCalendarFromMessageId(long mid) {
Calendar calendar = Calendar.getInstance();
if (mid != Long.MAX_VALUE && mid != 0) {
mid >>= (nodeIdWidth + rotateIdWidth);
Date date = new Date(mid + T201801010000);
calendar.setTime(date);
} else {
Date date = new Date(System.currentTimeMillis());
calendar.setTime(date);
}
return calendar;
}
public static long getMsgIdFromTimestamp(long timestamp) {
long id = timestamp - T201801010000;
id <<= rotateIdWidth;
id <<= nodeIdWidth;
return id;
}
public static String getPreviousMessageTable(long mid) { public static String getPreviousMessageTable(long mid) {
return getMessageTable(mid, -1); return getMessageTable(mid, -1);
@@ -31,6 +31,11 @@ local_port 80
##是否使用内置DB。0使用mysql;1使用h2db;2使用mysql+mongodb。社区版只支持0和1,专业版支持2 ##是否使用内置DB。0使用mysql;1使用h2db;2使用mysql+mongodb。社区版只支持0和1,专业版支持2
embed.db 1 embed.db 1
## 是否自动清理历史消息记录,当为true时,IM服务会定时扫描t_messages_x和t_user_messages_x表,清除掉3年前的历史消息。
## 如果不开启自动清理,需要手动清理t_messages_x和t_user_messages_x表内的历史数据,保存数据不能超过3年。
## 使用mongodb时次开关无效(因为mongodb设置了消息过期时间,会自动清理)。
db.auto_clean_history_messages true
##服务器管理接口密钥 ##服务器管理接口密钥
http.admin.secret_key 123456 http.admin.secret_key 123456