Skip to content

Commit

Permalink
Merge branch 'dev' of ssh://github.com/shardingjdbc/sharding-jdbc int…
Browse files Browse the repository at this point in the history
…o dev
  • Loading branch information
tristaZero committed Jul 19, 2018
2 parents cab8497 + 48aed45 commit 56f4ea5
Show file tree
Hide file tree
Showing 49 changed files with 353 additions and 410 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class ParsingResultCache {

private static final ParsingResultCache INSTANCE = new ParsingResultCache();

private volatile Map<String, SQLStatement> cache = new WeakHashMap<>(65535, 1);
private final Map<String, SQLStatement> cache = new WeakHashMap<>(65535, 1);

/**
* Get parsing result cache instance.
Expand Down
4 changes: 2 additions & 2 deletions sharding-core/src/test/resources/parser/drop.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@

<parser-result sql-case-id="drop_index_without_on">
<tokens>
<index-token begin-position="11" original-literals="t_log_index" table-name="" />
<index-token begin-position="11" original-literals="order_index" table-name="" />
</tokens>
</parser-result>

<parser-result sql-case-id="drop_index_if_exists">
<tokens>
<index-token begin-position="21" original-literals="t_order_index" table-name="" />
<index-token begin-position="21" original-literals="order_index" table-name="" />
</tokens>
</parser-result>
</parser-result-sets>
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
import io.shardingsphere.core.parsing.lexer.LexerEngine;
import io.shardingsphere.core.parsing.lexer.token.Assist;
import io.shardingsphere.core.parsing.lexer.token.Symbol;
import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.ZookeeperConstants;
import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil;
import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.ZookeeperConstants;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;

import java.util.Iterator;
import java.util.Map;
Expand All @@ -37,10 +36,9 @@
*
* @author lidongbo
*/
@Slf4j
public final class PathNode {

private static final Logger LOGGER = LoggerFactory.getLogger(PathNode.class);

private final Map<String, PathNode> children = new ConcurrentHashMap<>();

private final String nodeKey;
Expand Down Expand Up @@ -93,10 +91,10 @@ public void attachChild(final PathNode node) {

PathNode set(final Iterator<String> iterator, final String value) {
String key = iterator.next();
LOGGER.debug("PathNode set:{},value:{}", key, value);
log.debug("PathNode set:{},value:{}", key, value);
PathNode result = children.get(key);
if (result == null) {
LOGGER.debug("set children haven't:{}", key);
log.debug("set children haven't:{}", key);
result = new PathNode(key);
children.put(key, result);
}
Expand All @@ -110,10 +108,10 @@ PathNode set(final Iterator<String> iterator, final String value) {

PathNode get(final Iterator<String> iterator) {
String key = iterator.next();
LOGGER.debug("get:{}", key);
log.debug("get:{}", key);
PathNode result = children.get(key);
if (result == null) {
LOGGER.debug("get children haven't:{}", key);
log.debug("get children haven't:{}", key);
return null;
}
if (iterator.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.ZookeeperConstants;
import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.UsualClient;
import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.ZookeeperEventListener;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.common.PathUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand All @@ -35,23 +42,15 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import lombok.Getter;
import lombok.Setter;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.common.PathUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
* Zookeeper cache tree.
*
* @author lidongbo
*/
@Slf4j
public final class PathTree {

private static final Logger LOGGER = LoggerFactory.getLogger(PathTree.class);

private final transient ReentrantLock lock = new ReentrantLock();

private final AtomicReference<PathNode> rootNode = new AtomicReference<>();
Expand Down Expand Up @@ -92,23 +91,23 @@ public void load() throws KeeperException, InterruptedException {
}
try {
if (status == PathStatus.RELEASE) {
LOGGER.debug("loading status:{}", status);
this.setStatus(PathStatus.CHANGING);
log.debug("loading status:{}", status);
status = PathStatus.CHANGING;

PathNode newRoot = new PathNode(rootNode.get().getKey());
List<String> children = provider.getChildren(rootNode.get().getKey());
children.remove(PathUtil.getRealPath(rootNode.get().getKey(), ZookeeperConstants.CHANGING_KEY));
this.attechIntoNode(children, newRoot);
attachIntoNode(children, newRoot);
rootNode.set(newRoot);
this.setStatus(PathStatus.RELEASE);
LOGGER.debug("loading release:{}", status);

status = PathStatus.RELEASE;
log.debug("loading release:{}", status);
} else {
LOGGER.info("loading but cache status not release");
log.info("loading but cache status not release");
try {
Thread.sleep(10L);
} catch (final InterruptedException ex) {
LOGGER.error("loading sleep error:{}", ex.getMessage(), ex);
log.error("loading sleep error:{}", ex.getMessage(), ex);
}
load();
}
Expand All @@ -117,21 +116,21 @@ public void load() throws KeeperException, InterruptedException {
}
}

private void attechIntoNode(final List<String> children, final PathNode pathNode) throws KeeperException, InterruptedException {
private void attachIntoNode(final List<String> children, final PathNode pathNode) throws KeeperException, InterruptedException {
if (closed) {
return;
}
LOGGER.debug("attechIntoNode children:{}", children);
log.debug("attechIntoNode children:{}", children);
if (children.isEmpty()) {
LOGGER.info("attechIntoNode there are no children");
log.info("attechIntoNode there are no children");
return;
}
for (String each : children) {
String childPath = PathUtil.getRealPath(pathNode.getPath(), each);
PathNode current = new PathNode(PathUtil.checkPath(each), provider.getData(childPath));
pathNode.attachChild(current);
List<String> subs = provider.getChildren(childPath);
this.attechIntoNode(subs, current);
attachIntoNode(subs, current);
}
}

Expand All @@ -154,19 +153,19 @@ public void refreshPeriodic(final long period) {
if (threadPeriod < 1) {
threadPeriod = ZookeeperConstants.THREAD_PERIOD;
}
LOGGER.debug("refreshPeriodic:{}", period);
log.debug("refreshPeriodic:{}", period);
cacheService = Executors.newSingleThreadScheduledExecutor();
cacheService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
LOGGER.debug("cacheService run:{}", getStatus());
log.debug("cacheService run:{}", getStatus());
if (PathStatus.RELEASE == getStatus()) {
try {
load();
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
LOGGER.error(ex.getMessage(), ex);
log.error(ex.getMessage(), ex);
}
}
}
Expand All @@ -189,7 +188,7 @@ public void run() {
public void stopRefresh() {
cacheService.shutdown();
executorStart = false;
LOGGER.debug("stopRefresh");
log.debug("stopRefresh");
}

/**
Expand All @@ -200,7 +199,7 @@ public void watch() {
@Override
public void process(final WatchedEvent event) {
String path = event.getPath();
LOGGER.debug("PathTree Watch event:{}", event.toString());
log.debug("PathTree Watch event:{}", event.toString());
switch (event.getType()) {
case NodeCreated:
case NodeDataChanged:
Expand All @@ -227,12 +226,12 @@ public void watch(final ZookeeperEventListener zookeeperEventListener) {
return;
}
final String key = zookeeperEventListener.getKey();
LOGGER.debug("PathTree Watch:{}", key);
log.debug("PathTree Watch:{}", key);
client.registerWatch(rootNode.get().getKey(), zookeeperEventListener);
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
LOGGER.debug("PathTree Unregister Watch:{}", key);
log.debug("PathTree Unregister Watch:{}", key);
client.unregisterWatch(key);
}
}));
Expand All @@ -248,7 +247,7 @@ private void processNodeChange(final String path) {
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
LOGGER.error("PathTree put error : " + ex.getMessage());
log.error("PathTree put error : " + ex.getMessage());
}
}

Expand Down Expand Up @@ -277,7 +276,7 @@ public byte[] getValue(final String path) {

private Iterator<String> keyIterator(final String path) {
List<String> nodes = PathUtil.getShortPathNodes(path);
LOGGER.debug("keyIterator path{},nodes:{}", path, nodes);
log.debug("keyIterator path{},nodes:{}", path, nodes);
Iterator<String> iterator = nodes.iterator();
// root
iterator.next();
Expand All @@ -297,11 +296,11 @@ public List<String> getChildren(final String path) {
PathNode node = get(path);
List<String> result = new ArrayList<>();
if (node == null) {
LOGGER.info("getChildren null");
log.info("getChildren null");
return result;
}
if (node.getChildren().isEmpty()) {
LOGGER.info("getChildren no child");
log.info("getChildren no child");
return result;
}
Iterator<PathNode> children = node.getChildren().values().iterator();
Expand All @@ -312,7 +311,7 @@ public List<String> getChildren(final String path) {
}

private PathNode get(final String path) {
LOGGER.debug("PathTree get:{}", path);
log.debug("PathTree get:{}", path);
String realPath = provider.getRealPath(path);
if (realPath.equals(rootNode.get().getKey())) {
return rootNode.get();
Expand All @@ -322,7 +321,7 @@ private PathNode get(final String path) {
if (iterator.hasNext()) {
return rootNode.get().get(iterator);
}
LOGGER.debug("{} not exist", realPath);
log.debug("{} not exist", realPath);
return null;
}

Expand All @@ -339,9 +338,9 @@ public void put(final String path, final String value) {
return;
}
try {
LOGGER.debug("cache put:{},value:{}", path, value);
log.debug("cache put:{},value:{}", path, value);
PathUtils.validatePath(path);
LOGGER.debug("put status:{}", status);
log.debug("put status:{}", status);
if (status == PathStatus.RELEASE) {
if (path.equals(rootNode.get().getKey())) {
rootNode.set(new PathNode(rootNode.get().getKey(), value.getBytes(ZookeeperConstants.UTF_8)));
Expand All @@ -352,10 +351,10 @@ public void put(final String path, final String value) {
this.setStatus(PathStatus.RELEASE);
} else {
try {
LOGGER.debug("put but cache status not release");
log.debug("put but cache status not release");
Thread.sleep(10L);
} catch (final InterruptedException ex) {
LOGGER.error("put sleep error:{}", ex.getMessage(), ex);
log.error("put sleep error:{}", ex.getMessage(), ex);
}
put(path, value);
}
Expand All @@ -370,7 +369,7 @@ public void put(final String path, final String value) {
* @param path path
*/
public void delete(final String path) {
LOGGER.debug("PathTree begin delete:{}", path);
log.debug("PathTree begin delete:{}", path);
final ReentrantLock lock = this.lock;
lock.lock();
if (closed) {
Expand All @@ -391,7 +390,7 @@ public void delete(final String path) {
lexerEngine.skipIfEqual(Symbol.SLASH);
}
rootNode.get().delete(lexerEngine.getCurrentToken().getLiterals(), lexerEngine);
LOGGER.debug("PathTree end delete:{}", path);
log.debug("PathTree end delete:{}", path);
} finally {
lock.unlock();
}
Expand All @@ -412,7 +411,7 @@ public void close() {
// CHECKSTYLE:OFF
} catch (final Exception ex){
// CHECKSTYLE:ON
LOGGER.warn("PathTree close:{}", ex.getMessage());
log.warn("PathTree close:{}", ex.getMessage());
} finally {
lock.unlock();
}
Expand Down
Loading

0 comments on commit 56f4ea5

Please sign in to comment.