Skip to content

Commit

Permalink
[3.0] Optimize MigrationRuleListener (#8071)
Browse files Browse the repository at this point in the history
* optimize MigrationRuleListener

* remove unnecessary rwlock
  • Loading branch information
kevinw66 committed Jun 21, 2021
1 parent 3d3f974 commit f004643
Showing 1 changed file with 11 additions and 8 deletions.
Expand Up @@ -65,12 +65,12 @@ public MigrationRuleListener() {
if (StringUtils.isEmpty(rawRule)) {
rawRule = INIT;
}
this.rawRule = rawRule;
setRawRule(rawRule);
} else {
if (logger.isWarnEnabled()) {
logger.warn("Using default configuration rule because config center is not configured!");
}
rawRule = INIT;
setRawRule(INIT);
}

String localRawRule = ApplicationModel.getEnvironment().getLocalMigrationRule();
Expand Down Expand Up @@ -101,7 +101,7 @@ private int getDelay() {

@Override
public synchronized void process(ConfigChangedEvent event) {
rawRule = event.getContent();
String rawRule = event.getContent();
if (StringUtils.isEmpty(rawRule)) {
logger.warn("Received empty migration rule, will ignore.");
return;
Expand All @@ -110,13 +110,18 @@ public synchronized void process(ConfigChangedEvent event) {
logger.info("Using the following migration rule to migrate:");
logger.info(rawRule);

rule = parseRule(rawRule);
setRawRule(rawRule);

if (CollectionUtils.isNotEmptyMap(handlers)) {
handlers.forEach((_key, handler) -> handler.doMigrate(rule));
}
}

public void setRawRule(String rawRule) {
this.rawRule = rawRule;
this.rule = parseRule(this.rawRule);
}

private MigrationRule parseRule(String rawRule) {
MigrationRule tmpRule = rule;
if (INIT.equals(rawRule)) {
Expand All @@ -132,19 +137,17 @@ private MigrationRule parseRule(String rawRule) {
}

@Override
public synchronized void onExport(RegistryProtocol registryProtocol, Exporter<?> exporter) {
public void onExport(RegistryProtocol registryProtocol, Exporter<?> exporter) {

}

@Override
public synchronized void onRefer(RegistryProtocol registryProtocol, ClusterInvoker<?> invoker, URL consumerUrl, URL registryURL) {
public void onRefer(RegistryProtocol registryProtocol, ClusterInvoker<?> invoker, URL consumerUrl, URL registryURL) {
MigrationRuleHandler<?> migrationRuleHandler = handlers.computeIfAbsent((MigrationInvoker<?>) invoker, _key -> {
((MigrationInvoker<?>) invoker).setMigrationRuleListener(this);
return new MigrationRuleHandler<>((MigrationInvoker<?>) invoker, consumerUrl);
});

rule = parseRule(rawRule);

migrationRuleHandler.doMigrate(rule);
}

Expand Down

0 comments on commit f004643

Please sign in to comment.