@ -16,6 +16,7 @@
* /
* /
package org.apache.dolphinscheduler.api.utils ;
package org.apache.dolphinscheduler.api.utils ;
import org.apache.dolphinscheduler.common.Constants ;
import org.apache.dolphinscheduler.common.utils.StringUtils ;
import org.apache.dolphinscheduler.common.utils.StringUtils ;
import org.slf4j.Logger ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.slf4j.LoggerFactory ;
@ -43,6 +44,8 @@ public class ZooKeeperState {
private int watches = - 1 ;
private int watches = - 1 ;
private int connections = - 1 ;
private int connections = - 1 ;
private boolean healthFlag = false ;
public ZooKeeperState ( String connectionString ) {
public ZooKeeperState ( String connectionString ) {
String host = connectionString . substring ( 0 ,
String host = connectionString . substring ( 0 ,
connectionString . indexOf ( ':' ) ) ;
connectionString . indexOf ( ':' ) ) ;
@ -55,55 +58,68 @@ public class ZooKeeperState {
public void getZookeeperInfo ( ) {
public void getZookeeperInfo ( ) {
String content = cmd ( "srvr" ) ;
String content = cmd ( "srvr" ) ;
if ( StringUtils . isNotBlank ( content ) ) {
if ( StringUtils . isNotBlank ( content ) ) {
try ( Scanner scannerForStat = new Scanner ( content ) ) {
if ( Constants . STRING_FALSE . equals ( content ) ) {
while ( scannerForStat . hasNext ( ) ) {
healthFlag = true ;
String line = scannerForStat . nextLine ( ) ;
} else {
if ( line . startsWith ( "Latency min/avg/max:" ) ) {
try ( Scanner scannerForStat = new Scanner ( content ) ) {
String [ ] latencys = getStringValueFromLine ( line ) . split ( "/" ) ;
while ( scannerForStat . hasNext ( ) ) {
minLatency = Float . parseFloat ( latencys [ 0 ] ) ;
String line = scannerForStat . nextLine ( ) ;
avgLatency = Float . parseFloat ( latencys [ 1 ] ) ;
if ( line . startsWith ( "Latency min/avg/max:" ) ) {
maxLatency = Float . parseFloat ( latencys [ 2 ] ) ;
String [ ] latencys = getStringValueFromLine ( line ) . split ( "/" ) ;
} else if ( line . startsWith ( "Received:" ) ) {
minLatency = Float . parseFloat ( latencys [ 0 ] ) ;
received = Long . parseLong ( getStringValueFromLine ( line ) ) ;
avgLatency = Float . parseFloat ( latencys [ 1 ] ) ;
} else if ( line . startsWith ( "Sent:" ) ) {
maxLatency = Float . parseFloat ( latencys [ 2 ] ) ;
sent = Long . parseLong ( getStringValueFromLine ( line ) ) ;
} else if ( line . startsWith ( "Received:" ) ) {
} else if ( line . startsWith ( "Outstanding:" ) ) {
received = Long . parseLong ( getStringValueFromLine ( line ) ) ;
outStanding = Integer . parseInt ( getStringValueFromLine ( line ) ) ;
} else if ( line . startsWith ( "Sent:" ) ) {
} else if ( line . startsWith ( "Zxid:" ) ) {
sent = Long . parseLong ( getStringValueFromLine ( line ) ) ;
zxid = Long . parseLong ( getStringValueFromLine ( line ) . substring ( 2 ) , 16 ) ;
} else if ( line . startsWith ( "Outstanding:" ) ) {
} else if ( line . startsWith ( "Mode:" ) ) {
outStanding = Integer . parseInt ( getStringValueFromLine ( line ) ) ;
mode = getStringValueFromLine ( line ) ;
} else if ( line . startsWith ( "Zxid:" ) ) {
} else if ( line . startsWith ( "Node count:" ) ) {
zxid = Long . parseLong ( getStringValueFromLine ( line ) . substring ( 2 ) , 16 ) ;
nodeCount = Integer . parseInt ( getStringValueFromLine ( line ) ) ;
} else if ( line . startsWith ( "Mode:" ) ) {
mode = getStringValueFromLine ( line ) ;
} else if ( line . startsWith ( "Node count:" ) ) {
nodeCount = Integer . parseInt ( getStringValueFromLine ( line ) ) ;
}
}
}
}
}
}
}
}
}
String wchsText = cmd ( "wchs" ) ;
String wchsText = cmd ( "wchs" ) ;
if ( StringUtils . isNotBlank ( wchsText ) ) {
if ( StringUtils . isNotBlank ( wchsText ) ) {
try ( Scanner scannerForWchs = new Scanner ( wchsText ) ) {
if ( Constants . STRING_FALSE . equals ( wchsText ) ) {
while ( scannerForWchs . hasNext ( ) ) {
healthFlag = true ;
String line = scannerForWchs . nextLine ( ) ;
} else {
if ( line . startsWith ( "Total watches:" ) ) {
try ( Scanner scannerForWchs = new Scanner ( wchsText ) ) {
watches = Integer . parseInt ( getStringValueFromLine ( line ) ) ;
while ( scannerForWchs . hasNext ( ) ) {
String line = scannerForWchs . nextLine ( ) ;
if ( line . startsWith ( "Total watches:" ) ) {
watches = Integer . parseInt ( getStringValueFromLine ( line ) ) ;
}
}
}
}
}
}
}
}
}
String consText = cmd ( "cons" ) ;
String consText = cmd ( "cons" ) ;
if ( StringUtils . isNotBlank ( consText ) ) {
if ( StringUtils . isNotBlank ( consText ) ) {
Scanner scannerForCons = new Scanner ( consText ) ;
if ( Constants . STRING_FALSE . equals ( consText ) ) {
if ( StringUtils . isNotBlank ( consText ) ) {
healthFlag = true ;
connections = 0 ;
} else {
}
Scanner scannerForCons = new Scanner ( consText ) ;
while ( scannerForCons . hasNext ( ) ) {
if ( StringUtils . isNotBlank ( consText ) ) {
@SuppressWarnings ( "unused" )
connections = 0 ;
String line = scannerForCons . nextLine ( ) ;
}
+ + connections ;
while ( scannerForCons . hasNext ( ) ) {
@SuppressWarnings ( "unused" )
String line = scannerForCons . nextLine ( ) ;
+ + connections ;
}
scannerForCons . close ( ) ;
}
}
scannerForCons . close ( ) ;
}
}
}
}
@ -121,7 +137,7 @@ public class ZooKeeperState {
private class SendThread extends Thread {
private class SendThread extends Thread {
private String cmd ;
private String cmd ;
private String ret = "" ;
private String ret = Constants . STRING_FALSE ;
public SendThread ( String cmd ) {
public SendThread ( String cmd ) {
this . cmd = cmd ;
this . cmd = cmd ;
@ -150,7 +166,7 @@ public class ZooKeeperState {
} catch ( InterruptedException e ) {
} catch ( InterruptedException e ) {
logger . error ( "send " + cmd + " to server " + host + ":" + port + " failed!" , e ) ;
logger . error ( "send " + cmd + " to server " + host + ":" + port + " failed!" , e ) ;
}
}
return "" ;
return Constants . STRING_FALSE ;
}
}
public Logger getLogger ( ) {
public Logger getLogger ( ) {
@ -209,6 +225,10 @@ public class ZooKeeperState {
return connections ;
return connections ;
}
}
public boolean isHealthFlag ( ) {
return healthFlag ;
}
@Override
@Override
public String toString ( ) {
public String toString ( ) {
return "ZooKeeperState [host=" + host + ", port=" + port
return "ZooKeeperState [host=" + host + ", port=" + port
@ -217,7 +237,7 @@ public class ZooKeeperState {
+ ", sent=" + sent + ", outStanding=" + outStanding + ", zxid="
+ ", sent=" + sent + ", outStanding=" + outStanding + ", zxid="
+ zxid + ", mode=" + mode + ", nodeCount=" + nodeCount
+ zxid + ", mode=" + mode + ", nodeCount=" + nodeCount
+ ", watches=" + watches + ", connections="
+ ", watches=" + watches + ", connections="
+ connections + "]" ;
+ connections + ",healthFlag=" + healthFlag + " ]" ;
}
}