use of com.actiontech.dble.config.ServerConfig in project dble by actiontech.
the class ReloadConfig method reloadAll.
public static void reloadAll(final int loadAllMode) throws Exception {
/*
* 1 load new conf
* 1.1 ConfigInitializer init adn check itself
* 1.2 DataNode/DataHost test connection
*/
ConfigInitializer loader;
try {
loader = new ConfigInitializer(true, DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames());
} catch (Exception e) {
throw new Exception(e);
}
Map<String, UserConfig> newUsers = loader.getUsers();
Map<String, SchemaConfig> newSchemas = loader.getSchemas();
Map<String, PhysicalDBNode> newDataNodes = loader.getDataNodes();
Map<String, PhysicalDBPool> newDataHosts = loader.getDataHosts();
Map<ERTable, Set<ERTable>> newErRelations = loader.getErRelations();
FirewallConfig newFirewall = loader.getFirewall();
SystemVariables newSystemVariables = DbleServer.getInstance().getSystemVariables();
if (!loader.isDataHostWithoutWH()) {
VarsExtractorHandler handler = new VarsExtractorHandler(newDataNodes);
newSystemVariables = handler.execute();
ConfigInitializer confInit = new ConfigInitializer(newSystemVariables.isLowerCaseTableNames());
newUsers = confInit.getUsers();
newSchemas = confInit.getSchemas();
newDataNodes = confInit.getDataNodes();
newErRelations = confInit.getErRelations();
newFirewall = confInit.getFirewall();
newDataHosts = confInit.getDataHosts();
}
if ((loadAllMode & ManagerParseConfig.OPTT_MODE) != 0) {
try {
loader.testConnection(false);
} catch (Exception e) {
throw new Exception(e);
}
}
/*
* 2 transform
* 2.1 old dataSource continue to work
* 2.2 init the new dataSource
* 2.3 transform
* 2.4 put the old connection into a queue
*/
ServerConfig config = DbleServer.getInstance().getConfig();
/* 2.1 do nothing */
boolean isReloadStatusOK = true;
/* 2.2 init the new dataSource */
for (PhysicalDBPool dbPool : newDataHosts.values()) {
String hostName = dbPool.getHostName();
// set schemas
ArrayList<String> dnSchemas = new ArrayList<>(30);
for (PhysicalDBNode dn : newDataNodes.values()) {
if (dn.getDbPool().getHostName().equals(hostName)) {
dnSchemas.add(dn.getDatabase());
}
}
dbPool.setSchemas(dnSchemas.toArray(new String[dnSchemas.size()]));
// get data host
String dnIndex = DnPropertyUtil.loadDnIndexProps().getProperty(dbPool.getHostName(), "0");
if (!"0".equals(dnIndex)) {
LOGGER.info("init data host: " + dbPool.getHostName() + " to use datasource index:" + dnIndex);
}
dbPool.init(Integer.parseInt(dnIndex));
if (!dbPool.isInitSuccess()) {
isReloadStatusOK = false;
break;
}
}
if (isReloadStatusOK) {
/* 2.3 apply new conf */
config.reload(newUsers, newSchemas, newDataNodes, newDataHosts, newErRelations, newFirewall, newSystemVariables, loader.isDataHostWithoutWH(), true);
recycleOldBackendConnections(config, ((loadAllMode & ManagerParseConfig.OPTF_MODE) != 0));
AlarmAppender.refreshConfig();
} else {
// INIT FAILED
LOGGER.info("reload failed, clear previously created data sources ");
for (PhysicalDBPool dbPool : newDataHosts.values()) {
dbPool.clearDataSources("reload config");
dbPool.stopHeartbeat();
}
throw new Exception("Init DbPool failed");
}
}
use of com.actiontech.dble.config.ServerConfig in project dble by actiontech.
the class ShowDataNode method execute.
public static void execute(ManagerConnection c, String name) {
ByteBuffer buffer = c.allocate();
// write header
buffer = HEADER.write(buffer, c, true);
// write fields
for (FieldPacket field : FIELDS) {
buffer = field.write(buffer, c, true);
}
// write eof
buffer = EOF.write(buffer, c, true);
// write rows
byte packetId = EOF.getPacketId();
ServerConfig conf = DbleServer.getInstance().getConfig();
Map<String, PhysicalDBNode> dataNodes = conf.getDataNodes();
List<String> keys = new ArrayList<>();
if (StringUtil.isEmpty(name)) {
keys.addAll(dataNodes.keySet());
} else {
SchemaConfig sc = conf.getSchemas().get(name);
if (null != sc) {
keys.addAll(sc.getAllDataNodes());
}
}
Collections.sort(keys, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
Pair<String, Integer> p1 = PairUtil.splitIndex(o1, '[', ']');
Pair<String, Integer> p2 = PairUtil.splitIndex(o2, '[', ']');
if (p1.getKey().compareTo(p2.getKey()) == 0) {
return p1.getValue() - p2.getValue();
} else {
return p1.getKey().compareTo(p2.getKey());
}
}
});
for (String key : keys) {
RowDataPacket row = getRow(dataNodes.get(key), c.getCharset().getResults());
if (row != null) {
row.setPacketId(++packetId);
buffer = row.write(buffer, c, true);
}
}
// write last eof
EOFPacket lastEof = new EOFPacket();
lastEof.setPacketId(++packetId);
buffer = lastEof.write(buffer, c, true);
// post write
c.write(buffer);
}
use of com.actiontech.dble.config.ServerConfig in project dble by actiontech.
the class ShowDatasourceSyn method getRows.
private static List<RowDataPacket> getRows(String charset) {
List<RowDataPacket> list = new LinkedList<>();
ServerConfig conf = DbleServer.getInstance().getConfig();
// host nodes
Map<String, PhysicalDBPool> dataHosts = conf.getDataHosts();
for (PhysicalDBPool pool : dataHosts.values()) {
for (PhysicalDatasource ds : pool.getAllDataSources()) {
DBHeartbeat hb = ds.getHeartbeat();
DataSourceSyncRecorder record = hb.getAsyncRecorder();
Map<String, String> states = record.getRecords();
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
if (!states.isEmpty()) {
row.add(StringUtil.encode(ds.getName(), charset));
row.add(StringUtil.encode(ds.getConfig().getIp(), charset));
row.add(LongUtil.toBytes(ds.getConfig().getPort()));
row.add(StringUtil.encode(states.get("Master_Host"), charset));
row.add(LongUtil.toBytes(Long.parseLong(states.get("Master_Port"))));
row.add(StringUtil.encode(states.get("Master_User"), charset));
String seconds = states.get("Seconds_Behind_Master");
row.add(seconds == null ? null : LongUtil.toBytes(Long.parseLong(seconds)));
row.add(StringUtil.encode(states.get("Slave_IO_Running"), charset));
row.add(StringUtil.encode(states.get("Slave_SQL_Running"), charset));
row.add(StringUtil.encode(states.get("Slave_IO_State"), charset));
row.add(LongUtil.toBytes(Long.parseLong(states.get("Connect_Retry"))));
row.add(StringUtil.encode(states.get("Last_IO_Error"), charset));
list.add(row);
}
}
}
return list;
}
use of com.actiontech.dble.config.ServerConfig in project dble by actiontech.
the class ShowHeartbeat method getRows.
private static List<RowDataPacket> getRows() {
List<RowDataPacket> list = new LinkedList<>();
ServerConfig conf = DbleServer.getInstance().getConfig();
// host nodes
Map<String, PhysicalDBPool> dataHosts = conf.getDataHosts();
for (PhysicalDBPool pool : dataHosts.values()) {
for (PhysicalDatasource ds : pool.getAllDataSources()) {
DBHeartbeat hb = ds.getHeartbeat();
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(ds.getName().getBytes());
if (hb != null) {
row.add(ds.getConfig().getIp().getBytes());
row.add(IntegerUtil.toBytes(ds.getConfig().getPort()));
row.add(IntegerUtil.toBytes(hb.getStatus()));
row.add(IntegerUtil.toBytes(hb.getErrorCount()));
row.add(hb.isChecking() ? "checking".getBytes() : "idle".getBytes());
row.add(LongUtil.toBytes(hb.getTimeout()));
row.add(hb.getRecorder().get().getBytes());
String lat = hb.getLastActiveTime();
row.add(lat == null ? null : lat.getBytes());
row.add(hb.isStop() ? "true".getBytes() : "false".getBytes());
} else {
row.add(null);
row.add(null);
row.add(null);
row.add(null);
row.add(null);
row.add(null);
row.add(null);
row.add(null);
}
list.add(row);
}
}
return list;
}
use of com.actiontech.dble.config.ServerConfig in project dble by actiontech.
the class ShowTables method response.
public static void response(ServerConnection c, String stmt) {
ShowCreateStmtInfo info;
try {
info = new ShowCreateStmtInfo(stmt);
} catch (Exception e) {
c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, e.toString());
return;
}
String showSchema = info.getSchema();
if (showSchema != null && DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
showSchema = showSchema.toLowerCase();
}
String cSchema = showSchema == null ? c.getSchema() : showSchema;
if (cSchema == null) {
c.writeErrMessage("3D000", "No database selected", ErrorCode.ER_NO_DB_ERROR);
return;
}
SchemaConfig schema = DbleServer.getInstance().getConfig().getSchemas().get(cSchema);
if (schema == null) {
c.writeErrMessage("42000", "Unknown database '" + cSchema + "'", ErrorCode.ER_BAD_DB_ERROR);
return;
}
ServerConfig conf = DbleServer.getInstance().getConfig();
UserConfig user = conf.getUsers().get(c.getUser());
if (user == null || !user.getSchemas().contains(cSchema)) {
c.writeErrMessage("42000", "Access denied for user '" + c.getUser() + "' to database '" + cSchema + "'", ErrorCode.ER_DBACCESS_DENIED_ERROR);
return;
}
// if schema has default node ,show tables will send to backend
String node = schema.getDataNode();
if (!Strings.isNullOrEmpty(node)) {
try {
parserAndExecuteShowTables(c, stmt, node, info);
} catch (Exception e) {
c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, e.toString());
}
} else {
responseDirect(c, cSchema, info);
}
}
Aggregations