[Mycat 1.6 three configuration file loading source code reading]

1. Introduction to the three major configuration files

1) rule.xml shard key rule function mapping

2) schema.xml database node host mapping

3) server.xml global parameter configuration (user/firewall/memory/port configuration)

 

Second, the three configuration file loading process

1) Startup class MycatStartup part of the code
MycatStartup {
    private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
    private static final Logger LOGGER = LoggerFactory.getLogger(MycatStartup.class);
    public static void main(String[] args) {
        //use zk ?
    	//Use the singleton mode plus static code block to load the /myid.properties configuration file
    	//If the value of loadZk in the configuration file is true, the loadZktoFile method of the ZktoXmlMain class will be called,
    	// Otherwise, don't use ZK, this code is very beautifully written by Xiao Liu's comments, it is worth learning and praise
        ZkConfig.getInstance().initZk();
        
		//Check if the SYS_HOME = "MYCAT_HOME" variable value in SystemConfig is set
		String home = SystemConfig.getHomePath();
		if (home == null) {
			System.out.println(SystemConfig.SYS_HOME + "  is not set.");
			System.exit(-1);
		}
		// init
		//Use the singleton mode to initialize the configuration file to prepare for startup: read the configuration file and enable the scheduler scheduler,
		// The loading of the configuration file uses the DocumentBuilderFactory-->DocumentBuilder of w3c in java,
		// This kind of code is relatively low-level. What is the difference between dom sax and dom4j? dom4j is not used here, it is estimated that the dtd verification problem is considered
		MycatServer server = MycatServer.getInstance();

}

2) MycatServer class 
private MycatServer() {	
	 //Read the file configuration, load the configuration file entry 
	this.config = new MycatConfig();
}

3) MycatConfig class 
public MycatConfig () {
	 //Read schema.xml, rule.xml and server.xml
	ConfigInitializer confInit = new ConfigInitializer(true);
	this.system = confInit.getSystem();
	this.users = confInit.getUsers();
	this.schemas = confInit.getSchemas();
	this.dataHosts = confInit.getDataHosts();

	this.dataNodes = confInit.getDataNodes();
	for (PhysicalDBPool dbPool : dataHosts.values()) {
		dbPool.setSchemas(getDataNodeSchemasOfDataHost(dbPool.getHostName()));
	}
	
	this.firewall = confInit.getFirewall();
	this.cluster = confInit.getCluster();
		
		 
}

4)ConfigInitializer类
public ConfigInitializer(boolean loadDataHost) {
		//读取rule.xml和schema.xml
		SchemaLoader schemaLoader = new XMLSchemaLoader();
		
		//read server.xml
		XMLConfigLoader configLoader = new XMLConfigLoader(schemaLoader);
		
		schemaLoader = null;
		
		// load configuration
		this.system = configLoader.getSystemConfig();
		this.users = configLoader.getUserConfigs();
		this.schemas = configLoader.getSchemaConfigs();
		
		//Whether to reload DataHost and corresponding DataNode
		if (loadDataHost) {
			this.dataHosts = initDataHosts(configLoader);
			this.dataNodes = initDataNodes(configLoader);
		}
		
		//authority management
		this.firewall = configLoader.getFirewallConfig();
		this.cluster = initCobarCluster(configLoader);
		
		//Configuration loading for different types of global sequence processors
		if (system.getSequnceHandlerType() == SystemConfig.SEQUENCEHANDLER_MYSQLDB) {
			IncrSequenceMySQLHandler.getInstance().load();
		}
		
		if (system.getSequnceHandlerType() == SystemConfig.SEQUENCEHANDLER_LOCAL_TIME) {
			IncrSequenceTimeHandler.getInstance().load();
		}
		
		if (system.getSequnceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_DISTRIBUTED) {
			DistributedSequenceHandler.getInstance(system).load();
		}
		
		if (system.getSequnceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_GLOBAL_INCREMENT) {
			IncrSequenceZKHandler.getInstance().load();
		}
		
		/**
		 * Configuration file initialization, self-test
		 */
		this.selfChecking0();
	}
5)XMLSchemaLoader类	
     public XMLSchemaLoader() {
	this(null, null);
      }
	
	
      public XMLSchemaLoader(String schemaFile, String ruleFile) {
		//先读取rule.xml
		XMLRuleLoader ruleLoader = new XMLRuleLoader(ruleFile);
		//Take out tableRules and use it to load Schema here for effective rule judgment and subsequent fragment routing calculation
		this.tableRules = ruleLoader.getTableRules();
		//release ruleLoader
		ruleLoader = null;
		this.dataHosts = new HashMap<String, DataHostConfig>();
		this.dataNodes = new HashMap<String, DataNodeConfig>();
		this.schemas = new HashMap<String, SchemaConfig>();
		//Read and load schema configuration
	this.load(DEFAULT_DTD, schemaFile == null ? DEFAULT_XML : schemaFile);
	}

	
	//Read the schema configuration and load [dataHosts/dataNodes/schemas]
	private void load(String dtdFile, String xmlFile) {
		InputStream dtd = null;
		InputStream xml = null;
		dtd = XMLSchemaLoader.class.getResourceAsStream(dtdFile);
		xml = XMLSchemaLoader.class.getResourceAsStream(xmlFile);
		Element root = ConfigUtil.getDocument(dtd, xml).getDocumentElement();
		//Load all DataHost first
		loadDataHosts(root);
		//Reload all DataNodes
		loadDataNodes(root);
		//Finally load all schemas
		loadSchemas(root);
	}
6)XMLSchemaLoader类
	public XMLRuleLoader(String ruleFile) {
		// this.rules = new HashSet();
		//rule名 -> rule
		this.tableRules = new HashMap<String, TableRuleConfig>();
		//function name -> specific sharding algorithm
		this.functions = new HashMap<String, AbstractPartitionAlgorithm>();
		load(DEFAULT_DTD, ruleFile == null ? DEFAULT_XML : ruleFile);
	}
	
7) XMLConfigLoader class 
	public XMLConfigLoader (SchemaLoader schemaLoader) {
             XMLServerLoader serverLoader = new XMLServerLoader(); 
            //The following four properties [system/users/firewall/cluster] come from the Server.xml file
	    this.system = serverLoader.getSystem();
            this.users = serverLoader.getUsers();
            this.firewall = serverLoader.getFirewall();
            this.cluster = serverLoader.getCluster();
	    //The following three properties [dataHosts/dataNodes/schemas] are from the schema file
             this.dataHosts = schemaLoader.getDataHosts();
            this.dataNodes = schemaLoader.getDataNodes();
            this.schemas = schemaLoader.getSchemas();
        schemaLoader = null;
    }
8)XMLServerLoader
    public XMLServerLoader() {
        this.system = new SystemConfig();
        this.users = new HashMap<String, UserConfig>();
        this.firewall = new FirewallConfig();
	//Load function 
       this.load();
    }
	
	//Read the server.xml configuration and load [system/users/cluster/firewall]
	private void load() {
               InputStream dtd = null;
               InputStream xml = null;
		dtd = XMLServerLoader.class.getResourceAsStream("/server.dtd");
		xml = XMLServerLoader.class.getResourceAsStream("/server.xml");
		Element root = ConfigUtil.getDocument(dtd, xml).getDocumentElement();
		
		//Load the System tag
		loadSystem(root);
		
		//Load User tag
		loadUsers(root);
		
		//load cluster configuration
		this.cluster = new ClusterConfig(root, system.getServerPort());
		
		//Load global SQL firewall
		loadFirewall(root);
        
	}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326461674&siteId=291194637