Skip to content

Commit 50af0be

Browse files
authored
fix #491 [master]release 0.2.3 (#493)
1 parent 84cd529 commit 50af0be

File tree

42 files changed

+855
-112
lines changed

Some content is hidden

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

42 files changed

+855
-112
lines changed

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

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@ private static InetAddress getLocalAddress0() {
188188
LOGGER.error("Could not get local host ip address, will use 127.0.0.1 instead.");
189189
return localAddress;
190190
}
191-
191+
public static void validAddress(InetSocketAddress address) {
192+
if (null == address.getHostName() || 0 == address.getPort()) {
193+
throw new IllegalArgumentException("invalid address:" + address);
194+
}
195+
}
192196
private static boolean isValidAddress(InetAddress address) {
193197
if (address == null || address.isLoopbackAddress()) {
194198
return false;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 org.junit.Test;
20+
import org.testng.Assert;
21+
22+
import java.util.concurrent.CountDownLatch;
23+
import java.util.concurrent.LinkedBlockingQueue;
24+
import java.util.concurrent.ThreadPoolExecutor;
25+
import java.util.concurrent.TimeUnit;
26+
import java.util.concurrent.atomic.AtomicInteger;
27+
28+
/**
29+
* Created by guoyao on 2019/2/26.
30+
*/
31+
public class RejectedPolicysTest {
32+
33+
private final int DEFAULT_CORE_POOL_SIZE=1;
34+
private final int DEFAULT_KEEP_ALIVE_TIME=10;
35+
private final int MAX_QUEUE_SIZE=1;
36+
37+
@Test
38+
public void testRunsOldestTaskPolicy() throws Exception {
39+
AtomicInteger atomicInteger=new AtomicInteger();
40+
ThreadPoolExecutor poolExecutor=
41+
new ThreadPoolExecutor(DEFAULT_CORE_POOL_SIZE, DEFAULT_CORE_POOL_SIZE, DEFAULT_KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS,
42+
new LinkedBlockingQueue(MAX_QUEUE_SIZE),
43+
new NamedThreadFactory("OldestRunsPolicy", DEFAULT_CORE_POOL_SIZE), RejectedPolicys.runsOldestTaskPolicy());
44+
CountDownLatch downLatch1=new CountDownLatch(1);
45+
CountDownLatch downLatch2=new CountDownLatch(1);
46+
//task1
47+
poolExecutor.execute(new Runnable() {
48+
@Override
49+
public void run() {
50+
try {
51+
//wait the oldest task of queue count down
52+
downLatch1.await();
53+
} catch (InterruptedException e) {
54+
e.printStackTrace();
55+
}
56+
atomicInteger.getAndAdd(1);
57+
}
58+
});
59+
Assert.assertEquals(atomicInteger.get(), 0);
60+
//task2
61+
poolExecutor.execute(new Runnable() {
62+
@Override
63+
public void run() {
64+
// run second
65+
atomicInteger.getAndAdd(2);
66+
}
67+
});
68+
//task3
69+
poolExecutor.execute(new Runnable() {
70+
@Override
71+
public void run() {
72+
downLatch2.countDown();
73+
atomicInteger.getAndAdd(3);
74+
}
75+
});
76+
//only the task2 run which is the oldest task of queue
77+
Assert.assertEquals(atomicInteger.get(), 2);
78+
downLatch1.countDown();
79+
downLatch2.await();
80+
//run task3
81+
Assert.assertEquals(atomicInteger.get(), 6);
82+
83+
}
84+
}

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.2.2</version>
23+
<version>0.2.3</version>
2424
</parent>
2525
<modelVersion>4.0.0</modelVersion>
2626
<artifactId>fescar-config</artifactId>
@@ -48,6 +48,10 @@
4848
<groupId>com.alibaba.nacos</groupId>
4949
<artifactId>nacos-client</artifactId>
5050
</dependency>
51+
<dependency>
52+
<groupId>com.ctrip.framework.apollo</groupId>
53+
<artifactId>apollo-client</artifactId>
54+
</dependency>
5155
<dependency>
5256
<groupId>junit</groupId>
5357
<artifactId>junit</artifactId>

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,15 @@
2020
* The type Abstract configuration.
2121
*
2222
* @param <T> the type parameter
23-
* @Author: jimin.jm @alibaba-inc.com
24-
* @Project: fescar -all
25-
* @DateTime: 2019 /2/1 2:18 PM
26-
* @FileName: AbstractConfiguration
27-
* @Description:
23+
* @author: jimin.jm @alibaba-inc.com
24+
* @date: 2019 /2/1
2825
*/
2926
public abstract class AbstractConfiguration<T> implements Configuration<T> {
3027

3128
/**
3229
* The constant DEFAULT_CONFIG_TIMEOUT.
3330
*/
3431
protected static final long DEFAULT_CONFIG_TIMEOUT = 5 * 1000;
35-
protected static final String FILE_ROOT_REGISTRY = "registry";
36-
protected static final String FILE_CONFIG_SPLIT_CHAR = ".";
3732

3833
@Override
3934
public int getInt(String dataId, int defaultValue, long timeoutMills) {
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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.config;
18+
19+
import com.alibaba.fescar.common.exception.NotSupportYetException;
20+
import com.alibaba.fescar.common.thread.NamedThreadFactory;
21+
import com.ctrip.framework.apollo.Config;
22+
import com.ctrip.framework.apollo.ConfigService;
23+
import com.ctrip.framework.apollo.ConfigChangeListener;
24+
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
25+
import com.google.common.collect.Lists;
26+
27+
import static com.alibaba.fescar.config.ConfigurationKeys.*;
28+
29+
import java.util.*;
30+
import java.util.concurrent.*;
31+
32+
/**
33+
* The type Apollo configuration.
34+
*
35+
* @author: kl @kailing.pub
36+
* @date: 2019/2/27
37+
*/
38+
public class ApolloConfiguration extends AbstractConfiguration<ConfigChangeListener> {
39+
40+
private static final String REGISTRY_TYPE = "apollo";
41+
private static final String APP_ID = "app.id";
42+
private static final String APOLLO_META = "apollo.meta";
43+
private static final Configuration FILE_CONFIG = ConfigurationFactory.FILE_INSTANCE;
44+
private static volatile Config config;
45+
private ExecutorService configOperateExecutor;
46+
private static final int CORE_CONFIG_OPERATE_THREAD = 1;
47+
private static final ConcurrentMap<String, ConfigChangeListener> LISTENER_SERVICE_MAP = new ConcurrentHashMap<>();
48+
private static final int MAX_CONFIG_OPERATE_THREAD = 2;
49+
private static volatile ApolloConfiguration instance;
50+
51+
private ApolloConfiguration() {
52+
readyApolloConfig();
53+
if (null == config) {
54+
synchronized (ApolloConfiguration.class) {
55+
if (null == config) {
56+
config = ConfigService.getAppConfig();
57+
configOperateExecutor = new ThreadPoolExecutor(CORE_CONFIG_OPERATE_THREAD, MAX_CONFIG_OPERATE_THREAD,
58+
Integer.MAX_VALUE, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
59+
new NamedThreadFactory("apolloConfigOperate", MAX_CONFIG_OPERATE_THREAD));
60+
config.addChangeListener(new ConfigChangeListener() {
61+
@Override
62+
public void onChange(ConfigChangeEvent changeEvent) {
63+
for (Map.Entry<String, ConfigChangeListener> entry : LISTENER_SERVICE_MAP.entrySet()) {
64+
if (changeEvent.isChanged(entry.getKey())) {
65+
entry.getValue().onChange(changeEvent);
66+
}
67+
}
68+
}
69+
});
70+
}
71+
}
72+
}
73+
}
74+
75+
public static ApolloConfiguration getInstance() {
76+
if (null == instance) {
77+
synchronized (ApolloConfiguration.class) {
78+
if (null == instance) {
79+
instance = new ApolloConfiguration();
80+
}
81+
}
82+
}
83+
return instance;
84+
}
85+
86+
@Override
87+
public String getConfig(String dataId, String defaultValue, long timeoutMills) {
88+
ConfigFuture configFuture = new ConfigFuture(dataId, defaultValue, ConfigFuture.ConfigOperation.GET, timeoutMills);
89+
configOperateExecutor.submit(new Runnable() {
90+
@Override
91+
public void run() {
92+
String result = config.getProperty(dataId, defaultValue);
93+
configFuture.setResult(result);
94+
}
95+
});
96+
return (String) configFuture.get(timeoutMills, TimeUnit.MILLISECONDS);
97+
}
98+
99+
@Override
100+
public boolean putConfig(String dataId, String content, long timeoutMills) {
101+
throw new NotSupportYetException("not support putConfig");
102+
}
103+
104+
@Override
105+
public boolean putConfigIfAbsent(String dataId, String content, long timeoutMills) {
106+
throw new NotSupportYetException("not support putConfigIfAbsent");
107+
}
108+
109+
@Override
110+
public boolean removeConfig(String dataId, long timeoutMills) {
111+
throw new NotSupportYetException("not support removeConfig");
112+
}
113+
114+
@Override
115+
public void addConfigListener(String dataId, ConfigChangeListener listener) {
116+
LISTENER_SERVICE_MAP.put(dataId, listener);
117+
}
118+
119+
@Override
120+
public void removeConfigListener(String dataId, ConfigChangeListener listener) {
121+
LISTENER_SERVICE_MAP.remove(dataId, listener);
122+
}
123+
124+
@Override
125+
public List<ConfigChangeListener> getConfigListeners(String dataId) {
126+
return Lists.newArrayList(LISTENER_SERVICE_MAP.values());
127+
}
128+
129+
private void readyApolloConfig(){
130+
Properties properties = System.getProperties();
131+
if(!properties.containsKey(APP_ID)){
132+
System.setProperty(APP_ID,FILE_CONFIG.getConfig(getApolloAppIdFileKey()));
133+
}
134+
if(!properties.containsKey(APOLLO_META)){
135+
System.setProperty(APOLLO_META,FILE_CONFIG.getConfig(getApolloMetaFileKey()));
136+
}
137+
}
138+
139+
@Override
140+
public String getTypeName() {
141+
return REGISTRY_TYPE;
142+
}
143+
144+
private static String getApolloMetaFileKey() {
145+
return FILE_ROOT_CONFIG + FILE_CONFIG_SPLIT_CHAR + REGISTRY_TYPE + FILE_CONFIG_SPLIT_CHAR
146+
+ APOLLO_META;
147+
}
148+
149+
private static String getApolloAppIdFileKey() {
150+
return FILE_ROOT_CONFIG + FILE_CONFIG_SPLIT_CHAR + REGISTRY_TYPE + FILE_CONFIG_SPLIT_CHAR
151+
+ APP_ID;
152+
}
153+
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121
/**
2222
* The interface Config change listener.
2323
*
24-
* @Author: jimin.jm @alibaba-inc.com
25-
* @Project: fescar -all
26-
* @DateTime: 2018 /12/20 14:41
27-
* @FileName: ConfigChangeListener
28-
* @Description:
24+
* @author: jimin.jm @alibaba-inc.com
25+
* @date: 2018 /12/20
2926
*/
3027
public interface ConfigChangeListener {
3128

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@
2525
/**
2626
* The type Config future.
2727
*
28-
* @Author: jimin.jm @alibaba-inc.com
29-
* @Project: fescar -all
30-
* @DateTime: 2018 /12/20 16:30
31-
* @FileName: ConfigFuture
32-
* @Description:
28+
* @author: jimin.jm @alibaba-inc.com
29+
* @date: 2018 /12/20
3330
*/
3431
public class ConfigFuture {
3532
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigFuture.class);

0 commit comments

Comments
 (0)