private static boolean reload_all() { // 载入新的配置 ConfigInitializer loader = new ConfigInitializer(true); Map<String, UserConfig> users = loader.getUsers(); Map<String, SchemaConfig> schemas = loader.getSchemas(); Map<String, PhysicalDBNode> dataNodes = loader.getDataNodes(); Map<String, PhysicalDBPool> dataHosts = loader.getDataHosts(); CloudbCluster cluster = loader.getCluster(); QuarantineConfig quarantine = loader.getQuarantine(); // 应用新配置 CloudbConfig conf = CloudbServer.getInstance().getConfig(); conf.setDataNodes(dataNodes); Map<String, PhysicalDBPool> cNodes = conf.getDataHosts(); boolean reloadStatus = true; for (PhysicalDBPool dn : dataHosts.values()) { dn.setSchemas( CloudbServer.getInstance().getConfig().getDataNodeSchemasOfDataHost(dn.getHostName())); // init datahost String index = DnPropertyUtil.loadDnIndexProps().getProperty(dn.getHostName(), "0"); if (!"0".equals(index)) { LOGGER.info("init datahost: " + dn.getHostName() + " to use datasource index:" + index); } dn.init(Integer.valueOf(index)); // dn.init(0); if (!dn.isInitSuccess()) { reloadStatus = false; break; } } // 如果重载不成功,则清理已初始化的资源。 if (!reloadStatus) { LOGGER.warn("reload failed ,clear previously created datasources "); for (PhysicalDBPool dn : dataHosts.values()) { dn.clearDataSources("reload config"); dn.stopHeartbeat(); } return false; } // 应用重载 conf.reload(users, schemas, dataNodes, dataHosts, cluster, quarantine, true); // 处理旧的资源 for (PhysicalDBPool dn : cNodes.values()) { dn.clearDataSources("reload config clear old datasources"); dn.stopHeartbeat(); } // 清理缓存 CloudbServer.getInstance().getCacheService().clearCache(); return true; }
private static Set<String> getTableSet(ServerConnection c, Map<String, String> parm) { TreeSet<String> tableSet = new TreeSet<String>(); CloudbConfig conf = CloudbServer.getInstance().getConfig(); Map<String, UserConfig> users = conf.getUsers(); UserConfig user = users == null ? null : users.get(c.getUser()); if (user != null) { Map<String, SchemaConfig> schemas = conf.getSchemas(); for (String name : schemas.keySet()) { if (null != parm.get(SCHEMA_KEY) && parm.get(SCHEMA_KEY).toUpperCase().equals(name.toUpperCase())) { if (null == parm.get("LIKE_KEY")) { tableSet.addAll(schemas.get(name).getTables().keySet()); } else { String p = "^" + parm.get("LIKE_KEY").replaceAll("%", ".*"); Pattern pattern = Pattern.compile(p, Pattern.CASE_INSENSITIVE); Matcher ma; for (String tname : schemas.get(name).getTables().keySet()) { ma = pattern.matcher(tname); if (ma.matches()) { tableSet.add(tname); } } } } } ; } return tableSet; }
private static boolean reload() { // 载入新的配置 ConfigInitializer loader = new ConfigInitializer(false); Map<String, UserConfig> users = loader.getUsers(); Map<String, SchemaConfig> schemas = loader.getSchemas(); Map<String, PhysicalDBNode> dataNodes = loader.getDataNodes(); Map<String, PhysicalDBPool> dataHosts = loader.getDataHosts(); CloudbCluster cluster = loader.getCluster(); QuarantineConfig quarantine = loader.getQuarantine(); // 应用新配置 CloudbServer instance = CloudbServer.getInstance(); CloudbConfig conf = instance.getConfig(); // 应用重载 conf.reload(users, schemas, dataNodes, dataHosts, cluster, quarantine, false); // 清理缓存 instance.getCacheService().clearCache(); return true; }
public static void execute(ManagerConnection c, final boolean loadAll) { final ReentrantLock lock = CloudbServer.getInstance().getConfig().getLock(); lock.lock(); try { ListenableFuture<Boolean> listenableFuture = CloudbServer.getInstance() .getListeningExecutorService() .submit( new Callable<Boolean>() { @Override public Boolean call() throws Exception { return loadAll ? reload_all() : reload(); } }); Futures.addCallback( listenableFuture, new ReloadCallBack(c), CloudbServer.getInstance().getListeningExecutorService()); } finally { lock.unlock(); } }
/** * response method. * * @param c */ public static void response(ServerConnection c, String stmt, int type) { String showSchemal = SchemaUtil.parseShowTableSchema(stmt); String cSchema = showSchemal == null ? c.getSchema() : showSchemal; SchemaConfig schema = CloudbServer.getInstance().getConfig().getSchemas().get(cSchema); if (schema != null) { // 不分库的schema,show tables从后端 mysql中查 String node = schema.getDataNode(); if (!Strings.isNullOrEmpty(node)) { c.execute(stmt, ServerParse.SHOW, null); return; } } else { c.writeErrMessage(ErrorCode.ER_NO_DB_ERROR, "No database selected"); } // 分库的schema,直接从SchemaConfig中获取所有表名 Map<String, String> parm = buildFields(c, stmt); java.util.Set<String> tableSet = getTableSet(c, parm); int i = 0; byte packetId = 0; header.packetId = ++packetId; fields[i] = PacketUtil.getField("Tables in " + parm.get(SCHEMA_KEY), Fields.FIELD_TYPE_VAR_STRING); fields[i++].packetId = ++packetId; eof.packetId = ++packetId; 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 packetId = eof.packetId; for (String name : tableSet) { RowDataPacket row = new RowDataPacket(FIELD_COUNT); row.add(StringUtil.encode(name.toLowerCase(), c.getCharset())); row.packetId = ++packetId; buffer = row.write(buffer, c, true); } // write last eof EOFPacket lastEof = new EOFPacket(); lastEof.packetId = ++packetId; buffer = lastEof.write(buffer, c, true); // post write c.write(buffer); }
/** * 从全局的schema列表中查询指定的schema是否存在, 如果存在则替换connection属性中原有的schema, 如果不存在,则throws * SQLNonTransientException,表示指定的schema 不存在 * * @param sysConfig * @param schema * @param sqlType * @param realSQL * @param charset * @param info * @param cachePool * @param hintSQLValue * @return * @throws SQLNonTransientException */ @Override public RouteResultset route( SystemConfig sysConfig, SchemaConfig schema, int sqlType, String realSQL, String charset, ServerConnection sc, LayerCachePool cachePool, String hintSQLValue) throws SQLNonTransientException { SchemaConfig tempSchema = CloudbServer.getInstance().getConfig().getSchemas().get(hintSQLValue); if (tempSchema != null) { return routeStrategy.route( sysConfig, tempSchema, sqlType, realSQL, charset, sc, cachePool, null); } else { String msg = "can't find hint schema:" + hintSQLValue; LOGGER.warn(msg); throw new SQLNonTransientException(msg); } }