Skip to content

Commit

Permalink
Merge pull request #762 from chickenlj:bugfix#685_registrycache
Browse files Browse the repository at this point in the history
* Optimize(AbstractRegistry) registry cache, one registry file per 'registry & application', avoid reload (#685).

* Fixed #692 optimize zookeeper operation in service export/reference process: check already exists before create
  • Loading branch information
chickenlj authored Oct 28, 2017
1 parent 209aecf commit be965bf
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public AbstractRegistry(URL url) {
setUrl(url);
// 启动文件保存定时器
syncSaveFile = url.getParameter(Constants.REGISTRY_FILESAVE_SYNC_KEY, false);
String filename = url.getParameter(Constants.FILE_KEY, System.getProperty("user.home") + "/.dubbo/dubbo-registry-" + url.getHost() + ".cache");
String filename = url.getParameter(Constants.FILE_KEY, System.getProperty("user.home") + "/.dubbo/dubbo-registry-" + url.getParameter(Constants.APPLICATION_KEY) + "-" + url.getAddress() + ".cache");
File file = null;
if (ConfigUtils.isNotEmpty(filename)) {
file = new File(filename);
Expand Down Expand Up @@ -149,28 +149,8 @@ public void doSaveProperties(long version) {
if (file == null) {
return;
}
Properties newProperties = new Properties();
// 保存之前先读取一遍,防止多个注册中心之间冲突
InputStream in = null;
try {
if (file.exists()) {
in = new FileInputStream(file);
newProperties.load(in);
}
} catch (Throwable e) {
logger.warn("Failed to load registry store file, cause: " + e.getMessage(), e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
logger.warn(e.getMessage(), e);
}
}
}
// 保存
try {
newProperties.putAll(properties);
File lockfile = new File(file.getAbsolutePath() + ".lock");
if (!lockfile.exists()) {
lockfile.createNewFile();
Expand All @@ -190,7 +170,7 @@ public void doSaveProperties(long version) {
}
FileOutputStream outputFile = new FileOutputStream(file);
try {
newProperties.store(outputFile, "Dubbo Registry Cache");
properties.store(outputFile, "Dubbo Registry Cache");
} finally {
outputFile.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ public List<String> getChildren(String path) {
}
}

public boolean checkExists(String path) {
try {
if (client.checkExists().forPath(path) != null) {
return true;
}
} catch (Exception e) {
}
return false;
}
public boolean isConnected() {
return client.getZookeeperClient().isConnected();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public URL getUrl() {
public void create(String path, boolean ephemeral) {
int i = path.lastIndexOf('/');
if (i > 0) {
create(path.substring(0, i), false);
String parentPath = path.substring(0, i);
if (!checkExists(parentPath)) {
create(parentPath, false);
}
}
if (ephemeral) {
createEphemeral(path);
Expand Down Expand Up @@ -105,6 +108,8 @@ public void close() {

protected abstract void createEphemeral(String path);

protected abstract boolean checkExists(String path);

protected abstract TargetChildListener createTargetChildListener(String path, ChildListener listener);

protected abstract List<String> addTargetChildListener(String path, TargetChildListener listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public List<String> getChildren(String path) {
}
}

public boolean checkExists(String path) {
try {
return client.exists(path);
} catch (Throwable t) {
}
return false;
}

public boolean isConnected() {
return state == KeeperState.SyncConnected;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.alibaba.dubbo.remoting.zookeeper.curator;

import com.alibaba.dubbo.common.URL;

import org.junit.Assert;
import org.junit.Test;

/**
* @author ken.lj
* @date 2017/10/16
*/
public class CuratorZookeeperClientTest {

@Test
public void testCheckExists() {
CuratorZookeeperClient curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService"));
String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers";
curatorClient.create(path, false);
Assert.assertTrue(curatorClient.checkExists(path));
Assert.assertFalse(curatorClient.checkExists(path + "/noneexits"));
}

/**
* create checkExists 性能測試
*/
@Test
public void testCreate() {
CuratorZookeeperClient curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService"));
String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers";
curatorClient.create(path, false);

// 重复create 100次,耗时
long startTime = System.nanoTime();
for (int i = 0; i < 100; i++) {
curatorClient.create(path, true);
}
System.out.println("create cost: " + (System.nanoTime() - startTime) / 1000 / 1000);

// 判断100次,耗时
startTime = System.nanoTime();
for (int i = 0; i < 100; i++) {
curatorClient.checkExists(path);
}
System.out.println("judge cost: " + (System.nanoTime() - startTime) / 1000 / 1000);
}
}

1 comment on commit be965bf

@haodut
Copy link
Contributor

@haodut haodut commented on be965bf Nov 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String filename = url.getParameter(Constants.FILE_KEY, System.getProperty("user.home") + "/.dubbo/dubbo-registry-" + url.getParameter(Constants.APPLICATION_KEY) + "-" + url.getAddress() + ".cache");

这里使用了 url.getAddress(),可能会含有":",导致文件名不全,应该将":"替换为其它字符,或者直接使用url.getHost()。

Please sign in to comment.