Commit a7b94cfe authored by terrymanu's avatar terrymanu
Browse files

checkstyle for newzk

parent 3f365ffd
Loading
Loading
Loading
Loading
+34 −38
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
package io.shardingsphere.jdbc.orchestration.reg.newzk;

import com.google.common.base.Charsets;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import io.shardingsphere.jdbc.orchestration.reg.api.RegistryCenter;
import io.shardingsphere.jdbc.orchestration.reg.exception.RegExceptionHandler;
@@ -31,6 +32,11 @@ import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFac
import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.StrategyType;
import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.ZookeeperEventListener;
import io.shardingsphere.jdbc.orchestration.reg.zookeeper.ZookeeperConfiguration;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.ZooDefs;

import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
@@ -39,10 +45,6 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.ZooDefs;

/**
 * Zookeeper native based registry center.
@@ -53,62 +55,60 @@ public final class NewZookeeperRegistryCenter implements RegistryCenter {
    
    private final IClient client;
    
    private final Map<String, PathTree> caches = new HashMap<>();
    private final Map<String, PathTree> caches;
    
    public NewZookeeperRegistryCenter(final ZookeeperConfiguration zkConfig) {
        final ClientFactory creator = buildCreator(zkConfig);
        client = initClient(creator, zkConfig);
        client = initClient(buildClientFactory(zkConfig), zkConfig);
        caches = new HashMap<>();
    }
    
    private ClientFactory buildCreator(final ZookeeperConfiguration zkConfig) {
        final ClientFactory creator = new ClientFactory();
        creator.setClientNamespace(zkConfig.getNamespace())
                .newClient(zkConfig.getServerLists(), zkConfig.getSessionTimeoutMilliseconds())
    private ClientFactory buildClientFactory(final ZookeeperConfiguration zkConfig) {
        ClientFactory result = new ClientFactory();
        result.setClientNamespace(zkConfig.getNamespace()).newClient(zkConfig.getServerLists(), zkConfig.getSessionTimeoutMilliseconds())
                .setRetryPolicy(new DelayRetryPolicy(zkConfig.getBaseSleepTimeMilliseconds(), zkConfig.getMaxRetries(), zkConfig.getMaxSleepTimeMilliseconds()));
        if (!Strings.isNullOrEmpty(zkConfig.getDigest())) {
            creator.authorization("digest", zkConfig.getDigest().getBytes(Charsets.UTF_8), ZooDefs.Ids.CREATOR_ALL_ACL);
            result.authorization("digest", zkConfig.getDigest().getBytes(Charsets.UTF_8), ZooDefs.Ids.CREATOR_ALL_ACL);
        }
        return creator;
        return result;
    }
    
    private IClient initClient(final ClientFactory creator, final ZookeeperConfiguration zkConfig) {
        IClient newClient = null;
    private IClient initClient(final ClientFactory clientFactory, final ZookeeperConfiguration zkConfig) {
        IClient result = null;
        try {
            // todo There is a bug when the start time is very short, and I haven't found the reason yet
            // newClient = creator.start(zkConfig.getMaxSleepTimeMilliseconds() * zkConfig.getMaxRetries(), TimeUnit.MILLISECONDS);
            newClient = creator.start();
            if (!newClient.blockUntilConnected(zkConfig.getMaxSleepTimeMilliseconds() * zkConfig.getMaxRetries(), TimeUnit.MILLISECONDS)) {
                newClient.close();
            // TODO There is a bug when the start time is very short, and I haven't found the reason yet
            // result = clientFactory.start(zkConfig.getMaxSleepTimeMilliseconds() * zkConfig.getMaxRetries(), TimeUnit.MILLISECONDS);
            result = clientFactory.start();
            if (!result.blockUntilConnected(zkConfig.getMaxSleepTimeMilliseconds() * zkConfig.getMaxRetries(), TimeUnit.MILLISECONDS)) {
                result.close();
                throw new KeeperException.OperationTimeoutException();
            }
            
            newClient.useExecStrategy(StrategyType.SYNC_RETRY);
            result.useExecStrategy(StrategyType.SYNC_RETRY);
        } catch (final KeeperException.OperationTimeoutException | IOException | InterruptedException ex) {
            RegExceptionHandler.handleException(ex);
        }
        return newClient;
        return result;
    }
    
    @Override
    public String get(final String key) {
        final PathTree cache = findTreeCache(key);
        if (null == cache) {
        Optional<PathTree> cache = findTreeCache(key);
        if (!cache.isPresent()) {
            return getDirectly(key);
        }
        byte[] resultInCache = cache.getValue(key);
        byte[] resultInCache = cache.get().getValue(key);
        if (null != resultInCache) {
            return null == resultInCache ? null : new String(resultInCache, Charsets.UTF_8);
            return new String(resultInCache, Charsets.UTF_8);
        }
        return getDirectly(key);
    }
    
    private PathTree findTreeCache(final String key) {
    private Optional<PathTree> findTreeCache(final String key) {
        for (Entry<String, PathTree> entry : caches.entrySet()) {
            if (key.startsWith(entry.getKey())) {
                return entry.getValue();
                return Optional.of(entry.getValue());
            }
        }
        return null;
        return Optional.absent();
    }
    
    @Override
@@ -185,22 +185,18 @@ public final class NewZookeeperRegistryCenter implements RegistryCenter {
    
    @Override
    public void watch(final String key, final EventListener eventListener) {
        final String path = key + "/";
        String path = key + "/";
        if (!caches.containsKey(path)) {
            addCacheData(key);
        }
        final PathTree cache = caches.get(path);
        PathTree cache = caches.get(path);
        cache.watch(new ZookeeperEventListener() {
            
            @Override
            public void process(final WatchedEvent event) {
                if (!Strings.isNullOrEmpty(event.getPath())) {
                    eventListener.onChange(new DataChangedEvent(getEventType(event), event.getPath(), getWithoutCache(event.getPath())));
                }
                    eventListener.onChange(new DataChangedEvent(extractEventType(event), event.getPath(), getWithoutCache(event.getPath())));
                }
            
            private DataChangedEvent.Type getEventType(final WatchedEvent event) {
                return extractEventType(event);
            }
        });
    }
@@ -230,7 +226,7 @@ public final class NewZookeeperRegistryCenter implements RegistryCenter {
    }
    
    private void addCacheData(final String cachePath) {
        final PathTree cache = new PathTree(cachePath, client);
        PathTree cache = new PathTree(cachePath, client);
        try {
            cache.load();
            cache.watch();
+3 −3
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@

package io.shardingsphere.jdbc.orchestration.reg.newzk.client.action;

/*
/**
 * Callback on contention has not reached.
 *
 * @author lidongbo
+25 −25
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ import org.apache.zookeeper.Watcher;

import java.util.List;

/*
/**
 * The basic actions of the client.
 *
 * @author lidongbo
@@ -36,8 +36,8 @@ public interface IAction {
     *
     * @param key key
     * @return data String
     * @throws KeeperException Zookeeper Exception
     * @throws InterruptedException InterruptedException
     * @throws KeeperException zookeeper exception
     * @throws InterruptedException interrupted exception
     */
    String getDataString(String key) throws KeeperException, InterruptedException;
    
