kezhenxu94
3 years ago
committed by
GitHub
106 changed files with 534 additions and 2454 deletions
@ -1,226 +0,0 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.dolphinscheduler.api.utils; |
||||
|
||||
import org.apache.dolphinscheduler.common.utils.StringUtils; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.util.Scanner; |
||||
|
||||
/** |
||||
* zookeeper state monitor |
||||
* |
||||
*/ |
||||
public class ZooKeeperState { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ZooKeeperState.class); |
||||
|
||||
private final String host; |
||||
private final int port; |
||||
|
||||
private float minLatency = -1, avgLatency = -1, maxLatency = -1; |
||||
private long received = -1; |
||||
private long sent = -1; |
||||
private int outStanding = -1; |
||||
private long zxid = -1; |
||||
private String mode = null; |
||||
private int nodeCount = -1; |
||||
private int watches = -1; |
||||
private int connections = -1; |
||||
|
||||
public ZooKeeperState(String connectionString) { |
||||
String host = connectionString.substring(0, |
||||
connectionString.indexOf(':')); |
||||
int port = Integer.parseInt(connectionString.substring(connectionString |
||||
.indexOf(':') + 1)); |
||||
this.host = host; |
||||
this.port = port; |
||||
} |
||||
|
||||
public void getZookeeperInfo() { |
||||
String content = cmd("srvr"); |
||||
if (StringUtils.isNotBlank(content)) { |
||||
try (Scanner scannerForStat = new Scanner(content)) { |
||||
while (scannerForStat.hasNext()) { |
||||
String line = scannerForStat.nextLine(); |
||||
if (line.startsWith("Latency min/avg/max:")) { |
||||
String[] latencys = getStringValueFromLine(line).split("/"); |
||||
minLatency = Float.parseFloat(latencys[0]); |
||||
avgLatency = Float.parseFloat(latencys[1]); |
||||
maxLatency = Float.parseFloat(latencys[2]); |
||||
} else if (line.startsWith("Received:")) { |
||||
received = Long.parseLong(getStringValueFromLine(line)); |
||||
} else if (line.startsWith("Sent:")) { |
||||
sent = Long.parseLong(getStringValueFromLine(line)); |
||||
} else if (line.startsWith("Outstanding:")) { |
||||
outStanding = Integer.parseInt(getStringValueFromLine(line)); |
||||
} else if (line.startsWith("Zxid:")) { |
||||
zxid = Long.parseLong(getStringValueFromLine(line).substring(2), 16); |
||||
} else if (line.startsWith("Mode:")) { |
||||
mode = getStringValueFromLine(line); |
||||
} else if (line.startsWith("Node count:")) { |
||||
nodeCount = Integer.parseInt(getStringValueFromLine(line)); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
String wchsText = cmd("wchs"); |
||||
if (StringUtils.isNotBlank(wchsText)) { |
||||
try (Scanner scannerForWchs = new Scanner(wchsText)) { |
||||
while (scannerForWchs.hasNext()) { |
||||
String line = scannerForWchs.nextLine(); |
||||
if (line.startsWith("Total watches:")) { |
||||
watches = Integer.parseInt(getStringValueFromLine(line)); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
String consText = cmd("cons"); |
||||
if (StringUtils.isNotBlank(consText)) { |
||||
Scanner scannerForCons = new Scanner(consText); |
||||
if (StringUtils.isNotBlank(consText)) { |
||||
connections = 0; |
||||
} |
||||
while (scannerForCons.hasNext()) { |
||||
@SuppressWarnings("unused") |
||||
String line = scannerForCons.nextLine(); |
||||
++connections; |
||||
} |
||||
scannerForCons.close(); |
||||
} |
||||
} |
||||
|
||||
|
||||
public boolean ruok() { |
||||
return "imok\n".equals(cmd("ruok")); |
||||
} |
||||
|
||||
|
||||
private String getStringValueFromLine(String line) { |
||||
return line.substring(line.indexOf(":") + 1, line.length()).replaceAll( |
||||
" ", "").trim(); |
||||
} |
||||
|
||||
private class SendThread extends Thread { |
||||
private String cmd; |
||||
|
||||
private String ret = ""; |
||||
|
||||
public SendThread(String cmd) { |
||||
this.cmd = cmd; |
||||
} |
||||
|
||||
@Override |
||||
public void run() { |
||||
try { |
||||
ret = FourLetterWordMain.send4LetterWord(host, port, cmd); |
||||
} catch (Exception e) { |
||||
logger.error(e.getMessage(),e); |
||||
return; |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
private String cmd(String cmd) { |
||||
final int waitTimeout = 5; |
||||
SendThread sendThread = new SendThread(cmd); |
||||
sendThread.setName("FourLetterCmd:" + cmd); |
||||
sendThread.start(); |
||||
try { |
||||
sendThread.join(waitTimeout * 1000L); |
||||
return sendThread.ret; |
||||
} catch (InterruptedException e) { |
||||
logger.error("send " + cmd + " to server " + host + ":" + port + " failed!", e); |
||||
Thread.currentThread().interrupt(); |
||||
} |
||||
return ""; |
||||
} |
||||
|
||||
public Logger getLogger() { |
||||
return logger; |
||||
} |
||||
|
||||
public String getHost() { |
||||
return host; |
||||
} |
||||
|
||||
public int getPort() { |
||||
return port; |
||||
} |
||||
|
||||
public float getMinLatency() { |
||||
return minLatency; |
||||
} |
||||
|
||||
public float getAvgLatency() { |
||||
return avgLatency; |
||||
} |
||||
|
||||
public float getMaxLatency() { |
||||
return maxLatency; |
||||
} |
||||
|
||||
public long getReceived() { |
||||
return received; |
||||
} |
||||
|
||||
public long getSent() { |
||||
return sent; |
||||
} |
||||
|
||||
public int getOutStanding() { |
||||
return outStanding; |
||||
} |
||||
|
||||
public long getZxid() { |
||||
return zxid; |
||||
} |
||||
|
||||
public String getMode() { |
||||
return mode; |
||||
} |
||||
|
||||
public int getNodeCount() { |
||||
return nodeCount; |
||||
} |
||||
|
||||
public int getWatches() { |
||||
return watches; |
||||
} |
||||
|
||||
public int getConnections() { |
||||
return connections; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "ZooKeeperState [host=" + host + ", port=" + port |
||||
+ ", minLatency=" + minLatency + ", avgLatency=" + avgLatency |
||||
+ ", maxLatency=" + maxLatency + ", received=" + received |
||||
+ ", sent=" + sent + ", outStanding=" + outStanding + ", zxid=" |
||||
+ zxid + ", mode=" + mode + ", nodeCount=" + nodeCount |
||||
+ ", watches=" + watches + ", connections=" |
||||
+ connections + "]"; |
||||
} |
||||
|
||||
|
||||
|
||||
} |
@ -1,33 +0,0 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.common.utils; |
||||
|
||||
public class BooleanUtils { |
||||
|
||||
public static boolean isTrue(Boolean bool) { |
||||
if (bool == null) { |
||||
return false; |
||||
} else { |
||||
return bool; |
||||
} |
||||
} |
||||
|
||||
public static boolean isNotTrue(Boolean bool) { |
||||
return !isTrue(bool); |
||||
} |
||||
} |
@ -1,226 +0,0 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.common.utils; |
||||
|
||||
import java.util.Collection; |
||||
import java.util.Iterator; |
||||
|
||||
/** |
||||
* java.lang.String utils class
|
||||
*/ |
||||
public class StringUtils { |
||||
|
||||
/** |
||||
* The empty String {@code ""}. |
||||
*/ |
||||
public static final String EMPTY = ""; |
||||
|
||||
public static final int INDEX_NOT_FOUND = -1; |
||||
|
||||
private StringUtils() { |
||||
throw new UnsupportedOperationException("Construct StringUtils"); |
||||
} |
||||
|
||||
/** |
||||
* <p>Checks if a CharSequence is empty ("") or null.</p> |
||||
* |
||||
* @param cs the CharSequence to check, may be null |
||||
* @return {@code true} if the CharSequence is empty or null |
||||
*/ |
||||
public static boolean isEmpty(final CharSequence cs) { |
||||
return cs == null || cs.length() == 0; |
||||
} |
||||
|
||||
/** |
||||
* <p>Checks if a CharSequence is not empty ("") and not null.</p> |
||||
* |
||||
* @param cs the CharSequence to check, may be null |
||||
* @return {@code true} if the CharSequence is not empty and not null |
||||
*/ |
||||
public static boolean isNotEmpty(final CharSequence cs) { |
||||
return !isEmpty(cs); |
||||
} |
||||
|
||||
/** |
||||
* <p>Checks if a CharSequence is empty (""), null or whitespace only.</p> |
||||
* |
||||
* @param cs the CharSequence to check, may be null |
||||
* @return {@code true} if the CharSequence is null, empty or whitespace only |
||||
*/ |
||||
public static boolean isBlank(final CharSequence cs) { |
||||
int strLen; |
||||
if (cs == null || (strLen = cs.length()) == 0) { |
||||
return true; |
||||
} |
||||
for (int i = 0; i < strLen; i++) { |
||||
if (!Character.isWhitespace(cs.charAt(i))) { |
||||
return false; |
||||
} |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
/** |
||||
* <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p> |
||||
* |
||||
* @param cs the CharSequence to check, may be null |
||||
* @return {@code true} if the CharSequence is not empty and not null and not whitespace only |
||||
*/ |
||||
public static boolean isNotBlank(final CharSequence cs) { |
||||
return !isBlank(cs); |
||||
} |
||||
|
||||
/** |
||||
* <p>Replace all strings matching the regular expression \t \n \r with _</p> |
||||
* |
||||
* @param src the String , may be null |
||||
* @return the string that has been replaced |
||||
*/ |
||||
public static String replaceNRTtoUnderline(String src) { |
||||
return isBlank(src) ? src : src.replaceAll("[\n|\r|\t]", "_"); |
||||
} |
||||
|
||||
/** |
||||
* <p>Removes control characters (char <= 32) from both |
||||
* ends of this String, handling {@code null} by returning |
||||
* {@code null}.</p> |
||||
* |
||||
* @param str the String to be trimmed, may be null |
||||
* @return the trimmed string, {@code null} if null String input |
||||
*/ |
||||
public static String trim(final String str) { |
||||
return str == null ? null : str.trim(); |
||||
} |
||||
|
||||
/** |
||||
* <p>Returns either the passed in CharSequence, or if the CharSequence is |
||||
* whitespace, empty ("") or {@code null}, the value of {@code defaultStr}.</p> |
||||
* |
||||
* @param <T> the specific kind of CharSequence |
||||
* @param str the CharSequence to check, may be null |
||||
* @param defaultStr the default CharSequence to return |
||||
* if the input is whitespace, empty ("") or {@code null}, may be null |
||||
* @return the passed in CharSequence, or the default |
||||
*/ |
||||
public static <T extends CharSequence> T defaultIfBlank(final T str, final T defaultStr) { |
||||
return isBlank(str) ? defaultStr : str; |
||||
} |
||||
|
||||
/** |
||||
* <p>Compares two String, returning {@code true} if they represent |
||||
* equal string, ignoring case.</p> |
||||
* |
||||
* @param str1 the first String, may be null |
||||
* @param str2 the second String, may be null |
||||
* @return {@code true} if the String are equal, case insensitive, or |
||||
* both {@code null} |
||||
*/ |
||||
public static boolean equalsIgnoreCase(String str1, String str2) { |
||||
return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2); |
||||
} |
||||
|
||||
public static String substringBefore(final String str, final String separator) { |
||||
if (isEmpty(str) || separator == null) { |
||||
return str; |
||||
} |
||||
if (separator.isEmpty()) { |
||||
return EMPTY; |
||||
} |
||||
final int pos = str.indexOf(separator); |
||||
if (pos == INDEX_NOT_FOUND) { |
||||
return str; |
||||
} |
||||
return str.substring(0, pos); |
||||
} |
||||
|
||||
public static String substringAfter(final String str, final String separator) { |
||||
if (isEmpty(str)) { |
||||
return str; |
||||
} |
||||
if (separator == null) { |
||||
return EMPTY; |
||||
} |
||||
final int pos = str.indexOf(separator); |
||||
if (pos == INDEX_NOT_FOUND) { |
||||
return EMPTY; |
||||
} |
||||
return str.substring(pos + separator.length()); |
||||
} |
||||
|
||||
public static long strDigitToLong(String str, long defaultValue) { |
||||
if (str == null) { |
||||
return defaultValue; |
||||
} else { |
||||
try { |
||||
return Long.parseLong(str); |
||||
} catch (NumberFormatException var4) { |
||||
return defaultValue; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* <p>Joins the elements of the provided Collection into a single String |
||||
* containing the provided Collection of elements.</p> |
||||
* |
||||
* @param collection the collection, may be null |
||||
* @param separator the separator |
||||
* @return a single String |
||||
*/ |
||||
public static String join(Collection<?> collection, String separator) { |
||||
return collection == null ? null : join(collection.iterator(), separator); |
||||
} |
||||
|
||||
/** |
||||
* <p>Joins the elements of the provided Iterator into a single String |
||||
* containing the provided Iterator of elements.</p> |
||||
* |
||||
* @param iterator the iterator, may be null |
||||
* @param separator the separator |
||||
* @return a single String |
||||
*/ |
||||
public static String join(Iterator<?> iterator, String separator) { |
||||
if (iterator == null) { |
||||
return null; |
||||
} else if (!iterator.hasNext()) { |
||||
return ""; |
||||
} else { |
||||
Object first = iterator.next(); |
||||
if (!iterator.hasNext()) { |
||||
return first == null ? "" : first.toString(); |
||||
} else { |
||||
StringBuilder buf = new StringBuilder(256); |
||||
if (first != null) { |
||||
buf.append(first); |
||||
} |
||||
|
||||
while (iterator.hasNext()) { |
||||
if (separator != null) { |
||||
buf.append(separator); |
||||
} |
||||
|
||||
Object obj = iterator.next(); |
||||
if (obj != null) { |
||||
buf.append(obj); |
||||
} |
||||
} |
||||
return buf.toString(); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -1,96 +0,0 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.common.utils; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
|
||||
public class StringUtilsTest { |
||||
@Test |
||||
public void testIsNotEmpty() { |
||||
//null string
|
||||
boolean b = StringUtils.isNotEmpty(null); |
||||
Assert.assertFalse(b); |
||||
|
||||
//"" string
|
||||
b = StringUtils.isNotEmpty(""); |
||||
Assert.assertFalse(b); |
||||
|
||||
//" " string
|
||||
b = StringUtils.isNotEmpty(" "); |
||||
Assert.assertTrue(b); |
||||
|
||||
//"test" string
|
||||
b = StringUtils.isNotEmpty("test"); |
||||
Assert.assertTrue(b); |
||||
} |
||||
|
||||
@Test |
||||
public void testIsNotBlank() { |
||||
//null string
|
||||
boolean b = StringUtils.isNotBlank(null); |
||||
Assert.assertFalse(b); |
||||
|
||||
//"" string
|
||||
b = StringUtils.isNotBlank(""); |
||||
Assert.assertFalse(b); |
||||
|
||||
//" " string
|
||||
b = StringUtils.isNotBlank(" "); |
||||
Assert.assertFalse(b); |
||||
|
||||
//" test " string
|
||||
b = StringUtils.isNotBlank(" test "); |
||||
Assert.assertTrue(b); |
||||
|
||||
//"test" string
|
||||
b = StringUtils.isNotBlank("test"); |
||||
Assert.assertTrue(b); |
||||
} |
||||
|
||||
@Test |
||||
public void testTrim() { |
||||
String trim = StringUtils.trim(null); |
||||
Assert.assertNull(trim); |
||||
|
||||
trim = StringUtils.trim(" test "); |
||||
Assert.assertEquals("test", trim); |
||||
} |
||||
|
||||
@Test |
||||
public void testDefaultIfBlank() { |
||||
String defaultStr = StringUtils.defaultIfBlank("", "defaultStr"); |
||||
Assert.assertEquals("defaultStr", defaultStr); |
||||
|
||||
defaultStr = StringUtils.defaultIfBlank("test", "defaultStr"); |
||||
Assert.assertEquals("test", defaultStr); |
||||
} |
||||
|
||||
@Test |
||||
public void testJoin() { |
||||
List<String> list = new ArrayList<>(); |
||||
list.add("1"); |
||||
list.add("3"); |
||||
list.add("4"); |
||||
String join = StringUtils.join(list, ","); |
||||
Assert.assertEquals("1,3,4", join); |
||||
} |
||||
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue