Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mycat connection pool model source code

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

PhysicalDBNode is the counterpart of the Mycat cluster (Datanode), referencing a connection pool object PhysicalDBPool

PhysicalDBPool refers to the real connection pooling object PhysicalDatasource,PhysicalDBPool, which sets the

The read node of the cluster, the write node writes to their own PhysicalDatasource array, and decides which node to go through load balancing.

Load balancing strategy: random selection, setting random probability according to weight

Code: randomSelect

Node weight calculation formula String weightStr = node.getAttribute ("weight")

Int weight = "" .equals (weightStr)? PhysicalDBPool.WEIGHT: Integer.parseInt (weightStr)

Load balancing: offset-= okSources.get (I) .getConfig () .getWeight ()

I don't understand why it is so distributed. Is it possible that the greater the available weight is, the less likely the distribution is?

Public PhysicalDatasource randomSelect (ArrayList okSources) {if (okSources.isEmpty ()) {return this.getSource ();} else {int length = okSources.size (); / / Total weight int totalWeight = 0; / / Total weight boolean sameWeight = true; / / whether the weights are all the same for (int I = 0; I

< length; i++) { int weight = okSources.get(i).getConfig().getWeight(); totalWeight += weight; // 累计总权重 if (sameWeight && i >

0 & & weight! = okSources.get (iMur1) .getConfig () .getWeight () {/ / calculate whether the weights are the same sameWeight = false }} if (totalWeight > 0 & &! sameWeight) {/ / if the weights are different and the weights are greater than 0, then random int offset = random.nextInt (totalWeight) according to the total weights; / / and determine which segment the random value falls on (for (int I = 0; I < length) Offset +) {offset-= okSources.get (I) .getConfig () .getWeight (); if (offset < 0) {return okSources.get (I);} / / if the weight is the same or zero, then equal random return okSources.get (random.nextInt (length)) / int index = Math.abs (random.nextInt ())% okSources.size (); / / return okSources.get (index);}}

The data structure used by the PhysicalDatasource connection pool object to save the available connections for this connection is ConMap. The main function is to get the available connections of the current node. First, get the available connections from the current database. If not, get the available connections from other database.

Public BackendConnection tryTakeCon (final String schema, boolean autoCommit) {final ConQueue queue = items.get (schema); BackendConnection con = tryTakeCon (queue, autoCommit); if (con! = null) {return con;} else {for (ConQueue queue2: items.values ()) {if (queue! = queue2) {con = tryTakeCon (queue2, autoCommit); if (con! = null) {return con }} return null;} private BackendConnection tryTakeCon (ConQueue queue, boolean autoCommit) {BackendConnection con = null; if (queue! = null & & ((con = queue.takeIdleCon (autoCommit))! = null)) {return con;} else {return null;}

The available connections of database are stored in the data structure ConQueue, and the available connections are divided into automatic submission and manual submission, so ConQueue consists of two ConcurrentLinkedQueue, autoCommitCons automatic submission queue and manCommitCons manual submission queue.

Allocation of available connections: first from the submission mode queue first allocation, allocation failure, allocation from another queue, allocation failure, allocation from other databse. Guess: allocation here should not be the most available connection, but also need to do transaction isolation level, transaction mode, character set, Database and other processing and verification to execute specific sql instructions, which should be done in the MySQLConnection class.

Public BackendConnection takeIdleCon (boolean autoCommit) {ConcurrentLinkedQueue f1 = autoCommitCons; ConcurrentLinkedQueue f2 = manCommitCons; if (! autoCommit) {F1 = manCommitCons; f2 = autoCommitCons;} BackendConnection con = f1.poll (); if (con = = null | | con.isClosedOrQuit ()) {con = f2.poll ();} if (con = = null | con.isClosedOrQuit ()) {return null;} else {return con;}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report