Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
superrain51
apollo
提交
ad94cc77
apollo
项目概览
superrain51
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ad94cc77
编写于
1月 22, 2017
作者:
J
Jason Song
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
do async notification if there are too many clients to notify
上级
35cde2b7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
95 addition
and
0 deletion
+95
-0
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/config/BizConfig.java
...java/com/ctrip/framework/apollo/biz/config/BizConfig.java
+7
-0
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/NotificationControllerV2.java
...lo/configservice/controller/NotificationControllerV2.java
+36
-0
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/controller/NotificationControllerV2Test.java
...onfigservice/controller/NotificationControllerV2Test.java
+52
-0
未找到文件。
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/config/BizConfig.java
浏览文件 @
ad94cc77
...
...
@@ -105,4 +105,11 @@ public class BizConfig extends RefreshableConfig {
return
TimeUnit
.
SECONDS
;
}
public
int
releaseMessageNotificationBatch
()
{
return
getIntProperty
(
"apollo.release-message.notification.batch"
,
100
);
}
public
int
releaseMessageNotificationBatchIntervalInMilli
()
{
return
getIntProperty
(
"apollo.release-message.notification.batch.interval"
,
100
);
}
}
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/NotificationControllerV2.java
浏览文件 @
ad94cc77
...
...
@@ -12,6 +12,7 @@ import com.google.common.collect.Sets;
import
com.google.gson.Gson
;
import
com.google.gson.reflect.TypeToken
;
import
com.ctrip.framework.apollo.biz.config.BizConfig
;
import
com.ctrip.framework.apollo.biz.entity.ReleaseMessage
;
import
com.ctrip.framework.apollo.biz.message.ReleaseMessageListener
;
import
com.ctrip.framework.apollo.biz.message.Topics
;
...
...
@@ -22,6 +23,7 @@ import com.ctrip.framework.apollo.configservice.util.NamespaceUtil;
import
com.ctrip.framework.apollo.configservice.util.WatchKeysUtil
;
import
com.ctrip.framework.apollo.core.ConfigConsts
;
import
com.ctrip.framework.apollo.core.dto.ApolloConfigNotification
;
import
com.ctrip.framework.apollo.core.utils.ApolloThreadFactory
;
import
com.ctrip.framework.apollo.tracer.Tracer
;
import
org.slf4j.Logger
;
...
...
@@ -41,6 +43,9 @@ import java.util.Collection;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.TimeUnit
;
/**
* @author Jason Song(song_s@ctrip.com)
...
...
@@ -61,6 +66,8 @@ public class NotificationControllerV2 implements ReleaseMessageListener {
new
TypeToken
<
List
<
ApolloConfigNotification
>>()
{
}.
getType
();
private
final
ExecutorService
largeNotificationBatchExecutorService
;
@Autowired
private
WatchKeysUtil
watchKeysUtil
;
...
...
@@ -76,6 +83,14 @@ public class NotificationControllerV2 implements ReleaseMessageListener {
@Autowired
private
Gson
gson
;
@Autowired
private
BizConfig
bizConfig
;
public
NotificationControllerV2
()
{
largeNotificationBatchExecutorService
=
Executors
.
newSingleThreadExecutor
(
ApolloThreadFactory
.
create
(
"NotificationControllerV2"
,
true
));
}
@RequestMapping
(
method
=
RequestMethod
.
GET
)
public
DeferredResult
<
ResponseEntity
<
List
<
ApolloConfigNotification
>>>
pollNotification
(
@RequestParam
(
value
=
"appId"
)
String
appId
,
...
...
@@ -220,6 +235,27 @@ public class NotificationControllerV2 implements ReleaseMessageListener {
//create a new list to avoid ConcurrentModificationException
List
<
DeferredResult
<
ResponseEntity
<
List
<
ApolloConfigNotification
>>>>
results
=
Lists
.
newArrayList
(
deferredResults
.
get
(
content
));
//do async notification if too many clients
if
(
results
.
size
()
>
bizConfig
.
releaseMessageNotificationBatch
())
{
largeNotificationBatchExecutorService
.
submit
(()
->
{
logger
.
debug
(
"Async notify {} clients for key {} with batch {}"
,
results
.
size
(),
content
,
bizConfig
.
releaseMessageNotificationBatch
());
for
(
int
i
=
0
;
i
<
results
.
size
();
i
++)
{
if
(
i
>
0
&&
i
%
bizConfig
.
releaseMessageNotificationBatch
()
==
0
)
{
try
{
TimeUnit
.
MILLISECONDS
.
sleep
(
bizConfig
.
releaseMessageNotificationBatchIntervalInMilli
());
}
catch
(
InterruptedException
e
)
{
//ignore
}
}
logger
.
debug
(
"Async notify {}"
,
results
.
get
(
i
));
results
.
get
(
i
).
setResult
(
notification
);
}
});
return
;
}
logger
.
debug
(
"Notify {} clients for key {}"
,
results
.
size
(),
content
);
for
(
DeferredResult
<
ResponseEntity
<
List
<
ApolloConfigNotification
>>>
result
:
results
)
{
...
...
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/controller/NotificationControllerV2Test.java
浏览文件 @
ad94cc77
...
...
@@ -7,6 +7,7 @@ import com.google.common.collect.Multimap;
import
com.google.common.collect.Sets
;
import
com.google.gson.Gson
;
import
com.ctrip.framework.apollo.biz.config.BizConfig
;
import
com.ctrip.framework.apollo.biz.entity.ReleaseMessage
;
import
com.ctrip.framework.apollo.biz.message.Topics
;
import
com.ctrip.framework.apollo.biz.utils.EntityManagerUtil
;
...
...
@@ -27,6 +28,7 @@ import org.springframework.test.util.ReflectionTestUtils;
import
org.springframework.web.context.request.async.DeferredResult
;
import
java.util.List
;
import
java.util.concurrent.TimeUnit
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
...
...
@@ -57,6 +59,9 @@ public class NotificationControllerV2Test {
private
NamespaceUtil
namespaceUtil
;
@Mock
private
WatchKeysUtil
watchKeysUtil
;
@Mock
private
BizConfig
bizConfig
;
private
Gson
gson
;
private
Multimap
<
String
,
DeferredResult
<
ResponseEntity
<
List
<
ApolloConfigNotification
>>>>
...
...
@@ -66,11 +71,16 @@ public class NotificationControllerV2Test {
public
void
setUp
()
throws
Exception
{
controller
=
new
NotificationControllerV2
();
gson
=
new
Gson
();
when
(
bizConfig
.
releaseMessageNotificationBatch
()).
thenReturn
(
100
);
when
(
bizConfig
.
releaseMessageNotificationBatchIntervalInMilli
()).
thenReturn
(
5
);
ReflectionTestUtils
.
setField
(
controller
,
"releaseMessageService"
,
releaseMessageService
);
ReflectionTestUtils
.
setField
(
controller
,
"entityManagerUtil"
,
entityManagerUtil
);
ReflectionTestUtils
.
setField
(
controller
,
"namespaceUtil"
,
namespaceUtil
);
ReflectionTestUtils
.
setField
(
controller
,
"watchKeysUtil"
,
watchKeysUtil
);
ReflectionTestUtils
.
setField
(
controller
,
"gson"
,
gson
);
ReflectionTestUtils
.
setField
(
controller
,
"bizConfig"
,
bizConfig
);
someAppId
=
"someAppId"
;
someCluster
=
"someCluster"
;
...
...
@@ -283,6 +293,48 @@ public class NotificationControllerV2Test {
assertEquals
(
someId
,
notification
.
getNotificationId
());
}
@Test
public
void
testPollNotificationWithHandleMessageInBatch
()
throws
Exception
{
String
someWatchKey
=
Joiner
.
on
(
ConfigConsts
.
CLUSTER_NAMESPACE_SEPARATOR
)
.
join
(
someAppId
,
someCluster
,
defaultNamespace
);
int
someBatch
=
1
;
int
someBatchInterval
=
10
;
Multimap
<
String
,
String
>
watchKeysMap
=
assembleMultiMap
(
defaultNamespace
,
Lists
.
newArrayList
(
someWatchKey
));
String
notificationAsString
=
transformApolloConfigNotificationsToString
(
defaultNamespace
,
someNotificationId
);
when
(
watchKeysUtil
.
assembleAllWatchKeys
(
someAppId
,
someCluster
,
Sets
.
newHashSet
(
defaultNamespace
),
someDataCenter
)).
thenReturn
(
watchKeysMap
);
when
(
bizConfig
.
releaseMessageNotificationBatch
()).
thenReturn
(
someBatch
);
when
(
bizConfig
.
releaseMessageNotificationBatchIntervalInMilli
()).
thenReturn
(
someBatchInterval
);
DeferredResult
<
ResponseEntity
<
List
<
ApolloConfigNotification
>>>
deferredResult
=
controller
.
pollNotification
(
someAppId
,
someCluster
,
notificationAsString
,
someDataCenter
,
someClientIp
);
DeferredResult
<
ResponseEntity
<
List
<
ApolloConfigNotification
>>>
anotherDeferredResult
=
controller
.
pollNotification
(
someAppId
,
someCluster
,
notificationAsString
,
someDataCenter
,
someClientIp
);
long
someId
=
1
;
ReleaseMessage
someReleaseMessage
=
new
ReleaseMessage
(
someWatchKey
);
someReleaseMessage
.
setId
(
someId
);
controller
.
handleMessage
(
someReleaseMessage
,
Topics
.
APOLLO_RELEASE_TOPIC
);
assertTrue
(!
anotherDeferredResult
.
hasResult
());
TimeUnit
.
MILLISECONDS
.
sleep
(
someBatchInterval
*
3
);
assertTrue
(
anotherDeferredResult
.
hasResult
());
}
private
String
transformApolloConfigNotificationsToString
(
String
namespace
,
long
notificationId
)
{
List
<
ApolloConfigNotification
>
notifications
=
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录