Skip to content

Commit 3ffa06e

Browse files
authored
release 0.3.1
2 parents 9a256ab + 3f93d57 commit 3ffa06e

File tree

65 files changed

+1704
-217
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1704
-217
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ language: java
22
sudo: false # faster builds
33

44
jdk:
5-
- openjdk11
6-
- openjdk8
5+
- openjdk11
6+
- openjdk8
77

88
cache:
99
directories:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ For more details about principle and design, please go to [Fescar wiki page](htt
6565

6666
## Maven dependency
6767
```xml
68-
<fescar.version>0.2.2</fescar.version>
68+
<fescar.version>0.3.0</fescar.version>
6969

7070
<dependency>
7171
<groupId>com.alibaba.fescar</groupId>

common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<parent>
2121
<artifactId>fescar-all</artifactId>
2222
<groupId>com.alibaba.fescar</groupId>
23-
<version>0.3.0</version>
23+
<version>0.3.1</version>
2424
</parent>
2525
<modelVersion>4.0.0</modelVersion>
2626
<artifactId>fescar-common</artifactId>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 1999-2018 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.fescar.common.thread;
18+
19+
import java.util.concurrent.BlockingQueue;
20+
import java.util.concurrent.RejectedExecutionHandler;
21+
import java.util.concurrent.ThreadPoolExecutor;
22+
23+
/**
24+
* Policies for RejectedExecutionHandler
25+
*
26+
* Created by guoyao on 2019/2/26.
27+
*/
28+
public final class RejectedPolicies {
29+
30+
/**
31+
* when rejected happened ,add the new task and run the oldest task
32+
*
33+
* @return rejected execution handler
34+
*/
35+
public static RejectedExecutionHandler runsOldestTaskPolicy() {
36+
return new RejectedExecutionHandler() {
37+
@Override
38+
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
39+
if (executor.isShutdown()) {
40+
return;
41+
}
42+
BlockingQueue<Runnable> workQueue = executor.getQueue();
43+
Runnable firstWork = workQueue.poll();
44+
boolean newTaskAdd = workQueue.offer(r);
45+
if (firstWork != null) {
46+
firstWork.run();
47+
}
48+
if (!newTaskAdd) {
49+
executor.execute(r);
50+
}
51+
}
52+
};
53+
}
54+
}

common/src/main/java/com/alibaba/fescar/common/util/NetUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class NetUtil {
3636
private static final Logger LOGGER = LoggerFactory.getLogger(NetUtil.class);
3737
private static final String LOCALHOST = "127.0.0.1";
3838

39-
private static final String ANYHOST = "0.0.0.0";
39+
private static final String ANY_HOST = "0.0.0.0";
4040

4141
private static volatile InetAddress LOCAL_ADDRESS = null;
4242

@@ -202,6 +202,7 @@ private static boolean isValidAddress(InetAddress address) {
202202
return false;
203203
}
204204
String name = address.getHostAddress();
205-
return (name != null && !ANYHOST.equals(name) && !LOCALHOST.equals(name) && IP_PATTERN.matcher(name).matches());
205+
return (name != null && !ANY_HOST.equals(name) && !LOCALHOST.equals(name) && IP_PATTERN.matcher(name)
206+
.matches());
206207
}
207208
}

common/src/main/java/com/alibaba/fescar/common/util/StringUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private StringUtils() {
3939
* @param str the str
4040
* @return the boolean
4141
*/
42-
public static final boolean isEmpty(String str) {
42+
public static final boolean isNullOrEmpty(String str) {
4343
return (str == null) || (str.isEmpty());
4444
}
4545

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 1999-2018 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.fescar.common.thread;
18+
19+
import java.util.concurrent.CountDownLatch;
20+
import java.util.concurrent.LinkedBlockingQueue;
21+
import java.util.concurrent.ThreadPoolExecutor;
22+
import java.util.concurrent.TimeUnit;
23+
import java.util.concurrent.atomic.AtomicInteger;
24+
25+
import org.junit.Test;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Created by guoyao on 2019/2/26.
31+
*/
32+
public class RejectedPoliciesTest {
33+
34+
private final int DEFAULT_CORE_POOL_SIZE = 1;
35+
private final int DEFAULT_KEEP_ALIVE_TIME = 10;
36+
private final int MAX_QUEUE_SIZE = 1;
37+
38+
/**
39+
* Test runs oldest task policy.
40+
*
41+
* @throws Exception the exception
42+
*/
43+
@Test
44+
public void testRunsOldestTaskPolicy() throws Exception {
45+
AtomicInteger atomicInteger = new AtomicInteger();
46+
ThreadPoolExecutor poolExecutor =
47+
new ThreadPoolExecutor(DEFAULT_CORE_POOL_SIZE, DEFAULT_CORE_POOL_SIZE, DEFAULT_KEEP_ALIVE_TIME,
48+
TimeUnit.MILLISECONDS,
49+
new LinkedBlockingQueue(MAX_QUEUE_SIZE),
50+
new NamedThreadFactory("OldestRunsPolicy", DEFAULT_CORE_POOL_SIZE),
51+
RejectedPolicies.runsOldestTaskPolicy());
52+
CountDownLatch downLatch1 = new CountDownLatch(1);
53+
CountDownLatch downLatch2 = new CountDownLatch(1);
54+
CountDownLatch downLatch3 = new CountDownLatch(1);
55+
//task1
56+
poolExecutor.execute(new Runnable() {
57+
@Override
58+
public void run() {
59+
try {
60+
//wait the oldest task of queue count down
61+
downLatch1.await();
62+
} catch (InterruptedException e) {
63+
e.printStackTrace();
64+
}
65+
atomicInteger.getAndAdd(1);
66+
}
67+
});
68+
assertThat(atomicInteger.get()).isEqualTo(0);
69+
//task2
70+
poolExecutor.execute(new Runnable() {
71+
@Override
72+
public void run() {
73+
// run second
74+
atomicInteger.getAndAdd(2);
75+
}
76+
});
77+
//task3
78+
poolExecutor.execute(new Runnable() {
79+
@Override
80+
public void run() {
81+
downLatch2.countDown();
82+
//task3 run
83+
atomicInteger.getAndAdd(3);
84+
downLatch3.countDown();
85+
}
86+
});
87+
//only the task2 run which is the oldest task of queue
88+
assertThat(atomicInteger.get()).isEqualTo(2);
89+
downLatch1.countDown();
90+
downLatch2.await();
91+
//wait task3 run +3
92+
downLatch3.await();
93+
//run task3
94+
assertThat(atomicInteger.get()).isEqualTo(6);
95+
96+
}
97+
}

common/src/test/java/com/alibaba/fescar/common/util/StringUtilsTest.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.alibaba.fescar.common.util;
1818

19-
import java.io.FileNotFoundException;
2019
import java.io.IOException;
2120
import java.io.InputStream;
2221
import java.sql.SQLException;
@@ -40,11 +39,12 @@ public class StringUtilsTest {
4039
* Test is empty.
4140
*/
4241
@Test
43-
public void testIsEmpty() {
44-
assertThat(StringUtils.isEmpty(null)).isTrue();
45-
assertThat(StringUtils.isEmpty("abc")).isFalse();
46-
assertThat(StringUtils.isEmpty("")).isTrue();
47-
assertThat(StringUtils.isEmpty(" ")).isFalse();
42+
public void testIsNullOrEmpty() {
43+
44+
assertThat(StringUtils.isNullOrEmpty(null)).isTrue();
45+
assertThat(StringUtils.isNullOrEmpty("abc")).isFalse();
46+
assertThat(StringUtils.isNullOrEmpty("")).isTrue();
47+
assertThat(StringUtils.isNullOrEmpty(" ")).isFalse();
4848
}
4949

5050
/**
@@ -80,16 +80,9 @@ public void testBlob2string() throws SQLException {
8080
*/
8181
@Test
8282
@Ignore
83-
public void testInputStream2String() {
84-
try {
85-
InputStream inputStream = StringUtilsTest.class.getClassLoader().getResourceAsStream("test.txt");
86-
assertThat(StringUtils.inputStream2String(inputStream)).isEqualTo("abc\n"
87-
+ ":\"klsdf\n"
88-
+ "2ks,x:\".,-3sd˚ø≤ø¬≥");
89-
} catch (FileNotFoundException e) {
90-
e.printStackTrace();
91-
} catch (IOException e) {
92-
e.printStackTrace();
93-
}
83+
public void testInputStream2String() throws IOException {
84+
InputStream inputStream = StringUtilsTest.class.getClassLoader().getResourceAsStream("test.txt");
85+
assertThat(StringUtils.inputStream2String(inputStream))
86+
.isEqualTo("abc\n" + ":\"klsdf\n" + "2ks,x:\".,-3sd˚ø≤ø¬≥");
9487
}
9588
}

config/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<parent>
2121
<artifactId>fescar-all</artifactId>
2222
<groupId>com.alibaba.fescar</groupId>
23-
<version>0.3.0</version>
23+
<version>0.3.1</version>
2424
</parent>
2525
<modelVersion>4.0.0</modelVersion>
2626
<artifactId>fescar-config</artifactId>
@@ -57,5 +57,9 @@
5757
<artifactId>junit</artifactId>
5858
<scope>test</scope>
5959
</dependency>
60+
<dependency>
61+
<groupId>com.101tec</groupId>
62+
<artifactId>zkclient</artifactId>
63+
</dependency>
6064
</dependencies>
6165
</project>

config/src/main/java/com/alibaba/fescar/config/ConfigType.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.alibaba.fescar.config;
1818

19+
1920
import com.alibaba.fescar.common.exception.NotSupportYetException;
2021

2122
/**
@@ -29,6 +30,10 @@ public enum ConfigType {
2930
* File config type.
3031
*/
3132
File,
33+
/**
34+
* zookeeper config type.
35+
*/
36+
ZK,
3237
/**
3338
* Nacos config type.
3439
*/
@@ -45,14 +50,11 @@ public enum ConfigType {
4550
* @return the type
4651
*/
4752
public static ConfigType getType(String name) {
48-
if (File.name().equalsIgnoreCase(name)) {
49-
return File;
50-
} else if (Nacos.name().equalsIgnoreCase(name)) {
51-
return Nacos;
52-
} else if (Apollo.name().equalsIgnoreCase(name)) {
53-
return Apollo;
54-
} else {
55-
throw new NotSupportYetException("unsupport type:" + name);
53+
for (ConfigType configType : values()) {
54+
if (configType.name().equalsIgnoreCase(name)) {
55+
return configType;
56+
}
5657
}
58+
throw new IllegalArgumentException("illegal type:" + name);
5759
}
5860
}

config/src/main/java/com/alibaba/fescar/config/ConfigurationFactory.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ public final class ConfigurationFactory {
4646
*/
4747
public static Configuration getInstance() {
4848
ConfigType configType = null;
49+
String configTypeName = null;
4950
try {
50-
configType = ConfigType.getType(
51-
FILE_INSTANCE.getConfig(ConfigurationKeys.FILE_ROOT_CONFIG + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR
52-
+ ConfigurationKeys.FILE_ROOT_TYPE));
51+
configTypeName = FILE_INSTANCE.getConfig(ConfigurationKeys.FILE_ROOT_CONFIG + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR
52+
+ ConfigurationKeys.FILE_ROOT_TYPE);
53+
configType = ConfigType.getType(configTypeName);
5354
} catch (Exception exx) {
54-
LOGGER.error(exx.getMessage());
55+
throw new NotSupportYetException("not support register type: " + configTypeName);
5556
}
5657
Configuration configuration;
5758
switch (configType) {
@@ -76,6 +77,13 @@ public static Configuration getInstance() {
7677
String name = FILE_INSTANCE.getConfig(pathDataId);
7778
configuration = new FileConfiguration(name);
7879
break;
80+
case ZK:
81+
try {
82+
configuration = new ZKConfiguration();
83+
} catch (Exception e) {
84+
throw new RuntimeException(e);
85+
}
86+
break;
7987
default:
8088
throw new NotSupportYetException("not support register type:" + configType);
8189
}

0 commit comments

Comments
 (0)