use of com.alibaba.cobar.config.model.UserConfig in project cobar by alibaba.
the class CobarPrivileges method getUserSchemas.
@Override
public Set<String> getUserSchemas(String user) {
CobarConfig conf = CobarServer.getInstance().getConfig();
UserConfig uc = conf.getUsers().get(user);
if (uc != null) {
return uc.getSchemas();
} else {
return null;
}
}
use of com.alibaba.cobar.config.model.UserConfig in project cobar by alibaba.
the class ReloadConfig method reload.
private static boolean reload() {
// 载入新的配置
ConfigInitializer loader = new ConfigInitializer();
Map<String, UserConfig> users = loader.getUsers();
Map<String, SchemaConfig> schemas = loader.getSchemas();
Map<String, MySQLDataNode> dataNodes = loader.getDataNodes();
Map<String, DataSourceConfig> dataSources = loader.getDataSources();
CobarCluster cluster = loader.getCluster();
QuarantineConfig quarantine = loader.getQuarantine();
// 应用新配置
CobarConfig conf = CobarServer.getInstance().getConfig();
// 如果重载已经存在的数据节点,初始化连接数参考空闲连接数,否则为1。
boolean reloadStatus = true;
Map<String, MySQLDataNode> cNodes = conf.getDataNodes();
for (MySQLDataNode dn : dataNodes.values()) {
MySQLDataNode cdn = cNodes.get(dn.getName());
if (cdn != null && cdn.getSource() != null) {
int size = Math.min(cdn.getSource().getIdleCount(), dn.getConfig().getPoolSize());
dn.init(size > 0 ? size : 1, 0);
} else {
dn.init(1, 0);
}
if (!dn.isInitSuccess()) {
reloadStatus = false;
break;
}
}
// 如果重载不成功,则清理已初始化的资源。
if (!reloadStatus) {
for (MySQLDataNode dn : dataNodes.values()) {
MySQLDataSource ds = dn.getSource();
if (ds != null) {
ds.clear();
}
}
return false;
}
// 应用重载
conf.reload(users, schemas, dataNodes, dataSources, cluster, quarantine);
// 处理旧的资源
for (MySQLDataNode dn : cNodes.values()) {
MySQLDataSource ds = dn.getSource();
if (ds != null) {
ds.clear();
}
}
return true;
}
use of com.alibaba.cobar.config.model.UserConfig in project cobar by alibaba.
the class RollbackConfig method rollback.
private static boolean rollback() {
CobarConfig conf = CobarServer.getInstance().getConfig();
Map<String, UserConfig> users = conf.getBackupUsers();
Map<String, SchemaConfig> schemas = conf.getBackupSchemas();
Map<String, MySQLDataNode> dataNodes = conf.getBackupDataNodes();
Map<String, DataSourceConfig> dataSources = conf.getBackupDataSources();
CobarCluster cluster = conf.getBackupCluster();
QuarantineConfig quarantine = conf.getBackupQuarantine();
// 检查可回滚状态
if (!conf.canRollback()) {
return false;
}
// 如果回滚已经存在的pool,初始化连接数参考空闲连接数,否则为1。
boolean rollbackStatus = true;
Map<String, MySQLDataNode> cNodes = conf.getDataNodes();
for (MySQLDataNode dn : dataNodes.values()) {
MySQLDataNode cdn = cNodes.get(dn.getName());
if (cdn != null && cdn.getSource() != null) {
int size = Math.min(cdn.getSource().getIdleCount(), dn.getConfig().getPoolSize());
dn.init(size > 0 ? size : 1, dn.getActivedIndex());
} else {
dn.init(1, dn.getActivedIndex());
}
if (!dn.isInitSuccess()) {
rollbackStatus = false;
break;
}
}
// 如果回滚不成功,则清理已初始化的资源。
if (!rollbackStatus) {
for (MySQLDataNode dn : dataNodes.values()) {
MySQLDataSource ds = dn.getSource();
if (ds != null) {
ds.clear();
}
}
return false;
}
// 应用回滚
conf.rollback(users, schemas, dataNodes, dataSources, cluster, quarantine);
// 处理旧的资源
for (MySQLDataNode dn : cNodes.values()) {
MySQLDataSource ds = dn.getSource();
if (ds != null) {
ds.clear();
}
}
return true;
}
use of com.alibaba.cobar.config.model.UserConfig in project cobar by alibaba.
the class XMLServerLoader method loadUsers.
private void loadUsers(Element root) {
NodeList list = root.getElementsByTagName("user");
for (int i = 0, n = list.getLength(); i < n; i++) {
Node node = list.item(i);
if (node instanceof Element) {
Element e = (Element) node;
String name = e.getAttribute("name");
UserConfig user = new UserConfig();
user.setName(name);
Map<String, Object> props = ConfigUtil.loadElements(e);
user.setPassword((String) props.get("password"));
String schemas = (String) props.get("schemas");
if (schemas != null) {
String[] strArray = SplitUtil.split(schemas, ',', true);
user.setSchemas(new HashSet<String>(Arrays.asList(strArray)));
}
if (users.containsKey(name)) {
throw new ConfigException("user " + name + " duplicated!");
}
users.put(name, user);
}
}
}
use of com.alibaba.cobar.config.model.UserConfig in project cobar by alibaba.
the class XMLServerLoader method loadQuarantine.
private void loadQuarantine(Element root) {
NodeList list = root.getElementsByTagName("host");
for (int i = 0, n = list.getLength(); i < n; i++) {
Node node = list.item(i);
if (node instanceof Element) {
Element e = (Element) node;
String host = e.getAttribute("name").trim();
if (quarantine.getHosts().containsKey(host)) {
throw new ConfigException("host duplicated : " + host);
}
Map<String, Object> props = ConfigUtil.loadElements(e);
String[] users = SplitUtil.split((String) props.get("user"), ',', true);
HashSet<String> set = new HashSet<String>();
if (null != users) {
for (String user : users) {
UserConfig uc = this.users.get(user);
if (null == uc) {
throw new ConfigException("[user: " + user + "] doesn't exist in [host: " + host + "]");
}
if (null == uc.getSchemas() || uc.getSchemas().size() == 0) {
throw new ConfigException("[host: " + host + "] contains one root privileges user: " + user);
}
if (set.contains(user)) {
throw new ConfigException("[host: " + host + "] contains duplicate user: " + user);
} else {
set.add(user);
}
}
}
quarantine.getHosts().put(host, set);
}
}
}
Aggregations