Skip to content

Commit

Permalink
Merge pull request #1176 from magicdoom/1.6
Browse files Browse the repository at this point in the history
fix zk npe 优化zk写入
  • Loading branch information
magicdoom committed Oct 28, 2016
2 parents 3792945 + c9a5abf commit 460930b
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/main/java/io/mycat/MycatServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -440,24 +440,25 @@ public Thread newThread(Runnable r) {

RouteStrategyFactory.init();
// new Thread(tableStructureCheck()).start();

//首次启动如果发现zk上dnindex为空,则将本地初始化上zk
try {
File file = new File(SystemConfig.getHomePath(), "conf" + File.separator + "dnindex.properties");
dnindexLock.acquire(30,TimeUnit.SECONDS) ;
String path = ZKUtils.getZKBasePath() + "bindata/dnindex.properties";
CuratorFramework zk = ZKUtils.getConnection();
if(zk.checkExists().forPath(path)==null) {
zk.create().creatingParentsIfNeeded().forPath(path, Files.toByteArray(file));
}

} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if(isUseZkSwitch()) {
//首次启动如果发现zk上dnindex为空,则将本地初始化上zk
try {
dnindexLock.release();
File file = new File(SystemConfig.getHomePath(), "conf" + File.separator + "dnindex.properties");
dnindexLock.acquire(30, TimeUnit.SECONDS);
String path = ZKUtils.getZKBasePath() + "bindata/dnindex.properties";
CuratorFramework zk = ZKUtils.getConnection();
if (zk.checkExists().forPath(path) == null) {
zk.create().creatingParentsIfNeeded().forPath(path, Files.toByteArray(file));
}

} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
dnindexLock.release();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
Expand Down Expand Up @@ -623,9 +624,11 @@ public synchronized void saveDataHostIndex(String dataHost, int curIndex) {
ByteArrayOutputStream out=new ByteArrayOutputStream();
Properties properties=new Properties();
properties.load(new ByteArrayInputStream(data));
properties.setProperty(dataHost,String.valueOf(curIndex)) ;
properties.store(out,"update");
zk.setData().forPath(path,out.toByteArray());
if(!String.valueOf(curIndex).equals(properties.getProperty(dataHost))) {
properties.setProperty(dataHost, String.valueOf(curIndex));
properties.store(out, "update");
zk.setData().forPath(path, out.toByteArray());
}
}

}finally {
Expand Down

0 comments on commit 460930b

Please sign in to comment.