Skip to content

Commit

Permalink
release 0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
slievrly authored Mar 15, 2019
2 parents 9a256ab + 3f93d57 commit 3ffa06e
Show file tree
Hide file tree
Showing 65 changed files with 1,704 additions and 217 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ language: java
sudo: false # faster builds

jdk:
- openjdk11
- openjdk8
- openjdk11
- openjdk8

cache:
directories:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ For more details about principle and design, please go to [Fescar wiki page](htt

## Maven dependency
```xml
<fescar.version>0.2.2</fescar.version>
<fescar.version>0.3.0</fescar.version>

<dependency>
<groupId>com.alibaba.fescar</groupId>
Expand Down
2 changes: 1 addition & 1 deletion common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<artifactId>fescar-all</artifactId>
<groupId>com.alibaba.fescar</groupId>
<version>0.3.0</version>
<version>0.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>fescar-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.fescar.common.thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
* Policies for RejectedExecutionHandler
*
* Created by guoyao on 2019/2/26.
*/
public final class RejectedPolicies {

/**
* when rejected happened ,add the new task and run the oldest task
*
* @return rejected execution handler
*/
public static RejectedExecutionHandler runsOldestTaskPolicy() {
return new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
if (executor.isShutdown()) {
return;
}
BlockingQueue<Runnable> workQueue = executor.getQueue();
Runnable firstWork = workQueue.poll();
boolean newTaskAdd = workQueue.offer(r);
if (firstWork != null) {
firstWork.run();
}
if (!newTaskAdd) {
executor.execute(r);
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class NetUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(NetUtil.class);
private static final String LOCALHOST = "127.0.0.1";

private static final String ANYHOST = "0.0.0.0";
private static final String ANY_HOST = "0.0.0.0";

private static volatile InetAddress LOCAL_ADDRESS = null;

Expand Down Expand Up @@ -202,6 +202,7 @@ private static boolean isValidAddress(InetAddress address) {
return false;
}
String name = address.getHostAddress();
return (name != null && !ANYHOST.equals(name) && !LOCALHOST.equals(name) && IP_PATTERN.matcher(name).matches());
return (name != null && !ANY_HOST.equals(name) && !LOCALHOST.equals(name) && IP_PATTERN.matcher(name)
.matches());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private StringUtils() {
* @param str the str
* @return the boolean
*/
public static final boolean isEmpty(String str) {
public static final boolean isNullOrEmpty(String str) {
return (str == null) || (str.isEmpty());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.fescar.common.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Created by guoyao on 2019/2/26.
*/
public class RejectedPoliciesTest {

private final int DEFAULT_CORE_POOL_SIZE = 1;
private final int DEFAULT_KEEP_ALIVE_TIME = 10;
private final int MAX_QUEUE_SIZE = 1;

/**
* Test runs oldest task policy.
*
* @throws Exception the exception
*/
@Test
public void testRunsOldestTaskPolicy() throws Exception {
AtomicInteger atomicInteger = new AtomicInteger();
ThreadPoolExecutor poolExecutor =
new ThreadPoolExecutor(DEFAULT_CORE_POOL_SIZE, DEFAULT_CORE_POOL_SIZE, DEFAULT_KEEP_ALIVE_TIME,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue(MAX_QUEUE_SIZE),
new NamedThreadFactory("OldestRunsPolicy", DEFAULT_CORE_POOL_SIZE),
RejectedPolicies.runsOldestTaskPolicy());
CountDownLatch downLatch1 = new CountDownLatch(1);
CountDownLatch downLatch2 = new CountDownLatch(1);
CountDownLatch downLatch3 = new CountDownLatch(1);
//task1
poolExecutor.execute(new Runnable() {
@Override
public void run() {
try {
//wait the oldest task of queue count down
downLatch1.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
atomicInteger.getAndAdd(1);
}
});
assertThat(atomicInteger.get()).isEqualTo(0);
//task2
poolExecutor.execute(new Runnable() {
@Override
public void run() {
// run second
atomicInteger.getAndAdd(2);
}
});
//task3
poolExecutor.execute(new Runnable() {
@Override
public void run() {
downLatch2.countDown();
//task3 run
atomicInteger.getAndAdd(3);
downLatch3.countDown();
}
});
//only the task2 run which is the oldest task of queue
assertThat(atomicInteger.get()).isEqualTo(2);
downLatch1.countDown();
downLatch2.await();
//wait task3 run +3
downLatch3.await();
//run task3
assertThat(atomicInteger.get()).isEqualTo(6);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.alibaba.fescar.common.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
Expand All @@ -40,11 +39,12 @@ public class StringUtilsTest {
* Test is empty.
*/
@Test
public void testIsEmpty() {
assertThat(StringUtils.isEmpty(null)).isTrue();
assertThat(StringUtils.isEmpty("abc")).isFalse();
assertThat(StringUtils.isEmpty("")).isTrue();
assertThat(StringUtils.isEmpty(" ")).isFalse();
public void testIsNullOrEmpty() {

assertThat(StringUtils.isNullOrEmpty(null)).isTrue();
assertThat(StringUtils.isNullOrEmpty("abc")).isFalse();
assertThat(StringUtils.isNullOrEmpty("")).isTrue();
assertThat(StringUtils.isNullOrEmpty(" ")).isFalse();
}

/**
Expand Down Expand Up @@ -80,16 +80,9 @@ public void testBlob2string() throws SQLException {
*/
@Test
@Ignore
public void testInputStream2String() {
try {
InputStream inputStream = StringUtilsTest.class.getClassLoader().getResourceAsStream("test.txt");
assertThat(StringUtils.inputStream2String(inputStream)).isEqualTo("abc\n"
+ ":\"klsdf\n"
+ "2ks,x:\".,-3sd˚ø≤ø¬≥");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
public void testInputStream2String() throws IOException {
InputStream inputStream = StringUtilsTest.class.getClassLoader().getResourceAsStream("test.txt");
assertThat(StringUtils.inputStream2String(inputStream))
.isEqualTo("abc\n" + ":\"klsdf\n" + "2ks,x:\".,-3sd˚ø≤ø¬≥");
}
}
6 changes: 5 additions & 1 deletion config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<artifactId>fescar-all</artifactId>
<groupId>com.alibaba.fescar</groupId>
<version>0.3.0</version>
<version>0.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>fescar-config</artifactId>
Expand Down Expand Up @@ -57,5 +57,9 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
</dependencies>
</project>
18 changes: 10 additions & 8 deletions config/src/main/java/com/alibaba/fescar/config/ConfigType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.alibaba.fescar.config;


import com.alibaba.fescar.common.exception.NotSupportYetException;

/**
Expand All @@ -29,6 +30,10 @@ public enum ConfigType {
* File config type.
*/
File,
/**
* zookeeper config type.
*/
ZK,
/**
* Nacos config type.
*/
Expand All @@ -45,14 +50,11 @@ public enum ConfigType {
* @return the type
*/
public static ConfigType getType(String name) {
if (File.name().equalsIgnoreCase(name)) {
return File;
} else if (Nacos.name().equalsIgnoreCase(name)) {
return Nacos;
} else if (Apollo.name().equalsIgnoreCase(name)) {
return Apollo;
} else {
throw new NotSupportYetException("unsupport type:" + name);
for (ConfigType configType : values()) {
if (configType.name().equalsIgnoreCase(name)) {
return configType;
}
}
throw new IllegalArgumentException("illegal type:" + name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ public final class ConfigurationFactory {
*/
public static Configuration getInstance() {
ConfigType configType = null;
String configTypeName = null;
try {
configType = ConfigType.getType(
FILE_INSTANCE.getConfig(ConfigurationKeys.FILE_ROOT_CONFIG + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR
+ ConfigurationKeys.FILE_ROOT_TYPE));
configTypeName = FILE_INSTANCE.getConfig(ConfigurationKeys.FILE_ROOT_CONFIG + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR
+ ConfigurationKeys.FILE_ROOT_TYPE);
configType = ConfigType.getType(configTypeName);
} catch (Exception exx) {
LOGGER.error(exx.getMessage());
throw new NotSupportYetException("not support register type: " + configTypeName);
}
Configuration configuration;
switch (configType) {
Expand All @@ -76,6 +77,13 @@ public static Configuration getInstance() {
String name = FILE_INSTANCE.getConfig(pathDataId);
configuration = new FileConfiguration(name);
break;
case ZK:
try {
configuration = new ZKConfiguration();
} catch (Exception e) {
throw new RuntimeException(e);
}
break;
default:
throw new NotSupportYetException("not support register type:" + configType);
}
Expand Down
Loading

0 comments on commit 3ffa06e

Please sign in to comment.