db 自动清除过期历史消息数据
This commit is contained in:
@@ -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");
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -40,9 +44,12 @@ 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);
|
||||||
|
boolean autoCleanMsgs = "true".equals(config.getProperty(BrokerConstants.DB_AUTO_CLEAN_HISTORY_MESSAGES));
|
||||||
if (embedDB != null && embedDB.equals("1")) {
|
if (embedDB != null && embedDB.equals("1")) {
|
||||||
IsEmbedDB = true;
|
IsEmbedDB = true;
|
||||||
LOG.info("Use h2 database");
|
LOG.info("Use h2 database");
|
||||||
@@ -99,8 +106,194 @@ public class DBUtil {
|
|||||||
}
|
}
|
||||||
Flyway flyway = Flyway.configure().dataSource(comboPooledDataSource).locations(migrateLocation).baselineOnMigrate(true).load();
|
Flyway flyway = Flyway.configure().dataSource(comboPooledDataSource).locations(migrateLocation).baselineOnMigrate(true).load();
|
||||||
flyway.migrate();
|
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(sleep()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} while (i < 128);
|
||||||
|
|
||||||
|
boolean hasMore = true;
|
||||||
|
String msgTableName;
|
||||||
|
if(ClearDBDebugMode) {
|
||||||
|
msgTableName = MessageShardingUtil.getMessageTable(MessageShardingUtil.getMsgIdFromTimestamp(System.currentTimeMillis()));
|
||||||
|
} else {
|
||||||
|
msgTableName = MessageShardingUtil.getMessageTable(MessageShardingUtil.getMsgIdFromTimestamp(System.currentTimeMillis()), 1);
|
||||||
|
}
|
||||||
|
while (hasMore) {
|
||||||
|
hasMore = clearOneTable(msgTableName);
|
||||||
|
if(sleep()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.setTime(new Date());
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean clearOneTable(String tableName) {
|
||||||
|
Connection connection = null;
|
||||||
|
PreparedStatement statement = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
int clearCount = 999;
|
||||||
|
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 {
|
||||||
|
tm.add(Calendar.YEAR, 3);
|
||||||
|
tm.add(Calendar.MONTH, -1);
|
||||||
|
tm.add(Calendar.DATE, -4);
|
||||||
|
}
|
||||||
|
if(tm.getTimeInMillis() < System.currentTimeMillis()) {
|
||||||
|
statement.close();
|
||||||
|
sql = "delete from " + tableName + " where id <= " + id;
|
||||||
|
statement = connection.prepareStatement(sql);
|
||||||
|
int count = statement.executeUpdate();
|
||||||
|
LOG.info("Clear {} rows", count);
|
||||||
|
} else {
|
||||||
|
LOG.info("No history messages need clean");
|
||||||
|
hasMore = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LOG.info("No messages");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
hasMore = false;
|
||||||
|
e.printStackTrace();
|
||||||
|
Utility.printExecption(LOG, e, RDBS_Exception);
|
||||||
|
} finally {
|
||||||
|
DBUtil.closeDB(connection, statement, rs);
|
||||||
|
}
|
||||||
|
return hasMore;
|
||||||
|
}
|
||||||
|
|
||||||
private static List<String> getCreateSql() {
|
private static List<String> getCreateSql() {
|
||||||
List<String> out = new ArrayList<>();
|
List<String> out = new ArrayList<>();
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user