@@ -46,8 +46,8 @@ public interface IAction {
     *
     * @param key key
     * @return data
     * @throws KeeperException Zookeeper Exception
     * @throws InterruptedException InterruptedException
     * @throws KeeperException zookeeper exception
     * @throws InterruptedException interrupted exception
     */
    byte[] getData(String key) throws KeeperException, InterruptedException;
    
@@ -56,9 +56,9 @@ public interface IAction {
     *
     * @param key key
     * @param callback callback
     * @param ctx ctx
     * @throws KeeperException Zookeeper Exception
     * @throws InterruptedException InterruptedException
     * @param ctx context
     * @throws KeeperException zookeeper exception
     * @throws InterruptedException interrupted exception
     */
    void getData(String key, AsyncCallback.DataCallback callback, Object ctx) throws KeeperException, InterruptedException;
    
@@ -66,9 +66,9 @@ public interface IAction {
     * Check exist.
     *
     * @param key key
     * @return exist
     * @throws KeeperException Zookeeper Exception
     * @throws InterruptedException InterruptedException
     * @return exist or not
     * @throws KeeperException zookeeper exception
     * @throws InterruptedException interrupted exception
     */
    boolean checkExists(String key) throws KeeperException, InterruptedException;
    
@@ -77,9 +77,9 @@ public interface IAction {
     *
     * @param key key
     * @param watcher watcher
     * @return exist
     * @throws KeeperException Zookeeper Exception
     * @throws InterruptedException InterruptedException
     * @return exist or not
     * @throws KeeperException zookeeper exception
     * @throws InterruptedException interrupted exception
     */
    boolean checkExists(String key, Watcher watcher) throws KeeperException, InterruptedException;
    
@@ -88,8 +88,8 @@ public interface IAction {
     *
     * @param key key
     * @return children keys
     * @throws KeeperException Zookeeper Exception
     * @throws InterruptedException InterruptedException
     * @throws KeeperException zookeeper exception
     * @throws InterruptedException interrupted exception
     */
    List<String> getChildren(String key) throws KeeperException, InterruptedException;
    
@@ -99,8 +99,8 @@ public interface IAction {
     * @param key key
     * @param value value
     * @param createMode createMode
     * @throws KeeperException Zookeeper Exception
     * @throws InterruptedException InterruptedException
     * @throws KeeperException zookeeper exception
     * @throws InterruptedException interrupted exception
     */
    void createCurrentOnly(String key, String value, CreateMode createMode) throws KeeperException, InterruptedException;
    
@@ -109,8 +109,8 @@ public interface IAction {
     *
     * @param key key
     * @param value value
     * @throws KeeperException Zookeeper Exception
     * @throws InterruptedException InterruptedException
     * @throws KeeperException zookeeper exception
     * @throws InterruptedException interrupted exception
     */
    void update(String key, String value) throws KeeperException, InterruptedException;
    
@@ -118,8 +118,8 @@ public interface IAction {
     * Only delete target node..
     *
     * @param key key
     * @throws KeeperException Zookeeper Exception
     * @throws InterruptedException InterruptedException
     * @throws KeeperException zookeeper exception
     * @throws InterruptedException interrupted exception
     */
    void deleteOnlyCurrent(String key) throws KeeperException, InterruptedException;
    
@@ -128,9 +128,9 @@ public interface IAction {
     *
     * @param key key
     * @param callback callback
     * @param ctx ctx
     * @throws KeeperException Zookeeper Exception
     * @throws InterruptedException InterruptedException
     * @param ctx context
     * @throws KeeperException zookeeper exception
     * @throws InterruptedException interrupted exception
     */
    void deleteOnlyCurrent(String key, AsyncCallback.VoidCallback callback, Object ctx) throws KeeperException, InterruptedException;
}
+23 −23
Original line number Diff line number Diff line
@@ -24,8 +24,8 @@ import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transacti
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/*
 * Client api.
/**
 * Client API.
 *
 * @author lidongbo
 */
@@ -34,42 +34,37 @@ public interface IClient extends IAction, IGroupAction {
    /**
     * Start.
     *
     * @throws IOException IO Exception
     * @throws InterruptedException InterruptedException
     * @throws IOException IO exception
     * @throws InterruptedException interrupted exception
     */
    void start() throws IOException, InterruptedException;
    
    /**
     * Start until out.
     *
     * @param wait wait
     * @param units units
     * @return connected
     * @throws IOException IO Exception
     * @throws InterruptedException InterruptedException
     * @param waitingTime waiting time
     * @param timeUnit time unit
     * @return connected or not
     * @throws IOException IO exception
     * @throws InterruptedException interrupted exception
     */
    boolean start(int wait, TimeUnit units) throws IOException, InterruptedException;
    boolean start(int waitingTime, TimeUnit timeUnit) throws IOException, InterruptedException;
    
    /**
     * Block until connected.
     *
     * @param wait wait
     * @param units units
     * @return connected
     * @throws InterruptedException InterruptedException
     */
    boolean blockUntilConnected(int wait, TimeUnit units) throws InterruptedException;
    
    /**
     * Close.
     * @param waitingTime waiting time
     * @param timeUnit time unit
     * @return connected or not
     * @throws InterruptedException interrupted exception
     */
    void close();
    boolean blockUntilConnected(int waitingTime, TimeUnit timeUnit) throws InterruptedException;
    
    /**
     * Register watcher.
     *
     * @param key key
     * @param zookeeperEventListener listener
     * @param zookeeperEventListener zookeeper event listener
     */
    void registerWatch(String key, ZookeeperEventListener zookeeperEventListener);
    
@@ -88,9 +83,14 @@ public interface IClient extends IAction, IGroupAction {
    void useExecStrategy(StrategyType strategyType);
    
    /**
     * Create transaction.
     * Create zookeeper transaction.
     *
     * @return ZKTransaction
     * @return zookeeper transaction
     */
    BaseTransaction transaction();
    
    /**
     * Close.
     */
    void close();
}
+4 −4
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.action;

import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.BaseTransaction;

/*
/**
 * Client 's execution strategy.
 *
 * @author lidongbo
@@ -29,14 +29,14 @@ public interface IExecStrategy extends IAction, IGroupAction {
    /**
     * Get provider.
     *
     * @return IProvider
     * @return provider
     */
    IProvider getProvider();
    
    /**
     * Create transaction.
     * Create zookeeper transaction.
     *
     * @return BaseTransaction
     * @return zookeeper transaction
     */
    BaseTransaction transaction();
}
Loading