Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2
211010717
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Operate
Terraform modules
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Summer2021
211010717
Commits
80573c9d
Unverified
Commit
80573c9d
authored
4 years ago
by
曹华栋
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
optimize: optimize fileListener to decrease cpu time usage (#3222)
parent
ce41bce9
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
config/seata-config-core/src/main/java/io/seata/config/FileConfiguration.java
+31
-20
31 additions, 20 deletions
...core/src/main/java/io/seata/config/FileConfiguration.java
with
31 additions
and
20 deletions
config/seata-config-core/src/main/java/io/seata/config/FileConfiguration.java
+
31
−
20
View file @
80573c9d
...
...
@@ -20,6 +20,7 @@ import java.io.UnsupportedEncodingException;
import
java.net.URL
;
import
java.net.URLDecoder
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.concurrent.ConcurrentHashMap
;
...
...
@@ -73,6 +74,8 @@ public class FileConfiguration extends AbstractConfiguration {
private
final
String
name
;
private
final
FileListener
fileListener
=
new
FileListener
();
private
final
boolean
allowDynamicRefresh
;
/**
...
...
@@ -230,8 +233,7 @@ public class FileConfiguration extends AbstractConfiguration {
listenedConfigMap
.
put
(
dataId
,
ConfigurationFactory
.
getInstance
().
getConfig
(
dataId
));
// Start config change listener for the dataId.
FileListener
fileListener
=
new
FileListener
(
dataId
,
listener
);
fileListener
.
onProcessEvent
(
new
ConfigurationChangeEvent
());
fileListener
.
addListener
(
dataId
,
listener
);
}
@Override
...
...
@@ -333,8 +335,8 @@ public class FileConfiguration extends AbstractConfiguration {
*/
class
FileListener
implements
ConfigurationChangeListener
{
private
final
String
dataId
;
private
final
ConfigurationChangeListener
listener
;
private
final
Map
<
String
,
Set
<
ConfigurationChangeListener
>>
dataIdMap
=
new
HashMap
<>()
;
private
final
ExecutorService
executor
=
new
ThreadPoolExecutor
(
CORE_LISTENER_THREAD
,
MAX_LISTENER_THREAD
,
0L
,
TimeUnit
.
MILLISECONDS
,
new
LinkedBlockingQueue
<>(),
new
NamedThreadFactory
(
"fileListener"
,
MAX_LISTENER_THREAD
));
...
...
@@ -342,30 +344,39 @@ public class FileConfiguration extends AbstractConfiguration {
/**
* Instantiates a new FileListener.
*
* @param dataId the data id
* @param listener the listener
*/
public
FileListener
(
String
dataId
,
ConfigurationChangeListener
listener
)
{
this
.
dataId
=
dataId
;
this
.
listener
=
listener
;
FileListener
()
{}
public
synchronized
void
addListener
(
String
dataId
,
ConfigurationChangeListener
listener
)
{
// only the first time add listener will trigger on process event
if
(
dataIdMap
.
isEmpty
())
{
fileListener
.
onProcessEvent
(
new
ConfigurationChangeEvent
());
}
dataIdMap
.
computeIfAbsent
(
dataId
,
value
->
new
HashSet
<>()).
add
(
listener
);
}
@Override
public
void
onChangeEvent
(
ConfigurationChangeEvent
event
)
{
while
(
true
)
{
try
{
String
currentConfig
=
ConfigurationFactory
.
getInstance
().
getLatestConfig
(
dataId
,
null
,
DEFAULT_CONFIG_TIMEOUT
);
if
(
StringUtils
.
isNotBlank
(
currentConfig
))
{
String
oldConfig
=
listenedConfigMap
.
get
(
dataId
);
if
(
ObjectUtils
.
notEqual
(
currentConfig
,
oldConfig
))
{
listenedConfigMap
.
put
(
dataId
,
currentConfig
);
event
.
setDataId
(
dataId
).
setNewValue
(
currentConfig
).
setOldValue
(
oldConfig
);
listener
.
onChangeEvent
(
event
);
for
(
String
dataId
:
dataIdMap
.
keySet
())
{
try
{
String
currentConfig
=
ConfigurationFactory
.
getInstance
().
getLatestConfig
(
dataId
,
null
,
DEFAULT_CONFIG_TIMEOUT
);
if
(
StringUtils
.
isNotBlank
(
currentConfig
))
{
String
oldConfig
=
listenedConfigMap
.
get
(
dataId
);
if
(
ObjectUtils
.
notEqual
(
currentConfig
,
oldConfig
))
{
listenedConfigMap
.
put
(
dataId
,
currentConfig
);
event
.
setDataId
(
dataId
).
setNewValue
(
currentConfig
).
setOldValue
(
oldConfig
);
for
(
ConfigurationChangeListener
listener
:
dataIdMap
.
get
(
dataId
))
{
listener
.
onChangeEvent
(
event
);
}
}
}
}
catch
(
Exception
exx
)
{
LOGGER
.
error
(
"fileListener execute error, dataId :{}"
,
dataId
,
exx
);
}
}
catch
(
Exception
exx
)
{
LOGGER
.
error
(
"fileListener execute error:{}"
,
exx
.
getMessage
());
}
try
{
Thread
.
sleep
(
LISTENER_CONFIG_INTERVAL
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment