Browse Source
* [2.0.1-prepare#6712]Refactor registry plugin and simplify its usage (#6712) * upgrade version and fix dep err * Clean up the utility codes (#6732) * remove code style check * upgrade docker version Co-authored-by: kezhenxu94 <kezhenxu94@apache.org>2.0.7-release
Kirs
3 years ago
committed by
GitHub
178 changed files with 1291 additions and 4062 deletions
@ -1,95 +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.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.io.BufferedReader; |
||||
import java.io.IOException; |
||||
import java.io.InputStreamReader; |
||||
import java.io.OutputStream; |
||||
import java.net.InetAddress; |
||||
import java.net.InetSocketAddress; |
||||
import java.net.Socket; |
||||
import java.net.SocketTimeoutException; |
||||
import java.util.Objects; |
||||
|
||||
public class FourLetterWordMain { |
||||
|
||||
private static final int DEFAULT_SOCKET_TIMEOUT = 5000; |
||||
protected static final Logger LOG = LoggerFactory.getLogger(FourLetterWordMain.class); |
||||
|
||||
private FourLetterWordMain() { |
||||
throw new IllegalStateException("FourLetterWordMain class"); |
||||
} |
||||
|
||||
/** |
||||
* Send the 4letterword |
||||
* @param host the destination host |
||||
* @param port the destination port |
||||
* @param cmd the 4letterword |
||||
* @return server response |
||||
* @throws java.io.IOException io exceptions |
||||
*/ |
||||
public static String send4LetterWord(String host, int port, String cmd) |
||||
throws IOException { |
||||
return send4LetterWord(host, port, cmd, DEFAULT_SOCKET_TIMEOUT); |
||||
} |
||||
|
||||
/** |
||||
* Send the 4letterword |
||||
* @param host the destination host |
||||
* @param port the destination port |
||||
* @param cmd the 4letterword |
||||
* @param timeout in milliseconds, maximum time to wait while connecting/reading data |
||||
* @return server response |
||||
* @throws java.io.IOException io exceptions |
||||
*/ |
||||
public static String send4LetterWord(String host, int port, String cmd, int timeout) |
||||
throws IOException { |
||||
Objects.requireNonNull(cmd, "cmd must not be null"); |
||||
LOG.info("connecting to {} {}", host, port); |
||||
InetSocketAddress hostaddress= host != null ? new InetSocketAddress(host, port) : |
||||
new InetSocketAddress(InetAddress.getByName(null), port); |
||||
|
||||
try (Socket sock = new Socket()) { |
||||
sock.setSoTimeout(timeout); |
||||
sock.connect(hostaddress, timeout); |
||||
OutputStream outstream = sock.getOutputStream(); |
||||
outstream.write(cmd.getBytes()); |
||||
outstream.flush(); |
||||
// this replicates NC - close the output stream before reading
|
||||
sock.shutdownOutput(); |
||||
|
||||
try (BufferedReader reader = |
||||
new BufferedReader( |
||||
new InputStreamReader(sock.getInputStream()))) { |
||||
StringBuilder sb = new StringBuilder(); |
||||
String line; |
||||
while ((line = reader.readLine()) != null) { |
||||
sb.append(line + "\n"); |
||||
} |
||||
return sb.toString(); |
||||
} |
||||
} catch (SocketTimeoutException e) { |
||||
throw new IOException("Exception while executing four letter word: " + cmd, e); |
||||
} |
||||
} |
||||
} |
@ -1,82 +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.enums.NodeType; |
||||
import org.apache.dolphinscheduler.common.model.Server; |
||||
import org.apache.dolphinscheduler.dao.entity.ZookeeperRecord; |
||||
import org.apache.dolphinscheduler.service.registry.RegistryClient; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* monitor zookeeper info todo registry-spi |
||||
* fixme Some of the information obtained in the api belongs to the unique information of zk. |
||||
* I am not sure whether there is a good abstraction method. This is related to whether the specific plug-in is provided. |
||||
*/ |
||||
public class RegistryCenterUtils { |
||||
|
||||
private static RegistryClient registryClient = RegistryClient.getInstance(); |
||||
|
||||
/** |
||||
* @return zookeeper info list |
||||
*/ |
||||
public static List<ZookeeperRecord> zookeeperInfoList() { |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* get master servers |
||||
* |
||||
* @return master server information |
||||
*/ |
||||
public static List<Server> getMasterServers() { |
||||
return registryClient.getServerList(NodeType.MASTER); |
||||
} |
||||
|
||||
/** |
||||
* master construct is the same with worker, use the master instead |
||||
* |
||||
* @return worker server informations |
||||
*/ |
||||
public static List<Server> getWorkerServers() { |
||||
return registryClient.getServerList(NodeType.WORKER); |
||||
} |
||||
|
||||
public static Map<String, String> getServerMaps(NodeType nodeType, boolean hostOnly) { |
||||
return registryClient.getServerMaps(nodeType, hostOnly); |
||||
} |
||||
|
||||
public static List<String> getServerNodeList(NodeType nodeType, boolean hostOnly) { |
||||
return registryClient.getServerNodeList(nodeType, hostOnly); |
||||
} |
||||
|
||||
public static boolean isNodeExisted(String key) { |
||||
return registryClient.isExisted(key); |
||||
} |
||||
|
||||
public static List<String> getChildrenNodes(final String key) { |
||||
return registryClient.getChildrenKeys(key); |
||||
} |
||||
|
||||
public static String getNodeData(String key) { |
||||
return registryClient.get(key); |
||||
} |
||||
} |
@ -1,217 +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.junit.Assert; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.powermock.api.mockito.PowerMockito; |
||||
import org.powermock.core.classloader.annotations.PrepareForTest; |
||||
import org.powermock.modules.junit4.PowerMockRunner; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.io.ByteArrayInputStream; |
||||
import java.io.ByteArrayOutputStream; |
||||
import java.io.InputStream; |
||||
import java.net.InetSocketAddress; |
||||
import java.net.Socket; |
||||
import java.net.SocketTimeoutException; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.mockito.ArgumentMatchers.any; |
||||
import static org.mockito.Mockito.doThrow; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
@RunWith(PowerMockRunner.class) |
||||
@PrepareForTest({FourLetterWordMain.class, Socket.class}) |
||||
public class FourLetterWordMainTest { |
||||
|
||||
private static final Logger logger = |
||||
LoggerFactory.getLogger(FourLetterWordMainTest.class); |
||||
private static final String NEW_LINE = "\n"; |
||||
|
||||
@InjectMocks |
||||
private FourLetterWordMain fourLetterWord; |
||||
@Mock |
||||
private Socket socket; |
||||
@Mock |
||||
private InetSocketAddress socketAddress; |
||||
|
||||
private final String localHost = "127.0.0.1"; |
||||
private final int zkPort = 2181; |
||||
private ByteArrayOutputStream byteArrayOutputStream; |
||||
private InputStream inputStream; |
||||
|
||||
private String cmd; |
||||
private String testResult; |
||||
private String expectedStr; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
// mock socket class
|
||||
PowerMockito.mockStatic(Socket.class); |
||||
try { |
||||
PowerMockito.whenNew(Socket.class).withNoArguments() |
||||
.thenReturn(socket); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* None mock test method, just to check zookeeper status. |
||||
* Comment @Before notation to run this test. |
||||
* Zookeeper status will be as: |
||||
* Zookeeper version: 3.4.11 ... |
||||
* Received: 6739707 |
||||
* Sent: 6739773 |
||||
* Connections: 20 |
||||
* Outstanding: 0 |
||||
* Zxid: 0x9ba |
||||
* Mode: standalone |
||||
* Node count: 263 |
||||
*/ |
||||
public void testCmd() { |
||||
// "192.168.64.11"
|
||||
// final String zkHost = localHost;
|
||||
final String zkHost = "192.168.64.11"; |
||||
cmd = "srvr"; |
||||
try { |
||||
// Change localhost to right zk host ip.
|
||||
final String result = FourLetterWordMain |
||||
.send4LetterWord(zkHost, zkPort, cmd); |
||||
logger.info(cmd + ": " + result + "<<<"); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
public void testEmptyCmd() { |
||||
cmd = ""; |
||||
expectedStr = ""; |
||||
testSend4LetterWord(cmd, expectedStr); |
||||
} |
||||
|
||||
@Test |
||||
public void testNullCmd() { |
||||
cmd = null; |
||||
|
||||
try { |
||||
testResult = FourLetterWordMain |
||||
.send4LetterWord(localHost, zkPort, cmd); |
||||
} catch (Exception e) { |
||||
testResult = e.getMessage(); |
||||
} |
||||
|
||||
logger.info("testNullCmd result: " + testResult); |
||||
assertEquals("cmd must not be null", testResult); |
||||
} |
||||
|
||||
@Test |
||||
public void testNullSocketOutput() { |
||||
cmd = "test null socket output"; |
||||
expectedStr = null; |
||||
testSend4LetterWord(cmd, expectedStr); |
||||
} |
||||
|
||||
@Test |
||||
public void testOneLineOutput() { |
||||
cmd = "line 1"; |
||||
|
||||
// line end without \n
|
||||
expectedStr = "line 1" + NEW_LINE; |
||||
testSend4LetterWord(cmd, expectedStr); |
||||
|
||||
// line end with \n
|
||||
expectedStr = "line 1\n" + NEW_LINE; |
||||
testSend4LetterWord(cmd, expectedStr); |
||||
} |
||||
|
||||
@Test |
||||
public void testMultiline() { |
||||
cmd = "line 1 " + NEW_LINE + |
||||
"line 2 " + NEW_LINE + |
||||
"line 3 " + NEW_LINE; |
||||
|
||||
expectedStr = cmd + NEW_LINE; |
||||
testSend4LetterWord(cmd, expectedStr); |
||||
|
||||
expectedStr = NEW_LINE + NEW_LINE + NEW_LINE; |
||||
testSend4LetterWord(cmd, expectedStr); |
||||
} |
||||
|
||||
@Test |
||||
public void testSocketTimeOut() { |
||||
cmd = "test socket time out"; |
||||
|
||||
try { |
||||
doThrow(new SocketTimeoutException()) |
||||
.when(socket) |
||||
.connect(any(InetSocketAddress.class), Mockito.anyInt()); |
||||
testResult = FourLetterWordMain |
||||
.send4LetterWord(localHost, zkPort, cmd); |
||||
} catch (Exception e) { |
||||
testResult = e.getMessage(); |
||||
} |
||||
|
||||
logger.info("testSocketTimeOut result: " + testResult); |
||||
assertEquals( |
||||
"Exception while executing four letter word: " + cmd, |
||||
testResult |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* Test FourLetterWordMain.send4LetterWord() with input cmd and output |
||||
* string. |
||||
* @param cmd |
||||
* @param expectedStr |
||||
*/ |
||||
public void testSend4LetterWord(String cmd, String expectedStr) { |
||||
try { |
||||
final byte[] strBytes = cmd.getBytes(); |
||||
byteArrayOutputStream = new ByteArrayOutputStream(strBytes.length); |
||||
byteArrayOutputStream.write(strBytes, 0, strBytes.length); |
||||
|
||||
inputStream = new ByteArrayInputStream(expectedStr.getBytes()); |
||||
|
||||
when(socket.getOutputStream()) |
||||
.thenReturn(byteArrayOutputStream); |
||||
when(socket.getInputStream()).thenReturn(inputStream); |
||||
|
||||
final String result = FourLetterWordMain |
||||
.send4LetterWord(localHost, zkPort, cmd); |
||||
logger.info( |
||||
"testSend4LetterWord: " + |
||||
"cmd: " + cmd + |
||||
", expectedStr: " + expectedStr + |
||||
", result: " + result + "." |
||||
); |
||||
Assert.assertEquals(expectedStr, result); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,43 +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.model.Server; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.Ignore; |
||||
import org.junit.Test; |
||||
|
||||
/** |
||||
* zookeeper monitor utils test |
||||
*/ |
||||
@Ignore |
||||
public class RegistryCenterUtilsTest { |
||||
|
||||
@Test |
||||
public void testGetMasterList(){ |
||||
List<Server> masterServerList = RegistryCenterUtils.getMasterServers(); |
||||
List<Server> workerServerList = RegistryCenterUtils.getWorkerServers(); |
||||
|
||||
Assert.assertTrue(masterServerList.size() >= 0); |
||||
Assert.assertTrue(workerServerList.size() >= 0); |
||||
} |
||||
|
||||
} |
@ -1,38 +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 ArrayUtils { |
||||
|
||||
public static byte[] clone(byte[] array) { |
||||
return array == null ? null : (byte[])((byte[])array.clone()); |
||||
} |
||||
|
||||
public static byte[] addAll(byte[] array1, byte[] array2) { |
||||
if (array1 == null) { |
||||
return clone(array2); |
||||
} else if (array2 == null) { |
||||
return clone(array1); |
||||
} else { |
||||
byte[] joinedArray = new byte[array1.length + array2.length]; |
||||
System.arraycopy(array1, 0, joinedArray, 0, array1.length); |
||||
System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); |
||||
return joinedArray; |
||||
} |
||||
} |
||||
} |
@ -1,48 +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 EnumUtils { |
||||
|
||||
private EnumUtils() { |
||||
throw new UnsupportedOperationException("Construct EnumUtils"); |
||||
} |
||||
|
||||
public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName) { |
||||
if (enumName == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
return Enum.valueOf(enumClass, enumName); |
||||
} catch (final IllegalArgumentException ex) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
public static <E extends Enum<E>> boolean isValidEnum(final Class<E> enumClass, final String enumName) { |
||||
if (enumName == null) { |
||||
return false; |
||||
} |
||||
try { |
||||
Enum.valueOf(enumClass, enumName); |
||||
return true; |
||||
} catch (final IllegalArgumentException ex) { |
||||
return false; |
||||
} |
||||
} |
||||
} |
@ -1,45 +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 org.apache.dolphinscheduler.common.Constants; |
||||
|
||||
import org.apache.commons.lang.StringUtils; |
||||
|
||||
/** |
||||
* sensitive log Util |
||||
*/ |
||||
public class SensitiveLogUtils { |
||||
|
||||
private SensitiveLogUtils() { |
||||
throw new UnsupportedOperationException("Construct SensitiveLogUtils"); |
||||
} |
||||
|
||||
/** |
||||
* @param dataSourcePwd data source password |
||||
* @return String |
||||
*/ |
||||
public static String maskDataSourcePwd(String dataSourcePwd) { |
||||
|
||||
if (!StringUtils.isEmpty(dataSourcePwd)) { |
||||
dataSourcePwd = Constants.PASSWORD_DEFAULT; |
||||
} |
||||
return dataSourcePwd; |
||||
} |
||||
|
||||
} |
@ -1,161 +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 org.apache.dolphinscheduler.common.Constants; |
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
|
||||
import java.util.*; |
||||
|
||||
|
||||
public class CollectionUtilsTest { |
||||
|
||||
@Test |
||||
public void equalLists() { |
||||
Assert.assertTrue(CollectionUtils.equalLists(null,null)); |
||||
Assert.assertTrue(CollectionUtils.equalLists(new ArrayList<Integer>(),new ArrayList<Integer>())); |
||||
List<Integer> a = new ArrayList<Integer>(); |
||||
a.add(1); |
||||
a.add(2); |
||||
List<Integer> b = new ArrayList<Integer>(); |
||||
b.add(1); |
||||
b.add(2); |
||||
Assert.assertTrue(CollectionUtils.equalLists(a, b)); |
||||
a.add(1); |
||||
Assert.assertFalse(CollectionUtils.equalLists(a, b)); |
||||
b.add(2); |
||||
Assert.assertFalse(CollectionUtils.equalLists(a, b)); |
||||
a.add(2); |
||||
b.add(1); |
||||
a.add(4); |
||||
b.add(2); |
||||
Assert.assertFalse(CollectionUtils.equalLists(a, b)); |
||||
Assert.assertFalse(CollectionUtils.equalLists(null, new ArrayList<Integer>())); |
||||
Assert.assertFalse(CollectionUtils.equalLists(new ArrayList<Integer>(), null)); |
||||
} |
||||
|
||||
@Test |
||||
public void subtract() { |
||||
Set<Integer> a = new HashSet<Integer>(); |
||||
a.add(1); |
||||
a.add(2); |
||||
a.add(3); |
||||
Set<Integer> b = new HashSet<Integer>(); |
||||
b.add(0); |
||||
b.add(2); |
||||
b.add(4); |
||||
Assert.assertArrayEquals(new Integer[]{1,3},CollectionUtils.subtract(a,b).toArray()); |
||||
} |
||||
|
||||
@Test |
||||
public void stringToMap() { |
||||
Map<String, String> a = CollectionUtils.stringToMap("a=b;c=d;", ";"); |
||||
Assert.assertNotNull(a); |
||||
Assert.assertTrue(a.size() == 2); |
||||
a = CollectionUtils.stringToMap(null, ";"); |
||||
Assert.assertTrue(a.isEmpty()); |
||||
a = CollectionUtils.stringToMap("", ";"); |
||||
Assert.assertTrue(a.isEmpty()); |
||||
a = CollectionUtils.stringToMap("a=b;c=d", ""); |
||||
Assert.assertTrue(a.isEmpty()); |
||||
a = CollectionUtils.stringToMap("a=b;c=d", null); |
||||
Assert.assertTrue(a.isEmpty()); |
||||
a = CollectionUtils.stringToMap("a=b;c=d;e=f", ";"); |
||||
Assert.assertEquals(3, a.size()); |
||||
a = CollectionUtils.stringToMap("a;b=f", ";"); |
||||
Assert.assertTrue(a.isEmpty()); |
||||
a = CollectionUtils.stringToMap("a=b;c=d;e=f;", ";", "test"); |
||||
Assert.assertEquals(3, a.size()); |
||||
Assert.assertNotNull(a.get("testa")); |
||||
} |
||||
|
||||
@Test |
||||
public void getListByExclusion() { |
||||
Assert.assertNotNull(CollectionUtils.getListByExclusion(null, null)); |
||||
List<Integer> originList = new ArrayList<>(); |
||||
originList.add(1); |
||||
originList.add(2); |
||||
List<Map<String, Object>> ret = CollectionUtils.getListByExclusion(originList, null); |
||||
Assert.assertEquals(2, ret.size()); |
||||
ret = CollectionUtils.getListByExclusion(originList, new HashSet<>()); |
||||
Assert.assertEquals(2, ret.size()); |
||||
Assert.assertFalse(ret.get(0).isEmpty()); |
||||
Set<String> exclusion = new HashSet<>(); |
||||
exclusion.add(Constants.CLASS); |
||||
ret = CollectionUtils.getListByExclusion(originList, exclusion); |
||||
Assert.assertEquals(2, ret.size()); |
||||
Assert.assertTrue(ret.get(0).isEmpty()); |
||||
} |
||||
|
||||
@Test |
||||
public void isNotEmpty() { |
||||
List<Integer> list = new ArrayList<>(); |
||||
Assert.assertFalse(CollectionUtils.isNotEmpty(list)); |
||||
Assert.assertFalse(CollectionUtils.isNotEmpty(null)); |
||||
} |
||||
@Test |
||||
public void isEmpty(){ |
||||
List<Integer> list = new ArrayList<>(); |
||||
Assert.assertTrue(CollectionUtils.isEmpty(list)); |
||||
Assert.assertTrue(CollectionUtils.isEmpty(null)); |
||||
list.add(1); |
||||
Assert.assertFalse(CollectionUtils.isEmpty(list)); |
||||
} |
||||
@Test |
||||
public void isEqualCollection() { |
||||
List<Integer> a = new ArrayList<>(); |
||||
a.add(1); |
||||
List<Integer> b = new ArrayList<>(); |
||||
b.add(1); |
||||
Assert.assertTrue(CollectionUtils.isEqualCollection(a,b)); |
||||
b.add(2); |
||||
Assert.assertFalse(CollectionUtils.isEqualCollection(a,b)); |
||||
} |
||||
|
||||
@Test |
||||
public void getCardinalityMap(){ |
||||
List<Integer> a = new ArrayList<>(); |
||||
a.add(1); |
||||
a.add(2); |
||||
a.add(2); |
||||
a.add(3); |
||||
a.add(3); |
||||
a.add(3); |
||||
Map<Integer,Integer> cardinalityMap = CollectionUtils.getCardinalityMap(a); |
||||
Assert.assertEquals(3, cardinalityMap.size()); |
||||
Assert.assertEquals(1, cardinalityMap.get(1).intValue()); |
||||
Assert.assertEquals(2, cardinalityMap.get(2).intValue()); |
||||
Assert.assertEquals(3, cardinalityMap.get(3).intValue()); |
||||
} |
||||
|
||||
@Test |
||||
public void transformToList() { |
||||
List<String> stringList = new ArrayList<>(); |
||||
stringList.add("1"); |
||||
List<Integer> integers = CollectionUtils.transformToList(stringList, String::length); |
||||
Assert.assertFalse(integers.isEmpty()); |
||||
} |
||||
|
||||
@Test |
||||
public void collectionToMap() { |
||||
List<String> stringList = new ArrayList<>(); |
||||
stringList.add("1"); |
||||
Map<Integer, String> lengthStringMap = CollectionUtils.collectionToMap(stringList, String::length); |
||||
Assert.assertFalse(lengthStringMap.isEmpty()); |
||||
} |
||||
} |
@ -1,85 +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.io.BufferedReader; |
||||
import java.io.FileInputStream; |
||||
import java.io.IOException; |
||||
import java.io.InputStreamReader; |
||||
import java.util.List; |
||||
import java.util.Optional; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
import org.junit.Test.None; |
||||
import org.junit.runner.RunWith; |
||||
import org.powermock.api.mockito.PowerMockito; |
||||
import org.powermock.core.classloader.annotations.PrepareForTest; |
||||
import org.powermock.modules.junit4.PowerMockRunner; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
@RunWith(PowerMockRunner.class) |
||||
@PrepareForTest({LoggerUtils.class}) |
||||
public class LoggerUtilsTest { |
||||
private Logger logger = LoggerFactory.getLogger(LoggerUtilsTest.class); |
||||
|
||||
@Test |
||||
public void buildTaskId() { |
||||
|
||||
String taskId = LoggerUtils.buildTaskId(LoggerUtils.TASK_LOGGER_INFO_PREFIX, 798L,1,4084, 15210); |
||||
|
||||
Assert.assertEquals(" - [taskAppId=TASK-798_1-4084-15210]", taskId); |
||||
} |
||||
|
||||
@Test |
||||
public void getAppIds() { |
||||
List<String> appIdList = LoggerUtils.getAppIds("Running job: application_1_1", logger); |
||||
Assert.assertEquals("application_1_1", appIdList.get(0)); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void testReadWholeFileContent() throws Exception { |
||||
BufferedReader bufferedReader = PowerMockito.mock(BufferedReader.class); |
||||
PowerMockito.whenNew(BufferedReader.class).withAnyArguments().thenReturn(bufferedReader); |
||||
PowerMockito.when(bufferedReader.readLine()).thenReturn("").thenReturn(null); |
||||
FileInputStream fileInputStream = PowerMockito.mock(FileInputStream.class); |
||||
PowerMockito.whenNew(FileInputStream.class).withAnyArguments().thenReturn(fileInputStream); |
||||
|
||||
InputStreamReader inputStreamReader = PowerMockito.mock(InputStreamReader.class); |
||||
PowerMockito.whenNew(InputStreamReader.class).withAnyArguments().thenReturn(inputStreamReader); |
||||
|
||||
String log = LoggerUtils.readWholeFileContent("/tmp/log"); |
||||
Assert.assertNotNull(log); |
||||
|
||||
PowerMockito.when(bufferedReader.readLine()).thenThrow(new IOException()); |
||||
log = LoggerUtils.readWholeFileContent("/tmp/log"); |
||||
Assert.assertNotNull(log); |
||||
} |
||||
|
||||
@Test(expected = None.class) |
||||
public void testLogError() { |
||||
Optional<Logger> loggerOptional = Optional.of(this.logger); |
||||
|
||||
LoggerUtils.logError(loggerOptional, "error message"); |
||||
LoggerUtils.logError(loggerOptional, new RuntimeException("error message")); |
||||
LoggerUtils.logError(loggerOptional, "error message", new RuntimeException("runtime exception")); |
||||
LoggerUtils.logInfo(loggerOptional, "info message"); |
||||
} |
||||
} |
@ -1,37 +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 org.apache.dolphinscheduler.common.Constants; |
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
|
||||
|
||||
public class SensitiveLogUtilsTest { |
||||
|
||||
@Test |
||||
public void testMaskDataSourcePwd() { |
||||
|
||||
String password = "123456"; |
||||
String emptyPassword = ""; |
||||
|
||||
Assert.assertEquals(Constants.PASSWORD_DEFAULT, SensitiveLogUtils.maskDataSourcePwd(password)); |
||||
Assert.assertEquals("", SensitiveLogUtils.maskDataSourcePwd(emptyPassword)); |
||||
|
||||
} |
||||
} |
@ -1,56 +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.plugin.registry.zookeeper; |
||||
|
||||
import org.apache.dolphinscheduler.spi.register.RegistryConnectListener; |
||||
import org.apache.dolphinscheduler.spi.register.RegistryConnectState; |
||||
|
||||
import org.apache.curator.framework.CuratorFramework; |
||||
import org.apache.curator.framework.state.ConnectionState; |
||||
import org.apache.curator.framework.state.ConnectionStateListener; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
public class ZookeeperConnectionStateListener implements ConnectionStateListener { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ZookeeperConnectionStateListener.class); |
||||
|
||||
private RegistryConnectListener registryConnectListener; |
||||
|
||||
public ZookeeperConnectionStateListener(RegistryConnectListener registryConnectListener) { |
||||
this.registryConnectListener = registryConnectListener; |
||||
} |
||||
|
||||
@Override |
||||
public void stateChanged(CuratorFramework client, ConnectionState newState) { |
||||
|
||||
if (newState == ConnectionState.LOST) { |
||||
logger.error("connection lost from zookeeper"); |
||||
registryConnectListener.notify(RegistryConnectState.LOST); |
||||
} else if (newState == ConnectionState.RECONNECTED) { |
||||
logger.info("reconnected to zookeeper"); |
||||
registryConnectListener.notify(RegistryConnectState.RECONNECTED); |
||||
} else if (newState == ConnectionState.SUSPENDED) { |
||||
logger.warn("zookeeper connection SUSPENDED"); |
||||
registryConnectListener.notify(RegistryConnectState.SUSPENDED); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -1,34 +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.plugin.registry.zookeeper; |
||||
|
||||
import org.apache.dolphinscheduler.spi.DolphinSchedulerPlugin; |
||||
import org.apache.dolphinscheduler.spi.register.RegistryFactory; |
||||
|
||||
import com.google.common.collect.ImmutableList; |
||||
|
||||
/** |
||||
* zookeeper registry plugin |
||||
*/ |
||||
public class ZookeeperRegistryPlugin implements DolphinSchedulerPlugin { |
||||
|
||||
@Override |
||||
public Iterable<RegistryFactory> getRegisterFactorys() { |
||||
return ImmutableList.of(new ZookeeperRegistryFactory()); |
||||
} |
||||
} |
@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<!-- |
||||
~ Licensed to 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. Apache Software Foundation (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. |
||||
--> |
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<parent> |
||||
<artifactId>dolphinscheduler-registry</artifactId> |
||||
<groupId>org.apache.dolphinscheduler</groupId> |
||||
<version>2.0.1-SNAPSHOT</version> |
||||
</parent> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
|
||||
<artifactId>dolphinscheduler-registry-api</artifactId> |
||||
</project> |
@ -0,0 +1,25 @@
|
||||
/* |
||||
* Licensed to 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. Apache Software Foundation (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.registry.api; |
||||
|
||||
@FunctionalInterface |
||||
public interface ConnectionListener { |
||||
void onUpdate(ConnectionState newState); |
||||
} |
@ -0,0 +1,27 @@
|
||||
/* |
||||
* Licensed to 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. Apache Software Foundation (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.registry.api; |
||||
|
||||
public enum ConnectionState { |
||||
CONNECTED, |
||||
RECONNECTED, |
||||
SUSPENDED, |
||||
DISCONNECTED |
||||
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue