3030import java .io .IOException ;
3131import java .lang .reflect .Type ;
3232import java .util .*;
33+ import java .util .concurrent .atomic .AtomicLong ;
3334import java .util .concurrent .locks .ReentrantReadWriteLock ;
3435
3536public class PhysicalDbGroup {
@@ -42,11 +43,15 @@ public class PhysicalDbGroup {
4243 public static final int RW_SPLIT_ALL = 2 ;
4344 // weight
4445 public static final int WEIGHT = 0 ;
45- private final List <PhysicalDbInstance > writeInstanceList ;
46+
47+ enum USAGE {
48+ NONE , RW , SHARDING ;
49+ }
4650
4751 private final String groupName ;
4852 private final DbGroupConfig dbGroupConfig ;
4953 private volatile PhysicalDbInstance writeDbInstance ;
54+ private final List <PhysicalDbInstance > writeInstanceList ;
5055 private Map <String , PhysicalDbInstance > allSourceMap = new HashMap <>();
5156
5257 private final int rwSplitMode ;
@@ -55,8 +60,10 @@ public class PhysicalDbGroup {
5560 private final LocalReadLoadBalancer localReadLoadBalancer = new LocalReadLoadBalancer ();
5661 private final ReentrantReadWriteLock adjustLock = new ReentrantReadWriteLock ();
5762
58- private boolean shardingUseless = true ;
59- private boolean rwSplitUseless = true ;
63+ //delayDetection
64+ private AtomicLong logicTimestamp = new AtomicLong ();
65+
66+ private USAGE usedFor = USAGE .NONE ;
6067
6168 public PhysicalDbGroup (String name , DbGroupConfig config , PhysicalDbInstance writeDbInstances , PhysicalDbInstance [] readDbInstances , int rwSplitMode ) {
6269 this .groupName = name ;
@@ -66,8 +73,8 @@ public PhysicalDbGroup(String name, DbGroupConfig config, PhysicalDbInstance wri
6673 writeDbInstances .setDbGroup (this );
6774 this .writeDbInstance = writeDbInstances ;
6875 this .writeInstanceList = Collections .singletonList (writeDbInstance );
69- allSourceMap .put (writeDbInstances .getName (), writeDbInstances );
7076
77+ allSourceMap .put (writeDbInstances .getName (), writeDbInstances );
7178 for (PhysicalDbInstance readDbInstance : readDbInstances ) {
7279 readDbInstance .setDbGroup (this );
7380 allSourceMap .put (readDbInstance .getName (), readDbInstance );
@@ -89,6 +96,54 @@ public PhysicalDbGroup(PhysicalDbGroup org) {
8996 writeInstanceList = Collections .singletonList (writeDbInstance );
9097 }
9198
99+ public void init (String reason ) {
100+ for (Map .Entry <String , PhysicalDbInstance > entry : allSourceMap .entrySet ()) {
101+ entry .getValue ().init (reason );
102+ }
103+ }
104+
105+ // only fresh backend connection pool
106+ public void init (List <String > sourceNames , String reason ) {
107+ for (String sourceName : sourceNames ) {
108+ if (allSourceMap .containsKey (sourceName )) {
109+ allSourceMap .get (sourceName ).init (reason , false );
110+ }
111+ }
112+ }
113+
114+ public void stop (String reason ) {
115+ stop (reason , false );
116+ }
117+
118+ public void stop (String reason , boolean closeFront ) {
119+ for (PhysicalDbInstance dbInstance : allSourceMap .values ()) {
120+ dbInstance .stop (reason , closeFront );
121+ }
122+ }
123+
124+ // only fresh backend connection pool
125+ public void stop (List <String > sourceNames , String reason , boolean closeFront ) {
126+ for (String sourceName : sourceNames ) {
127+ if (allSourceMap .containsKey (sourceName )) {
128+ allSourceMap .get (sourceName ).stop (reason , closeFront , false );
129+ }
130+ }
131+
132+ if (closeFront ) {
133+ Iterator <PooledConnection > iterator = IOProcessor .BACKENDS_OLD .iterator ();
134+ while (iterator .hasNext ()) {
135+ PooledConnection con = iterator .next ();
136+ if (con instanceof BackendConnection ) {
137+ BackendConnection backendCon = (BackendConnection ) con ;
138+ if (backendCon .getPoolDestroyedTime () != 0 && sourceNames .contains (backendCon .getInstance ().getConfig ().getInstanceName ())) {
139+ backendCon .closeWithFront ("old active backend conn will be forced closed by closing front conn" );
140+ iterator .remove ();
141+ }
142+ }
143+ }
144+ }
145+ }
146+
92147 public String getGroupName () {
93148 return groupName ;
94149 }
@@ -125,88 +180,46 @@ PhysicalDbInstance findDbInstance(BackendConnection exitsCon) {
125180 }
126181
127182 boolean isSlave (PhysicalDbInstance ds ) {
128- return !( writeDbInstance == ds ) ;
183+ return writeDbInstance != ds ;
129184 }
130185
131186 public int getRwSplitMode () {
132187 return rwSplitMode ;
133188 }
134189
135190 public boolean isUseless () {
136- return shardingUseless && rwSplitUseless ;
191+ return usedFor == USAGE .NONE ;
192+ }
193+
194+ public boolean usedForSharding () {
195+ return usedFor == USAGE .SHARDING ;
137196 }
138197
139- public boolean isShardingUseless () {
140- return shardingUseless ;
198+ public boolean usedForRW () {
199+ return usedFor == USAGE . RW ;
141200 }
142201
143- public boolean isRwSplitUseless () {
144- return rwSplitUseless ;
202+ public void setUsedForSharding () {
203+ usedFor = USAGE . SHARDING ;
145204 }
146205
147- public void setShardingUseless ( boolean shardingUseless ) {
148- this . shardingUseless = shardingUseless ;
206+ public void setUsedForRW ( ) {
207+ usedFor = USAGE . RW ;
149208 }
150209
151- public void setRwSplitUseless ( boolean rwSplitUseless ) {
152- this . rwSplitUseless = rwSplitUseless ;
210+ public USAGE getUsedFor ( ) {
211+ return usedFor ;
153212 }
154213
155214 private boolean checkSlaveSynStatus () {
156- return (dbGroupConfig .getDelayThreshold () != -1 ) &&
157- ( dbGroupConfig .isShowSlaveSql () );
215+ return (( dbGroupConfig .getDelayThreshold () != -1 ) && dbGroupConfig . isShowSlaveSql ()) ||
216+ dbGroupConfig .isDelayDetection ( );
158217 }
159218
160219 public PhysicalDbInstance getWriteDbInstance () {
161220 return writeDbInstance ;
162221 }
163222
164- public void init (String reason ) {
165- for (Map .Entry <String , PhysicalDbInstance > entry : allSourceMap .entrySet ()) {
166- entry .getValue ().init (reason );
167- }
168- }
169-
170- public void init (List <String > sourceNames , String reason ) {
171- for (String sourceName : sourceNames ) {
172- if (allSourceMap .containsKey (sourceName )) {
173- allSourceMap .get (sourceName ).init (reason , false );
174- }
175- }
176- }
177-
178- public void stop (String reason ) {
179- stop (reason , false );
180- }
181-
182- public void stop (String reason , boolean closeFront ) {
183- for (PhysicalDbInstance dbInstance : allSourceMap .values ()) {
184- dbInstance .stop (reason , closeFront );
185- }
186- }
187-
188- public void stop (List <String > sourceNames , String reason , boolean closeFront ) {
189- for (String sourceName : sourceNames ) {
190- if (allSourceMap .containsKey (sourceName )) {
191- allSourceMap .get (sourceName ).stop (reason , closeFront , false );
192- }
193- }
194-
195- if (closeFront ) {
196- Iterator <PooledConnection > iterator = IOProcessor .BACKENDS_OLD .iterator ();
197- while (iterator .hasNext ()) {
198- PooledConnection con = iterator .next ();
199- if (con instanceof BackendConnection ) {
200- BackendConnection backendCon = (BackendConnection ) con ;
201- if (backendCon .getPoolDestroyedTime () != 0 && sourceNames .contains (backendCon .getInstance ().getConfig ().getInstanceName ())) {
202- backendCon .closeWithFront ("old active backend conn will be forced closed by closing front conn" );
203- iterator .remove ();
204- }
205- }
206- }
207- }
208- }
209-
210223 public Collection <PhysicalDbInstance > getDbInstances (boolean isAll ) {
211224 if (!isAll && rwSplitMode == RW_SPLIT_OFF ) {
212225 return writeInstanceList ;
@@ -230,18 +243,6 @@ public PhysicalDbInstance[] getReadDbInstances() {
230243 return readSources ;
231244 }
232245
233- /**
234- * rwsplit user
235- *
236- * @param master
237- * @param writeStatistical
238- * @return
239- * @throws IOException
240- */
241- public PhysicalDbInstance rwSelect (Boolean master , Boolean writeStatistical ) throws IOException {
242- return rwSelect (master , writeStatistical , false );
243- }
244-
245246 /**
246247 * rwsplit user
247248 *
@@ -546,6 +547,14 @@ public boolean checkInstanceExist(String instanceName) {
546547 return true ;
547548 }
548549
550+ public AtomicLong getLogicTimestamp () {
551+ return logicTimestamp ;
552+ }
553+
554+ public void setLogicTimestamp (AtomicLong logicTimestamp ) {
555+ this .logicTimestamp = logicTimestamp ;
556+ }
557+
549558 private void reportHeartbeatError (PhysicalDbInstance ins ) throws IOException {
550559 final DbInstanceConfig config = ins .getConfig ();
551560 String heartbeatError = "the dbInstance[" + config .getUrl () + "] can't reach. Please check the dbInstance status" ;
@@ -565,7 +574,9 @@ public boolean equalsBaseInfo(PhysicalDbGroup pool) {
565574 pool .getDbGroupConfig ().getErrorRetryCount () == this .dbGroupConfig .getErrorRetryCount () &&
566575 pool .getDbGroupConfig ().getRwSplitMode () == this .dbGroupConfig .getRwSplitMode () &&
567576 pool .getDbGroupConfig ().getDelayThreshold () == this .dbGroupConfig .getDelayThreshold () &&
577+ pool .getDbGroupConfig ().getDelayPeriodMillis () == this .dbGroupConfig .getDelayPeriodMillis () &&
578+ pool .getDbGroupConfig ().getDelayDatabase ().equals (this .dbGroupConfig .getDelayDatabase ()) &&
568579 pool .getDbGroupConfig ().isDisableHA () == this .dbGroupConfig .isDisableHA () &&
569- pool .getGroupName ().equals (this .groupName ) && pool .isShardingUseless () == this .isShardingUseless () && pool . isRwSplitUseless () == this . isRwSplitUseless ();
580+ pool .getGroupName ().equals (this .groupName ) && pool .getUsedFor () == this .getUsedFor ();
570581 }
571582}
0 commit comments