From e9b8857faa94fe83d15d64c9c88b302613c650e6 Mon Sep 17 00:00:00 2001 From: saaav Date: Tue, 17 Apr 2018 18:54:02 +0800 Subject: [PATCH 001/351] Initial commit --- .gitignore | 22 ++++++ LICENSE | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 2 + 3 files changed, 225 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000..6143e53f9e36d --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000..261eeb9e9f8b2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md new file mode 100644 index 0000000000000..bd1b7aeffd4cd --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# ZookeeperClient +test From dca9c8dfc1585751b5172fbf0a5c9aa1f35b0efe Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 18 Apr 2018 14:42:00 +0800 Subject: [PATCH 002/351] init --- build.gradle | 22 +++++ settings.gradle | 2 + .../com/saaavsaaa/client/ClientFactory.java | 41 +++++++++ .../com/saaavsaaa/client/ZookeeperClient.java | 85 +++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 build.gradle create mode 100644 settings.gradle create mode 100644 src/main/java/com/saaavsaaa/client/ClientFactory.java create mode 100644 src/main/java/com/saaavsaaa/client/ZookeeperClient.java diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000000000..0558119b5bc86 --- /dev/null +++ b/build.gradle @@ -0,0 +1,22 @@ +group 'com.saaavsaaa' +version '1.0-SNAPSHOT' + +task wrapper(type: Wrapper) { + gradleVersion = '2.13' + distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip" +} + +apply plugin: 'java' + +sourceCompatibility = 1.5 + +repositories { + mavenCentral() +} + +dependencies { + // https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper + compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.4.11' + + testCompile group: 'junit', name: 'junit', version: '4.11' +} diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000000000..b93534156f452 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'zookeeper-client' + diff --git a/src/main/java/com/saaavsaaa/client/ClientFactory.java b/src/main/java/com/saaavsaaa/client/ClientFactory.java new file mode 100644 index 0000000000000..7b645310216d9 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/ClientFactory.java @@ -0,0 +1,41 @@ +package com.saaavsaaa.client; + +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; + +/** + * Created by aaa on 18-4-18. + */ +public class ClientFactory { + + private static final CountDownLatch CONNECTED = new CountDownLatch(1); + + private ClientFactory(){} + + public static void main(String[] args) throws Exception{ + ClientFactory creator = new ClientFactory(); + creator.newClient("192.168.2.44:2181", 20000).setNamespace("test").authorization("digest", "digest".getBytes()); + } + + ZookeeperClient newClient(final String serverAddrs, final int sessionTimeoutMilliseconds) throws IOException, InterruptedException { + ZookeeperClient client = new ZookeeperClient(serverAddrs, sessionTimeoutMilliseconds); + client.start(connectWatcher()); + CONNECTED.wait(); + return client; + } + + private Watcher connectWatcher() { + return new Watcher(){ + public void process(WatchedEvent event) { + if(Event.KeeperState.SyncConnected == event.getState()){ + if(Event.EventType.None == event.getType()){ + CONNECTED.countDown(); + } + } + } + }; + } +} diff --git a/src/main/java/com/saaavsaaa/client/ZookeeperClient.java b/src/main/java/com/saaavsaaa/client/ZookeeperClient.java new file mode 100644 index 0000000000000..f8fc40739b177 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/ZookeeperClient.java @@ -0,0 +1,85 @@ +package com.saaavsaaa.client; +/** + * Created by aaa on 18-4-18. + */ + +import org.apache.zookeeper.*; +import org.apache.zookeeper.data.ACL; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.CountDownLatch; + +public class ZookeeperClient{ + public static final String PATH_SEPARATOR = "/"; + + private final String servers; + private final int sessionTimeOut; + + private String rootNode; + private ZooKeeper zooKeeper; + private List authorities; + + ZookeeperClient(String servers, int sessionTimeoutMilliseconds) { + this.servers = servers; + this.sessionTimeOut = sessionTimeoutMilliseconds; + } + + void start(final Watcher watcher) throws IOException, InterruptedException { + zooKeeper = new ZooKeeper(servers, sessionTimeOut, watcher); + } + + public ZookeeperClient setNamespace(String namespace) { + if (!namespace.startsWith("/")){ + namespace = "/" + namespace; + } + this.rootNode = namespace; + return this; + } + + public void authorization(String scheme, byte[] auth){ + if (scheme == null || scheme.trim().length() == 0) { + return; + } + zooKeeper.addAuthInfo(scheme , auth); + authorities = ZooDefs.Ids.CREATOR_ALL_ACL; + } + + public byte[] getData(String key) throws KeeperException, InterruptedException { + return zooKeeper.getData(getRealPath(key), false, null); + } + + private String getRealPath(String path){ + if (path.equals(rootNode)){ + return new StringBuilder().append(PATH_SEPARATOR).append(rootNode).toString(); + } + return new StringBuilder().append(PATH_SEPARATOR).append(rootNode).append(PATH_SEPARATOR).append(path).toString(); + } + + public boolean checkExists(String key) throws KeeperException, InterruptedException { + return null != zooKeeper.exists(getRealPath(key), false); + } + + public List getChildren(String key) throws KeeperException, InterruptedException { + return zooKeeper.getChildren(getRealPath(key), false); + } + + public void create(String path, byte[] data, CreateMode createMode) throws KeeperException, InterruptedException { + // TODO: 18-4-12 + if (path.indexOf("/") > -1){ + System.out.println("exist / need op ..============================================="); + } + zooKeeper.create(path, data, authorities, createMode); + } + + private void createRootNode() throws KeeperException, InterruptedException { + if (checkExists(rootNode)){ + return; + } + zooKeeper.create(getRealPath(rootNode), new byte[0], authorities, CreateMode.PERSISTENT); + } + + public void update(String key, byte[] data) throws KeeperException, InterruptedException { + zooKeeper.transaction().setData(getRealPath(key), data, -1).commit(); + } +} From 2c9d0e55f82f24f973db3e484b7612cb5d7ba9b9 Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 18 Apr 2018 15:30:00 +0800 Subject: [PATCH 003/351] CLEAR --- .../com/saaavsaaa/client/ClientFactory.java | 52 +++++++++++-------- .../com/saaavsaaa/client/ZookeeperClient.java | 45 +++++++++------- 2 files changed, 56 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/ClientFactory.java b/src/main/java/com/saaavsaaa/client/ClientFactory.java index 7b645310216d9..5065115848f47 100644 --- a/src/main/java/com/saaavsaaa/client/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/ClientFactory.java @@ -1,41 +1,49 @@ package com.saaavsaaa.client; -import org.apache.zookeeper.WatchedEvent; -import org.apache.zookeeper.Watcher; - import java.io.IOException; -import java.util.concurrent.CountDownLatch; /** * Created by aaa on 18-4-18. */ public class ClientFactory { + private ZookeeperClient client; + private String namespace; + private String scheme; + private byte[] auth; - private static final CountDownLatch CONNECTED = new CountDownLatch(1); - - private ClientFactory(){} + public ClientFactory(){} public static void main(String[] args) throws Exception{ ClientFactory creator = new ClientFactory(); - creator.newClient("192.168.2.44:2181", 20000).setNamespace("test").authorization("digest", "digest".getBytes()); + ZookeeperClient client = creator.setNamespace("test").authorization("digest", "digest".getBytes()).newClient("192.168.2.44:2181", 20000).start(); + } + + public ClientFactory newClient(final String servers, final int sessionTimeoutMilliseconds) { + client = new ZookeeperClient(servers, sessionTimeoutMilliseconds); + return this; } - ZookeeperClient newClient(final String serverAddrs, final int sessionTimeoutMilliseconds) throws IOException, InterruptedException { - ZookeeperClient client = new ZookeeperClient(serverAddrs, sessionTimeoutMilliseconds); - client.start(connectWatcher()); - CONNECTED.wait(); + public synchronized ZookeeperClient start() throws IOException, InterruptedException { + client.setRootNode(namespace); + client.setAuthorities(scheme , auth); + client.start(); return client; } - private Watcher connectWatcher() { - return new Watcher(){ - public void process(WatchedEvent event) { - if(Event.KeeperState.SyncConnected == event.getState()){ - if(Event.EventType.None == event.getType()){ - CONNECTED.countDown(); - } - } - } - }; + public ClientFactory setNamespace(String namespace) { + if (!namespace.startsWith("/")){ + namespace = "/" + namespace; + } + this.namespace = namespace; + return this; + } + + public ClientFactory authorization(String scheme, byte[] auth){ + if (scheme == null || scheme.trim().length() == 0) { + return this; + } + this.scheme = scheme; + this.auth = auth; + return this; } } diff --git a/src/main/java/com/saaavsaaa/client/ZookeeperClient.java b/src/main/java/com/saaavsaaa/client/ZookeeperClient.java index f8fc40739b177..a2f6136149d8e 100644 --- a/src/main/java/com/saaavsaaa/client/ZookeeperClient.java +++ b/src/main/java/com/saaavsaaa/client/ZookeeperClient.java @@ -11,6 +11,7 @@ import java.util.concurrent.CountDownLatch; public class ZookeeperClient{ + private static final CountDownLatch CONNECTED = new CountDownLatch(1); public static final String PATH_SEPARATOR = "/"; private final String servers; @@ -25,24 +26,21 @@ public class ZookeeperClient{ this.sessionTimeOut = sessionTimeoutMilliseconds; } - void start(final Watcher watcher) throws IOException, InterruptedException { - zooKeeper = new ZooKeeper(servers, sessionTimeOut, watcher); + void start() throws IOException, InterruptedException { + zooKeeper = new ZooKeeper(servers, sessionTimeOut, connectWatcher()); + CONNECTED.wait(); } - public ZookeeperClient setNamespace(String namespace) { - if (!namespace.startsWith("/")){ - namespace = "/" + namespace; - } - this.rootNode = namespace; - return this; - } - - public void authorization(String scheme, byte[] auth){ - if (scheme == null || scheme.trim().length() == 0) { - return; - } - zooKeeper.addAuthInfo(scheme , auth); - authorities = ZooDefs.Ids.CREATOR_ALL_ACL; + private Watcher connectWatcher() { + return new Watcher(){ + public void process(WatchedEvent event) { + if(Event.KeeperState.SyncConnected == event.getState()){ + if(Event.EventType.None == event.getType()){ + CONNECTED.countDown(); + } + } + } + }; } public byte[] getData(String key) throws KeeperException, InterruptedException { @@ -69,17 +67,26 @@ public void create(String path, byte[] data, CreateMode createMode) throws Keepe if (path.indexOf("/") > -1){ System.out.println("exist / need op ..============================================="); } - zooKeeper.create(path, data, authorities, createMode); + this.zooKeeper.create(path, data, authorities, createMode); } private void createRootNode() throws KeeperException, InterruptedException { if (checkExists(rootNode)){ return; } - zooKeeper.create(getRealPath(rootNode), new byte[0], authorities, CreateMode.PERSISTENT); + this.zooKeeper.create(getRealPath(rootNode), new byte[0], authorities, CreateMode.PERSISTENT); } public void update(String key, byte[] data) throws KeeperException, InterruptedException { - zooKeeper.transaction().setData(getRealPath(key), data, -1).commit(); + this.zooKeeper.transaction().setData(getRealPath(key), data, -1).commit(); + } + + public void setRootNode(String rootNode) { + this.rootNode = rootNode; + } + + public void setAuthorities(String scheme, byte[] auth) { + this.zooKeeper.addAuthInfo(scheme , auth); + this.authorities = ZooDefs.Ids.CREATOR_ALL_ACL; } } From 459d8ed79b9d42d3e10680f8e1678c10ecf8dfb9 Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 18 Apr 2018 18:01:08 +0800 Subject: [PATCH 004/351] start --- build.gradle | 71 +++++++++++++++++-- .../com/saaavsaaa/client/ClientFactory.java | 2 +- .../com/saaavsaaa/client/ZookeeperClient.java | 21 ++---- 3 files changed, 72 insertions(+), 22 deletions(-) diff --git a/build.gradle b/build.gradle index 0558119b5bc86..fc591baa4d023 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,10 @@ group 'com.saaavsaaa' version '1.0-SNAPSHOT' -task wrapper(type: Wrapper) { - gradleVersion = '2.13' - distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip" -} - +apply plugin: 'idea' apply plugin: 'java' - -sourceCompatibility = 1.5 +apply plugin: 'maven' +apply plugin: 'signing' repositories { mavenCentral() @@ -20,3 +16,64 @@ dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' } + +uploadArchives { + repositories { + mavenDeployer { + //为Pom文件做数字签名 + beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } + + repository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { + authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) + } + + pom.project { + name project.name + packaging 'jar' + description 'zk client' + url 'https://github.com/saaavsaaa/ZookeeperClient' + + scm { + url 'scm:git@github.com:saaavsaaa/ZookeeperClient.git' + connection 'scm:git@github.com:saaavsaaa/ZookeeperClient.git' + developerConnection 'git@github.com:saaavsaaa/ZookeeperClient.git' + } + + licenses { + license { + name 'The Apache Software License, Version 2.0' + url 'http://www.apache.org/licenses/LICENSE-2.0.txt' + distribution 'repo' + } + } + + developers { + developer { + id 'saaav' + name 'lidb' + } + } + } + } + } +} + +task javadocJar(type: Jar, dependsOn: javadoc) { + classifier = 'javadoc' + from 'build/docs/javadoc' +} + +task sourcesJar(type: Jar) { + classifier = 'sources' + from sourceSets.main.allSource +} + +artifacts { + archives jar + archives javadocJar + archives sourcesJar +} + +signing { + sign configurations.archives +} \ No newline at end of file diff --git a/src/main/java/com/saaavsaaa/client/ClientFactory.java b/src/main/java/com/saaavsaaa/client/ClientFactory.java index 5065115848f47..e8f9a87fd7fa0 100644 --- a/src/main/java/com/saaavsaaa/client/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/ClientFactory.java @@ -24,9 +24,9 @@ public ClientFactory newClient(final String servers, final int sessionTimeoutMil } public synchronized ZookeeperClient start() throws IOException, InterruptedException { + client.start(); client.setRootNode(namespace); client.setAuthorities(scheme , auth); - client.start(); return client; } diff --git a/src/main/java/com/saaavsaaa/client/ZookeeperClient.java b/src/main/java/com/saaavsaaa/client/ZookeeperClient.java index a2f6136149d8e..86aa4e7a60e42 100644 --- a/src/main/java/com/saaavsaaa/client/ZookeeperClient.java +++ b/src/main/java/com/saaavsaaa/client/ZookeeperClient.java @@ -3,6 +3,7 @@ * Created by aaa on 18-4-18. */ +import com.saaavsaaa.client.untils.PathUtil; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; @@ -12,7 +13,6 @@ public class ZookeeperClient{ private static final CountDownLatch CONNECTED = new CountDownLatch(1); - public static final String PATH_SEPARATOR = "/"; private final String servers; private final int sessionTimeOut; @@ -28,7 +28,7 @@ public class ZookeeperClient{ void start() throws IOException, InterruptedException { zooKeeper = new ZooKeeper(servers, sessionTimeOut, connectWatcher()); - CONNECTED.wait(); + CONNECTED.await(); } private Watcher connectWatcher() { @@ -44,22 +44,15 @@ public void process(WatchedEvent event) { } public byte[] getData(String key) throws KeeperException, InterruptedException { - return zooKeeper.getData(getRealPath(key), false, null); - } - - private String getRealPath(String path){ - if (path.equals(rootNode)){ - return new StringBuilder().append(PATH_SEPARATOR).append(rootNode).toString(); - } - return new StringBuilder().append(PATH_SEPARATOR).append(rootNode).append(PATH_SEPARATOR).append(path).toString(); + return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), false, null); } public boolean checkExists(String key) throws KeeperException, InterruptedException { - return null != zooKeeper.exists(getRealPath(key), false); + return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), false); } public List getChildren(String key) throws KeeperException, InterruptedException { - return zooKeeper.getChildren(getRealPath(key), false); + return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); } public void create(String path, byte[] data, CreateMode createMode) throws KeeperException, InterruptedException { @@ -74,11 +67,11 @@ private void createRootNode() throws KeeperException, InterruptedException { if (checkExists(rootNode)){ return; } - this.zooKeeper.create(getRealPath(rootNode), new byte[0], authorities, CreateMode.PERSISTENT); + this.zooKeeper.create(rootNode, new byte[0], authorities, CreateMode.PERSISTENT); } public void update(String key, byte[] data) throws KeeperException, InterruptedException { - this.zooKeeper.transaction().setData(getRealPath(key), data, -1).commit(); + this.zooKeeper.transaction().setData(PathUtil.getRealPath(rootNode, key), data, -1).commit(); } public void setRootNode(String rootNode) { From be58ebd71afa2561a7160ef3080a636af14efd8f Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 18 Apr 2018 18:01:48 +0800 Subject: [PATCH 005/351] start --- .../com/saaavsaaa/client/untils/PathUtil.java | 45 +++++++++++++++++++ .../saaavsaaa/client/untils/StringUtil.java | 10 +++++ 2 files changed, 55 insertions(+) create mode 100644 src/main/java/com/saaavsaaa/client/untils/PathUtil.java create mode 100644 src/main/java/com/saaavsaaa/client/untils/StringUtil.java diff --git a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java new file mode 100644 index 0000000000000..f02b8ebf85915 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java @@ -0,0 +1,45 @@ +package com.saaavsaaa.client.untils; + +import java.util.List; +import java.util.Stack; + +/** + * Created by aaa on 18-4-18. + */ +public class PathUtil { + public static final String PATH_SEPARATOR = "/"; + + public static String getRealPath(final String root, String path){ + path = adjustPath(path); + if (path.startsWith(root)){ + return path; + } + return new StringBuilder().append(root).append(path).toString(); + } + + private static String adjustPath(String path){ + if (StringUtil.isNullOrWhite(path)){ + throw new IllegalArgumentException("path should have content!"); + } + if (!path.startsWith(PATH_SEPARATOR)){ + path = PATH_SEPARATOR + path; + } + return path; + } + + public static Stack getPathNodes(String path){ + path = adjustPath(path); + Stack pathStack = new Stack<>(); + int index = 1; + int position = path.indexOf(PATH_SEPARATOR, index); + do{ + pathStack.push(path.substring(0, position)); + index = position + 1; + position = path.indexOf(PATH_SEPARATOR, index); + } + while (position > -1); + pathStack.push(path); + return pathStack; + } + +} diff --git a/src/main/java/com/saaavsaaa/client/untils/StringUtil.java b/src/main/java/com/saaavsaaa/client/untils/StringUtil.java new file mode 100644 index 0000000000000..1f8bf1b8abce0 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/untils/StringUtil.java @@ -0,0 +1,10 @@ +package com.saaavsaaa.client.untils; + +/** + * Created by aaa on 18-4-18. + */ +public class StringUtil { + public static boolean isNullOrWhite(String string) { + return string == null || string.trim().length() == 0; + } +} From 49f78442c2d847061b93aeb40254a401df3b5e56 Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 18 Apr 2018 18:25:16 +0800 Subject: [PATCH 006/351] start create all needed nodes --- .../com/saaavsaaa/client/ClientFactory.java | 7 +-- .../com/saaavsaaa/client/ZookeeperClient.java | 45 ++++++++++++++----- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/ClientFactory.java b/src/main/java/com/saaavsaaa/client/ClientFactory.java index e8f9a87fd7fa0..5b24d41c0bae7 100644 --- a/src/main/java/com/saaavsaaa/client/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/ClientFactory.java @@ -12,12 +12,7 @@ public class ClientFactory { private byte[] auth; public ClientFactory(){} - - public static void main(String[] args) throws Exception{ - ClientFactory creator = new ClientFactory(); - ZookeeperClient client = creator.setNamespace("test").authorization("digest", "digest".getBytes()).newClient("192.168.2.44:2181", 20000).start(); - } - + public ClientFactory newClient(final String servers, final int sessionTimeoutMilliseconds) { client = new ZookeeperClient(servers, sessionTimeoutMilliseconds); return this; diff --git a/src/main/java/com/saaavsaaa/client/ZookeeperClient.java b/src/main/java/com/saaavsaaa/client/ZookeeperClient.java index 86aa4e7a60e42..582f7c0b0acf6 100644 --- a/src/main/java/com/saaavsaaa/client/ZookeeperClient.java +++ b/src/main/java/com/saaavsaaa/client/ZookeeperClient.java @@ -4,11 +4,13 @@ */ import com.saaavsaaa.client.untils.PathUtil; +import com.saaavsaaa.client.untils.StringUtil; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; import java.io.IOException; import java.util.List; +import java.util.Stack; import java.util.concurrent.CountDownLatch; public class ZookeeperClient{ @@ -29,6 +31,16 @@ public class ZookeeperClient{ void start() throws IOException, InterruptedException { zooKeeper = new ZooKeeper(servers, sessionTimeOut, connectWatcher()); CONNECTED.await(); + /*if (StringUtil.isNullOrWhite(rootNode)){ + createRootNode(); + }*/ + } + + private void createRootNode() throws KeeperException, InterruptedException { + if (checkExists(rootNode)){ + return; + } + zooKeeper.create(rootNode, new byte[0], authorities, CreateMode.PERSISTENT); } private Watcher connectWatcher() { @@ -55,23 +67,34 @@ public List getChildren(String key) throws KeeperException, InterruptedE return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); } - public void create(String path, byte[] data, CreateMode createMode) throws KeeperException, InterruptedException { - // TODO: 18-4-12 - if (path.indexOf("/") > -1){ - System.out.println("exist / need op ..============================================="); - } - this.zooKeeper.create(path, data, authorities, createMode); + public void createCurrentPathOnly(String path, byte[] data, CreateMode createMode) throws KeeperException, InterruptedException { + zooKeeper.create(PathUtil.getRealPath(rootNode, path), data, authorities, createMode); } - private void createRootNode() throws KeeperException, InterruptedException { - if (checkExists(rootNode)){ + public void createAllNeedPath(String path, byte[] data, CreateMode createMode) throws KeeperException, InterruptedException { + if (path.indexOf("/") < -1){ + this.createCurrentPathOnly(path, data, createMode); return; } - this.zooKeeper.create(rootNode, new byte[0], authorities, CreateMode.PERSISTENT); + Transaction transaction = zooKeeper.transaction(); + // sync cache + Stack pathStack = PathUtil.getPathNodes(path); + while (!pathStack.empty()){ + String node = pathStack.pop(); + // contrast cache + if (!checkExists(node)){ + createInTransaction(path, data, createMode, transaction); + } + } + transaction.commit(); + } + + private Transaction createInTransaction(String path, byte[] data, CreateMode createMode, Transaction transaction){ + return transaction.create(PathUtil.getRealPath(rootNode, path), data, authorities, createMode); } public void update(String key, byte[] data) throws KeeperException, InterruptedException { - this.zooKeeper.transaction().setData(PathUtil.getRealPath(rootNode, key), data, -1).commit(); + zooKeeper.transaction().setData(PathUtil.getRealPath(rootNode, key), data, -1).commit(); } public void setRootNode(String rootNode) { @@ -79,7 +102,7 @@ public void setRootNode(String rootNode) { } public void setAuthorities(String scheme, byte[] auth) { - this.zooKeeper.addAuthInfo(scheme , auth); + zooKeeper.addAuthInfo(scheme , auth); this.authorities = ZooDefs.Ids.CREATOR_ALL_ACL; } } From e46a9ec3b1b5afa38b355302f2c2d142a15f4d2e Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 19 Apr 2018 12:04:13 +0800 Subject: [PATCH 007/351] cache begin --- .../java/com/saaavsaaa/client/BaseClient.java | 43 +++++++++++++++++++ .../com/saaavsaaa/client/cache/PathNode.java | 7 +++ .../client/zookeeper/CacheClient.java | 7 +++ .../client/{ => zookeeper}/ClientFactory.java | 2 +- .../{ => zookeeper}/ZookeeperClient.java | 42 +++++------------- 5 files changed, 69 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/BaseClient.java create mode 100644 src/main/java/com/saaavsaaa/client/cache/PathNode.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java rename src/main/java/com/saaavsaaa/client/{ => zookeeper}/ClientFactory.java (96%) rename src/main/java/com/saaavsaaa/client/{ => zookeeper}/ZookeeperClient.java (71%) diff --git a/src/main/java/com/saaavsaaa/client/BaseClient.java b/src/main/java/com/saaavsaaa/client/BaseClient.java new file mode 100644 index 0000000000000..145a739a2216b --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/BaseClient.java @@ -0,0 +1,43 @@ +package com.saaavsaaa.client; + +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.ACL; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.CountDownLatch; + +/** + * Created by aaa on 18-4-19. + */ +public class BaseClient { + private static final CountDownLatch CONNECTED = new CountDownLatch(1); + + private final String servers; + private final int sessionTimeOut; + protected ZooKeeper zooKeeper; + + protected BaseClient(String servers, int sessionTimeoutMilliseconds) { + this.servers = servers; + this.sessionTimeOut = sessionTimeoutMilliseconds; + } + + public void start() throws IOException, InterruptedException { + zooKeeper = new ZooKeeper(servers, sessionTimeOut, connectWatcher()); + CONNECTED.await(); + } + + private Watcher connectWatcher() { + return new Watcher(){ + public void process(WatchedEvent event) { + if(Event.KeeperState.SyncConnected == event.getState()){ + if(Event.EventType.None == event.getType()){ + CONNECTED.countDown(); + } + } + } + }; + } +} diff --git a/src/main/java/com/saaavsaaa/client/cache/PathNode.java b/src/main/java/com/saaavsaaa/client/cache/PathNode.java new file mode 100644 index 0000000000000..5e52775182395 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/cache/PathNode.java @@ -0,0 +1,7 @@ +package com.saaavsaaa.client.cache; + +/** + * Created by aaa on 18-4-18. + */ +public class PathNode { +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java new file mode 100644 index 0000000000000..b5cf82ddf17ff --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -0,0 +1,7 @@ +package com.saaavsaaa.client.zookeeper; + +/** + * Created by aaa on 18-4-19. + */ +public class CacheClient { +} diff --git a/src/main/java/com/saaavsaaa/client/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java similarity index 96% rename from src/main/java/com/saaavsaaa/client/ClientFactory.java rename to src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 5b24d41c0bae7..71f3825426146 100644 --- a/src/main/java/com/saaavsaaa/client/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client; +package com.saaavsaaa.client.zookeeper; import java.io.IOException; diff --git a/src/main/java/com/saaavsaaa/client/ZookeeperClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java similarity index 71% rename from src/main/java/com/saaavsaaa/client/ZookeeperClient.java rename to src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java index 582f7c0b0acf6..b454e2e1f70db 100644 --- a/src/main/java/com/saaavsaaa/client/ZookeeperClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java @@ -1,8 +1,9 @@ -package com.saaavsaaa.client; +package com.saaavsaaa.client.zookeeper; /** * Created by aaa on 18-4-18. */ +import com.saaavsaaa.client.BaseClient; import com.saaavsaaa.client.untils.PathUtil; import com.saaavsaaa.client.untils.StringUtil; import org.apache.zookeeper.*; @@ -13,47 +14,26 @@ import java.util.Stack; import java.util.concurrent.CountDownLatch; -public class ZookeeperClient{ - private static final CountDownLatch CONNECTED = new CountDownLatch(1); - - private final String servers; - private final int sessionTimeOut; +/* +* cache +*/ +public class ZookeeperClient extends BaseClient { private String rootNode; - private ZooKeeper zooKeeper; private List authorities; ZookeeperClient(String servers, int sessionTimeoutMilliseconds) { - this.servers = servers; - this.sessionTimeOut = sessionTimeoutMilliseconds; - } - - void start() throws IOException, InterruptedException { - zooKeeper = new ZooKeeper(servers, sessionTimeOut, connectWatcher()); - CONNECTED.await(); - /*if (StringUtil.isNullOrWhite(rootNode)){ - createRootNode(); - }*/ + super(servers, sessionTimeoutMilliseconds); } - - private void createRootNode() throws KeeperException, InterruptedException { + + public void createRootNode() throws KeeperException, InterruptedException { if (checkExists(rootNode)){ return; } zooKeeper.create(rootNode, new byte[0], authorities, CreateMode.PERSISTENT); } - private Watcher connectWatcher() { - return new Watcher(){ - public void process(WatchedEvent event) { - if(Event.KeeperState.SyncConnected == event.getState()){ - if(Event.EventType.None == event.getType()){ - CONNECTED.countDown(); - } - } - } - }; - } + public byte[] getData(String key) throws KeeperException, InterruptedException { return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), false, null); @@ -77,7 +57,7 @@ public void createAllNeedPath(String path, byte[] data, CreateMode createMode) t return; } Transaction transaction = zooKeeper.transaction(); - // sync cache + //todo sync cache Stack pathStack = PathUtil.getPathNodes(path); while (!pathStack.empty()){ String node = pathStack.pop(); From 817b5b31c5c2883dfbcd454309560c3fe10e16ac Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 19 Apr 2018 15:26:00 +0800 Subject: [PATCH 008/351] unit test --- build.gradle | 51 ++++++- .../java/com/saaavsaaa/client/BaseClient.java | 15 ++- .../client/zookeeper/CacheClient.java | 5 +- .../client/zookeeper/ClientFactory.java | 7 + .../client/zookeeper/ZookeeperClient.java | 19 +-- .../client/test/UsualClientTest.java | 126 ++++++++++++++++++ 6 files changed, 206 insertions(+), 17 deletions(-) create mode 100644 src/test/java/com/saaavsaaa/client/test/UsualClientTest.java diff --git a/build.gradle b/build.gradle index fc591baa4d023..45ec9dfb15271 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,20 @@ +/* + * Copyright 2013 Chris Banes + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + group 'com.saaavsaaa' version '1.0-SNAPSHOT' @@ -23,8 +40,15 @@ uploadArchives { //为Pom文件做数字签名 beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } - repository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { - authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) +// repository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { +// authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) +// } + + repository(url: getReleaseRepositoryUrl()) { + authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) + } + snapshotRepository(url: getSnapshotRepositoryUrl()) { + authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) } pom.project { @@ -68,6 +92,28 @@ task sourcesJar(type: Jar) { from sourceSets.main.allSource } +def isReleaseBuild() { + return VERSION_NAME.contains("SNAPSHOT") == false +} + +def getReleaseRepositoryUrl() { + return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL + : "https://oss.sonatype.org/service/local/staging/deploy/maven2/" +} + +def getSnapshotRepositoryUrl() { + return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL + : "https://oss.sonatype.org/content/repositories/snapshots/" +} + +def getRepositoryUsername() { + return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : "" +} + +def getRepositoryPassword() { + return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : "" +} + artifacts { archives jar archives javadocJar @@ -75,5 +121,6 @@ artifacts { } signing { + required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") } sign configurations.archives } \ No newline at end of file diff --git a/src/main/java/com/saaavsaaa/client/BaseClient.java b/src/main/java/com/saaavsaaa/client/BaseClient.java index 145a739a2216b..5e261ce4cd868 100644 --- a/src/main/java/com/saaavsaaa/client/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/BaseClient.java @@ -2,6 +2,7 @@ import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.ACL; @@ -12,13 +13,16 @@ /** * Created by aaa on 18-4-19. */ -public class BaseClient { +public abstract class BaseClient { private static final CountDownLatch CONNECTED = new CountDownLatch(1); private final String servers; private final int sessionTimeOut; protected ZooKeeper zooKeeper; + protected String rootNode = "InitValue"; + protected List authorities; + protected BaseClient(String servers, int sessionTimeoutMilliseconds) { this.servers = servers; this.sessionTimeOut = sessionTimeoutMilliseconds; @@ -40,4 +44,13 @@ public void process(WatchedEvent event) { } }; } + + public void setRootNode(String rootNode) { + this.rootNode = rootNode; + } + + public void setAuthorities(String scheme, byte[] auth) { + zooKeeper.addAuthInfo(scheme , auth); + this.authorities = ZooDefs.Ids.CREATOR_ALL_ACL; + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index b5cf82ddf17ff..f92175876e815 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -3,5 +3,8 @@ /** * Created by aaa on 18-4-19. */ -public class CacheClient { +public class CacheClient extends ZookeeperClient { + CacheClient(String servers, int sessionTimeoutMilliseconds) { + super(servers, sessionTimeoutMilliseconds); + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 71f3825426146..79e53a7ef7f24 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -6,12 +6,19 @@ * Created by aaa on 18-4-18. */ public class ClientFactory { +// private static final String CLIENT_EXCLUSIVE_NODE = "ZKC"; + private ZookeeperClient client; private String namespace; private String scheme; private byte[] auth; public ClientFactory(){} + + public ClientFactory newCacheClient(final String servers, final int sessionTimeoutMilliseconds) { + client = new CacheClient(servers, sessionTimeoutMilliseconds); + return this; + } public ClientFactory newClient(final String servers, final int sessionTimeoutMilliseconds) { client = new ZookeeperClient(servers, sessionTimeoutMilliseconds); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java index b454e2e1f70db..a9ff773d8a479 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java @@ -18,10 +18,6 @@ * cache */ public class ZookeeperClient extends BaseClient { - - private String rootNode; - private List authorities; - ZookeeperClient(String servers, int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); } @@ -32,9 +28,7 @@ public void createRootNode() throws KeeperException, InterruptedException { } zooKeeper.create(rootNode, new byte[0], authorities, CreateMode.PERSISTENT); } - - public byte[] getData(String key) throws KeeperException, InterruptedException { return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), false, null); } @@ -69,20 +63,19 @@ public void createAllNeedPath(String path, byte[] data, CreateMode createMode) t transaction.commit(); } - private Transaction createInTransaction(String path, byte[] data, CreateMode createMode, Transaction transaction){ + public Transaction createInTransaction(String path, byte[] data, CreateMode createMode, Transaction transaction){ return transaction.create(PathUtil.getRealPath(rootNode, path), data, authorities, createMode); } - public void update(String key, byte[] data) throws KeeperException, InterruptedException { + public void updateInTransaction(String key, byte[] data) throws KeeperException, InterruptedException { zooKeeper.transaction().setData(PathUtil.getRealPath(rootNode, key), data, -1).commit(); } - public void setRootNode(String rootNode) { - this.rootNode = rootNode; + public void update(String key, byte[] data) throws KeeperException, InterruptedException { + zooKeeper.setData(PathUtil.getRealPath(rootNode, key), data, -1); } - public void setAuthorities(String scheme, byte[] auth) { - zooKeeper.addAuthInfo(scheme , auth); - this.authorities = ZooDefs.Ids.CREATOR_ALL_ACL; + public void deleteTogetherBranch(String key) { + } } diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java new file mode 100644 index 0000000000000..e19a6da37482a --- /dev/null +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -0,0 +1,126 @@ +package com.saaavsaaa.client.test; + +import com.saaavsaaa.client.zookeeper.ClientFactory; +import com.saaavsaaa.client.zookeeper.ZookeeperClient; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.IOException; +import java.util.Collections; +import java.util.Comparator; +import java.util.EventListener; +import java.util.List; + +/** + * Created by aaa on 18-4-18. + */ +public class UsualClientTest { + static final String SERVERS = "192.168.2.44:2181"; + static final int SESSION_TIMEOUT = 200000;//ms + + static ZookeeperClient client = null; + + @BeforeClass + public static void start() throws IOException, InterruptedException { + ClientFactory creator = new ClientFactory(); + client = creator.setNamespace("test").authorization("digest", "digest".getBytes()).newClient(SERVERS, SESSION_TIMEOUT).start(); + } + + @Test + public void createRoot() throws KeeperException, InterruptedException { + client.createRootNode(); + } + + @Test + public void createChild() throws KeeperException, InterruptedException { + client.createCurrentPathOnly("a/b/bb", "bbb11".getBytes(), CreateMode.PERSISTENT); + } + + @Test + public void get() throws KeeperException, InterruptedException { + String key = "a/b"; + + getDirectly(key); + } + + private void getDirectly(String key) throws KeeperException, InterruptedException { + System.out.println(new String(client.getData(key))); + } + + @Test + public void getDirectly() throws KeeperException, InterruptedException { + String key = ""; + getDirectly(key); + } + + @Test + public void isExisted() throws KeeperException, InterruptedException { + String key = "a/b/bb"; + System.out.println(isExisted(key)); + } + + private boolean isExisted(String key) throws KeeperException, InterruptedException { + return client.checkExists(key); + } + + @Test + public void getChildrenKeys() throws KeeperException, InterruptedException { + String key = "a"; + List result = client.getChildren(key); + Collections.sort(result, new Comparator() { + public int compare(final String o1, final String o2) { + return o2.compareTo(o1); + } + }); + System.out.println(result); + } + + @Test + public void persist() throws KeeperException, InterruptedException { + String key = "a"; + String value = "aa"; + if (!isExisted(key)) { + client.createAllNeedPath(key, value.getBytes(), CreateMode.PERSISTENT); + } else { + update(key, value); + } + } + + private void update(String key, String value) throws KeeperException, InterruptedException { + client.update(key, value.getBytes()); + } + + @Test + public void update() throws KeeperException, InterruptedException { + String key = "a"; + String value = "aaa"; + update(key, value); //inTransaction().check().forPath(key).and().setData().forPath().and().commit(); + getDirectly(key); + value = "aa"; + client.updateInTransaction(key, value.getBytes()); + getDirectly(key); + } + + @Test + public void persistEphemeral() throws KeeperException, InterruptedException { + String key = "bb"; + String value = "b1b"; + if (isExisted(key)) { + client.deleteTogetherBranch(key); + } + client.createAllNeedPath(key, value.getBytes(), CreateMode.EPHEMERAL); + } + + @Test + public void watch() { + String key = ""; + EventListener eventListener; + } + + @Test + public void close() throws Exception { + + } +} From 5c7f3b590803a3d4d8476c0b6828a77f730cddd8 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 19 Apr 2018 18:53:32 +0800 Subject: [PATCH 009/351] unit test --- .../com/saaavsaaa/client/untils/PathUtil.java | 31 +++++++++++++---- .../client/{ => zookeeper}/BaseClient.java | 8 ++--- .../client/zookeeper/ZookeeperClient.java | 33 +++++++++++++------ .../client/test/UsualClientTest.java | 15 ++++++--- 4 files changed, 62 insertions(+), 25 deletions(-) rename src/main/java/com/saaavsaaa/client/{ => zookeeper}/BaseClient.java (89%) diff --git a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java index f02b8ebf85915..b99e44bd2429e 100644 --- a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java +++ b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.untils; +import java.util.LinkedList; import java.util.List; import java.util.Stack; @@ -10,25 +11,26 @@ public class PathUtil { public static final String PATH_SEPARATOR = "/"; public static String getRealPath(final String root, String path){ - path = adjustPath(path); - if (path.startsWith(root)){ - return path; - } + path = adjustPath(root, path); return new StringBuilder().append(root).append(path).toString(); } - private static String adjustPath(String path){ + private static String adjustPath(final String root, String path){ if (StringUtil.isNullOrWhite(path)){ throw new IllegalArgumentException("path should have content!"); } + if (path.startsWith(root)){ + return path; + } if (!path.startsWith(PATH_SEPARATOR)){ path = PATH_SEPARATOR + path; } return path; } - public static Stack getPathNodes(String path){ - path = adjustPath(path); + //child to root + public static Stack getPathReverseNodes(final String root, String path){ + path = adjustPath(root, path); Stack pathStack = new Stack<>(); int index = 1; int position = path.indexOf(PATH_SEPARATOR, index); @@ -42,4 +44,19 @@ public static Stack getPathNodes(String path){ return pathStack; } + public static List getPathOrderNodes(final String root, String path){ + path = adjustPath(root, path); + List paths = new LinkedList<>(); + int index = 1; + int position = path.indexOf('/', index); + + do{ + paths.add(path.substring(0, position)); + index = position + 1; + position = path.indexOf('/', index); + } + while (position > -1); + paths.add(path); + return paths; + } } diff --git a/src/main/java/com/saaavsaaa/client/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java similarity index 89% rename from src/main/java/com/saaavsaaa/client/BaseClient.java rename to src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java index 5e261ce4cd868..3e6b259d30c3c 100644 --- a/src/main/java/com/saaavsaaa/client/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client; +package com.saaavsaaa.client.zookeeper; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; @@ -20,7 +20,7 @@ public abstract class BaseClient { private final int sessionTimeOut; protected ZooKeeper zooKeeper; - protected String rootNode = "InitValue"; + protected String rootNode = "/InitValue"; protected List authorities; protected BaseClient(String servers, int sessionTimeoutMilliseconds) { @@ -45,11 +45,11 @@ public void process(WatchedEvent event) { }; } - public void setRootNode(String rootNode) { + void setRootNode(String rootNode) { this.rootNode = rootNode; } - public void setAuthorities(String scheme, byte[] auth) { + void setAuthorities(String scheme, byte[] auth) { zooKeeper.addAuthInfo(scheme , auth); this.authorities = ZooDefs.Ids.CREATOR_ALL_ACL; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java index a9ff773d8a479..c90d43f093bd5 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java @@ -3,16 +3,12 @@ * Created by aaa on 18-4-18. */ -import com.saaavsaaa.client.BaseClient; import com.saaavsaaa.client.untils.PathUtil; -import com.saaavsaaa.client.untils.StringUtil; import org.apache.zookeeper.*; -import org.apache.zookeeper.data.ACL; -import java.io.IOException; +import java.util.Iterator; import java.util.List; import java.util.Stack; -import java.util.concurrent.CountDownLatch; /* * cache @@ -52,12 +48,17 @@ public void createAllNeedPath(String path, byte[] data, CreateMode createMode) t } Transaction transaction = zooKeeper.transaction(); //todo sync cache - Stack pathStack = PathUtil.getPathNodes(path); - while (!pathStack.empty()){ - String node = pathStack.pop(); + Iterator nodes = PathUtil.getPathOrderNodes(rootNode, path).iterator(); + while (nodes.hasNext()){ + String node = nodes.next(); // contrast cache if (!checkExists(node)){ - createInTransaction(path, data, createMode, transaction); + try { + // TODO: exception + createInTransaction(path, data, createMode, transaction); + } catch (Exception e){ + System.out.println(e.getMessage()); + } } } transaction.commit(); @@ -75,7 +76,19 @@ public void update(String key, byte[] data) throws KeeperException, InterruptedE zooKeeper.setData(PathUtil.getRealPath(rootNode, key), data, -1); } - public void deleteTogetherBranch(String key) { + public void deleteOnlyCurrent(String key) throws KeeperException, InterruptedException { + zooKeeper.delete(PathUtil.getRealPath(rootNode, key), -1); + } + + public void deleteOnlyCurrent(String key, AsyncCallback.VoidCallback callback, Object ctx) throws KeeperException, InterruptedException { + zooKeeper.delete(PathUtil.getRealPath(rootNode, key), -1, callback, ctx); + } + + public void deleteCurrentBranch(String key) { + + } + + public void deleteAllChild(String key) { } } diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java index e19a6da37482a..ef5f485f02f2e 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -41,7 +41,14 @@ public void createChild() throws KeeperException, InterruptedException { @Test public void get() throws KeeperException, InterruptedException { String key = "a/b"; - + /*TreeCache cache = findTreeCache(key); + if (null == cache) { + return getDirectly(key); + } + ChildData resultInCache = cache.getCurrentData(key); + if (null != resultInCache) { + return null == resultInCache.getData() ? null : new String(resultInCache.getData(), Charsets.UTF_8); + }*/ getDirectly(key); } @@ -51,13 +58,13 @@ private void getDirectly(String key) throws KeeperException, InterruptedExceptio @Test public void getDirectly() throws KeeperException, InterruptedException { - String key = ""; + String key = "a/b/bb"; getDirectly(key); } @Test public void isExisted() throws KeeperException, InterruptedException { - String key = "a/b/bb"; + String key = "a"; System.out.println(isExisted(key)); } @@ -108,7 +115,7 @@ public void persistEphemeral() throws KeeperException, InterruptedException { String key = "bb"; String value = "b1b"; if (isExisted(key)) { - client.deleteTogetherBranch(key); + client.deleteCurrentBranch(key); } client.createAllNeedPath(key, value.getBytes(), CreateMode.EPHEMERAL); } From 35fd2dfc2652e6d28b6d3720f333a5bf76777a65 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 20 Apr 2018 12:32:22 +0800 Subject: [PATCH 010/351] snapshot --- build.gradle | 20 ++--- .../com/saaavsaaa/client/untils/PathUtil.java | 40 +++++++++- .../client/zookeeper/BaseClient.java | 1 + .../client/zookeeper/ZookeeperClient.java | 80 ++++++++++++++++--- .../client/test/UsualClientTest.java | 29 +++---- 5 files changed, 132 insertions(+), 38 deletions(-) diff --git a/build.gradle b/build.gradle index 45ec9dfb15271..dc6550cdc9ebe 100644 --- a/build.gradle +++ b/build.gradle @@ -40,17 +40,17 @@ uploadArchives { //为Pom文件做数字签名 beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } -// repository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { -// authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) -// } - - repository(url: getReleaseRepositoryUrl()) { - authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) - } - snapshotRepository(url: getSnapshotRepositoryUrl()) { - authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) + repository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { + authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) } +// repository(url: getReleaseRepositoryUrl()) { +// authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) +// } +// snapshotRepository(url: getSnapshotRepositoryUrl()) { +// authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) +// } + pom.project { name project.name packaging 'jar' @@ -121,6 +121,6 @@ artifacts { } signing { - required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") } +// required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") } sign configurations.archives } \ No newline at end of file diff --git a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java index b99e44bd2429e..355ab7d1d95ce 100644 --- a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java +++ b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java @@ -1,8 +1,7 @@ package com.saaavsaaa.client.untils; -import java.util.LinkedList; -import java.util.List; -import java.util.Stack; +import javax.swing.tree.TreeNode; +import java.util.*; /** * Created by aaa on 18-4-18. @@ -59,4 +58,39 @@ public static List getPathOrderNodes(final String root, String path){ paths.add(path); return paths; } + + public static List breadthToB(TreeNode root) { + List lists = new ArrayList<>(); + if(root==null) + return lists; + Queue queue=new LinkedList<>(); + queue.offer(root); + while(!queue.isEmpty()){ + /*TreeNode tree=queue.poll(); + if(tree.left!=null) + queue.offer(tree.left); + if(tree.right!=null) + queue.offer(tree.right); + lists.add(tree.val);*/ + } + return lists; + } + + public static List depthToB(TreeNode root) { + List lists = new ArrayList<>(); + if(root==null) + return lists; + Stack stack=new Stack(); + stack.push(root); + while(!stack.isEmpty()){ + TreeNode tree=stack.pop(); + //先往栈中压入右节点,再压左节点,这样出栈就是先左节点后右节点了。 + /*if(tree.right!=null) + stack.push(tree.right); + if(tree.left!=null) + stack.push(tree.left); + lists.add(tree.val);*/ + } + return lists; + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java index 3e6b259d30c3c..61630eb869abf 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java @@ -15,6 +15,7 @@ */ public abstract class BaseClient { private static final CountDownLatch CONNECTED = new CountDownLatch(1); + public static final int VERSION = -1; private final String servers; private final int sessionTimeOut; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java index c90d43f093bd5..9ced2a2f31e79 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java @@ -6,6 +6,8 @@ import com.saaavsaaa.client.untils.PathUtil; import org.apache.zookeeper.*; +import javax.swing.tree.DefaultMutableTreeNode; +import java.util.EventListener; import java.util.Iterator; import java.util.List; import java.util.Stack; @@ -37,13 +39,16 @@ public List getChildren(String key) throws KeeperException, InterruptedE return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); } - public void createCurrentPathOnly(String path, byte[] data, CreateMode createMode) throws KeeperException, InterruptedException { + public void createCurrentOnly(String path, byte[] data, CreateMode createMode) throws KeeperException, InterruptedException { zooKeeper.create(PathUtil.getRealPath(rootNode, path), data, authorities, createMode); } + /* + * closed beta + */ public void createAllNeedPath(String path, byte[] data, CreateMode createMode) throws KeeperException, InterruptedException { - if (path.indexOf("/") < -1){ - this.createCurrentPathOnly(path, data, createMode); + if (path.indexOf(PathUtil.PATH_SEPARATOR) < -1){ + this.createCurrentOnly(path, data, createMode); return; } Transaction transaction = zooKeeper.transaction(); @@ -69,26 +74,79 @@ public Transaction createInTransaction(String path, byte[] data, CreateMode crea } public void updateInTransaction(String key, byte[] data) throws KeeperException, InterruptedException { - zooKeeper.transaction().setData(PathUtil.getRealPath(rootNode, key), data, -1).commit(); + zooKeeper.transaction().setData(PathUtil.getRealPath(rootNode, key), data, VERSION).commit(); } public void update(String key, byte[] data) throws KeeperException, InterruptedException { - zooKeeper.setData(PathUtil.getRealPath(rootNode, key), data, -1); + zooKeeper.setData(PathUtil.getRealPath(rootNode, key), data, VERSION); } public void deleteOnlyCurrent(String key) throws KeeperException, InterruptedException { - zooKeeper.delete(PathUtil.getRealPath(rootNode, key), -1); + zooKeeper.delete(PathUtil.getRealPath(rootNode, key), VERSION); } public void deleteOnlyCurrent(String key, AsyncCallback.VoidCallback callback, Object ctx) throws KeeperException, InterruptedException { - zooKeeper.delete(PathUtil.getRealPath(rootNode, key), -1, callback, ctx); + zooKeeper.delete(PathUtil.getRealPath(rootNode, key), VERSION, callback, ctx); } - public void deleteCurrentBranch(String key) { - + /* + * closed beta + */ + public void deleteCurrentBranch(String path) throws KeeperException, InterruptedException { + if (path.indexOf(PathUtil.PATH_SEPARATOR) < -1){ + this.deleteOnlyCurrent(path); + return; + } + Transaction transaction = zooKeeper.transaction(); + //todo branch check + Stack pathStack = PathUtil.getPathReverseNodes(rootNode, path); + while (!pathStack.empty()){ + String node = pathStack.pop(); + // contrast cache + if (!checkExists(node)){ + transaction.delete(node, VERSION); + } + } + transaction.commit(); + } + + /* + * closed beta + */ + public void deleteAllChild(String path) throws KeeperException, InterruptedException { + if (path.indexOf(PathUtil.PATH_SEPARATOR) < -1){ + this.deleteOnlyCurrent(path); + return; + } + Transaction transaction = zooKeeper.transaction(); + //todo sync cache + new DefaultMutableTreeNode(); + Iterator nodes = PathUtil.depthToB(new DefaultMutableTreeNode()).iterator(); + while (nodes.hasNext()){ + String node = nodes.next(); + // contrast cache + if (checkExists(node)){ + try { + // TODO: exception + deleteOnlyCurrent(node); + } catch (Exception e){ + System.out.println(e.getMessage()); + } + } + } + transaction.commit(); + } + + private Watcher watcher; + public void watch(final String key, final EventListener eventListener){ + watcher = new Watcher() { + @Override + public void process(WatchedEvent event) { + } + }; } - public void deleteAllChild(String key) { - + public void close() throws InterruptedException { + zooKeeper.close(); } } diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java index ef5f485f02f2e..5774277fa03a7 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -22,23 +22,23 @@ public class UsualClientTest { static ZookeeperClient client = null; - @BeforeClass +// @BeforeClass public static void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); client = creator.setNamespace("test").authorization("digest", "digest".getBytes()).newClient(SERVERS, SESSION_TIMEOUT).start(); } - @Test +// @Test public void createRoot() throws KeeperException, InterruptedException { client.createRootNode(); } - @Test +// @Test public void createChild() throws KeeperException, InterruptedException { - client.createCurrentPathOnly("a/b/bb", "bbb11".getBytes(), CreateMode.PERSISTENT); + client.createCurrentOnly("a/b/bb", "bbb11".getBytes(), CreateMode.PERSISTENT); } - @Test +// @Test public void get() throws KeeperException, InterruptedException { String key = "a/b"; /*TreeCache cache = findTreeCache(key); @@ -56,13 +56,13 @@ private void getDirectly(String key) throws KeeperException, InterruptedExceptio System.out.println(new String(client.getData(key))); } - @Test +// @Test public void getDirectly() throws KeeperException, InterruptedException { String key = "a/b/bb"; getDirectly(key); } - @Test +// @Test public void isExisted() throws KeeperException, InterruptedException { String key = "a"; System.out.println(isExisted(key)); @@ -72,7 +72,7 @@ private boolean isExisted(String key) throws KeeperException, InterruptedExcepti return client.checkExists(key); } - @Test +// @Test public void getChildrenKeys() throws KeeperException, InterruptedException { String key = "a"; List result = client.getChildren(key); @@ -84,7 +84,7 @@ public int compare(final String o1, final String o2) { System.out.println(result); } - @Test +// @Test public void persist() throws KeeperException, InterruptedException { String key = "a"; String value = "aa"; @@ -99,7 +99,7 @@ private void update(String key, String value) throws KeeperException, Interrupte client.update(key, value.getBytes()); } - @Test +// @Test public void update() throws KeeperException, InterruptedException { String key = "a"; String value = "aaa"; @@ -110,7 +110,7 @@ public void update() throws KeeperException, InterruptedException { getDirectly(key); } - @Test +// @Test public void persistEphemeral() throws KeeperException, InterruptedException { String key = "bb"; String value = "b1b"; @@ -120,13 +120,14 @@ public void persistEphemeral() throws KeeperException, InterruptedException { client.createAllNeedPath(key, value.getBytes(), CreateMode.EPHEMERAL); } - @Test +// @Test public void watch() { - String key = ""; + String key = "a"; + final String path = key + "/"; EventListener eventListener; } - @Test +// @Test public void close() throws Exception { } From a2a2be6e7281aa21f78904a3fb83a48b38d84421 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 20 Apr 2018 15:27:29 +0800 Subject: [PATCH 011/351] version --- build.gradle | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/build.gradle b/build.gradle index dc6550cdc9ebe..c92cd5334fcc4 100644 --- a/build.gradle +++ b/build.gradle @@ -17,6 +17,7 @@ group 'com.saaavsaaa' version '1.0-SNAPSHOT' +//version '1.1' apply plugin: 'idea' apply plugin: 'java' @@ -40,17 +41,16 @@ uploadArchives { //为Pom文件做数字签名 beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } - repository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { - authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) + if (isReleaseBuild()){ + repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2") { + authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) + } + } else { + repository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { + authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) + } } -// repository(url: getReleaseRepositoryUrl()) { -// authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) -// } -// snapshotRepository(url: getSnapshotRepositoryUrl()) { -// authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) -// } - pom.project { name project.name packaging 'jar' @@ -93,25 +93,7 @@ task sourcesJar(type: Jar) { } def isReleaseBuild() { - return VERSION_NAME.contains("SNAPSHOT") == false -} - -def getReleaseRepositoryUrl() { - return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL - : "https://oss.sonatype.org/service/local/staging/deploy/maven2/" -} - -def getSnapshotRepositoryUrl() { - return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL - : "https://oss.sonatype.org/content/repositories/snapshots/" -} - -def getRepositoryUsername() { - return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : "" -} - -def getRepositoryPassword() { - return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : "" + return version.contains("SNAPSHOT") == false } artifacts { @@ -121,6 +103,5 @@ artifacts { } signing { -// required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") } sign configurations.archives } \ No newline at end of file From 16c504844e75e934cd8ad6409a659f9dd63b1845 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 20 Apr 2018 17:33:00 +0800 Subject: [PATCH 012/351] unit test --- build.gradle | 4 +- .../com/saaavsaaa/client/untils/PathUtil.java | 11 +- .../client/zookeeper/BaseClient.java | 7 + .../client/zookeeper/CacheClient.java | 2 +- .../client/zookeeper/ClientFactory.java | 6 +- ...{ZookeeperClient.java => UsualClient.java} | 79 +++++------ .../client/test/UsualClientTest.java | 124 +++++++++--------- 7 files changed, 124 insertions(+), 109 deletions(-) rename src/main/java/com/saaavsaaa/client/zookeeper/{ZookeeperClient.java => UsualClient.java} (62%) diff --git a/build.gradle b/build.gradle index c92cd5334fcc4..f7d140472b2ed 100644 --- a/build.gradle +++ b/build.gradle @@ -104,4 +104,6 @@ artifacts { signing { sign configurations.archives -} \ No newline at end of file +} + +// https://issues.sonatype.org/browse/OSSRH-39239 \ No newline at end of file diff --git a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java index 355ab7d1d95ce..fa01c7fe11bc4 100644 --- a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java +++ b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java @@ -10,20 +10,19 @@ public class PathUtil { public static final String PATH_SEPARATOR = "/"; public static String getRealPath(final String root, String path){ - path = adjustPath(root, path); - return new StringBuilder().append(root).append(path).toString(); + return adjustPath(root, path); } private static String adjustPath(final String root, String path){ if (StringUtil.isNullOrWhite(path)){ throw new IllegalArgumentException("path should have content!"); } - if (path.startsWith(root)){ - return path; - } if (!path.startsWith(PATH_SEPARATOR)){ path = PATH_SEPARATOR + path; } + if (!path.startsWith(root)){ + return root + path; + } return path; } @@ -45,7 +44,7 @@ public static Stack getPathReverseNodes(final String root, String path){ public static List getPathOrderNodes(final String root, String path){ path = adjustPath(root, path); - List paths = new LinkedList<>(); + List paths = new ArrayList<>(); int index = 1; int position = path.indexOf('/', index); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java index 61630eb869abf..7511080914cc6 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java @@ -7,6 +7,7 @@ import org.apache.zookeeper.data.ACL; import java.io.IOException; +import java.nio.charset.Charset; import java.util.List; import java.util.concurrent.CountDownLatch; @@ -16,6 +17,8 @@ public abstract class BaseClient { private static final CountDownLatch CONNECTED = new CountDownLatch(1); public static final int VERSION = -1; + public static final byte[] NOTHING_DATA = new byte[0]; + public static final Charset UTF_8 = Charset.forName("UTF-8"); private final String servers; private final int sessionTimeOut; @@ -34,6 +37,10 @@ public void start() throws IOException, InterruptedException { CONNECTED.await(); } + public ZooKeeper getZooKeeper(){ + return zooKeeper; + } + private Watcher connectWatcher() { return new Watcher(){ public void process(WatchedEvent event) { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index f92175876e815..75f555d1a71a0 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -3,7 +3,7 @@ /** * Created by aaa on 18-4-19. */ -public class CacheClient extends ZookeeperClient { +public class CacheClient extends UsualClient { CacheClient(String servers, int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 79e53a7ef7f24..66bbef5695770 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -8,7 +8,7 @@ public class ClientFactory { // private static final String CLIENT_EXCLUSIVE_NODE = "ZKC"; - private ZookeeperClient client; + private UsualClient client; private String namespace; private String scheme; private byte[] auth; @@ -21,11 +21,11 @@ public ClientFactory newCacheClient(final String servers, final int sessionTimeo } public ClientFactory newClient(final String servers, final int sessionTimeoutMilliseconds) { - client = new ZookeeperClient(servers, sessionTimeoutMilliseconds); + client = new UsualClient(servers, sessionTimeoutMilliseconds); return this; } - public synchronized ZookeeperClient start() throws IOException, InterruptedException { + public synchronized UsualClient start() throws IOException, InterruptedException { client.start(); client.setRootNode(namespace); client.setAuthorities(scheme , auth); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java similarity index 62% rename from src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java rename to src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 9ced2a2f31e79..d0cbfd8d2905c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ZookeeperClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -7,6 +7,7 @@ import org.apache.zookeeper.*; import javax.swing.tree.DefaultMutableTreeNode; +import java.nio.charset.Charset; import java.util.EventListener; import java.util.Iterator; import java.util.List; @@ -15,8 +16,8 @@ /* * cache */ -public class ZookeeperClient extends BaseClient { - ZookeeperClient(String servers, int sessionTimeoutMilliseconds) { +public class UsualClient extends BaseClient { + UsualClient(String servers, int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); } @@ -26,6 +27,10 @@ public void createRootNode() throws KeeperException, InterruptedException { } zooKeeper.create(rootNode, new byte[0], authorities, CreateMode.PERSISTENT); } + + public void deleteRoot() throws KeeperException, InterruptedException { + zooKeeper.delete(rootNode, VERSION); + } public byte[] getData(String key) throws KeeperException, InterruptedException { return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), false, null); @@ -39,46 +44,47 @@ public List getChildren(String key) throws KeeperException, InterruptedE return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); } - public void createCurrentOnly(String path, byte[] data, CreateMode createMode) throws KeeperException, InterruptedException { - zooKeeper.create(PathUtil.getRealPath(rootNode, path), data, authorities, createMode); + public void createCurrentOnly(String key, String value, CreateMode createMode) throws KeeperException, InterruptedException { + zooKeeper.create(PathUtil.getRealPath(rootNode, key), value.getBytes(UTF_8), authorities, createMode); } /* * closed beta */ - public void createAllNeedPath(String path, byte[] data, CreateMode createMode) throws KeeperException, InterruptedException { - if (path.indexOf(PathUtil.PATH_SEPARATOR) < -1){ - this.createCurrentOnly(path, data, createMode); + public void createAllNeedPath(String key, String value, CreateMode createMode) throws KeeperException, InterruptedException { + if (key.indexOf(PathUtil.PATH_SEPARATOR) < -1){ + this.createCurrentOnly(key, value, createMode); return; } Transaction transaction = zooKeeper.transaction(); //todo sync cache - Iterator nodes = PathUtil.getPathOrderNodes(rootNode, path).iterator(); - while (nodes.hasNext()){ - String node = nodes.next(); - // contrast cache - if (!checkExists(node)){ - try { - // TODO: exception - createInTransaction(path, data, createMode, transaction); - } catch (Exception e){ - System.out.println(e.getMessage()); - } + List nodes = PathUtil.getPathOrderNodes(rootNode, key); + for (int i = 0; i < nodes.size(); i++) { + // todo contrast cache + if (checkExists(nodes.get(i))){ + continue; + } + if (i == nodes.size() - 1){ + createInTransaction(nodes.get(i), value.getBytes(UTF_8), createMode, transaction); + } else { + createInTransaction(nodes.get(i), NOTHING_DATA, createMode, transaction); } } + + // org.apache.zookeeper.KeeperException$NodeExistsException: KeeperErrorCode = NodeExists transaction.commit(); } - public Transaction createInTransaction(String path, byte[] data, CreateMode createMode, Transaction transaction){ - return transaction.create(PathUtil.getRealPath(rootNode, path), data, authorities, createMode); + private Transaction createInTransaction(String key, byte[] data, CreateMode createMode, Transaction transaction){ + return transaction.create(PathUtil.getRealPath(rootNode, key), data, authorities, createMode); } - public void updateInTransaction(String key, byte[] data) throws KeeperException, InterruptedException { - zooKeeper.transaction().setData(PathUtil.getRealPath(rootNode, key), data, VERSION).commit(); + public void updateInTransaction(String key, String value) throws KeeperException, InterruptedException { + zooKeeper.transaction().setData(PathUtil.getRealPath(rootNode, key), value.getBytes(UTF_8), VERSION).commit(); } - public void update(String key, byte[] data) throws KeeperException, InterruptedException { - zooKeeper.setData(PathUtil.getRealPath(rootNode, key), data, VERSION); + public void update(String key, String value) throws KeeperException, InterruptedException { + zooKeeper.setData(PathUtil.getRealPath(rootNode, key), value.getBytes(UTF_8), VERSION); } public void deleteOnlyCurrent(String key) throws KeeperException, InterruptedException { @@ -92,18 +98,18 @@ public void deleteOnlyCurrent(String key, AsyncCallback.VoidCallback callback, O /* * closed beta */ - public void deleteCurrentBranch(String path) throws KeeperException, InterruptedException { - if (path.indexOf(PathUtil.PATH_SEPARATOR) < -1){ - this.deleteOnlyCurrent(path); + public void deleteCurrentBranch(String key) throws KeeperException, InterruptedException { + if (key.indexOf(PathUtil.PATH_SEPARATOR) < -1){ + this.deleteOnlyCurrent(key); return; } Transaction transaction = zooKeeper.transaction(); //todo branch check - Stack pathStack = PathUtil.getPathReverseNodes(rootNode, path); + Stack pathStack = PathUtil.getPathReverseNodes(rootNode, key); while (!pathStack.empty()){ String node = pathStack.pop(); // contrast cache - if (!checkExists(node)){ + if (checkExists(node)){ transaction.delete(node, VERSION); } } @@ -113,9 +119,9 @@ public void deleteCurrentBranch(String path) throws KeeperException, Interrupted /* * closed beta */ - public void deleteAllChild(String path) throws KeeperException, InterruptedException { - if (path.indexOf(PathUtil.PATH_SEPARATOR) < -1){ - this.deleteOnlyCurrent(path); + public void deleteAllChild(String key) throws KeeperException, InterruptedException { + if (key.indexOf(PathUtil.PATH_SEPARATOR) < -1){ + this.deleteOnlyCurrent(key); return; } Transaction transaction = zooKeeper.transaction(); @@ -126,14 +132,11 @@ public void deleteAllChild(String path) throws KeeperException, InterruptedExcep String node = nodes.next(); // contrast cache if (checkExists(node)){ - try { - // TODO: exception - deleteOnlyCurrent(node); - } catch (Exception e){ - System.out.println(e.getMessage()); - } + deleteOnlyCurrent(node); } } + + // TODO: exception transaction.commit(); } diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java index 5774277fa03a7..35baf5ea82588 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -1,9 +1,11 @@ package com.saaavsaaa.client.test; import com.saaavsaaa.client.zookeeper.ClientFactory; -import com.saaavsaaa.client.zookeeper.ZookeeperClient; +import com.saaavsaaa.client.zookeeper.UsualClient; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; import org.junit.BeforeClass; import org.junit.Test; @@ -17,118 +19,120 @@ * Created by aaa on 18-4-18. */ public class UsualClientTest { - static final String SERVERS = "192.168.2.44:2181"; - static final int SESSION_TIMEOUT = 200000;//ms + private static final String SERVERS = "192.168.2.44:2181"; + private static final int SESSION_TIMEOUT = 2000000;//ms + private static final String ROOT = "test"; + private static final String AUTH = "digest"; - static ZookeeperClient client = null; + private static UsualClient client = null; -// @BeforeClass + @BeforeClass public static void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); - client = creator.setNamespace("test").authorization("digest", "digest".getBytes()).newClient(SERVERS, SESSION_TIMEOUT).start(); + client = creator.setNamespace(ROOT).authorization(AUTH, AUTH.getBytes()).newClient(SERVERS, SESSION_TIMEOUT).start(); } -// @Test + @Test public void createRoot() throws KeeperException, InterruptedException { client.createRootNode(); + assert client.getZooKeeper().exists("/" + ROOT, false) != null; + client.deleteRoot(); + assert client.getZooKeeper().exists("/" + ROOT, false) == null; } -// @Test + @Test public void createChild() throws KeeperException, InterruptedException { - client.createCurrentOnly("a/b/bb", "bbb11".getBytes(), CreateMode.PERSISTENT); - } - -// @Test - public void get() throws KeeperException, InterruptedException { - String key = "a/b"; - /*TreeCache cache = findTreeCache(key); - if (null == cache) { - return getDirectly(key); - } - ChildData resultInCache = cache.getCurrentData(key); - if (null != resultInCache) { - return null == resultInCache.getData() ? null : new String(resultInCache.getData(), Charsets.UTF_8); - }*/ - getDirectly(key); - } - - private void getDirectly(String key) throws KeeperException, InterruptedException { - System.out.println(new String(client.getData(key))); + String key = "a/b/bb"; + client.createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); + assert client.getZooKeeper().exists("/" + ROOT + "/" + key, false) != null; + client.deleteCurrentBranch(key); + assert client.getZooKeeper().exists("/" + ROOT + "/" + key, false) == null; } -// @Test - public void getDirectly() throws KeeperException, InterruptedException { + @Test + public void isExisted() throws KeeperException, InterruptedException { String key = "a/b/bb"; - getDirectly(key); + client.createAllNeedPath(key, "", CreateMode.PERSISTENT); + assert isExisted(key); + client.deleteCurrentBranch(key); } -// @Test - public void isExisted() throws KeeperException, InterruptedException { + @Test + public void get() throws KeeperException, InterruptedException { + client.createAllNeedPath("a/b", "bbb11", CreateMode.PERSISTENT); String key = "a"; - System.out.println(isExisted(key)); + // TODO: cache + assert getDirectly(key).equals(""); + key = "a/b"; + assert getDirectly(key).equals("bbb11"); + client.deleteCurrentBranch("a/b"); + } + + private String getDirectly(String key) throws KeeperException, InterruptedException { + return new String(client.getData(key)); } private boolean isExisted(String key) throws KeeperException, InterruptedException { return client.checkExists(key); } -// @Test + @Test public void getChildrenKeys() throws KeeperException, InterruptedException { - String key = "a"; - List result = client.getChildren(key); + String key = "a/b"; + String current = "a"; + client.createAllNeedPath(key, "", CreateMode.PERSISTENT); + List result = client.getChildren(current); Collections.sort(result, new Comparator() { public int compare(final String o1, final String o2) { return o2.compareTo(o1); } }); - System.out.println(result); + assert result.get(0).equals("b"); + client.deleteCurrentBranch(key); } -// @Test + @Test public void persist() throws KeeperException, InterruptedException { String key = "a"; String value = "aa"; + String newValue = "aaa"; if (!isExisted(key)) { - client.createAllNeedPath(key, value.getBytes(), CreateMode.PERSISTENT); + client.createAllNeedPath(key, value, CreateMode.PERSISTENT); } else { update(key, value); } - } - private void update(String key, String value) throws KeeperException, InterruptedException { - client.update(key, value.getBytes()); + assert getDirectly(key).equals(value); + + update(key, newValue); + assert getDirectly(key).equals(newValue); + client.deleteCurrentBranch(key); } -// @Test - public void update() throws KeeperException, InterruptedException { - String key = "a"; - String value = "aaa"; - update(key, value); //inTransaction().check().forPath(key).and().setData().forPath().and().commit(); - getDirectly(key); - value = "aa"; - client.updateInTransaction(key, value.getBytes()); - getDirectly(key); + private void update(String key, String value) throws KeeperException, InterruptedException { + client.update(key, value); } -// @Test + @Test public void persistEphemeral() throws KeeperException, InterruptedException { - String key = "bb"; + /*String key = "a/b/bb"; String value = "b1b"; + client.createAllNeedPath(key, value, CreateMode.PERSISTENT); if (isExisted(key)) { - client.deleteCurrentBranch(key); + client.deleteAllChild(key); } - client.createAllNeedPath(key, value.getBytes(), CreateMode.EPHEMERAL); + client.createAllNeedPath(key, value, CreateMode.EPHEMERAL);*/ } -// @Test + @Test public void watch() { - String key = "a"; - final String path = key + "/"; + String key = ""; EventListener eventListener; } -// @Test + @Test public void close() throws Exception { - + client.close(); + assert client.getZooKeeper().getState() == ZooKeeper.States.CLOSED; } } From 2a1d59f3ddb77e39f22ebc1247814986faf40b0c Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 20 Apr 2018 18:22:31 +0800 Subject: [PATCH 013/351] unit test --- .../saaavsaaa/client/test/UsualClientTest.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java index 35baf5ea82588..383bb4cc73c4c 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -5,8 +5,8 @@ import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.ZooKeeper; -import org.apache.zookeeper.data.Stat; -import org.junit.BeforeClass; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import java.io.IOException; @@ -20,18 +20,23 @@ */ public class UsualClientTest { private static final String SERVERS = "192.168.2.44:2181"; - private static final int SESSION_TIMEOUT = 2000000;//ms + private static final int SESSION_TIMEOUT = 200000;//ms private static final String ROOT = "test"; private static final String AUTH = "digest"; - private static UsualClient client = null; + private UsualClient client = null; - @BeforeClass - public static void start() throws IOException, InterruptedException { + @Before + public void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); client = creator.setNamespace(ROOT).authorization(AUTH, AUTH.getBytes()).newClient(SERVERS, SESSION_TIMEOUT).start(); } + @After + public void stop() throws InterruptedException { + client.close(); + } + @Test public void createRoot() throws KeeperException, InterruptedException { client.createRootNode(); From a638d67d85b1a90db430824e525e3e69edf15d3d Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 20 Apr 2018 18:24:52 +0800 Subject: [PATCH 014/351] unit test --- .../java/com/saaavsaaa/client/test/CacheClientTest.java | 7 +++++++ .../java/com/saaavsaaa/client/test/UsualClientTest.java | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/saaavsaaa/client/test/CacheClientTest.java diff --git a/src/test/java/com/saaavsaaa/client/test/CacheClientTest.java b/src/test/java/com/saaavsaaa/client/test/CacheClientTest.java new file mode 100644 index 0000000000000..5889a49739daa --- /dev/null +++ b/src/test/java/com/saaavsaaa/client/test/CacheClientTest.java @@ -0,0 +1,7 @@ +package com.saaavsaaa.client.test; + +/** + * Created by aaa on 18-4-20. + */ +public class CacheClientTest { +} diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java index 383bb4cc73c4c..b902174f993b9 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -7,6 +7,7 @@ import org.apache.zookeeper.ZooKeeper; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import java.io.IOException; @@ -118,15 +119,16 @@ private void update(String key, String value) throws KeeperException, Interrupte client.update(key, value); } + @Ignore @Test public void persistEphemeral() throws KeeperException, InterruptedException { - /*String key = "a/b/bb"; + String key = "a/b/bb"; String value = "b1b"; client.createAllNeedPath(key, value, CreateMode.PERSISTENT); if (isExisted(key)) { client.deleteAllChild(key); } - client.createAllNeedPath(key, value, CreateMode.EPHEMERAL);*/ + client.createAllNeedPath(key, value, CreateMode.EPHEMERAL); } @Test From eb48a1a34630f8300faf7c7ca13578035dfe6e0b Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 23 Apr 2018 12:32:07 +0800 Subject: [PATCH 015/351] delete children --- .../client/zookeeper/CacheClient.java | 28 ++++++++++++++ .../client/zookeeper/UsualClient.java | 38 +++++++++---------- .../client/test/UsualClientTest.java | 16 +++++++- 3 files changed, 59 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 75f555d1a71a0..2f2602e2e1e31 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -1,5 +1,12 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.untils.PathUtil; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Transaction; + +import javax.swing.tree.DefaultMutableTreeNode; +import java.util.Iterator; + /** * Created by aaa on 18-4-19. */ @@ -7,4 +14,25 @@ public class CacheClient extends UsualClient { CacheClient(String servers, int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); } + + public void deleteAllChild(String key) throws KeeperException, InterruptedException { + if (key.indexOf(PathUtil.PATH_SEPARATOR) < -1){ + this.deleteOnlyCurrent(key); + return; + } + Transaction transaction = zooKeeper.transaction(); + //todo sync cache + new DefaultMutableTreeNode(); + Iterator nodes = PathUtil.depthToB(new DefaultMutableTreeNode()).iterator(); + while (nodes.hasNext()){ + String node = nodes.next(); + // contrast cache + if (checkExists(node)){ + deleteOnlyCurrent(node); + } + } + + // TODO: exception + transaction.commit(); + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index d0cbfd8d2905c..c15db813e5db9 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -15,6 +15,7 @@ /* * cache +* todo Sequential */ public class UsualClient extends BaseClient { UsualClient(String servers, int sessionTimeoutMilliseconds) { @@ -62,8 +63,10 @@ public void createAllNeedPath(String key, String value, CreateMode createMode) t for (int i = 0; i < nodes.size(); i++) { // todo contrast cache if (checkExists(nodes.get(i))){ + System.out.println("exist:" + nodes.get(i)); continue; } + System.out.println("not exist:" + nodes.get(i)); if (i == nodes.size() - 1){ createInTransaction(nodes.get(i), value.getBytes(UTF_8), createMode, transaction); } else { @@ -79,12 +82,9 @@ private Transaction createInTransaction(String key, byte[] data, CreateMode crea return transaction.create(PathUtil.getRealPath(rootNode, key), data, authorities, createMode); } - public void updateInTransaction(String key, String value) throws KeeperException, InterruptedException { - zooKeeper.transaction().setData(PathUtil.getRealPath(rootNode, key), value.getBytes(UTF_8), VERSION).commit(); - } - public void update(String key, String value) throws KeeperException, InterruptedException { - zooKeeper.setData(PathUtil.getRealPath(rootNode, key), value.getBytes(UTF_8), VERSION); + String realPath = PathUtil.getRealPath(rootNode, key); + zooKeeper.transaction().check(realPath, VERSION).setData(realPath, value.getBytes(UTF_8), VERSION).commit(); } public void deleteOnlyCurrent(String key) throws KeeperException, InterruptedException { @@ -118,26 +118,22 @@ public void deleteCurrentBranch(String key) throws KeeperException, InterruptedE /* * closed beta + * 当前实现方法用于缓存方式 + * 缓存实现后此类判断换为异常方式(包括创建) */ public void deleteAllChild(String key) throws KeeperException, InterruptedException { - if (key.indexOf(PathUtil.PATH_SEPARATOR) < -1){ - this.deleteOnlyCurrent(key); - return; - } - Transaction transaction = zooKeeper.transaction(); - //todo sync cache - new DefaultMutableTreeNode(); - Iterator nodes = PathUtil.depthToB(new DefaultMutableTreeNode()).iterator(); - while (nodes.hasNext()){ - String node = nodes.next(); - // contrast cache - if (checkExists(node)){ - deleteOnlyCurrent(node); + String realPath = PathUtil.getRealPath(rootNode, key); + try { + this.deleteOnlyCurrent(realPath); + }catch (KeeperException.NotEmptyException ee){ + List children = this.getChildren(realPath); + for (String child : children) { + child = realPath + PathUtil.PATH_SEPARATOR + child; + this.deleteAllChild(child); } + } catch (KeeperException.NoNodeException ee){ + System.out.println(ee.getMessage()); } - - // TODO: exception - transaction.commit(); } private Watcher watcher; diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java index b902174f993b9..9ab555a5438b1 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -118,8 +118,7 @@ public void persist() throws KeeperException, InterruptedException { private void update(String key, String value) throws KeeperException, InterruptedException { client.update(key, value); } - - @Ignore + @Test public void persistEphemeral() throws KeeperException, InterruptedException { String key = "a/b/bb"; @@ -131,6 +130,19 @@ public void persistEphemeral() throws KeeperException, InterruptedException { client.createAllNeedPath(key, value, CreateMode.EPHEMERAL); } + @Test + public void delAllChildren() throws KeeperException, InterruptedException { + String key = "a/b/bb"; + client.createAllNeedPath(key, "bb", CreateMode.PERSISTENT); + key = "a/c/cc"; + client.createAllNeedPath(key, "cc", CreateMode.PERSISTENT); + assert client.getZooKeeper().exists("/" + ROOT + "/" + key, false) != null; + client.deleteAllChild("a"); + assert client.getZooKeeper().exists("/" + ROOT + "/" + key, false) == null; + assert client.getZooKeeper().exists("/" + ROOT + "/a", false) != null; + client.createAllNeedPath("a", "bb", CreateMode.PERSISTENT); + } + @Test public void watch() { String key = ""; From 21b7d854943f7b25762645864e575a693cc18fee Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 23 Apr 2018 15:30:29 +0800 Subject: [PATCH 016/351] Ephemeral node create --- .../client/zookeeper/UsualClient.java | 11 +++++-- .../client/test/UsualClientTest.java | 30 +++++++++++-------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index c15db813e5db9..c791a56cc7c30 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -83,6 +83,10 @@ private Transaction createInTransaction(String key, byte[] data, CreateMode crea } public void update(String key, String value) throws KeeperException, InterruptedException { + zooKeeper.setData(PathUtil.getRealPath(rootNode, key), value.getBytes(UTF_8), VERSION); + } + + public void updateInTransaction(String key, String value) throws KeeperException, InterruptedException { String realPath = PathUtil.getRealPath(rootNode, key); zooKeeper.transaction().check(realPath, VERSION).setData(realPath, value.getBytes(UTF_8), VERSION).commit(); } @@ -95,9 +99,6 @@ public void deleteOnlyCurrent(String key, AsyncCallback.VoidCallback callback, O zooKeeper.delete(PathUtil.getRealPath(rootNode, key), VERSION, callback, ctx); } - /* - * closed beta - */ public void deleteCurrentBranch(String key) throws KeeperException, InterruptedException { if (key.indexOf(PathUtil.PATH_SEPARATOR) < -1){ this.deleteOnlyCurrent(key); @@ -125,14 +126,18 @@ public void deleteAllChild(String key) throws KeeperException, InterruptedExcept String realPath = PathUtil.getRealPath(rootNode, key); try { this.deleteOnlyCurrent(realPath); + System.out.println("delete : " + realPath); }catch (KeeperException.NotEmptyException ee){ List children = this.getChildren(realPath); for (String child : children) { child = realPath + PathUtil.PATH_SEPARATOR + child; this.deleteAllChild(child); } + this.deleteOnlyCurrent(realPath); + System.out.println("delete : " + realPath); } catch (KeeperException.NoNodeException ee){ System.out.println(ee.getMessage()); + return; } } diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java index 9ab555a5438b1..6be97395a31d9 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.test; +import com.saaavsaaa.client.untils.PathUtil; import com.saaavsaaa.client.zookeeper.ClientFactory; import com.saaavsaaa.client.zookeeper.UsualClient; import org.apache.zookeeper.CreateMode; @@ -41,18 +42,18 @@ public void stop() throws InterruptedException { @Test public void createRoot() throws KeeperException, InterruptedException { client.createRootNode(); - assert client.getZooKeeper().exists("/" + ROOT, false) != null; + assert client.getZooKeeper().exists(PathUtil.PATH_SEPARATOR + ROOT, false) != null; client.deleteRoot(); - assert client.getZooKeeper().exists("/" + ROOT, false) == null; + assert client.getZooKeeper().exists(PathUtil.PATH_SEPARATOR + ROOT, false) == null; } @Test public void createChild() throws KeeperException, InterruptedException { String key = "a/b/bb"; client.createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); - assert client.getZooKeeper().exists("/" + ROOT + "/" + key, false) != null; + assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key)/*"/" + ROOT + "/" + key*/, false) != null; client.deleteCurrentBranch(key); - assert client.getZooKeeper().exists("/" + ROOT + "/" + key, false) == null; + assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key)/*"/" + ROOT + "/" + key*/, false) == null; } @Test @@ -74,7 +75,7 @@ public void get() throws KeeperException, InterruptedException { client.deleteCurrentBranch("a/b"); } - private String getDirectly(String key) throws KeeperException, InterruptedException { + private String getDirectly(String key) throws KeeperException, InterruptedException { return new String(client.getData(key)); } @@ -116,7 +117,7 @@ public void persist() throws KeeperException, InterruptedException { } private void update(String key, String value) throws KeeperException, InterruptedException { - client.update(key, value); + client.updateInTransaction(key, value); } @Test @@ -124,10 +125,12 @@ public void persistEphemeral() throws KeeperException, InterruptedException { String key = "a/b/bb"; String value = "b1b"; client.createAllNeedPath(key, value, CreateMode.PERSISTENT); - if (isExisted(key)) { - client.deleteAllChild(key); - } + assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), null).getEphemeralOwner() == 0; + client.deleteAllChild(key); + assert !isExisted(key); client.createAllNeedPath(key, value, CreateMode.EPHEMERAL); + assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), null).getEphemeralOwner() != 0; // Ephemeral node connection session id + client.deleteCurrentBranch(key); } @Test @@ -136,11 +139,12 @@ public void delAllChildren() throws KeeperException, InterruptedException { client.createAllNeedPath(key, "bb", CreateMode.PERSISTENT); key = "a/c/cc"; client.createAllNeedPath(key, "cc", CreateMode.PERSISTENT); - assert client.getZooKeeper().exists("/" + ROOT + "/" + key, false) != null; + System.out.println(client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, "a"), null).getNumChildren()); // nearest children count + assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), false) != null; client.deleteAllChild("a"); - assert client.getZooKeeper().exists("/" + ROOT + "/" + key, false) == null; - assert client.getZooKeeper().exists("/" + ROOT + "/a", false) != null; - client.createAllNeedPath("a", "bb", CreateMode.PERSISTENT); + assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), false) == null; + assert client.getZooKeeper().exists("/" + ROOT, false) != null; + client.deleteRoot(); } @Test From c6d21ff2edeef1139e3169d0e5267c173e3d82cf Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 23 Apr 2018 16:05:20 +0800 Subject: [PATCH 017/351] final --- .../client/zookeeper/CacheClient.java | 31 ++++++++--------- .../client/zookeeper/UsualClient.java | 33 +++++++++++-------- .../client/test/UsualClientTest.java | 6 ++-- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 2f2602e2e1e31..e0f01a2dc9fd0 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -10,29 +10,24 @@ /** * Created by aaa on 18-4-19. */ -public class CacheClient extends UsualClient { +public final class CacheClient extends UsualClient { CacheClient(String servers, int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); } - public void deleteAllChild(String key) throws KeeperException, InterruptedException { - if (key.indexOf(PathUtil.PATH_SEPARATOR) < -1){ - this.deleteOnlyCurrent(key); - return; - } + /* + * closed beta + * 当前实现方法用于缓存方式 + * 缓存实现后此类判断换为异常方式(包括创建) + * 用事务不能用异常 + */ + @Override + public void deleteAllChild(final String key) throws KeeperException, InterruptedException { Transaction transaction = zooKeeper.transaction(); - //todo sync cache - new DefaultMutableTreeNode(); - Iterator nodes = PathUtil.depthToB(new DefaultMutableTreeNode()).iterator(); - while (nodes.hasNext()){ - String node = nodes.next(); - // contrast cache - if (checkExists(node)){ - deleteOnlyCurrent(node); - } - } - - // TODO: exception + this.deleteAllChild(key, transaction); transaction.commit(); } + + private void deleteAllChild(final String key, final Transaction transaction) throws KeeperException, InterruptedException { + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index c791a56cc7c30..a7231e4e071ec 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -22,37 +22,37 @@ public class UsualClient extends BaseClient { super(servers, sessionTimeoutMilliseconds); } - public void createRootNode() throws KeeperException, InterruptedException { + public void createNamespace() throws KeeperException, InterruptedException { if (checkExists(rootNode)){ return; } zooKeeper.create(rootNode, new byte[0], authorities, CreateMode.PERSISTENT); } - public void deleteRoot() throws KeeperException, InterruptedException { + public void deleteNamespace() throws KeeperException, InterruptedException { zooKeeper.delete(rootNode, VERSION); } - public byte[] getData(String key) throws KeeperException, InterruptedException { + public byte[] getData(final String key) throws KeeperException, InterruptedException { return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), false, null); } - public boolean checkExists(String key) throws KeeperException, InterruptedException { + public boolean checkExists(final String key) throws KeeperException, InterruptedException { return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), false); } - public List getChildren(String key) throws KeeperException, InterruptedException { + public List getChildren(final String key) throws KeeperException, InterruptedException { return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); } - public void createCurrentOnly(String key, String value, CreateMode createMode) throws KeeperException, InterruptedException { + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { zooKeeper.create(PathUtil.getRealPath(rootNode, key), value.getBytes(UTF_8), authorities, createMode); } /* * closed beta */ - public void createAllNeedPath(String key, String value, CreateMode createMode) throws KeeperException, InterruptedException { + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { if (key.indexOf(PathUtil.PATH_SEPARATOR) < -1){ this.createCurrentOnly(key, value, createMode); return; @@ -78,28 +78,32 @@ public void createAllNeedPath(String key, String value, CreateMode createMode) t transaction.commit(); } - private Transaction createInTransaction(String key, byte[] data, CreateMode createMode, Transaction transaction){ + private Transaction createInTransaction(final String key, byte[] data, final CreateMode createMode, final Transaction transaction){ return transaction.create(PathUtil.getRealPath(rootNode, key), data, authorities, createMode); } - public void update(String key, String value) throws KeeperException, InterruptedException { + public void update(final String key, final String value) throws KeeperException, InterruptedException { zooKeeper.setData(PathUtil.getRealPath(rootNode, key), value.getBytes(UTF_8), VERSION); } - public void updateInTransaction(String key, String value) throws KeeperException, InterruptedException { + public void updateInTransaction(final String key, final String value) throws KeeperException, InterruptedException { String realPath = PathUtil.getRealPath(rootNode, key); zooKeeper.transaction().check(realPath, VERSION).setData(realPath, value.getBytes(UTF_8), VERSION).commit(); } - public void deleteOnlyCurrent(String key) throws KeeperException, InterruptedException { + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { zooKeeper.delete(PathUtil.getRealPath(rootNode, key), VERSION); } - public void deleteOnlyCurrent(String key, AsyncCallback.VoidCallback callback, Object ctx) throws KeeperException, InterruptedException { + private void deleteOnlyCurrent(final String key, final Transaction transaction) throws KeeperException, InterruptedException { + zooKeeper.delete(PathUtil.getRealPath(rootNode, key), VERSION); + } + + public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { zooKeeper.delete(PathUtil.getRealPath(rootNode, key), VERSION, callback, ctx); } - public void deleteCurrentBranch(String key) throws KeeperException, InterruptedException { + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { if (key.indexOf(PathUtil.PATH_SEPARATOR) < -1){ this.deleteOnlyCurrent(key); return; @@ -121,8 +125,9 @@ public void deleteCurrentBranch(String key) throws KeeperException, InterruptedE * closed beta * 当前实现方法用于缓存方式 * 缓存实现后此类判断换为异常方式(包括创建) + * 用事务不能用异常 */ - public void deleteAllChild(String key) throws KeeperException, InterruptedException { + public void deleteAllChild(final String key) throws KeeperException, InterruptedException { String realPath = PathUtil.getRealPath(rootNode, key); try { this.deleteOnlyCurrent(realPath); diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java index 6be97395a31d9..766072e92f907 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -41,9 +41,9 @@ public void stop() throws InterruptedException { @Test public void createRoot() throws KeeperException, InterruptedException { - client.createRootNode(); + client.createNamespace(); assert client.getZooKeeper().exists(PathUtil.PATH_SEPARATOR + ROOT, false) != null; - client.deleteRoot(); + client.deleteNamespace(); assert client.getZooKeeper().exists(PathUtil.PATH_SEPARATOR + ROOT, false) == null; } @@ -144,7 +144,7 @@ public void delAllChildren() throws KeeperException, InterruptedException { client.deleteAllChild("a"); assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), false) == null; assert client.getZooKeeper().exists("/" + ROOT, false) != null; - client.deleteRoot(); + client.deleteNamespace(); } @Test From 5ee796b42e8c759596244af35ec13e8b633374c1 Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 23 Apr 2018 18:57:45 +0800 Subject: [PATCH 018/351] watcher --- .../com/saaavsaaa/client/untils/Listener.java | 10 ++ .../com/saaavsaaa/client/untils/PathUtil.java | 54 ++++++++- .../client/zookeeper/UsualClient.java | 17 ++- .../client/test/UsualClientTest.java | 112 ++++++++++++++++-- 4 files changed, 178 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/untils/Listener.java diff --git a/src/main/java/com/saaavsaaa/client/untils/Listener.java b/src/main/java/com/saaavsaaa/client/untils/Listener.java new file mode 100644 index 0000000000000..0e792e1fb0f1e --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/untils/Listener.java @@ -0,0 +1,10 @@ +package com.saaavsaaa.client.untils; + +import org.apache.zookeeper.WatchedEvent; + +/** + * Created by aaa on 18-4-23. + */ +public interface Listener { + void process(WatchedEvent event); +} diff --git a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java index fa01c7fe11bc4..4758133f1bbc7 100644 --- a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java +++ b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java @@ -9,14 +9,17 @@ public class PathUtil { public static final String PATH_SEPARATOR = "/"; - public static String getRealPath(final String root, String path){ + public static String getRealPath(final String root, final String path){ return adjustPath(root, path); } - private static String adjustPath(final String root, String path){ + private static String adjustPath(String root, String path){ if (StringUtil.isNullOrWhite(path)){ throw new IllegalArgumentException("path should have content!"); } + if (!root.startsWith(PATH_SEPARATOR)){ + root = PATH_SEPARATOR + root; + } if (!path.startsWith(PATH_SEPARATOR)){ path = PATH_SEPARATOR + path; } @@ -92,4 +95,51 @@ public static List depthToB(TreeNode root) { } return lists; } + + //isSequential + /* + * ignore invalid char and // /./ /../ + */ + public static String checkPath(String path) throws IllegalArgumentException { + if(path == null || path.length() == 0) { + throw new IllegalArgumentException("path should not be null"); + } + if(path.charAt(0) != 47 || path.charAt(path.length() - 1) == 47){ + path = PATH_SEPARATOR + path; + } + + if(path.charAt(path.length() - 1) == 47){ + path = PATH_SEPARATOR + path; + } + + char previous = 47; + char[] chars = path.toCharArray(); + StringBuilder builder = new StringBuilder(); + + for(int i = 1; i < chars.length; ++i) { + char c = chars[i]; + if (c == 0 || (c == 47 && previous == 47)) { + continue; + } + if (c == 46) { + // ignore /./ /../ + boolean preWarn = previous == 47 || (previous == 46 && chars[i - 2] == 47); + if (previous == 47 && (i + 1 == chars.length || chars[i + 1] == 47)) { + i++; + continue; + } + if ((previous == 46 && chars[i - 2] == 47) && (i + 1 == chars.length || chars[i + 1] == 47)) { + i+=2; + continue; + } + } + if (c > 0 && c < 31 || c > 127 && c < 159 || c > '\ud800' && c < '\uf8ff' || c > '\ufff0' && c < '\uffff') { + continue; + } + + previous = chars[i]; + builder.append(c); + } + return builder.toString(); + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index a7231e4e071ec..66817ac0c1322 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -3,13 +3,11 @@ * Created by aaa on 18-4-18. */ +import com.saaavsaaa.client.untils.Listener; import com.saaavsaaa.client.untils.PathUtil; import org.apache.zookeeper.*; -import javax.swing.tree.DefaultMutableTreeNode; -import java.nio.charset.Charset; import java.util.EventListener; -import java.util.Iterator; import java.util.List; import java.util.Stack; @@ -34,7 +32,11 @@ public void deleteNamespace() throws KeeperException, InterruptedException { } public byte[] getData(final String key) throws KeeperException, InterruptedException { - return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), false, null); + return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), watcher, null); + } + + public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { + zooKeeper.getData(PathUtil.getRealPath(rootNode, key), false, callback, ctx); } public boolean checkExists(final String key) throws KeeperException, InterruptedException { @@ -74,7 +76,7 @@ public void createAllNeedPath(final String key, final String value, final Create } } - // org.apache.zookeeper.KeeperException$NodeExistsException: KeeperErrorCode = NodeExists + // todo org.apache.zookeeper.KeeperException$NodeExistsException: KeeperErrorCode = NodeExists transaction.commit(); } @@ -147,10 +149,12 @@ public void deleteAllChild(final String key) throws KeeperException, Interrupted } private Watcher watcher; - public void watch(final String key, final EventListener eventListener){ + public void watch(final String key, final Listener eventListener){ watcher = new Watcher() { + @Override public void process(WatchedEvent event) { + } }; } @@ -159,3 +163,4 @@ public void close() throws InterruptedException { zooKeeper.close(); } } + diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java index 766072e92f907..d2d2eab84eb2f 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -1,21 +1,21 @@ package com.saaavsaaa.client.test; +import com.saaavsaaa.client.untils.Listener; import com.saaavsaaa.client.untils.PathUtil; +import com.saaavsaaa.client.zookeeper.BaseClient; import com.saaavsaaa.client.zookeeper.ClientFactory; import com.saaavsaaa.client.zookeeper.UsualClient; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.*; +import org.apache.zookeeper.data.Stat; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import java.io.IOException; import java.util.Collections; import java.util.Comparator; -import java.util.EventListener; import java.util.List; +import java.util.concurrent.CountDownLatch; /** * Created by aaa on 18-4-18. @@ -75,6 +75,26 @@ public void get() throws KeeperException, InterruptedException { client.deleteCurrentBranch("a/b"); } + @Test + public void asynGet() throws KeeperException, InterruptedException { + CountDownLatch ready = new CountDownLatch(1); + String key = "a/b"; + String value = "bbb11"; + client.createAllNeedPath(key, value, CreateMode.PERSISTENT); + AsyncCallback.DataCallback callback = new AsyncCallback.DataCallback() { + @Override + public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) { + String result = new String(data); + System.out.println(new StringBuffer().append("rc:").append(rc).append(",path:").append(path).append(",ctx:").append(ctx).append(",stat:").append(stat)); + assert result.equals(ctx); + ready.countDown(); + } + }; + client.getData(key, callback, value); + ready.await(); + client.deleteCurrentBranch("a/b"); + } + private String getDirectly(String key) throws KeeperException, InterruptedException { return new String(client.getData(key)); } @@ -125,10 +145,15 @@ public void persistEphemeral() throws KeeperException, InterruptedException { String key = "a/b/bb"; String value = "b1b"; client.createAllNeedPath(key, value, CreateMode.PERSISTENT); - assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), null).getEphemeralOwner() == 0; +// assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), null).getEphemeralOwner() == 0; + Stat stat = new Stat(); + client.getZooKeeper().getData(PathUtil.getRealPath(ROOT, key), false, stat); + assert stat.getEphemeralOwner() == 0; + client.deleteAllChild(key); assert !isExisted(key); client.createAllNeedPath(key, value, CreateMode.EPHEMERAL); + assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), null).getEphemeralOwner() != 0; // Ephemeral node connection session id client.deleteCurrentBranch(key); } @@ -150,7 +175,41 @@ public void delAllChildren() throws KeeperException, InterruptedException { @Test public void watch() { String key = ""; - EventListener eventListener; + + EventListener eventListener = new EventListener() { + @Override + public void onChange(DataChangedEvent event) { + System.out.println(event.getKey() + " : " + event.getValue()); + } + }; + Listener listener = new Listener() { + @Override + public void process(WatchedEvent event) { + byte[] data = new byte[0]; + try { + data = client.getZooKeeper().getData(event.getPath(),false, null); + } catch (KeeperException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + eventListener.onChange(new DataChangedEvent(getEventType(event), event.getPath(), + null == data ? null : new String(data, BaseClient.UTF_8))); + } + + private DataChangedEvent.Type getEventType(final WatchedEvent event) { + switch (event.getType()) { + case NodeDataChanged: + case NodeChildrenChanged: + return DataChangedEvent.Type.UPDATED; + case NodeDeleted: + return DataChangedEvent.Type.DELETED; + default: + return DataChangedEvent.Type.IGNORED; + } + } + }; + client.watch(key, listener); } @Test @@ -159,3 +218,42 @@ public void close() throws Exception { assert client.getZooKeeper().getState() == ZooKeeper.States.CLOSED; } } + +interface EventListener { + void onChange(DataChangedEvent event); +} + +class DataChangedEvent { + + public Type getEventType() { + return eventType; + } + + public String getKey() { + return key; + } + + public String getValue() { + return value; + } + + public DataChangedEvent(Type eventType, String key, String value) { + this.eventType = eventType; + this.key = key; + this.value = value; + } + + private final Type eventType; + + private final String key; + + private final String value; + + /** + * Data changed event type. + */ + public enum Type { + + UPDATED, DELETED, IGNORED + } +} \ No newline at end of file From 656ba2814022a1382b7c6197b66c07bea169d350 Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 23 Apr 2018 18:58:33 +0800 Subject: [PATCH 019/351] wait test --- src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 66817ac0c1322..01817b09c4411 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -32,7 +32,7 @@ public void deleteNamespace() throws KeeperException, InterruptedException { } public byte[] getData(final String key) throws KeeperException, InterruptedException { - return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), watcher, null); + return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), false, null); } public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { From 5e487712b80051a6058a445ce1f63eb35f4be8a1 Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 23 Apr 2018 19:00:59 +0800 Subject: [PATCH 020/351] wait test --- .../java/com/saaavsaaa/client/zookeeper/UsualClient.java | 6 +++--- .../java/com/saaavsaaa/client/test/UsualClientTest.java | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 01817b09c4411..0f022836c7ae5 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -149,14 +149,14 @@ public void deleteAllChild(final String key) throws KeeperException, Interrupted } private Watcher watcher; - public void watch(final String key, final Listener eventListener){ + public Watcher watch(final String key, final Listener eventListener){ watcher = new Watcher() { - @Override public void process(WatchedEvent event) { - + eventListener.process(event); } }; + return watcher; } public void close() throws InterruptedException { diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java index d2d2eab84eb2f..95e5a800bf302 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -173,7 +173,7 @@ public void delAllChildren() throws KeeperException, InterruptedException { } @Test - public void watch() { + public void watch() throws KeeperException, InterruptedException { String key = ""; EventListener eventListener = new EventListener() { @@ -209,7 +209,8 @@ private DataChangedEvent.Type getEventType(final WatchedEvent event) { } } }; - client.watch(key, listener); + Watcher watcher = client.watch(key, listener); + client.getZooKeeper().exists("a", watcher); } @Test From 1d3ba887a558347ed7516f8267fea001fc32f4ea Mon Sep 17 00:00:00 2001 From: WangKai Date: Mon, 23 Apr 2018 19:20:35 +0800 Subject: [PATCH 021/351] create MySql execute backend handler --- .../mysql/MySQLExecuteBackendHandler.java | 249 ++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLExecuteBackendHandler.java diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLExecuteBackendHandler.java new file mode 100644 index 0000000000000..50b4c61f074ee --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLExecuteBackendHandler.java @@ -0,0 +1,249 @@ +/* + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingjdbc.proxy.backend.mysql; + +import io.shardingjdbc.core.constant.DatabaseType; +import io.shardingjdbc.core.constant.SQLType; +import io.shardingjdbc.core.merger.MergeEngineFactory; +import io.shardingjdbc.core.merger.MergedResult; +import io.shardingjdbc.core.merger.QueryResult; +import io.shardingjdbc.core.parsing.parser.sql.SQLStatement; +import io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement; +import io.shardingjdbc.core.routing.SQLExecutionUnit; +import io.shardingjdbc.core.routing.SQLRouteResult; +import io.shardingjdbc.core.routing.StatementRoutingEngine; +import io.shardingjdbc.proxy.backend.common.BackendHandler; +import io.shardingjdbc.proxy.config.ShardingRuleRegistry; +import io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket; +import io.shardingjdbc.proxy.transport.mysql.constant.ColumnType; +import io.shardingjdbc.proxy.transport.mysql.constant.StatusFlag; +import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; +import io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet; +import io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.FieldCountPacket; +import io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket; +import io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket; +import io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket; +import io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +/** + * SQL execute backend handler with mysql protocol. + * + * @author wangkai + */ +public final class MySQLExecuteBackendHandler implements BackendHandler { + + private final String sql; + + private final StatementRoutingEngine routingEngine; + + public MySQLExecuteBackendHandler(final String sql, final DatabaseType databaseType, final boolean showSQL) { + this.sql = sql; + routingEngine = new StatementRoutingEngine(ShardingRuleRegistry.getInstance().getShardingRule(), databaseType, showSQL); + } + + @Override + public CommandResponsePackets execute() { + SQLRouteResult routeResult = routingEngine.route(sql); + if (routeResult.getExecutionUnits().isEmpty()) { + return new CommandResponsePackets(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); + } + List result = new LinkedList<>(); + for (SQLExecutionUnit each : routeResult.getExecutionUnits()) { + // TODO multiple threads + result.add(execute(routeResult.getSqlStatement(), each)); + } + return merge(routeResult.getSqlStatement(), result); + } + + private CommandResponsePackets execute(final SQLStatement sqlStatement, final SQLExecutionUnit sqlExecutionUnit) { + switch (sqlStatement.getType()) { + case DQL: + return executeQuery(ShardingRuleRegistry.getInstance().getDataSourceMap().get(sqlExecutionUnit.getDataSource()), sqlExecutionUnit.getSql()); + case DML: + case DDL: + return executeUpdate(ShardingRuleRegistry.getInstance().getDataSourceMap().get(sqlExecutionUnit.getDataSource()), sqlExecutionUnit.getSql(), sqlStatement); + default: + return executeCommon(ShardingRuleRegistry.getInstance().getDataSourceMap().get(sqlExecutionUnit.getDataSource()), sqlExecutionUnit.getSql()); + } + } + + private CommandResponsePackets executeQuery(final DataSource dataSource, final String sql) { + try ( + Connection connection = dataSource.getConnection(); + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery(sql)) { + return getDatabaseProtocolPackets(resultSet); + } catch (final SQLException ex) { + return new CommandResponsePackets(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage())); + } + } + + private CommandResponsePackets executeCommon(final DataSource dataSource, final String sql) { + try ( + Connection connection = dataSource.getConnection(); + Statement statement = connection.createStatement()) { + boolean hasResultSet = statement.execute(sql); + if (hasResultSet) { + return getDatabaseProtocolPackets(statement.getResultSet()); + } else { + return new CommandResponsePackets(new OKPacket(1, statement.getUpdateCount(), 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); + } + } catch (final SQLException ex) { + return new CommandResponsePackets(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage())); + } + } + + private CommandResponsePackets getDatabaseProtocolPackets(final ResultSet resultSet) throws SQLException { + CommandResponsePackets result = new CommandResponsePackets(); + int currentSequenceId = 0; + ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); + int columnCount = resultSetMetaData.getColumnCount(); + if (0 == columnCount) { + result.addPacket(new OKPacket(++currentSequenceId, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); + return result; + } + result.addPacket(new FieldCountPacket(++currentSequenceId, columnCount)); + for (int i = 1; i <= columnCount; i++) { + result.addPacket(new ColumnDefinition41Packet(++currentSequenceId, resultSetMetaData.getSchemaName(i), resultSetMetaData.getTableName(i), + resultSetMetaData.getTableName(i), resultSetMetaData.getColumnLabel(i), resultSetMetaData.getColumnName(i), + resultSetMetaData.getColumnDisplaySize(i), ColumnType.valueOfJDBCType(resultSetMetaData.getColumnType(i)), 0)); + } + result.addPacket(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue())); + while (resultSet.next()) { + List data = new ArrayList<>(columnCount); + for (int i = 1; i <= columnCount; i++) { + data.add(resultSet.getObject(i)); + } + result.addPacket(new TextResultSetRowPacket(++currentSequenceId, data)); + } + result.addPacket(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue())); + return result; + } + + private CommandResponsePackets executeUpdate(final DataSource dataSource, final String sql, final SQLStatement sqlStatement) { + try ( + Connection connection = dataSource.getConnection(); + Statement statement = connection.createStatement()) { + int affectedRows; + long lastInsertId = 0; + if (sqlStatement instanceof InsertStatement) { + affectedRows = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS); + lastInsertId = getGeneratedKey(statement); + } else { + affectedRows = statement.executeUpdate(sql); + } + return new CommandResponsePackets(new OKPacket(1, affectedRows, lastInsertId, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); + } catch (final SQLException ex) { + return new CommandResponsePackets(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage())); + } + } + + private long getGeneratedKey(final Statement statement) throws SQLException { + long result = 0; + ResultSet resultSet = statement.getGeneratedKeys(); + if (resultSet.next()) { + result = resultSet.getLong(1); + } + return result; + } + + private CommandResponsePackets merge(final SQLStatement sqlStatement, final List packets) { + if (1 == packets.size()) { + return packets.iterator().next(); + } + CommandResponsePackets headPackets = new CommandResponsePackets(); + for (CommandResponsePackets each : packets) { + headPackets.addPacket(each.getHeadPacket()); + } + for (DatabaseProtocolPacket each : headPackets.getDatabaseProtocolPackets()) { + if (each instanceof ErrPacket) { + return new CommandResponsePackets(each); + } + } + if (SQLType.DML == sqlStatement.getType()) { + return mergeDML(headPackets); + } + if (SQLType.DQL == sqlStatement.getType() || SQLType.DAL == sqlStatement.getType()) { + return mergeDQLorDAL(sqlStatement, packets); + } + return packets.get(0); + } + + private CommandResponsePackets mergeDML(final CommandResponsePackets firstPackets) { + int affectedRows = 0; + for (DatabaseProtocolPacket each : firstPackets.getDatabaseProtocolPackets()) { + if (each instanceof OKPacket) { + OKPacket okPacket = (OKPacket) each; + affectedRows += okPacket.getAffectedRows(); + } + } + return new CommandResponsePackets(new OKPacket(1, affectedRows, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); + } + + private CommandResponsePackets mergeDQLorDAL(final SQLStatement sqlStatement, final List packets) { + List queryResults = new ArrayList<>(packets.size()); + for (CommandResponsePackets each : packets) { + // TODO replace to a common PacketQueryResult + queryResults.add(new MySQLPacketQueryResult(each)); + } + MergedResult mergedResult; + try { + mergedResult = MergeEngineFactory.newInstance(ShardingRuleRegistry.getInstance().getShardingRule(), queryResults, sqlStatement).merge(); + } catch (final SQLException ex) { + return new CommandResponsePackets(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage())); + } + return buildPackets(packets, mergedResult); + } + + private CommandResponsePackets buildPackets(final List packets, final MergedResult mergedResult) { + CommandResponsePackets result = new CommandResponsePackets(); + Iterator databaseProtocolPacketsSampling = packets.iterator().next().getDatabaseProtocolPackets().iterator(); + FieldCountPacket fieldCountPacketSampling = (FieldCountPacket) databaseProtocolPacketsSampling.next(); + result.addPacket(fieldCountPacketSampling); + int columnCount = fieldCountPacketSampling.getColumnCount(); + for (int i = 0; i < columnCount; i++) { + result.addPacket(databaseProtocolPacketsSampling.next()); + } + result.addPacket(databaseProtocolPacketsSampling.next()); + int currentSequenceId = result.size(); + try { + while (mergedResult.next()) { + List data = new ArrayList<>(columnCount); + for (int i = 1; i <= columnCount; i++) { + data.add(mergedResult.getValue(i, Object.class)); + } + result.addPacket(new TextResultSetRowPacket(++currentSequenceId, data)); + } + } catch (final SQLException ex) { + return new CommandResponsePackets(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage())); + } + result.addPacket(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue())); + return result; + } +} From 5ead408211a315664c147376cef6815bb8d5fcd8 Mon Sep 17 00:00:00 2001 From: WangKai Date: Mon, 23 Apr 2018 22:39:33 +0800 Subject: [PATCH 022/351] create netty client --- .../proxy/backend/ShardingProxyClient.java | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java new file mode 100644 index 0000000000000..08a51763422f1 --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java @@ -0,0 +1,109 @@ +/* + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingjdbc.proxy.backend; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.epoll.EpollChannelOption; +import io.netty.channel.epoll.EpollEventLoopGroup; +import io.netty.channel.epoll.EpollServerSocketChannel; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.logging.LogLevel; +import io.netty.handler.logging.LoggingHandler; +import io.shardingjdbc.proxy.frontend.netty.ServerHandlerInitializer; + +/** + * Sharding-Proxy Client. + * + * @author wangkai + */ +public final class ShardingProxyClient { + + private static final int WORKER_MAX_THREADS = Runtime.getRuntime().availableProcessors() * 2; + + private EventLoopGroup bossGroup; + + private EventLoopGroup workerGroup; + + private EventLoopGroup userGroup; + + /** + * Start Sharding-Proxy. + * + * @param port port + * @throws InterruptedException interrupted exception + */ + public void start(final int port) throws InterruptedException { + try { + ServerBootstrap bootstrap = new ServerBootstrap(); + bossGroup = createEventLoopGroup(); + if (bossGroup instanceof EpollEventLoopGroup) { + groupsEpoll(bootstrap); + } else { + groupsNio(bootstrap); + } + ChannelFuture future = bootstrap.bind(port).sync(); + future.channel().closeFuture().sync(); + } finally { + workerGroup.shutdownGracefully(); + bossGroup.shutdownGracefully(); + userGroup.shutdownGracefully(); + } + } + + private EventLoopGroup createEventLoopGroup() { + try { + return new EpollEventLoopGroup(1); + } catch (final UnsatisfiedLinkError ex) { + return new NioEventLoopGroup(1); + } + } + + private void groupsEpoll(final ServerBootstrap bootstrap) { + workerGroup = new EpollEventLoopGroup(WORKER_MAX_THREADS); + userGroup = new EpollEventLoopGroup(WORKER_MAX_THREADS); + bootstrap.group(bossGroup, workerGroup) + .channel(EpollServerSocketChannel.class) + .option(EpollChannelOption.TCP_CORK, true) + .option(EpollChannelOption.SO_KEEPALIVE, true) + .option(EpollChannelOption.SO_BACKLOG, 128) + .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) + .childOption(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) + .handler(new LoggingHandler(LogLevel.INFO)) + .childHandler(new ServerHandlerInitializer(userGroup)); + } + + private void groupsNio(final ServerBootstrap bootstrap) { + workerGroup = new NioEventLoopGroup(WORKER_MAX_THREADS); + userGroup = new NioEventLoopGroup(WORKER_MAX_THREADS); + bootstrap.group(bossGroup, workerGroup) + .channel(NioServerSocketChannel.class) + .option(ChannelOption.SO_KEEPALIVE, true) + .option(ChannelOption.TCP_NODELAY, true) + .option(ChannelOption.SO_BACKLOG, 128) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100) + .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) + .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) + .handler(new LoggingHandler(LogLevel.INFO)) + .childHandler(new ServerHandlerInitializer(userGroup)); + } +} From a2e34c7750dbba6ab49db14396a3244075613bcb Mon Sep 17 00:00:00 2001 From: saaav Date: Tue, 24 Apr 2018 12:18:13 +0800 Subject: [PATCH 023/351] wait test --- .../saaavsaaa/client/untils/StringUtil.java | 3 ++ .../client/zookeeper/BaseClient.java | 1 - .../client/zookeeper/UsualClient.java | 22 ++++++++--- .../client/test/UsualClientTest.java | 39 +++++++++++++++---- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/untils/StringUtil.java b/src/main/java/com/saaavsaaa/client/untils/StringUtil.java index 1f8bf1b8abce0..758e17f0e947e 100644 --- a/src/main/java/com/saaavsaaa/client/untils/StringUtil.java +++ b/src/main/java/com/saaavsaaa/client/untils/StringUtil.java @@ -1,9 +1,12 @@ package com.saaavsaaa.client.untils; +import java.nio.charset.Charset; + /** * Created by aaa on 18-4-18. */ public class StringUtil { + public static final Charset UTF_8 = Charset.forName("UTF-8"); public static boolean isNullOrWhite(String string) { return string == null || string.trim().length() == 0; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java index 7511080914cc6..b17e9bfdf9119 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java @@ -18,7 +18,6 @@ public abstract class BaseClient { private static final CountDownLatch CONNECTED = new CountDownLatch(1); public static final int VERSION = -1; public static final byte[] NOTHING_DATA = new byte[0]; - public static final Charset UTF_8 = Charset.forName("UTF-8"); private final String servers; private final int sessionTimeOut; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 0f022836c7ae5..9c1ed47907c2d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -5,9 +5,9 @@ import com.saaavsaaa.client.untils.Listener; import com.saaavsaaa.client.untils.PathUtil; +import com.saaavsaaa.client.untils.StringUtil; import org.apache.zookeeper.*; -import java.util.EventListener; import java.util.List; import java.util.Stack; @@ -30,6 +30,10 @@ public void createNamespace() throws KeeperException, InterruptedException { public void deleteNamespace() throws KeeperException, InterruptedException { zooKeeper.delete(rootNode, VERSION); } + + public String getDataString(final String key) throws KeeperException, InterruptedException { + return new String(getData(key)); + } public byte[] getData(final String key) throws KeeperException, InterruptedException { return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), false, null); @@ -40,7 +44,12 @@ public void getData(final String key, final AsyncCallback.DataCallback callback, } public boolean checkExists(final String key) throws KeeperException, InterruptedException { - return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), false); +// return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), false); + return checkExists(key, null); + } + + public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { + return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), watcher); } public List getChildren(final String key) throws KeeperException, InterruptedException { @@ -48,7 +57,7 @@ public List getChildren(final String key) throws KeeperException, Interr } public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - zooKeeper.create(PathUtil.getRealPath(rootNode, key), value.getBytes(UTF_8), authorities, createMode); + zooKeeper.create(PathUtil.getRealPath(rootNode, key), value.getBytes(StringUtil.UTF_8), authorities, createMode); } /* @@ -70,7 +79,7 @@ public void createAllNeedPath(final String key, final String value, final Create } System.out.println("not exist:" + nodes.get(i)); if (i == nodes.size() - 1){ - createInTransaction(nodes.get(i), value.getBytes(UTF_8), createMode, transaction); + createInTransaction(nodes.get(i), value.getBytes(StringUtil.UTF_8), createMode, transaction); } else { createInTransaction(nodes.get(i), NOTHING_DATA, createMode, transaction); } @@ -85,12 +94,12 @@ private Transaction createInTransaction(final String key, byte[] data, final Cre } public void update(final String key, final String value) throws KeeperException, InterruptedException { - zooKeeper.setData(PathUtil.getRealPath(rootNode, key), value.getBytes(UTF_8), VERSION); + zooKeeper.setData(PathUtil.getRealPath(rootNode, key), value.getBytes(StringUtil.UTF_8), VERSION); } public void updateInTransaction(final String key, final String value) throws KeeperException, InterruptedException { String realPath = PathUtil.getRealPath(rootNode, key); - zooKeeper.transaction().check(realPath, VERSION).setData(realPath, value.getBytes(UTF_8), VERSION).commit(); + zooKeeper.transaction().check(realPath, VERSION).setData(realPath, value.getBytes(StringUtil.UTF_8), VERSION).commit(); } public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { @@ -118,6 +127,7 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr // contrast cache if (checkExists(node)){ transaction.delete(node, VERSION); + System.out.println("delete : " + node); } } transaction.commit(); diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java index 95e5a800bf302..2000350841d23 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -2,6 +2,7 @@ import com.saaavsaaa.client.untils.Listener; import com.saaavsaaa.client.untils.PathUtil; +import com.saaavsaaa.client.untils.StringUtil; import com.saaavsaaa.client.zookeeper.BaseClient; import com.saaavsaaa.client.zookeeper.ClientFactory; import com.saaavsaaa.client.zookeeper.UsualClient; @@ -12,6 +13,7 @@ import org.junit.Test; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -174,8 +176,12 @@ public void delAllChildren() throws KeeperException, InterruptedException { @Test public void watch() throws KeeperException, InterruptedException { - String key = ""; - + List expected = new ArrayList<>(); + expected.add("update_/test/a_value"); + expected.add("update_/test/a_value1"); + expected.add("update_/test/a_value2"); + expected.add("delete_/test/a_value2"); + List actual = new ArrayList<>(); EventListener eventListener = new EventListener() { @Override public void onChange(DataChangedEvent event) { @@ -193,24 +199,41 @@ public void process(WatchedEvent event) { } catch (InterruptedException e) { e.printStackTrace(); } - eventListener.onChange(new DataChangedEvent(getEventType(event), event.getPath(), - null == data ? null : new String(data, BaseClient.UTF_8))); + String result = null == data ? null : new String(data, StringUtil.UTF_8); + eventListener.onChange(new DataChangedEvent(getEventType(event, result), event.getPath(), result)); } - private DataChangedEvent.Type getEventType(final WatchedEvent event) { + private DataChangedEvent.Type getEventType(final WatchedEvent event, final String result) { switch (event.getType()) { case NodeDataChanged: - case NodeChildrenChanged: + case NodeChildrenChanged: { + actual.add(new StringBuilder().append("update_").append(event.getPath()).append("_").append(result).toString()); return DataChangedEvent.Type.UPDATED; - case NodeDeleted: + } + case NodeDeleted: { + actual.add(new StringBuilder().append("delete_").append(event.getPath()).append("_").append(result).toString()); return DataChangedEvent.Type.DELETED; + } default: + actual.add(new StringBuilder().append("ignore_").append(event.getPath()).append("_").append(result).toString()); return DataChangedEvent.Type.IGNORED; } } }; + String key = "a"; Watcher watcher = client.watch(key, listener); - client.getZooKeeper().exists("a", watcher); + client.createNamespace(); + client.createCurrentOnly(key, "aaa", CreateMode.EPHEMERAL); + client.checkExists(key, watcher); + client.updateInTransaction(key, "value"); + System.out.println(new String(client.getData(key))); + assert client.getDataString(key).equals("value"); + client.updateInTransaction(key, "value1"); + assert client.getDataString(key).equals("value1"); + client.updateInTransaction(key, "value2"); + assert client.getDataString(key).equals("value2"); + client.deleteCurrentBranch(key); + assert expected.size() == actual.size(); } @Test From 7da9713ee70aa20a9ba9af25b5900b4ffb35e7f1 Mon Sep 17 00:00:00 2001 From: saaav Date: Tue, 24 Apr 2018 15:27:05 +0800 Subject: [PATCH 024/351] start watcher --- .../client/zookeeper/BaseClient.java | 4 ++ .../client/zookeeper/ClientFactory.java | 8 +++ .../client/zookeeper/UsualClient.java | 42 ++++++++++--- .../client/test/UsualClientTest.java | 61 ++++++++++++++----- 4 files changed, 91 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java index b17e9bfdf9119..eddcf6e61c033 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java @@ -25,6 +25,7 @@ public abstract class BaseClient { protected String rootNode = "/InitValue"; protected List authorities; + protected Watcher watcher; protected BaseClient(String servers, int sessionTimeoutMilliseconds) { this.servers = servers; @@ -48,6 +49,9 @@ public void process(WatchedEvent event) { CONNECTED.countDown(); } } + if (watcher != null){ + watcher.process(event); + } } }; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 66bbef5695770..7de011191ebd9 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -1,5 +1,8 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.untils.Listener; +import org.apache.zookeeper.Watcher; + import java.io.IOException; /** @@ -25,6 +28,11 @@ public ClientFactory newClient(final String servers, final int sessionTimeoutMil return this; } + public ClientFactory watch(final Listener listener){ + client.watch(listener); + return this; + } + public synchronized UsualClient start() throws IOException, InterruptedException { client.start(); client.setRootNode(namespace); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 9c1ed47907c2d..119150dd16733 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -16,6 +16,8 @@ * todo Sequential */ public class UsualClient extends BaseClient { + private final boolean watched = true; //false + private boolean watchRegistered = false; UsualClient(String servers, int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); } @@ -36,16 +38,15 @@ public String getDataString(final String key) throws KeeperException, Interrupte } public byte[] getData(final String key) throws KeeperException, InterruptedException { - return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), false, null); + return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), watched, null); } public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { - zooKeeper.getData(PathUtil.getRealPath(rootNode, key), false, callback, ctx); + zooKeeper.getData(PathUtil.getRealPath(rootNode, key), watched, callback, ctx); } public boolean checkExists(final String key) throws KeeperException, InterruptedException { -// return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), false); - return checkExists(key, null); + return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), watched); } public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { @@ -53,7 +54,7 @@ public boolean checkExists(final String key, final Watcher watcher) throws Keepe } public List getChildren(final String key) throws KeeperException, InterruptedException { - return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); + return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), watched); } public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { @@ -158,12 +159,37 @@ public void deleteAllChild(final String key) throws KeeperException, Interrupted } } - private Watcher watcher; - public Watcher watch(final String key, final Listener eventListener){ + void watch(final Listener listener){ + if (watchRegistered){ + return; + } + watchRegistered = true; + watcher = new Watcher() { + @Override + public void process(WatchedEvent event) { + listener.process(event); + } + }; + } + + public Watcher watch(final String key, final Listener listener){ watcher = new Watcher() { @Override public void process(WatchedEvent event) { - eventListener.process(event); + /*new Thread(new Runnable() { + @Override + public void run() { + zooKeeper.register(watcher); + } + }).start();*/ + listener.process(event); + /*try { + zooKeeper.exists(PathUtil.getRealPath(rootNode, key), watcher); + } catch (KeeperException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + }*/ } }; return watcher; diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java index 2000350841d23..55498decc2bdb 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -10,6 +10,7 @@ import org.apache.zookeeper.data.Stat; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import java.io.IOException; @@ -30,10 +31,31 @@ public class UsualClientTest { private UsualClient client = null; - @Before + /*@Before public void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); client = creator.setNamespace(ROOT).authorization(AUTH, AUTH.getBytes()).newClient(SERVERS, SESSION_TIMEOUT).start(); + }*/ + + @Before + public void startWithWatch() throws IOException, InterruptedException { + ClientFactory creator = new ClientFactory(); + Listener listener = buildListener(); + client = creator.setNamespace(ROOT).authorization(AUTH, AUTH.getBytes()).newClient(SERVERS, SESSION_TIMEOUT).watch(listener).start(); + } + + private Listener buildListener(){ + Listener listener = new Listener() { + @Override + public void process(WatchedEvent event) { + System.out.println("=========================================================="); + System.out.println(event.getPath()); + System.out.println(event.getState()); + System.out.println(event.getType()); + System.out.println("=========================================================="); + } + }; + return listener; } @After @@ -177,11 +199,31 @@ public void delAllChildren() throws KeeperException, InterruptedException { @Test public void watch() throws KeeperException, InterruptedException { List expected = new ArrayList<>(); + expected.add("ignore_/test_"); expected.add("update_/test/a_value"); expected.add("update_/test/a_value1"); expected.add("update_/test/a_value2"); expected.add("delete_/test/a_value2"); List actual = new ArrayList<>(); + Listener listener = buildListener(actual); + String key = "a"; + Watcher watcher = client.watch(key, listener); + client.createNamespace(); + client.createCurrentOnly(key, "aaa", CreateMode.EPHEMERAL); + client.checkExists(key, watcher); + client.updateInTransaction(key, "value"); + System.out.println(new String(client.getData(key))); + assert client.getDataString(key).equals("value"); + client.updateInTransaction(key, "value1"); + assert client.getDataString(key).equals("value1"); + client.updateInTransaction(key, "value2"); + assert client.getDataString(key).equals("value2"); + client.deleteCurrentBranch(key); + assert expected.size() == actual.size(); + assert expected.containsAll(actual); + } + + private Listener buildListener(List actual){ EventListener eventListener = new EventListener() { @Override public void onChange(DataChangedEvent event) { @@ -202,7 +244,7 @@ public void process(WatchedEvent event) { String result = null == data ? null : new String(data, StringUtil.UTF_8); eventListener.onChange(new DataChangedEvent(getEventType(event, result), event.getPath(), result)); } - + private DataChangedEvent.Type getEventType(final WatchedEvent event, final String result) { switch (event.getType()) { case NodeDataChanged: @@ -220,20 +262,7 @@ private DataChangedEvent.Type getEventType(final WatchedEvent event, final Strin } } }; - String key = "a"; - Watcher watcher = client.watch(key, listener); - client.createNamespace(); - client.createCurrentOnly(key, "aaa", CreateMode.EPHEMERAL); - client.checkExists(key, watcher); - client.updateInTransaction(key, "value"); - System.out.println(new String(client.getData(key))); - assert client.getDataString(key).equals("value"); - client.updateInTransaction(key, "value1"); - assert client.getDataString(key).equals("value1"); - client.updateInTransaction(key, "value2"); - assert client.getDataString(key).equals("value2"); - client.deleteCurrentBranch(key); - assert expected.size() == actual.size(); + return listener; } @Test From 83f19cf754e8d45c59912746890e74d2715077ba Mon Sep 17 00:00:00 2001 From: saaav Date: Tue, 24 Apr 2018 17:42:57 +0800 Subject: [PATCH 025/351] watcher --- .../com/saaavsaaa/client/untils/Listener.java | 13 +++- .../client/zookeeper/BaseClient.java | 16 ++++- .../client/zookeeper/ClientFactory.java | 6 +- .../client/zookeeper/UsualClient.java | 22 ++---- .../client/test/UsualClientTest.java | 68 ++++++++++++++++--- 5 files changed, 93 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/untils/Listener.java b/src/main/java/com/saaavsaaa/client/untils/Listener.java index 0e792e1fb0f1e..1a9ff8efff872 100644 --- a/src/main/java/com/saaavsaaa/client/untils/Listener.java +++ b/src/main/java/com/saaavsaaa/client/untils/Listener.java @@ -5,6 +5,15 @@ /** * Created by aaa on 18-4-23. */ -public interface Listener { - void process(WatchedEvent event); +public abstract class Listener { + private String key = null; + public abstract void process(WatchedEvent event); + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java index eddcf6e61c033..557d81036f334 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java @@ -8,7 +8,11 @@ import java.io.IOException; import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; /** @@ -16,8 +20,10 @@ */ public abstract class BaseClient { private static final CountDownLatch CONNECTED = new CountDownLatch(1); + private static final boolean WATCH_ON = true; public static final int VERSION = -1; public static final byte[] NOTHING_DATA = new byte[0]; + protected static final Map watchers = new ConcurrentHashMap<>(); private final String servers; private final int sessionTimeOut; @@ -25,7 +31,6 @@ public abstract class BaseClient { protected String rootNode = "/InitValue"; protected List authorities; - protected Watcher watcher; protected BaseClient(String servers, int sessionTimeoutMilliseconds) { this.servers = servers; @@ -49,8 +54,13 @@ public void process(WatchedEvent event) { CONNECTED.countDown(); } } - if (watcher != null){ - watcher.process(event); + // key == rootNode signify that register a watcher without appoint path when client init + // or want to watch the whole namespace + if (watchers.containsKey(rootNode)){ + watchers.get(rootNode).process(event); + } + if (WATCH_ON && watchers.containsKey(event.getPath())){ + watchers.get(event.getPath()).process(event); } } }; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 7de011191ebd9..6a3a73a48359a 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -12,6 +12,7 @@ public class ClientFactory { // private static final String CLIENT_EXCLUSIVE_NODE = "ZKC"; private UsualClient client; + private Listener globalListener; private String namespace; private String scheme; private byte[] auth; @@ -29,7 +30,7 @@ public ClientFactory newClient(final String servers, final int sessionTimeoutMil } public ClientFactory watch(final Listener listener){ - client.watch(listener); + globalListener = listener; return this; } @@ -37,6 +38,9 @@ public synchronized UsualClient start() throws IOException, InterruptedException client.start(); client.setRootNode(namespace); client.setAuthorities(scheme , auth); + if (globalListener != null) { + client.watch(globalListener); + } return client; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 119150dd16733..7498c996d44be 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -164,34 +164,24 @@ void watch(final Listener listener){ return; } watchRegistered = true; - watcher = new Watcher() { + watchers.put(rootNode, new Watcher() { @Override public void process(WatchedEvent event) { listener.process(event); } - }; + }); } public Watcher watch(final String key, final Listener listener){ - watcher = new Watcher() { + String path = PathUtil.getRealPath(rootNode, key); + listener.setKey(path); + Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { - /*new Thread(new Runnable() { - @Override - public void run() { - zooKeeper.register(watcher); - } - }).start();*/ listener.process(event); - /*try { - zooKeeper.exists(PathUtil.getRealPath(rootNode, key), watcher); - } catch (KeeperException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); - }*/ } }; + watchers.put(path, watcher); return watcher; } diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java index 55498decc2bdb..2b14662543540 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -3,14 +3,12 @@ import com.saaavsaaa.client.untils.Listener; import com.saaavsaaa.client.untils.PathUtil; import com.saaavsaaa.client.untils.StringUtil; -import com.saaavsaaa.client.zookeeper.BaseClient; import com.saaavsaaa.client.zookeeper.ClientFactory; import com.saaavsaaa.client.zookeeper.UsualClient; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import java.io.IOException; @@ -31,13 +29,13 @@ public class UsualClientTest { private UsualClient client = null; - /*@Before + @Before public void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); client = creator.setNamespace(ROOT).authorization(AUTH, AUTH.getBytes()).newClient(SERVERS, SESSION_TIMEOUT).start(); - }*/ + } - @Before +// @Before public void startWithWatch() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); Listener listener = buildListener(); @@ -196,16 +194,18 @@ public void delAllChildren() throws KeeperException, InterruptedException { client.deleteNamespace(); } +// @Ignore @Test public void watch() throws KeeperException, InterruptedException { List expected = new ArrayList<>(); - expected.add("ignore_/test_"); expected.add("update_/test/a_value"); expected.add("update_/test/a_value1"); expected.add("update_/test/a_value2"); - expected.add("delete_/test/a_value2"); + expected.add("delete_/test/a_"); List actual = new ArrayList<>(); - Listener listener = buildListener(actual); + + Listener listener = buildEventListener(actual); //buildListener(actual); + String key = "a"; Watcher watcher = client.watch(key, listener); client.createNamespace(); @@ -219,15 +219,59 @@ public void watch() throws KeeperException, InterruptedException { client.updateInTransaction(key, "value2"); assert client.getDataString(key).equals("value2"); client.deleteCurrentBranch(key); + Thread.sleep(100); assert expected.size() == actual.size(); assert expected.containsAll(actual); } private Listener buildListener(List actual){ + Listener listener = new Listener() { + @Override + public void process(WatchedEvent event) { + System.out.println("=========================================================="); + System.out.println(event.getPath()); + System.out.println(event.getType()); + + switch (event.getType()) { + case NodeDataChanged: + case NodeChildrenChanged: { + String result; + try { + result = new String(client.getZooKeeper().getData(event.getPath(),false, null)); + System.out.println(); + } catch (KeeperException e) { + result = e.getMessage(); + e.printStackTrace(); + } catch (InterruptedException e) { + result = e.getMessage(); + e.printStackTrace(); + } + actual.add(new StringBuilder().append("update_").append(event.getPath()).append("_").append(result).toString()); + break; + } + case NodeDeleted: { + actual.add(new StringBuilder().append("delete_").append(event.getPath()).append("_").toString()); + break; + } + default: + actual.add(new StringBuilder().append("ignore_").append(event.getPath()).append("_").append(event.getType()).toString()); + break; + } + System.out.println("=========================================================="); + } + }; + return listener; + } + + private Listener buildEventListener(List actual){ EventListener eventListener = new EventListener() { @Override public void onChange(DataChangedEvent event) { - System.out.println(event.getKey() + " : " + event.getValue()); + System.out.println("=========================================================="); + System.out.println(event.getKey()); + System.out.println(event.getValue()); + System.out.println(event.getEventType()); + System.out.println("=========================================================="); } }; Listener listener = new Listener() { @@ -237,7 +281,11 @@ public void process(WatchedEvent event) { try { data = client.getZooKeeper().getData(event.getPath(),false, null); } catch (KeeperException e) { - e.printStackTrace(); + if (e instanceof KeeperException.NoNodeException){ + System.out.println(event.getType() +" : "+ e.getMessage()); + } else { + e.printStackTrace(); + } } catch (InterruptedException e) { e.printStackTrace(); } From 5d148311b2bcd3509bb9fb4c7e0691e00ee61420 Mon Sep 17 00:00:00 2001 From: saaav Date: Tue, 24 Apr 2018 17:57:52 +0800 Subject: [PATCH 026/351] listener and test --- .../java/com/saaavsaaa/client/untils/Listener.java | 13 ++----------- .../com/saaavsaaa/client/zookeeper/UsualClient.java | 2 +- .../com/saaavsaaa/client/test/UsualClientTest.java | 6 ++++-- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/untils/Listener.java b/src/main/java/com/saaavsaaa/client/untils/Listener.java index 1a9ff8efff872..0e792e1fb0f1e 100644 --- a/src/main/java/com/saaavsaaa/client/untils/Listener.java +++ b/src/main/java/com/saaavsaaa/client/untils/Listener.java @@ -5,15 +5,6 @@ /** * Created by aaa on 18-4-23. */ -public abstract class Listener { - private String key = null; - public abstract void process(WatchedEvent event); - - public String getKey() { - return key; - } - - public void setKey(String key) { - this.key = key; - } +public interface Listener { + void process(WatchedEvent event); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 7498c996d44be..836379cf4ec4e 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -174,7 +174,7 @@ public void process(WatchedEvent event) { public Watcher watch(final String key, final Listener listener){ String path = PathUtil.getRealPath(rootNode, key); - listener.setKey(path); +// listener.setKey(path); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java index 2b14662543540..999c559f44c5d 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -9,6 +9,7 @@ import org.apache.zookeeper.data.Stat; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import java.io.IOException; @@ -29,13 +30,13 @@ public class UsualClientTest { private UsualClient client = null; - @Before +// @Before public void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); client = creator.setNamespace(ROOT).authorization(AUTH, AUTH.getBytes()).newClient(SERVERS, SESSION_TIMEOUT).start(); } -// @Before + @Before public void startWithWatch() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); Listener listener = buildListener(); @@ -59,6 +60,7 @@ public void process(WatchedEvent event) { @After public void stop() throws InterruptedException { client.close(); + client = null; } @Test From 1b4c417292466d60352386f1e3e9f970d96dfba7 Mon Sep 17 00:00:00 2001 From: saaav Date: Tue, 24 Apr 2018 18:23:34 +0800 Subject: [PATCH 027/351] unregister watcher --- .../java/com/saaavsaaa/client/untils/PathUtil.java | 2 +- .../com/saaavsaaa/client/untils/StringUtil.java | 2 +- .../saaavsaaa/client/zookeeper/ClientFactory.java | 2 +- .../saaavsaaa/client/zookeeper/UsualClient.java | 14 ++++++++++++-- .../com/saaavsaaa/client/test/UsualClientTest.java | 9 +++++---- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java index 4758133f1bbc7..bea32e376f808 100644 --- a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java +++ b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java @@ -14,7 +14,7 @@ public static String getRealPath(final String root, final String path){ } private static String adjustPath(String root, String path){ - if (StringUtil.isNullOrWhite(path)){ + if (StringUtil.isNullOrBlank(path)){ throw new IllegalArgumentException("path should have content!"); } if (!root.startsWith(PATH_SEPARATOR)){ diff --git a/src/main/java/com/saaavsaaa/client/untils/StringUtil.java b/src/main/java/com/saaavsaaa/client/untils/StringUtil.java index 758e17f0e947e..585ce22004c2e 100644 --- a/src/main/java/com/saaavsaaa/client/untils/StringUtil.java +++ b/src/main/java/com/saaavsaaa/client/untils/StringUtil.java @@ -7,7 +7,7 @@ */ public class StringUtil { public static final Charset UTF_8 = Charset.forName("UTF-8"); - public static boolean isNullOrWhite(String string) { + public static boolean isNullOrBlank(String string) { return string == null || string.trim().length() == 0; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 6a3a73a48359a..4285dbc88659d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -39,7 +39,7 @@ public synchronized UsualClient start() throws IOException, InterruptedException client.setRootNode(namespace); client.setAuthorities(scheme , auth); if (globalListener != null) { - client.watch(globalListener); + client.registerWatch(globalListener); } return client; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 836379cf4ec4e..e316c44cfe491 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -159,7 +159,7 @@ public void deleteAllChild(final String key) throws KeeperException, Interrupted } } - void watch(final Listener listener){ + void registerWatch(final Listener listener){ if (watchRegistered){ return; } @@ -172,7 +172,7 @@ public void process(WatchedEvent event) { }); } - public Watcher watch(final String key, final Listener listener){ + public Watcher registerWatch(final String key, final Listener listener){ String path = PathUtil.getRealPath(rootNode, key); // listener.setKey(path); Watcher watcher = new Watcher() { @@ -185,6 +185,16 @@ public void process(WatchedEvent event) { return watcher; } + public void unregisterWatch(final String key){ + if (StringUtil.isNullOrBlank(key)){ + throw new IllegalArgumentException("key should not be blank"); + } + String path = PathUtil.getRealPath(rootNode, key); + if (watchers.containsKey(path)){ + watchers.remove(path); + } + } + public void close() throws InterruptedException { zooKeeper.close(); } diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java index 999c559f44c5d..4349eabdb7b01 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java @@ -206,10 +206,10 @@ public void watch() throws KeeperException, InterruptedException { expected.add("delete_/test/a_"); List actual = new ArrayList<>(); - Listener listener = buildEventListener(actual); //buildListener(actual); + Listener listener = buildListener(actual); String key = "a"; - Watcher watcher = client.watch(key, listener); + Watcher watcher = client.registerWatch(key, listener); client.createNamespace(); client.createCurrentOnly(key, "aaa", CreateMode.EPHEMERAL); client.checkExists(key, watcher); @@ -224,13 +224,14 @@ public void watch() throws KeeperException, InterruptedException { Thread.sleep(100); assert expected.size() == actual.size(); assert expected.containsAll(actual); + client.unregisterWatch(key); } private Listener buildListener(List actual){ Listener listener = new Listener() { @Override public void process(WatchedEvent event) { - System.out.println("=========================================================="); + System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); System.out.println(event.getPath()); System.out.println(event.getType()); @@ -259,7 +260,7 @@ public void process(WatchedEvent event) { actual.add(new StringBuilder().append("ignore_").append(event.getPath()).append("_").append(event.getType()).toString()); break; } - System.out.println("=========================================================="); + System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); } }; return listener; From 45412618eddfd68810074dba2629714121aafd15 Mon Sep 17 00:00:00 2001 From: WangKai Date: Tue, 24 Apr 2018 19:45:27 +0800 Subject: [PATCH 028/351] temp submit --- sharding-proxy/pom.xml | 6 +- .../proxy/backend/ShardingProxyClient.java | 57 ++++++++----------- .../proxy/backend/common/BackendHandler.java | 31 +++++++++- .../backend/common/BackendHandlerFactory.java | 48 ++++++++++++++++ .../netty/ClientHandlerInitializer.java | 44 ++++++++++++++ .../frontend/common/FrontendHandler.java | 1 + 6 files changed, 150 insertions(+), 37 deletions(-) create mode 100644 sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java create mode 100644 sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java diff --git a/sharding-proxy/pom.xml b/sharding-proxy/pom.xml index 148051bd9a866..f683f4172220e 100644 --- a/sharding-proxy/pom.xml +++ b/sharding-proxy/pom.xml @@ -48,7 +48,6 @@ org.apache.commons commons-dbcp2 - runtime mysql @@ -69,6 +68,11 @@ org.hamcrest hamcrest-library + + org.apache.commons + commons-dbcp2 + compile + diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java index 08a51763422f1..373b19175b5a8 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java @@ -17,7 +17,7 @@ package io.shardingjdbc.proxy.backend; -import io.netty.bootstrap.ServerBootstrap; +import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelOption; @@ -29,7 +29,12 @@ import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; -import io.shardingjdbc.proxy.frontend.netty.ServerHandlerInitializer; +import io.shardingjdbc.proxy.backend.netty.ClientHandlerInitializer; +import io.shardingjdbc.proxy.config.ShardingRuleRegistry; +import org.apache.commons.dbcp2.BasicDataSource; + +import javax.sql.DataSource; +import java.util.Map; /** * Sharding-Proxy Client. @@ -38,72 +43,56 @@ */ public final class ShardingProxyClient { - private static final int WORKER_MAX_THREADS = Runtime.getRuntime().availableProcessors() * 2; - - private EventLoopGroup bossGroup; + private static final int WORKER_MAX_THREADS = Runtime.getRuntime().availableProcessors(); private EventLoopGroup workerGroup; - private EventLoopGroup userGroup; - /** * Start Sharding-Proxy. * - * @param port port * @throws InterruptedException interrupted exception */ - public void start(final int port) throws InterruptedException { + public void start() throws InterruptedException { try { - ServerBootstrap bootstrap = new ServerBootstrap(); - bossGroup = createEventLoopGroup(); - if (bossGroup instanceof EpollEventLoopGroup) { + Bootstrap bootstrap = new Bootstrap(); + if (workerGroup instanceof EpollEventLoopGroup) { groupsEpoll(bootstrap); } else { groupsNio(bootstrap); } - ChannelFuture future = bootstrap.bind(port).sync(); - future.channel().closeFuture().sync(); + Map dataSourceMap = ShardingRuleRegistry.getInstance().getDataSourceMap(); + for (DataSource each : dataSourceMap.values()) { + ((BasicDataSource)each).getUrl(). + ChannelFuture future = bootstrap.connect("localhost",3306).sync(); + future.channel().closeFuture().sync(); + } } finally { workerGroup.shutdownGracefully(); - bossGroup.shutdownGracefully(); - userGroup.shutdownGracefully(); - } - } - - private EventLoopGroup createEventLoopGroup() { - try { - return new EpollEventLoopGroup(1); - } catch (final UnsatisfiedLinkError ex) { - return new NioEventLoopGroup(1); } } - private void groupsEpoll(final ServerBootstrap bootstrap) { + private void groupsEpoll(final Bootstrap bootstrap) { workerGroup = new EpollEventLoopGroup(WORKER_MAX_THREADS); - userGroup = new EpollEventLoopGroup(WORKER_MAX_THREADS); - bootstrap.group(bossGroup, workerGroup) + bootstrap.group(workerGroup) .channel(EpollServerSocketChannel.class) .option(EpollChannelOption.TCP_CORK, true) .option(EpollChannelOption.SO_KEEPALIVE, true) .option(EpollChannelOption.SO_BACKLOG, 128) .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) - .childOption(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)) - .childHandler(new ServerHandlerInitializer(userGroup)); + .handler(new ClientHandlerInitializer()); } - private void groupsNio(final ServerBootstrap bootstrap) { + private void groupsNio(final Bootstrap bootstrap) { workerGroup = new NioEventLoopGroup(WORKER_MAX_THREADS); - userGroup = new NioEventLoopGroup(WORKER_MAX_THREADS); - bootstrap.group(bossGroup, workerGroup) + bootstrap.group(workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.SO_BACKLOG, 128) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) - .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)) - .childHandler(new ServerHandlerInitializer(userGroup)); + .handler(new ClientHandlerInitializer()); } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandler.java index 35fb4ad0f432f..37cde952cc452 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandler.java @@ -17,19 +17,46 @@ package io.shardingjdbc.proxy.backend.common; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; /** * Backend handler. * * @author zhangliang + * @author wangkai */ -public interface BackendHandler { +public abstract class BackendHandler extends ChannelInboundHandlerAdapter { + + private boolean handshaked; + + private boolean authorized; + + protected abstract void handshake(ChannelHandlerContext context, ByteBuf message); + + @Override + public void channelRead(final ChannelHandlerContext context, final Object message) { + if (!handshaked) { + handshake(context, (ByteBuf) message); + handshaked = true; + } else if(authorized) { + auth(context, (ByteBuf) message); + authorized = true; + } else { + executeCommand(context, (ByteBuf) message); + } + } + + protected abstract void auth(ChannelHandlerContext context, ByteBuf message); + + protected abstract void executeCommand(ChannelHandlerContext context, ByteBuf message); /** * Execute command. * * @return result packets to be sent */ - CommandResponsePackets execute(); + protected abstract CommandResponsePackets execute(); } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java new file mode 100644 index 0000000000000..ec9d14fc2da4a --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java @@ -0,0 +1,48 @@ +/* + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingjdbc.proxy.backend.common; + +import io.netty.channel.EventLoopGroup; +import io.shardingjdbc.core.constant.DatabaseType; +import io.shardingjdbc.proxy.frontend.common.FrontendHandler; +import io.shardingjdbc.proxy.frontend.mysql.MySQLFrontendHandler; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * Backend handler factory. + * + * @author wangkai + */ +public final class BackendHandlerFactory { + + /** + * Create frontend handler instance. + * + * @param databaseType database type + * @return backend handler instance + */ + public static BackendHandler createFrontendHandlerInstance(final DatabaseType databaseType) { + switch (databaseType) { + case MySQL: + return new MySQLFrontendHandler(); + default: + throw new UnsupportedOperationException(String.format("Cannot support database type '%s'", databaseType)); + } + } +} diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java new file mode 100644 index 0000000000000..1597f6441ceea --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java @@ -0,0 +1,44 @@ +/* + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingjdbc.proxy.backend.netty; + +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.shardingjdbc.core.constant.DatabaseType; +import io.shardingjdbc.proxy.frontend.common.FrontendHandlerFactory; +import io.shardingjdbc.proxy.transport.common.codec.PacketCodecFactory; +import lombok.RequiredArgsConstructor; + +/** + * Channel initializer. + * + * @author wangkai + */ +@RequiredArgsConstructor +public final class ClientHandlerInitializer extends ChannelInitializer { + + @Override + protected void initChannel(final SocketChannel socketChannel) { + ChannelPipeline pipeline = socketChannel.pipeline(); + // TODO load database type from yaml or startup arguments + pipeline.addLast(PacketCodecFactory.createPacketCodecInstance(DatabaseType.MySQL)); + pipeline.addLast(FrontendHandlerFactory.createFrontendHandlerInstance(DatabaseType.MySQL, userGroup)); + } +} diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/common/FrontendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/common/FrontendHandler.java index 4bae0c6c14886..dcdb78358f4d5 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/common/FrontendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/common/FrontendHandler.java @@ -20,6 +20,7 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.ChannelOutboundHandlerAdapter; /** * Frontend handler. From 4b6f711281f6721bfb789bb9d480254e2f77081a Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 25 Apr 2018 15:55:23 +0800 Subject: [PATCH 029/351] clear code --- .../saaavsaaa/client/untils/Constants.java | 10 ++ .../com/saaavsaaa/client/untils/PathUtil.java | 2 +- .../saaavsaaa/client/untils/Properties.java | 8 ++ .../client/zookeeper/BaseClient.java | 55 +++++++-- .../client/zookeeper/CacheClient.java | 64 +++++++++- .../client/zookeeper/UsualClient.java | 113 ++++++------------ .../{test => zookeeper}/CacheClientTest.java | 2 +- .../{test => zookeeper}/UsualClientTest.java | 32 +++-- 8 files changed, 186 insertions(+), 100 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/untils/Constants.java create mode 100644 src/main/java/com/saaavsaaa/client/untils/Properties.java rename src/test/java/com/saaavsaaa/client/{test => zookeeper}/CacheClientTest.java (64%) rename src/test/java/com/saaavsaaa/client/{test => zookeeper}/UsualClientTest.java (92%) diff --git a/src/main/java/com/saaavsaaa/client/untils/Constants.java b/src/main/java/com/saaavsaaa/client/untils/Constants.java new file mode 100644 index 0000000000000..d13309cf3470b --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/untils/Constants.java @@ -0,0 +1,10 @@ +package com.saaavsaaa.client.untils; + +/** + * Created by aaa on 18-4-25. + */ +public final class Constants { + public static final int VERSION = -1; + public static final byte[] NOTHING_DATA = new byte[0]; + public static final String NOTHING_VALUE = ""; +} diff --git a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java index bea32e376f808..c2f6ae11bac64 100644 --- a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java +++ b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java @@ -137,8 +137,8 @@ public static String checkPath(String path) throws IllegalArgumentException { continue; } + builder.append(previous); previous = chars[i]; - builder.append(c); } return builder.toString(); } diff --git a/src/main/java/com/saaavsaaa/client/untils/Properties.java b/src/main/java/com/saaavsaaa/client/untils/Properties.java new file mode 100644 index 0000000000000..eae36e7b3b087 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/untils/Properties.java @@ -0,0 +1,8 @@ +package com.saaavsaaa.client.untils; + +/** + * Created by aaa on 18-4-25. + */ +public final class Properties { + public static final boolean WATCH_ON = true; +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java index 557d81036f334..f3c40cd1cb000 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java @@ -1,5 +1,9 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.untils.Listener; +import com.saaavsaaa.client.untils.PathUtil; +import com.saaavsaaa.client.untils.Properties; +import com.saaavsaaa.client.untils.StringUtil; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooDefs; @@ -7,9 +11,6 @@ import org.apache.zookeeper.data.ACL; import java.io.IOException; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -20,10 +21,8 @@ */ public abstract class BaseClient { private static final CountDownLatch CONNECTED = new CountDownLatch(1); - private static final boolean WATCH_ON = true; - public static final int VERSION = -1; - public static final byte[] NOTHING_DATA = new byte[0]; protected static final Map watchers = new ConcurrentHashMap<>(); + private boolean watchRegistered = false; private final String servers; private final int sessionTimeOut; @@ -42,7 +41,7 @@ public void start() throws IOException, InterruptedException { CONNECTED.await(); } - public ZooKeeper getZooKeeper(){ + ZooKeeper getZooKeeper(){ return zooKeeper; } @@ -59,13 +58,53 @@ public void process(WatchedEvent event) { if (watchers.containsKey(rootNode)){ watchers.get(rootNode).process(event); } - if (WATCH_ON && watchers.containsKey(event.getPath())){ + if (Properties.WATCH_ON && watchers.containsKey(event.getPath())){ watchers.get(event.getPath()).process(event); } } }; } + void registerWatch(final Listener listener){ + if (watchRegistered){ + return; + } + watchRegistered = true; + watchers.put(rootNode, new Watcher() { + @Override + public void process(WatchedEvent event) { + listener.process(event); + } + }); + } + + public Watcher registerWatch(final String key, final Listener listener){ + String path = PathUtil.getRealPath(rootNode, key); +// listener.setKey(path); + Watcher watcher = new Watcher() { + @Override + public void process(WatchedEvent event) { + listener.process(event); + } + }; + watchers.put(path, watcher); + return watcher; + } + + public void unregisterWatch(final String key){ + if (StringUtil.isNullOrBlank(key)){ + throw new IllegalArgumentException("key should not be blank"); + } + String path = PathUtil.getRealPath(rootNode, key); + if (watchers.containsKey(path)){ + watchers.remove(path); + } + } + + public void close() throws InterruptedException { + zooKeeper.close(); + } + void setRootNode(String rootNode) { this.rootNode = rootNode; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index e0f01a2dc9fd0..2347b438e8412 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -1,11 +1,14 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.untils.Constants; import com.saaavsaaa.client.untils.PathUtil; +import com.saaavsaaa.client.untils.StringUtil; +import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.Transaction; -import javax.swing.tree.DefaultMutableTreeNode; -import java.util.Iterator; +import java.util.List; +import java.util.Stack; /** * Created by aaa on 18-4-19. @@ -15,6 +18,40 @@ public final class CacheClient extends UsualClient { super(servers, sessionTimeoutMilliseconds); } + + /* + * closed beta + */ + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + if (key.indexOf(PathUtil.PATH_SEPARATOR) < -1){ + this.createCurrentOnly(key, value, createMode); + return; + } + Transaction transaction = zooKeeper.transaction(); + //todo sync cache + List nodes = PathUtil.getPathOrderNodes(rootNode, key); + for (int i = 0; i < nodes.size(); i++) { + // todo contrast cache + if (checkExists(nodes.get(i))){ + System.out.println("exist:" + nodes.get(i)); + continue; + } + System.out.println("not exist:" + nodes.get(i)); + if (i == nodes.size() - 1){ + createInTransaction(nodes.get(i), value.getBytes(StringUtil.UTF_8), createMode, transaction); + } else { + createInTransaction(nodes.get(i), Constants.NOTHING_DATA, createMode, transaction); + } + } + + // todo org.apache.zookeeper.KeeperException$NodeExistsException: KeeperErrorCode = NodeExists + transaction.commit(); + } + + private Transaction createInTransaction(final String key, byte[] data, final CreateMode createMode, final Transaction transaction){ + return transaction.create(PathUtil.getRealPath(rootNode, key), data, authorities, createMode); + } + /* * closed beta * 当前实现方法用于缓存方式 @@ -22,7 +59,7 @@ public final class CacheClient extends UsualClient { * 用事务不能用异常 */ @Override - public void deleteAllChild(final String key) throws KeeperException, InterruptedException { + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { Transaction transaction = zooKeeper.transaction(); this.deleteAllChild(key, transaction); transaction.commit(); @@ -30,4 +67,25 @@ public void deleteAllChild(final String key) throws KeeperException, Interrupted private void deleteAllChild(final String key, final Transaction transaction) throws KeeperException, InterruptedException { } + + + @Override + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + if (key.indexOf(PathUtil.PATH_SEPARATOR) < -1){ + this.deleteOnlyCurrent(key); + return; + } + Transaction transaction = zooKeeper.transaction(); + //todo branch check + Stack pathStack = PathUtil.getPathReverseNodes(rootNode, key); + while (!pathStack.empty()){ + String node = pathStack.pop(); + // contrast cache + if (checkExists(node)){ + transaction.delete(node, Constants.VERSION); + System.out.println("delete : " + node); + } + } + transaction.commit(); + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index e316c44cfe491..b78475a905c9a 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -3,13 +3,12 @@ * Created by aaa on 18-4-18. */ -import com.saaavsaaa.client.untils.Listener; +import com.saaavsaaa.client.untils.Constants; import com.saaavsaaa.client.untils.PathUtil; import com.saaavsaaa.client.untils.StringUtil; import org.apache.zookeeper.*; import java.util.List; -import java.util.Stack; /* * cache @@ -17,7 +16,7 @@ */ public class UsualClient extends BaseClient { private final boolean watched = true; //false - private boolean watchRegistered = false; + UsualClient(String servers, int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); } @@ -30,7 +29,7 @@ public void createNamespace() throws KeeperException, InterruptedException { } public void deleteNamespace() throws KeeperException, InterruptedException { - zooKeeper.delete(rootNode, VERSION); + zooKeeper.delete(rootNode, Constants.VERSION); } public String getDataString(final String key) throws KeeperException, InterruptedException { @@ -80,9 +79,9 @@ public void createAllNeedPath(final String key, final String value, final Create } System.out.println("not exist:" + nodes.get(i)); if (i == nodes.size() - 1){ - createInTransaction(nodes.get(i), value.getBytes(StringUtil.UTF_8), createMode, transaction); + createCurrentOnly(nodes.get(i), value, createMode); } else { - createInTransaction(nodes.get(i), NOTHING_DATA, createMode, transaction); + createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); } } @@ -90,113 +89,73 @@ public void createAllNeedPath(final String key, final String value, final Create transaction.commit(); } - private Transaction createInTransaction(final String key, byte[] data, final CreateMode createMode, final Transaction transaction){ - return transaction.create(PathUtil.getRealPath(rootNode, key), data, authorities, createMode); - } - public void update(final String key, final String value) throws KeeperException, InterruptedException { - zooKeeper.setData(PathUtil.getRealPath(rootNode, key), value.getBytes(StringUtil.UTF_8), VERSION); + zooKeeper.setData(PathUtil.getRealPath(rootNode, key), value.getBytes(StringUtil.UTF_8), Constants.VERSION); } - public void updateInTransaction(final String key, final String value) throws KeeperException, InterruptedException { + public void updateWithCheck(final String key, final String value) throws KeeperException, InterruptedException { String realPath = PathUtil.getRealPath(rootNode, key); - zooKeeper.transaction().check(realPath, VERSION).setData(realPath, value.getBytes(StringUtil.UTF_8), VERSION).commit(); + zooKeeper.transaction().check(realPath, Constants.VERSION).setData(realPath, value.getBytes(StringUtil.UTF_8), Constants.VERSION).commit(); } public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - zooKeeper.delete(PathUtil.getRealPath(rootNode, key), VERSION); + zooKeeper.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION); + System.out.println("delete : " + PathUtil.getRealPath(rootNode, key)); } private void deleteOnlyCurrent(final String key, final Transaction transaction) throws KeeperException, InterruptedException { - zooKeeper.delete(PathUtil.getRealPath(rootNode, key), VERSION); + zooKeeper.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION); } public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - zooKeeper.delete(PathUtil.getRealPath(rootNode, key), VERSION, callback, ctx); - } - - public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - if (key.indexOf(PathUtil.PATH_SEPARATOR) < -1){ - this.deleteOnlyCurrent(key); - return; - } - Transaction transaction = zooKeeper.transaction(); - //todo branch check - Stack pathStack = PathUtil.getPathReverseNodes(rootNode, key); - while (!pathStack.empty()){ - String node = pathStack.pop(); - // contrast cache - if (checkExists(node)){ - transaction.delete(node, VERSION); - System.out.println("delete : " + node); - } - } - transaction.commit(); + zooKeeper.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION, callback, ctx); } - /* - * closed beta - * 当前实现方法用于缓存方式 - * 缓存实现后此类判断换为异常方式(包括创建) - * 用事务不能用异常 - */ - public void deleteAllChild(final String key) throws KeeperException, InterruptedException { + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { String realPath = PathUtil.getRealPath(rootNode, key); try { this.deleteOnlyCurrent(realPath); - System.out.println("delete : " + realPath); }catch (KeeperException.NotEmptyException ee){ List children = this.getChildren(realPath); for (String child : children) { child = realPath + PathUtil.PATH_SEPARATOR + child; - this.deleteAllChild(child); + this.deleteAllChildren(child); } this.deleteOnlyCurrent(realPath); - System.out.println("delete : " + realPath); } catch (KeeperException.NoNodeException ee){ System.out.println(ee.getMessage()); return; } } - void registerWatch(final Listener listener){ - if (watchRegistered){ + /* + * delete the current node with force and delete the super node whose only child node is current node recursively + */ + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(rootNode, key); + this.deleteAllChildren(path); + String superPath = path.substring(0, path.lastIndexOf(PathUtil.PATH_SEPARATOR)); + try { + this.deleteRecursively(superPath); + } catch (KeeperException.NotEmptyException ee){ return; } - watchRegistered = true; - watchers.put(rootNode, new Watcher() { - @Override - public void process(WatchedEvent event) { - listener.process(event); - } - }); - } - - public Watcher registerWatch(final String key, final Listener listener){ - String path = PathUtil.getRealPath(rootNode, key); -// listener.setKey(path); - Watcher watcher = new Watcher() { - @Override - public void process(WatchedEvent event) { - listener.process(event); - } - }; - watchers.put(path, watcher); - return watcher; } - public void unregisterWatch(final String key){ - if (StringUtil.isNullOrBlank(key)){ - throw new IllegalArgumentException("key should not be blank"); + private void deleteRecursively(final String path) throws KeeperException, InterruptedException { + int index = path.lastIndexOf(PathUtil.PATH_SEPARATOR); + if (index < 0){ + return; } - String path = PathUtil.getRealPath(rootNode, key); - if (watchers.containsKey(path)){ - watchers.remove(path); + String superPath = path.substring(0, index); + try { + this.deleteOnlyCurrent(path); + this.deleteRecursively(superPath); + } catch (KeeperException.NotEmptyException ee){ + List children = this.getChildren(path); + children.forEach((c) -> System.out.println(path + " exist other children " + c)); + return; } } - - public void close() throws InterruptedException { - zooKeeper.close(); - } } diff --git a/src/test/java/com/saaavsaaa/client/test/CacheClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java similarity index 64% rename from src/test/java/com/saaavsaaa/client/test/CacheClientTest.java rename to src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java index 5889a49739daa..0876640b0bd29 100644 --- a/src/test/java/com/saaavsaaa/client/test/CacheClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.test; +package com.saaavsaaa.client.zookeeper; /** * Created by aaa on 18-4-20. diff --git a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java similarity index 92% rename from src/test/java/com/saaavsaaa/client/test/UsualClientTest.java rename to src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index 4349eabdb7b01..ff62172d9ff07 100644 --- a/src/test/java/com/saaavsaaa/client/test/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -1,15 +1,12 @@ -package com.saaavsaaa.client.test; +package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.untils.Listener; import com.saaavsaaa.client.untils.PathUtil; import com.saaavsaaa.client.untils.StringUtil; -import com.saaavsaaa.client.zookeeper.ClientFactory; -import com.saaavsaaa.client.zookeeper.UsualClient; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import java.io.IOException; @@ -80,6 +77,21 @@ public void createChild() throws KeeperException, InterruptedException { assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key)/*"/" + ROOT + "/" + key*/, false) == null; } + @Test + public void deleteBranch() throws KeeperException, InterruptedException { + String keyB = "a/b/bb"; + client.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); + assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, keyB), false) != null; + String keyC = "a/c/cc"; + client.createAllNeedPath(keyC, "ccc11", CreateMode.PERSISTENT); + assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, keyC), false) != null; + client.deleteCurrentBranch(keyC); + assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, keyC), false) == null; + assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, "a"), false) != null; + client.deleteCurrentBranch(keyB); + assert client.getZooKeeper().exists(PathUtil.checkPath(ROOT), false) == null; + } + @Test public void isExisted() throws KeeperException, InterruptedException { String key = "a/b/bb"; @@ -161,7 +173,7 @@ public void persist() throws KeeperException, InterruptedException { } private void update(String key, String value) throws KeeperException, InterruptedException { - client.updateInTransaction(key, value); + client.updateWithCheck(key, value); } @Test @@ -174,7 +186,7 @@ public void persistEphemeral() throws KeeperException, InterruptedException { client.getZooKeeper().getData(PathUtil.getRealPath(ROOT, key), false, stat); assert stat.getEphemeralOwner() == 0; - client.deleteAllChild(key); + client.deleteAllChildren(key); assert !isExisted(key); client.createAllNeedPath(key, value, CreateMode.EPHEMERAL); @@ -190,7 +202,7 @@ public void delAllChildren() throws KeeperException, InterruptedException { client.createAllNeedPath(key, "cc", CreateMode.PERSISTENT); System.out.println(client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, "a"), null).getNumChildren()); // nearest children count assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), false) != null; - client.deleteAllChild("a"); + client.deleteAllChildren("a"); assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), false) == null; assert client.getZooKeeper().exists("/" + ROOT, false) != null; client.deleteNamespace(); @@ -213,12 +225,12 @@ public void watch() throws KeeperException, InterruptedException { client.createNamespace(); client.createCurrentOnly(key, "aaa", CreateMode.EPHEMERAL); client.checkExists(key, watcher); - client.updateInTransaction(key, "value"); + client.updateWithCheck(key, "value"); System.out.println(new String(client.getData(key))); assert client.getDataString(key).equals("value"); - client.updateInTransaction(key, "value1"); + client.updateWithCheck(key, "value1"); assert client.getDataString(key).equals("value1"); - client.updateInTransaction(key, "value2"); + client.updateWithCheck(key, "value2"); assert client.getDataString(key).equals("value2"); client.deleteCurrentBranch(key); Thread.sleep(100); From f689735fbd7d6f9f425fba7bdf69a92d87411c3e Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 25 Apr 2018 16:03:41 +0800 Subject: [PATCH 030/351] clear code --- .../saaavsaaa/client/untils/Constants.java | 4 ++++ .../com/saaavsaaa/client/untils/PathUtil.java | 21 +++++++++---------- .../saaavsaaa/client/untils/StringUtil.java | 1 - .../client/zookeeper/CacheClient.java | 6 +++--- .../client/zookeeper/UsualClient.java | 14 ++++++------- .../client/zookeeper/UsualClientTest.java | 7 ++++--- 6 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/untils/Constants.java b/src/main/java/com/saaavsaaa/client/untils/Constants.java index d13309cf3470b..e2a697444af9f 100644 --- a/src/main/java/com/saaavsaaa/client/untils/Constants.java +++ b/src/main/java/com/saaavsaaa/client/untils/Constants.java @@ -1,5 +1,7 @@ package com.saaavsaaa.client.untils; +import java.nio.charset.Charset; + /** * Created by aaa on 18-4-25. */ @@ -7,4 +9,6 @@ public final class Constants { public static final int VERSION = -1; public static final byte[] NOTHING_DATA = new byte[0]; public static final String NOTHING_VALUE = ""; + public static final Charset UTF_8 = Charset.forName("UTF-8"); + public static final String PATH_SEPARATOR = "/"; } diff --git a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java index c2f6ae11bac64..8dd5b3a428fe1 100644 --- a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java +++ b/src/main/java/com/saaavsaaa/client/untils/PathUtil.java @@ -7,7 +7,6 @@ * Created by aaa on 18-4-18. */ public class PathUtil { - public static final String PATH_SEPARATOR = "/"; public static String getRealPath(final String root, final String path){ return adjustPath(root, path); @@ -17,11 +16,11 @@ private static String adjustPath(String root, String path){ if (StringUtil.isNullOrBlank(path)){ throw new IllegalArgumentException("path should have content!"); } - if (!root.startsWith(PATH_SEPARATOR)){ - root = PATH_SEPARATOR + root; + if (!root.startsWith(Constants.PATH_SEPARATOR)){ + root = Constants.PATH_SEPARATOR + root; } - if (!path.startsWith(PATH_SEPARATOR)){ - path = PATH_SEPARATOR + path; + if (!path.startsWith(Constants.PATH_SEPARATOR)){ + path = Constants.PATH_SEPARATOR + path; } if (!path.startsWith(root)){ return root + path; @@ -34,11 +33,11 @@ public static Stack getPathReverseNodes(final String root, String path){ path = adjustPath(root, path); Stack pathStack = new Stack<>(); int index = 1; - int position = path.indexOf(PATH_SEPARATOR, index); + int position = path.indexOf(Constants.PATH_SEPARATOR, index); do{ pathStack.push(path.substring(0, position)); index = position + 1; - position = path.indexOf(PATH_SEPARATOR, index); + position = path.indexOf(Constants.PATH_SEPARATOR, index); } while (position > -1); pathStack.push(path); @@ -49,12 +48,12 @@ public static List getPathOrderNodes(final String root, String path){ path = adjustPath(root, path); List paths = new ArrayList<>(); int index = 1; - int position = path.indexOf('/', index); + int position = path.indexOf(Constants.PATH_SEPARATOR, index); do{ paths.add(path.substring(0, position)); index = position + 1; - position = path.indexOf('/', index); + position = path.indexOf(Constants.PATH_SEPARATOR, index); } while (position > -1); paths.add(path); @@ -105,11 +104,11 @@ public static String checkPath(String path) throws IllegalArgumentException { throw new IllegalArgumentException("path should not be null"); } if(path.charAt(0) != 47 || path.charAt(path.length() - 1) == 47){ - path = PATH_SEPARATOR + path; + path = Constants.PATH_SEPARATOR + path; } if(path.charAt(path.length() - 1) == 47){ - path = PATH_SEPARATOR + path; + path = Constants.PATH_SEPARATOR + path; } char previous = 47; diff --git a/src/main/java/com/saaavsaaa/client/untils/StringUtil.java b/src/main/java/com/saaavsaaa/client/untils/StringUtil.java index 585ce22004c2e..f23c3a64dedd4 100644 --- a/src/main/java/com/saaavsaaa/client/untils/StringUtil.java +++ b/src/main/java/com/saaavsaaa/client/untils/StringUtil.java @@ -6,7 +6,6 @@ * Created by aaa on 18-4-18. */ public class StringUtil { - public static final Charset UTF_8 = Charset.forName("UTF-8"); public static boolean isNullOrBlank(String string) { return string == null || string.trim().length() == 0; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 2347b438e8412..c62fa4e09442c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -23,7 +23,7 @@ public final class CacheClient extends UsualClient { * closed beta */ public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - if (key.indexOf(PathUtil.PATH_SEPARATOR) < -1){ + if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ this.createCurrentOnly(key, value, createMode); return; } @@ -38,7 +38,7 @@ public void createAllNeedPath(final String key, final String value, final Create } System.out.println("not exist:" + nodes.get(i)); if (i == nodes.size() - 1){ - createInTransaction(nodes.get(i), value.getBytes(StringUtil.UTF_8), createMode, transaction); + createInTransaction(nodes.get(i), value.getBytes(Constants.UTF_8), createMode, transaction); } else { createInTransaction(nodes.get(i), Constants.NOTHING_DATA, createMode, transaction); } @@ -71,7 +71,7 @@ private void deleteAllChild(final String key, final Transaction transaction) thr @Override public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - if (key.indexOf(PathUtil.PATH_SEPARATOR) < -1){ + if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ this.deleteOnlyCurrent(key); return; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index b78475a905c9a..6dedef6954757 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -57,14 +57,14 @@ public List getChildren(final String key) throws KeeperException, Interr } public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - zooKeeper.create(PathUtil.getRealPath(rootNode, key), value.getBytes(StringUtil.UTF_8), authorities, createMode); + zooKeeper.create(PathUtil.getRealPath(rootNode, key), value.getBytes(Constants.UTF_8), authorities, createMode); } /* * closed beta */ public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - if (key.indexOf(PathUtil.PATH_SEPARATOR) < -1){ + if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ this.createCurrentOnly(key, value, createMode); return; } @@ -90,12 +90,12 @@ public void createAllNeedPath(final String key, final String value, final Create } public void update(final String key, final String value) throws KeeperException, InterruptedException { - zooKeeper.setData(PathUtil.getRealPath(rootNode, key), value.getBytes(StringUtil.UTF_8), Constants.VERSION); + zooKeeper.setData(PathUtil.getRealPath(rootNode, key), value.getBytes(Constants.UTF_8), Constants.VERSION); } public void updateWithCheck(final String key, final String value) throws KeeperException, InterruptedException { String realPath = PathUtil.getRealPath(rootNode, key); - zooKeeper.transaction().check(realPath, Constants.VERSION).setData(realPath, value.getBytes(StringUtil.UTF_8), Constants.VERSION).commit(); + zooKeeper.transaction().check(realPath, Constants.VERSION).setData(realPath, value.getBytes(Constants.UTF_8), Constants.VERSION).commit(); } public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { @@ -118,7 +118,7 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup }catch (KeeperException.NotEmptyException ee){ List children = this.getChildren(realPath); for (String child : children) { - child = realPath + PathUtil.PATH_SEPARATOR + child; + child = realPath + Constants.PATH_SEPARATOR + child; this.deleteAllChildren(child); } this.deleteOnlyCurrent(realPath); @@ -134,7 +134,7 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { String path = PathUtil.getRealPath(rootNode, key); this.deleteAllChildren(path); - String superPath = path.substring(0, path.lastIndexOf(PathUtil.PATH_SEPARATOR)); + String superPath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); try { this.deleteRecursively(superPath); } catch (KeeperException.NotEmptyException ee){ @@ -143,7 +143,7 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr } private void deleteRecursively(final String path) throws KeeperException, InterruptedException { - int index = path.lastIndexOf(PathUtil.PATH_SEPARATOR); + int index = path.lastIndexOf(Constants.PATH_SEPARATOR); if (index < 0){ return; } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index ff62172d9ff07..4860ceec179d2 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.untils.Constants; import com.saaavsaaa.client.untils.Listener; import com.saaavsaaa.client.untils.PathUtil; import com.saaavsaaa.client.untils.StringUtil; @@ -63,9 +64,9 @@ public void stop() throws InterruptedException { @Test public void createRoot() throws KeeperException, InterruptedException { client.createNamespace(); - assert client.getZooKeeper().exists(PathUtil.PATH_SEPARATOR + ROOT, false) != null; + assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + ROOT, false) != null; client.deleteNamespace(); - assert client.getZooKeeper().exists(PathUtil.PATH_SEPARATOR + ROOT, false) == null; + assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + ROOT, false) == null; } @Test @@ -304,7 +305,7 @@ public void process(WatchedEvent event) { } catch (InterruptedException e) { e.printStackTrace(); } - String result = null == data ? null : new String(data, StringUtil.UTF_8); + String result = null == data ? null : new String(data, Constants.UTF_8); eventListener.onChange(new DataChangedEvent(getEventType(event, result), event.getPath(), result)); } From 8f2d8b27417363b0c0a679fb7070042c44162f1e Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 25 Apr 2018 17:48:15 +0800 Subject: [PATCH 031/351] root delete watcher --- .../saaavsaaa/client/untils/Constants.java | 1 + .../client/zookeeper/BaseClient.java | 30 ++++++++-------- .../client/zookeeper/ClientFactory.java | 2 +- .../client/zookeeper/UsualClient.java | 34 +++++++++++++++---- .../client/zookeeper/UsualClientTest.java | 8 +++++ 5 files changed, 51 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/untils/Constants.java b/src/main/java/com/saaavsaaa/client/untils/Constants.java index e2a697444af9f..f0a9760975446 100644 --- a/src/main/java/com/saaavsaaa/client/untils/Constants.java +++ b/src/main/java/com/saaavsaaa/client/untils/Constants.java @@ -11,4 +11,5 @@ public final class Constants { public static final String NOTHING_VALUE = ""; public static final Charset UTF_8 = Charset.forName("UTF-8"); public static final String PATH_SEPARATOR = "/"; + public static final String GLOBAL_LISTENER_KEY = "globalListener"; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java index f3c40cd1cb000..8d53f78cee6bb 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java @@ -1,9 +1,6 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.untils.Listener; -import com.saaavsaaa.client.untils.PathUtil; -import com.saaavsaaa.client.untils.Properties; -import com.saaavsaaa.client.untils.StringUtil; +import com.saaavsaaa.client.untils.*; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooDefs; @@ -16,19 +13,22 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; +import static org.apache.zookeeper.Watcher.Event.EventType.NodeDeleted; + /** * Created by aaa on 18-4-19. */ public abstract class BaseClient { private static final CountDownLatch CONNECTED = new CountDownLatch(1); protected static final Map watchers = new ConcurrentHashMap<>(); - private boolean watchRegistered = false; + private boolean globalListenerRegistered = false; private final String servers; private final int sessionTimeOut; protected ZooKeeper zooKeeper; protected String rootNode = "/InitValue"; + protected boolean rootExist = false; protected List authorities; protected BaseClient(String servers, int sessionTimeoutMilliseconds) { @@ -37,7 +37,7 @@ protected BaseClient(String servers, int sessionTimeoutMilliseconds) { } public void start() throws IOException, InterruptedException { - zooKeeper = new ZooKeeper(servers, sessionTimeOut, connectWatcher()); + zooKeeper = new ZooKeeper(servers, sessionTimeOut, startWatcher()); CONNECTED.await(); } @@ -45,7 +45,7 @@ ZooKeeper getZooKeeper(){ return zooKeeper; } - private Watcher connectWatcher() { + private Watcher startWatcher() { return new Watcher(){ public void process(WatchedEvent event) { if(Event.KeeperState.SyncConnected == event.getState()){ @@ -53,10 +53,8 @@ public void process(WatchedEvent event) { CONNECTED.countDown(); } } - // key == rootNode signify that register a watcher without appoint path when client init - // or want to watch the whole namespace - if (watchers.containsKey(rootNode)){ - watchers.get(rootNode).process(event); + if (globalListenerRegistered){ + watchers.get(Constants.GLOBAL_LISTENER_KEY).process(event); } if (Properties.WATCH_ON && watchers.containsKey(event.getPath())){ watchers.get(event.getPath()).process(event); @@ -65,15 +63,15 @@ public void process(WatchedEvent event) { }; } - void registerWatch(final Listener listener){ - if (watchRegistered){ + void registerWatch(final Listener globalListener){ + if (globalListenerRegistered){ return; } - watchRegistered = true; - watchers.put(rootNode, new Watcher() { + globalListenerRegistered = true; + watchers.put(Constants.GLOBAL_LISTENER_KEY, new Watcher() { @Override public void process(WatchedEvent event) { - listener.process(event); + globalListener.process(event); } }); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 4285dbc88659d..e55af7198c7cd 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -35,8 +35,8 @@ public ClientFactory watch(final Listener listener){ } public synchronized UsualClient start() throws IOException, InterruptedException { - client.start(); client.setRootNode(namespace); + client.start(); client.setAuthorities(scheme , auth); if (globalListener != null) { client.registerWatch(globalListener); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 6dedef6954757..3f6d150f7fbe9 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -10,9 +10,12 @@ import java.util.List; +import static org.apache.zookeeper.Watcher.Event.EventType.NodeDeleted; + /* * cache * todo Sequential +* todo org.apache.zookeeper.KeeperException$NodeExistsException: KeeperErrorCode = NodeExists */ public class UsualClient extends BaseClient { private final boolean watched = true; //false @@ -22,10 +25,25 @@ public class UsualClient extends BaseClient { } public void createNamespace() throws KeeperException, InterruptedException { - if (checkExists(rootNode)){ + if (rootExist){ return; } zooKeeper.create(rootNode, new byte[0], authorities, CreateMode.PERSISTENT); + rootExist = true; + zooKeeper.exists(rootNode, new Watcher() { + @Override + public void process(WatchedEvent event) { + if (rootNode.equals(event.getPath()) && NodeDeleted.equals(event.getType())){ + rootExist = false; + System.out.println("----------------------------------------------delete root"); + System.out.println(event.getPath()); + System.out.println(event.getState()); + System.out.println(event.getType()); + System.out.println("----------------------------------------------delete root"); + } + } + }); + System.out.println("----------------------------------------------create root"); } public void deleteNamespace() throws KeeperException, InterruptedException { @@ -57,7 +75,12 @@ public List getChildren(final String key) throws KeeperException, Interr } public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - zooKeeper.create(PathUtil.getRealPath(rootNode, key), value.getBytes(Constants.UTF_8), authorities, createMode); + createNamespace(); + String path = PathUtil.getRealPath(rootNode, key); + if (rootNode.equals(path)){ + return; + } + zooKeeper.create(path, value.getBytes(Constants.UTF_8), authorities, createMode); } /* @@ -68,9 +91,9 @@ public void createAllNeedPath(final String key, final String value, final Create this.createCurrentOnly(key, value, createMode); return; } - Transaction transaction = zooKeeper.transaction(); - //todo sync cache + List nodes = PathUtil.getPathOrderNodes(rootNode, key); + nodes.remove(rootNode); for (int i = 0; i < nodes.size(); i++) { // todo contrast cache if (checkExists(nodes.get(i))){ @@ -84,9 +107,6 @@ public void createAllNeedPath(final String key, final String value, final Create createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); } } - - // todo org.apache.zookeeper.KeeperException$NodeExistsException: KeeperErrorCode = NodeExists - transaction.commit(); } public void update(final String key, final String value) throws KeeperException, InterruptedException { diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index 4860ceec179d2..1878dca3e0b67 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -8,6 +8,7 @@ import org.apache.zookeeper.data.Stat; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import java.io.IOException; @@ -69,6 +70,13 @@ public void createRoot() throws KeeperException, InterruptedException { assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + ROOT, false) == null; } + @Ignore + @Test + public void deleteRoot() throws KeeperException, InterruptedException { + client.deleteNamespace(); + assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + ROOT, false) == null; + } + @Test public void createChild() throws KeeperException, InterruptedException { String key = "a/b/bb"; From 6c6f2e42b38baed8429d659c8405502aa7e1723a Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 25 Apr 2018 19:01:38 +0800 Subject: [PATCH 032/351] test --- .../client/zookeeper/UsualClient.java | 29 ++++++++++++------- .../client/zookeeper/UsualClientTest.java | 8 +++-- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 3f6d150f7fbe9..d7c01e9b51c55 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -80,11 +80,19 @@ public void createCurrentOnly(final String key, final String value, final Create if (rootNode.equals(path)){ return; } - zooKeeper.create(path, value.getBytes(Constants.UTF_8), authorities, createMode); + try { + zooKeeper.create(path, value.getBytes(Constants.UTF_8), authorities, createMode); + } catch (KeeperException.NoNodeException e) { + // I don't know whether it will happen or not, if root watcher don't update rootExist timely + if (e.getMessage().contains(path)) { + rootExist = false; + this.createCurrentOnly(key, value, createMode); + } + } } /* - * closed beta + * todo exception recursion */ public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ @@ -95,17 +103,18 @@ public void createAllNeedPath(final String key, final String value, final Create List nodes = PathUtil.getPathOrderNodes(rootNode, key); nodes.remove(rootNode); for (int i = 0; i < nodes.size(); i++) { - // todo contrast cache - if (checkExists(nodes.get(i))){ + try { + if (i == nodes.size() - 1){ + this.createCurrentOnly(nodes.get(i), value, createMode); + } else { +// this.deleteAllChildren(nodes.get(i)); + this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); + } + System.out.println("not exist and create:" + nodes.get(i)); + } catch (KeeperException.NodeExistsException ee){ System.out.println("exist:" + nodes.get(i)); continue; } - System.out.println("not exist:" + nodes.get(i)); - if (i == nodes.size() - 1){ - createCurrentOnly(nodes.get(i), value, createMode); - } else { - createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); - } } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index 1878dca3e0b67..1de9e5823204f 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -29,13 +29,13 @@ public class UsualClientTest { private UsualClient client = null; -// @Before + @Before public void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); client = creator.setNamespace(ROOT).authorization(AUTH, AUTH.getBytes()).newClient(SERVERS, SESSION_TIMEOUT).start(); } - @Before +// @Before public void startWithWatch() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); Listener listener = buildListener(); @@ -99,6 +99,10 @@ public void deleteBranch() throws KeeperException, InterruptedException { assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, "a"), false) != null; client.deleteCurrentBranch(keyB); assert client.getZooKeeper().exists(PathUtil.checkPath(ROOT), false) == null; + client.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); + assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, keyB), false) != null; + client.deleteCurrentBranch(keyB); + assert client.getZooKeeper().exists(PathUtil.checkPath(ROOT), false) == null; } @Test From a06208bdc8e70cf894f69b10eda5a47cb5a14fa6 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 26 Apr 2018 10:59:23 +0800 Subject: [PATCH 033/351] sleep --- src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java | 3 ++- .../java/com/saaavsaaa/client/zookeeper/UsualClientTest.java | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index d7c01e9b51c55..143e4085b91c9 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -85,7 +85,8 @@ public void createCurrentOnly(final String key, final String value, final Create } catch (KeeperException.NoNodeException e) { // I don't know whether it will happen or not, if root watcher don't update rootExist timely if (e.getMessage().contains(path)) { - rootExist = false; + System.out.println("rootExist : " + e.getMessage()); + Thread.sleep(50); this.createCurrentOnly(key, value, createMode); } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index 1de9e5823204f..da9ca10bbb701 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -29,13 +29,13 @@ public class UsualClientTest { private UsualClient client = null; - @Before +// @Before public void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); client = creator.setNamespace(ROOT).authorization(AUTH, AUTH.getBytes()).newClient(SERVERS, SESSION_TIMEOUT).start(); } -// @Before + @Before public void startWithWatch() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); Listener listener = buildListener(); From ddedabdfb844a245adc35d93e766ff041cff6e04 Mon Sep 17 00:00:00 2001 From: WangKai Date: Thu, 26 Apr 2018 11:03:54 +0800 Subject: [PATCH 034/351] netty client framework --- .../java/io/shardingjdbc/proxy/Bootstrap.java | 5 +- .../proxy/backend/ShardingProxyClient.java | 50 ++-- .../proxy/backend/common/BackendHandler.java | 28 +- .../backend/common/BackendHandlerFactory.java | 7 +- .../common/CommandResponsePacketsHandler.java | 10 + .../backend/mysql/MySQLBackendHandler.java | 54 ++++ .../mysql/MySQLExecuteBackendHandler.java | 249 ------------------ .../netty/ClientHandlerInitializer.java | 3 +- .../proxy/config/ShardingRuleRegistry.java | 2 + .../proxy/frontend/ShardingProxy.java | 13 +- .../proxy/util/SynchronizedFuture.java | 82 ++++++ 11 files changed, 207 insertions(+), 296 deletions(-) create mode 100644 sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java create mode 100644 sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java delete mode 100644 sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLExecuteBackendHandler.java create mode 100644 sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/Bootstrap.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/Bootstrap.java index ac100fef9bd4f..b14c6c1901ee4 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/Bootstrap.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/Bootstrap.java @@ -19,10 +19,13 @@ import io.shardingjdbc.proxy.frontend.ShardingProxy; +import java.net.MalformedURLException; + /** * Sharding-Proxy Bootstrap. * * @author zhangliang + * @author wangkai */ public final class Bootstrap { @@ -34,7 +37,7 @@ public final class Bootstrap { * @param args startup arguments * @throws InterruptedException interrupted exception */ - public static void main(final String[] args) throws InterruptedException { + public static void main(final String[] args) throws InterruptedException, MalformedURLException { new ShardingProxy().start(getPort(args)); } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java index 373b19175b5a8..0782af8868dab 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java @@ -19,6 +19,7 @@ import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; @@ -31,9 +32,13 @@ import io.netty.handler.logging.LoggingHandler; import io.shardingjdbc.proxy.backend.netty.ClientHandlerInitializer; import io.shardingjdbc.proxy.config.ShardingRuleRegistry; +import lombok.Getter; import org.apache.commons.dbcp2.BasicDataSource; import javax.sql.DataSource; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; import java.util.Map; /** @@ -42,31 +47,37 @@ * @author wangkai */ public final class ShardingProxyClient { + private static final ShardingProxyClient INSTANCE = new ShardingProxyClient(); private static final int WORKER_MAX_THREADS = Runtime.getRuntime().availableProcessors(); private EventLoopGroup workerGroup; + @Getter + private Map channelMap = new HashMap<>(); + /** * Start Sharding-Proxy. * * @throws InterruptedException interrupted exception */ - public void start() throws InterruptedException { - try { - Bootstrap bootstrap = new Bootstrap(); - if (workerGroup instanceof EpollEventLoopGroup) { - groupsEpoll(bootstrap); - } else { - groupsNio(bootstrap); - } - Map dataSourceMap = ShardingRuleRegistry.getInstance().getDataSourceMap(); - for (DataSource each : dataSourceMap.values()) { - ((BasicDataSource)each).getUrl(). - ChannelFuture future = bootstrap.connect("localhost",3306).sync(); - future.channel().closeFuture().sync(); - } - } finally { + public void start() throws InterruptedException, MalformedURLException { + Bootstrap bootstrap = new Bootstrap(); + if (workerGroup instanceof EpollEventLoopGroup) { + groupsEpoll(bootstrap); + } else { + groupsNio(bootstrap); + } + Map dataSourceMap = ShardingRuleRegistry.getInstance().getDataSourceMap(); + for (Map.Entry each : dataSourceMap.entrySet()) { + URL url = new URL(((BasicDataSource)each.getValue()).getUrl().replaceAll("jdbc:mysql://","http://")); + ChannelFuture future = bootstrap.connect(url.getHost(),url.getPort()).sync(); + channelMap.put(each.getKey(),future.channel()); + } + } + + public void stop(){ + if(workerGroup != null){ workerGroup.shutdownGracefully(); } } @@ -95,4 +106,13 @@ private void groupsNio(final Bootstrap bootstrap) { .handler(new LoggingHandler(LogLevel.INFO)) .handler(new ClientHandlerInitializer()); } + + /** + * Get instance of sharding-proxy client. + * + * @return instance of sharding-proxy client + */ + public static ShardingProxyClient getInstance() { + return INSTANCE; + } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandler.java index 37cde952cc452..d3fedc99d2513 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandler.java @@ -26,37 +26,13 @@ * Backend handler. * * @author zhangliang - * @author wangkai */ -public abstract class BackendHandler extends ChannelInboundHandlerAdapter { - - private boolean handshaked; - - private boolean authorized; - - protected abstract void handshake(ChannelHandlerContext context, ByteBuf message); - - @Override - public void channelRead(final ChannelHandlerContext context, final Object message) { - if (!handshaked) { - handshake(context, (ByteBuf) message); - handshaked = true; - } else if(authorized) { - auth(context, (ByteBuf) message); - authorized = true; - } else { - executeCommand(context, (ByteBuf) message); - } - } - - protected abstract void auth(ChannelHandlerContext context, ByteBuf message); - - protected abstract void executeCommand(ChannelHandlerContext context, ByteBuf message); +public interface BackendHandler { /** * Execute command. * * @return result packets to be sent */ - protected abstract CommandResponsePackets execute(); + CommandResponsePackets execute(); } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java index ec9d14fc2da4a..b600f3a57b8c1 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java @@ -19,6 +19,7 @@ import io.netty.channel.EventLoopGroup; import io.shardingjdbc.core.constant.DatabaseType; +import io.shardingjdbc.proxy.backend.mysql.MySQLBackendHandler; import io.shardingjdbc.proxy.frontend.common.FrontendHandler; import io.shardingjdbc.proxy.frontend.mysql.MySQLFrontendHandler; import lombok.AccessLevel; @@ -32,15 +33,15 @@ public final class BackendHandlerFactory { /** - * Create frontend handler instance. + * Create bakcend handler instance. * * @param databaseType database type * @return backend handler instance */ - public static BackendHandler createFrontendHandlerInstance(final DatabaseType databaseType) { + public static CommandResponsePacketsHandler createBackendHandlerInstance(final DatabaseType databaseType) { switch (databaseType) { case MySQL: - return new MySQLFrontendHandler(); + return new MySQLBackendHandler(); default: throw new UnsupportedOperationException(String.format("Cannot support database type '%s'", databaseType)); } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java new file mode 100644 index 0000000000000..5c28e1f01c8c1 --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java @@ -0,0 +1,10 @@ +package io.shardingjdbc.proxy.backend.common; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +public abstract class CommandResponsePacketsHandler extends ChannelInboundHandlerAdapter { + protected abstract void auth(ChannelHandlerContext context, ByteBuf message); + protected abstract void executeCommandResponsePackets(ChannelHandlerContext context, ByteBuf message); +} diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java new file mode 100644 index 0000000000000..bf378b43fb198 --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java @@ -0,0 +1,54 @@ +/* + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingjdbc.proxy.backend.mysql; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.shardingjdbc.proxy.backend.common.BackendHandler; +import io.shardingjdbc.proxy.backend.common.CommandResponsePacketsHandler; +import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; + +/** + * Backend handler. + * + * @author wangkai + */ +public class MySQLBackendHandler extends CommandResponsePacketsHandler { + + @Override + public void channelRead(final ChannelHandlerContext context, final Object message) { + //TODO 判断报文是握手还是 + if (true) { + auth(context,(ByteBuf) message); + } else { + + } + } + + @Override + protected void auth(ChannelHandlerContext context, ByteBuf message) { + + } + + @Override + protected void executeCommandResponsePackets(ChannelHandlerContext context, ByteBuf message) { + + } + +} diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLExecuteBackendHandler.java deleted file mode 100644 index 50b4c61f074ee..0000000000000 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLExecuteBackendHandler.java +++ /dev/null @@ -1,249 +0,0 @@ -/* - * Copyright 1999-2015 dangdang.com. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingjdbc.proxy.backend.mysql; - -import io.shardingjdbc.core.constant.DatabaseType; -import io.shardingjdbc.core.constant.SQLType; -import io.shardingjdbc.core.merger.MergeEngineFactory; -import io.shardingjdbc.core.merger.MergedResult; -import io.shardingjdbc.core.merger.QueryResult; -import io.shardingjdbc.core.parsing.parser.sql.SQLStatement; -import io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement; -import io.shardingjdbc.core.routing.SQLExecutionUnit; -import io.shardingjdbc.core.routing.SQLRouteResult; -import io.shardingjdbc.core.routing.StatementRoutingEngine; -import io.shardingjdbc.proxy.backend.common.BackendHandler; -import io.shardingjdbc.proxy.config.ShardingRuleRegistry; -import io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket; -import io.shardingjdbc.proxy.transport.mysql.constant.ColumnType; -import io.shardingjdbc.proxy.transport.mysql.constant.StatusFlag; -import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; -import io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet; -import io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.FieldCountPacket; -import io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket; -import io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket; -import io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket; -import io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket; - -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; - -/** - * SQL execute backend handler with mysql protocol. - * - * @author wangkai - */ -public final class MySQLExecuteBackendHandler implements BackendHandler { - - private final String sql; - - private final StatementRoutingEngine routingEngine; - - public MySQLExecuteBackendHandler(final String sql, final DatabaseType databaseType, final boolean showSQL) { - this.sql = sql; - routingEngine = new StatementRoutingEngine(ShardingRuleRegistry.getInstance().getShardingRule(), databaseType, showSQL); - } - - @Override - public CommandResponsePackets execute() { - SQLRouteResult routeResult = routingEngine.route(sql); - if (routeResult.getExecutionUnits().isEmpty()) { - return new CommandResponsePackets(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); - } - List result = new LinkedList<>(); - for (SQLExecutionUnit each : routeResult.getExecutionUnits()) { - // TODO multiple threads - result.add(execute(routeResult.getSqlStatement(), each)); - } - return merge(routeResult.getSqlStatement(), result); - } - - private CommandResponsePackets execute(final SQLStatement sqlStatement, final SQLExecutionUnit sqlExecutionUnit) { - switch (sqlStatement.getType()) { - case DQL: - return executeQuery(ShardingRuleRegistry.getInstance().getDataSourceMap().get(sqlExecutionUnit.getDataSource()), sqlExecutionUnit.getSql()); - case DML: - case DDL: - return executeUpdate(ShardingRuleRegistry.getInstance().getDataSourceMap().get(sqlExecutionUnit.getDataSource()), sqlExecutionUnit.getSql(), sqlStatement); - default: - return executeCommon(ShardingRuleRegistry.getInstance().getDataSourceMap().get(sqlExecutionUnit.getDataSource()), sqlExecutionUnit.getSql()); - } - } - - private CommandResponsePackets executeQuery(final DataSource dataSource, final String sql) { - try ( - Connection connection = dataSource.getConnection(); - Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery(sql)) { - return getDatabaseProtocolPackets(resultSet); - } catch (final SQLException ex) { - return new CommandResponsePackets(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage())); - } - } - - private CommandResponsePackets executeCommon(final DataSource dataSource, final String sql) { - try ( - Connection connection = dataSource.getConnection(); - Statement statement = connection.createStatement()) { - boolean hasResultSet = statement.execute(sql); - if (hasResultSet) { - return getDatabaseProtocolPackets(statement.getResultSet()); - } else { - return new CommandResponsePackets(new OKPacket(1, statement.getUpdateCount(), 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); - } - } catch (final SQLException ex) { - return new CommandResponsePackets(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage())); - } - } - - private CommandResponsePackets getDatabaseProtocolPackets(final ResultSet resultSet) throws SQLException { - CommandResponsePackets result = new CommandResponsePackets(); - int currentSequenceId = 0; - ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); - int columnCount = resultSetMetaData.getColumnCount(); - if (0 == columnCount) { - result.addPacket(new OKPacket(++currentSequenceId, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); - return result; - } - result.addPacket(new FieldCountPacket(++currentSequenceId, columnCount)); - for (int i = 1; i <= columnCount; i++) { - result.addPacket(new ColumnDefinition41Packet(++currentSequenceId, resultSetMetaData.getSchemaName(i), resultSetMetaData.getTableName(i), - resultSetMetaData.getTableName(i), resultSetMetaData.getColumnLabel(i), resultSetMetaData.getColumnName(i), - resultSetMetaData.getColumnDisplaySize(i), ColumnType.valueOfJDBCType(resultSetMetaData.getColumnType(i)), 0)); - } - result.addPacket(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue())); - while (resultSet.next()) { - List data = new ArrayList<>(columnCount); - for (int i = 1; i <= columnCount; i++) { - data.add(resultSet.getObject(i)); - } - result.addPacket(new TextResultSetRowPacket(++currentSequenceId, data)); - } - result.addPacket(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue())); - return result; - } - - private CommandResponsePackets executeUpdate(final DataSource dataSource, final String sql, final SQLStatement sqlStatement) { - try ( - Connection connection = dataSource.getConnection(); - Statement statement = connection.createStatement()) { - int affectedRows; - long lastInsertId = 0; - if (sqlStatement instanceof InsertStatement) { - affectedRows = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS); - lastInsertId = getGeneratedKey(statement); - } else { - affectedRows = statement.executeUpdate(sql); - } - return new CommandResponsePackets(new OKPacket(1, affectedRows, lastInsertId, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); - } catch (final SQLException ex) { - return new CommandResponsePackets(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage())); - } - } - - private long getGeneratedKey(final Statement statement) throws SQLException { - long result = 0; - ResultSet resultSet = statement.getGeneratedKeys(); - if (resultSet.next()) { - result = resultSet.getLong(1); - } - return result; - } - - private CommandResponsePackets merge(final SQLStatement sqlStatement, final List packets) { - if (1 == packets.size()) { - return packets.iterator().next(); - } - CommandResponsePackets headPackets = new CommandResponsePackets(); - for (CommandResponsePackets each : packets) { - headPackets.addPacket(each.getHeadPacket()); - } - for (DatabaseProtocolPacket each : headPackets.getDatabaseProtocolPackets()) { - if (each instanceof ErrPacket) { - return new CommandResponsePackets(each); - } - } - if (SQLType.DML == sqlStatement.getType()) { - return mergeDML(headPackets); - } - if (SQLType.DQL == sqlStatement.getType() || SQLType.DAL == sqlStatement.getType()) { - return mergeDQLorDAL(sqlStatement, packets); - } - return packets.get(0); - } - - private CommandResponsePackets mergeDML(final CommandResponsePackets firstPackets) { - int affectedRows = 0; - for (DatabaseProtocolPacket each : firstPackets.getDatabaseProtocolPackets()) { - if (each instanceof OKPacket) { - OKPacket okPacket = (OKPacket) each; - affectedRows += okPacket.getAffectedRows(); - } - } - return new CommandResponsePackets(new OKPacket(1, affectedRows, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); - } - - private CommandResponsePackets mergeDQLorDAL(final SQLStatement sqlStatement, final List packets) { - List queryResults = new ArrayList<>(packets.size()); - for (CommandResponsePackets each : packets) { - // TODO replace to a common PacketQueryResult - queryResults.add(new MySQLPacketQueryResult(each)); - } - MergedResult mergedResult; - try { - mergedResult = MergeEngineFactory.newInstance(ShardingRuleRegistry.getInstance().getShardingRule(), queryResults, sqlStatement).merge(); - } catch (final SQLException ex) { - return new CommandResponsePackets(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage())); - } - return buildPackets(packets, mergedResult); - } - - private CommandResponsePackets buildPackets(final List packets, final MergedResult mergedResult) { - CommandResponsePackets result = new CommandResponsePackets(); - Iterator databaseProtocolPacketsSampling = packets.iterator().next().getDatabaseProtocolPackets().iterator(); - FieldCountPacket fieldCountPacketSampling = (FieldCountPacket) databaseProtocolPacketsSampling.next(); - result.addPacket(fieldCountPacketSampling); - int columnCount = fieldCountPacketSampling.getColumnCount(); - for (int i = 0; i < columnCount; i++) { - result.addPacket(databaseProtocolPacketsSampling.next()); - } - result.addPacket(databaseProtocolPacketsSampling.next()); - int currentSequenceId = result.size(); - try { - while (mergedResult.next()) { - List data = new ArrayList<>(columnCount); - for (int i = 1; i <= columnCount; i++) { - data.add(mergedResult.getValue(i, Object.class)); - } - result.addPacket(new TextResultSetRowPacket(++currentSequenceId, data)); - } - } catch (final SQLException ex) { - return new CommandResponsePackets(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage())); - } - result.addPacket(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue())); - return result; - } -} diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java index 1597f6441ceea..45b3a5598b7b2 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java @@ -22,6 +22,7 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.shardingjdbc.core.constant.DatabaseType; +import io.shardingjdbc.proxy.backend.common.BackendHandlerFactory; import io.shardingjdbc.proxy.frontend.common.FrontendHandlerFactory; import io.shardingjdbc.proxy.transport.common.codec.PacketCodecFactory; import lombok.RequiredArgsConstructor; @@ -39,6 +40,6 @@ protected void initChannel(final SocketChannel socketChannel) { ChannelPipeline pipeline = socketChannel.pipeline(); // TODO load database type from yaml or startup arguments pipeline.addLast(PacketCodecFactory.createPacketCodecInstance(DatabaseType.MySQL)); - pipeline.addLast(FrontendHandlerFactory.createFrontendHandlerInstance(DatabaseType.MySQL, userGroup)); + pipeline.addLast(BackendHandlerFactory.createBackendHandlerInstance(DatabaseType.MySQL)); } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java index e70c4cd99e314..a40c7cd95f341 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java @@ -32,9 +32,11 @@ * Sharding rule registry. * * @author zhangliang + * @author wangkai */ @Getter public final class ShardingRuleRegistry { + public static final boolean WITHOUT_JDBC = true; private static final ShardingRuleRegistry INSTANCE = new ShardingRuleRegistry(); diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/ShardingProxy.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/ShardingProxy.java index 0d3a621b1a2e0..eae0931752164 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/ShardingProxy.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/ShardingProxy.java @@ -29,13 +29,18 @@ import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; +import io.shardingjdbc.proxy.backend.ShardingProxyClient; +import io.shardingjdbc.proxy.config.ShardingRuleRegistry; import io.shardingjdbc.proxy.frontend.netty.ServerHandlerInitializer; +import java.net.MalformedURLException; + /** * Sharding-Proxy. * * @author zhangliang * @author xiaoyu + * @author wangkai */ public final class ShardingProxy { @@ -53,8 +58,11 @@ public final class ShardingProxy { * @param port port * @throws InterruptedException interrupted exception */ - public void start(final int port) throws InterruptedException { + public void start(final int port) throws InterruptedException, MalformedURLException { try { + if(ShardingRuleRegistry.WITHOUT_JDBC){ + ShardingProxyClient.getInstance().start(); + } ServerBootstrap bootstrap = new ServerBootstrap(); bossGroup = createEventLoopGroup(); if (bossGroup instanceof EpollEventLoopGroup) { @@ -68,6 +76,9 @@ public void start(final int port) throws InterruptedException { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); userGroup.shutdownGracefully(); + if(ShardingRuleRegistry.WITHOUT_JDBC){ + ShardingProxyClient.getInstance().stop(); + } } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java new file mode 100644 index 0000000000000..6e7be4de31cdb --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java @@ -0,0 +1,82 @@ +/* + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingjdbc.proxy.util; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +/** + * sync get multiple netty return. + * + * @author wangkai + */ +public class SynchronizedFuture implements Future> { + + private CountDownLatch latch; + + private List responses; + + private long beginTime = System.currentTimeMillis(); + + public SynchronizedFuture(int resultSize){ + latch = new CountDownLatch(resultSize); + responses = new ArrayList<>(resultSize); + } + + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + return false; + } + @Override + public boolean isCancelled() { + return false; + } + @Override + public boolean isDone() { + if (null != responses && responses.size() > 0) { + return true; + } + return false; + } + + @Override + public List get() throws InterruptedException { + latch.await(); + return this.responses; + } + + @Override + public List get(long timeout, TimeUnit unit) throws InterruptedException { + if (latch.await(timeout, unit)) { + return this.responses; + } + return null; + } + + public void setResponse(T response) { + this.responses.add(response); + latch.countDown(); + } + + public long getBeginTime() { + return beginTime; + } +} \ No newline at end of file From 4cba2b4911daaac75a1d42fed8d0a02b81de835c Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 26 Apr 2018 14:46:09 +0800 Subject: [PATCH 035/351] abstract client --- .../com/saaavsaaa/client/cache/PathNode.java | 17 ++++++++ .../com/saaavsaaa/client/cache/PathTree.java | 12 ++++++ .../client/zookeeper/CacheClient.java | 4 ++ .../{BaseClient.java => Client.java} | 35 ++++++++++++--- .../client/zookeeper/ClientFactory.java | 6 +-- .../saaavsaaa/client/zookeeper/IClient.java | 43 +++++++++++++++++++ .../client/zookeeper/UsualClient.java | 30 ------------- .../client/zookeeper/UsualClientTest.java | 7 ++- 8 files changed, 111 insertions(+), 43 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/cache/PathTree.java rename src/main/java/com/saaavsaaa/client/zookeeper/{BaseClient.java => Client.java} (72%) create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/IClient.java diff --git a/src/main/java/com/saaavsaaa/client/cache/PathNode.java b/src/main/java/com/saaavsaaa/client/cache/PathNode.java index 5e52775182395..3418f11249957 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathNode.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathNode.java @@ -1,7 +1,24 @@ package com.saaavsaaa.client.cache; +import java.util.HashMap; +import java.util.Map; + /** * Created by aaa on 18-4-18. */ public class PathNode { + private final String nodeKey; + private Map children; + + public PathNode(String key) { + this.nodeKey = key; + } + + public Map getChildren() { + return children; + } + + public void setChildren(Map children) { + this.children = children; + } } diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java new file mode 100644 index 0000000000000..94fa434fde5b1 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -0,0 +1,12 @@ +package com.saaavsaaa.client.cache; + +/** + * Created by aaa on 18-4-26. + */ +public class PathTree { + private final PathNode rootNode; + + public PathTree(String root) { + this.rootNode = new PathNode(root); + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index c62fa4e09442c..746b9c7312d0d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -68,6 +68,10 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup private void deleteAllChild(final String key, final Transaction transaction) throws KeeperException, InterruptedException { } + private void deleteOnlyCurrent(final String key, final Transaction transaction) throws KeeperException, InterruptedException { + zooKeeper.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION); + } + @Override public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java similarity index 72% rename from src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java rename to src/main/java/com/saaavsaaa/client/zookeeper/Client.java index 8d53f78cee6bb..80bd1d3ce8955 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -1,10 +1,7 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.untils.*; -import org.apache.zookeeper.WatchedEvent; -import org.apache.zookeeper.Watcher; -import org.apache.zookeeper.ZooDefs; -import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; import java.io.IOException; @@ -18,7 +15,7 @@ /** * Created by aaa on 18-4-19. */ -public abstract class BaseClient { +public abstract class Client implements IClient{ private static final CountDownLatch CONNECTED = new CountDownLatch(1); protected static final Map watchers = new ConcurrentHashMap<>(); private boolean globalListenerRegistered = false; @@ -31,7 +28,7 @@ public abstract class BaseClient { protected boolean rootExist = false; protected List authorities; - protected BaseClient(String servers, int sessionTimeoutMilliseconds) { + protected Client(String servers, int sessionTimeoutMilliseconds) { this.servers = servers; this.sessionTimeOut = sessionTimeoutMilliseconds; } @@ -103,6 +100,32 @@ public void close() throws InterruptedException { zooKeeper.close(); } + void createNamespace() throws KeeperException, InterruptedException { + if (rootExist){ + return; + } + zooKeeper.create(rootNode, Constants.NOTHING_DATA, authorities, CreateMode.PERSISTENT); + rootExist = true; + zooKeeper.exists(rootNode, new Watcher() { + @Override + public void process(WatchedEvent event) { + if (rootNode.equals(event.getPath()) && NodeDeleted.equals(event.getType())){ + rootExist = false; + System.out.println("----------------------------------------------delete root"); + System.out.println(event.getPath()); + System.out.println(event.getState()); + System.out.println(event.getType()); + System.out.println("----------------------------------------------delete root"); + } + } + }); + System.out.println("----------------------------------------------create root"); + } + + void deleteNamespace() throws KeeperException, InterruptedException { + zooKeeper.delete(rootNode, Constants.VERSION); + } + void setRootNode(String rootNode) { this.rootNode = rootNode; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index e55af7198c7cd..46fd889e52308 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -11,7 +11,7 @@ public class ClientFactory { // private static final String CLIENT_EXCLUSIVE_NODE = "ZKC"; - private UsualClient client; + private BaseClient client; private Listener globalListener; private String namespace; private String scheme; @@ -24,7 +24,7 @@ public ClientFactory newCacheClient(final String servers, final int sessionTimeo return this; } - public ClientFactory newClient(final String servers, final int sessionTimeoutMilliseconds) { + public ClientFactory newUsualClient(final String servers, final int sessionTimeoutMilliseconds) { client = new UsualClient(servers, sessionTimeoutMilliseconds); return this; } @@ -34,7 +34,7 @@ public ClientFactory watch(final Listener listener){ return this; } - public synchronized UsualClient start() throws IOException, InterruptedException { + public synchronized BaseClient start() throws IOException, InterruptedException { client.setRootNode(namespace); client.start(); client.setAuthorities(scheme , auth); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/IClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/IClient.java new file mode 100644 index 0000000000000..af6f55a8b3f82 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/IClient.java @@ -0,0 +1,43 @@ +package com.saaavsaaa.client.zookeeper; + +import com.saaavsaaa.client.untils.Listener; +import org.apache.zookeeper.*; + +import java.util.List; + +/** + * Created by aaa on 18-4-26. + */ +public interface IClient { + String getDataString(final String key) throws KeeperException, InterruptedException ; + byte[] getData(final String key) throws KeeperException, InterruptedException ; + void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException; + boolean checkExists(final String key) throws KeeperException, InterruptedException ; + boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException ; + List getChildren(final String key) throws KeeperException, InterruptedException ; + void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException ; + void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException ; + void update(final String key, final String value) throws KeeperException, InterruptedException ; + void updateWithCheck(final String key, final String value) throws KeeperException, InterruptedException ; + void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException ; + void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException ; + void deleteAllChildren(final String key) throws KeeperException, InterruptedException ; + + /* + * delete the current node with force and delete the super node whose only child node is current node recursively + */ + void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException ; + + /* + void createNamespace(); + void deleteNamespace(); + Watcher registerWatch(String key, Listener listener); + void unregisterWatch(String key); + void close(); + Watcher registerWatch(Listener listener); + void setRootNode(String namespace); + void start(); + void setAuthorities(String scheme, byte[] auth); + ZooKeeper getZooKeeper(); + */ +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 143e4085b91c9..ac66f2f45c343 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -23,32 +23,6 @@ public class UsualClient extends BaseClient { UsualClient(String servers, int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); } - - public void createNamespace() throws KeeperException, InterruptedException { - if (rootExist){ - return; - } - zooKeeper.create(rootNode, new byte[0], authorities, CreateMode.PERSISTENT); - rootExist = true; - zooKeeper.exists(rootNode, new Watcher() { - @Override - public void process(WatchedEvent event) { - if (rootNode.equals(event.getPath()) && NodeDeleted.equals(event.getType())){ - rootExist = false; - System.out.println("----------------------------------------------delete root"); - System.out.println(event.getPath()); - System.out.println(event.getState()); - System.out.println(event.getType()); - System.out.println("----------------------------------------------delete root"); - } - } - }); - System.out.println("----------------------------------------------create root"); - } - - public void deleteNamespace() throws KeeperException, InterruptedException { - zooKeeper.delete(rootNode, Constants.VERSION); - } public String getDataString(final String key) throws KeeperException, InterruptedException { return new String(getData(key)); @@ -133,10 +107,6 @@ public void deleteOnlyCurrent(final String key) throws KeeperException, Interrup System.out.println("delete : " + PathUtil.getRealPath(rootNode, key)); } - private void deleteOnlyCurrent(final String key, final Transaction transaction) throws KeeperException, InterruptedException { - zooKeeper.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION); - } - public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { zooKeeper.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION, callback, ctx); } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index da9ca10bbb701..f905800d1e5a4 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -27,19 +27,19 @@ public class UsualClientTest { private static final String ROOT = "test"; private static final String AUTH = "digest"; - private UsualClient client = null; + private BaseClient client = null; // @Before public void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); - client = creator.setNamespace(ROOT).authorization(AUTH, AUTH.getBytes()).newClient(SERVERS, SESSION_TIMEOUT).start(); + client = creator.setNamespace(ROOT).authorization(AUTH, AUTH.getBytes()).newUsualClient(SERVERS, SESSION_TIMEOUT).start(); } @Before public void startWithWatch() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); Listener listener = buildListener(); - client = creator.setNamespace(ROOT).authorization(AUTH, AUTH.getBytes()).newClient(SERVERS, SESSION_TIMEOUT).watch(listener).start(); + client = creator.setNamespace(ROOT).authorization(AUTH, AUTH.getBytes()).newUsualClient(SERVERS, SESSION_TIMEOUT).watch(listener).start(); } private Listener buildListener(){ @@ -235,7 +235,6 @@ public void watch() throws KeeperException, InterruptedException { String key = "a"; Watcher watcher = client.registerWatch(key, listener); - client.createNamespace(); client.createCurrentOnly(key, "aaa", CreateMode.EPHEMERAL); client.checkExists(key, watcher); client.updateWithCheck(key, "value"); From 8be595d8a66c4f076e6ba487876a10010836e8b0 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 26 Apr 2018 15:01:47 +0800 Subject: [PATCH 036/351] abstract client --- .../java/com/saaavsaaa/client/zookeeper/ClientFactory.java | 4 ++-- src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java | 2 +- .../java/com/saaavsaaa/client/zookeeper/UsualClientTest.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 46fd889e52308..a3bb3f14956c0 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -11,7 +11,7 @@ public class ClientFactory { // private static final String CLIENT_EXCLUSIVE_NODE = "ZKC"; - private BaseClient client; + private Client client; private Listener globalListener; private String namespace; private String scheme; @@ -34,7 +34,7 @@ public ClientFactory watch(final Listener listener){ return this; } - public synchronized BaseClient start() throws IOException, InterruptedException { + public synchronized Client start() throws IOException, InterruptedException { client.setRootNode(namespace); client.start(); client.setAuthorities(scheme , auth); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index ac66f2f45c343..8a976fb308a5c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -17,7 +17,7 @@ * todo Sequential * todo org.apache.zookeeper.KeeperException$NodeExistsException: KeeperErrorCode = NodeExists */ -public class UsualClient extends BaseClient { +public class UsualClient extends Client { private final boolean watched = true; //false UsualClient(String servers, int sessionTimeoutMilliseconds) { diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index f905800d1e5a4..62c248dbd3771 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -27,7 +27,7 @@ public class UsualClientTest { private static final String ROOT = "test"; private static final String AUTH = "digest"; - private BaseClient client = null; + private Client client = null; // @Before public void start() throws IOException, InterruptedException { From b2ed89e233524eaac9a5d586d2fcafe115bb05af Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 26 Apr 2018 15:12:15 +0800 Subject: [PATCH 037/351] cache begin --- .../client/zookeeper/CacheClientTest.java | 29 +++++++++ .../client/zookeeper/TestSupport.java | 28 +++++++++ .../client/zookeeper/UsualClientTest.java | 60 +++++++------------ 3 files changed, 78 insertions(+), 39 deletions(-) create mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java index 0876640b0bd29..179bbcbba041f 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java @@ -1,7 +1,36 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.untils.Constants; +import com.saaavsaaa.client.untils.Listener; +import org.apache.zookeeper.KeeperException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; + /** * Created by aaa on 18-4-20. */ public class CacheClientTest { + private Client client = null; + + // @Before + public void start() throws IOException, InterruptedException { + ClientFactory creator = new ClientFactory(); + client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); + } + + @Before + public void startWithWatch() throws IOException, InterruptedException { + ClientFactory creator = new ClientFactory(); + Listener listener = TestSupport.buildListener(); + client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + } + + @After + public void stop() throws InterruptedException { + client.close(); + client = null; + } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java b/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java new file mode 100644 index 0000000000000..b8179774b7120 --- /dev/null +++ b/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java @@ -0,0 +1,28 @@ +package com.saaavsaaa.client.zookeeper; + +import com.saaavsaaa.client.untils.Listener; +import org.apache.zookeeper.WatchedEvent; + +/** + * Created by aaa on 18-4-26. + */ +public class TestSupport { + static final String AUTH = "digest"; + static final String SERVERS = "192.168.2.44:2181"; + static final int SESSION_TIMEOUT = 200000;//ms + static final String ROOT = "test"; + + static Listener buildListener(){ + Listener listener = new Listener() { + @Override + public void process(WatchedEvent event) { + System.out.println("=========================================================="); + System.out.println(event.getPath()); + System.out.println(event.getState()); + System.out.println(event.getType()); + System.out.println("=========================================================="); + } + }; + return listener; + } +} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index 62c248dbd3771..60e96878de80c 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -22,38 +22,20 @@ * Created by aaa on 18-4-18. */ public class UsualClientTest { - private static final String SERVERS = "192.168.2.44:2181"; - private static final int SESSION_TIMEOUT = 200000;//ms - private static final String ROOT = "test"; - private static final String AUTH = "digest"; private Client client = null; // @Before public void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); - client = creator.setNamespace(ROOT).authorization(AUTH, AUTH.getBytes()).newUsualClient(SERVERS, SESSION_TIMEOUT).start(); + client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); } @Before public void startWithWatch() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); - Listener listener = buildListener(); - client = creator.setNamespace(ROOT).authorization(AUTH, AUTH.getBytes()).newUsualClient(SERVERS, SESSION_TIMEOUT).watch(listener).start(); - } - - private Listener buildListener(){ - Listener listener = new Listener() { - @Override - public void process(WatchedEvent event) { - System.out.println("=========================================================="); - System.out.println(event.getPath()); - System.out.println(event.getState()); - System.out.println(event.getType()); - System.out.println("=========================================================="); - } - }; - return listener; + Listener listener = TestSupport.buildListener(); + client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); } @After @@ -65,44 +47,44 @@ public void stop() throws InterruptedException { @Test public void createRoot() throws KeeperException, InterruptedException { client.createNamespace(); - assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + ROOT, false) != null; + assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) != null; client.deleteNamespace(); - assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + ROOT, false) == null; + assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; } @Ignore @Test public void deleteRoot() throws KeeperException, InterruptedException { client.deleteNamespace(); - assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + ROOT, false) == null; + assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; } @Test public void createChild() throws KeeperException, InterruptedException { String key = "a/b/bb"; client.createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); - assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key)/*"/" + ROOT + "/" + key*/, false) != null; + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) != null; client.deleteCurrentBranch(key); - assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key)/*"/" + ROOT + "/" + key*/, false) == null; + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) == null; } @Test public void deleteBranch() throws KeeperException, InterruptedException { String keyB = "a/b/bb"; client.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); - assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, keyB), false) != null; + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; String keyC = "a/c/cc"; client.createAllNeedPath(keyC, "ccc11", CreateMode.PERSISTENT); - assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, keyC), false) != null; + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; client.deleteCurrentBranch(keyC); - assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, keyC), false) == null; - assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, "a"), false) != null; + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) == null; + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), false) != null; client.deleteCurrentBranch(keyB); - assert client.getZooKeeper().exists(PathUtil.checkPath(ROOT), false) == null; + assert client.getZooKeeper().exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; client.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); - assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, keyB), false) != null; + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; client.deleteCurrentBranch(keyB); - assert client.getZooKeeper().exists(PathUtil.checkPath(ROOT), false) == null; + assert client.getZooKeeper().exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; } @Test @@ -196,14 +178,14 @@ public void persistEphemeral() throws KeeperException, InterruptedException { client.createAllNeedPath(key, value, CreateMode.PERSISTENT); // assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), null).getEphemeralOwner() == 0; Stat stat = new Stat(); - client.getZooKeeper().getData(PathUtil.getRealPath(ROOT, key), false, stat); + client.getZooKeeper().getData(PathUtil.getRealPath(TestSupport.ROOT, key), false, stat); assert stat.getEphemeralOwner() == 0; client.deleteAllChildren(key); assert !isExisted(key); client.createAllNeedPath(key, value, CreateMode.EPHEMERAL); - assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), null).getEphemeralOwner() != 0; // Ephemeral node connection session id + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key), null).getEphemeralOwner() != 0; // Ephemeral node connection session id client.deleteCurrentBranch(key); } @@ -213,11 +195,11 @@ public void delAllChildren() throws KeeperException, InterruptedException { client.createAllNeedPath(key, "bb", CreateMode.PERSISTENT); key = "a/c/cc"; client.createAllNeedPath(key, "cc", CreateMode.PERSISTENT); - System.out.println(client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, "a"), null).getNumChildren()); // nearest children count - assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), false) != null; + System.out.println(client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), null).getNumChildren()); // nearest children count + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) != null; client.deleteAllChildren("a"); - assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), false) == null; - assert client.getZooKeeper().exists("/" + ROOT, false) != null; + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) == null; + assert client.getZooKeeper().exists("/" + TestSupport.ROOT, false) != null; client.deleteNamespace(); } From 4b1f39a1f8ca08e3c7f6f67811a4d66f35938e73 Mon Sep 17 00:00:00 2001 From: WangKai Date: Thu, 26 Apr 2018 16:43:59 +0800 Subject: [PATCH 038/351] replace jdbc to netty --- .../proxy/backend/ShardingProxyClient.java | 4 +- .../common/SQLExecuteBackendHandler.java | 11 ++- .../common/SQLPacketsBackendHandler.java | 92 +++++++++++++++++++ .../backend/mysql/MySQLBackendHandler.java | 13 ++- .../text/fieldlist/ComFieldListPacket.java | 11 ++- .../command/text/query/ComQueryPacket.java | 8 +- .../proxy/util/MySQLResultCache.java | 65 +++++++++++++ .../proxy/util/SynchronizedFuture.java | 16 +++- 8 files changed, 202 insertions(+), 18 deletions(-) create mode 100644 sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java create mode 100644 sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java index 0782af8868dab..3316880c95f1e 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java @@ -17,6 +17,7 @@ package io.shardingjdbc.proxy.backend; +import com.google.common.collect.Maps; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.Channel; @@ -54,12 +55,13 @@ public final class ShardingProxyClient { private EventLoopGroup workerGroup; @Getter - private Map channelMap = new HashMap<>(); + private Map channelMap = Maps.newHashMap(); /** * Start Sharding-Proxy. * * @throws InterruptedException interrupted exception + * @throws MalformedURLException url is illegal. */ public void start() throws InterruptedException, MalformedURLException { Bootstrap bootstrap = new Bootstrap(); diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java index b81b5af3d7e02..0961714c0f399 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java @@ -55,12 +55,13 @@ * SQL execute backend handler. * * @author zhangliang + * @author wangkai */ -public final class SQLExecuteBackendHandler implements BackendHandler { +public class SQLExecuteBackendHandler implements BackendHandler { - private final String sql; + protected final String sql; - private final StatementRoutingEngine routingEngine; + protected final StatementRoutingEngine routingEngine; public SQLExecuteBackendHandler(final String sql, final DatabaseType databaseType, final boolean showSQL) { this.sql = sql; @@ -81,7 +82,7 @@ public CommandResponsePackets execute() { return merge(routeResult.getSqlStatement(), result); } - private CommandResponsePackets execute(final SQLStatement sqlStatement, final SQLExecutionUnit sqlExecutionUnit) { + protected CommandResponsePackets execute(final SQLStatement sqlStatement, final SQLExecutionUnit sqlExecutionUnit) { switch (sqlStatement.getType()) { case DQL: return executeQuery(ShardingRuleRegistry.getInstance().getDataSourceMap().get(sqlExecutionUnit.getDataSource()), sqlExecutionUnit.getSql()); @@ -173,7 +174,7 @@ private long getGeneratedKey(final Statement statement) throws SQLException { return result; } - private CommandResponsePackets merge(final SQLStatement sqlStatement, final List packets) { + protected CommandResponsePackets merge(final SQLStatement sqlStatement, final List packets) { if (1 == packets.size()) { return packets.iterator().next(); } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java new file mode 100644 index 0000000000000..a9a1ee5db7078 --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java @@ -0,0 +1,92 @@ +/* + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingjdbc.proxy.backend.common; + +import com.sun.xml.internal.bind.v2.TODO; +import io.netty.channel.Channel; +import io.shardingjdbc.core.constant.DatabaseType; +import io.shardingjdbc.core.parsing.parser.sql.SQLStatement; +import io.shardingjdbc.core.routing.SQLExecutionUnit; +import io.shardingjdbc.core.routing.SQLRouteResult; +import io.shardingjdbc.proxy.backend.ShardingProxyClient; +import io.shardingjdbc.proxy.transport.mysql.constant.StatusFlag; +import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; +import io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket; +import io.shardingjdbc.proxy.util.MySQLResultCache; +import io.shardingjdbc.proxy.util.SynchronizedFuture; + +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * SQL packets backend handler. + * + * @author wangkai + */ +public final class SQLPacketsBackendHandler extends SQLExecuteBackendHandler { + private SynchronizedFuture synchronizedFuture; + + public SQLPacketsBackendHandler(final String sql, final DatabaseType databaseType, final boolean showSQL) { + super(sql, databaseType, showSQL); + } + + @Override + public CommandResponsePackets execute() { + SQLRouteResult routeResult = routingEngine.route(sql); + //TODO sequenceId should be set. + int sequenceId = 0; + if (routeResult.getExecutionUnits().isEmpty()) { + return new CommandResponsePackets(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); + } + synchronizedFuture = new SynchronizedFuture<>(routeResult.getExecutionUnits().size()); + MySQLResultCache.getInstance().put(sequenceId,synchronizedFuture); + for (SQLExecutionUnit each : routeResult.getExecutionUnits()) { + execute(routeResult.getSqlStatement(), each); + } + //TODO timeout will be set. + List result = synchronizedFuture.get(30, TimeUnit.SECONDS); + MySQLResultCache.getInstance().delete(sequenceId); + return merge(routeResult.getSqlStatement(), result); + } + + @Override + protected CommandResponsePackets execute(final SQLStatement sqlStatement, final SQLExecutionUnit sqlExecutionUnit) { + switch (sqlStatement.getType()) { + case DQL: + executeQuery(ShardingProxyClient.getInstance().getChannelMap().get(sqlExecutionUnit.getDataSource()), sqlExecutionUnit.getSql()); + case DML: + case DDL: + executeUpdate(ShardingProxyClient.getInstance().getChannelMap().get(sqlExecutionUnit.getDataSource()), sqlExecutionUnit.getSql(), sqlStatement); + default: + executeCommon(ShardingProxyClient.getInstance().getChannelMap().get(sqlExecutionUnit.getDataSource()), sqlExecutionUnit.getSql()); + } + return null; + } + //TODO + private void executeQuery(final Channel channel, final String sql){ + channel.writeAndFlush(""); + + } + //TODO + private void executeUpdate(final Channel channel, final String sql, final SQLStatement sqlStatement){ + } + //TODO + private void executeCommon(final Channel channel, final String sql){ + } +} diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java index bf378b43fb198..40dd907945cef 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java @@ -22,7 +22,10 @@ import io.netty.channel.ChannelInboundHandlerAdapter; import io.shardingjdbc.proxy.backend.common.BackendHandler; import io.shardingjdbc.proxy.backend.common.CommandResponsePacketsHandler; +import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload; +import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandPacketFactory; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; +import io.shardingjdbc.proxy.util.MySQLResultCache; /** * Backend handler. @@ -33,22 +36,24 @@ public class MySQLBackendHandler extends CommandResponsePacketsHandler { @Override public void channelRead(final ChannelHandlerContext context, final Object message) { - //TODO 判断报文是握手还是 + //TODO 判断报文是握手还是OKPacket还是结果集 if (true) { auth(context,(ByteBuf) message); } else { } } - + //TODO @Override protected void auth(ChannelHandlerContext context, ByteBuf message) { } - + //TODO @Override protected void executeCommandResponsePackets(ChannelHandlerContext context, ByteBuf message) { - + MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload(message); + int sequenceId = mysqlPacketPayload.readInt1(); + MySQLResultCache.getInstance().get(sequenceId).setResponse(null); } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java index 259565eed1505..2d83061f3abc6 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java @@ -20,6 +20,8 @@ import io.shardingjdbc.core.constant.DatabaseType; import io.shardingjdbc.core.constant.ShardingConstant; import io.shardingjdbc.proxy.backend.common.SQLExecuteBackendHandler; +import io.shardingjdbc.proxy.backend.common.SQLPacketsBackendHandler; +import io.shardingjdbc.proxy.config.ShardingRuleRegistry; import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandPacket; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandPacketType; @@ -28,9 +30,10 @@ /** * COM_FIELD_LIST command packet. - * @see COM_FIELD_LIST * * @author zhangliang + * @author wangkai + * @see COM_FIELD_LIST */ @Slf4j public final class ComFieldListPacket extends CommandPacket { @@ -58,6 +61,10 @@ public CommandResponsePackets execute() { log.debug("field wildcard received for Sharding-Proxy: {}", fieldWildcard); String sql = String.format("SHOW COLUMNS FROM %s FROM %s", table, ShardingConstant.LOGIC_SCHEMA_NAME); // TODO use common database type - return new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, true).execute(); + if (ShardingRuleRegistry.WITHOUT_JDBC) { + return new SQLPacketsBackendHandler(sql, DatabaseType.MySQL, true).execute(); + }else { + return new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, true).execute(); + } } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java index eae292de4e961..e7011bd1be456 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java @@ -19,6 +19,8 @@ import io.shardingjdbc.core.constant.DatabaseType; import io.shardingjdbc.proxy.backend.common.SQLExecuteBackendHandler; +import io.shardingjdbc.proxy.backend.common.SQLPacketsBackendHandler; +import io.shardingjdbc.proxy.config.ShardingRuleRegistry; import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandPacket; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; @@ -48,6 +50,10 @@ public void write(final MySQLPacketPayload mysqlPacketPayload) { @Override public CommandResponsePackets execute() { log.debug("COM_QUERY received for Sharding-Proxy: {}", sql); - return new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, true).execute(); + if (ShardingRuleRegistry.WITHOUT_JDBC) { + return new SQLPacketsBackendHandler(sql, DatabaseType.MySQL, true).execute(); + }else { + return new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, true).execute(); + } } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java new file mode 100644 index 0000000000000..c703c4aced90c --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java @@ -0,0 +1,65 @@ +/* + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingjdbc.proxy.util; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import io.netty.channel.Channel; +import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; + +import java.util.concurrent.TimeUnit; + +/** + * cache for SynchronizedFuture. + * + * @author wangkai + */ +public class MySQLResultCache { + private static final MySQLResultCache INSTANCE = new MySQLResultCache(); + //TODO expire time will be set. + private Cache cache = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.SECONDS).build(); + + /** + * @param sequenceId send mysql server transaction id. + * @param synchronizedFuture multiple result set. + */ + public void put(int sequenceId, SynchronizedFuture synchronizedFuture){ + cache.put(sequenceId + "", synchronizedFuture); + } + + /** + * @param sequenceId send mysql server transaction id. + * @return multiple result set. + */ + public SynchronizedFuture get(int sequenceId){ + return cache.getIfPresent(sequenceId + ""); + } + + public void delete(int sequenceId){ + cache.invalidate(sequenceId + ""); + } + + /** + * Get instance of MySQLResultCache. + * + * @return instance of MySQLResultCache + */ + public static MySQLResultCache getInstance() { + return INSTANCE; + } +} diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java index 6e7be4de31cdb..a07fc9dc91164 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java @@ -17,6 +17,8 @@ package io.shardingjdbc.proxy.util; +import com.google.common.collect.Lists; + import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; @@ -38,7 +40,7 @@ public class SynchronizedFuture implements Future> { public SynchronizedFuture(int resultSize){ latch = new CountDownLatch(resultSize); - responses = new ArrayList<>(resultSize); + responses = Lists.newArrayListWithCapacity(resultSize); } @Override @@ -64,11 +66,15 @@ public List get() throws InterruptedException { } @Override - public List get(long timeout, TimeUnit unit) throws InterruptedException { - if (latch.await(timeout, unit)) { - return this.responses; + public List get(long timeout, TimeUnit unit) { + try { + if (latch.await(timeout, unit)) { + return this.responses; + } + } catch (InterruptedException e) { + //TODO } - return null; + return this.responses; } public void setResponse(T response) { From 26c856bed44b5e7171bf8a13d13464de46407e21 Mon Sep 17 00:00:00 2001 From: WangKai Date: Thu, 26 Apr 2018 16:47:06 +0800 Subject: [PATCH 039/351] replace jdbc to netty --- .../proxy/backend/common/SQLPacketsBackendHandler.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java index a9a1ee5db7078..f9533081aa659 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java @@ -17,7 +17,6 @@ package io.shardingjdbc.proxy.backend.common; -import com.sun.xml.internal.bind.v2.TODO; import io.netty.channel.Channel; import io.shardingjdbc.core.constant.DatabaseType; import io.shardingjdbc.core.parsing.parser.sql.SQLStatement; @@ -30,7 +29,6 @@ import io.shardingjdbc.proxy.util.MySQLResultCache; import io.shardingjdbc.proxy.util.SynchronizedFuture; -import java.util.LinkedList; import java.util.List; import java.util.concurrent.TimeUnit; From f13c1b9dc489917f12be6f6f905631e6f75e4769 Mon Sep 17 00:00:00 2001 From: WangKai Date: Thu, 26 Apr 2018 16:57:26 +0800 Subject: [PATCH 040/351] fix some code style --- .../backend/common/SQLExecuteBackendHandler.java | 4 ++-- .../backend/common/SQLPacketsBackendHandler.java | 11 +++++++---- .../proxy/backend/mysql/MySQLBackendHandler.java | 10 ++++------ .../io/shardingjdbc/proxy/util/MySQLResultCache.java | 1 - .../shardingjdbc/proxy/util/SynchronizedFuture.java | 1 - 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java index 0961714c0f399..314fa03d119f6 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java @@ -29,13 +29,13 @@ import io.shardingjdbc.core.routing.StatementRoutingEngine; import io.shardingjdbc.proxy.backend.mysql.MySQLPacketQueryResult; import io.shardingjdbc.proxy.config.ShardingRuleRegistry; +import io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingjdbc.proxy.transport.mysql.constant.ColumnType; import io.shardingjdbc.proxy.transport.mysql.constant.StatusFlag; -import io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; +import io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet; import io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.FieldCountPacket; import io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket; -import io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet; import io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket; import io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket; import io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket; diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java index f9533081aa659..3761c7d6abccf 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java @@ -53,7 +53,7 @@ public CommandResponsePackets execute() { return new CommandResponsePackets(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); } synchronizedFuture = new SynchronizedFuture<>(routeResult.getExecutionUnits().size()); - MySQLResultCache.getInstance().put(sequenceId,synchronizedFuture); + MySQLResultCache.getInstance().put(sequenceId, synchronizedFuture); for (SQLExecutionUnit each : routeResult.getExecutionUnits()) { execute(routeResult.getSqlStatement(), each); } @@ -76,15 +76,18 @@ protected CommandResponsePackets execute(final SQLStatement sqlStatement, final } return null; } + //TODO - private void executeQuery(final Channel channel, final String sql){ + private void executeQuery(final Channel channel, final String sql) { channel.writeAndFlush(""); } + //TODO - private void executeUpdate(final Channel channel, final String sql, final SQLStatement sqlStatement){ + private void executeUpdate(final Channel channel, final String sql, final SQLStatement sqlStatement) { } + //TODO - private void executeCommon(final Channel channel, final String sql){ + private void executeCommon(final Channel channel, final String sql) { } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java index 40dd907945cef..fde67ad7dccda 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java @@ -19,12 +19,8 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelInboundHandlerAdapter; -import io.shardingjdbc.proxy.backend.common.BackendHandler; import io.shardingjdbc.proxy.backend.common.CommandResponsePacketsHandler; import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload; -import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandPacketFactory; -import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; import io.shardingjdbc.proxy.util.MySQLResultCache; /** @@ -38,16 +34,18 @@ public class MySQLBackendHandler extends CommandResponsePacketsHandler { public void channelRead(final ChannelHandlerContext context, final Object message) { //TODO 判断报文是握手还是OKPacket还是结果集 if (true) { - auth(context,(ByteBuf) message); + auth(context, (ByteBuf) message); } else { } } + //TODO @Override protected void auth(ChannelHandlerContext context, ByteBuf message) { } + //TODO @Override protected void executeCommandResponsePackets(ChannelHandlerContext context, ByteBuf message) { @@ -55,5 +53,5 @@ protected void executeCommandResponsePackets(ChannelHandlerContext context, Byte int sequenceId = mysqlPacketPayload.readInt1(); MySQLResultCache.getInstance().get(sequenceId).setResponse(null); } - + } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java index c703c4aced90c..a94a8961f80d5 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java @@ -19,7 +19,6 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; -import io.netty.channel.Channel; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; import java.util.concurrent.TimeUnit; diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java index a07fc9dc91164..3d4e556df8aa3 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java @@ -19,7 +19,6 @@ import com.google.common.collect.Lists; -import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Future; From 69cd67729ba120d25ba1257656fc1679320f967a Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 26 Apr 2018 17:50:26 +0800 Subject: [PATCH 041/351] cache begin --- .../client/zookeeper/CacheClient.java | 10 +- .../client/zookeeper/UsualClient.java | 8 +- .../client/zookeeper/BaseClientTest.java | 36 ++++++ .../client/zookeeper/ClientsTest.java | 47 +++++++ .../client/zookeeper/UsualClientTest.java | 115 +----------------- 5 files changed, 98 insertions(+), 118 deletions(-) create mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java create mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 746b9c7312d0d..19f460bbbe1e6 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -1,12 +1,13 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.cache.PathTree; import com.saaavsaaa.client.untils.Constants; import com.saaavsaaa.client.untils.PathUtil; -import com.saaavsaaa.client.untils.StringUtil; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.Transaction; +import java.io.IOException; import java.util.List; import java.util.Stack; @@ -14,10 +15,17 @@ * Created by aaa on 18-4-19. */ public final class CacheClient extends UsualClient { + PathTree pathTree = null; + CacheClient(String servers, int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); } + @Override + public void start() throws IOException, InterruptedException { + super.start(); + pathTree = new PathTree(rootNode); + } /* * closed beta diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 8a976fb308a5c..562b248574272 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -5,13 +5,13 @@ import com.saaavsaaa.client.untils.Constants; import com.saaavsaaa.client.untils.PathUtil; -import com.saaavsaaa.client.untils.StringUtil; -import org.apache.zookeeper.*; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; import java.util.List; -import static org.apache.zookeeper.Watcher.Event.EventType.NodeDeleted; - /* * cache * todo Sequential diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java new file mode 100644 index 0000000000000..1ca3addfd8f57 --- /dev/null +++ b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java @@ -0,0 +1,36 @@ +package com.saaavsaaa.client.zookeeper; + +import com.saaavsaaa.client.untils.Listener; +import org.junit.After; +import org.junit.Before; + +import java.io.IOException; + +/** + * Created by aaa on 18-4-26. + */ +public class BaseClientTest { + protected Client client = null; + + @Before + public void start() throws IOException, InterruptedException { + ClientFactory creator = new ClientFactory(); +// client = createClient(creator); + client = createWatchClient(creator); + } + + protected Client createClient(ClientFactory creator) throws IOException, InterruptedException { + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); + } + + protected Client createWatchClient(ClientFactory creator) throws IOException, InterruptedException { + Listener listener = TestSupport.buildListener(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + } + + @After + public void stop() throws InterruptedException { + client.close(); + client = null; + } +} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java new file mode 100644 index 0000000000000..3e9051738fbef --- /dev/null +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java @@ -0,0 +1,47 @@ +package com.saaavsaaa.client.zookeeper; + +import com.saaavsaaa.client.untils.Constants; +import com.saaavsaaa.client.untils.Listener; +import org.apache.zookeeper.KeeperException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by aaa on 18-4-26. + */ +public class ClientsTest extends BaseClientTest { + private List clients; + + @Before + @Override + public void start() throws IOException, InterruptedException { + clients = new ArrayList<>(2); + ClientFactory creator = new ClientFactory(); + clients.add(createClient(creator)); + clients.add(createWatchClient(creator)); + } + + @After + @Override + public void stop() throws InterruptedException { + for (Client client : clients) { + client.close(); + } + clients = null; + } + + @Test + public void createRoot() throws KeeperException, InterruptedException { + for (Client client : clients) { + client.createNamespace(); + assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) != null; + client.deleteNamespace(); + assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; + } + } +} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index 60e96878de80c..3e5e090b15d0e 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -3,7 +3,6 @@ import com.saaavsaaa.client.untils.Constants; import com.saaavsaaa.client.untils.Listener; import com.saaavsaaa.client.untils.PathUtil; -import com.saaavsaaa.client.untils.StringUtil; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import org.junit.After; @@ -19,30 +18,9 @@ import java.util.concurrent.CountDownLatch; /** - * Created by aaa on 18-4-18. + * Created by aaa */ -public class UsualClientTest { - - private Client client = null; - -// @Before - public void start() throws IOException, InterruptedException { - ClientFactory creator = new ClientFactory(); - client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); - } - - @Before - public void startWithWatch() throws IOException, InterruptedException { - ClientFactory creator = new ClientFactory(); - Listener listener = TestSupport.buildListener(); - client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - } - - @After - public void stop() throws InterruptedException { - client.close(); - client = null; - } +public class UsualClientTest extends BaseClientTest { @Test public void createRoot() throws KeeperException, InterruptedException { @@ -272,98 +250,9 @@ public void process(WatchedEvent event) { return listener; } - private Listener buildEventListener(List actual){ - EventListener eventListener = new EventListener() { - @Override - public void onChange(DataChangedEvent event) { - System.out.println("=========================================================="); - System.out.println(event.getKey()); - System.out.println(event.getValue()); - System.out.println(event.getEventType()); - System.out.println("=========================================================="); - } - }; - Listener listener = new Listener() { - @Override - public void process(WatchedEvent event) { - byte[] data = new byte[0]; - try { - data = client.getZooKeeper().getData(event.getPath(),false, null); - } catch (KeeperException e) { - if (e instanceof KeeperException.NoNodeException){ - System.out.println(event.getType() +" : "+ e.getMessage()); - } else { - e.printStackTrace(); - } - } catch (InterruptedException e) { - e.printStackTrace(); - } - String result = null == data ? null : new String(data, Constants.UTF_8); - eventListener.onChange(new DataChangedEvent(getEventType(event, result), event.getPath(), result)); - } - - private DataChangedEvent.Type getEventType(final WatchedEvent event, final String result) { - switch (event.getType()) { - case NodeDataChanged: - case NodeChildrenChanged: { - actual.add(new StringBuilder().append("update_").append(event.getPath()).append("_").append(result).toString()); - return DataChangedEvent.Type.UPDATED; - } - case NodeDeleted: { - actual.add(new StringBuilder().append("delete_").append(event.getPath()).append("_").append(result).toString()); - return DataChangedEvent.Type.DELETED; - } - default: - actual.add(new StringBuilder().append("ignore_").append(event.getPath()).append("_").append(result).toString()); - return DataChangedEvent.Type.IGNORED; - } - } - }; - return listener; - } - @Test public void close() throws Exception { client.close(); assert client.getZooKeeper().getState() == ZooKeeper.States.CLOSED; } -} - -interface EventListener { - void onChange(DataChangedEvent event); -} - -class DataChangedEvent { - - public Type getEventType() { - return eventType; - } - - public String getKey() { - return key; - } - - public String getValue() { - return value; - } - - public DataChangedEvent(Type eventType, String key, String value) { - this.eventType = eventType; - this.key = key; - this.value = value; - } - - private final Type eventType; - - private final String key; - - private final String value; - - /** - * Data changed event type. - */ - public enum Type { - - UPDATED, DELETED, IGNORED - } } \ No newline at end of file From ff8a60ea68ee8c9f161691b2aab12dbec357bca5 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 26 Apr 2018 18:05:38 +0800 Subject: [PATCH 042/351] test --- .../client/zookeeper/BaseClientTest.java | 240 +++++++++++++++++- .../client/zookeeper/UsualClientTest.java | 198 +-------------- 2 files changed, 248 insertions(+), 190 deletions(-) diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java index 1ca3addfd8f57..d0850af1253b0 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java @@ -1,22 +1,33 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.untils.Constants; import com.saaavsaaa.client.untils.Listener; +import com.saaavsaaa.client.untils.PathUtil; +import org.apache.zookeeper.*; +import org.apache.zookeeper.data.Stat; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.concurrent.CountDownLatch; /** * Created by aaa on 18-4-26. */ public class BaseClientTest { - protected Client client = null; + protected Client testClient = null; @Before public void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); -// client = createClient(creator); - client = createWatchClient(creator); + testClient = createClient(creator); +// testClient = createWatchClient(creator); } protected Client createClient(ClientFactory creator) throws IOException, InterruptedException { @@ -30,7 +41,228 @@ protected Client createWatchClient(ClientFactory creator) throws IOException, In @After public void stop() throws InterruptedException { + testClient.close(); + testClient = null; + } + + @Ignore + @Test + public void deleteRoot() throws KeeperException, InterruptedException { + testClient.deleteNamespace(); + assert testClient.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; + } + + protected void createRoot(Client client) throws KeeperException, InterruptedException { + client.createNamespace(); + assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) != null; + client.deleteNamespace(); + assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; + } + + protected void createChild(Client client) throws KeeperException, InterruptedException { + String key = "a/b/bb"; + client.createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) != null; + client.deleteCurrentBranch(key); + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) == null; + } + + protected void deleteBranch(Client client) throws KeeperException, InterruptedException { + String keyB = "a/b/bb"; + client.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; + String keyC = "a/c/cc"; + client.createAllNeedPath(keyC, "ccc11", CreateMode.PERSISTENT); + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; + client.deleteCurrentBranch(keyC); + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) == null; + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), false) != null; + client.deleteCurrentBranch(keyB); + assert client.getZooKeeper().exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; + client.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; + client.deleteCurrentBranch(keyB); + assert client.getZooKeeper().exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; + } + + protected void isExisted(Client client) throws KeeperException, InterruptedException { + String key = "a/b/bb"; + client.createAllNeedPath(key, "", CreateMode.PERSISTENT); + assert isExisted(key, client); + client.deleteCurrentBranch(key); + } + + protected void get(Client client) throws KeeperException, InterruptedException { + client.createAllNeedPath("a/b", "bbb11", CreateMode.PERSISTENT); + String key = "a"; + // TODO: cache + assert getDirectly(key, client).equals(""); + key = "a/b"; + assert getDirectly(key, client).equals("bbb11"); + client.deleteCurrentBranch("a/b"); + } + + protected void asynGet(Client client) throws KeeperException, InterruptedException { + CountDownLatch ready = new CountDownLatch(1); + String key = "a/b"; + String value = "bbb11"; + client.createAllNeedPath(key, value, CreateMode.PERSISTENT); + AsyncCallback.DataCallback callback = new AsyncCallback.DataCallback() { + @Override + public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) { + String result = new String(data); + System.out.println(new StringBuffer().append("rc:").append(rc).append(",path:").append(path).append(",ctx:").append(ctx).append(",stat:").append(stat)); + assert result.equals(ctx); + ready.countDown(); + } + }; + client.getData(key, callback, value); + ready.await(); + client.deleteCurrentBranch("a/b"); + } + + private String getDirectly(String key, Client client) throws KeeperException, InterruptedException { + return new String(client.getData(key)); + } + + private boolean isExisted(String key, Client client) throws KeeperException, InterruptedException { + return client.checkExists(key); + } + + protected void getChildrenKeys(Client client) throws KeeperException, InterruptedException { + String key = "a/b"; + String current = "a"; + client.createAllNeedPath(key, "", CreateMode.PERSISTENT); + List result = client.getChildren(current); + Collections.sort(result, new Comparator() { + public int compare(final String o1, final String o2) { + return o2.compareTo(o1); + } + }); + assert result.get(0).equals("b"); + client.deleteCurrentBranch(key); + } + + protected void persist(Client client) throws KeeperException, InterruptedException { + String key = "a"; + String value = "aa"; + String newValue = "aaa"; + if (!isExisted(key, client)) { + client.createAllNeedPath(key, value, CreateMode.PERSISTENT); + } else { + update(key, value, client); + } + + assert getDirectly(key, client).equals(value); + + update(key, newValue, client); + assert getDirectly(key, client).equals(newValue); + client.deleteCurrentBranch(key); + } + + private void update(String key, String value, Client client) throws KeeperException, InterruptedException { + client.updateWithCheck(key, value); + } + + protected void persistEphemeral(Client client) throws KeeperException, InterruptedException { + String key = "a/b/bb"; + String value = "b1b"; + client.createAllNeedPath(key, value, CreateMode.PERSISTENT); +// assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), null).getEphemeralOwner() == 0; + Stat stat = new Stat(); + client.getZooKeeper().getData(PathUtil.getRealPath(TestSupport.ROOT, key), false, stat); + assert stat.getEphemeralOwner() == 0; + + client.deleteAllChildren(key); + assert !isExisted(key, client); + client.createAllNeedPath(key, value, CreateMode.EPHEMERAL); + + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key), null).getEphemeralOwner() != 0; // Ephemeral node connection session id + client.deleteCurrentBranch(key); + } + + protected void delAllChildren(Client client) throws KeeperException, InterruptedException { + String key = "a/b/bb"; + client.createAllNeedPath(key, "bb", CreateMode.PERSISTENT); + key = "a/c/cc"; + client.createAllNeedPath(key, "cc", CreateMode.PERSISTENT); + System.out.println(client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), null).getNumChildren()); // nearest children count + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) != null; + client.deleteAllChildren("a"); + assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) == null; + assert client.getZooKeeper().exists("/" + TestSupport.ROOT, false) != null; + client.deleteNamespace(); + } + + protected void watch(Client client) throws KeeperException, InterruptedException { + List expected = new ArrayList<>(); + expected.add("update_/test/a_value"); + expected.add("update_/test/a_value1"); + expected.add("update_/test/a_value2"); + expected.add("delete_/test/a_"); + List actual = new ArrayList<>(); + + Listener listener = buildListener(client, actual); + + String key = "a"; + Watcher watcher = client.registerWatch(key, listener); + client.createCurrentOnly(key, "aaa", CreateMode.EPHEMERAL); + client.checkExists(key, watcher); + client.updateWithCheck(key, "value"); + System.out.println(new String(client.getData(key))); + assert client.getDataString(key).equals("value"); + client.updateWithCheck(key, "value1"); + assert client.getDataString(key).equals("value1"); + client.updateWithCheck(key, "value2"); + assert client.getDataString(key).equals("value2"); + client.deleteCurrentBranch(key); + Thread.sleep(100); + assert expected.size() == actual.size(); + assert expected.containsAll(actual); + client.unregisterWatch(key); + } + + protected Listener buildListener(Client client, List actual){ + Listener listener = new Listener() { + @Override + public void process(WatchedEvent event) { + System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); + System.out.println(event.getPath()); + System.out.println(event.getType()); + + switch (event.getType()) { + case NodeDataChanged: + case NodeChildrenChanged: { + String result; + try { + result = new String(client.getZooKeeper().getData(event.getPath(),false, null)); + System.out.println(); + } catch (KeeperException e) { + result = e.getMessage(); + e.printStackTrace(); + } catch (InterruptedException e) { + result = e.getMessage(); + e.printStackTrace(); + } + actual.add(new StringBuilder().append("update_").append(event.getPath()).append("_").append(result).toString()); + break; + } + case NodeDeleted: { + actual.add(new StringBuilder().append("delete_").append(event.getPath()).append("_").toString()); + break; + } + default: + actual.add(new StringBuilder().append("ignore_").append(event.getPath()).append("_").append(event.getType()).toString()); + break; + } + System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); + } + }; + return listener; + } + + protected void close(Client client) throws Exception { client.close(); - client = null; + assert client.getZooKeeper().getState() == ZooKeeper.States.CLOSED; } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index 3e5e090b15d0e..1cf87e8ac605b 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -24,235 +24,61 @@ public class UsualClientTest extends BaseClientTest { @Test public void createRoot() throws KeeperException, InterruptedException { - client.createNamespace(); - assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) != null; - client.deleteNamespace(); - assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; - } - - @Ignore - @Test - public void deleteRoot() throws KeeperException, InterruptedException { - client.deleteNamespace(); - assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; + super.createRoot(testClient); } @Test public void createChild() throws KeeperException, InterruptedException { - String key = "a/b/bb"; - client.createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) != null; - client.deleteCurrentBranch(key); - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) == null; + super.createChild(testClient); } @Test public void deleteBranch() throws KeeperException, InterruptedException { - String keyB = "a/b/bb"; - client.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; - String keyC = "a/c/cc"; - client.createAllNeedPath(keyC, "ccc11", CreateMode.PERSISTENT); - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; - client.deleteCurrentBranch(keyC); - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) == null; - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), false) != null; - client.deleteCurrentBranch(keyB); - assert client.getZooKeeper().exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; - client.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; - client.deleteCurrentBranch(keyB); - assert client.getZooKeeper().exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; + super.deleteBranch(testClient); } @Test public void isExisted() throws KeeperException, InterruptedException { - String key = "a/b/bb"; - client.createAllNeedPath(key, "", CreateMode.PERSISTENT); - assert isExisted(key); - client.deleteCurrentBranch(key); + super.isExisted(testClient); } @Test public void get() throws KeeperException, InterruptedException { - client.createAllNeedPath("a/b", "bbb11", CreateMode.PERSISTENT); - String key = "a"; - // TODO: cache - assert getDirectly(key).equals(""); - key = "a/b"; - assert getDirectly(key).equals("bbb11"); - client.deleteCurrentBranch("a/b"); + super.get(testClient); } @Test public void asynGet() throws KeeperException, InterruptedException { - CountDownLatch ready = new CountDownLatch(1); - String key = "a/b"; - String value = "bbb11"; - client.createAllNeedPath(key, value, CreateMode.PERSISTENT); - AsyncCallback.DataCallback callback = new AsyncCallback.DataCallback() { - @Override - public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) { - String result = new String(data); - System.out.println(new StringBuffer().append("rc:").append(rc).append(",path:").append(path).append(",ctx:").append(ctx).append(",stat:").append(stat)); - assert result.equals(ctx); - ready.countDown(); - } - }; - client.getData(key, callback, value); - ready.await(); - client.deleteCurrentBranch("a/b"); - } - - private String getDirectly(String key) throws KeeperException, InterruptedException { - return new String(client.getData(key)); - } - - private boolean isExisted(String key) throws KeeperException, InterruptedException { - return client.checkExists(key); + super.asynGet(testClient); } @Test public void getChildrenKeys() throws KeeperException, InterruptedException { - String key = "a/b"; - String current = "a"; - client.createAllNeedPath(key, "", CreateMode.PERSISTENT); - List result = client.getChildren(current); - Collections.sort(result, new Comparator() { - public int compare(final String o1, final String o2) { - return o2.compareTo(o1); - } - }); - assert result.get(0).equals("b"); - client.deleteCurrentBranch(key); + super.getChildrenKeys(testClient); } @Test public void persist() throws KeeperException, InterruptedException { - String key = "a"; - String value = "aa"; - String newValue = "aaa"; - if (!isExisted(key)) { - client.createAllNeedPath(key, value, CreateMode.PERSISTENT); - } else { - update(key, value); - } - - assert getDirectly(key).equals(value); - - update(key, newValue); - assert getDirectly(key).equals(newValue); - client.deleteCurrentBranch(key); + super.persist(testClient); } - private void update(String key, String value) throws KeeperException, InterruptedException { - client.updateWithCheck(key, value); - } - @Test public void persistEphemeral() throws KeeperException, InterruptedException { - String key = "a/b/bb"; - String value = "b1b"; - client.createAllNeedPath(key, value, CreateMode.PERSISTENT); -// assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), null).getEphemeralOwner() == 0; - Stat stat = new Stat(); - client.getZooKeeper().getData(PathUtil.getRealPath(TestSupport.ROOT, key), false, stat); - assert stat.getEphemeralOwner() == 0; - - client.deleteAllChildren(key); - assert !isExisted(key); - client.createAllNeedPath(key, value, CreateMode.EPHEMERAL); - - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key), null).getEphemeralOwner() != 0; // Ephemeral node connection session id - client.deleteCurrentBranch(key); + super.persistEphemeral(testClient); } @Test public void delAllChildren() throws KeeperException, InterruptedException { - String key = "a/b/bb"; - client.createAllNeedPath(key, "bb", CreateMode.PERSISTENT); - key = "a/c/cc"; - client.createAllNeedPath(key, "cc", CreateMode.PERSISTENT); - System.out.println(client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), null).getNumChildren()); // nearest children count - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) != null; - client.deleteAllChildren("a"); - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) == null; - assert client.getZooKeeper().exists("/" + TestSupport.ROOT, false) != null; - client.deleteNamespace(); + super.delAllChildren(testClient); } -// @Ignore @Test public void watch() throws KeeperException, InterruptedException { - List expected = new ArrayList<>(); - expected.add("update_/test/a_value"); - expected.add("update_/test/a_value1"); - expected.add("update_/test/a_value2"); - expected.add("delete_/test/a_"); - List actual = new ArrayList<>(); - - Listener listener = buildListener(actual); - - String key = "a"; - Watcher watcher = client.registerWatch(key, listener); - client.createCurrentOnly(key, "aaa", CreateMode.EPHEMERAL); - client.checkExists(key, watcher); - client.updateWithCheck(key, "value"); - System.out.println(new String(client.getData(key))); - assert client.getDataString(key).equals("value"); - client.updateWithCheck(key, "value1"); - assert client.getDataString(key).equals("value1"); - client.updateWithCheck(key, "value2"); - assert client.getDataString(key).equals("value2"); - client.deleteCurrentBranch(key); - Thread.sleep(100); - assert expected.size() == actual.size(); - assert expected.containsAll(actual); - client.unregisterWatch(key); - } - - private Listener buildListener(List actual){ - Listener listener = new Listener() { - @Override - public void process(WatchedEvent event) { - System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); - System.out.println(event.getPath()); - System.out.println(event.getType()); - - switch (event.getType()) { - case NodeDataChanged: - case NodeChildrenChanged: { - String result; - try { - result = new String(client.getZooKeeper().getData(event.getPath(),false, null)); - System.out.println(); - } catch (KeeperException e) { - result = e.getMessage(); - e.printStackTrace(); - } catch (InterruptedException e) { - result = e.getMessage(); - e.printStackTrace(); - } - actual.add(new StringBuilder().append("update_").append(event.getPath()).append("_").append(result).toString()); - break; - } - case NodeDeleted: { - actual.add(new StringBuilder().append("delete_").append(event.getPath()).append("_").toString()); - break; - } - default: - actual.add(new StringBuilder().append("ignore_").append(event.getPath()).append("_").append(event.getType()).toString()); - break; - } - System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); - } - }; - return listener; + super.watch(testClient); } @Test public void close() throws Exception { - client.close(); - assert client.getZooKeeper().getState() == ZooKeeper.States.CLOSED; + super.close(testClient); } } \ No newline at end of file From 5b8e90181abb57514d8b9cf570aa49946f647507 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 26 Apr 2018 18:09:30 +0800 Subject: [PATCH 043/351] test --- .../client/zookeeper/ClientsTest.java | 88 ++++++++++++++++--- .../client/zookeeper/UsualClientTest.java | 16 +--- 2 files changed, 79 insertions(+), 25 deletions(-) diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java index 3e9051738fbef..f97d5e2181d65 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java @@ -1,10 +1,6 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.untils.Constants; -import com.saaavsaaa.client.untils.Listener; import org.apache.zookeeper.KeeperException; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import java.io.IOException; @@ -17,7 +13,6 @@ public class ClientsTest extends BaseClientTest { private List clients; - @Before @Override public void start() throws IOException, InterruptedException { clients = new ArrayList<>(2); @@ -26,7 +21,6 @@ public void start() throws IOException, InterruptedException { clients.add(createWatchClient(creator)); } - @After @Override public void stop() throws InterruptedException { for (Client client : clients) { @@ -38,10 +32,84 @@ public void stop() throws InterruptedException { @Test public void createRoot() throws KeeperException, InterruptedException { for (Client client : clients) { - client.createNamespace(); - assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) != null; - client.deleteNamespace(); - assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; + super.createRoot(client); + } + } + + @Test + public void createChild() throws KeeperException, InterruptedException { + for (Client client : clients) { + super.createChild(client); + } + } + + @Test + public void deleteBranch() throws KeeperException, InterruptedException { + for (Client client : clients) { + super.deleteBranch(client); + } + } + + @Test + public void isExisted() throws KeeperException, InterruptedException { + for (Client client : clients) { + super.isExisted(client); + } + } + + @Test + public void get() throws KeeperException, InterruptedException { + for (Client client : clients) { + super.get(client); + } + } + + @Test + public void asynGet() throws KeeperException, InterruptedException { + for (Client client : clients) { + super.asynGet(client); + } + } + + @Test + public void getChildrenKeys() throws KeeperException, InterruptedException { + for (Client client : clients) { + super.getChildrenKeys(client); + } + } + + @Test + public void persist() throws KeeperException, InterruptedException { + for (Client client : clients) { + super.persist(client); + } + } + + @Test + public void persistEphemeral() throws KeeperException, InterruptedException { + for (Client client : clients) { + super.persistEphemeral(client); + } + } + + @Test + public void delAllChildren() throws KeeperException, InterruptedException { + for (Client client : clients) { + super.delAllChildren(client); + } + } + + @Test + public void watch() throws KeeperException, InterruptedException { + for (Client client : clients) { + super.watch(client); + } + } + + @Test + public void close() throws Exception { + for (Client client : clients) { + super.close(client); } } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index 1cf87e8ac605b..14894259e1de5 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -1,22 +1,8 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.untils.Constants; -import com.saaavsaaa.client.untils.Listener; -import com.saaavsaaa.client.untils.PathUtil; -import org.apache.zookeeper.*; -import org.apache.zookeeper.data.Stat; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; +import org.apache.zookeeper.KeeperException; import org.junit.Test; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.concurrent.CountDownLatch; - /** * Created by aaa */ From e94b7e7af776c57729e602fe3b7f9f671389f223 Mon Sep 17 00:00:00 2001 From: WangKai Date: Thu, 26 Apr 2018 20:38:54 +0800 Subject: [PATCH 044/351] channelid <-> connectionid --- .../common/SQLPacketsBackendHandler.java | 20 +++++---- .../backend/mysql/MySQLBackendHandler.java | 19 +++++---- .../frontend/mysql/MySQLFrontendHandler.java | 6 ++- .../mysql/packet/command/CommandPacket.java | 10 ++++- .../packet/command/CommandPacketFactory.java | 13 +++--- .../text/fieldlist/ComFieldListPacket.java | 6 +-- .../command/text/query/ComQueryPacket.java | 11 ++--- .../proxy/util/MySQLResultCache.java | 41 +++++++++++++++---- 8 files changed, 85 insertions(+), 41 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java index 3761c7d6abccf..ee912221e352c 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java @@ -40,39 +40,41 @@ public final class SQLPacketsBackendHandler extends SQLExecuteBackendHandler { private SynchronizedFuture synchronizedFuture; - public SQLPacketsBackendHandler(final String sql, final DatabaseType databaseType, final boolean showSQL) { + private int connectionId; + + public SQLPacketsBackendHandler(final String sql, final int connectionId, final DatabaseType databaseType, final boolean showSQL) { super(sql, databaseType, showSQL); + this.connectionId = connectionId; } @Override public CommandResponsePackets execute() { SQLRouteResult routeResult = routingEngine.route(sql); - //TODO sequenceId should be set. - int sequenceId = 0; if (routeResult.getExecutionUnits().isEmpty()) { return new CommandResponsePackets(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); } synchronizedFuture = new SynchronizedFuture<>(routeResult.getExecutionUnits().size()); - MySQLResultCache.getInstance().put(sequenceId, synchronizedFuture); + MySQLResultCache.getInstance().put(connectionId, synchronizedFuture); for (SQLExecutionUnit each : routeResult.getExecutionUnits()) { execute(routeResult.getSqlStatement(), each); } - //TODO timeout will be set. + //TODO timeout should be set. List result = synchronizedFuture.get(30, TimeUnit.SECONDS); - MySQLResultCache.getInstance().delete(sequenceId); + MySQLResultCache.getInstance().delete(connectionId); return merge(routeResult.getSqlStatement(), result); } @Override protected CommandResponsePackets execute(final SQLStatement sqlStatement, final SQLExecutionUnit sqlExecutionUnit) { + Channel channel = ShardingProxyClient.getInstance().getChannelMap().get(sqlExecutionUnit.getDataSource()); switch (sqlStatement.getType()) { case DQL: - executeQuery(ShardingProxyClient.getInstance().getChannelMap().get(sqlExecutionUnit.getDataSource()), sqlExecutionUnit.getSql()); + executeQuery(channel, sqlExecutionUnit.getSql()); case DML: case DDL: - executeUpdate(ShardingProxyClient.getInstance().getChannelMap().get(sqlExecutionUnit.getDataSource()), sqlExecutionUnit.getSql(), sqlStatement); + executeUpdate(channel, sqlExecutionUnit.getSql(), sqlStatement); default: - executeCommon(ShardingProxyClient.getInstance().getChannelMap().get(sqlExecutionUnit.getDataSource()), sqlExecutionUnit.getSql()); + executeCommon(channel, sqlExecutionUnit.getSql()); } return null; } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java index fde67ad7dccda..95f47fb0edc6f 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java @@ -21,6 +21,8 @@ import io.netty.channel.ChannelHandlerContext; import io.shardingjdbc.proxy.backend.common.CommandResponsePacketsHandler; import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload; +import io.shardingjdbc.proxy.transport.mysql.packet.handshake.AuthPluginData; +import io.shardingjdbc.proxy.transport.mysql.packet.handshake.HandshakePacket; import io.shardingjdbc.proxy.util.MySQLResultCache; /** @@ -32,26 +34,29 @@ public class MySQLBackendHandler extends CommandResponsePacketsHandler { @Override public void channelRead(final ChannelHandlerContext context, final Object message) { - //TODO 判断报文是握手还是OKPacket还是结果集 + //TODO if handshake, then invoke auth. + //TODO if execute command, then invoke executeCommandResponsePackets. + //TODO if OKPacket or ERRPacket, then log. if (true) { auth(context, (ByteBuf) message); - } else { + } else if(true){ + + } else{ } } - //TODO + //TODO message to handshakePacket; send handshakeResponse back. @Override protected void auth(ChannelHandlerContext context, ByteBuf message) { - + HandshakePacket handshakePacket = new HandshakePacket(1,new AuthPluginData()); } //TODO @Override protected void executeCommandResponsePackets(ChannelHandlerContext context, ByteBuf message) { - MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload(message); - int sequenceId = mysqlPacketPayload.readInt1(); - MySQLResultCache.getInstance().get(sequenceId).setResponse(null); + int connectionId = 0; + MySQLResultCache.getInstance().get(connectionId).setResponse(null); } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/mysql/MySQLFrontendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/mysql/MySQLFrontendHandler.java index c77652d86f59d..4bfce52deb5e1 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/mysql/MySQLFrontendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/mysql/MySQLFrontendHandler.java @@ -31,6 +31,7 @@ import io.shardingjdbc.proxy.transport.mysql.packet.handshake.ConnectionIdGenerator; import io.shardingjdbc.proxy.transport.mysql.packet.handshake.HandshakePacket; import io.shardingjdbc.proxy.transport.mysql.packet.handshake.HandshakeResponse41Packet; +import io.shardingjdbc.proxy.util.MySQLResultCache; import lombok.RequiredArgsConstructor; /** @@ -48,6 +49,8 @@ public final class MySQLFrontendHandler extends FrontendHandler { @Override protected void handshake(final ChannelHandlerContext context) { authPluginData = new AuthPluginData(); + int connectionId = ConnectionIdGenerator.getInstance().nextId(); + MySQLResultCache.getInstance().putConnectionMap(context.channel().id().asShortText(),connectionId); context.writeAndFlush(new HandshakePacket(ConnectionIdGenerator.getInstance().nextId(), authPluginData)); } @@ -67,7 +70,8 @@ protected void executeCommand(final ChannelHandlerContext context, final ByteBuf public void run() { MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload(message); int sequenceId = mysqlPacketPayload.readInt1(); - CommandPacket commandPacket = CommandPacketFactory.getCommandPacket(sequenceId, mysqlPacketPayload); + int connectionId = MySQLResultCache.getInstance().getonnectionMap(context.channel().id().asShortText()); + CommandPacket commandPacket = CommandPacketFactory.getCommandPacket(sequenceId, connectionId, mysqlPacketPayload); for (DatabaseProtocolPacket each : commandPacket.execute().getDatabaseProtocolPackets()) { context.write(each); } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacket.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacket.java index 31f3780d2bf4c..f2d64910cac81 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacket.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacket.java @@ -23,16 +23,24 @@ * Command packet. * * @author zhangliang + * @author wangkai */ public abstract class CommandPacket extends MySQLPacket { + protected final int connectionId; + + public CommandPacket(final int sequenceId, final int connectionId) { + super(sequenceId); + this.connectionId = connectionId; + } public CommandPacket(final int sequenceId) { super(sequenceId); + this.connectionId = 0; } /** * Execute command. - * + * * @return result packets to be sent */ public abstract CommandResponsePackets execute(); diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacketFactory.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacketFactory.java index 39540e6f27697..984a663f99639 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacketFactory.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacketFactory.java @@ -20,9 +20,9 @@ import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload; import io.shardingjdbc.proxy.transport.mysql.packet.command.statement.close.ComStmtClosePacket; import io.shardingjdbc.proxy.transport.mysql.packet.command.statement.execute.ComStmtExecutePacket; +import io.shardingjdbc.proxy.transport.mysql.packet.command.statement.prepare.ComStmtPreparePacket; import io.shardingjdbc.proxy.transport.mysql.packet.command.text.fieldlist.ComFieldListPacket; import io.shardingjdbc.proxy.transport.mysql.packet.command.text.initdb.ComInitDbPacket; -import io.shardingjdbc.proxy.transport.mysql.packet.command.statement.prepare.ComStmtPreparePacket; import io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ComQueryPacket; import io.shardingjdbc.proxy.transport.mysql.packet.command.text.quit.ComQuitPacket; @@ -30,17 +30,18 @@ * Command packet factory. * * @author zhangliang + * @author wangkai */ public final class CommandPacketFactory { /** * Get command Packet. - * - * @param sequenceId sequence ID + * + * @param sequenceId sequence ID * @param mysqlPacketPayload MySQL packet payload * @return Command packet */ - public static CommandPacket getCommandPacket(final int sequenceId, final MySQLPacketPayload mysqlPacketPayload) { + public static CommandPacket getCommandPacket(final int sequenceId, final int connectionId, final MySQLPacketPayload mysqlPacketPayload) { int commandPacketTypeValue = mysqlPacketPayload.readInt1(); CommandPacketType type = CommandPacketType.valueOf(commandPacketTypeValue); switch (type) { @@ -49,9 +50,9 @@ public static CommandPacket getCommandPacket(final int sequenceId, final MySQLPa case COM_INIT_DB: return new ComInitDbPacket(sequenceId, mysqlPacketPayload); case COM_FIELD_LIST: - return new ComFieldListPacket(sequenceId, mysqlPacketPayload); + return new ComFieldListPacket(sequenceId, connectionId, mysqlPacketPayload); case COM_QUERY: - return new ComQueryPacket(sequenceId, mysqlPacketPayload); + return new ComQueryPacket(sequenceId, connectionId, mysqlPacketPayload); case COM_STMT_PREPARE: return new ComStmtPreparePacket(sequenceId, mysqlPacketPayload); case COM_STMT_EXECUTE: diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java index 2d83061f3abc6..3129afd8f44e2 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java @@ -42,8 +42,8 @@ public final class ComFieldListPacket extends CommandPacket { private final String fieldWildcard; - public ComFieldListPacket(final int sequenceId, final MySQLPacketPayload mysqlPacketPayload) { - super(sequenceId); + public ComFieldListPacket(final int sequenceId, final int connectionId, final MySQLPacketPayload mysqlPacketPayload) { + super(sequenceId, connectionId); table = mysqlPacketPayload.readStringNul(); fieldWildcard = mysqlPacketPayload.readStringEOF(); } @@ -62,7 +62,7 @@ public CommandResponsePackets execute() { String sql = String.format("SHOW COLUMNS FROM %s FROM %s", table, ShardingConstant.LOGIC_SCHEMA_NAME); // TODO use common database type if (ShardingRuleRegistry.WITHOUT_JDBC) { - return new SQLPacketsBackendHandler(sql, DatabaseType.MySQL, true).execute(); + return new SQLPacketsBackendHandler(sql, connectionId, DatabaseType.MySQL, true).execute(); }else { return new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, true).execute(); } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java index e7011bd1be456..b6b574097d644 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java @@ -28,17 +28,18 @@ /** * COM_QUERY command packet. - * @see COM_QUERY * * @author zhangliang + * @author wangkai + * @see COM_QUERY */ @Slf4j public final class ComQueryPacket extends CommandPacket { private final String sql; - public ComQueryPacket(final int sequenceId, final MySQLPacketPayload mysqlPacketPayload) { - super(sequenceId); + public ComQueryPacket(final int sequenceId, final int connectionId, MySQLPacketPayload mysqlPacketPayload) { + super(sequenceId, connectionId); sql = mysqlPacketPayload.readStringEOF(); } @@ -51,8 +52,8 @@ public void write(final MySQLPacketPayload mysqlPacketPayload) { public CommandResponsePackets execute() { log.debug("COM_QUERY received for Sharding-Proxy: {}", sql); if (ShardingRuleRegistry.WITHOUT_JDBC) { - return new SQLPacketsBackendHandler(sql, DatabaseType.MySQL, true).execute(); - }else { + return new SQLPacketsBackendHandler(sql, connectionId, DatabaseType.MySQL, true).execute(); + } else { return new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, true).execute(); } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java index a94a8961f80d5..8707ef4053e91 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java @@ -31,26 +31,49 @@ public class MySQLResultCache { private static final MySQLResultCache INSTANCE = new MySQLResultCache(); //TODO expire time will be set. - private Cache cache = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.SECONDS).build(); + private Cache resultCache = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.SECONDS).build(); + + private Cache connectionCache = CacheBuilder.newBuilder().build(); + + private Cache channelCache = CacheBuilder.newBuilder().build(); /** - * @param sequenceId send mysql server transaction id. + * @param connectionId mysql connection id. * @param synchronizedFuture multiple result set. */ - public void put(int sequenceId, SynchronizedFuture synchronizedFuture){ - cache.put(sequenceId + "", synchronizedFuture); + public void put(int connectionId, SynchronizedFuture synchronizedFuture) { + resultCache.put(connectionId, synchronizedFuture); } /** - * @param sequenceId send mysql server transaction id. + * @param connectionId mysql connection id. * @return multiple result set. */ - public SynchronizedFuture get(int sequenceId){ - return cache.getIfPresent(sequenceId + ""); + public SynchronizedFuture get(int connectionId) { + return resultCache.getIfPresent(connectionId); + } + + /** + * @param connectionId mysql connection id. + */ + public void delete(int connectionId) { + resultCache.invalidate(connectionId); } - public void delete(int sequenceId){ - cache.invalidate(sequenceId + ""); + /** + * @param serverChannelId netty server channel id. + * @param connectionId mysql connection id. + */ + public void putConnectionMap(String serverChannelId, int connectionId) { + connectionCache.put(serverChannelId, connectionId); + } + + /** + * @param serverChannelId netty server channel id. + * @return connectionId mysql connection id. + */ + public int getonnectionMap(String serverChannelId) { + return connectionCache.getIfPresent(serverChannelId); } /** From a3a9b6f86653b05bf5b44caae1b2c03f2ce7847a Mon Sep 17 00:00:00 2001 From: WangKai Date: Fri, 27 Apr 2018 11:01:13 +0800 Subject: [PATCH 045/351] get connectionid by channel --- .../backend/common/SQLPacketsBackendHandler.java | 1 + .../proxy/backend/mysql/MySQLBackendHandler.java | 2 +- .../shardingjdbc/proxy/util/MySQLResultCache.java | 14 +++++++------- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java index ee912221e352c..8e210fa92a1cf 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java @@ -67,6 +67,7 @@ public CommandResponsePackets execute() { @Override protected CommandResponsePackets execute(final SQLStatement sqlStatement, final SQLExecutionUnit sqlExecutionUnit) { Channel channel = ShardingProxyClient.getInstance().getChannelMap().get(sqlExecutionUnit.getDataSource()); + MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(),connectionId); switch (sqlStatement.getType()) { case DQL: executeQuery(channel, sqlExecutionUnit.getSql()); diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java index 95f47fb0edc6f..e4a137d5329fc 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java @@ -55,7 +55,7 @@ protected void auth(ChannelHandlerContext context, ByteBuf message) { //TODO @Override protected void executeCommandResponsePackets(ChannelHandlerContext context, ByteBuf message) { - int connectionId = 0; + int connectionId = MySQLResultCache.getInstance().getonnectionMap(context.channel().id().asShortText()); MySQLResultCache.getInstance().get(connectionId).setResponse(null); } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java index 8707ef4053e91..ec152b7d4a2b3 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java @@ -30,7 +30,7 @@ */ public class MySQLResultCache { private static final MySQLResultCache INSTANCE = new MySQLResultCache(); - //TODO expire time will be set. + //TODO expire time should be set. private Cache resultCache = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.SECONDS).build(); private Cache connectionCache = CacheBuilder.newBuilder().build(); @@ -61,19 +61,19 @@ public void delete(int connectionId) { } /** - * @param serverChannelId netty server channel id. + * @param channelId netty channel id. * @param connectionId mysql connection id. */ - public void putConnectionMap(String serverChannelId, int connectionId) { - connectionCache.put(serverChannelId, connectionId); + public void putConnectionMap(String channelId, int connectionId) { + connectionCache.put(channelId, connectionId); } /** - * @param serverChannelId netty server channel id. + * @param channelId netty channel id. * @return connectionId mysql connection id. */ - public int getonnectionMap(String serverChannelId) { - return connectionCache.getIfPresent(serverChannelId); + public int getonnectionMap(String channelId) { + return connectionCache.getIfPresent(channelId); } /** From b275f78c9f4f257cbc6bb7d4b0ff60f7da05a1a2 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 27 Apr 2018 15:10:19 +0800 Subject: [PATCH 046/351] test --- .../client/{untils => utility}/PathUtil.java | 4 +- .../{untils => utility}/StringUtil.java | 4 +- .../constant}/Constants.java | 6 ++- .../constant}/Properties.java | 2 +- .../{untils => utility/section}/Listener.java | 2 +- .../utility/section/WatcherCreator.java | 28 ++++++++++++++ .../client/zookeeper/CacheClient.java | 37 ++++++++++++++++--- .../saaavsaaa/client/zookeeper/Client.java | 25 +++++++------ .../client/zookeeper/ClientFactory.java | 3 +- .../saaavsaaa/client/zookeeper/IClient.java | 1 - .../client/zookeeper/TimelyCacheClient.java | 8 ++++ .../client/zookeeper/UsualClient.java | 4 +- .../client/zookeeper/BaseClientTest.java | 10 ++--- .../client/zookeeper/CacheClientTest.java | 5 +-- .../client/zookeeper/ClientsTest.java | 5 ++- .../client/zookeeper/TestSupport.java | 2 +- 16 files changed, 105 insertions(+), 41 deletions(-) rename src/main/java/com/saaavsaaa/client/{untils => utility}/PathUtil.java (98%) rename src/main/java/com/saaavsaaa/client/{untils => utility}/StringUtil.java (73%) rename src/main/java/com/saaavsaaa/client/{untils => utility/constant}/Constants.java (64%) rename src/main/java/com/saaavsaaa/client/{untils => utility/constant}/Properties.java (72%) rename src/main/java/com/saaavsaaa/client/{untils => utility/section}/Listener.java (76%) create mode 100644 src/main/java/com/saaavsaaa/client/utility/section/WatcherCreator.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java diff --git a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java b/src/main/java/com/saaavsaaa/client/utility/PathUtil.java similarity index 98% rename from src/main/java/com/saaavsaaa/client/untils/PathUtil.java rename to src/main/java/com/saaavsaaa/client/utility/PathUtil.java index 8dd5b3a428fe1..70abbef2077fb 100644 --- a/src/main/java/com/saaavsaaa/client/untils/PathUtil.java +++ b/src/main/java/com/saaavsaaa/client/utility/PathUtil.java @@ -1,4 +1,6 @@ -package com.saaavsaaa.client.untils; +package com.saaavsaaa.client.utility; + +import com.saaavsaaa.client.utility.constant.Constants; import javax.swing.tree.TreeNode; import java.util.*; diff --git a/src/main/java/com/saaavsaaa/client/untils/StringUtil.java b/src/main/java/com/saaavsaaa/client/utility/StringUtil.java similarity index 73% rename from src/main/java/com/saaavsaaa/client/untils/StringUtil.java rename to src/main/java/com/saaavsaaa/client/utility/StringUtil.java index f23c3a64dedd4..0bbe795320312 100644 --- a/src/main/java/com/saaavsaaa/client/untils/StringUtil.java +++ b/src/main/java/com/saaavsaaa/client/utility/StringUtil.java @@ -1,6 +1,4 @@ -package com.saaavsaaa.client.untils; - -import java.nio.charset.Charset; +package com.saaavsaaa.client.utility; /** * Created by aaa on 18-4-18. diff --git a/src/main/java/com/saaavsaaa/client/untils/Constants.java b/src/main/java/com/saaavsaaa/client/utility/constant/Constants.java similarity index 64% rename from src/main/java/com/saaavsaaa/client/untils/Constants.java rename to src/main/java/com/saaavsaaa/client/utility/constant/Constants.java index f0a9760975446..f907046f666f9 100644 --- a/src/main/java/com/saaavsaaa/client/untils/Constants.java +++ b/src/main/java/com/saaavsaaa/client/utility/constant/Constants.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.untils; +package com.saaavsaaa.client.utility.constant; import java.nio.charset.Charset; @@ -12,4 +12,8 @@ public final class Constants { public static final Charset UTF_8 = Charset.forName("UTF-8"); public static final String PATH_SEPARATOR = "/"; public static final String GLOBAL_LISTENER_KEY = "globalListener"; + + public static final byte[] CHANGING_VALUE = new byte[]{'c'}; + public static final byte[] RELEASE_VALUE = new byte[]{'r'}; + public static final String CHANGING_KEY = "CHANGING_KEY"; } diff --git a/src/main/java/com/saaavsaaa/client/untils/Properties.java b/src/main/java/com/saaavsaaa/client/utility/constant/Properties.java similarity index 72% rename from src/main/java/com/saaavsaaa/client/untils/Properties.java rename to src/main/java/com/saaavsaaa/client/utility/constant/Properties.java index eae36e7b3b087..252719bfa743b 100644 --- a/src/main/java/com/saaavsaaa/client/untils/Properties.java +++ b/src/main/java/com/saaavsaaa/client/utility/constant/Properties.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.untils; +package com.saaavsaaa.client.utility.constant; /** * Created by aaa on 18-4-25. diff --git a/src/main/java/com/saaavsaaa/client/untils/Listener.java b/src/main/java/com/saaavsaaa/client/utility/section/Listener.java similarity index 76% rename from src/main/java/com/saaavsaaa/client/untils/Listener.java rename to src/main/java/com/saaavsaaa/client/utility/section/Listener.java index 0e792e1fb0f1e..3fa9d2adca2c2 100644 --- a/src/main/java/com/saaavsaaa/client/untils/Listener.java +++ b/src/main/java/com/saaavsaaa/client/utility/section/Listener.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.untils; +package com.saaavsaaa.client.utility.section; import org.apache.zookeeper.WatchedEvent; diff --git a/src/main/java/com/saaavsaaa/client/utility/section/WatcherCreator.java b/src/main/java/com/saaavsaaa/client/utility/section/WatcherCreator.java new file mode 100644 index 0000000000000..47142c3b31df7 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/utility/section/WatcherCreator.java @@ -0,0 +1,28 @@ +package com.saaavsaaa.client.utility.section; + +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; + +import static org.apache.zookeeper.Watcher.Event.EventType.NodeDeleted; + +/** + * Created by aaa on 18-4-27. + */ +public class WatcherCreator { + public static Watcher deleteWatcher(final String path, Listener listener){ + return new Watcher() { + @Override + public void process(WatchedEvent event) { + if (path.equals(event.getPath()) && NodeDeleted.equals(event.getType())){ + listener.process(event); + + System.out.println("----------------------------------------------delete root"); + System.out.println(event.getPath()); + System.out.println(event.getState()); + System.out.println(event.getType()); + System.out.println("----------------------------------------------delete root"); + } + } + }; + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 19f460bbbe1e6..31b2aa606fd97 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -1,22 +1,26 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.cache.PathTree; -import com.saaavsaaa.client.untils.Constants; -import com.saaavsaaa.client.untils.PathUtil; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.Transaction; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.utility.section.WatcherCreator; +import org.apache.zookeeper.*; import java.io.IOException; import java.util.List; import java.util.Stack; /** - * Created by aaa on 18-4-19. + * Created by aaa + * todo restructure the three clients to strategies + * 延迟缓存 定时刷新,刷新时先判断更新根节点数据,写成功开始更新,更新后改回根数据 */ public final class CacheClient extends UsualClient { PathTree pathTree = null; + ZooKeeper reader; + CacheClient(String servers, int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); } @@ -27,6 +31,27 @@ public void start() throws IOException, InterruptedException { pathTree = new PathTree(rootNode); } + //用替换整树的方式更新 + private synchronized void loadCache(Client client) throws KeeperException, InterruptedException { + try { + this.createCurrentOnly(Constants.CHANGING_KEY, Constants.NOTHING_VALUE, CreateMode.EPHEMERAL); + } catch (KeeperException.NodeExistsException e) { + this.checkExists(Constants.CHANGING_KEY, WatcherCreator.deleteWatcher(PathUtil.getRealPath(rootNode, Constants.CHANGING_KEY), new Listener() { + @Override + public void process(WatchedEvent event) { + try { + loadCache(client); + } catch (Exception ee){ + System.out.println(ee.getMessage()); + ee.printStackTrace(); + } + } + })); + } + + this.deleteOnlyCurrent(Constants.CHANGING_KEY); + } + /* * closed beta */ diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index 80bd1d3ce8955..7a47032a0995d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -1,6 +1,10 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.untils.*; +import com.saaavsaaa.client.utility.*; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.utility.constant.Properties; +import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.utility.section.WatcherCreator; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; @@ -101,24 +105,21 @@ public void close() throws InterruptedException { } void createNamespace() throws KeeperException, InterruptedException { + createNamespace(Constants.NOTHING_DATA); + } + + private void createNamespace(final byte[] date) throws KeeperException, InterruptedException { if (rootExist){ return; } - zooKeeper.create(rootNode, Constants.NOTHING_DATA, authorities, CreateMode.PERSISTENT); + zooKeeper.create(rootNode, date, authorities, CreateMode.PERSISTENT); rootExist = true; - zooKeeper.exists(rootNode, new Watcher() { + zooKeeper.exists(rootNode, WatcherCreator.deleteWatcher(rootNode, new Listener() { @Override public void process(WatchedEvent event) { - if (rootNode.equals(event.getPath()) && NodeDeleted.equals(event.getType())){ - rootExist = false; - System.out.println("----------------------------------------------delete root"); - System.out.println(event.getPath()); - System.out.println(event.getState()); - System.out.println(event.getType()); - System.out.println("----------------------------------------------delete root"); - } + rootExist = false; } - }); + })); System.out.println("----------------------------------------------create root"); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index a3bb3f14956c0..d8bedc4fdad3a 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -1,7 +1,6 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.untils.Listener; -import org.apache.zookeeper.Watcher; +import com.saaavsaaa.client.utility.section.Listener; import java.io.IOException; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/IClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/IClient.java index af6f55a8b3f82..0afaadf916f19 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/IClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/IClient.java @@ -1,6 +1,5 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.untils.Listener; import org.apache.zookeeper.*; import java.util.List; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java new file mode 100644 index 0000000000000..9a5843dd1ce2a --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java @@ -0,0 +1,8 @@ +package com.saaavsaaa.client.zookeeper; + +/** + * Created by aaa on 18-4-27. + * 及时缓存 watcher + */ +public class TimelyCacheClient { +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 562b248574272..7579e01969d8b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -3,8 +3,8 @@ * Created by aaa on 18-4-18. */ -import com.saaavsaaa.client.untils.Constants; -import com.saaavsaaa.client.untils.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.utility.PathUtil; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java index d0850af1253b0..62570b144b1be 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java @@ -1,8 +1,8 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.untils.Constants; -import com.saaavsaaa.client.untils.Listener; -import com.saaavsaaa.client.untils.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.utility.PathUtil; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import org.junit.After; @@ -26,8 +26,8 @@ public class BaseClientTest { @Before public void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); - testClient = createClient(creator); -// testClient = createWatchClient(creator); +// testClient = createClient(creator); + testClient = createWatchClient(creator); } protected Client createClient(ClientFactory creator) throws IOException, InterruptedException { diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java index 179bbcbba041f..1fe089d4342b4 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java @@ -1,11 +1,8 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.untils.Constants; -import com.saaavsaaa.client.untils.Listener; -import org.apache.zookeeper.KeeperException; +import com.saaavsaaa.client.utility.section.Listener; import org.junit.After; import org.junit.Before; -import org.junit.Test; import java.io.IOException; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java index f97d5e2181d65..974ecc91ec3f0 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java @@ -15,9 +15,12 @@ public class ClientsTest extends BaseClientTest { @Override public void start() throws IOException, InterruptedException { - clients = new ArrayList<>(2); + clients = new ArrayList<>(5); ClientFactory creator = new ClientFactory(); clients.add(createClient(creator)); + clients.add(createClient(creator)); + clients.add(createClient(creator)); + clients.add(createClient(creator)); clients.add(createWatchClient(creator)); } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java b/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java index b8179774b7120..41d40f45a0289 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java @@ -1,6 +1,6 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.untils.Listener; +import com.saaavsaaa.client.utility.section.Listener; import org.apache.zookeeper.WatchedEvent; /** From e07bb3d1489aada97d91dc260f5ef97b2c8856b5 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 27 Apr 2018 16:04:45 +0800 Subject: [PATCH 047/351] election contend --- .../saaavsaaa/client/cache/PathStatus.java | 9 +++++++ .../client/election/LeaderElection.java | 27 +++++++++++++++++++ .../client/utility/section/Property.java | 22 +++++++++++++++ src/main/resources/client.properties | 1 + 4 files changed, 59 insertions(+) create mode 100644 src/main/java/com/saaavsaaa/client/cache/PathStatus.java create mode 100644 src/main/java/com/saaavsaaa/client/election/LeaderElection.java create mode 100644 src/main/java/com/saaavsaaa/client/utility/section/Property.java create mode 100644 src/main/resources/client.properties diff --git a/src/main/java/com/saaavsaaa/client/cache/PathStatus.java b/src/main/java/com/saaavsaaa/client/cache/PathStatus.java new file mode 100644 index 0000000000000..0321ccb072eac --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/cache/PathStatus.java @@ -0,0 +1,9 @@ +package com.saaavsaaa.client.cache; + +/** + * Created by aaa on 18-4-27. + */ +public enum PathStatus { + CHANGING, + RELEASE +} diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java new file mode 100644 index 0000000000000..7cc1be7c1c730 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -0,0 +1,27 @@ +package com.saaavsaaa.client.election; + +import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.utility.section.Property; +import com.saaavsaaa.client.utility.section.WatcherCreator; +import com.saaavsaaa.client.zookeeper.Client; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; + +/** + * Created by aaa on 18-4-27. + */ +public class LeaderElection { + public static boolean Contend(final String path, final Client client, final Listener listener) throws KeeperException, InterruptedException { + boolean success = false; + try { + client.createCurrentOnly(Constants.CHANGING_KEY, Property.INSTANCE.getClientId(), CreateMode.EPHEMERAL); + success = true; + } catch (KeeperException.NodeExistsException e) { + client.checkExists(Constants.CHANGING_KEY, WatcherCreator.deleteWatcher(PathUtil.getRealPath(path, Constants.CHANGING_KEY), listener)); + } + return success; + } +} diff --git a/src/main/java/com/saaavsaaa/client/utility/section/Property.java b/src/main/java/com/saaavsaaa/client/utility/section/Property.java new file mode 100644 index 0000000000000..1163c083055ca --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/utility/section/Property.java @@ -0,0 +1,22 @@ +package com.saaavsaaa.client.utility.section; + +import com.saaavsaaa.client.utility.StringUtil; + +import java.util.ResourceBundle; + +/** + * Created by aaa on 18-4-27. + */ +public enum Property { + INSTANCE; + + public String getClientId(){ + // ResourceBundle caches the value in Thread + ResourceBundle bundle = ResourceBundle.getBundle("client"); + String clientId = bundle.getString("client.id"); + if (StringUtil.isNullOrBlank(clientId)){ + throw new IllegalArgumentException("client.id doesn't exist"); + } + return clientId; + } +} diff --git a/src/main/resources/client.properties b/src/main/resources/client.properties new file mode 100644 index 0000000000000..4cb3754f82519 --- /dev/null +++ b/src/main/resources/client.properties @@ -0,0 +1 @@ +client.id=1 \ No newline at end of file From 4c9d876623a59bd4d044fc422af8e0a42ab99a11 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 27 Apr 2018 16:39:02 +0800 Subject: [PATCH 048/351] note --- src/main/java/com/saaavsaaa/client/cache/PathTree.java | 9 +++++++++ .../com/saaavsaaa/client/election/LeaderElection.java | 3 +++ .../saaavsaaa/client/utility/constant/Properties.java | 3 ++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index 94fa434fde5b1..f4a1ae296ea28 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -5,8 +5,17 @@ */ public class PathTree { private final PathNode rootNode; + private PathStatus Status; public PathTree(String root) { this.rootNode = new PathNode(root); } + + public PathStatus getStatus() { + return Status; + } + + public void setStatus(PathStatus status) { + Status = status; + } } diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index 7cc1be7c1c730..90b43d525e402 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -14,6 +14,9 @@ * Created by aaa on 18-4-27. */ public class LeaderElection { + /* + * listener will be register when the contention of the path is unsuccessful + */ public static boolean Contend(final String path, final Client client, final Listener listener) throws KeeperException, InterruptedException { boolean success = false; try { diff --git a/src/main/java/com/saaavsaaa/client/utility/constant/Properties.java b/src/main/java/com/saaavsaaa/client/utility/constant/Properties.java index 252719bfa743b..c0253cb861595 100644 --- a/src/main/java/com/saaavsaaa/client/utility/constant/Properties.java +++ b/src/main/java/com/saaavsaaa/client/utility/constant/Properties.java @@ -1,7 +1,8 @@ package com.saaavsaaa.client.utility.constant; /** - * Created by aaa on 18-4-25. + * Created by aaa + * todo replace by section.Property */ public final class Properties { public static final boolean WATCH_ON = true; From fb0ae6e5b743df82ed02529da956b3d58f2028cf Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 27 Apr 2018 18:12:57 +0800 Subject: [PATCH 049/351] task and override and so on --- .../com/saaavsaaa/client/cache/PathNode.java | 8 ++- .../com/saaavsaaa/client/cache/PathTree.java | 4 ++ .../client/utility/constant/Properties.java | 2 + .../client/utility/section/ClientTask.java | 29 ++++++++ .../client/zookeeper/CacheClient.java | 71 ++++++++++++++----- .../saaavsaaa/client/zookeeper/Client.java | 7 +- .../client/zookeeper/UsualClient.java | 19 +++-- 7 files changed, 112 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java diff --git a/src/main/java/com/saaavsaaa/client/cache/PathNode.java b/src/main/java/com/saaavsaaa/client/cache/PathNode.java index 3418f11249957..54bb0defb2177 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathNode.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathNode.java @@ -18,7 +18,11 @@ public Map getChildren() { return children; } - public void setChildren(Map children) { - this.children = children; + public String getKey(){ + return this.nodeKey; + } + + public void attechChild(PathNode node) { + this.children.put(node.nodeKey, node); } } diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index f4a1ae296ea28..2397699b56718 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -18,4 +18,8 @@ public PathStatus getStatus() { public void setStatus(PathStatus status) { Status = status; } + + public PathNode getRootNode() { + return rootNode; + } } diff --git a/src/main/java/com/saaavsaaa/client/utility/constant/Properties.java b/src/main/java/com/saaavsaaa/client/utility/constant/Properties.java index c0253cb861595..0a7f18767eb10 100644 --- a/src/main/java/com/saaavsaaa/client/utility/constant/Properties.java +++ b/src/main/java/com/saaavsaaa/client/utility/constant/Properties.java @@ -6,4 +6,6 @@ */ public final class Properties { public static final boolean WATCH_ON = true; + public static final long THREAD_INITIAL_DELAY = 1000; + public static final long THREAD_PERIOD = 3000; } diff --git a/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java b/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java new file mode 100644 index 0000000000000..ef808ce942b9f --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java @@ -0,0 +1,29 @@ +package com.saaavsaaa.client.utility.section; + +import com.saaavsaaa.client.zookeeper.Client; +import org.apache.zookeeper.KeeperException; + +/** + * Created by aaa on 18-4-27. + */ +public abstract class ClientTask implements Runnable { + private final Client client; + public ClientTask(final Client client){ + this.client = client; + } + + public abstract void run(final Client client) throws KeeperException, InterruptedException; + + @Override + public void run() { + try { + run(client); + } catch (KeeperException e) { + System.out.println("ClientTask"); + e.printStackTrace(); + } catch (InterruptedException e) { + System.out.println("ClientTask"); + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 31b2aa606fd97..a13bddbc67a10 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -1,15 +1,22 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.cache.PathNode; +import com.saaavsaaa.client.cache.PathStatus; import com.saaavsaaa.client.cache.PathTree; -import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.utility.constant.Properties; +import com.saaavsaaa.client.utility.section.ClientTask; import com.saaavsaaa.client.utility.section.Listener; -import com.saaavsaaa.client.utility.section.WatcherCreator; import org.apache.zookeeper.*; import java.io.IOException; import java.util.List; import java.util.Stack; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; /** * Created by aaa @@ -17,6 +24,7 @@ * 延迟缓存 定时刷新,刷新时先判断更新根节点数据,写成功开始更新,更新后改回根数据 */ public final class CacheClient extends UsualClient { + private final ScheduledExecutorService cacheService = Executors.newSingleThreadScheduledExecutor(); PathTree pathTree = null; ZooKeeper reader; @@ -29,27 +37,54 @@ public final class CacheClient extends UsualClient { public void start() throws IOException, InterruptedException { super.start(); pathTree = new PathTree(rootNode); + cacheService.scheduleAtFixedRate(new ClientTask(this) { + @Override + public void run(Client client) throws KeeperException, InterruptedException { + loadCache(client); + } + }, Properties.THREAD_INITIAL_DELAY, Properties.THREAD_PERIOD, TimeUnit.MILLISECONDS); } //用替换整树的方式更新 - private synchronized void loadCache(Client client) throws KeeperException, InterruptedException { - try { - this.createCurrentOnly(Constants.CHANGING_KEY, Constants.NOTHING_VALUE, CreateMode.EPHEMERAL); - } catch (KeeperException.NodeExistsException e) { - this.checkExists(Constants.CHANGING_KEY, WatcherCreator.deleteWatcher(PathUtil.getRealPath(rootNode, Constants.CHANGING_KEY), new Listener() { - @Override - public void process(WatchedEvent event) { - try { - loadCache(client); - } catch (Exception ee){ - System.out.println(ee.getMessage()); - ee.printStackTrace(); - } + private synchronized void loadCache(final Client client) throws KeeperException, InterruptedException { + boolean canBegin; + canBegin = LeaderElection.Contend(rootNode, client, new Listener() { + @Override + public void process(WatchedEvent event) { + try { + loadCache(client); + } catch (Exception ee){ + System.out.println(ee.getMessage()); + ee.printStackTrace(); } - })); - } + } + }); - this.deleteOnlyCurrent(Constants.CHANGING_KEY); + if (canBegin){ + this.loadingCache(client); + this.deleteOnlyCurrent(Constants.CHANGING_KEY); + } + } + + private void loadingCache(final Client client) throws KeeperException, InterruptedException { + pathTree.setStatus(PathStatus.CHANGING); + PathTree newTree = new PathTree(rootNode); + List children = client.getChildren(rootNode); + children.remove(PathUtil.getRealPath(rootNode, Constants.CHANGING_KEY)); + this.attechIntoNode(children, newTree.getRootNode(), client); + pathTree.setStatus(PathStatus.CHANGING); + } + + private void attechIntoNode(final List children, final PathNode pathNode, final Client client) throws KeeperException, InterruptedException { + if (children.isEmpty()){ + return; + } + for (String child : children) { + List subs = client.getChildren(child); + PathNode current = new PathNode(child); + pathNode.attechChild(current); + this.attechIntoNode(subs, current, client); + } } /* diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index 7a47032a0995d..19e9daed540fb 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.utility.*; +import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.StringUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.constant.Properties; import com.saaavsaaa.client.utility.section.Listener; @@ -14,8 +15,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; -import static org.apache.zookeeper.Watcher.Event.EventType.NodeDeleted; - /** * Created by aaa on 18-4-19. */ @@ -68,13 +67,13 @@ void registerWatch(final Listener globalListener){ if (globalListenerRegistered){ return; } - globalListenerRegistered = true; watchers.put(Constants.GLOBAL_LISTENER_KEY, new Watcher() { @Override public void process(WatchedEvent event) { globalListener.process(event); } }); + globalListenerRegistered = true; } public Watcher registerWatch(final String key, final Listener listener){ diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 7579e01969d8b..21150e6c47842 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -24,30 +24,37 @@ public class UsualClient extends Client { super(servers, sessionTimeoutMilliseconds); } + @Override public String getDataString(final String key) throws KeeperException, InterruptedException { return new String(getData(key)); } - + + @Override public byte[] getData(final String key) throws KeeperException, InterruptedException { return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), watched, null); } + @Override public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { zooKeeper.getData(PathUtil.getRealPath(rootNode, key), watched, callback, ctx); } + @Override public boolean checkExists(final String key) throws KeeperException, InterruptedException { return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), watched); } + @Override public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), watcher); } + @Override public List getChildren(final String key) throws KeeperException, InterruptedException { return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), watched); } + @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { createNamespace(); String path = PathUtil.getRealPath(rootNode, key); @@ -66,9 +73,7 @@ public void createCurrentOnly(final String key, final String value, final Create } } - /* - * todo exception recursion - */ + @Override public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ this.createCurrentOnly(key, value, createMode); @@ -93,24 +98,29 @@ public void createAllNeedPath(final String key, final String value, final Create } } + @Override public void update(final String key, final String value) throws KeeperException, InterruptedException { zooKeeper.setData(PathUtil.getRealPath(rootNode, key), value.getBytes(Constants.UTF_8), Constants.VERSION); } + @Override public void updateWithCheck(final String key, final String value) throws KeeperException, InterruptedException { String realPath = PathUtil.getRealPath(rootNode, key); zooKeeper.transaction().check(realPath, Constants.VERSION).setData(realPath, value.getBytes(Constants.UTF_8), Constants.VERSION).commit(); } + @Override public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { zooKeeper.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION); System.out.println("delete : " + PathUtil.getRealPath(rootNode, key)); } + @Override public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { zooKeeper.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION, callback, ctx); } + @Override public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { String realPath = PathUtil.getRealPath(rootNode, key); try { @@ -131,6 +141,7 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup /* * delete the current node with force and delete the super node whose only child node is current node recursively */ + @Override public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { String path = PathUtil.getRealPath(rootNode, key); this.deleteAllChildren(path); From c1d2169ab51373f4c173dce0c5ccd032133ddf2d Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 27 Apr 2018 18:56:20 +0800 Subject: [PATCH 050/351] lazy cache load --- .../client/election/LeaderElection.java | 2 +- .../client/zookeeper/CacheClient.java | 25 +++++++++++++++---- .../saaavsaaa/client/zookeeper/Client.java | 16 +++++++++--- .../client/zookeeper/ClientFactory.java | 6 +++-- .../client/zookeeper/UsualClient.java | 2 -- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index 90b43d525e402..dc472f5253ecc 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -17,7 +17,7 @@ public class LeaderElection { /* * listener will be register when the contention of the path is unsuccessful */ - public static boolean Contend(final String path, final Client client, final Listener listener) throws KeeperException, InterruptedException { + public static boolean contend(final String path, final Client client, final Listener listener) throws KeeperException, InterruptedException { boolean success = false; try { client.createCurrentOnly(Constants.CHANGING_KEY, Property.INSTANCE.getClientId(), CreateMode.EPHEMERAL); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index a13bddbc67a10..f5f1fe1b375d6 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -48,13 +48,13 @@ public void run(Client client) throws KeeperException, InterruptedException { //用替换整树的方式更新 private synchronized void loadCache(final Client client) throws KeeperException, InterruptedException { boolean canBegin; - canBegin = LeaderElection.Contend(rootNode, client, new Listener() { + canBegin = LeaderElection.contend(rootNode, client, new Listener() { @Override public void process(WatchedEvent event) { try { loadCache(client); } catch (Exception ee){ - System.out.println(ee.getMessage()); + System.out.println("loadCache Exception"); ee.printStackTrace(); } } @@ -72,7 +72,7 @@ private void loadingCache(final Client client) throws KeeperException, Interrupt List children = client.getChildren(rootNode); children.remove(PathUtil.getRealPath(rootNode, Constants.CHANGING_KEY)); this.attechIntoNode(children, newTree.getRootNode(), client); - pathTree.setStatus(PathStatus.CHANGING); + pathTree.setStatus(PathStatus.RELEASE); } private void attechIntoNode(final List children, final PathNode pathNode, final Client client) throws KeeperException, InterruptedException { @@ -80,9 +80,9 @@ private void attechIntoNode(final List children, final PathNode pathNode return; } for (String child : children) { - List subs = client.getChildren(child); - PathNode current = new PathNode(child); + PathNode current = new PathNode(PathUtil.getRealPath(pathNode.getKey(), child)); pathNode.attechChild(current); + List subs = client.getChildren(child); this.attechIntoNode(subs, current, client); } } @@ -90,6 +90,7 @@ private void attechIntoNode(final List children, final PathNode pathNode /* * closed beta */ + @Override public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ this.createCurrentOnly(key, value, createMode); @@ -160,4 +161,18 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr } transaction.commit(); } + + //=========================================================================================================== + + private boolean cacheReady(){ + return PathStatus.RELEASE == pathTree.getStatus(); + } + + @Override + public byte[] getData(final String key) throws KeeperException, InterruptedException { + if (cacheReady()){ +// return + } + return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), watched, null); + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index 19e9daed540fb..fca62ac50daf9 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -21,15 +21,17 @@ public abstract class Client implements IClient{ private static final CountDownLatch CONNECTED = new CountDownLatch(1); protected static final Map watchers = new ConcurrentHashMap<>(); - private boolean globalListenerRegistered = false; + protected final boolean watched = true; //false private final String servers; private final int sessionTimeOut; - protected ZooKeeper zooKeeper; + protected ZooKeeper zooKeeper; + protected List authorities; + private boolean globalListenerRegistered = false; protected String rootNode = "/InitValue"; protected boolean rootExist = false; - protected List authorities; + private ClientFactory clientFactory; protected Client(String servers, int sessionTimeoutMilliseconds) { this.servers = servers; @@ -134,4 +136,12 @@ void setAuthorities(String scheme, byte[] auth) { zooKeeper.addAuthInfo(scheme , auth); this.authorities = ZooDefs.Ids.CREATOR_ALL_ACL; } + + public ClientFactory getClientFactory() { + return clientFactory; + } + + void setClientFactory(ClientFactory clientFactory) { + this.clientFactory = clientFactory; + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index d8bedc4fdad3a..843feb3f84372 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.Listener; import java.io.IOException; @@ -34,6 +35,7 @@ public ClientFactory watch(final Listener listener){ } public synchronized Client start() throws IOException, InterruptedException { + client.setClientFactory(this); client.setRootNode(namespace); client.start(); client.setAuthorities(scheme , auth); @@ -44,8 +46,8 @@ public synchronized Client start() throws IOException, InterruptedException { } public ClientFactory setNamespace(String namespace) { - if (!namespace.startsWith("/")){ - namespace = "/" + namespace; + if (!namespace.startsWith(Constants.PATH_SEPARATOR)){ + namespace = Constants.PATH_SEPARATOR + namespace; } this.namespace = namespace; return this; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 21150e6c47842..85c1b77f9c6fb 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -18,8 +18,6 @@ * todo org.apache.zookeeper.KeeperException$NodeExistsException: KeeperErrorCode = NodeExists */ public class UsualClient extends Client { - private final boolean watched = true; //false - UsualClient(String servers, int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); } From 6977b7bcde0e39ea4610d89ad7dc0f801c9e995f Mon Sep 17 00:00:00 2001 From: saaav Date: Sat, 28 Apr 2018 15:15:22 +0800 Subject: [PATCH 051/351] lazy cache load --- .../com/saaavsaaa/client/cache/PathNode.java | 31 +++++++++++++++++-- .../com/saaavsaaa/client/cache/PathTree.java | 22 +++++++++++-- .../client/election/LeaderElection.java | 1 + .../client/zookeeper/CacheClient.java | 21 +++++++++---- 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/cache/PathNode.java b/src/main/java/com/saaavsaaa/client/cache/PathNode.java index 54bb0defb2177..d338ba282af23 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathNode.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathNode.java @@ -1,17 +1,23 @@ package com.saaavsaaa.client.cache; +import com.saaavsaaa.client.utility.constant.Constants; + import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * Created by aaa on 18-4-18. + * todo nodeKey can use current node short path: get children.containsKey(path) should be change -> /root/aaa/aaa */ public class PathNode { private final String nodeKey; - private Map children; + private byte[] value; + private Map children = new ConcurrentHashMap<>(); - public PathNode(String key) { + public PathNode(final String key, final byte[] value) { this.nodeKey = key; + this.value = value; } public Map getChildren() { @@ -22,7 +28,26 @@ public String getKey(){ return this.nodeKey; } - public void attechChild(PathNode node) { + public void attechChild(final PathNode node) { this.children.put(node.nodeKey, node); } + + PathNode get(final int index, final String path) { + if (children.isEmpty()){ + return null; + } + if (children.containsKey(path)){ + return children.get(path); + } + int nextSeparate = path.indexOf(Constants.PATH_SEPARATOR, index); + if (nextSeparate == -1){ + nextSeparate = path.length() - 1; + } + + return children.get(path.substring(0, nextSeparate)).get(nextSeparate + 1, path); + } + + public byte[] getValue() { + return value; + } } diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index 2397699b56718..4f3f2365854d0 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -1,5 +1,10 @@ package com.saaavsaaa.client.cache; +import com.saaavsaaa.client.utility.constant.Constants; +import org.apache.zookeeper.common.PathUtils; + +import java.util.TreeMap; + /** * Created by aaa on 18-4-26. */ @@ -7,19 +12,30 @@ public class PathTree { private final PathNode rootNode; private PathStatus Status; - public PathTree(String root) { - this.rootNode = new PathNode(root); + public PathTree(final String root) { + this.rootNode = new PathNode(root, Constants.RELEASE_VALUE); + Status = PathStatus.RELEASE; } public PathStatus getStatus() { return Status; } - public void setStatus(PathStatus status) { + public void setStatus(final PathStatus status) { Status = status; } public PathNode getRootNode() { return rootNode; } + + public byte[] getValue(final String path){ + PathNode node = get(path); + return null == node ? null : node.getValue(); + } + + private PathNode get(final String path){ + PathUtils.validatePath(path); + return rootNode.get(1, path); + } } diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index dc472f5253ecc..2a5e6ad671db4 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -23,6 +23,7 @@ public static boolean contend(final String path, final Client client, final List client.createCurrentOnly(Constants.CHANGING_KEY, Property.INSTANCE.getClientId(), CreateMode.EPHEMERAL); success = true; } catch (KeeperException.NodeExistsException e) { + // TODO: or changing_key node value == current client id client.checkExists(Constants.CHANGING_KEY, WatcherCreator.deleteWatcher(PathUtil.getRealPath(path, Constants.CHANGING_KEY), listener)); } return success; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index f5f1fe1b375d6..b8f48ee2341be 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -13,7 +13,9 @@ import java.io.IOException; import java.util.List; +import java.util.Map; import java.util.Stack; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -21,10 +23,12 @@ /** * Created by aaa * todo restructure the three clients to strategies - * 延迟缓存 定时刷新,刷新时先判断更新根节点数据,写成功开始更新,更新后改回根数据 + * todo log + * todo currentNodes */ public final class CacheClient extends UsualClient { private final ScheduledExecutorService cacheService = Executors.newSingleThreadScheduledExecutor(); + private final Map currentNodes = new ConcurrentHashMap<>(); PathTree pathTree = null; ZooKeeper reader; @@ -40,7 +44,9 @@ public void start() throws IOException, InterruptedException { cacheService.scheduleAtFixedRate(new ClientTask(this) { @Override public void run(Client client) throws KeeperException, InterruptedException { - loadCache(client); + if (PathStatus.RELEASE == pathTree.getStatus()) { + loadCache(client); + } } }, Properties.THREAD_INITIAL_DELAY, Properties.THREAD_PERIOD, TimeUnit.MILLISECONDS); } @@ -62,6 +68,7 @@ public void process(WatchedEvent event) { if (canBegin){ this.loadingCache(client); + currentNodes.clear(); this.deleteOnlyCurrent(Constants.CHANGING_KEY); } } @@ -72,7 +79,8 @@ private void loadingCache(final Client client) throws KeeperException, Interrupt List children = client.getChildren(rootNode); children.remove(PathUtil.getRealPath(rootNode, Constants.CHANGING_KEY)); this.attechIntoNode(children, newTree.getRootNode(), client); - pathTree.setStatus(PathStatus.RELEASE); + newTree.setStatus(PathStatus.RELEASE); + pathTree = newTree; } private void attechIntoNode(final List children, final PathNode pathNode, final Client client) throws KeeperException, InterruptedException { @@ -80,7 +88,7 @@ private void attechIntoNode(final List children, final PathNode pathNode return; } for (String child : children) { - PathNode current = new PathNode(PathUtil.getRealPath(pathNode.getKey(), child)); + PathNode current = new PathNode(PathUtil.getRealPath(pathNode.getKey(), child), super.getData(child)); pathNode.attechChild(current); List subs = client.getChildren(child); this.attechIntoNode(subs, current, client); @@ -170,9 +178,10 @@ private boolean cacheReady(){ @Override public byte[] getData(final String key) throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(rootNode, key); if (cacheReady()){ -// return + return pathTree.getValue(path); } - return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), watched, null); + return zooKeeper.getData(path, watched, null); } } From 04c845943d048c5cd8b1e1612249252bc9f5d1a2 Mon Sep 17 00:00:00 2001 From: saaav Date: Sat, 28 Apr 2018 17:05:38 +0800 Subject: [PATCH 052/351] lazy cache base --- .../client/zookeeper/CacheClient.java | 74 +++++++++++-------- 1 file changed, 45 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index b8f48ee2341be..59f159937378d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -28,7 +28,6 @@ */ public final class CacheClient extends UsualClient { private final ScheduledExecutorService cacheService = Executors.newSingleThreadScheduledExecutor(); - private final Map currentNodes = new ConcurrentHashMap<>(); PathTree pathTree = null; ZooKeeper reader; @@ -41,7 +40,8 @@ public final class CacheClient extends UsualClient { public void start() throws IOException, InterruptedException { super.start(); pathTree = new PathTree(rootNode); - cacheService.scheduleAtFixedRate(new ClientTask(this) { + Client usualClient = this.getClientFactory().newUsualClient().start(); + cacheService.scheduleAtFixedRate(new ClientTask(usualClient) { @Override public void run(Client client) throws KeeperException, InterruptedException { if (PathStatus.RELEASE == pathTree.getStatus()) { @@ -67,34 +67,11 @@ public void process(WatchedEvent event) { }); if (canBegin){ - this.loadingCache(client); - currentNodes.clear(); + pathTree.loading(client); this.deleteOnlyCurrent(Constants.CHANGING_KEY); } } - private void loadingCache(final Client client) throws KeeperException, InterruptedException { - pathTree.setStatus(PathStatus.CHANGING); - PathTree newTree = new PathTree(rootNode); - List children = client.getChildren(rootNode); - children.remove(PathUtil.getRealPath(rootNode, Constants.CHANGING_KEY)); - this.attechIntoNode(children, newTree.getRootNode(), client); - newTree.setStatus(PathStatus.RELEASE); - pathTree = newTree; - } - - private void attechIntoNode(final List children, final PathNode pathNode, final Client client) throws KeeperException, InterruptedException { - if (children.isEmpty()){ - return; - } - for (String child : children) { - PathNode current = new PathNode(PathUtil.getRealPath(pathNode.getKey(), child), super.getData(child)); - pathNode.attechChild(current); - List subs = client.getChildren(child); - this.attechIntoNode(subs, current, client); - } - } - /* * closed beta */ @@ -138,11 +115,11 @@ private Transaction createInTransaction(final String key, byte[] data, final Cre @Override public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { Transaction transaction = zooKeeper.transaction(); - this.deleteAllChild(key, transaction); + this.deleteAllChildren(key, transaction); transaction.commit(); } - private void deleteAllChild(final String key, final Transaction transaction) throws KeeperException, InterruptedException { + private void deleteAllChildren(final String key, final Transaction transaction) throws KeeperException, InterruptedException { } private void deleteOnlyCurrent(final String key, final Transaction transaction) throws KeeperException, InterruptedException { @@ -182,6 +159,45 @@ public byte[] getData(final String key) throws KeeperException, InterruptedExcep if (cacheReady()){ return pathTree.getValue(path); } - return zooKeeper.getData(path, watched, null); + // without watcher ensure cache execute result consistency + byte[] data = zooKeeper.getData(path, false, null); + pathTree.put(path, new String(data)); + return data; + } + + @Override + public boolean checkExists(final String key) throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(rootNode, key); + if (cacheReady()){ + return null != pathTree.getValue(path); + } + return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), false); + } + + @Override + public List getChildren(final String key) throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(rootNode, key); + if (cacheReady()){ + return pathTree.getChildren(path); + } + return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + super.createCurrentOnly(key, value, createMode); + pathTree.put(PathUtil.getRealPath(rootNode, key), value); + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + super.deleteOnlyCurrent(key); + pathTree.delete(PathUtil.getRealPath(rootNode, key)); + } + + @Override + public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + zooKeeper.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION, callback, ctx); + pathTree.delete(PathUtil.getRealPath(rootNode, key)); } } From a5e064fa940f434aef53867784aab164045d5493 Mon Sep 17 00:00:00 2001 From: saaav Date: Sat, 28 Apr 2018 17:37:04 +0800 Subject: [PATCH 053/351] lazy cache base --- .../com/saaavsaaa/client/cache/PathNode.java | 4 + .../com/saaavsaaa/client/cache/PathTree.java | 77 +++++++++++++++++-- .../client/zookeeper/ClientFactory.java | 17 ++++ 3 files changed, 93 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/cache/PathNode.java b/src/main/java/com/saaavsaaa/client/cache/PathNode.java index d338ba282af23..83dfb28c13ac6 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathNode.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathNode.java @@ -15,6 +15,10 @@ public class PathNode { private byte[] value; private Map children = new ConcurrentHashMap<>(); + public PathNode(final String key) { + this(key, Constants.RELEASE_VALUE); + } + public PathNode(final String key, final byte[] value) { this.nodeKey = key; this.value = value; diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index 4f3f2365854d0..d6263003e784c 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -1,20 +1,51 @@ package com.saaavsaaa.client.cache; +import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.zookeeper.Client; +import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.common.PathUtils; -import java.util.TreeMap; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; /** * Created by aaa on 18-4-26. */ -public class PathTree { - private final PathNode rootNode; +public final class PathTree { + private final Map currentNodes = new ConcurrentHashMap<>(); + private PathNode rootNode; private PathStatus Status; public PathTree(final String root) { - this.rootNode = new PathNode(root, Constants.RELEASE_VALUE); - Status = PathStatus.RELEASE; + this.rootNode = new PathNode(root); + this.Status = PathStatus.RELEASE; + } + + public void loading(final Client client) throws KeeperException, InterruptedException { + this.setStatus(PathStatus.CHANGING); + + PathNode newRoot = new PathNode(rootNode.getKey()); + List children = client.getChildren(rootNode.getKey()); + children.remove(PathUtil.getRealPath(rootNode.getKey(), Constants.CHANGING_KEY)); + this.attechIntoNode(children, newRoot, client); + rootNode = newRoot; + + this.setStatus(PathStatus.RELEASE); + } + + private void attechIntoNode(final List children, final PathNode pathNode, final Client client) throws KeeperException, InterruptedException { + if (children.isEmpty()){ + return; + } + for (String child : children) { + PathNode current = new PathNode(PathUtil.getRealPath(pathNode.getKey(), child), client.getData(child)); + pathNode.attechChild(current); + List subs = client.getChildren(child); + this.attechIntoNode(subs, current, client); + } } public PathStatus getStatus() { @@ -22,6 +53,9 @@ public PathStatus getStatus() { } public void setStatus(final PathStatus status) { + if (PathStatus.RELEASE == status){ + currentNodes.clear(); + } Status = status; } @@ -30,6 +64,9 @@ public PathNode getRootNode() { } public byte[] getValue(final String path){ + if (currentNodes.containsKey(path)){ + return currentNodes.get(path).getBytes(Constants.UTF_8); + } PathNode node = get(path); return null == node ? null : node.getValue(); } @@ -38,4 +75,34 @@ private PathNode get(final String path){ PathUtils.validatePath(path); return rootNode.get(1, path); } + + public List getChildren(String path) { + PathNode node = get(path); + List result = new ArrayList<>(); + if (node == null){ + return result; + } + if (node.getChildren().isEmpty()) { + return result; + } + Iterator children = node.getChildren().values().iterator(); + while (children.hasNext()){ + // children keys don't needn't currentNodes + result.add(new String(children.next().getValue())); + } + return result; + } + + public void put(String path, String value) { + PathUtils.validatePath(path); + currentNodes.put(path, value); + } + + public void delete(String path) { + PathUtils.validatePath(path); + currentNodes.remove(path); + String prxpath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); + PathNode node = get(prxpath); + node.getChildren().remove(path); + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 843feb3f84372..5aa822940b9a0 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.utility.StringUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.Listener; @@ -16,15 +17,31 @@ public class ClientFactory { private String namespace; private String scheme; private byte[] auth; + private String servers; + private int sessionTimeoutMilliseconds; public ClientFactory(){} public ClientFactory newCacheClient(final String servers, final int sessionTimeoutMilliseconds) { + this.servers = servers; + this.sessionTimeoutMilliseconds = sessionTimeoutMilliseconds; + client = new CacheClient(servers, sessionTimeoutMilliseconds); + return this; + } + + ClientFactory newCacheClient() { client = new CacheClient(servers, sessionTimeoutMilliseconds); return this; } public ClientFactory newUsualClient(final String servers, final int sessionTimeoutMilliseconds) { + this.servers = servers; + this.sessionTimeoutMilliseconds = sessionTimeoutMilliseconds; + client = new UsualClient(servers, sessionTimeoutMilliseconds); + return this; + } + + ClientFactory newUsualClient() { client = new UsualClient(servers, sessionTimeoutMilliseconds); return this; } From bd521dfd02386f35da0c0295a5b8f79d6507a463 Mon Sep 17 00:00:00 2001 From: saaav Date: Sat, 28 Apr 2018 18:31:18 +0800 Subject: [PATCH 054/351] lazy cache base --- .../client/election/LeaderElection.java | 39 +++++++- .../client/zookeeper/CacheClient.java | 99 ++++++++++++++----- 2 files changed, 107 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index 2a5e6ad671db4..3617c254cae81 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -13,11 +13,9 @@ /** * Created by aaa on 18-4-27. */ -public class LeaderElection { - /* - * listener will be register when the contention of the path is unsuccessful - */ - public static boolean contend(final String path, final Client client, final Listener listener) throws KeeperException, InterruptedException { +public abstract class LeaderElection { + + private boolean contend(final String path, final Client client, final Listener listener) throws KeeperException, InterruptedException { boolean success = false; try { client.createCurrentOnly(Constants.CHANGING_KEY, Property.INSTANCE.getClientId(), CreateMode.EPHEMERAL); @@ -28,4 +26,35 @@ public static boolean contend(final String path, final Client client, final List } return success; } + + /* + * listener will be register when the contention of the path is unsuccessful + */ + public void executeContention(final String path, final Client client) throws KeeperException, InterruptedException { + boolean canBegin; + canBegin = this.contend(path, client, new Listener() { + @Override + public void process(WatchedEvent event) { + try { + actionWhenUnreached(); + } catch (Exception ee){ + System.out.println("Listener Exception " + path); + ee.printStackTrace(); + } + } + }); + + if (canBegin){ + try { + action(); + } catch (Exception ee){ + System.out.println("action Exception " + path); + ee.printStackTrace(); + } + client.deleteOnlyCurrent(Constants.CHANGING_KEY); + } + } + + public abstract void actionWhenUnreached() throws KeeperException, InterruptedException; + public abstract void action() throws KeeperException, InterruptedException; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 59f159937378d..c1433fecc53db 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -19,6 +19,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; /** * Created by aaa @@ -28,9 +29,9 @@ */ public final class CacheClient extends UsualClient { private final ScheduledExecutorService cacheService = Executors.newSingleThreadScheduledExecutor(); - PathTree pathTree = null; + private Client usualClient; - ZooKeeper reader; + protected PathTree pathTree = null; CacheClient(String servers, int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); @@ -40,7 +41,7 @@ public final class CacheClient extends UsualClient { public void start() throws IOException, InterruptedException { super.start(); pathTree = new PathTree(rootNode); - Client usualClient = this.getClientFactory().newUsualClient().start(); + usualClient = this.getClientFactory().newUsualClient().start(); cacheService.scheduleAtFixedRate(new ClientTask(usualClient) { @Override public void run(Client client) throws KeeperException, InterruptedException { @@ -53,23 +54,18 @@ public void run(Client client) throws KeeperException, InterruptedException { //用替换整树的方式更新 private synchronized void loadCache(final Client client) throws KeeperException, InterruptedException { - boolean canBegin; - canBegin = LeaderElection.contend(rootNode, client, new Listener() { + LeaderElection election = new LeaderElection() { @Override - public void process(WatchedEvent event) { - try { - loadCache(client); - } catch (Exception ee){ - System.out.println("loadCache Exception"); - ee.printStackTrace(); - } + public void actionWhenUnreached() throws KeeperException, InterruptedException { + loadCache(client); } - }); - - if (canBegin){ - pathTree.loading(client); - this.deleteOnlyCurrent(Constants.CHANGING_KEY); - } + + @Override + public void action() throws KeeperException, InterruptedException { + pathTree.loading(client); + } + }; + election.executeContention(rootNode, client); } /* @@ -77,16 +73,32 @@ public void process(WatchedEvent event) { */ @Override public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + LeaderElection election = new LeaderElection() { + @Override + public void actionWhenUnreached() throws KeeperException, InterruptedException { + createAllNeedPath(key, value, createMode); + } + + @Override + public void action() throws KeeperException, InterruptedException { + createBegin(key, value, createMode); + } + }; + election.executeContention(rootNode, this); + } + + + private void createBegin(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - this.createCurrentOnly(key, value, createMode); + super.createCurrentOnly(key, value, createMode); return; } Transaction transaction = zooKeeper.transaction(); - //todo sync cache + List nodes = PathUtil.getPathOrderNodes(rootNode, key); for (int i = 0; i < nodes.size(); i++) { - // todo contrast cache - if (checkExists(nodes.get(i))){ + // todo not goog + if (super.checkExists(nodes.get(i))){ System.out.println("exist:" + nodes.get(i)); continue; } @@ -98,7 +110,6 @@ public void createAllNeedPath(final String key, final String value, final Create } } - // todo org.apache.zookeeper.KeeperException$NodeExistsException: KeeperErrorCode = NodeExists transaction.commit(); } @@ -185,19 +196,55 @@ public List getChildren(final String key) throws KeeperException, Interr @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - super.createCurrentOnly(key, value, createMode); +// super.createCurrentOnly(key, value, createMode); + LeaderElection election = new LeaderElection() { + @Override + public void actionWhenUnreached() throws KeeperException, InterruptedException { + createCurrentOnly(key, value, createMode); + } + + @Override + public void action() throws KeeperException, InterruptedException { + usualClient.createCurrentOnly(key, value, createMode); + } + }; + election.executeContention(rootNode, this); pathTree.put(PathUtil.getRealPath(rootNode, key), value); } @Override public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - super.deleteOnlyCurrent(key); +// super.deleteOnlyCurrent(key); + LeaderElection election = new LeaderElection() { + @Override + public void actionWhenUnreached() throws KeeperException, InterruptedException { + deleteOnlyCurrent(key); + } + + @Override + public void action() throws KeeperException, InterruptedException { + usualClient.deleteOnlyCurrent(key); + } + }; + election.executeContention(rootNode, this); pathTree.delete(PathUtil.getRealPath(rootNode, key)); } @Override public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - zooKeeper.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION, callback, ctx); +// super.deleteOnlyCurrent(key, callback, ctx); + LeaderElection election = new LeaderElection() { + @Override + public void actionWhenUnreached() throws KeeperException, InterruptedException { + deleteOnlyCurrent(key, callback, ctx); + } + + @Override + public void action() throws KeeperException, InterruptedException { + usualClient.deleteOnlyCurrent(key, callback, ctx); + } + }; + election.executeContention(rootNode, this); pathTree.delete(PathUtil.getRealPath(rootNode, key)); } } From 5a2caa43c8c93d153609e43203c33ce59ce6256d Mon Sep 17 00:00:00 2001 From: saaav Date: Sat, 28 Apr 2018 18:48:58 +0800 Subject: [PATCH 055/351] clear --- .../java/com/saaavsaaa/client/cache/PathNode.java | 2 +- .../java/com/saaavsaaa/client/cache/PathStatus.java | 2 +- .../java/com/saaavsaaa/client/cache/PathTree.java | 2 +- .../com/saaavsaaa/client/election/LeaderElection.java | 2 +- .../java/com/saaavsaaa/client/utility/PathUtil.java | 2 +- .../java/com/saaavsaaa/client/utility/StringUtil.java | 2 +- .../saaavsaaa/client/utility/constant/Constants.java | 2 +- .../saaavsaaa/client/utility/section/ClientTask.java | 2 +- .../saaavsaaa/client/utility/section/Listener.java | 2 +- .../saaavsaaa/client/utility/section/Property.java | 2 +- .../client/utility/section/WatcherCreator.java | 2 +- .../com/saaavsaaa/client/zookeeper/CacheClient.java | 11 +++-------- .../java/com/saaavsaaa/client/zookeeper/Client.java | 2 +- .../com/saaavsaaa/client/zookeeper/ClientFactory.java | 2 +- .../java/com/saaavsaaa/client/zookeeper/IClient.java | 2 +- .../saaavsaaa/client/zookeeper/TimelyCacheClient.java | 2 +- .../com/saaavsaaa/client/zookeeper/UsualClient.java | 2 +- .../saaavsaaa/client/zookeeper/BaseClientTest.java | 2 +- .../saaavsaaa/client/zookeeper/CacheClientTest.java | 2 +- .../com/saaavsaaa/client/zookeeper/ClientsTest.java | 2 +- .../com/saaavsaaa/client/zookeeper/TestSupport.java | 2 +- 21 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/cache/PathNode.java b/src/main/java/com/saaavsaaa/client/cache/PathNode.java index 83dfb28c13ac6..613934303b1fe 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathNode.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathNode.java @@ -7,7 +7,7 @@ import java.util.concurrent.ConcurrentHashMap; /** - * Created by aaa on 18-4-18. + * Created by aaa * todo nodeKey can use current node short path: get children.containsKey(path) should be change -> /root/aaa/aaa */ public class PathNode { diff --git a/src/main/java/com/saaavsaaa/client/cache/PathStatus.java b/src/main/java/com/saaavsaaa/client/cache/PathStatus.java index 0321ccb072eac..e899e19e926d4 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathStatus.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathStatus.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.cache; /** - * Created by aaa on 18-4-27. + * Created by aaa */ public enum PathStatus { CHANGING, diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index d6263003e784c..89302d7934f10 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -12,7 +12,7 @@ import java.util.concurrent.ScheduledExecutorService; /** - * Created by aaa on 18-4-26. + * Created by aaa */ public final class PathTree { private final Map currentNodes = new ConcurrentHashMap<>(); diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index 3617c254cae81..6c343cd405b44 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -11,7 +11,7 @@ import org.apache.zookeeper.WatchedEvent; /** - * Created by aaa on 18-4-27. + * Created by aaa */ public abstract class LeaderElection { diff --git a/src/main/java/com/saaavsaaa/client/utility/PathUtil.java b/src/main/java/com/saaavsaaa/client/utility/PathUtil.java index 70abbef2077fb..047c69c4cf10c 100644 --- a/src/main/java/com/saaavsaaa/client/utility/PathUtil.java +++ b/src/main/java/com/saaavsaaa/client/utility/PathUtil.java @@ -6,7 +6,7 @@ import java.util.*; /** - * Created by aaa on 18-4-18. + * Created by aaa */ public class PathUtil { diff --git a/src/main/java/com/saaavsaaa/client/utility/StringUtil.java b/src/main/java/com/saaavsaaa/client/utility/StringUtil.java index 0bbe795320312..c26a5fdfab88d 100644 --- a/src/main/java/com/saaavsaaa/client/utility/StringUtil.java +++ b/src/main/java/com/saaavsaaa/client/utility/StringUtil.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.utility; /** - * Created by aaa on 18-4-18. + * Created by aaa */ public class StringUtil { public static boolean isNullOrBlank(String string) { diff --git a/src/main/java/com/saaavsaaa/client/utility/constant/Constants.java b/src/main/java/com/saaavsaaa/client/utility/constant/Constants.java index f907046f666f9..23d80e6ebb67b 100644 --- a/src/main/java/com/saaavsaaa/client/utility/constant/Constants.java +++ b/src/main/java/com/saaavsaaa/client/utility/constant/Constants.java @@ -3,7 +3,7 @@ import java.nio.charset.Charset; /** - * Created by aaa on 18-4-25. + * Created by aaa */ public final class Constants { public static final int VERSION = -1; diff --git a/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java b/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java index ef808ce942b9f..55d3289b6918e 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java +++ b/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java @@ -4,7 +4,7 @@ import org.apache.zookeeper.KeeperException; /** - * Created by aaa on 18-4-27. + * Created by aaa */ public abstract class ClientTask implements Runnable { private final Client client; diff --git a/src/main/java/com/saaavsaaa/client/utility/section/Listener.java b/src/main/java/com/saaavsaaa/client/utility/section/Listener.java index 3fa9d2adca2c2..879d29ede6057 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/Listener.java +++ b/src/main/java/com/saaavsaaa/client/utility/section/Listener.java @@ -3,7 +3,7 @@ import org.apache.zookeeper.WatchedEvent; /** - * Created by aaa on 18-4-23. + * Created by aaa */ public interface Listener { void process(WatchedEvent event); diff --git a/src/main/java/com/saaavsaaa/client/utility/section/Property.java b/src/main/java/com/saaavsaaa/client/utility/section/Property.java index 1163c083055ca..f335bb10f3be4 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/Property.java +++ b/src/main/java/com/saaavsaaa/client/utility/section/Property.java @@ -5,7 +5,7 @@ import java.util.ResourceBundle; /** - * Created by aaa on 18-4-27. + * Created by aaa */ public enum Property { INSTANCE; diff --git a/src/main/java/com/saaavsaaa/client/utility/section/WatcherCreator.java b/src/main/java/com/saaavsaaa/client/utility/section/WatcherCreator.java index 47142c3b31df7..f96b2157d4f53 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/WatcherCreator.java +++ b/src/main/java/com/saaavsaaa/client/utility/section/WatcherCreator.java @@ -6,7 +6,7 @@ import static org.apache.zookeeper.Watcher.Event.EventType.NodeDeleted; /** - * Created by aaa on 18-4-27. + * Created by aaa */ public class WatcherCreator { public static Watcher deleteWatcher(final String path, Listener listener){ diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index c1433fecc53db..06ff7d18b2dfc 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -97,7 +97,7 @@ private void createBegin(final String key, final String value, final CreateMode List nodes = PathUtil.getPathOrderNodes(rootNode, key); for (int i = 0; i < nodes.size(); i++) { - // todo not goog + // todo not good if (super.checkExists(nodes.get(i))){ System.out.println("exist:" + nodes.get(i)); continue; @@ -117,12 +117,6 @@ private Transaction createInTransaction(final String key, byte[] data, final Cre return transaction.create(PathUtil.getRealPath(rootNode, key), data, authorities, createMode); } - /* - * closed beta - * 当前实现方法用于缓存方式 - * 缓存实现后此类判断换为异常方式(包括创建) - * 用事务不能用异常 - */ @Override public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { Transaction transaction = zooKeeper.transaction(); @@ -131,10 +125,11 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup } private void deleteAllChildren(final String key, final Transaction transaction) throws KeeperException, InterruptedException { + } private void deleteOnlyCurrent(final String key, final Transaction transaction) throws KeeperException, InterruptedException { - zooKeeper.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION); + transaction.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index fca62ac50daf9..c93c327399294 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -16,7 +16,7 @@ import java.util.concurrent.CountDownLatch; /** - * Created by aaa on 18-4-19. + * Created by aaa */ public abstract class Client implements IClient{ private static final CountDownLatch CONNECTED = new CountDownLatch(1); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 5aa822940b9a0..fc780ab3cc74c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -7,7 +7,7 @@ import java.io.IOException; /** - * Created by aaa on 18-4-18. + * Created by aaa */ public class ClientFactory { // private static final String CLIENT_EXCLUSIVE_NODE = "ZKC"; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/IClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/IClient.java index 0afaadf916f19..b98c58ce350f8 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/IClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/IClient.java @@ -5,7 +5,7 @@ import java.util.List; /** - * Created by aaa on 18-4-26. + * Created by aaa */ public interface IClient { String getDataString(final String key) throws KeeperException, InterruptedException ; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java index 9a5843dd1ce2a..12f32fff33116 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.zookeeper; /** - * Created by aaa on 18-4-27. + * Created by aaa * 及时缓存 watcher */ public class TimelyCacheClient { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 85c1b77f9c6fb..e620fd01a40a4 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -1,6 +1,6 @@ package com.saaavsaaa.client.zookeeper; /** - * Created by aaa on 18-4-18. + * Created by aaa */ import com.saaavsaaa.client.utility.constant.Constants; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java index 62570b144b1be..f6ba3797993a6 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java @@ -18,7 +18,7 @@ import java.util.concurrent.CountDownLatch; /** - * Created by aaa on 18-4-26. + * Created by aaa */ public class BaseClientTest { protected Client testClient = null; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java index 1fe089d4342b4..4d14204f59988 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java @@ -7,7 +7,7 @@ import java.io.IOException; /** - * Created by aaa on 18-4-20. + * Created by aaa */ public class CacheClientTest { private Client client = null; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java index 974ecc91ec3f0..ffbd67a61c0dc 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java @@ -8,7 +8,7 @@ import java.util.List; /** - * Created by aaa on 18-4-26. + * Created by aaa */ public class ClientsTest extends BaseClientTest { private List clients; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java b/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java index 41d40f45a0289..43d48d13fb692 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java @@ -4,7 +4,7 @@ import org.apache.zookeeper.WatchedEvent; /** - * Created by aaa on 18-4-26. + * Created by aaa */ public class TestSupport { static final String AUTH = "digest"; From b17694cf4493727cdcb3a336195a386dd7af13f9 Mon Sep 17 00:00:00 2001 From: saaav Date: Sat, 28 Apr 2018 18:55:15 +0800 Subject: [PATCH 056/351] version --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index f7d140472b2ed..4254cd5abb824 100644 --- a/build.gradle +++ b/build.gradle @@ -16,8 +16,8 @@ group 'com.saaavsaaa' -version '1.0-SNAPSHOT' -//version '1.1' +//version '1.0-SNAPSHOT' +version '1.2' apply plugin: 'idea' apply plugin: 'java' From 3f123bd6dacfce7ba9ffaa920000d6954d1d1c85 Mon Sep 17 00:00:00 2001 From: saaav Date: Sun, 29 Apr 2018 11:40:28 +0800 Subject: [PATCH 057/351] javadoc --- src/main/java/com/saaavsaaa/client/cache/PathNode.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/saaavsaaa/client/cache/PathNode.java b/src/main/java/com/saaavsaaa/client/cache/PathNode.java index 613934303b1fe..7bc699cd131ba 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathNode.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathNode.java @@ -8,7 +8,7 @@ /** * Created by aaa - * todo nodeKey can use current node short path: get children.containsKey(path) should be change -> /root/aaa/aaa + * todo nodeKey can use current node short path: get children.containsKey(path) should be change ,because path can like /root/aaa/aaa */ public class PathNode { private final String nodeKey; From 7d6d435b1da821319ab15f426d4598bbe4a71ef6 Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 2 May 2018 15:59:03 +0800 Subject: [PATCH 058/351] test --- .../client/zookeeper/CacheClient.java | 99 ++++++++++++------- .../client/zookeeper/TimelyCacheClient.java | 2 + .../zookeeper/transaction/Transaction.java | 8 ++ .../client/zookeeper/BaseClientTest.java | 14 +-- .../client/zookeeper/CacheClientTest.java | 5 +- .../client/zookeeper/ClientsTest.java | 35 +++++-- .../client/zookeeper/UsualClientTest.java | 8 ++ .../zookeeper/UsualWatchClientTest.java | 79 +++++++++++++++ 8 files changed, 192 insertions(+), 58 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/transaction/Transaction.java create mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 06ff7d18b2dfc..1d4dbc83986f8 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -1,6 +1,5 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.cache.PathNode; import com.saaavsaaa.client.cache.PathStatus; import com.saaavsaaa.client.cache.PathTree; import com.saaavsaaa.client.election.LeaderElection; @@ -8,24 +7,21 @@ import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.constant.Properties; import com.saaavsaaa.client.utility.section.ClientTask; -import com.saaavsaaa.client.utility.section.Listener; -import org.apache.zookeeper.*; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; import java.io.IOException; import java.util.List; -import java.util.Map; import java.util.Stack; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import java.util.function.Supplier; /** * Created by aaa * todo restructure the three clients to strategies * todo log - * todo currentNodes */ public final class CacheClient extends UsualClient { private final ScheduledExecutorService cacheService = Executors.newSingleThreadScheduledExecutor(); @@ -93,68 +89,95 @@ private void createBegin(final String key, final String value, final CreateMode super.createCurrentOnly(key, value, createMode); return; } - Transaction transaction = zooKeeper.transaction(); List nodes = PathUtil.getPathOrderNodes(rootNode, key); for (int i = 0; i < nodes.size(); i++) { - // todo not good if (super.checkExists(nodes.get(i))){ - System.out.println("exist:" + nodes.get(i)); + System.out.println("create :" + nodes.get(i)); continue; } - System.out.println("not exist:" + nodes.get(i)); + System.out.println("create not exist:" + nodes.get(i)); if (i == nodes.size() - 1){ - createInTransaction(nodes.get(i), value.getBytes(Constants.UTF_8), createMode, transaction); + super.createCurrentOnly(nodes.get(i), value, createMode); } else { - createInTransaction(nodes.get(i), Constants.NOTHING_DATA, createMode, transaction); + this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); } } - - transaction.commit(); - } - - private Transaction createInTransaction(final String key, byte[] data, final CreateMode createMode, final Transaction transaction){ - return transaction.create(PathUtil.getRealPath(rootNode, key), data, authorities, createMode); } + @Override public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { - Transaction transaction = zooKeeper.transaction(); - this.deleteAllChildren(key, transaction); - transaction.commit(); - } - - private void deleteAllChildren(final String key, final Transaction transaction) throws KeeperException, InterruptedException { + LeaderElection election = new LeaderElection() { + @Override + public void actionWhenUnreached() throws KeeperException, InterruptedException { + deleteAllChildren(key); + } + @Override + public void action() throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(rootNode, key); + deleteChildren(path); + pathTree.delete(path); + } + }; + election.executeContention(rootNode, this); } - private void deleteOnlyCurrent(final String key, final Transaction transaction) throws KeeperException, InterruptedException { - transaction.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION); + private void deleteChildren(final String key) throws KeeperException, InterruptedException { + List children = super.getChildren(key); + if (children.isEmpty()){ + if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ + super.deleteOnlyCurrent(key); + return; + } + return; + } + for (int i = 0; i < children.size(); i++) { + if (!super.checkExists(children.get(i))){ + System.out.println("delete not exist:" + children.get(i)); + continue; + } + this.deleteChildren(key); + } } - @Override public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - this.deleteOnlyCurrent(key); - return; - } - Transaction transaction = zooKeeper.transaction(); - //todo branch check + LeaderElection election = new LeaderElection() { + @Override + public void actionWhenUnreached() throws KeeperException, InterruptedException { + deleteCurrentBranch(key); + } + + @Override + public void action() throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(rootNode, key); + try { + deleteBranch(path); + } catch (KeeperException.NotEmptyException ee){ + System.out.println(path + " exist other children"); + pathTree.delete(path); + return; + } + } + }; + election.executeContention(rootNode, this); + } + + private void deleteBranch(String key) throws KeeperException, InterruptedException { + deleteChildren(PathUtil.getRealPath(rootNode, key)); Stack pathStack = PathUtil.getPathReverseNodes(rootNode, key); while (!pathStack.empty()){ String node = pathStack.pop(); // contrast cache if (checkExists(node)){ - transaction.delete(node, Constants.VERSION); + super.deleteOnlyCurrent(key); System.out.println("delete : " + node); } } - transaction.commit(); } - //=========================================================================================================== - private boolean cacheReady(){ return PathStatus.RELEASE == pathTree.getStatus(); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java index 12f32fff33116..ee3696a3c4e2c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java @@ -5,4 +5,6 @@ * 及时缓存 watcher */ public class TimelyCacheClient { + //锁定缓存 + // 从低广度遍历 } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/Transaction.java b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/Transaction.java new file mode 100644 index 0000000000000..3899544432be8 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/Transaction.java @@ -0,0 +1,8 @@ +package com.saaavsaaa.client.zookeeper.transaction; + +/** + * Created by aaa + * todo transaction api + */ +public class Transaction { +} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java index f6ba3797993a6..5049f61a4d82c 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java @@ -20,24 +20,16 @@ /** * Created by aaa */ -public class BaseClientTest { +public abstract class BaseClientTest { protected Client testClient = null; @Before public void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); -// testClient = createClient(creator); - testClient = createWatchClient(creator); + testClient = createClient(creator); } - protected Client createClient(ClientFactory creator) throws IOException, InterruptedException { - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); - } - - protected Client createWatchClient(ClientFactory creator) throws IOException, InterruptedException { - Listener listener = TestSupport.buildListener(); - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - } + protected abstract Client createClient(ClientFactory creator) throws IOException, InterruptedException; @After public void stop() throws InterruptedException { diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java index 4d14204f59988..52a94debb0e73 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java @@ -12,14 +12,13 @@ public class CacheClientTest { private Client client = null; - // @Before - public void start() throws IOException, InterruptedException { + public void createClient() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); } @Before - public void startWithWatch() throws IOException, InterruptedException { + public void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); Listener listener = TestSupport.buildListener(); client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java index ffbd67a61c0dc..cdb8f402664ba 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.utility.section.Listener; import org.apache.zookeeper.KeeperException; import org.junit.Test; @@ -12,16 +13,38 @@ */ public class ClientsTest extends BaseClientTest { private List clients; + private final int count = 5; + private final int shard = 2; @Override public void start() throws IOException, InterruptedException { - clients = new ArrayList<>(5); + clients = new ArrayList<>(count); ClientFactory creator = new ClientFactory(); - clients.add(createClient(creator)); - clients.add(createClient(creator)); - clients.add(createClient(creator)); - clients.add(createClient(creator)); - clients.add(createWatchClient(creator)); + for (int i = 0; i < count; i++) { + clients.add(createClient(creator)); + } + } + + @Override + protected Client createClient(ClientFactory creator) throws IOException, InterruptedException { + Client client; + if (clients.size() % shard == 1){ + System.out.println("create client"); + client = newClient(creator); + } else { + System.out.println("create watch client"); + client = newWatchClient(creator); + } + return client; + } + + private Client newClient(ClientFactory creator) throws IOException, InterruptedException { + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); + } + + protected Client newWatchClient(ClientFactory creator) throws IOException, InterruptedException { + Listener listener = TestSupport.buildListener(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); } @Override diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index 14894259e1de5..38e3f4f031cce 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -1,13 +1,21 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.utility.section.Listener; import org.apache.zookeeper.KeeperException; import org.junit.Test; +import java.io.IOException; + /** * Created by aaa */ public class UsualClientTest extends BaseClientTest { + @Override + protected Client createClient(final ClientFactory creator) throws IOException, InterruptedException { + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); + } + @Test public void createRoot() throws KeeperException, InterruptedException { super.createRoot(testClient); diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java new file mode 100644 index 0000000000000..ce117ded980f5 --- /dev/null +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java @@ -0,0 +1,79 @@ +package com.saaavsaaa.client.zookeeper; + +import com.saaavsaaa.client.utility.section.Listener; +import org.apache.zookeeper.KeeperException; +import org.junit.Test; + +import java.io.IOException; + +/** + * Created by aaa + */ +public class UsualWatchClientTest extends BaseClientTest { + + @Override + protected Client createClient(final ClientFactory creator) throws IOException, InterruptedException { + Listener listener = TestSupport.buildListener(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + } + + @Test + public void createRoot() throws KeeperException, InterruptedException { + super.createRoot(testClient); + } + + @Test + public void createChild() throws KeeperException, InterruptedException { + super.createChild(testClient); + } + + @Test + public void deleteBranch() throws KeeperException, InterruptedException { + super.deleteBranch(testClient); + } + + @Test + public void isExisted() throws KeeperException, InterruptedException { + super.isExisted(testClient); + } + + @Test + public void get() throws KeeperException, InterruptedException { + super.get(testClient); + } + + @Test + public void asynGet() throws KeeperException, InterruptedException { + super.asynGet(testClient); + } + + @Test + public void getChildrenKeys() throws KeeperException, InterruptedException { + super.getChildrenKeys(testClient); + } + + @Test + public void persist() throws KeeperException, InterruptedException { + super.persist(testClient); + } + + @Test + public void persistEphemeral() throws KeeperException, InterruptedException { + super.persistEphemeral(testClient); + } + + @Test + public void delAllChildren() throws KeeperException, InterruptedException { + super.delAllChildren(testClient); + } + + @Test + public void watch() throws KeeperException, InterruptedException { + super.watch(testClient); + } + + @Test + public void close() throws Exception { + super.close(testClient); + } +} \ No newline at end of file From 3e91e928e7d6d553fc2ca03757e1238093a56b71 Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 2 May 2018 16:05:51 +0800 Subject: [PATCH 059/351] test --- .../zookeeper/UsualWatchClientTest.java | 62 +------------------ 1 file changed, 1 insertion(+), 61 deletions(-) diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java index ce117ded980f5..5fecbcf9a9394 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java @@ -9,71 +9,11 @@ /** * Created by aaa */ -public class UsualWatchClientTest extends BaseClientTest { +public class UsualWatchClientTest extends UsualClientTest { @Override protected Client createClient(final ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); } - - @Test - public void createRoot() throws KeeperException, InterruptedException { - super.createRoot(testClient); - } - - @Test - public void createChild() throws KeeperException, InterruptedException { - super.createChild(testClient); - } - - @Test - public void deleteBranch() throws KeeperException, InterruptedException { - super.deleteBranch(testClient); - } - - @Test - public void isExisted() throws KeeperException, InterruptedException { - super.isExisted(testClient); - } - - @Test - public void get() throws KeeperException, InterruptedException { - super.get(testClient); - } - - @Test - public void asynGet() throws KeeperException, InterruptedException { - super.asynGet(testClient); - } - - @Test - public void getChildrenKeys() throws KeeperException, InterruptedException { - super.getChildrenKeys(testClient); - } - - @Test - public void persist() throws KeeperException, InterruptedException { - super.persist(testClient); - } - - @Test - public void persistEphemeral() throws KeeperException, InterruptedException { - super.persistEphemeral(testClient); - } - - @Test - public void delAllChildren() throws KeeperException, InterruptedException { - super.delAllChildren(testClient); - } - - @Test - public void watch() throws KeeperException, InterruptedException { - super.watch(testClient); - } - - @Test - public void close() throws Exception { - super.close(testClient); - } } \ No newline at end of file From 8f0a750455d81b4f006c5b8c73d0ee1589381c77 Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 2 May 2018 16:58:18 +0800 Subject: [PATCH 060/351] test --- .../client/zookeeper/CacheClient.java | 2 +- .../saaavsaaa/client/zookeeper/Client.java | 2 +- .../client/zookeeper/CacheClientTest.java | 23 ++++--------------- .../client/zookeeper/CacheWathClientTest.java | 16 +++++++++++++ 4 files changed, 22 insertions(+), 21 deletions(-) create mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 1d4dbc83986f8..b34b71be4d59c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -34,7 +34,7 @@ public final class CacheClient extends UsualClient { } @Override - public void start() throws IOException, InterruptedException { + public synchronized void start() throws IOException, InterruptedException { super.start(); pathTree = new PathTree(rootNode); usualClient = this.getClientFactory().newUsualClient().start(); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index c93c327399294..225813b24a33d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -38,7 +38,7 @@ protected Client(String servers, int sessionTimeoutMilliseconds) { this.sessionTimeOut = sessionTimeoutMilliseconds; } - public void start() throws IOException, InterruptedException { + public synchronized void start() throws IOException, InterruptedException { zooKeeper = new ZooKeeper(servers, sessionTimeOut, startWatcher()); CONNECTED.await(); } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java index 52a94debb0e73..ce8e5d30df352 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java @@ -9,24 +9,9 @@ /** * Created by aaa */ -public class CacheClientTest { - private Client client = null; - - public void createClient() throws IOException, InterruptedException { - ClientFactory creator = new ClientFactory(); - client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); - } - - @Before - public void start() throws IOException, InterruptedException { - ClientFactory creator = new ClientFactory(); - Listener listener = TestSupport.buildListener(); - client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - } - - @After - public void stop() throws InterruptedException { - client.close(); - client = null; +public class CacheClientTest extends UsualClientTest { + @Override + protected Client createClient(ClientFactory creator) throws IOException, InterruptedException { + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java new file mode 100644 index 0000000000000..aca09affd86d8 --- /dev/null +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java @@ -0,0 +1,16 @@ +package com.saaavsaaa.client.zookeeper; + +import com.saaavsaaa.client.utility.section.Listener; + +import java.io.IOException; + +/** + * Created by aaa on 18-5-2. + */ +public class CacheWathClientTest extends UsualClientTest { + @Override + protected Client createClient(ClientFactory creator) throws IOException, InterruptedException { + Listener listener = TestSupport.buildListener(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + } +} From 65e335e9f8c6d1991f8081b28fcb501c5c8e871b Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 2 May 2018 17:23:39 +0800 Subject: [PATCH 061/351] test --- .../client/election/LeaderElection.java | 4 +- .../client/utility/constant/Properties.java | 11 ---- .../client/utility/section/Properties.java | 55 +++++++++++++++++++ .../client/utility/section/Property.java | 22 -------- .../client/zookeeper/CacheClient.java | 4 +- .../saaavsaaa/client/zookeeper/Client.java | 4 +- src/main/resources/client.properties | 5 +- 7 files changed, 65 insertions(+), 40 deletions(-) delete mode 100644 src/main/java/com/saaavsaaa/client/utility/constant/Properties.java create mode 100644 src/main/java/com/saaavsaaa/client/utility/section/Properties.java delete mode 100644 src/main/java/com/saaavsaaa/client/utility/section/Property.java diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index 6c343cd405b44..a98fade14a848 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -3,7 +3,7 @@ import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.Listener; -import com.saaavsaaa.client.utility.section.Property; +import com.saaavsaaa.client.utility.section.Properties; import com.saaavsaaa.client.utility.section.WatcherCreator; import com.saaavsaaa.client.zookeeper.Client; import org.apache.zookeeper.CreateMode; @@ -18,7 +18,7 @@ public abstract class LeaderElection { private boolean contend(final String path, final Client client, final Listener listener) throws KeeperException, InterruptedException { boolean success = false; try { - client.createCurrentOnly(Constants.CHANGING_KEY, Property.INSTANCE.getClientId(), CreateMode.EPHEMERAL); + client.createCurrentOnly(Constants.CHANGING_KEY, Properties.INSTANCE.getClientId(), CreateMode.EPHEMERAL); success = true; } catch (KeeperException.NodeExistsException e) { // TODO: or changing_key node value == current client id diff --git a/src/main/java/com/saaavsaaa/client/utility/constant/Properties.java b/src/main/java/com/saaavsaaa/client/utility/constant/Properties.java deleted file mode 100644 index 0a7f18767eb10..0000000000000 --- a/src/main/java/com/saaavsaaa/client/utility/constant/Properties.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.saaavsaaa.client.utility.constant; - -/** - * Created by aaa - * todo replace by section.Property - */ -public final class Properties { - public static final boolean WATCH_ON = true; - public static final long THREAD_INITIAL_DELAY = 1000; - public static final long THREAD_PERIOD = 3000; -} diff --git a/src/main/java/com/saaavsaaa/client/utility/section/Properties.java b/src/main/java/com/saaavsaaa/client/utility/section/Properties.java new file mode 100644 index 0000000000000..f9e9e43c9464e --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/utility/section/Properties.java @@ -0,0 +1,55 @@ +package com.saaavsaaa.client.utility.section; + +import com.saaavsaaa.client.utility.StringUtil; + +import java.util.ResourceBundle; + +/** + * Created by aaa + */ +public enum Properties { + INSTANCE; + + private final ResourceBundle bundle; + + private Properties(){ + bundle = ResourceBundle.getBundle("client"); + } + + public String getClientId(){ + // ResourceBundle caches the value in Thread + String clientId = bundle.getString("client.id"); + if (StringUtil.isNullOrBlank(clientId)){ + System.out.println("client.id doesn't exist"); + throw new IllegalArgumentException("client.id doesn't exist"); + } + return clientId; + } + + public boolean watchOn(){ + String result = bundle.getString("client.watch.on"); + if (StringUtil.isNullOrBlank(result)){ + System.out.println("client.watch.on doesn't exist"); + throw new IllegalArgumentException("client.watch.on doesn't exist"); + } + return "up".equals(result); + } + + public long getThreadInitialDelay(){ + String result = bundle.getString("client.thread.delay"); + if (StringUtil.isNullOrBlank(result)){ + System.out.println("client.thread.delay doesn't exist"); + throw new IllegalArgumentException("client.thread.delay doesn't exist"); + } + return Long.valueOf(result); + } + + public long getThreadPeriod(){ + String result = bundle.getString("client.thread.period"); + if (StringUtil.isNullOrBlank(result)){ + System.out.println("client.thread.period doesn't exist"); + throw new IllegalArgumentException("client.thread.period doesn't exist"); + } + return Long.valueOf(result); + } +} diff --git a/src/main/java/com/saaavsaaa/client/utility/section/Property.java b/src/main/java/com/saaavsaaa/client/utility/section/Property.java deleted file mode 100644 index f335bb10f3be4..0000000000000 --- a/src/main/java/com/saaavsaaa/client/utility/section/Property.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.saaavsaaa.client.utility.section; - -import com.saaavsaaa.client.utility.StringUtil; - -import java.util.ResourceBundle; - -/** - * Created by aaa - */ -public enum Property { - INSTANCE; - - public String getClientId(){ - // ResourceBundle caches the value in Thread - ResourceBundle bundle = ResourceBundle.getBundle("client"); - String clientId = bundle.getString("client.id"); - if (StringUtil.isNullOrBlank(clientId)){ - throw new IllegalArgumentException("client.id doesn't exist"); - } - return clientId; - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index b34b71be4d59c..151791d96b26e 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -5,8 +5,8 @@ import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.utility.constant.Properties; import com.saaavsaaa.client.utility.section.ClientTask; +import com.saaavsaaa.client.utility.section.Properties; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -45,7 +45,7 @@ public void run(Client client) throws KeeperException, InterruptedException { loadCache(client); } } - }, Properties.THREAD_INITIAL_DELAY, Properties.THREAD_PERIOD, TimeUnit.MILLISECONDS); + }, Properties.INSTANCE.getThreadInitialDelay(), Properties.INSTANCE.getThreadPeriod(), TimeUnit.MILLISECONDS); } //用替换整树的方式更新 diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index 225813b24a33d..ede9f4ba0360f 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -3,8 +3,8 @@ import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.StringUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.utility.constant.Properties; import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.utility.section.Properties; import com.saaavsaaa.client.utility.section.WatcherCreator; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; @@ -58,7 +58,7 @@ public void process(WatchedEvent event) { if (globalListenerRegistered){ watchers.get(Constants.GLOBAL_LISTENER_KEY).process(event); } - if (Properties.WATCH_ON && watchers.containsKey(event.getPath())){ + if (Properties.INSTANCE.watchOn() && watchers.containsKey(event.getPath())){ watchers.get(event.getPath()).process(event); } } diff --git a/src/main/resources/client.properties b/src/main/resources/client.properties index 4cb3754f82519..dd54fbac2701b 100644 --- a/src/main/resources/client.properties +++ b/src/main/resources/client.properties @@ -1 +1,4 @@ -client.id=1 \ No newline at end of file +client.id=1 +client.watch.on=up +client.thread.delay=1000 +client.thread.period=3000 \ No newline at end of file From 3c837642ff95ee4bc6b9598bba6b0675638b3b3a Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 2 May 2018 18:14:53 +0800 Subject: [PATCH 062/351] re create --- .../client/zookeeper/CacheAllClient.java | 267 ++++++++++++++++++ .../client/zookeeper/CacheClient.java | 265 +---------------- 2 files changed, 270 insertions(+), 262 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java new file mode 100644 index 0000000000000..dbc8321847ff5 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java @@ -0,0 +1,267 @@ +package com.saaavsaaa.client.zookeeper; + +import com.saaavsaaa.client.cache.PathStatus; +import com.saaavsaaa.client.cache.PathTree; +import com.saaavsaaa.client.election.LeaderElection; +import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.utility.section.ClientTask; +import com.saaavsaaa.client.utility.section.Properties; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; + +import java.io.IOException; +import java.util.List; +import java.util.Stack; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * Created by aaa + */ +@Deprecated +public final class CacheAllClient extends UsualClient { + private final ScheduledExecutorService cacheService = Executors.newSingleThreadScheduledExecutor(); + private Client usualClient; + + protected PathTree pathTree = null; + + CacheAllClient(String servers, int sessionTimeoutMilliseconds) { + super(servers, sessionTimeoutMilliseconds); + } + + @Override + public synchronized void start() throws IOException, InterruptedException { + super.start(); + pathTree = new PathTree(rootNode); + usualClient = this.getClientFactory().newUsualClient().start(); + cacheService.scheduleAtFixedRate(new ClientTask(usualClient) { + @Override + public void run(Client client) throws KeeperException, InterruptedException { + if (PathStatus.RELEASE == pathTree.getStatus()) { + loadCache(client); + } + } + }, Properties.INSTANCE.getThreadInitialDelay(), Properties.INSTANCE.getThreadPeriod(), TimeUnit.MILLISECONDS); + } + + //用替换整树的方式更新 + private synchronized void loadCache(final Client client) throws KeeperException, InterruptedException { + LeaderElection election = new LeaderElection() { + @Override + public void actionWhenUnreached() throws KeeperException, InterruptedException { + loadCache(client); + } + + @Override + public void action() throws KeeperException, InterruptedException { + pathTree.loading(client); + } + }; + election.executeContention(rootNode, client); + } + + /* + * closed beta + */ + @Override + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + LeaderElection election = new LeaderElection() { + @Override + public void actionWhenUnreached() throws KeeperException, InterruptedException { + createAllNeedPath(key, value, createMode); + } + + @Override + public void action() throws KeeperException, InterruptedException { + createBegin(key, value, createMode); + } + }; + election.executeContention(rootNode, this); + } + + + private void createBegin(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ + super.createCurrentOnly(key, value, createMode); + return; + } + + List nodes = PathUtil.getPathOrderNodes(rootNode, key); + for (int i = 0; i < nodes.size(); i++) { + if (super.checkExists(nodes.get(i))){ + System.out.println("create :" + nodes.get(i)); + continue; + } + System.out.println("create not exist:" + nodes.get(i)); + if (i == nodes.size() - 1){ + super.createCurrentOnly(nodes.get(i), value, createMode); + } else { + this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); + } + } + } + + + @Override + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { + LeaderElection election = new LeaderElection() { + @Override + public void actionWhenUnreached() throws KeeperException, InterruptedException { + deleteAllChildren(key); + } + + @Override + public void action() throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(rootNode, key); + deleteChildren(path); + pathTree.delete(path); + } + }; + election.executeContention(rootNode, this); + } + + private void deleteChildren(final String key) throws KeeperException, InterruptedException { + List children = super.getChildren(key); + if (children.isEmpty()){ + if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ + super.deleteOnlyCurrent(key); + return; + } + return; + } + for (int i = 0; i < children.size(); i++) { + if (!super.checkExists(children.get(i))){ + System.out.println("delete not exist:" + children.get(i)); + continue; + } + this.deleteChildren(key); + } + } + + @Override + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + LeaderElection election = new LeaderElection() { + @Override + public void actionWhenUnreached() throws KeeperException, InterruptedException { + deleteCurrentBranch(key); + } + + @Override + public void action() throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(rootNode, key); + try { + deleteBranch(path); + } catch (KeeperException.NotEmptyException ee){ + System.out.println(path + " exist other children"); + pathTree.delete(path); + return; + } + } + }; + election.executeContention(rootNode, this); + } + + private void deleteBranch(String key) throws KeeperException, InterruptedException { + deleteChildren(PathUtil.getRealPath(rootNode, key)); + Stack pathStack = PathUtil.getPathReverseNodes(rootNode, key); + while (!pathStack.empty()){ + String node = pathStack.pop(); + // contrast cache + if (checkExists(node)){ + super.deleteOnlyCurrent(key); + System.out.println("delete : " + node); + } + } + } + + private boolean cacheReady(){ + return PathStatus.RELEASE == pathTree.getStatus(); + } + + @Override + public byte[] getData(final String key) throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(rootNode, key); + if (cacheReady()){ + return pathTree.getValue(path); + } + // without watcher ensure cache execute result consistency + byte[] data = zooKeeper.getData(path, false, null); + pathTree.put(path, new String(data)); + return data; + } + + @Override + public boolean checkExists(final String key) throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(rootNode, key); + if (cacheReady()){ + return null != pathTree.getValue(path); + } + return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), false); + } + + @Override + public List getChildren(final String key) throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(rootNode, key); + if (cacheReady()){ + return pathTree.getChildren(path); + } + return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { +// super.createCurrentOnly(key, value, createMode); + LeaderElection election = new LeaderElection() { + @Override + public void actionWhenUnreached() throws KeeperException, InterruptedException { + createCurrentOnly(key, value, createMode); + } + + @Override + public void action() throws KeeperException, InterruptedException { + usualClient.createCurrentOnly(key, value, createMode); + } + }; + election.executeContention(rootNode, this); + pathTree.put(PathUtil.getRealPath(rootNode, key), value); + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { +// super.deleteOnlyCurrent(key); + LeaderElection election = new LeaderElection() { + @Override + public void actionWhenUnreached() throws KeeperException, InterruptedException { + deleteOnlyCurrent(key); + } + + @Override + public void action() throws KeeperException, InterruptedException { + usualClient.deleteOnlyCurrent(key); + } + }; + election.executeContention(rootNode, this); + pathTree.delete(PathUtil.getRealPath(rootNode, key)); + } + + @Override + public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { +// super.deleteOnlyCurrent(key, callback, ctx); + LeaderElection election = new LeaderElection() { + @Override + public void actionWhenUnreached() throws KeeperException, InterruptedException { + deleteOnlyCurrent(key, callback, ctx); + } + + @Override + public void action() throws KeeperException, InterruptedException { + usualClient.deleteOnlyCurrent(key, callback, ctx); + } + }; + election.executeContention(rootNode, this); + pathTree.delete(PathUtil.getRealPath(rootNode, key)); + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 151791d96b26e..8c6b00223858b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -1,268 +1,9 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.cache.PathStatus; -import com.saaavsaaa.client.cache.PathTree; -import com.saaavsaaa.client.election.LeaderElection; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.utility.section.ClientTask; -import com.saaavsaaa.client.utility.section.Properties; -import org.apache.zookeeper.AsyncCallback; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; - -import java.io.IOException; -import java.util.List; -import java.util.Stack; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - /** - * Created by aaa - * todo restructure the three clients to strategies + * Created by aaa on 18-5-2. + * todo restructure the three clients to strategies * todo log */ -public final class CacheClient extends UsualClient { - private final ScheduledExecutorService cacheService = Executors.newSingleThreadScheduledExecutor(); - private Client usualClient; - - protected PathTree pathTree = null; - - CacheClient(String servers, int sessionTimeoutMilliseconds) { - super(servers, sessionTimeoutMilliseconds); - } - - @Override - public synchronized void start() throws IOException, InterruptedException { - super.start(); - pathTree = new PathTree(rootNode); - usualClient = this.getClientFactory().newUsualClient().start(); - cacheService.scheduleAtFixedRate(new ClientTask(usualClient) { - @Override - public void run(Client client) throws KeeperException, InterruptedException { - if (PathStatus.RELEASE == pathTree.getStatus()) { - loadCache(client); - } - } - }, Properties.INSTANCE.getThreadInitialDelay(), Properties.INSTANCE.getThreadPeriod(), TimeUnit.MILLISECONDS); - } - - //用替换整树的方式更新 - private synchronized void loadCache(final Client client) throws KeeperException, InterruptedException { - LeaderElection election = new LeaderElection() { - @Override - public void actionWhenUnreached() throws KeeperException, InterruptedException { - loadCache(client); - } - - @Override - public void action() throws KeeperException, InterruptedException { - pathTree.loading(client); - } - }; - election.executeContention(rootNode, client); - } - - /* - * closed beta - */ - @Override - public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - LeaderElection election = new LeaderElection() { - @Override - public void actionWhenUnreached() throws KeeperException, InterruptedException { - createAllNeedPath(key, value, createMode); - } - - @Override - public void action() throws KeeperException, InterruptedException { - createBegin(key, value, createMode); - } - }; - election.executeContention(rootNode, this); - } - - - private void createBegin(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - super.createCurrentOnly(key, value, createMode); - return; - } - - List nodes = PathUtil.getPathOrderNodes(rootNode, key); - for (int i = 0; i < nodes.size(); i++) { - if (super.checkExists(nodes.get(i))){ - System.out.println("create :" + nodes.get(i)); - continue; - } - System.out.println("create not exist:" + nodes.get(i)); - if (i == nodes.size() - 1){ - super.createCurrentOnly(nodes.get(i), value, createMode); - } else { - this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); - } - } - } - - - @Override - public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { - LeaderElection election = new LeaderElection() { - @Override - public void actionWhenUnreached() throws KeeperException, InterruptedException { - deleteAllChildren(key); - } - - @Override - public void action() throws KeeperException, InterruptedException { - String path = PathUtil.getRealPath(rootNode, key); - deleteChildren(path); - pathTree.delete(path); - } - }; - election.executeContention(rootNode, this); - } - - private void deleteChildren(final String key) throws KeeperException, InterruptedException { - List children = super.getChildren(key); - if (children.isEmpty()){ - if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - super.deleteOnlyCurrent(key); - return; - } - return; - } - for (int i = 0; i < children.size(); i++) { - if (!super.checkExists(children.get(i))){ - System.out.println("delete not exist:" + children.get(i)); - continue; - } - this.deleteChildren(key); - } - } - - @Override - public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - LeaderElection election = new LeaderElection() { - @Override - public void actionWhenUnreached() throws KeeperException, InterruptedException { - deleteCurrentBranch(key); - } - - @Override - public void action() throws KeeperException, InterruptedException { - String path = PathUtil.getRealPath(rootNode, key); - try { - deleteBranch(path); - } catch (KeeperException.NotEmptyException ee){ - System.out.println(path + " exist other children"); - pathTree.delete(path); - return; - } - } - }; - election.executeContention(rootNode, this); - } - - private void deleteBranch(String key) throws KeeperException, InterruptedException { - deleteChildren(PathUtil.getRealPath(rootNode, key)); - Stack pathStack = PathUtil.getPathReverseNodes(rootNode, key); - while (!pathStack.empty()){ - String node = pathStack.pop(); - // contrast cache - if (checkExists(node)){ - super.deleteOnlyCurrent(key); - System.out.println("delete : " + node); - } - } - } - - private boolean cacheReady(){ - return PathStatus.RELEASE == pathTree.getStatus(); - } - - @Override - public byte[] getData(final String key) throws KeeperException, InterruptedException { - String path = PathUtil.getRealPath(rootNode, key); - if (cacheReady()){ - return pathTree.getValue(path); - } - // without watcher ensure cache execute result consistency - byte[] data = zooKeeper.getData(path, false, null); - pathTree.put(path, new String(data)); - return data; - } - - @Override - public boolean checkExists(final String key) throws KeeperException, InterruptedException { - String path = PathUtil.getRealPath(rootNode, key); - if (cacheReady()){ - return null != pathTree.getValue(path); - } - return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), false); - } - - @Override - public List getChildren(final String key) throws KeeperException, InterruptedException { - String path = PathUtil.getRealPath(rootNode, key); - if (cacheReady()){ - return pathTree.getChildren(path); - } - return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); - } - - @Override - public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { -// super.createCurrentOnly(key, value, createMode); - LeaderElection election = new LeaderElection() { - @Override - public void actionWhenUnreached() throws KeeperException, InterruptedException { - createCurrentOnly(key, value, createMode); - } - - @Override - public void action() throws KeeperException, InterruptedException { - usualClient.createCurrentOnly(key, value, createMode); - } - }; - election.executeContention(rootNode, this); - pathTree.put(PathUtil.getRealPath(rootNode, key), value); - } - - @Override - public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { -// super.deleteOnlyCurrent(key); - LeaderElection election = new LeaderElection() { - @Override - public void actionWhenUnreached() throws KeeperException, InterruptedException { - deleteOnlyCurrent(key); - } - - @Override - public void action() throws KeeperException, InterruptedException { - usualClient.deleteOnlyCurrent(key); - } - }; - election.executeContention(rootNode, this); - pathTree.delete(PathUtil.getRealPath(rootNode, key)); - } - - @Override - public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { -// super.deleteOnlyCurrent(key, callback, ctx); - LeaderElection election = new LeaderElection() { - @Override - public void actionWhenUnreached() throws KeeperException, InterruptedException { - deleteOnlyCurrent(key, callback, ctx); - } - - @Override - public void action() throws KeeperException, InterruptedException { - usualClient.deleteOnlyCurrent(key, callback, ctx); - } - }; - election.executeContention(rootNode, this); - pathTree.delete(PathUtil.getRealPath(rootNode, key)); - } +public class CacheClient { } From 3e196c30b017ec9dfb7c52ca1172492fff9ef294 Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 2 May 2018 19:08:53 +0800 Subject: [PATCH 063/351] strategy start --- .../client/zookeeper/BlendClient.java | 103 +++++++++++ .../client/zookeeper/CacheClient.java | 6 +- .../saaavsaaa/client/zookeeper/Client.java | 2 +- .../zookeeper/strategy/BaseStrategy.java | 165 ++++++++++++++++++ .../strategy/ContentionStrategy.java | 7 + .../client/zookeeper/strategy/IStrategy.java | 9 + .../zookeeper/strategy/StrategyType.java | 9 + 7 files changed, 298 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/BlendClient.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/IStrategy.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/BlendClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/BlendClient.java new file mode 100644 index 0000000000000..a3e4c003ba64f --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/BlendClient.java @@ -0,0 +1,103 @@ +package com.saaavsaaa.client.zookeeper; + +import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.zookeeper.strategy.BaseStrategy; +import com.saaavsaaa.client.zookeeper.strategy.IStrategy; +import com.saaavsaaa.client.zookeeper.strategy.StrategyType; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; + +import java.io.IOException; +import java.util.List; + +/** + * Created by aaa on 18-5-2. + */ +public class BlendClient extends Client { + private IStrategy strategy; + + BlendClient(final String servers, final int sessionTimeoutMilliseconds) { + super(servers, sessionTimeoutMilliseconds); + } + + public synchronized void UseStrategy(StrategyType strategyType) { + if (StrategyType.BASE == strategyType){ + strategy = new BaseStrategy(rootNode, zooKeeper, watched, authorities); + } else { + + } + } + + @Override + public String getDataString(final String key) throws KeeperException, InterruptedException { + return strategy.getDataString(PathUtil.getRealPath(rootNode, key)); + } + + @Override + public byte[] getData(final String key) throws KeeperException, InterruptedException { + return strategy.getData(PathUtil.getRealPath(rootNode, key)); + } + + @Override + public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { + strategy.getData(PathUtil.getRealPath(rootNode, key), callback, ctx); + } + + @Override + public boolean checkExists(final String key) throws KeeperException, InterruptedException { + return strategy.checkExists(PathUtil.getRealPath(rootNode, key)); + } + + @Override + public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { + return strategy.checkExists(PathUtil.getRealPath(rootNode, key), watcher); + } + + @Override + public List getChildren(final String key) throws KeeperException, InterruptedException { + return strategy.getChildren(PathUtil.getRealPath(rootNode, key)); + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + createNamespace(); + strategy.createCurrentOnly(PathUtil.getRealPath(rootNode, key), value, createMode); + } + + @Override + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + strategy.createAllNeedPath(PathUtil.getRealPath(rootNode, key), value, createMode); + } + + @Override + public void update(final String key, final String value) throws KeeperException, InterruptedException { + strategy.update(PathUtil.getRealPath(rootNode, key), value); + } + + @Override + public void updateWithCheck(final String key, final String value) throws KeeperException, InterruptedException { + strategy.updateWithCheck(PathUtil.getRealPath(rootNode, key), value); + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + strategy.deleteOnlyCurrent(PathUtil.getRealPath(rootNode, key)); + } + + @Override + public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + strategy.deleteOnlyCurrent(PathUtil.getRealPath(rootNode, key), callback, ctx); + } + + @Override + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { + strategy.deleteAllChildren(PathUtil.getRealPath(rootNode, key)); + } + + @Override + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + strategy.deleteCurrentBranch(PathUtil.getRealPath(rootNode, key)); + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 8c6b00223858b..515963e9e5534 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -2,8 +2,10 @@ /** * Created by aaa on 18-5-2. - * todo restructure the three clients to strategies * todo log */ -public class CacheClient { +public final class CacheClient extends UsualClient { + CacheClient(String servers, int sessionTimeoutMilliseconds) { + super(servers, sessionTimeoutMilliseconds); + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index ede9f4ba0360f..b4dc7808b7566 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -33,7 +33,7 @@ public abstract class Client implements IClient{ protected boolean rootExist = false; private ClientFactory clientFactory; - protected Client(String servers, int sessionTimeoutMilliseconds) { + protected Client(final String servers, final int sessionTimeoutMilliseconds) { this.servers = servers; this.sessionTimeOut = sessionTimeoutMilliseconds; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java new file mode 100644 index 0000000000000..4034610320acf --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java @@ -0,0 +1,165 @@ +package com.saaavsaaa.client.zookeeper.strategy; + +import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; +import org.apache.zookeeper.*; +import org.apache.zookeeper.data.ACL; + +import java.util.List; + +/** + * Created by aaa on 18-5-2. + */ +public class BaseStrategy implements IStrategy { + protected final ZooKeeper zooKeeper; + protected final boolean watched; + protected final List authorities; + protected final String rootNode; + + public BaseStrategy(final String rootNode, final ZooKeeper zooKeeper, final boolean watched, final List authorities){ + this.rootNode = rootNode; + this.zooKeeper = zooKeeper; + this.watched = watched; + this.authorities = authorities; + } + + @Override + public String getDataString(final String key) throws KeeperException, InterruptedException { + return new String(getData(key)); + } + + @Override + public byte[] getData(final String key) throws KeeperException, InterruptedException { + return zooKeeper.getData(key, watched, null); + } + + @Override + public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { + zooKeeper.getData(key, watched, callback, ctx); + } + + @Override + public boolean checkExists(final String key) throws KeeperException, InterruptedException { + return null != zooKeeper.exists(key, watched); + } + + @Override + public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { + return null != zooKeeper.exists(key, watcher); + } + + @Override + public List getChildren(final String key) throws KeeperException, InterruptedException { + return zooKeeper.getChildren(key, watched); + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + if (rootNode.equals(key)){ + return; + } + try { + zooKeeper.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); + } catch (KeeperException.NoNodeException e) { + // I don't know whether it will happen or not, if root watcher don't update rootExist timely + if (e.getMessage().contains(key)) { + System.out.println("rootExist : " + e.getMessage()); + Thread.sleep(50); + this.createCurrentOnly(key, value, createMode); + } + } + } + + @Override + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ + this.createCurrentOnly(key, value, createMode); + return; + } + + List nodes = PathUtil.getPathOrderNodes(rootNode, key); + nodes.remove(rootNode); + for (int i = 0; i < nodes.size(); i++) { + try { + if (i == nodes.size() - 1){ + this.createCurrentOnly(nodes.get(i), value, createMode); + } else { +// this.deleteAllChildren(nodes.get(i)); + this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); + } + System.out.println("not exist and create:" + nodes.get(i)); + } catch (KeeperException.NodeExistsException ee){ + System.out.println("exist:" + nodes.get(i)); + continue; + } + } + } + + @Override + public void update(final String key, final String value) throws KeeperException, InterruptedException { + zooKeeper.setData(key, value.getBytes(Constants.UTF_8), Constants.VERSION); + } + + @Override + public void updateWithCheck(final String key, final String value) throws KeeperException, InterruptedException { + zooKeeper.transaction().check(key, Constants.VERSION).setData(key, value.getBytes(Constants.UTF_8), Constants.VERSION).commit(); + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + zooKeeper.delete(key, Constants.VERSION); + System.out.println("delete : " + key); + } + + @Override + public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + zooKeeper.delete(key, Constants.VERSION, callback, ctx); + } + + @Override + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { + try { + this.deleteOnlyCurrent(key); + }catch (KeeperException.NotEmptyException ee){ + List children = this.getChildren(key); + for (String child : children) { + child = key + Constants.PATH_SEPARATOR + child; + this.deleteAllChildren(child); + } + this.deleteOnlyCurrent(key); + } catch (KeeperException.NoNodeException ee){ + System.out.println(ee.getMessage()); + return; + } + } + + /* + * delete the current node with force and delete the super node whose only child node is current node recursively + */ + @Override + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + this.deleteAllChildren(key); + String superPath = key.substring(0, key.lastIndexOf(Constants.PATH_SEPARATOR)); + try { + this.deleteRecursively(superPath); + } catch (KeeperException.NotEmptyException ee){ + return; + } + } + + private void deleteRecursively(final String path) throws KeeperException, InterruptedException { + int index = path.lastIndexOf(Constants.PATH_SEPARATOR); + if (index < 0){ + return; + } + String superPath = path.substring(0, index); + try { + this.deleteOnlyCurrent(path); + this.deleteRecursively(superPath); + } catch (KeeperException.NotEmptyException ee){ + List children = this.getChildren(path); + children.forEach((c) -> System.out.println(path + " exist other children " + c)); + return; + } + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java new file mode 100644 index 0000000000000..efa3aa1abd389 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -0,0 +1,7 @@ +package com.saaavsaaa.client.zookeeper.strategy; + +/** + * Created by aaa on 18-5-2. + */ +public class ContentionStrategy { +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/IStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/IStrategy.java new file mode 100644 index 0000000000000..9c36bfa26f084 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/IStrategy.java @@ -0,0 +1,9 @@ +package com.saaavsaaa.client.zookeeper.strategy; + +import com.saaavsaaa.client.zookeeper.IClient; + +/** + * Created by aaa on 18-5-2. + */ +public interface IStrategy extends IClient { +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java new file mode 100644 index 0000000000000..888db025b920b --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java @@ -0,0 +1,9 @@ +package com.saaavsaaa.client.zookeeper.strategy; + +/** + * Created by aaa on 18-5-2. + */ +public enum StrategyType { + BASE, + CONTENTION +} From afbc939f786d72c753a26609bc62a22707a5f176 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 3 May 2018 19:08:28 +0800 Subject: [PATCH 064/351] restructure start --- .../com/saaavsaaa/client/action/IAction.java | 26 +++ .../com/saaavsaaa/client/action/IClient.java | 34 ++++ .../saaavsaaa/client/action/IProvider.java | 16 ++ .../saaavsaaa/client/action/IStrategy.java | 7 + .../client/election/LeaderElection.java | 4 +- .../client/zookeeper/BlendClient.java | 34 +++- .../client/zookeeper/CacheAllClient.java | 28 ++-- .../saaavsaaa/client/zookeeper/Client.java | 20 ++- .../client/zookeeper/ClientFactory.java | 9 +- .../saaavsaaa/client/zookeeper/IClient.java | 42 ----- .../saaavsaaa/client/zookeeper/Provider.java | 117 +++++++++++++ .../client/zookeeper/UsualClient.java | 11 +- .../zookeeper/strategy/BaseStrategy.java | 106 +++++------- .../strategy/ContentionStrategy.java | 155 +++++++++++++++++- .../client/zookeeper/strategy/IStrategy.java | 9 - .../zookeeper/transaction/Transaction.java | 8 - .../zookeeper/transaction/ZKTransaction.java | 49 ++++++ .../client/zookeeper/BaseClientTest.java | 16 +- 18 files changed, 523 insertions(+), 168 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/action/IAction.java create mode 100644 src/main/java/com/saaavsaaa/client/action/IClient.java create mode 100644 src/main/java/com/saaavsaaa/client/action/IProvider.java create mode 100644 src/main/java/com/saaavsaaa/client/action/IStrategy.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/IClient.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/Provider.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/IStrategy.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/transaction/Transaction.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java diff --git a/src/main/java/com/saaavsaaa/client/action/IAction.java b/src/main/java/com/saaavsaaa/client/action/IAction.java new file mode 100644 index 0000000000000..c3cd562b3556a --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/action/IAction.java @@ -0,0 +1,26 @@ +package com.saaavsaaa.client.action; + +import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; + +import java.util.List; + +/** + * Created by aaa + */ +public interface IAction { + String getDataString(final String key) throws KeeperException, InterruptedException; + byte[] getData(final String key) throws KeeperException, InterruptedException; + void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException; + boolean checkExists(final String key) throws KeeperException, InterruptedException; + boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException; + List getChildren(final String key) throws KeeperException, InterruptedException; + void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException; + void update(final String key, final String value) throws KeeperException, InterruptedException; + void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException; + void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException; + ZKTransaction transaction(); +} diff --git a/src/main/java/com/saaavsaaa/client/action/IClient.java b/src/main/java/com/saaavsaaa/client/action/IClient.java new file mode 100644 index 0000000000000..655a56fc8c04b --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/action/IClient.java @@ -0,0 +1,34 @@ +package com.saaavsaaa.client.action; + +import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; +import org.apache.zookeeper.*; + +import java.util.List; + +/** + * Created by aaa + */ +public interface IClient extends IAction{ + + void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException ; + + void deleteAllChildren(final String key) throws KeeperException, InterruptedException ; + + /* + * delete the current node with force and delete the super node whose only child node is current node recursively + */ + void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException ; + + /* + void createNamespace(); + void deleteNamespace(); + Watcher registerWatch(String key, Listener listener); + void unregisterWatch(String key); + void close(); + Watcher registerWatch(Listener listener); + void setRootNode(String namespace); + void start(); + void setAuthorities(String scheme, byte[] auth); + ZooKeeper getZooKeeper(); + */ +} diff --git a/src/main/java/com/saaavsaaa/client/action/IProvider.java b/src/main/java/com/saaavsaaa/client/action/IProvider.java new file mode 100644 index 0000000000000..e170a7833967f --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/action/IProvider.java @@ -0,0 +1,16 @@ +package com.saaavsaaa.client.action; + +import com.saaavsaaa.client.election.LeaderElection; +import org.apache.zookeeper.KeeperException; + +import java.util.List; +import java.util.Stack; + +/** + * Created by aaa on 18-5-3. + */ +public interface IProvider extends IAction { + List getNecessaryPaths(final String key); + Stack getDeletingPaths(final String key); + void executeContention(final LeaderElection election) throws KeeperException, InterruptedException; +} diff --git a/src/main/java/com/saaavsaaa/client/action/IStrategy.java b/src/main/java/com/saaavsaaa/client/action/IStrategy.java new file mode 100644 index 0000000000000..b6880b6a7436e --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/action/IStrategy.java @@ -0,0 +1,7 @@ +package com.saaavsaaa.client.action; + +/** + * Created by aaa on 18-5-2. + */ +public interface IStrategy extends IClient { +} diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index a98fade14a848..de085a67d8aed 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -36,7 +36,7 @@ public void executeContention(final String path, final Client client) throws Kee @Override public void process(WatchedEvent event) { try { - actionWhenUnreached(); + executeContention(path, client); } catch (Exception ee){ System.out.println("Listener Exception " + path); ee.printStackTrace(); @@ -55,6 +55,6 @@ public void process(WatchedEvent event) { } } - public abstract void actionWhenUnreached() throws KeeperException, InterruptedException; +// public abstract void actionWhenUnreached() throws KeeperException, InterruptedException; public abstract void action() throws KeeperException, InterruptedException; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/BlendClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/BlendClient.java index a3e4c003ba64f..1b007dbae19b3 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/BlendClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/BlendClient.java @@ -1,9 +1,12 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.strategy.BaseStrategy; -import com.saaavsaaa.client.zookeeper.strategy.IStrategy; +import com.saaavsaaa.client.zookeeper.strategy.ContentionStrategy; +import com.saaavsaaa.client.action.IStrategy; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; +import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -11,23 +14,37 @@ import java.io.IOException; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * Created by aaa on 18-5-2. */ public class BlendClient extends Client { + private final Map strategies = new ConcurrentHashMap<>(); private IStrategy strategy; BlendClient(final String servers, final int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); } + @Override + public synchronized void start() throws IOException, InterruptedException { + super.start(); + UseStrategy(StrategyType.BASE); + } + public synchronized void UseStrategy(StrategyType strategyType) { + if (strategies.containsKey(strategyType)){ + strategy = strategies.get(strategyType); + return; + } if (StrategyType.BASE == strategyType){ - strategy = new BaseStrategy(rootNode, zooKeeper, watched, authorities); + strategy = new BaseStrategy(new Provider(rootNode, this, watched, authorities)); } else { - + strategy = new ContentionStrategy(new Provider(rootNode, this, watched, authorities)); } + strategies.put(strategyType, strategy); } @Override @@ -62,7 +79,6 @@ public List getChildren(final String key) throws KeeperException, Interr @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - createNamespace(); strategy.createCurrentOnly(PathUtil.getRealPath(rootNode, key), value, createMode); } @@ -76,11 +92,6 @@ public void update(final String key, final String value) throws KeeperException, strategy.update(PathUtil.getRealPath(rootNode, key), value); } - @Override - public void updateWithCheck(final String key, final String value) throws KeeperException, InterruptedException { - strategy.updateWithCheck(PathUtil.getRealPath(rootNode, key), value); - } - @Override public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { strategy.deleteOnlyCurrent(PathUtil.getRealPath(rootNode, key)); @@ -100,4 +111,9 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { strategy.deleteCurrentBranch(PathUtil.getRealPath(rootNode, key)); } + + @Override + public ZKTransaction transaction() { + return strategy.transaction(); + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java index dbc8321847ff5..1949f37aa8e14 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java @@ -50,10 +50,10 @@ public void run(Client client) throws KeeperException, InterruptedException { //用替换整树的方式更新 private synchronized void loadCache(final Client client) throws KeeperException, InterruptedException { LeaderElection election = new LeaderElection() { - @Override + /*@Override public void actionWhenUnreached() throws KeeperException, InterruptedException { loadCache(client); - } + }*/ @Override public void action() throws KeeperException, InterruptedException { @@ -69,10 +69,10 @@ public void action() throws KeeperException, InterruptedException { @Override public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { LeaderElection election = new LeaderElection() { - @Override + /*@Override public void actionWhenUnreached() throws KeeperException, InterruptedException { createAllNeedPath(key, value, createMode); - } + }*/ @Override public void action() throws KeeperException, InterruptedException { @@ -108,10 +108,10 @@ private void createBegin(final String key, final String value, final CreateMode @Override public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { LeaderElection election = new LeaderElection() { - @Override + /*@Override public void actionWhenUnreached() throws KeeperException, InterruptedException { deleteAllChildren(key); - } + }*/ @Override public void action() throws KeeperException, InterruptedException { @@ -144,10 +144,10 @@ private void deleteChildren(final String key) throws KeeperException, Interrupte @Override public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { LeaderElection election = new LeaderElection() { - @Override + /*@Override public void actionWhenUnreached() throws KeeperException, InterruptedException { deleteCurrentBranch(key); - } + }*/ @Override public void action() throws KeeperException, InterruptedException { @@ -215,11 +215,11 @@ public List getChildren(final String key) throws KeeperException, Interr public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { // super.createCurrentOnly(key, value, createMode); LeaderElection election = new LeaderElection() { - @Override +/* @Override public void actionWhenUnreached() throws KeeperException, InterruptedException { createCurrentOnly(key, value, createMode); } - + */ @Override public void action() throws KeeperException, InterruptedException { usualClient.createCurrentOnly(key, value, createMode); @@ -233,10 +233,10 @@ public void action() throws KeeperException, InterruptedException { public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { // super.deleteOnlyCurrent(key); LeaderElection election = new LeaderElection() { - @Override + /*@Override public void actionWhenUnreached() throws KeeperException, InterruptedException { deleteOnlyCurrent(key); - } + }*/ @Override public void action() throws KeeperException, InterruptedException { @@ -251,10 +251,10 @@ public void action() throws KeeperException, InterruptedException { public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { // super.deleteOnlyCurrent(key, callback, ctx); LeaderElection election = new LeaderElection() { - @Override + /*@Override public void actionWhenUnreached() throws KeeperException, InterruptedException { deleteOnlyCurrent(key, callback, ctx); - } + }*/ @Override public void action() throws KeeperException, InterruptedException { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index b4dc7808b7566..2ba63c96d6b18 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.StringUtil; import com.saaavsaaa.client.utility.constant.Constants; @@ -18,13 +19,15 @@ /** * Created by aaa */ -public abstract class Client implements IClient{ +public abstract class Client implements IClient { private static final CountDownLatch CONNECTED = new CountDownLatch(1); protected static final Map watchers = new ConcurrentHashMap<>(); protected final boolean watched = true; //false private final String servers; private final int sessionTimeOut; + private String scheme; + private byte[] auth; protected ZooKeeper zooKeeper; protected List authorities; @@ -40,6 +43,7 @@ protected Client(final String servers, final int sessionTimeoutMilliseconds) { public synchronized void start() throws IOException, InterruptedException { zooKeeper = new ZooKeeper(servers, sessionTimeOut, startWatcher()); + zooKeeper.addAuthInfo(scheme , auth); CONNECTED.await(); } @@ -113,7 +117,12 @@ private void createNamespace(final byte[] date) throws KeeperException, Interrup if (rootExist){ return; } - zooKeeper.create(rootNode, date, authorities, CreateMode.PERSISTENT); + try { + zooKeeper.create(rootNode, date, authorities, CreateMode.PERSISTENT); + } catch (KeeperException.NodeExistsException ee){ + System.out.println("root create : " + ee.getMessage()); + return; + } rootExist = true; zooKeeper.exists(rootNode, WatcherCreator.deleteWatcher(rootNode, new Listener() { @Override @@ -128,12 +137,13 @@ void deleteNamespace() throws KeeperException, InterruptedException { zooKeeper.delete(rootNode, Constants.VERSION); } - void setRootNode(String rootNode) { + void setRootNode(final String rootNode) { this.rootNode = rootNode; } - void setAuthorities(String scheme, byte[] auth) { - zooKeeper.addAuthInfo(scheme , auth); + void setAuthorities(final String scheme, final byte[] auth) { + this.scheme = scheme; + this.auth = auth; this.authorities = ZooDefs.Ids.CREATOR_ALL_ACL; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index fc780ab3cc74c..3846e3eaacc95 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -28,16 +28,11 @@ public ClientFactory newCacheClient(final String servers, final int sessionTimeo client = new CacheClient(servers, sessionTimeoutMilliseconds); return this; } - - ClientFactory newCacheClient() { - client = new CacheClient(servers, sessionTimeoutMilliseconds); - return this; - } public ClientFactory newUsualClient(final String servers, final int sessionTimeoutMilliseconds) { this.servers = servers; this.sessionTimeoutMilliseconds = sessionTimeoutMilliseconds; - client = new UsualClient(servers, sessionTimeoutMilliseconds); + client = new BlendClient(servers, sessionTimeoutMilliseconds); return this; } @@ -54,8 +49,8 @@ public ClientFactory watch(final Listener listener){ public synchronized Client start() throws IOException, InterruptedException { client.setClientFactory(this); client.setRootNode(namespace); - client.start(); client.setAuthorities(scheme , auth); + client.start(); if (globalListener != null) { client.registerWatch(globalListener); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/IClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/IClient.java deleted file mode 100644 index b98c58ce350f8..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/IClient.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -import org.apache.zookeeper.*; - -import java.util.List; - -/** - * Created by aaa - */ -public interface IClient { - String getDataString(final String key) throws KeeperException, InterruptedException ; - byte[] getData(final String key) throws KeeperException, InterruptedException ; - void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException; - boolean checkExists(final String key) throws KeeperException, InterruptedException ; - boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException ; - List getChildren(final String key) throws KeeperException, InterruptedException ; - void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException ; - void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException ; - void update(final String key, final String value) throws KeeperException, InterruptedException ; - void updateWithCheck(final String key, final String value) throws KeeperException, InterruptedException ; - void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException ; - void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException ; - void deleteAllChildren(final String key) throws KeeperException, InterruptedException ; - - /* - * delete the current node with force and delete the super node whose only child node is current node recursively - */ - void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException ; - - /* - void createNamespace(); - void deleteNamespace(); - Watcher registerWatch(String key, Listener listener); - void unregisterWatch(String key); - void close(); - Watcher registerWatch(Listener listener); - void setRootNode(String namespace); - void start(); - void setAuthorities(String scheme, byte[] auth); - ZooKeeper getZooKeeper(); - */ -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java b/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java new file mode 100644 index 0000000000000..4b68022b577a4 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java @@ -0,0 +1,117 @@ +package com.saaavsaaa.client.zookeeper; + +import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.election.LeaderElection; +import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; +import org.apache.zookeeper.*; +import org.apache.zookeeper.data.ACL; + +import java.util.List; +import java.util.Stack; + +/** + * Created by aaa on 18-5-3. + */ +public class Provider implements IProvider { + protected final Client client; + private final ZooKeeper zooKeeper; + protected final boolean watched; + protected final List authorities; + protected final String rootNode; + + Provider(final String rootNode, final Client client, final boolean watched, final List authorities){ + this.rootNode = rootNode; + this.client = client; + this.zooKeeper = client.getZooKeeper(); + this.watched = watched; + this.authorities = authorities; + } + + public String getDataString(final String key) throws KeeperException, InterruptedException { + return new String(getData(key)); + } + + @Override + public byte[] getData(final String key) throws KeeperException, InterruptedException { + return zooKeeper.getData(key, watched, null); + } + + @Override + public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { + zooKeeper.getData(key, watched, callback, ctx); + } + + @Override + public boolean checkExists(final String key) throws KeeperException, InterruptedException { + return null != zooKeeper.exists(key, watched); + } + + @Override + public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { + return null != zooKeeper.exists(key, watcher); + } + + @Override + public List getChildren(final String key) throws KeeperException, InterruptedException { + return zooKeeper.getChildren(key, watched); + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + client.createNamespace(); + if (rootNode.equals(key)){ + return; + } + try { + zooKeeper.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); + } catch (KeeperException.NoNodeException e) { + // I don't know whether it will happen or not, if root watcher don't update rootExist timely + if (e.getMessage().contains(key)) { + System.out.println("rootExist : " + e.getMessage()); + Thread.sleep(50); + this.createCurrentOnly(key, value, createMode); + } + } + } + + @Override + public void update(final String key, final String value) throws KeeperException, InterruptedException { + zooKeeper.setData(key, value.getBytes(Constants.UTF_8), Constants.VERSION); + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + zooKeeper.delete(key, Constants.VERSION); + System.out.println("delete : " + key); + } + + @Override + public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + zooKeeper.delete(key, Constants.VERSION, callback, ctx); + } + + + @Override + public List getNecessaryPaths(final String key){ + List nodes = PathUtil.getPathOrderNodes(rootNode, key); + nodes.remove(rootNode); + return nodes; + } + + @Override + public Stack getDeletingPaths(String key) { + return PathUtil.getPathReverseNodes(rootNode, key); + } + + @Override + public void executeContention(final LeaderElection election) throws KeeperException, InterruptedException { + election.executeContention(rootNode, client); + } + + @Override + public ZKTransaction transaction() { + return new ZKTransaction(rootNode, zooKeeper); + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index e620fd01a40a4..7008029fbb571 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -5,6 +5,7 @@ import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -101,11 +102,12 @@ public void update(final String key, final String value) throws KeeperException, zooKeeper.setData(PathUtil.getRealPath(rootNode, key), value.getBytes(Constants.UTF_8), Constants.VERSION); } - @Override + /*@Override public void updateWithCheck(final String key, final String value) throws KeeperException, InterruptedException { String realPath = PathUtil.getRealPath(rootNode, key); zooKeeper.transaction().check(realPath, Constants.VERSION).setData(realPath, value.getBytes(Constants.UTF_8), Constants.VERSION).commit(); - } +// transaction().check(realPath, Constants.VERSION).setData(realPath, value.getBytes(Constants.UTF_8), Constants.VERSION).commit(); + }*/ @Override public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { @@ -118,6 +120,11 @@ public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback zooKeeper.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION, callback, ctx); } + @Override + public ZKTransaction transaction() { + return new ZKTransaction(rootNode, this.getZooKeeper()); + } + @Override public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { String realPath = PathUtil.getRealPath(rootNode, key); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java index 4034610320acf..d9b70cf23fdfe 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java @@ -1,9 +1,13 @@ package com.saaavsaaa.client.zookeeper.strategy; -import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.action.IStrategy; import com.saaavsaaa.client.utility.constant.Constants; -import org.apache.zookeeper.*; -import org.apache.zookeeper.data.ACL; +import com.saaavsaaa.client.zookeeper.Provider; +import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; import java.util.List; @@ -11,16 +15,9 @@ * Created by aaa on 18-5-2. */ public class BaseStrategy implements IStrategy { - protected final ZooKeeper zooKeeper; - protected final boolean watched; - protected final List authorities; - protected final String rootNode; - - public BaseStrategy(final String rootNode, final ZooKeeper zooKeeper, final boolean watched, final List authorities){ - this.rootNode = rootNode; - this.zooKeeper = zooKeeper; - this.watched = watched; - this.authorities = authorities; + protected final Provider provider; + public BaseStrategy(final Provider provider){ + this.provider = provider; } @Override @@ -30,61 +27,58 @@ public String getDataString(final String key) throws KeeperException, Interrupte @Override public byte[] getData(final String key) throws KeeperException, InterruptedException { - return zooKeeper.getData(key, watched, null); + return provider.getData(key); } @Override public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { - zooKeeper.getData(key, watched, callback, ctx); + provider.getData(key, callback, ctx); } @Override public boolean checkExists(final String key) throws KeeperException, InterruptedException { - return null != zooKeeper.exists(key, watched); + return provider.checkExists(key); } @Override public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { - return null != zooKeeper.exists(key, watcher); + return provider.checkExists(key, watcher); } @Override public List getChildren(final String key) throws KeeperException, InterruptedException { - return zooKeeper.getChildren(key, watched); + return provider.getChildren(key); } @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - if (rootNode.equals(key)){ - return; - } - try { - zooKeeper.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); - } catch (KeeperException.NoNodeException e) { - // I don't know whether it will happen or not, if root watcher don't update rootExist timely - if (e.getMessage().contains(key)) { - System.out.println("rootExist : " + e.getMessage()); - Thread.sleep(50); - this.createCurrentOnly(key, value, createMode); - } - } + provider.createCurrentOnly(key, value, createMode); + } + + @Override + public void update(final String key, final String value) throws KeeperException, InterruptedException { + provider.update(key, value); + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + provider.deleteOnlyCurrent(key); + } + + @Override + public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + provider.deleteOnlyCurrent(key, callback, ctx); } @Override public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - this.createCurrentOnly(key, value, createMode); - return; - } - - List nodes = PathUtil.getPathOrderNodes(rootNode, key); - nodes.remove(rootNode); + List nodes = provider.getNecessaryPaths(key); for (int i = 0; i < nodes.size(); i++) { try { +// this.deleteAllChildren(nodes.get(i)); if (i == nodes.size() - 1){ this.createCurrentOnly(nodes.get(i), value, createMode); } else { -// this.deleteAllChildren(nodes.get(i)); this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); } System.out.println("not exist and create:" + nodes.get(i)); @@ -95,33 +89,18 @@ public void createAllNeedPath(final String key, final String value, final Create } } - @Override - public void update(final String key, final String value) throws KeeperException, InterruptedException { - zooKeeper.setData(key, value.getBytes(Constants.UTF_8), Constants.VERSION); - } - - @Override - public void updateWithCheck(final String key, final String value) throws KeeperException, InterruptedException { - zooKeeper.transaction().check(key, Constants.VERSION).setData(key, value.getBytes(Constants.UTF_8), Constants.VERSION).commit(); - } - - @Override - public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - zooKeeper.delete(key, Constants.VERSION); - System.out.println("delete : " + key); - } - - @Override - public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - zooKeeper.delete(key, Constants.VERSION, callback, ctx); - } - @Override public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { try { this.deleteOnlyCurrent(key); }catch (KeeperException.NotEmptyException ee){ - List children = this.getChildren(key); + List children; + try{ + children = this.getChildren(key); + } catch (KeeperException.NoNodeException e) { + // someone else has deleted the node since we checked + return; + } for (String child : children) { child = key + Constants.PATH_SEPARATOR + child; this.deleteAllChildren(child); @@ -162,4 +141,9 @@ private void deleteRecursively(final String path) throws KeeperException, Interr return; } } + + @Override + public ZKTransaction transaction() { + return provider.transaction(); + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index efa3aa1abd389..efc2b43c66c0f 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -1,7 +1,160 @@ package com.saaavsaaa.client.zookeeper.strategy; +import com.saaavsaaa.client.election.LeaderElection; +import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.zookeeper.Client; +import com.saaavsaaa.client.zookeeper.Provider; +import org.apache.zookeeper.*; +import org.apache.zookeeper.data.ACL; + +import java.util.List; +import java.util.Stack; + /** * Created by aaa on 18-5-2. */ -public class ContentionStrategy { +public class ContentionStrategy extends BaseStrategy { + public ContentionStrategy(final Provider provider) { + super(provider); + } + + @Override + /* + * Don't use it if you don't have to use it + */ + public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { + LeaderElection election = new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + provider.getData(key,callback, ctx); + } + }; + provider.executeContention(election); + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + LeaderElection election = new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + provider.createCurrentOnly(key, value, createMode); + } + }; + provider.executeContention(election); + } + + @Override + public void update(final String key, final String value) throws KeeperException, InterruptedException { + provider.executeContention(new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + provider.update(key, value); + } + }); + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + provider.executeContention(new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + provider.deleteOnlyCurrent(key); + } + }); + } + + @Override + public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + provider.executeContention(new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + provider.deleteOnlyCurrent(key, callback, ctx); + } + }); + } + + @Override + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + LeaderElection election = new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + createBegin(key, value, createMode); + } + }; + } + + + private void createBegin(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + List nodes = provider.getNecessaryPaths(key); + for (int i = 0; i < nodes.size(); i++) { + if (provider.checkExists(nodes.get(i))){ + System.out.println("create :" + nodes.get(i)); + continue; + } + System.out.println("create not exist:" + nodes.get(i)); + if (i == nodes.size() - 1){ + provider.createCurrentOnly(nodes.get(i), value, createMode); + } else { + provider.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); + } + } + } + + @Override + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { + provider.executeContention(new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + deleteChildren(key, true); + } + }); + } + + private void deleteChildren(final String key, final boolean deleteCurrentNode) throws KeeperException, InterruptedException { + List children = provider.getChildren(key); + if (children.isEmpty()){ + if (deleteCurrentNode){ + provider.deleteOnlyCurrent(key); + return; + } + return; + } + for (int i = 0; i < children.size(); i++) { + String child = PathUtil.getRealPath(key, children.get(i)); + if (!provider.checkExists(child)){ + System.out.println("delete not exist:" + child); + continue; + } + deleteChildren(child, true); + } + } + + @Override + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + provider.executeContention(new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + try { + deleteBranch(key); + } catch (KeeperException.NotEmptyException ee){ + System.out.println(key + " exist other children"); + return; + } + } + }); + } + + private void deleteBranch(String key) throws KeeperException, InterruptedException { + deleteChildren(key, false); + Stack pathStack = provider.getDeletingPaths(key); + while (!pathStack.empty()){ + String node = pathStack.pop(); + // contrast cache + if (checkExists(node)){ + provider.deleteOnlyCurrent(key); + System.out.println("delete : " + node); + } + } + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/IStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/IStrategy.java deleted file mode 100644 index 9c36bfa26f084..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/IStrategy.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.saaavsaaa.client.zookeeper.strategy; - -import com.saaavsaaa.client.zookeeper.IClient; - -/** - * Created by aaa on 18-5-2. - */ -public interface IStrategy extends IClient { -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/Transaction.java b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/Transaction.java deleted file mode 100644 index 3899544432be8..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/Transaction.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.saaavsaaa.client.zookeeper.transaction; - -/** - * Created by aaa - * todo transaction api - */ -public class Transaction { -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java new file mode 100644 index 0000000000000..4b80ad42efe6e --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java @@ -0,0 +1,49 @@ +package com.saaavsaaa.client.zookeeper.transaction; + +import com.saaavsaaa.client.utility.PathUtil; +import org.apache.zookeeper.*; +import org.apache.zookeeper.data.ACL; + +import java.util.List; + +/** + * Created by aaa + * todo transaction api + */ +public class ZKTransaction { + private final Transaction transaction; + private final String rootNode; + + public ZKTransaction(final String root, final ZooKeeper zk) { + transaction = zk.transaction(); + rootNode = root; + } + + public ZKTransaction create(String path, byte[] data, List acl, CreateMode createMode) { + this.transaction.create(PathUtil.getRealPath(rootNode, path), data, acl, createMode); + return this; + } + + public ZKTransaction delete(String path, int version) { + this.transaction.delete(PathUtil.getRealPath(rootNode, path), version); + return this; + } + + public ZKTransaction check(String path, int version) { + this.transaction.check(PathUtil.getRealPath(rootNode, path), version); + return this; + } + + public ZKTransaction setData(String path, byte[] data, int version) { + this.transaction.setData(PathUtil.getRealPath(rootNode, path), data, version); + return this; + } + + public List commit() throws InterruptedException, KeeperException { + return this.transaction.commit(); + } + + public void commit(AsyncCallback.MultiCallback cb, Object ctx) { + this.transaction.commit(cb, ctx); + } +} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java index 5049f61a4d82c..3fa4d50842205 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java @@ -142,18 +142,18 @@ protected void persist(Client client) throws KeeperException, InterruptedExcepti if (!isExisted(key, client)) { client.createAllNeedPath(key, value, CreateMode.PERSISTENT); } else { - update(key, value, client); + updateWithCheck(key, value, client); } assert getDirectly(key, client).equals(value); - - update(key, newValue, client); + + updateWithCheck(key, newValue, client); assert getDirectly(key, client).equals(newValue); client.deleteCurrentBranch(key); } - private void update(String key, String value, Client client) throws KeeperException, InterruptedException { - client.updateWithCheck(key, value); + private void updateWithCheck(String key, String value, Client client) throws KeeperException, InterruptedException { + client.transaction().check(key, Constants.VERSION).setData(key, value.getBytes(Constants.UTF_8), Constants.VERSION).commit(); } protected void persistEphemeral(Client client) throws KeeperException, InterruptedException { @@ -200,12 +200,12 @@ protected void watch(Client client) throws KeeperException, InterruptedException Watcher watcher = client.registerWatch(key, listener); client.createCurrentOnly(key, "aaa", CreateMode.EPHEMERAL); client.checkExists(key, watcher); - client.updateWithCheck(key, "value"); + client.update(key, "value"); System.out.println(new String(client.getData(key))); assert client.getDataString(key).equals("value"); - client.updateWithCheck(key, "value1"); + client.update(key, "value1"); assert client.getDataString(key).equals("value1"); - client.updateWithCheck(key, "value2"); + client.update(key, "value2"); assert client.getDataString(key).equals("value2"); client.deleteCurrentBranch(key); Thread.sleep(100); From 17ba5d7c16b46b50ba1a905f82acd5fe5a722d74 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 4 May 2018 11:42:47 +0800 Subject: [PATCH 065/351] UsualClient --- .../client/zookeeper/BlendClient.java | 119 ------------- .../saaavsaaa/client/zookeeper/Client.java | 1 + .../client/zookeeper/ClientFactory.java | 3 +- .../saaavsaaa/client/zookeeper/Provider.java | 5 + .../client/zookeeper/UsualClient.java | 158 ++++++------------ .../zookeeper/strategy/BaseStrategy.java | 73 ++++---- .../strategy/ContentionStrategy.java | 16 +- 7 files changed, 112 insertions(+), 263 deletions(-) delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/BlendClient.java diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/BlendClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/BlendClient.java deleted file mode 100644 index 1b007dbae19b3..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/BlendClient.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.strategy.BaseStrategy; -import com.saaavsaaa.client.zookeeper.strategy.ContentionStrategy; -import com.saaavsaaa.client.action.IStrategy; -import com.saaavsaaa.client.zookeeper.strategy.StrategyType; -import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; -import org.apache.zookeeper.AsyncCallback; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.Watcher; - -import java.io.IOException; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * Created by aaa on 18-5-2. - */ -public class BlendClient extends Client { - private final Map strategies = new ConcurrentHashMap<>(); - private IStrategy strategy; - - BlendClient(final String servers, final int sessionTimeoutMilliseconds) { - super(servers, sessionTimeoutMilliseconds); - } - - @Override - public synchronized void start() throws IOException, InterruptedException { - super.start(); - UseStrategy(StrategyType.BASE); - } - - public synchronized void UseStrategy(StrategyType strategyType) { - if (strategies.containsKey(strategyType)){ - strategy = strategies.get(strategyType); - return; - } - if (StrategyType.BASE == strategyType){ - strategy = new BaseStrategy(new Provider(rootNode, this, watched, authorities)); - } else { - strategy = new ContentionStrategy(new Provider(rootNode, this, watched, authorities)); - } - strategies.put(strategyType, strategy); - } - - @Override - public String getDataString(final String key) throws KeeperException, InterruptedException { - return strategy.getDataString(PathUtil.getRealPath(rootNode, key)); - } - - @Override - public byte[] getData(final String key) throws KeeperException, InterruptedException { - return strategy.getData(PathUtil.getRealPath(rootNode, key)); - } - - @Override - public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { - strategy.getData(PathUtil.getRealPath(rootNode, key), callback, ctx); - } - - @Override - public boolean checkExists(final String key) throws KeeperException, InterruptedException { - return strategy.checkExists(PathUtil.getRealPath(rootNode, key)); - } - - @Override - public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { - return strategy.checkExists(PathUtil.getRealPath(rootNode, key), watcher); - } - - @Override - public List getChildren(final String key) throws KeeperException, InterruptedException { - return strategy.getChildren(PathUtil.getRealPath(rootNode, key)); - } - - @Override - public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - strategy.createCurrentOnly(PathUtil.getRealPath(rootNode, key), value, createMode); - } - - @Override - public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - strategy.createAllNeedPath(PathUtil.getRealPath(rootNode, key), value, createMode); - } - - @Override - public void update(final String key, final String value) throws KeeperException, InterruptedException { - strategy.update(PathUtil.getRealPath(rootNode, key), value); - } - - @Override - public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - strategy.deleteOnlyCurrent(PathUtil.getRealPath(rootNode, key)); - } - - @Override - public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - strategy.deleteOnlyCurrent(PathUtil.getRealPath(rootNode, key), callback, ctx); - } - - @Override - public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { - strategy.deleteAllChildren(PathUtil.getRealPath(rootNode, key)); - } - - @Override - public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - strategy.deleteCurrentBranch(PathUtil.getRealPath(rootNode, key)); - } - - @Override - public ZKTransaction transaction() { - return strategy.transaction(); - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index 2ba63c96d6b18..f437dae867d9f 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -121,6 +121,7 @@ private void createNamespace(final byte[] date) throws KeeperException, Interrup zooKeeper.create(rootNode, date, authorities, CreateMode.PERSISTENT); } catch (KeeperException.NodeExistsException ee){ System.out.println("root create : " + ee.getMessage()); + rootExist = true; return; } rootExist = true; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 3846e3eaacc95..5bfa69b7d2bd0 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -1,6 +1,5 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.utility.StringUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.Listener; @@ -32,7 +31,7 @@ public ClientFactory newCacheClient(final String servers, final int sessionTimeo public ClientFactory newUsualClient(final String servers, final int sessionTimeoutMilliseconds) { this.servers = servers; this.sessionTimeoutMilliseconds = sessionTimeoutMilliseconds; - client = new BlendClient(servers, sessionTimeoutMilliseconds); + client = new UsualClient(servers, sessionTimeoutMilliseconds); return this; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java b/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java index 4b68022b577a4..b9fdf9bfd5995 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java @@ -93,6 +93,11 @@ public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback } + @Override + public String getRealPath(String path) { + return PathUtil.getRealPath(rootNode, path); + } + @Override public List getNecessaryPaths(final String key){ List nodes = PathUtil.getPathOrderNodes(rootNode, key); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 7008029fbb571..e5d691b7dd544 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -1,177 +1,119 @@ package com.saaavsaaa.client.zookeeper; -/** - * Created by aaa - */ -import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.zookeeper.strategy.BaseStrategy; +import com.saaavsaaa.client.zookeeper.strategy.ContentionStrategy; +import com.saaavsaaa.client.action.IStrategy; +import com.saaavsaaa.client.zookeeper.strategy.StrategyType; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.Watcher; +import java.io.IOException; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; -/* -* cache -* todo Sequential -* todo org.apache.zookeeper.KeeperException$NodeExistsException: KeeperErrorCode = NodeExists -*/ +/** + * Created by aaa on 18-5-2. + */ public class UsualClient extends Client { - UsualClient(String servers, int sessionTimeoutMilliseconds) { + private final Map strategies = new ConcurrentHashMap<>(); + private IStrategy strategy; + + UsualClient(final String servers, final int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); } + + @Override + public synchronized void start() throws IOException, InterruptedException { + super.start(); + UseStrategy(StrategyType.BASE); + } + + public synchronized void UseStrategy(StrategyType strategyType) { + if (strategies.containsKey(strategyType)){ + strategy = strategies.get(strategyType); + return; + } + if (StrategyType.BASE == strategyType){ + strategy = new BaseStrategy(new Provider(rootNode, this, watched, authorities)); + } else { + strategy = new ContentionStrategy(new Provider(rootNode, this, watched, authorities)); + } + strategies.put(strategyType, strategy); + } @Override public String getDataString(final String key) throws KeeperException, InterruptedException { - return new String(getData(key)); + return strategy.getDataString(key); } @Override public byte[] getData(final String key) throws KeeperException, InterruptedException { - return zooKeeper.getData(PathUtil.getRealPath(rootNode, key), watched, null); + return strategy.getData(key); } @Override public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { - zooKeeper.getData(PathUtil.getRealPath(rootNode, key), watched, callback, ctx); + strategy.getData(key, callback, ctx); } @Override public boolean checkExists(final String key) throws KeeperException, InterruptedException { - return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), watched); + return strategy.checkExists(key); } @Override public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { - return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), watcher); + return strategy.checkExists(key, watcher); } @Override public List getChildren(final String key) throws KeeperException, InterruptedException { - return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), watched); + return strategy.getChildren(key); } @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - createNamespace(); - String path = PathUtil.getRealPath(rootNode, key); - if (rootNode.equals(path)){ - return; - } - try { - zooKeeper.create(path, value.getBytes(Constants.UTF_8), authorities, createMode); - } catch (KeeperException.NoNodeException e) { - // I don't know whether it will happen or not, if root watcher don't update rootExist timely - if (e.getMessage().contains(path)) { - System.out.println("rootExist : " + e.getMessage()); - Thread.sleep(50); - this.createCurrentOnly(key, value, createMode); - } - } + strategy.createCurrentOnly(key, value, createMode); } @Override public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - this.createCurrentOnly(key, value, createMode); - return; - } - - List nodes = PathUtil.getPathOrderNodes(rootNode, key); - nodes.remove(rootNode); - for (int i = 0; i < nodes.size(); i++) { - try { - if (i == nodes.size() - 1){ - this.createCurrentOnly(nodes.get(i), value, createMode); - } else { -// this.deleteAllChildren(nodes.get(i)); - this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); - } - System.out.println("not exist and create:" + nodes.get(i)); - } catch (KeeperException.NodeExistsException ee){ - System.out.println("exist:" + nodes.get(i)); - continue; - } - } + strategy.createAllNeedPath(key, value, createMode); } @Override public void update(final String key, final String value) throws KeeperException, InterruptedException { - zooKeeper.setData(PathUtil.getRealPath(rootNode, key), value.getBytes(Constants.UTF_8), Constants.VERSION); + strategy.update(key, value); } - /*@Override - public void updateWithCheck(final String key, final String value) throws KeeperException, InterruptedException { - String realPath = PathUtil.getRealPath(rootNode, key); - zooKeeper.transaction().check(realPath, Constants.VERSION).setData(realPath, value.getBytes(Constants.UTF_8), Constants.VERSION).commit(); -// transaction().check(realPath, Constants.VERSION).setData(realPath, value.getBytes(Constants.UTF_8), Constants.VERSION).commit(); - }*/ - @Override public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - zooKeeper.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION); - System.out.println("delete : " + PathUtil.getRealPath(rootNode, key)); + strategy.deleteOnlyCurrent(key); } @Override public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - zooKeeper.delete(PathUtil.getRealPath(rootNode, key), Constants.VERSION, callback, ctx); - } - - @Override - public ZKTransaction transaction() { - return new ZKTransaction(rootNode, this.getZooKeeper()); + strategy.deleteOnlyCurrent(key, callback, ctx); } @Override public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { - String realPath = PathUtil.getRealPath(rootNode, key); - try { - this.deleteOnlyCurrent(realPath); - }catch (KeeperException.NotEmptyException ee){ - List children = this.getChildren(realPath); - for (String child : children) { - child = realPath + Constants.PATH_SEPARATOR + child; - this.deleteAllChildren(child); - } - this.deleteOnlyCurrent(realPath); - } catch (KeeperException.NoNodeException ee){ - System.out.println(ee.getMessage()); - return; - } + strategy.deleteAllChildren(key); } - /* - * delete the current node with force and delete the super node whose only child node is current node recursively - */ @Override public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - String path = PathUtil.getRealPath(rootNode, key); - this.deleteAllChildren(path); - String superPath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); - try { - this.deleteRecursively(superPath); - } catch (KeeperException.NotEmptyException ee){ - return; - } + strategy.deleteCurrentBranch(key); } - private void deleteRecursively(final String path) throws KeeperException, InterruptedException { - int index = path.lastIndexOf(Constants.PATH_SEPARATOR); - if (index < 0){ - return; - } - String superPath = path.substring(0, index); - try { - this.deleteOnlyCurrent(path); - this.deleteRecursively(superPath); - } catch (KeeperException.NotEmptyException ee){ - List children = this.getChildren(path); - children.forEach((c) -> System.out.println(path + " exist other children " + c)); - return; - } + @Override + public ZKTransaction transaction() { + return strategy.transaction(); } } - diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java index d9b70cf23fdfe..bd56abadf090b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.zookeeper.strategy; import com.saaavsaaa.client.action.IStrategy; +import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.Provider; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; @@ -27,51 +28,55 @@ public String getDataString(final String key) throws KeeperException, Interrupte @Override public byte[] getData(final String key) throws KeeperException, InterruptedException { - return provider.getData(key); + return provider.getData(provider.getRealPath(key)); } @Override public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { - provider.getData(key, callback, ctx); + provider.getData(provider.getRealPath(key), callback, ctx); } @Override public boolean checkExists(final String key) throws KeeperException, InterruptedException { - return provider.checkExists(key); + return provider.checkExists(provider.getRealPath(key)); } @Override public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { - return provider.checkExists(key, watcher); + return provider.checkExists(provider.getRealPath(key), watcher); } @Override public List getChildren(final String key) throws KeeperException, InterruptedException { - return provider.getChildren(key); + return provider.getChildren(provider.getRealPath(key)); } @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - provider.createCurrentOnly(key, value, createMode); + provider.createCurrentOnly(provider.getRealPath(key), value, createMode); } @Override public void update(final String key, final String value) throws KeeperException, InterruptedException { - provider.update(key, value); + provider.update(provider.getRealPath(key), value); } @Override public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - provider.deleteOnlyCurrent(key); + provider.deleteOnlyCurrent(provider.getRealPath(key)); } @Override public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - provider.deleteOnlyCurrent(key, callback, ctx); + provider.deleteOnlyCurrent(provider.getRealPath(key), callback, ctx); } @Override public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ + this.createCurrentOnly(key, value, createMode); + return; + } List nodes = provider.getNecessaryPaths(key); for (int i = 0; i < nodes.size(); i++) { try { @@ -91,24 +96,29 @@ public void createAllNeedPath(final String key, final String value, final Create @Override public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { - try { - this.deleteOnlyCurrent(key); - }catch (KeeperException.NotEmptyException ee){ - List children; + this.deleteChildren(provider.getRealPath(key), true); + } + + private void deleteChildren(final String path, final boolean deleteCurrentNode) throws KeeperException, InterruptedException { + List children; + try{ + children = provider.getChildren(path); + } catch (KeeperException.NoNodeException e){ // someone else has deleted the node since we checked + System.out.println(e.getMessage()); + return; + } + for (String child : children){ + child = PathUtil.getRealPath(path, child); + this.deleteAllChildren(child); + } + if (deleteCurrentNode){ try{ - children = this.getChildren(key); - } catch (KeeperException.NoNodeException e) { - // someone else has deleted the node since we checked - return; + this.deleteOnlyCurrent(path); + } catch(KeeperException.NotEmptyException e){ //someone has created a new child since we checked ... delete again. + deleteChildren(path, true); + } catch(KeeperException.NoNodeException e){ // ignore... someone else has deleted the node since we checked + System.out.println(e.getMessage()); } - for (String child : children) { - child = key + Constants.PATH_SEPARATOR + child; - this.deleteAllChildren(child); - } - this.deleteOnlyCurrent(key); - } catch (KeeperException.NoNodeException ee){ - System.out.println(ee.getMessage()); - return; } } @@ -117,18 +127,25 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup */ @Override public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - this.deleteAllChildren(key); - String superPath = key.substring(0, key.lastIndexOf(Constants.PATH_SEPARATOR)); + if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ + this.deleteOnlyCurrent(key); + return; + } + String path = provider.getRealPath(key); + this.deleteChildren(path, true); + String superPath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); try { this.deleteRecursively(superPath); } catch (KeeperException.NotEmptyException ee){ + System.out.println(ee.getMessage()); return; } } private void deleteRecursively(final String path) throws KeeperException, InterruptedException { int index = path.lastIndexOf(Constants.PATH_SEPARATOR); - if (index < 0){ + if (index == 0){ + this.deleteOnlyCurrent(path); return; } String superPath = path.substring(0, index); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index efc2b43c66c0f..92c3ab284a757 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -27,7 +27,7 @@ public void getData(final String key, final AsyncCallback.DataCallback callback, LeaderElection election = new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - provider.getData(key,callback, ctx); + provider.getData(provider.getRealPath(key), callback, ctx); } }; provider.executeContention(election); @@ -38,7 +38,7 @@ public void createCurrentOnly(final String key, final String value, final Create LeaderElection election = new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - provider.createCurrentOnly(key, value, createMode); + provider.createCurrentOnly(provider.getRealPath(key), value, createMode); } }; provider.executeContention(election); @@ -49,7 +49,7 @@ public void update(final String key, final String value) throws KeeperException, provider.executeContention(new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - provider.update(key, value); + provider.update(provider.getRealPath(key), value); } }); } @@ -59,7 +59,7 @@ public void deleteOnlyCurrent(final String key) throws KeeperException, Interrup provider.executeContention(new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - provider.deleteOnlyCurrent(key); + provider.deleteOnlyCurrent(provider.getRealPath(key)); } }); } @@ -69,7 +69,7 @@ public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback provider.executeContention(new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - provider.deleteOnlyCurrent(key, callback, ctx); + provider.deleteOnlyCurrent(provider.getRealPath(key), callback, ctx); } }); } @@ -86,6 +86,10 @@ public void action() throws KeeperException, InterruptedException { private void createBegin(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ + this.createCurrentOnly(key, value, createMode); + return; + } List nodes = provider.getNecessaryPaths(key); for (int i = 0; i < nodes.size(); i++) { if (provider.checkExists(nodes.get(i))){ @@ -106,7 +110,7 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup provider.executeContention(new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - deleteChildren(key, true); + deleteChildren(provider.getRealPath(key), true); } }); } From 609a301403701285dfff975bc4361786f7498ec1 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 4 May 2018 14:12:06 +0800 Subject: [PATCH 066/351] UsualClient --- .../saaavsaaa/client/action/IProvider.java | 1 + .../zookeeper/strategy/BaseStrategy.java | 150 +--------------- .../strategy/ContentionStrategy.java | 8 +- .../zookeeper/strategy/StrategyType.java | 2 +- .../zookeeper/strategy/UsualStrategy.java | 160 ++++++++++++++++++ 5 files changed, 167 insertions(+), 154 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java diff --git a/src/main/java/com/saaavsaaa/client/action/IProvider.java b/src/main/java/com/saaavsaaa/client/action/IProvider.java index e170a7833967f..681a6b525a91d 100644 --- a/src/main/java/com/saaavsaaa/client/action/IProvider.java +++ b/src/main/java/com/saaavsaaa/client/action/IProvider.java @@ -10,6 +10,7 @@ * Created by aaa on 18-5-3. */ public interface IProvider extends IAction { + String getRealPath(final String path); List getNecessaryPaths(final String key); Stack getDeletingPaths(final String key); void executeContention(final LeaderElection election) throws KeeperException, InterruptedException; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java index bd56abadf090b..9dfcd017e331d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java @@ -1,21 +1,13 @@ package com.saaavsaaa.client.zookeeper.strategy; import com.saaavsaaa.client.action.IStrategy; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.Provider; -import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; -import org.apache.zookeeper.AsyncCallback; -import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.Watcher; - -import java.util.List; /** - * Created by aaa on 18-5-2. + * Created by aaa on 18-5-4. */ -public class BaseStrategy implements IStrategy { +public abstract class BaseStrategy implements IStrategy { protected final Provider provider; public BaseStrategy(final Provider provider){ this.provider = provider; @@ -25,142 +17,4 @@ public BaseStrategy(final Provider provider){ public String getDataString(final String key) throws KeeperException, InterruptedException { return new String(getData(key)); } - - @Override - public byte[] getData(final String key) throws KeeperException, InterruptedException { - return provider.getData(provider.getRealPath(key)); - } - - @Override - public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { - provider.getData(provider.getRealPath(key), callback, ctx); - } - - @Override - public boolean checkExists(final String key) throws KeeperException, InterruptedException { - return provider.checkExists(provider.getRealPath(key)); - } - - @Override - public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { - return provider.checkExists(provider.getRealPath(key), watcher); - } - - @Override - public List getChildren(final String key) throws KeeperException, InterruptedException { - return provider.getChildren(provider.getRealPath(key)); - } - - @Override - public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - provider.createCurrentOnly(provider.getRealPath(key), value, createMode); - } - - @Override - public void update(final String key, final String value) throws KeeperException, InterruptedException { - provider.update(provider.getRealPath(key), value); - } - - @Override - public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - provider.deleteOnlyCurrent(provider.getRealPath(key)); - } - - @Override - public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - provider.deleteOnlyCurrent(provider.getRealPath(key), callback, ctx); - } - - @Override - public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - this.createCurrentOnly(key, value, createMode); - return; - } - List nodes = provider.getNecessaryPaths(key); - for (int i = 0; i < nodes.size(); i++) { - try { -// this.deleteAllChildren(nodes.get(i)); - if (i == nodes.size() - 1){ - this.createCurrentOnly(nodes.get(i), value, createMode); - } else { - this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); - } - System.out.println("not exist and create:" + nodes.get(i)); - } catch (KeeperException.NodeExistsException ee){ - System.out.println("exist:" + nodes.get(i)); - continue; - } - } - } - - @Override - public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { - this.deleteChildren(provider.getRealPath(key), true); - } - - private void deleteChildren(final String path, final boolean deleteCurrentNode) throws KeeperException, InterruptedException { - List children; - try{ - children = provider.getChildren(path); - } catch (KeeperException.NoNodeException e){ // someone else has deleted the node since we checked - System.out.println(e.getMessage()); - return; - } - for (String child : children){ - child = PathUtil.getRealPath(path, child); - this.deleteAllChildren(child); - } - if (deleteCurrentNode){ - try{ - this.deleteOnlyCurrent(path); - } catch(KeeperException.NotEmptyException e){ //someone has created a new child since we checked ... delete again. - deleteChildren(path, true); - } catch(KeeperException.NoNodeException e){ // ignore... someone else has deleted the node since we checked - System.out.println(e.getMessage()); - } - } - } - - /* - * delete the current node with force and delete the super node whose only child node is current node recursively - */ - @Override - public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - this.deleteOnlyCurrent(key); - return; - } - String path = provider.getRealPath(key); - this.deleteChildren(path, true); - String superPath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); - try { - this.deleteRecursively(superPath); - } catch (KeeperException.NotEmptyException ee){ - System.out.println(ee.getMessage()); - return; - } - } - - private void deleteRecursively(final String path) throws KeeperException, InterruptedException { - int index = path.lastIndexOf(Constants.PATH_SEPARATOR); - if (index == 0){ - this.deleteOnlyCurrent(path); - return; - } - String superPath = path.substring(0, index); - try { - this.deleteOnlyCurrent(path); - this.deleteRecursively(superPath); - } catch (KeeperException.NotEmptyException ee){ - List children = this.getChildren(path); - children.forEach((c) -> System.out.println(path + " exist other children " + c)); - return; - } - } - - @Override - public ZKTransaction transaction() { - return provider.transaction(); - } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index 92c3ab284a757..b1b27d64fd632 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -12,17 +12,15 @@ import java.util.Stack; /** - * Created by aaa on 18-5-2. + * Created by aaa + * Don't use it if you don't have to use it */ -public class ContentionStrategy extends BaseStrategy { +public class ContentionStrategy extends UsualStrategy { public ContentionStrategy(final Provider provider) { super(provider); } @Override - /* - * Don't use it if you don't have to use it - */ public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { LeaderElection election = new LeaderElection() { @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java index 888db025b920b..0e3cc52241b5b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java @@ -4,6 +4,6 @@ * Created by aaa on 18-5-2. */ public enum StrategyType { - BASE, + USUAL, CONTENTION } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java new file mode 100644 index 0000000000000..c5ee80ae7f8aa --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java @@ -0,0 +1,160 @@ +package com.saaavsaaa.client.zookeeper.strategy; + +import com.saaavsaaa.client.action.IStrategy; +import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.zookeeper.Provider; +import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; + +import java.util.List; + +/** + * Created by aaa on 18-5-2. + */ +public class UsualStrategy extends BaseStrategy { + public UsualStrategy(final Provider provider){ + super(provider); + } + + @Override + public byte[] getData(final String key) throws KeeperException, InterruptedException { + return provider.getData(provider.getRealPath(key)); + } + + @Override + public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { + provider.getData(provider.getRealPath(key), callback, ctx); + } + + @Override + public boolean checkExists(final String key) throws KeeperException, InterruptedException { + return provider.checkExists(provider.getRealPath(key)); + } + + @Override + public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { + return provider.checkExists(provider.getRealPath(key), watcher); + } + + @Override + public List getChildren(final String key) throws KeeperException, InterruptedException { + return provider.getChildren(provider.getRealPath(key)); + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + provider.createCurrentOnly(provider.getRealPath(key), value, createMode); + } + + @Override + public void update(final String key, final String value) throws KeeperException, InterruptedException { + provider.update(provider.getRealPath(key), value); + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + provider.deleteOnlyCurrent(provider.getRealPath(key)); + } + + @Override + public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + provider.deleteOnlyCurrent(provider.getRealPath(key), callback, ctx); + } + + @Override + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ + this.createCurrentOnly(key, value, createMode); + return; + } + List nodes = provider.getNecessaryPaths(key); + for (int i = 0; i < nodes.size(); i++) { + try { +// this.deleteAllChildren(nodes.get(i)); + if (i == nodes.size() - 1){ + this.createCurrentOnly(nodes.get(i), value, createMode); + } else { + this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); + } + System.out.println("not exist and create:" + nodes.get(i)); + } catch (KeeperException.NodeExistsException ee){ + System.out.println("exist:" + nodes.get(i)); + continue; + } + } + } + + @Override + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { + this.deleteChildren(provider.getRealPath(key), true); + } + + private void deleteChildren(final String path, final boolean deleteCurrentNode) throws KeeperException, InterruptedException { + List children; + try{ + children = provider.getChildren(path); + } catch (KeeperException.NoNodeException e){ // someone else has deleted the node since we checked + System.out.println(e.getMessage()); + return; + } + for (String child : children){ + child = PathUtil.getRealPath(path, child); + this.deleteAllChildren(child); + } + if (deleteCurrentNode){ + try{ + this.deleteOnlyCurrent(path); + } catch(KeeperException.NotEmptyException e){ //someone has created a new child since we checked ... delete again. + deleteChildren(path, true); + } catch(KeeperException.NoNodeException e){ // ignore... someone else has deleted the node since we checked + System.out.println(e.getMessage()); + } + } + } + + /* + * delete the current node with force and delete the super node whose only child node is current node recursively + */ + @Override + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ + this.deleteOnlyCurrent(key); + return; + } + String path = provider.getRealPath(key); + this.deleteChildren(path, true); + String superPath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); + try { + this.deleteRecursively(superPath); + } catch (KeeperException.NotEmptyException ee){ + System.out.println(ee.getMessage()); + return; + } + } + + private void deleteRecursively(final String path) throws KeeperException, InterruptedException { + int index = path.lastIndexOf(Constants.PATH_SEPARATOR); + if (index == 0){ + this.deleteOnlyCurrent(path); + return; + } + String superPath = path.substring(0, index); + try { + this.deleteOnlyCurrent(path); + this.deleteRecursively(superPath); + } catch (KeeperException.NotEmptyException ee){ + List children = this.getChildren(path); + children.forEach((c) -> System.out.println(path + " exist other children " + c)); + return; + } + } + + @Override + public ZKTransaction transaction() { + return provider.transaction(); + } +} From 1dcd941213586a8db9e5a42248bc25237c7f332d Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 4 May 2018 14:13:43 +0800 Subject: [PATCH 067/351] UsualClient --- .../java/com/saaavsaaa/client/zookeeper/Client.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index f437dae867d9f..f732e6e60cfa2 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -7,6 +7,7 @@ import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.utility.section.Properties; import com.saaavsaaa.client.utility.section.WatcherCreator; +import com.saaavsaaa.client.zookeeper.strategy.StrategyType; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; @@ -47,6 +48,8 @@ public synchronized void start() throws IOException, InterruptedException { CONNECTED.await(); } + public abstract void useStrategy(StrategyType strategyType); + ZooKeeper getZooKeeper(){ return zooKeeper; } @@ -148,11 +151,10 @@ void setAuthorities(final String scheme, final byte[] auth) { this.authorities = ZooDefs.Ids.CREATOR_ALL_ACL; } - public ClientFactory getClientFactory() { - return clientFactory; - } - void setClientFactory(ClientFactory clientFactory) { this.clientFactory = clientFactory; } + public ClientFactory getClientFactory() { + return clientFactory; + } } From a92f71b9d0638edc095d75a6efe17230246c8850 Mon Sep 17 00:00:00 2001 From: WangKai Date: Fri, 4 May 2018 14:23:56 +0800 Subject: [PATCH 068/351] fix some code style. --- .../backend/common/BackendHandlerFactory.java | 5 ----- .../common/CommandResponsePacketsHandler.java | 22 +++++++++++++++++++ .../common/SQLPacketsBackendHandler.java | 2 +- .../backend/mysql/MySQLBackendHandler.java | 7 +++--- .../proxy/frontend/ShardingProxy.java | 4 ++-- .../proxy/util/MySQLResultCache.java | 4 ++-- .../proxy/util/SynchronizedFuture.java | 4 +++- 7 files changed, 33 insertions(+), 15 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java index b600f3a57b8c1..a347249ff7da8 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java @@ -17,13 +17,8 @@ package io.shardingjdbc.proxy.backend.common; -import io.netty.channel.EventLoopGroup; import io.shardingjdbc.core.constant.DatabaseType; import io.shardingjdbc.proxy.backend.mysql.MySQLBackendHandler; -import io.shardingjdbc.proxy.frontend.common.FrontendHandler; -import io.shardingjdbc.proxy.frontend.mysql.MySQLFrontendHandler; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; /** * Backend handler factory. diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java index 5c28e1f01c8c1..9fa3b74e19b7d 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java @@ -1,9 +1,31 @@ +/* + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + package io.shardingjdbc.proxy.backend.common; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; +/** + * SQL execute backend handler. + * + * @author wangkai + */ public abstract class CommandResponsePacketsHandler extends ChannelInboundHandlerAdapter { protected abstract void auth(ChannelHandlerContext context, ByteBuf message); protected abstract void executeCommandResponsePackets(ChannelHandlerContext context, ByteBuf message); diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java index 8e210fa92a1cf..0fc7b459bef80 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java @@ -67,7 +67,7 @@ public CommandResponsePackets execute() { @Override protected CommandResponsePackets execute(final SQLStatement sqlStatement, final SQLExecutionUnit sqlExecutionUnit) { Channel channel = ShardingProxyClient.getInstance().getChannelMap().get(sqlExecutionUnit.getDataSource()); - MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(),connectionId); + MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); switch (sqlStatement.getType()) { case DQL: executeQuery(channel, sqlExecutionUnit.getSql()); diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java index e4a137d5329fc..c9adc27c60931 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java @@ -20,7 +20,6 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.shardingjdbc.proxy.backend.common.CommandResponsePacketsHandler; -import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload; import io.shardingjdbc.proxy.transport.mysql.packet.handshake.AuthPluginData; import io.shardingjdbc.proxy.transport.mysql.packet.handshake.HandshakePacket; import io.shardingjdbc.proxy.util.MySQLResultCache; @@ -39,9 +38,9 @@ public void channelRead(final ChannelHandlerContext context, final Object messag //TODO if OKPacket or ERRPacket, then log. if (true) { auth(context, (ByteBuf) message); - } else if(true){ + } else if (true) { - } else{ + } else { } } @@ -49,7 +48,7 @@ public void channelRead(final ChannelHandlerContext context, final Object messag //TODO message to handshakePacket; send handshakeResponse back. @Override protected void auth(ChannelHandlerContext context, ByteBuf message) { - HandshakePacket handshakePacket = new HandshakePacket(1,new AuthPluginData()); + HandshakePacket handshakePacket = new HandshakePacket(1, new AuthPluginData()); } //TODO diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/ShardingProxy.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/ShardingProxy.java index eae0931752164..aea7d862b7fb8 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/ShardingProxy.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/ShardingProxy.java @@ -60,7 +60,7 @@ public final class ShardingProxy { */ public void start(final int port) throws InterruptedException, MalformedURLException { try { - if(ShardingRuleRegistry.WITHOUT_JDBC){ + if (ShardingRuleRegistry.WITHOUT_JDBC) { ShardingProxyClient.getInstance().start(); } ServerBootstrap bootstrap = new ServerBootstrap(); @@ -76,7 +76,7 @@ public void start(final int port) throws InterruptedException, MalformedURLExcep workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); userGroup.shutdownGracefully(); - if(ShardingRuleRegistry.WITHOUT_JDBC){ + if (ShardingRuleRegistry.WITHOUT_JDBC) { ShardingProxyClient.getInstance().stop(); } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java index ec152b7d4a2b3..f598ff866e786 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java @@ -61,8 +61,8 @@ public void delete(int connectionId) { } /** - * @param channelId netty channel id. - * @param connectionId mysql connection id. + * @param channelId netty channel id. + * @param connectionId mysql connection id. */ public void putConnectionMap(String channelId, int connectionId) { connectionCache.put(channelId, connectionId); diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java index 3d4e556df8aa3..63313c7b7e65a 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java @@ -37,7 +37,7 @@ public class SynchronizedFuture implements Future> { private long beginTime = System.currentTimeMillis(); - public SynchronizedFuture(int resultSize){ + public SynchronizedFuture(int resultSize) { latch = new CountDownLatch(resultSize); responses = Lists.newArrayListWithCapacity(resultSize); } @@ -46,10 +46,12 @@ public SynchronizedFuture(int resultSize){ public boolean cancel(boolean mayInterruptIfRunning) { return false; } + @Override public boolean isCancelled() { return false; } + @Override public boolean isDone() { if (null != responses && responses.size() > 0) { From 22e48d897727b303e65fd8ae4648b04f3e772e15 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 4 May 2018 14:42:11 +0800 Subject: [PATCH 069/351] UsualClient --- .../saaavsaaa/client/election/LeaderElection.java | 14 ++++++++++++++ .../com/saaavsaaa/client/zookeeper/Client.java | 4 +++- .../saaavsaaa/client/zookeeper/ClientFactory.java | 6 +----- .../saaavsaaa/client/zookeeper/UsualClient.java | 13 ++++++------- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index de085a67d8aed..4f9274186dd10 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -14,6 +14,7 @@ * Created by aaa */ public abstract class LeaderElection { + private boolean done = false; private boolean contend(final String path, final Client client, final Listener listener) throws KeeperException, InterruptedException { boolean success = false; @@ -47,6 +48,7 @@ public void process(WatchedEvent event) { if (canBegin){ try { action(); + done = true; } catch (Exception ee){ System.out.println("action Exception " + path); ee.printStackTrace(); @@ -55,6 +57,18 @@ public void process(WatchedEvent event) { } } + public void waitDone(){ + while (!done){ + try { + Thread.sleep(10L); + } catch (InterruptedException e) { + System.out.println(e.getMessage()); + } + } + } + // public abstract void actionWhenUnreached() throws KeeperException, InterruptedException; public abstract void action() throws KeeperException, InterruptedException; + + public void callBack(){} } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index f732e6e60cfa2..a2fa967a6f135 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -44,7 +44,9 @@ protected Client(final String servers, final int sessionTimeoutMilliseconds) { public synchronized void start() throws IOException, InterruptedException { zooKeeper = new ZooKeeper(servers, sessionTimeOut, startWatcher()); - zooKeeper.addAuthInfo(scheme , auth); + if (scheme == null || scheme.trim().length() == 0) { + zooKeeper.addAuthInfo(scheme, auth); + } CONNECTED.await(); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 5bfa69b7d2bd0..965f079dc88ff 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -14,8 +14,6 @@ public class ClientFactory { private Client client; private Listener globalListener; private String namespace; - private String scheme; - private byte[] auth; private String servers; private int sessionTimeoutMilliseconds; @@ -48,7 +46,6 @@ public ClientFactory watch(final Listener listener){ public synchronized Client start() throws IOException, InterruptedException { client.setClientFactory(this); client.setRootNode(namespace); - client.setAuthorities(scheme , auth); client.start(); if (globalListener != null) { client.registerWatch(globalListener); @@ -68,8 +65,7 @@ public ClientFactory authorization(String scheme, byte[] auth){ if (scheme == null || scheme.trim().length() == 0) { return this; } - this.scheme = scheme; - this.auth = auth; + client.setAuthorities(scheme , auth); return this; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index e5d691b7dd544..c242196b6c18b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -1,8 +1,6 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.strategy.BaseStrategy; +import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import com.saaavsaaa.client.zookeeper.strategy.ContentionStrategy; import com.saaavsaaa.client.action.IStrategy; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; @@ -31,16 +29,17 @@ public class UsualClient extends Client { @Override public synchronized void start() throws IOException, InterruptedException { super.start(); - UseStrategy(StrategyType.BASE); + useStrategy(StrategyType.USUAL); } - public synchronized void UseStrategy(StrategyType strategyType) { + @Override + public synchronized void useStrategy(StrategyType strategyType) { if (strategies.containsKey(strategyType)){ strategy = strategies.get(strategyType); return; } - if (StrategyType.BASE == strategyType){ - strategy = new BaseStrategy(new Provider(rootNode, this, watched, authorities)); + if (StrategyType.USUAL == strategyType){ + strategy = new UsualStrategy(new Provider(rootNode, this, watched, authorities)); } else { strategy = new ContentionStrategy(new Provider(rootNode, this, watched, authorities)); } From 39a3d5a23206b2faf69507395ab551ae9a380401 Mon Sep 17 00:00:00 2001 From: WangKai Date: Fri, 4 May 2018 15:09:28 +0800 Subject: [PATCH 070/351] fix some merge bugs --- .../common/SQLPacketsBackendHandler.java | 6 ++-- .../text/fieldlist/ComFieldListPacket.java | 5 +--- .../command/text/query/ComQueryPacket.java | 29 ++++++++----------- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java index 0fc7b459bef80..dc8df75d7e195 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java @@ -70,12 +70,12 @@ protected CommandResponsePackets execute(final SQLStatement sqlStatement, final MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); switch (sqlStatement.getType()) { case DQL: - executeQuery(channel, sqlExecutionUnit.getSql()); + executeQuery(channel, sqlExecutionUnit.getSqlUnit().getSql()); case DML: case DDL: - executeUpdate(channel, sqlExecutionUnit.getSql(), sqlStatement); + executeUpdate(channel, sqlExecutionUnit.getSqlUnit().getSql(), sqlStatement); default: - executeCommon(channel, sqlExecutionUnit.getSql()); + executeCommon(channel, sqlExecutionUnit.getSqlUnit().getSql()); } return null; } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java index dc3bb79823a7f..6ef2c7c6225e7 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java @@ -20,12 +20,9 @@ import io.shardingjdbc.core.constant.DatabaseType; import io.shardingjdbc.core.constant.ShardingConstant; import io.shardingjdbc.proxy.backend.common.SQLExecuteBackendHandler; -<<<<<<< HEAD import io.shardingjdbc.proxy.backend.common.SQLPacketsBackendHandler; import io.shardingjdbc.proxy.config.ShardingRuleRegistry; -======= import io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket; ->>>>>>> upstream/dev import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandPacket; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandPacketType; @@ -67,7 +64,7 @@ public CommandResponsePackets execute() { // TODO use common database type if (ShardingRuleRegistry.WITHOUT_JDBC) { return new SQLPacketsBackendHandler(sql, connectionId, DatabaseType.MySQL, true).execute(); - }else { + } else { return new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, true).execute(); } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java index 823c88733eccd..de4bfb8bbf658 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java @@ -19,12 +19,9 @@ import io.shardingjdbc.core.constant.DatabaseType; import io.shardingjdbc.proxy.backend.common.SQLExecuteBackendHandler; -<<<<<<< HEAD import io.shardingjdbc.proxy.backend.common.SQLPacketsBackendHandler; import io.shardingjdbc.proxy.config.ShardingRuleRegistry; -======= import io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket; ->>>>>>> upstream/dev import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandPacket; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; @@ -44,17 +41,15 @@ public final class ComQueryPacket extends CommandPacket { private final String sql; -<<<<<<< HEAD - public ComQueryPacket(final int sequenceId, final int connectionId, MySQLPacketPayload mysqlPacketPayload) { - super(sequenceId, connectionId); -======= private final SQLExecuteBackendHandler sqlExecuteBackendHandler; - public ComQueryPacket(final int sequenceId, final MySQLPacketPayload mysqlPacketPayload) { - super(sequenceId); ->>>>>>> upstream/dev + private final SQLPacketsBackendHandler sqlPacketsBackendHandler; + + public ComQueryPacket(final int sequenceId, final int connectionId, final MySQLPacketPayload mysqlPacketPayload) { + super(sequenceId, connectionId); sql = mysqlPacketPayload.readStringEOF(); sqlExecuteBackendHandler = new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, true); + sqlPacketsBackendHandler = new SQLPacketsBackendHandler(sql, connectionId, DatabaseType.MySQL, true); } @Override @@ -65,14 +60,11 @@ public void write(final MySQLPacketPayload mysqlPacketPayload) { @Override public CommandResponsePackets execute() { log.debug("COM_QUERY received for Sharding-Proxy: {}", sql); -<<<<<<< HEAD if (ShardingRuleRegistry.WITHOUT_JDBC) { - return new SQLPacketsBackendHandler(sql, connectionId, DatabaseType.MySQL, true).execute(); + return sqlPacketsBackendHandler.execute(); } else { - return new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, true).execute(); + return sqlExecuteBackendHandler.execute(); } -======= - return sqlExecuteBackendHandler.execute(); } /** @@ -82,7 +74,11 @@ public CommandResponsePackets execute() { */ public boolean hasMoreResultValue() { try { - return sqlExecuteBackendHandler.hasMoreResultValue(); + if (ShardingRuleRegistry.WITHOUT_JDBC) { + return false; + } else { + return sqlExecuteBackendHandler.hasMoreResultValue(); + } } catch (final SQLException ex) { return false; } @@ -95,6 +91,5 @@ public boolean hasMoreResultValue() { */ public DatabaseProtocolPacket getResultValue() { return sqlExecuteBackendHandler.getResultValue(); ->>>>>>> upstream/dev } } From f9293a61562b6549859956f9a67bf51999ee0add Mon Sep 17 00:00:00 2001 From: WangKai Date: Fri, 4 May 2018 15:11:04 +0800 Subject: [PATCH 071/351] fix some merge bugs --- .../java/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java | 1 - 1 file changed, 1 deletion(-) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java index 8f228921763ec..2992593bab139 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java @@ -26,7 +26,6 @@ import io.shardingjdbc.core.yaml.sharding.YamlShardingConfigurationForProxy; import io.shardingjdbc.proxy.metadata.ProxyShardingMetaData; import lombok.Getter; - import javax.sql.DataSource; import java.io.File; import java.io.IOException; From 60ed83cbbd9a73213cfa5256b07d5907ab3b9a53 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 4 May 2018 16:16:50 +0800 Subject: [PATCH 072/351] test --- .../client/election/LeaderElection.java | 20 +- .../client/zookeeper/CacheAllClient.java | 476 +++++++++--------- .../saaavsaaa/client/zookeeper/Client.java | 2 +- .../client/zookeeper/ClientFactory.java | 6 +- .../saaavsaaa/client/zookeeper/Provider.java | 2 +- .../strategy/ContentionStrategy.java | 17 +- .../zookeeper/ContentionStrategyTest.java | 29 ++ 7 files changed, 292 insertions(+), 260 deletions(-) create mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index 4f9274186dd10..8cb50a3f9ab1a 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -6,6 +6,7 @@ import com.saaavsaaa.client.utility.section.Properties; import com.saaavsaaa.client.utility.section.WatcherCreator; import com.saaavsaaa.client.zookeeper.Client; +import com.saaavsaaa.client.zookeeper.Provider; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; @@ -16,14 +17,14 @@ public abstract class LeaderElection { private boolean done = false; - private boolean contend(final String path, final Client client, final Listener listener) throws KeeperException, InterruptedException { + private boolean contend(final String node, final Provider provider, final Listener listener) throws KeeperException, InterruptedException { boolean success = false; try { - client.createCurrentOnly(Constants.CHANGING_KEY, Properties.INSTANCE.getClientId(), CreateMode.EPHEMERAL); + provider.createCurrentOnly(node, Properties.INSTANCE.getClientId(), CreateMode.EPHEMERAL); success = true; } catch (KeeperException.NodeExistsException e) { // TODO: or changing_key node value == current client id - client.checkExists(Constants.CHANGING_KEY, WatcherCreator.deleteWatcher(PathUtil.getRealPath(path, Constants.CHANGING_KEY), listener)); + provider.checkExists(node, WatcherCreator.deleteWatcher(node, listener)); } return success; } @@ -31,15 +32,16 @@ private boolean contend(final String path, final Client client, final Listener l /* * listener will be register when the contention of the path is unsuccessful */ - public void executeContention(final String path, final Client client) throws KeeperException, InterruptedException { + public void executeContention(final Provider provider) throws KeeperException, InterruptedException { boolean canBegin; - canBegin = this.contend(path, client, new Listener() { + String contendNode = provider.getRealPath(Constants.CHANGING_KEY); + canBegin = this.contend(contendNode, provider, new Listener() { @Override public void process(WatchedEvent event) { try { - executeContention(path, client); + executeContention(provider); } catch (Exception ee){ - System.out.println("Listener Exception " + path); + System.out.println("Listener Exception executeContention"); ee.printStackTrace(); } } @@ -50,10 +52,10 @@ public void process(WatchedEvent event) { action(); done = true; } catch (Exception ee){ - System.out.println("action Exception " + path); + System.out.println("action Exception executeContention"); ee.printStackTrace(); } - client.deleteOnlyCurrent(Constants.CHANGING_KEY); + provider.deleteOnlyCurrent(contendNode); } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java index 1949f37aa8e14..6f0587f06ffe3 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java @@ -23,245 +23,245 @@ */ @Deprecated public final class CacheAllClient extends UsualClient { - private final ScheduledExecutorService cacheService = Executors.newSingleThreadScheduledExecutor(); - private Client usualClient; - - protected PathTree pathTree = null; - +// private final ScheduledExecutorService cacheService = Executors.newSingleThreadScheduledExecutor(); +// private Client usualClient; +// +// protected PathTree pathTree = null; +// CacheAllClient(String servers, int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); } - - @Override - public synchronized void start() throws IOException, InterruptedException { - super.start(); - pathTree = new PathTree(rootNode); - usualClient = this.getClientFactory().newUsualClient().start(); - cacheService.scheduleAtFixedRate(new ClientTask(usualClient) { - @Override - public void run(Client client) throws KeeperException, InterruptedException { - if (PathStatus.RELEASE == pathTree.getStatus()) { - loadCache(client); - } - } - }, Properties.INSTANCE.getThreadInitialDelay(), Properties.INSTANCE.getThreadPeriod(), TimeUnit.MILLISECONDS); - } - - //用替换整树的方式更新 - private synchronized void loadCache(final Client client) throws KeeperException, InterruptedException { - LeaderElection election = new LeaderElection() { - /*@Override - public void actionWhenUnreached() throws KeeperException, InterruptedException { - loadCache(client); - }*/ - - @Override - public void action() throws KeeperException, InterruptedException { - pathTree.loading(client); - } - }; - election.executeContention(rootNode, client); - } - - /* - * closed beta - */ - @Override - public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - LeaderElection election = new LeaderElection() { - /*@Override - public void actionWhenUnreached() throws KeeperException, InterruptedException { - createAllNeedPath(key, value, createMode); - }*/ - - @Override - public void action() throws KeeperException, InterruptedException { - createBegin(key, value, createMode); - } - }; - election.executeContention(rootNode, this); - } - - - private void createBegin(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - super.createCurrentOnly(key, value, createMode); - return; - } - - List nodes = PathUtil.getPathOrderNodes(rootNode, key); - for (int i = 0; i < nodes.size(); i++) { - if (super.checkExists(nodes.get(i))){ - System.out.println("create :" + nodes.get(i)); - continue; - } - System.out.println("create not exist:" + nodes.get(i)); - if (i == nodes.size() - 1){ - super.createCurrentOnly(nodes.get(i), value, createMode); - } else { - this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); - } - } - } - - - @Override - public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { - LeaderElection election = new LeaderElection() { - /*@Override - public void actionWhenUnreached() throws KeeperException, InterruptedException { - deleteAllChildren(key); - }*/ - - @Override - public void action() throws KeeperException, InterruptedException { - String path = PathUtil.getRealPath(rootNode, key); - deleteChildren(path); - pathTree.delete(path); - } - }; - election.executeContention(rootNode, this); - } - - private void deleteChildren(final String key) throws KeeperException, InterruptedException { - List children = super.getChildren(key); - if (children.isEmpty()){ - if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - super.deleteOnlyCurrent(key); - return; - } - return; - } - for (int i = 0; i < children.size(); i++) { - if (!super.checkExists(children.get(i))){ - System.out.println("delete not exist:" + children.get(i)); - continue; - } - this.deleteChildren(key); - } - } - - @Override - public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - LeaderElection election = new LeaderElection() { - /*@Override - public void actionWhenUnreached() throws KeeperException, InterruptedException { - deleteCurrentBranch(key); - }*/ - - @Override - public void action() throws KeeperException, InterruptedException { - String path = PathUtil.getRealPath(rootNode, key); - try { - deleteBranch(path); - } catch (KeeperException.NotEmptyException ee){ - System.out.println(path + " exist other children"); - pathTree.delete(path); - return; - } - } - }; - election.executeContention(rootNode, this); - } - - private void deleteBranch(String key) throws KeeperException, InterruptedException { - deleteChildren(PathUtil.getRealPath(rootNode, key)); - Stack pathStack = PathUtil.getPathReverseNodes(rootNode, key); - while (!pathStack.empty()){ - String node = pathStack.pop(); - // contrast cache - if (checkExists(node)){ - super.deleteOnlyCurrent(key); - System.out.println("delete : " + node); - } - } - } - - private boolean cacheReady(){ - return PathStatus.RELEASE == pathTree.getStatus(); - } - - @Override - public byte[] getData(final String key) throws KeeperException, InterruptedException { - String path = PathUtil.getRealPath(rootNode, key); - if (cacheReady()){ - return pathTree.getValue(path); - } - // without watcher ensure cache execute result consistency - byte[] data = zooKeeper.getData(path, false, null); - pathTree.put(path, new String(data)); - return data; - } - - @Override - public boolean checkExists(final String key) throws KeeperException, InterruptedException { - String path = PathUtil.getRealPath(rootNode, key); - if (cacheReady()){ - return null != pathTree.getValue(path); - } - return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), false); - } - - @Override - public List getChildren(final String key) throws KeeperException, InterruptedException { - String path = PathUtil.getRealPath(rootNode, key); - if (cacheReady()){ - return pathTree.getChildren(path); - } - return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); - } - - @Override - public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { -// super.createCurrentOnly(key, value, createMode); - LeaderElection election = new LeaderElection() { -/* @Override - public void actionWhenUnreached() throws KeeperException, InterruptedException { - createCurrentOnly(key, value, createMode); - } - */ - @Override - public void action() throws KeeperException, InterruptedException { - usualClient.createCurrentOnly(key, value, createMode); - } - }; - election.executeContention(rootNode, this); - pathTree.put(PathUtil.getRealPath(rootNode, key), value); - } - - @Override - public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { -// super.deleteOnlyCurrent(key); - LeaderElection election = new LeaderElection() { - /*@Override - public void actionWhenUnreached() throws KeeperException, InterruptedException { - deleteOnlyCurrent(key); - }*/ - - @Override - public void action() throws KeeperException, InterruptedException { - usualClient.deleteOnlyCurrent(key); - } - }; - election.executeContention(rootNode, this); - pathTree.delete(PathUtil.getRealPath(rootNode, key)); - } - - @Override - public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { -// super.deleteOnlyCurrent(key, callback, ctx); - LeaderElection election = new LeaderElection() { - /*@Override - public void actionWhenUnreached() throws KeeperException, InterruptedException { - deleteOnlyCurrent(key, callback, ctx); - }*/ - - @Override - public void action() throws KeeperException, InterruptedException { - usualClient.deleteOnlyCurrent(key, callback, ctx); - } - }; - election.executeContention(rootNode, this); - pathTree.delete(PathUtil.getRealPath(rootNode, key)); - } +// +// @Override +// public synchronized void start() throws IOException, InterruptedException { +// super.start(); +// pathTree = new PathTree(rootNode); +// usualClient = this.getClientFactory().newUsualClient().start(); +// cacheService.scheduleAtFixedRate(new ClientTask(usualClient) { +// @Override +// public void run(Client client) throws KeeperException, InterruptedException { +// if (PathStatus.RELEASE == pathTree.getStatus()) { +// loadCache(client); +// } +// } +// }, Properties.INSTANCE.getThreadInitialDelay(), Properties.INSTANCE.getThreadPeriod(), TimeUnit.MILLISECONDS); +// } +// +// //用替换整树的方式更新 +// private synchronized void loadCache(final Client client) throws KeeperException, InterruptedException { +// LeaderElection election = new LeaderElection() { +// /*@Override +// public void actionWhenUnreached() throws KeeperException, InterruptedException { +// loadCache(client); +// }*/ +// +// @Override +// public void action() throws KeeperException, InterruptedException { +// pathTree.loading(client); +// } +// }; +// election.executeContention(rootNode, client); +// } +// +// /* +// * closed beta +// */ +// @Override +// public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { +// LeaderElection election = new LeaderElection() { +// /*@Override +// public void actionWhenUnreached() throws KeeperException, InterruptedException { +// createAllNeedPath(key, value, createMode); +// }*/ +// +// @Override +// public void action() throws KeeperException, InterruptedException { +// createBegin(key, value, createMode); +// } +// }; +// election.executeContention(rootNode, this); +// } +// +// +// private void createBegin(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { +// if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ +// super.createCurrentOnly(key, value, createMode); +// return; +// } +// +// List nodes = PathUtil.getPathOrderNodes(rootNode, key); +// for (int i = 0; i < nodes.size(); i++) { +// if (super.checkExists(nodes.get(i))){ +// System.out.println("create :" + nodes.get(i)); +// continue; +// } +// System.out.println("create not exist:" + nodes.get(i)); +// if (i == nodes.size() - 1){ +// super.createCurrentOnly(nodes.get(i), value, createMode); +// } else { +// this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); +// } +// } +// } +// +// +// @Override +// public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { +// LeaderElection election = new LeaderElection() { +// /*@Override +// public void actionWhenUnreached() throws KeeperException, InterruptedException { +// deleteAllChildren(key); +// }*/ +// +// @Override +// public void action() throws KeeperException, InterruptedException { +// String path = PathUtil.getRealPath(rootNode, key); +// deleteChildren(path); +// pathTree.delete(path); +// } +// }; +// election.executeContention(rootNode, this); +// } +// +// private void deleteChildren(final String key) throws KeeperException, InterruptedException { +// List children = super.getChildren(key); +// if (children.isEmpty()){ +// if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ +// super.deleteOnlyCurrent(key); +// return; +// } +// return; +// } +// for (int i = 0; i < children.size(); i++) { +// if (!super.checkExists(children.get(i))){ +// System.out.println("delete not exist:" + children.get(i)); +// continue; +// } +// this.deleteChildren(key); +// } +// } +// +// @Override +// public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { +// LeaderElection election = new LeaderElection() { +// /*@Override +// public void actionWhenUnreached() throws KeeperException, InterruptedException { +// deleteCurrentBranch(key); +// }*/ +// +// @Override +// public void action() throws KeeperException, InterruptedException { +// String path = PathUtil.getRealPath(rootNode, key); +// try { +// deleteBranch(path); +// } catch (KeeperException.NotEmptyException ee){ +// System.out.println(path + " exist other children"); +// pathTree.delete(path); +// return; +// } +// } +// }; +// election.executeContention(rootNode, this); +// } +// +// private void deleteBranch(String key) throws KeeperException, InterruptedException { +// deleteChildren(PathUtil.getRealPath(rootNode, key)); +// Stack pathStack = PathUtil.getPathReverseNodes(rootNode, key); +// while (!pathStack.empty()){ +// String node = pathStack.pop(); +// // contrast cache +// if (checkExists(node)){ +// super.deleteOnlyCurrent(key); +// System.out.println("delete : " + node); +// } +// } +// } +// +// private boolean cacheReady(){ +// return PathStatus.RELEASE == pathTree.getStatus(); +// } +// +// @Override +// public byte[] getData(final String key) throws KeeperException, InterruptedException { +// String path = PathUtil.getRealPath(rootNode, key); +// if (cacheReady()){ +// return pathTree.getValue(path); +// } +// // without watcher ensure cache execute result consistency +// byte[] data = zooKeeper.getData(path, false, null); +// pathTree.put(path, new String(data)); +// return data; +// } +// +// @Override +// public boolean checkExists(final String key) throws KeeperException, InterruptedException { +// String path = PathUtil.getRealPath(rootNode, key); +// if (cacheReady()){ +// return null != pathTree.getValue(path); +// } +// return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), false); +// } +// +// @Override +// public List getChildren(final String key) throws KeeperException, InterruptedException { +// String path = PathUtil.getRealPath(rootNode, key); +// if (cacheReady()){ +// return pathTree.getChildren(path); +// } +// return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); +// } +// +// @Override +// public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { +//// super.createCurrentOnly(key, value, createMode); +// LeaderElection election = new LeaderElection() { +///* @Override +// public void actionWhenUnreached() throws KeeperException, InterruptedException { +// createCurrentOnly(key, value, createMode); +// } +// */ +// @Override +// public void action() throws KeeperException, InterruptedException { +// usualClient.createCurrentOnly(key, value, createMode); +// } +// }; +// election.executeContention(rootNode, this); +// pathTree.put(PathUtil.getRealPath(rootNode, key), value); +// } +// +// @Override +// public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { +//// super.deleteOnlyCurrent(key); +// LeaderElection election = new LeaderElection() { +// /*@Override +// public void actionWhenUnreached() throws KeeperException, InterruptedException { +// deleteOnlyCurrent(key); +// }*/ +// +// @Override +// public void action() throws KeeperException, InterruptedException { +// usualClient.deleteOnlyCurrent(key); +// } +// }; +// election.executeContention(rootNode, this); +// pathTree.delete(PathUtil.getRealPath(rootNode, key)); +// } +// +// @Override +// public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { +//// super.deleteOnlyCurrent(key, callback, ctx); +// LeaderElection election = new LeaderElection() { +// /*@Override +// public void actionWhenUnreached() throws KeeperException, InterruptedException { +// deleteOnlyCurrent(key, callback, ctx); +// }*/ +// +// @Override +// public void action() throws KeeperException, InterruptedException { +// usualClient.deleteOnlyCurrent(key, callback, ctx); +// } +// }; +// election.executeContention(rootNode, this); +// pathTree.delete(PathUtil.getRealPath(rootNode, key)); +// } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index a2fa967a6f135..d7bccdda2a06c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -44,7 +44,7 @@ protected Client(final String servers, final int sessionTimeoutMilliseconds) { public synchronized void start() throws IOException, InterruptedException { zooKeeper = new ZooKeeper(servers, sessionTimeOut, startWatcher()); - if (scheme == null || scheme.trim().length() == 0) { + if (!StringUtil.isNullOrBlank(scheme)) { zooKeeper.addAuthInfo(scheme, auth); } CONNECTED.await(); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 965f079dc88ff..5bfa69b7d2bd0 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -14,6 +14,8 @@ public class ClientFactory { private Client client; private Listener globalListener; private String namespace; + private String scheme; + private byte[] auth; private String servers; private int sessionTimeoutMilliseconds; @@ -46,6 +48,7 @@ public ClientFactory watch(final Listener listener){ public synchronized Client start() throws IOException, InterruptedException { client.setClientFactory(this); client.setRootNode(namespace); + client.setAuthorities(scheme , auth); client.start(); if (globalListener != null) { client.registerWatch(globalListener); @@ -65,7 +68,8 @@ public ClientFactory authorization(String scheme, byte[] auth){ if (scheme == null || scheme.trim().length() == 0) { return this; } - client.setAuthorities(scheme , auth); + this.scheme = scheme; + this.auth = auth; return this; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java b/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java index b9fdf9bfd5995..fa324d896f40b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java @@ -112,7 +112,7 @@ public Stack getDeletingPaths(String key) { @Override public void executeContention(final LeaderElection election) throws KeeperException, InterruptedException { - election.executeContention(rootNode, client); + election.executeContention(this); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index b1b27d64fd632..a4702c0fb4702 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -80,6 +80,7 @@ public void action() throws KeeperException, InterruptedException { createBegin(key, value, createMode); } }; + provider.executeContention(election); } @@ -91,7 +92,7 @@ private void createBegin(final String key, final String value, final CreateMode List nodes = provider.getNecessaryPaths(key); for (int i = 0; i < nodes.size(); i++) { if (provider.checkExists(nodes.get(i))){ - System.out.println("create :" + nodes.get(i)); + System.out.println("create exist:" + nodes.get(i)); continue; } System.out.println("create not exist:" + nodes.get(i)); @@ -115,13 +116,6 @@ public void action() throws KeeperException, InterruptedException { private void deleteChildren(final String key, final boolean deleteCurrentNode) throws KeeperException, InterruptedException { List children = provider.getChildren(key); - if (children.isEmpty()){ - if (deleteCurrentNode){ - provider.deleteOnlyCurrent(key); - return; - } - return; - } for (int i = 0; i < children.size(); i++) { String child = PathUtil.getRealPath(key, children.get(i)); if (!provider.checkExists(child)){ @@ -130,6 +124,9 @@ private void deleteChildren(final String key, final boolean deleteCurrentNode) t } deleteChildren(child, true); } + if (deleteCurrentNode){ + provider.deleteOnlyCurrent(key); + } } @Override @@ -138,7 +135,7 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr @Override public void action() throws KeeperException, InterruptedException { try { - deleteBranch(key); + deleteBranch(provider.getRealPath(key)); } catch (KeeperException.NotEmptyException ee){ System.out.println(key + " exist other children"); return; @@ -154,7 +151,7 @@ private void deleteBranch(String key) throws KeeperException, InterruptedExcepti String node = pathStack.pop(); // contrast cache if (checkExists(node)){ - provider.deleteOnlyCurrent(key); + provider.deleteOnlyCurrent(node); System.out.println("delete : " + node); } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java new file mode 100644 index 0000000000000..a80a3280a3707 --- /dev/null +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java @@ -0,0 +1,29 @@ +package com.saaavsaaa.client.zookeeper; + +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.zookeeper.Client; +import com.saaavsaaa.client.zookeeper.ClientFactory; +import com.saaavsaaa.client.zookeeper.TestSupport; +import com.saaavsaaa.client.zookeeper.UsualClientTest; +import com.saaavsaaa.client.zookeeper.strategy.StrategyType; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.junit.Ignore; +import org.junit.Test; + +import java.io.IOException; +import java.util.List; + +/** + * Created by aaa on 18-5-4. + */ +public class ContentionStrategyTest extends UsualClientTest { + @Override + protected Client createClient(final ClientFactory creator) throws IOException, InterruptedException { + Listener listener = TestSupport.buildListener(); + Client client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + client.useStrategy(StrategyType.CONTENTION); + return client; + } +} From f7c180ba6fa43130b2b62ebff8d588179a4fbe4f Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 4 May 2018 16:21:04 +0800 Subject: [PATCH 073/351] test --- .../saaavsaaa/client/election/LeaderElection.java | 2 -- .../zookeeper/strategy/ContentionStrategy.java | 6 +++--- .../client/zookeeper/strategy/UsualStrategy.java | 1 - .../client/zookeeper/ContentionStrategyTest.java | 12 ++---------- .../saaavsaaa/client/zookeeper/UsualClientTest.java | 1 - .../client/zookeeper/UsualWatchClientTest.java | 2 -- 6 files changed, 5 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index 8cb50a3f9ab1a..64f8146182f79 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -1,11 +1,9 @@ package com.saaavsaaa.client.election; -import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.utility.section.Properties; import com.saaavsaaa.client.utility.section.WatcherCreator; -import com.saaavsaaa.client.zookeeper.Client; import com.saaavsaaa.client.zookeeper.Provider; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index a4702c0fb4702..9828a62dde5e6 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -3,10 +3,10 @@ import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.Client; import com.saaavsaaa.client.zookeeper.Provider; -import org.apache.zookeeper.*; -import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; import java.util.List; import java.util.Stack; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java index c5ee80ae7f8aa..147c4e0000d6e 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java @@ -1,6 +1,5 @@ package com.saaavsaaa.client.zookeeper.strategy; -import com.saaavsaaa.client.action.IStrategy; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.Provider; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java index a80a3280a3707..63f2ac0419b6a 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java @@ -1,19 +1,9 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.Listener; -import com.saaavsaaa.client.zookeeper.Client; -import com.saaavsaaa.client.zookeeper.ClientFactory; -import com.saaavsaaa.client.zookeeper.TestSupport; -import com.saaavsaaa.client.zookeeper.UsualClientTest; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.junit.Ignore; -import org.junit.Test; import java.io.IOException; -import java.util.List; /** * Created by aaa on 18-5-4. @@ -26,4 +16,6 @@ protected Client createClient(final ClientFactory creator) throws IOException, I client.useStrategy(StrategyType.CONTENTION); return client; } + + //todo test node contention case } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index 38e3f4f031cce..76a2de458f781 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -1,6 +1,5 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.utility.section.Listener; import org.apache.zookeeper.KeeperException; import org.junit.Test; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java index 5fecbcf9a9394..ce31488d98742 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java @@ -1,8 +1,6 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.utility.section.Listener; -import org.apache.zookeeper.KeeperException; -import org.junit.Test; import java.io.IOException; From b1509f279b21f4c1f532720b3906dee36e6d9d64 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 4 May 2018 18:51:02 +0800 Subject: [PATCH 074/351] cache restart --- .../saaavsaaa/client/action/IStrategy.java | 3 + .../com/saaavsaaa/client/cache/PathNode.java | 50 +++++++++++++--- .../com/saaavsaaa/client/cache/PathTree.java | 59 +++++++++++-------- .../saaavsaaa/client/utility/PathUtil.java | 19 ++++++ .../client/utility/section/ClientTask.java | 11 ++-- .../client/zookeeper/ClientFactory.java | 7 --- .../client/zookeeper/UsualClient.java | 4 +- .../zookeeper/strategy/BaseStrategy.java | 4 ++ .../strategy/ContentionStrategy.java | 12 ++-- .../client/zookeeper/CacheClientTest.java | 2 +- .../client/zookeeper/CacheWathClientTest.java | 2 +- 11 files changed, 117 insertions(+), 56 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/action/IStrategy.java b/src/main/java/com/saaavsaaa/client/action/IStrategy.java index b6880b6a7436e..ad4f88560d704 100644 --- a/src/main/java/com/saaavsaaa/client/action/IStrategy.java +++ b/src/main/java/com/saaavsaaa/client/action/IStrategy.java @@ -1,7 +1,10 @@ package com.saaavsaaa.client.action; +import com.saaavsaaa.client.zookeeper.Provider; + /** * Created by aaa on 18-5-2. */ public interface IStrategy extends IClient { + Provider getProvider(); } diff --git a/src/main/java/com/saaavsaaa/client/cache/PathNode.java b/src/main/java/com/saaavsaaa/client/cache/PathNode.java index 7bc699cd131ba..b6eae76703f04 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathNode.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathNode.java @@ -3,23 +3,25 @@ import com.saaavsaaa.client.utility.constant.Constants; import java.util.HashMap; +import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * Created by aaa - * todo nodeKey can use current node short path: get children.containsKey(path) should be change ,because path can like /root/aaa/aaa */ public class PathNode { private final String nodeKey; + private byte[] value; private Map children = new ConcurrentHashMap<>(); - public PathNode(final String key) { + PathNode(final String key) { this(key, Constants.RELEASE_VALUE); } - public PathNode(final String key, final byte[] value) { + PathNode(final String key, final byte[] value) { this.nodeKey = key; this.value = value; } @@ -32,10 +34,46 @@ public String getKey(){ return this.nodeKey; } - public void attechChild(final PathNode node) { + public void attachChild(final PathNode node) { this.children.put(node.nodeKey, node); } + + PathNode set(final Iterator iterator, final String value){ + String key = iterator.next(); + PathNode node = children.get(key); + if (node == null){ + node = new PathNode(key); + children.put(key, node); + } + if (iterator.hasNext()){ + node.set(iterator, value); + } else { + node.setValue(value.getBytes(Constants.UTF_8)); + } + return node; + } + + PathNode get(final Iterator iterator){ + String key = iterator.next(); + PathNode node = children.get(key); + if (node == null){ + return null; + } + if (iterator.hasNext()){ + return node.get(iterator); + } + return node; + } + + public byte[] getValue() { + return value; + } + public void setValue(byte[] value) { + this.value = value; + } + + @Deprecated PathNode get(final int index, final String path) { if (children.isEmpty()){ return null; @@ -50,8 +88,4 @@ PathNode get(final int index, final String path) { return children.get(path.substring(0, nextSeparate)).get(nextSeparate + 1, path); } - - public byte[] getValue() { - return value; - } } diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index 89302d7934f10..d164bef01bdd4 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -2,20 +2,18 @@ import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.Client; +import com.saaavsaaa.client.zookeeper.Provider; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.common.PathUtils; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; /** * Created by aaa */ public final class PathTree { - private final Map currentNodes = new ConcurrentHashMap<>(); private PathNode rootNode; private PathStatus Status; @@ -24,27 +22,28 @@ public PathTree(final String root) { this.Status = PathStatus.RELEASE; } - public void loading(final Client client) throws KeeperException, InterruptedException { + public void loading(final Provider provider) throws KeeperException, InterruptedException { this.setStatus(PathStatus.CHANGING); PathNode newRoot = new PathNode(rootNode.getKey()); - List children = client.getChildren(rootNode.getKey()); - children.remove(PathUtil.getRealPath(rootNode.getKey(), Constants.CHANGING_KEY)); - this.attechIntoNode(children, newRoot, client); + List children = provider.getChildren(rootNode.getKey()); + children.remove(provider.getRealPath(Constants.CHANGING_KEY)); + this.attechIntoNode(children, newRoot, provider); rootNode = newRoot; this.setStatus(PathStatus.RELEASE); } - private void attechIntoNode(final List children, final PathNode pathNode, final Client client) throws KeeperException, InterruptedException { + private void attechIntoNode(final List children, final PathNode pathNode, final Provider provider) throws KeeperException, InterruptedException { if (children.isEmpty()){ return; } for (String child : children) { - PathNode current = new PathNode(PathUtil.getRealPath(pathNode.getKey(), child), client.getData(child)); - pathNode.attechChild(current); - List subs = client.getChildren(child); - this.attechIntoNode(subs, current, client); + String childPath = PathUtil.getRealPath(pathNode.getKey(), child); + PathNode current = new PathNode(PathUtil.checkPath(child), provider.getData(childPath)); + pathNode.attachChild(current); + List subs = provider.getChildren(childPath); + this.attechIntoNode(subs, current, provider); } } @@ -53,9 +52,6 @@ public PathStatus getStatus() { } public void setStatus(final PathStatus status) { - if (PathStatus.RELEASE == status){ - currentNodes.clear(); - } Status = status; } @@ -64,16 +60,20 @@ public PathNode getRootNode() { } public byte[] getValue(final String path){ - if (currentNodes.containsKey(path)){ - return currentNodes.get(path).getBytes(Constants.UTF_8); - } PathNode node = get(path); return null == node ? null : node.getValue(); } private PathNode get(final String path){ PathUtils.validatePath(path); - return rootNode.get(1, path); + return rootNode.get(keyIterator(path)); //rootNode.get(1, path); + } + + private Iterator keyIterator(final String path){ + List nodes = PathUtil.getShortPathNodes(path); + Iterator iterator = nodes.iterator(); + iterator.next(); // root + return iterator; } public List getChildren(String path) { @@ -87,20 +87,27 @@ public List getChildren(String path) { } Iterator children = node.getChildren().values().iterator(); while (children.hasNext()){ - // children keys don't needn't currentNodes result.add(new String(children.next().getValue())); } return result; } - public void put(String path, String value) { + public void put(final String path, final String value) { PathUtils.validatePath(path); - currentNodes.put(path, value); + if (Status == Status.RELEASE){ + rootNode.set(keyIterator(path), value); + } else { + try { + Thread.sleep(50L); + } catch (InterruptedException e) { + System.out.println("cache put status not release"); + } + put(path, value); + } } public void delete(String path) { PathUtils.validatePath(path); - currentNodes.remove(path); String prxpath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); PathNode node = get(prxpath); node.getChildren().remove(path); diff --git a/src/main/java/com/saaavsaaa/client/utility/PathUtil.java b/src/main/java/com/saaavsaaa/client/utility/PathUtil.java index 047c69c4cf10c..8dfbde31e27b2 100644 --- a/src/main/java/com/saaavsaaa/client/utility/PathUtil.java +++ b/src/main/java/com/saaavsaaa/client/utility/PathUtil.java @@ -62,6 +62,25 @@ public static List getPathOrderNodes(final String root, String path){ return paths; } + public static List getShortPathNodes(String path){ + path = checkPath(path); + List paths = new ArrayList<>(); + char[] chars = path.toCharArray(); + StringBuilder builder = new StringBuilder(Constants.PATH_SEPARATOR); + for (int i = 1; i < chars.length; i++) { + if (chars[i] == Constants.PATH_SEPARATOR.charAt(0)){ + paths.add(builder.toString()); + builder = new StringBuilder(Constants.PATH_SEPARATOR); + continue; + } + builder.append(chars[i]); + if (i == chars.length - 1){ + paths.add(builder.toString()); + } + } + return paths; + } + public static List breadthToB(TreeNode root) { List lists = new ArrayList<>(); if(root==null) diff --git a/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java b/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java index 55d3289b6918e..9946e703cc41c 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java +++ b/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java @@ -1,23 +1,24 @@ package com.saaavsaaa.client.utility.section; import com.saaavsaaa.client.zookeeper.Client; +import com.saaavsaaa.client.zookeeper.Provider; import org.apache.zookeeper.KeeperException; /** * Created by aaa */ public abstract class ClientTask implements Runnable { - private final Client client; - public ClientTask(final Client client){ - this.client = client; + private final Provider provider; + public ClientTask(final Provider provider){ + this.provider = provider; } - public abstract void run(final Client client) throws KeeperException, InterruptedException; + public abstract void run(final Provider provider) throws KeeperException, InterruptedException; @Override public void run() { try { - run(client); + run(provider); } catch (KeeperException e) { System.out.println("ClientTask"); e.printStackTrace(); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 5bfa69b7d2bd0..1f7951a686db7 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -21,13 +21,6 @@ public class ClientFactory { public ClientFactory(){} - public ClientFactory newCacheClient(final String servers, final int sessionTimeoutMilliseconds) { - this.servers = servers; - this.sessionTimeoutMilliseconds = sessionTimeoutMilliseconds; - client = new CacheClient(servers, sessionTimeoutMilliseconds); - return this; - } - public ClientFactory newUsualClient(final String servers, final int sessionTimeoutMilliseconds) { this.servers = servers; this.sessionTimeoutMilliseconds = sessionTimeoutMilliseconds; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index c242196b6c18b..ebb0e9498730a 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -19,8 +19,8 @@ * Created by aaa on 18-5-2. */ public class UsualClient extends Client { - private final Map strategies = new ConcurrentHashMap<>(); - private IStrategy strategy; + protected final Map strategies = new ConcurrentHashMap<>(); + protected IStrategy strategy; UsualClient(final String servers, final int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java index 9dfcd017e331d..f81dbbcfbb0ee 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java @@ -17,4 +17,8 @@ public BaseStrategy(final Provider provider){ public String getDataString(final String key) throws KeeperException, InterruptedException { return new String(getData(key)); } + + public Provider getProvider() { + return provider; + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index 9828a62dde5e6..dffc56691e0dc 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -91,15 +91,15 @@ private void createBegin(final String key, final String value, final CreateMode } List nodes = provider.getNecessaryPaths(key); for (int i = 0; i < nodes.size(); i++) { - if (provider.checkExists(nodes.get(i))){ + if (this.checkExists(nodes.get(i))){ System.out.println("create exist:" + nodes.get(i)); continue; } System.out.println("create not exist:" + nodes.get(i)); if (i == nodes.size() - 1){ - provider.createCurrentOnly(nodes.get(i), value, createMode); + this.createCurrentOnly(nodes.get(i), value, createMode); } else { - provider.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); + this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); } } } @@ -118,14 +118,14 @@ private void deleteChildren(final String key, final boolean deleteCurrentNode) t List children = provider.getChildren(key); for (int i = 0; i < children.size(); i++) { String child = PathUtil.getRealPath(key, children.get(i)); - if (!provider.checkExists(child)){ + if (!this.checkExists(child)){ System.out.println("delete not exist:" + child); continue; } deleteChildren(child, true); } if (deleteCurrentNode){ - provider.deleteOnlyCurrent(key); + this.deleteOnlyCurrent(key); } } @@ -151,7 +151,7 @@ private void deleteBranch(String key) throws KeeperException, InterruptedExcepti String node = pathStack.pop(); // contrast cache if (checkExists(node)){ - provider.deleteOnlyCurrent(node); + this.deleteOnlyCurrent(node); System.out.println("delete : " + node); } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java index ce8e5d30df352..292ec9e8edd9a 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java @@ -12,6 +12,6 @@ public class CacheClientTest extends UsualClientTest { @Override protected Client createClient(ClientFactory creator) throws IOException, InterruptedException { - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java index aca09affd86d8..c2e9427db95b1 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java @@ -11,6 +11,6 @@ public class CacheWathClientTest extends UsualClientTest { @Override protected Client createClient(ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); } } From abbfdfe7476a3116b7d4deb1b0408df1a39e2e40 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 4 May 2018 19:00:35 +0800 Subject: [PATCH 075/351] cache restart --- .../client/zookeeper/CacheClient.java | 105 +++++++++++++++++- 1 file changed, 103 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 515963e9e5534..2ce02fe519932 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -1,11 +1,112 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.cache.PathStatus; +import com.saaavsaaa.client.cache.PathTree; +import com.saaavsaaa.client.election.LeaderElection; +import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.utility.section.ClientTask; +import com.saaavsaaa.client.utility.section.Properties; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; + +import java.io.IOException; +import java.util.List; +import java.util.Stack; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + /** - * Created by aaa on 18-5-2. - * todo log + * Created by aaa */ public final class CacheClient extends UsualClient { + private final ScheduledExecutorService cacheService = Executors.newSingleThreadScheduledExecutor(); + protected PathTree pathTree = null; + CacheClient(String servers, int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); } + + @Override + public synchronized void start() throws IOException, InterruptedException { + super.start(); + pathTree = new PathTree(rootNode); + cacheService.scheduleAtFixedRate(new ClientTask(strategy.getProvider()) { + @Override + public void run(Provider provider) throws KeeperException, InterruptedException { + if (PathStatus.RELEASE == pathTree.getStatus()) { + loadCache(provider); + } + } + }, Properties.INSTANCE.getThreadInitialDelay(), Properties.INSTANCE.getThreadPeriod(), TimeUnit.MILLISECONDS); + } + + //用替换整树的方式更新 + private synchronized void loadCache(final Provider provider) throws KeeperException, InterruptedException { + LeaderElection election = new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + pathTree.loading(provider); + } + }; + provider.executeContention(election); + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + strategy.createCurrentOnly(key, value, createMode); + pathTree.put(PathUtil.getRealPath(rootNode, key), value); + System.out.println("cache put : " + key); + } + + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + strategy.deleteOnlyCurrent(key); + pathTree.delete(PathUtil.getRealPath(rootNode, key)); + } + + @Override + public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + strategy.deleteOnlyCurrent(key, callback, ctx); + pathTree.delete(PathUtil.getRealPath(rootNode, key)); + } + + //================================================================== + + private boolean cacheReady(){ + return PathStatus.RELEASE == pathTree.getStatus(); + } + + @Override + public byte[] getData(final String key) throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(rootNode, key); + if (cacheReady()){ + return pathTree.getValue(path); + } + // without watcher ensure cache execute result consistency + byte[] data = zooKeeper.getData(path, false, null); + pathTree.put(path, new String(data)); + return data; + } + + @Override + public boolean checkExists(final String key) throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(rootNode, key); + if (cacheReady()){ + return null != pathTree.getValue(path); + } + return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), false); + } + + @Override + public List getChildren(final String key) throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(rootNode, key); + if (cacheReady()){ + return pathTree.getChildren(path); + } + return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); + } } From f438fa68ca96780c4c94232992dc8453444ee845 Mon Sep 17 00:00:00 2001 From: WangKai Date: Fri, 4 May 2018 21:35:45 +0800 Subject: [PATCH 076/351] gencericResponse and part of handshake --- .../common/CommandResponsePacketsHandler.java | 6 +- .../common/SQLPacketsBackendHandler.java | 2 +- .../backend/mysql/MySQLBackendHandler.java | 83 +++++++++++++++---- .../frontend/mysql/MySQLFrontendHandler.java | 1 + .../mysql/packet/generic/EofPacket.java | 5 +- .../mysql/packet/generic/ErrPacket.java | 5 +- .../mysql/packet/generic/OKPacket.java | 5 +- 7 files changed, 84 insertions(+), 23 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java index 9fa3b74e19b7d..eb6286e961cda 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java @@ -20,6 +20,7 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; +import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload; /** * SQL execute backend handler. @@ -27,6 +28,7 @@ * @author wangkai */ public abstract class CommandResponsePacketsHandler extends ChannelInboundHandlerAdapter { - protected abstract void auth(ChannelHandlerContext context, ByteBuf message); - protected abstract void executeCommandResponsePackets(ChannelHandlerContext context, ByteBuf message); + protected abstract void auth(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload); + protected abstract void genericResponsePacket(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload); + protected abstract void executeCommandResponsePackets(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload); } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java index dc8df75d7e195..93394226998e8 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java @@ -67,7 +67,7 @@ public CommandResponsePackets execute() { @Override protected CommandResponsePackets execute(final SQLStatement sqlStatement, final SQLExecutionUnit sqlExecutionUnit) { Channel channel = ShardingProxyClient.getInstance().getChannelMap().get(sqlExecutionUnit.getDataSource()); - MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); + //MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); switch (sqlStatement.getType()) { case DQL: executeQuery(channel, sqlExecutionUnit.getSqlUnit().getSql()); diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java index c9adc27c60931..e0aa9167ebbdd 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java @@ -17,45 +17,100 @@ package io.shardingjdbc.proxy.backend.mysql; +import com.google.common.primitives.Bytes; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.shardingjdbc.proxy.backend.common.CommandResponsePacketsHandler; -import io.shardingjdbc.proxy.transport.mysql.packet.handshake.AuthPluginData; -import io.shardingjdbc.proxy.transport.mysql.packet.handshake.HandshakePacket; +import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload; +import io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket; +import io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket; +import io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket; import io.shardingjdbc.proxy.util.MySQLResultCache; +import lombok.extern.slf4j.Slf4j; /** * Backend handler. * * @author wangkai */ +@Slf4j public class MySQLBackendHandler extends CommandResponsePacketsHandler { + private boolean authorized; + @Override public void channelRead(final ChannelHandlerContext context, final Object message) { - //TODO if handshake, then invoke auth. - //TODO if execute command, then invoke executeCommandResponsePackets. - //TODO if OKPacket or ERRPacket, then log. - if (true) { - auth(context, (ByteBuf) message); - } else if (true) { - + MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload((ByteBuf) message); + int packetSize = mysqlPacketPayload.readInt3(); + int sequenceId = mysqlPacketPayload.readInt1(); + int header = mysqlPacketPayload.readInt1(); + if (!authorized) { + auth(context, header, mysqlPacketPayload); + } else if (OKPacket.HEADER == header || ErrPacket.HEADER == header || EofPacket.HEADER == header) { + genericResponsePacket(context, header, mysqlPacketPayload); } else { - + executeCommandResponsePackets(context, header, mysqlPacketPayload); } } - //TODO message to handshakePacket; send handshakeResponse back. @Override - protected void auth(ChannelHandlerContext context, ByteBuf message) { - HandshakePacket handshakePacket = new HandshakePacket(1, new AuthPluginData()); + protected void auth(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload) { + int protocolVersion = header; + String serverVersion = mysqlPacketPayload.readStringNul(); + int connectionId = mysqlPacketPayload.readInt4(); + MySQLResultCache.getInstance().putConnectionMap(context.channel().id().asShortText(), connectionId); + byte[] authPluginDataPart1 = mysqlPacketPayload.readStringNul().getBytes(); + int capabilityFlagsLower = mysqlPacketPayload.readInt2(); + int charset = mysqlPacketPayload.readInt1(); + int statusFlag = mysqlPacketPayload.readInt2(); + int capabilityFlagsUpper = mysqlPacketPayload.readInt2(); + int authPluginDataLength = mysqlPacketPayload.readInt1(); + mysqlPacketPayload.skipReserved(10); + byte[] authPluginDataPart2 = mysqlPacketPayload.readStringNul().getBytes(); + byte[] authPluginData = Bytes.concat(authPluginDataPart1, authPluginDataPart2); + //TODO get username, password(encrypt) and schema to write message. + + } + + @Override + protected void genericResponsePacket(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload) { + switch (header) { + case OKPacket.HEADER: + if (!authorized) { + authorized = true; + } + long affectedRows = mysqlPacketPayload.readIntLenenc(); + long lastInsertId = mysqlPacketPayload.readIntLenenc(); + int statusFlags = mysqlPacketPayload.readInt2(); + int warnings = mysqlPacketPayload.readInt2(); + String info = mysqlPacketPayload.readStringEOF(); + log.debug("OKPacket[affectedRows={},lastInsertId={},statusFlags={},warnings={},info={}]", affectedRows, lastInsertId, statusFlags, warnings, info); + break; + case ErrPacket.HEADER: + int errorCode = mysqlPacketPayload.readInt2(); + String sqlStateMarker = mysqlPacketPayload.readStringFix(1); + String sqlState = mysqlPacketPayload.readStringFix(5); + String errorMessage = mysqlPacketPayload.readStringEOF(); + log.debug("ErrPacket[errorCode={},sqlStateMarker={},sqlState={},errorMessage={}]", errorCode, sqlStateMarker, sqlState, errorMessage); + break; + case EofPacket.HEADER: + warnings = mysqlPacketPayload.readInt2(); + statusFlags = mysqlPacketPayload.readInt2(); + log.debug("EofPacket[warnings={},statusFlags={}]", warnings, statusFlags); + break; + } } //TODO @Override - protected void executeCommandResponsePackets(ChannelHandlerContext context, ByteBuf message) { + protected void executeCommandResponsePackets(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload) { int connectionId = MySQLResultCache.getInstance().getonnectionMap(context.channel().id().asShortText()); MySQLResultCache.getInstance().get(connectionId).setResponse(null); } + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + //TODO delete connection map. + super.channelInactive(ctx); + } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/mysql/MySQLFrontendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/mysql/MySQLFrontendHandler.java index fafc67913291b..5acab6dc68696 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/mysql/MySQLFrontendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/mysql/MySQLFrontendHandler.java @@ -38,6 +38,7 @@ * MySQL frontend handler. * * @author zhangliang + * @author wangkai */ @RequiredArgsConstructor public final class MySQLFrontendHandler extends FrontendHandler { diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/generic/EofPacket.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/generic/EofPacket.java index 5eef3e457ac24..4c94962f0648f 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/generic/EofPacket.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/generic/EofPacket.java @@ -26,12 +26,13 @@ * EOF packet protocol. * @see EOF Packet * - * @author zhangliang + * @author zhangliang + * @author wangkai */ @Getter public class EofPacket extends MySQLPacket { - private static final int HEADER = 0xfe; + public static final int HEADER = 0xfe; private final int warnings; diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/generic/ErrPacket.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/generic/ErrPacket.java index 4ba9dcbc5f728..b21a9a8f458c1 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/generic/ErrPacket.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/generic/ErrPacket.java @@ -26,12 +26,13 @@ * ERR packet protocol. * @see ERR Packet * - * @author zhangliang + * @author zhangliang + * @author wangkai */ @Getter public class ErrPacket extends MySQLPacket { - private static final int HEADER = 0xff; + public static final int HEADER = 0xff; private final int errorCode; diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/generic/OKPacket.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/generic/OKPacket.java index e6527efdab974..005241046e61d 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/generic/OKPacket.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/generic/OKPacket.java @@ -26,12 +26,13 @@ * OK packet protocol. * @see OK Packet * - * @author zhangliang + * @author zhangliang + * @author wangkai */ @Getter public class OKPacket extends MySQLPacket { - private static final int HEADER = 0x00; + public static final int HEADER = 0x00; private final long affectedRows; From b6227ba2791f4bd16951af3a920dfc39bbc951df Mon Sep 17 00:00:00 2001 From: WangKai Date: Mon, 7 May 2018 16:29:09 +0800 Subject: [PATCH 077/351] handshake finish --- .../proxy/backend/ShardingProxyClient.java | 45 ++++++++++-------- .../backend/common/BackendHandlerFactory.java | 4 +- .../common/CommandResponsePacketsHandler.java | 2 +- .../backend/mysql/MySQLBackendHandler.java | 46 +++++++++++++++++-- .../netty/ClientHandlerInitializer.java | 12 +++-- .../handshake/HandshakeResponse41Packet.java | 17 +++++-- 6 files changed, 93 insertions(+), 33 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java index 3316880c95f1e..ea9abf508b96b 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java @@ -39,7 +39,6 @@ import javax.sql.DataSource; import java.net.MalformedURLException; import java.net.URL; -import java.util.HashMap; import java.util.Map; /** @@ -55,36 +54,44 @@ public final class ShardingProxyClient { private EventLoopGroup workerGroup; @Getter - private Map channelMap = Maps.newHashMap(); + private Map channelMap = Maps.newHashMap(); + private Map bootstrapMap = Maps.newHashMap(); /** * Start Sharding-Proxy. * - * @throws InterruptedException interrupted exception + * @throws InterruptedException interrupted exception * @throws MalformedURLException url is illegal. */ public void start() throws InterruptedException, MalformedURLException { - Bootstrap bootstrap = new Bootstrap(); - if (workerGroup instanceof EpollEventLoopGroup) { - groupsEpoll(bootstrap); - } else { - groupsNio(bootstrap); - } Map dataSourceMap = ShardingRuleRegistry.getInstance().getDataSourceMap(); - for (Map.Entry each : dataSourceMap.entrySet()) { - URL url = new URL(((BasicDataSource)each.getValue()).getUrl().replaceAll("jdbc:mysql://","http://")); - ChannelFuture future = bootstrap.connect(url.getHost(),url.getPort()).sync(); - channelMap.put(each.getKey(),future.channel()); + for (Map.Entry each : dataSourceMap.entrySet()) { + URL url = new URL(((BasicDataSource) each.getValue()).getUrl().replaceAll("jdbc:mysql://", "http://")); + String ip = url.getHost(); + int port = url.getPort(); + String database = url.getPath().substring(1); + String username = ((BasicDataSource) each.getValue()).getUsername(); + String password = ((BasicDataSource) each.getValue()).getPassword(); + Bootstrap bootstrap = new Bootstrap(); + if (workerGroup instanceof EpollEventLoopGroup) { + groupsEpoll(bootstrap, ip, port, database, username, password); + } else { + groupsNio(bootstrap, ip, port, database, username, password); + } + //TODO use connection pool. + bootstrapMap.put(each.getKey(), bootstrap); + ChannelFuture future = bootstrap.connect(ip, port).sync(); + channelMap.put(each.getKey(), future.channel()); } } - public void stop(){ - if(workerGroup != null){ + public void stop() { + if (workerGroup != null) { workerGroup.shutdownGracefully(); } } - private void groupsEpoll(final Bootstrap bootstrap) { + private void groupsEpoll(final Bootstrap bootstrap, String ip, int port, String database, String username, String password) { workerGroup = new EpollEventLoopGroup(WORKER_MAX_THREADS); bootstrap.group(workerGroup) .channel(EpollServerSocketChannel.class) @@ -93,10 +100,10 @@ private void groupsEpoll(final Bootstrap bootstrap) { .option(EpollChannelOption.SO_BACKLOG, 128) .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)) - .handler(new ClientHandlerInitializer()); + .handler(new ClientHandlerInitializer(ip, port, database, username, password)); } - private void groupsNio(final Bootstrap bootstrap) { + private void groupsNio(final Bootstrap bootstrap, String ip, int port, String database, String username, String password) { workerGroup = new NioEventLoopGroup(WORKER_MAX_THREADS); bootstrap.group(workerGroup) .channel(NioServerSocketChannel.class) @@ -106,7 +113,7 @@ private void groupsNio(final Bootstrap bootstrap) { .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)) - .handler(new ClientHandlerInitializer()); + .handler(new ClientHandlerInitializer(ip, port, database, username, password)); } /** diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java index a347249ff7da8..b3947b9272178 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java @@ -33,10 +33,10 @@ public final class BackendHandlerFactory { * @param databaseType database type * @return backend handler instance */ - public static CommandResponsePacketsHandler createBackendHandlerInstance(final DatabaseType databaseType) { + public static CommandResponsePacketsHandler createBackendHandlerInstance(final DatabaseType databaseType, String ip, int port, String database, String username, String password) { switch (databaseType) { case MySQL: - return new MySQLBackendHandler(); + return new MySQLBackendHandler(ip, port, database, username, password); default: throw new UnsupportedOperationException(String.format("Cannot support database type '%s'", databaseType)); } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java index eb6286e961cda..029cdcd706565 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java @@ -28,7 +28,7 @@ * @author wangkai */ public abstract class CommandResponsePacketsHandler extends ChannelInboundHandlerAdapter { - protected abstract void auth(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload); + protected abstract void auth(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload); protected abstract void genericResponsePacket(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload); protected abstract void executeCommandResponsePackets(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload); } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java index e0aa9167ebbdd..a3e3e8b221185 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java @@ -21,22 +21,35 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.shardingjdbc.proxy.backend.common.CommandResponsePacketsHandler; +import io.shardingjdbc.proxy.transport.mysql.constant.CapabilityFlag; +import io.shardingjdbc.proxy.transport.mysql.constant.ServerInfo; import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload; import io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket; import io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket; import io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket; +import io.shardingjdbc.proxy.transport.mysql.packet.handshake.HandshakeResponse41Packet; import io.shardingjdbc.proxy.util.MySQLResultCache; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + /** * Backend handler. * * @author wangkai */ @Slf4j +@RequiredArgsConstructor public class MySQLBackendHandler extends CommandResponsePacketsHandler { private boolean authorized; + private final String ip; + private final int port; + private final String database; + private final String username; + private final String password; @Override public void channelRead(final ChannelHandlerContext context, final Object message) { @@ -45,7 +58,7 @@ public void channelRead(final ChannelHandlerContext context, final Object messag int sequenceId = mysqlPacketPayload.readInt1(); int header = mysqlPacketPayload.readInt1(); if (!authorized) { - auth(context, header, mysqlPacketPayload); + auth(context, sequenceId, header, mysqlPacketPayload); } else if (OKPacket.HEADER == header || ErrPacket.HEADER == header || EofPacket.HEADER == header) { genericResponsePacket(context, header, mysqlPacketPayload); } else { @@ -54,7 +67,7 @@ public void channelRead(final ChannelHandlerContext context, final Object messag } @Override - protected void auth(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload) { + protected void auth(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload) { int protocolVersion = header; String serverVersion = mysqlPacketPayload.readStringNul(); int connectionId = mysqlPacketPayload.readInt4(); @@ -68,8 +81,10 @@ protected void auth(ChannelHandlerContext context, int header, MySQLPacketPayloa mysqlPacketPayload.skipReserved(10); byte[] authPluginDataPart2 = mysqlPacketPayload.readStringNul().getBytes(); byte[] authPluginData = Bytes.concat(authPluginDataPart1, authPluginDataPart2); - //TODO get username, password(encrypt) and schema to write message. - + byte[] authResponse = byteXOR(SHA1(password.getBytes()), SHA1(Bytes.concat(authPluginData, SHA1(SHA1(password.getBytes()))))); + //TODO maxSizePactet should be set. + HandshakeResponse41Packet handshakeResponse41Packet = new HandshakeResponse41Packet(sequenceId, CapabilityFlag.calculateHandshakeCapabilityFlagsLower(), 4194304, ServerInfo.CHARSET, username, authResponse, database); + context.writeAndFlush(handshakeResponse41Packet); } @Override @@ -113,4 +128,27 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { //TODO delete connection map. super.channelInactive(ctx); } + + private byte[] SHA1(byte[] decript) { + try { + MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1"); + digest.update(decript); + return digest.digest(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + return null; + } + + private byte[] byteXOR(byte[] arg1, byte[] arg2) { + byte[] byteXOR = new byte[arg1.length]; + byte temp3; + for (int i = 0; i < arg1.length; i++) { + byte temp1 = arg1[i]; + byte temp2 = arg2[i]; + temp3 = (byte) (temp1 ^ temp2); + byteXOR[i] = temp3; + } + return byteXOR; + } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java index 45b3a5598b7b2..07500c2f4f7dc 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java @@ -19,27 +19,31 @@ import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; -import io.netty.channel.EventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.shardingjdbc.core.constant.DatabaseType; import io.shardingjdbc.proxy.backend.common.BackendHandlerFactory; -import io.shardingjdbc.proxy.frontend.common.FrontendHandlerFactory; import io.shardingjdbc.proxy.transport.common.codec.PacketCodecFactory; import lombok.RequiredArgsConstructor; /** * Channel initializer. - * + * * @author wangkai */ @RequiredArgsConstructor public final class ClientHandlerInitializer extends ChannelInitializer { + private final String ip; + private final int port; + private final String database; + private final String username; + private final String password; + @Override protected void initChannel(final SocketChannel socketChannel) { ChannelPipeline pipeline = socketChannel.pipeline(); // TODO load database type from yaml or startup arguments pipeline.addLast(PacketCodecFactory.createPacketCodecInstance(DatabaseType.MySQL)); - pipeline.addLast(BackendHandlerFactory.createBackendHandlerInstance(DatabaseType.MySQL)); + pipeline.addLast(BackendHandlerFactory.createBackendHandlerInstance(DatabaseType.MySQL, ip, port, database, username, password)); } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java index f30763c749344..1fe2432e038dc 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java @@ -24,9 +24,10 @@ /** * Handshake response above MySQL 4.1 packet protocol. - * @see HandshakeResponse41 - * + * * @author zhangliang + * @author wangkai + * @see HandshakeResponse41 */ @Getter public final class HandshakeResponse41Packet extends MySQLPacket { @@ -54,6 +55,16 @@ public HandshakeResponse41Packet(final MySQLPacketPayload mysqlPacketPayload) { readDatabase(mysqlPacketPayload); } + public HandshakeResponse41Packet(final int sequenceId, final int capabilityFlags, final int maxPacketSize, final int characterSet, final String username, final byte[] authResponse, final String database) { + super(sequenceId); + this.capabilityFlags = capabilityFlags; + this.maxPacketSize = maxPacketSize; + this.characterSet = characterSet; + this.username = username; + this.authResponse = authResponse; + this.database = database; + } + private void readAuthResponse(final MySQLPacketPayload mysqlPacketPayload) { if (0 != (capabilityFlags & CapabilityFlag.CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA.getValue())) { authResponse = mysqlPacketPayload.readStringLenenc().getBytes(); @@ -94,7 +105,7 @@ private void writeAuthResponse(final MySQLPacketPayload mysqlPacketPayload) { } private void writeDatabase(final MySQLPacketPayload mysqlPacketPayload) { - if (0 != (capabilityFlags & CapabilityFlag.CLIENT_CONNECT_WITH_DB.getValue())) { + if (0 != (capabilityFlags & CapabilityFlag.CLIENT_CONNECT_WITH_DB.getValue())) { mysqlPacketPayload.writeStringNul(database); } } From 392697f3b2ebeeccd19c1093fc81a677df05ab3a Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 7 May 2018 17:17:16 +0800 Subject: [PATCH 078/351] cache test --- .../saaavsaaa/client/action/IProvider.java | 2 + .../saaavsaaa/client/cache/CacheStrategy.java | 10 + .../com/saaavsaaa/client/cache/PathNode.java | 3 +- .../com/saaavsaaa/client/cache/PathTree.java | 89 ++++-- .../saaavsaaa/client/utility/PathUtil.java | 7 +- .../client/zookeeper/CacheAllClient.java | 267 ------------------ .../client/zookeeper/CacheClient.java | 104 ++++--- .../client/zookeeper/ClientFactory.java | 10 + .../saaavsaaa/client/zookeeper/Provider.java | 6 + .../client/zookeeper/TimelyCacheClient.java | 10 - .../client/zookeeper/CacheClientTest.java | 2 +- .../client/zookeeper/CacheWathClientTest.java | 2 +- 12 files changed, 168 insertions(+), 344 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/cache/CacheStrategy.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java diff --git a/src/main/java/com/saaavsaaa/client/action/IProvider.java b/src/main/java/com/saaavsaaa/client/action/IProvider.java index 681a6b525a91d..9849c67e157a0 100644 --- a/src/main/java/com/saaavsaaa/client/action/IProvider.java +++ b/src/main/java/com/saaavsaaa/client/action/IProvider.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.action; import com.saaavsaaa.client.election.LeaderElection; +import com.saaavsaaa.client.utility.section.Listener; import org.apache.zookeeper.KeeperException; import java.util.List; @@ -14,4 +15,5 @@ public interface IProvider extends IAction { List getNecessaryPaths(final String key); Stack getDeletingPaths(final String key); void executeContention(final LeaderElection election) throws KeeperException, InterruptedException; + void watch(final String key, final Listener listener); } diff --git a/src/main/java/com/saaavsaaa/client/cache/CacheStrategy.java b/src/main/java/com/saaavsaaa/client/cache/CacheStrategy.java new file mode 100644 index 0000000000000..3a523e1b3e59c --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/cache/CacheStrategy.java @@ -0,0 +1,10 @@ +package com.saaavsaaa.client.cache; + +/** + * Created by aaa on 18-5-7. + */ +public enum CacheStrategy { + NONE, + WATCH, + ALL +} diff --git a/src/main/java/com/saaavsaaa/client/cache/PathNode.java b/src/main/java/com/saaavsaaa/client/cache/PathNode.java index b6eae76703f04..d44398c1960d1 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathNode.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathNode.java @@ -2,9 +2,7 @@ import com.saaavsaaa.client.utility.constant.Constants; -import java.util.HashMap; import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -69,6 +67,7 @@ PathNode get(final Iterator iterator){ public byte[] getValue() { return value; } + public void setValue(byte[] value) { this.value = value; } diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index d164bef01bdd4..ca9046eee57b2 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -2,6 +2,9 @@ import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.utility.section.ClientTask; +import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.utility.section.Properties; import com.saaavsaaa.client.zookeeper.Provider; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.common.PathUtils; @@ -9,29 +12,44 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; /** * Created by aaa */ public final class PathTree { + private ScheduledExecutorService cacheService; + private final Provider provider; private PathNode rootNode; private PathStatus Status; - public PathTree(final String root) { + public PathTree(final String root, final Provider provider) { this.rootNode = new PathNode(root); this.Status = PathStatus.RELEASE; + this.provider = provider; } - public void loading(final Provider provider) throws KeeperException, InterruptedException { - this.setStatus(PathStatus.CHANGING); - - PathNode newRoot = new PathNode(rootNode.getKey()); - List children = provider.getChildren(rootNode.getKey()); - children.remove(provider.getRealPath(Constants.CHANGING_KEY)); - this.attechIntoNode(children, newRoot, provider); - rootNode = newRoot; - - this.setStatus(PathStatus.RELEASE); + public synchronized void loading() throws KeeperException, InterruptedException { + if (Status == Status.RELEASE){ + this.setStatus(PathStatus.CHANGING); + + PathNode newRoot = new PathNode(rootNode.getKey()); + List children = provider.getChildren(rootNode.getKey()); + children.remove(provider.getRealPath(Constants.CHANGING_KEY)); + this.attechIntoNode(children, newRoot, provider); + rootNode = newRoot; + + this.setStatus(PathStatus.RELEASE); + } else { + try { + Thread.sleep(10L); + } catch (InterruptedException e) { + System.out.println("cache put status not release"); + } + loading(); + } } private void attechIntoNode(final List children, final PathNode pathNode, final Provider provider) throws KeeperException, InterruptedException { @@ -47,6 +65,26 @@ private void attechIntoNode(final List children, final PathNode pathNode } } + public void refreshPeriodic(final long period){ + long threadPeriod = period; + if (threadPeriod < 1){ + threadPeriod = Properties.INSTANCE.getThreadPeriod(); + } + cacheService = Executors.newSingleThreadScheduledExecutor(); + cacheService.scheduleAtFixedRate(new ClientTask(provider) { + @Override + public void run(Provider provider) throws KeeperException, InterruptedException { + if (PathStatus.RELEASE == getStatus()) { + loading(); + } + } + }, Properties.INSTANCE.getThreadInitialDelay(), threadPeriod, TimeUnit.MILLISECONDS); + } + + public void watch(final Listener listener){ + provider.watch(rootNode.getKey(), listener); + } + public PathStatus getStatus() { return Status; } @@ -64,11 +102,6 @@ public byte[] getValue(final String path){ return null == node ? null : node.getValue(); } - private PathNode get(final String path){ - PathUtils.validatePath(path); - return rootNode.get(keyIterator(path)); //rootNode.get(1, path); - } - private Iterator keyIterator(final String path){ List nodes = PathUtil.getShortPathNodes(path); Iterator iterator = nodes.iterator(); @@ -92,13 +125,31 @@ public List getChildren(String path) { return result; } - public void put(final String path, final String value) { + private PathNode get(final String path){ + PathUtils.validatePath(path); + if (path.equals(rootNode.getKey())){ + return rootNode; + } + Iterator iterator = keyIterator(path); + if (iterator.hasNext()) { + return rootNode.get(iterator); //rootNode.get(1, path); + } + return null; + } + + public synchronized void put(final String path, final String value) { PathUtils.validatePath(path); if (Status == Status.RELEASE){ + if (path.equals(rootNode.getKey())){ + rootNode.setValue(value.getBytes(Constants.UTF_8)); + return; + } + this.setStatus(PathStatus.CHANGING); rootNode.set(keyIterator(path), value); + this.setStatus(PathStatus.RELEASE); } else { try { - Thread.sleep(50L); + Thread.sleep(10L); } catch (InterruptedException e) { System.out.println("cache put status not release"); } @@ -106,7 +157,7 @@ public void put(final String path, final String value) { } } - public void delete(String path) { + public synchronized void delete(String path) { PathUtils.validatePath(path); String prxpath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); PathNode node = get(prxpath); diff --git a/src/main/java/com/saaavsaaa/client/utility/PathUtil.java b/src/main/java/com/saaavsaaa/client/utility/PathUtil.java index 8dfbde31e27b2..fb0a36f04f379 100644 --- a/src/main/java/com/saaavsaaa/client/utility/PathUtil.java +++ b/src/main/java/com/saaavsaaa/client/utility/PathUtil.java @@ -135,7 +135,8 @@ public static String checkPath(String path) throws IllegalArgumentException { char previous = 47; char[] chars = path.toCharArray(); StringBuilder builder = new StringBuilder(); - + builder.append(previous); + for(int i = 1; i < chars.length; ++i) { char c = chars[i]; if (c == 0 || (c == 47 && previous == 47)) { @@ -157,8 +158,8 @@ public static String checkPath(String path) throws IllegalArgumentException { continue; } - builder.append(previous); - previous = chars[i]; + builder.append(c); + previous = c; } return builder.toString(); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java deleted file mode 100644 index 6f0587f06ffe3..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheAllClient.java +++ /dev/null @@ -1,267 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -import com.saaavsaaa.client.cache.PathStatus; -import com.saaavsaaa.client.cache.PathTree; -import com.saaavsaaa.client.election.LeaderElection; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.utility.section.ClientTask; -import com.saaavsaaa.client.utility.section.Properties; -import org.apache.zookeeper.AsyncCallback; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; - -import java.io.IOException; -import java.util.List; -import java.util.Stack; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -/** - * Created by aaa - */ -@Deprecated -public final class CacheAllClient extends UsualClient { -// private final ScheduledExecutorService cacheService = Executors.newSingleThreadScheduledExecutor(); -// private Client usualClient; -// -// protected PathTree pathTree = null; -// - CacheAllClient(String servers, int sessionTimeoutMilliseconds) { - super(servers, sessionTimeoutMilliseconds); - } -// -// @Override -// public synchronized void start() throws IOException, InterruptedException { -// super.start(); -// pathTree = new PathTree(rootNode); -// usualClient = this.getClientFactory().newUsualClient().start(); -// cacheService.scheduleAtFixedRate(new ClientTask(usualClient) { -// @Override -// public void run(Client client) throws KeeperException, InterruptedException { -// if (PathStatus.RELEASE == pathTree.getStatus()) { -// loadCache(client); -// } -// } -// }, Properties.INSTANCE.getThreadInitialDelay(), Properties.INSTANCE.getThreadPeriod(), TimeUnit.MILLISECONDS); -// } -// -// //用替换整树的方式更新 -// private synchronized void loadCache(final Client client) throws KeeperException, InterruptedException { -// LeaderElection election = new LeaderElection() { -// /*@Override -// public void actionWhenUnreached() throws KeeperException, InterruptedException { -// loadCache(client); -// }*/ -// -// @Override -// public void action() throws KeeperException, InterruptedException { -// pathTree.loading(client); -// } -// }; -// election.executeContention(rootNode, client); -// } -// -// /* -// * closed beta -// */ -// @Override -// public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { -// LeaderElection election = new LeaderElection() { -// /*@Override -// public void actionWhenUnreached() throws KeeperException, InterruptedException { -// createAllNeedPath(key, value, createMode); -// }*/ -// -// @Override -// public void action() throws KeeperException, InterruptedException { -// createBegin(key, value, createMode); -// } -// }; -// election.executeContention(rootNode, this); -// } -// -// -// private void createBegin(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { -// if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ -// super.createCurrentOnly(key, value, createMode); -// return; -// } -// -// List nodes = PathUtil.getPathOrderNodes(rootNode, key); -// for (int i = 0; i < nodes.size(); i++) { -// if (super.checkExists(nodes.get(i))){ -// System.out.println("create :" + nodes.get(i)); -// continue; -// } -// System.out.println("create not exist:" + nodes.get(i)); -// if (i == nodes.size() - 1){ -// super.createCurrentOnly(nodes.get(i), value, createMode); -// } else { -// this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); -// } -// } -// } -// -// -// @Override -// public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { -// LeaderElection election = new LeaderElection() { -// /*@Override -// public void actionWhenUnreached() throws KeeperException, InterruptedException { -// deleteAllChildren(key); -// }*/ -// -// @Override -// public void action() throws KeeperException, InterruptedException { -// String path = PathUtil.getRealPath(rootNode, key); -// deleteChildren(path); -// pathTree.delete(path); -// } -// }; -// election.executeContention(rootNode, this); -// } -// -// private void deleteChildren(final String key) throws KeeperException, InterruptedException { -// List children = super.getChildren(key); -// if (children.isEmpty()){ -// if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ -// super.deleteOnlyCurrent(key); -// return; -// } -// return; -// } -// for (int i = 0; i < children.size(); i++) { -// if (!super.checkExists(children.get(i))){ -// System.out.println("delete not exist:" + children.get(i)); -// continue; -// } -// this.deleteChildren(key); -// } -// } -// -// @Override -// public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { -// LeaderElection election = new LeaderElection() { -// /*@Override -// public void actionWhenUnreached() throws KeeperException, InterruptedException { -// deleteCurrentBranch(key); -// }*/ -// -// @Override -// public void action() throws KeeperException, InterruptedException { -// String path = PathUtil.getRealPath(rootNode, key); -// try { -// deleteBranch(path); -// } catch (KeeperException.NotEmptyException ee){ -// System.out.println(path + " exist other children"); -// pathTree.delete(path); -// return; -// } -// } -// }; -// election.executeContention(rootNode, this); -// } -// -// private void deleteBranch(String key) throws KeeperException, InterruptedException { -// deleteChildren(PathUtil.getRealPath(rootNode, key)); -// Stack pathStack = PathUtil.getPathReverseNodes(rootNode, key); -// while (!pathStack.empty()){ -// String node = pathStack.pop(); -// // contrast cache -// if (checkExists(node)){ -// super.deleteOnlyCurrent(key); -// System.out.println("delete : " + node); -// } -// } -// } -// -// private boolean cacheReady(){ -// return PathStatus.RELEASE == pathTree.getStatus(); -// } -// -// @Override -// public byte[] getData(final String key) throws KeeperException, InterruptedException { -// String path = PathUtil.getRealPath(rootNode, key); -// if (cacheReady()){ -// return pathTree.getValue(path); -// } -// // without watcher ensure cache execute result consistency -// byte[] data = zooKeeper.getData(path, false, null); -// pathTree.put(path, new String(data)); -// return data; -// } -// -// @Override -// public boolean checkExists(final String key) throws KeeperException, InterruptedException { -// String path = PathUtil.getRealPath(rootNode, key); -// if (cacheReady()){ -// return null != pathTree.getValue(path); -// } -// return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), false); -// } -// -// @Override -// public List getChildren(final String key) throws KeeperException, InterruptedException { -// String path = PathUtil.getRealPath(rootNode, key); -// if (cacheReady()){ -// return pathTree.getChildren(path); -// } -// return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); -// } -// -// @Override -// public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { -//// super.createCurrentOnly(key, value, createMode); -// LeaderElection election = new LeaderElection() { -///* @Override -// public void actionWhenUnreached() throws KeeperException, InterruptedException { -// createCurrentOnly(key, value, createMode); -// } -// */ -// @Override -// public void action() throws KeeperException, InterruptedException { -// usualClient.createCurrentOnly(key, value, createMode); -// } -// }; -// election.executeContention(rootNode, this); -// pathTree.put(PathUtil.getRealPath(rootNode, key), value); -// } -// -// @Override -// public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { -//// super.deleteOnlyCurrent(key); -// LeaderElection election = new LeaderElection() { -// /*@Override -// public void actionWhenUnreached() throws KeeperException, InterruptedException { -// deleteOnlyCurrent(key); -// }*/ -// -// @Override -// public void action() throws KeeperException, InterruptedException { -// usualClient.deleteOnlyCurrent(key); -// } -// }; -// election.executeContention(rootNode, this); -// pathTree.delete(PathUtil.getRealPath(rootNode, key)); -// } -// -// @Override -// public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { -//// super.deleteOnlyCurrent(key, callback, ctx); -// LeaderElection election = new LeaderElection() { -// /*@Override -// public void actionWhenUnreached() throws KeeperException, InterruptedException { -// deleteOnlyCurrent(key, callback, ctx); -// }*/ -// -// @Override -// public void action() throws KeeperException, InterruptedException { -// usualClient.deleteOnlyCurrent(key, callback, ctx); -// } -// }; -// election.executeContention(rootNode, this); -// pathTree.delete(PathUtil.getRealPath(rootNode, key)); -// } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 2ce02fe519932..859beafb3d358 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -1,15 +1,18 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.cache.CacheStrategy; import com.saaavsaaa.client.cache.PathStatus; import com.saaavsaaa.client.cache.PathTree; import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.ClientTask; +import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.utility.section.Properties; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; import java.io.IOException; import java.util.List; @@ -22,7 +25,6 @@ * Created by aaa */ public final class CacheClient extends UsualClient { - private final ScheduledExecutorService cacheService = Executors.newSingleThreadScheduledExecutor(); protected PathTree pathTree = null; CacheClient(String servers, int sessionTimeoutMilliseconds) { @@ -32,26 +34,62 @@ public final class CacheClient extends UsualClient { @Override public synchronized void start() throws IOException, InterruptedException { super.start(); - pathTree = new PathTree(rootNode); - cacheService.scheduleAtFixedRate(new ClientTask(strategy.getProvider()) { - @Override - public void run(Provider provider) throws KeeperException, InterruptedException { - if (PathStatus.RELEASE == pathTree.getStatus()) { - loadCache(provider); - } - } - }, Properties.INSTANCE.getThreadInitialDelay(), Properties.INSTANCE.getThreadPeriod(), TimeUnit.MILLISECONDS); + try { + useCacheStrategy(CacheStrategy.WATCH); + } catch (KeeperException e) { + System.out.println("CacheClient useCacheStrategy : " + e.getMessage()); + } } - //用替换整树的方式更新 - private synchronized void loadCache(final Provider provider) throws KeeperException, InterruptedException { - LeaderElection election = new LeaderElection() { - @Override - public void action() throws KeeperException, InterruptedException { - pathTree.loading(provider); + void useCacheStrategy(CacheStrategy cacheStrategy) throws KeeperException, InterruptedException { + switch (cacheStrategy){ + case WATCH:{ + pathTree = new PathTree(rootNode, strategy.getProvider()); + pathTree.watch(new Listener() { + @Override + public void process(WatchedEvent event) { + String path = event.getPath(); + switch (event.getType()) { + case NodeCreated: + case NodeDataChanged: + case NodeChildrenChanged: { + try { + pathTree.put(path, strategy.getDataString(path)); + } catch (Exception e) { + System.out.println("path tree put error : " + e.getMessage()); + } + break; + } + case NodeDeleted: { + pathTree.delete(path); + break; + } + default: + break; + } + } + }); + return; + } + case ALL:{ + pathTree = getPathTree(rootNode); + return; } - }; - provider.executeContention(election); + case NONE: + default:{ + return; + } + } + } + + public PathTree getPathTree() throws KeeperException, InterruptedException { + return getPathTree(rootNode); + } + + public PathTree getPathTree(final String treeRoot) throws KeeperException, InterruptedException { + PathTree tree = new PathTree(treeRoot, strategy.getProvider()); + tree.loading(); + return tree; } @Override @@ -74,38 +112,22 @@ public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback pathTree.delete(PathUtil.getRealPath(rootNode, key)); } - //================================================================== - - private boolean cacheReady(){ - return PathStatus.RELEASE == pathTree.getStatus(); - } - @Override public byte[] getData(final String key) throws KeeperException, InterruptedException { String path = PathUtil.getRealPath(rootNode, key); - if (cacheReady()){ - return pathTree.getValue(path); - } - // without watcher ensure cache execute result consistency - byte[] data = zooKeeper.getData(path, false, null); - pathTree.put(path, new String(data)); - return data; - } - - @Override - public boolean checkExists(final String key) throws KeeperException, InterruptedException { - String path = PathUtil.getRealPath(rootNode, key); - if (cacheReady()){ - return null != pathTree.getValue(path); + byte[] data = pathTree.getValue(path); + if (data != null){ + return data; } - return null != zooKeeper.exists(PathUtil.getRealPath(rootNode, key), false); + return strategy.getData(key); } @Override public List getChildren(final String key) throws KeeperException, InterruptedException { String path = PathUtil.getRealPath(rootNode, key); - if (cacheReady()){ - return pathTree.getChildren(path); + List keys = pathTree.getChildren(path); + if (!keys.isEmpty()){ + return keys; } return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 1f7951a686db7..091aa6b1163a9 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -28,11 +28,21 @@ public ClientFactory newUsualClient(final String servers, final int sessionTimeo return this; } + /* + * used for create new clients through a existing client + */ ClientFactory newUsualClient() { client = new UsualClient(servers, sessionTimeoutMilliseconds); return this; } + public ClientFactory newCacheClient(final String servers, final int sessionTimeoutMilliseconds) { + this.servers = servers; + this.sessionTimeoutMilliseconds = sessionTimeoutMilliseconds; + client = new CacheClient(servers, sessionTimeoutMilliseconds); + return this; + } + public ClientFactory watch(final Listener listener){ globalListener = listener; return this; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java b/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java index fa324d896f40b..bc3ac07abe00c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java @@ -4,6 +4,7 @@ import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; @@ -115,6 +116,11 @@ public void executeContention(final LeaderElection election) throws KeeperExcept election.executeContention(this); } + @Override + public void watch(final String key, final Listener listener) { + client.registerWatch(getRealPath(key), listener); + } + @Override public ZKTransaction transaction() { return new ZKTransaction(rootNode, zooKeeper); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java deleted file mode 100644 index ee3696a3c4e2c..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/TimelyCacheClient.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -/** - * Created by aaa - * 及时缓存 watcher - */ -public class TimelyCacheClient { - //锁定缓存 - // 从低广度遍历 -} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java index 292ec9e8edd9a..ce8e5d30df352 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java @@ -12,6 +12,6 @@ public class CacheClientTest extends UsualClientTest { @Override protected Client createClient(ClientFactory creator) throws IOException, InterruptedException { - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java index c2e9427db95b1..aca09affd86d8 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java @@ -11,6 +11,6 @@ public class CacheWathClientTest extends UsualClientTest { @Override protected Client createClient(ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); } } From fe7f11c053fae938fef0e367cdb8828fb1357205 Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 7 May 2018 18:26:39 +0800 Subject: [PATCH 079/351] test --- .../saaavsaaa/client/zookeeper/Client.java | 1 + .../strategy/ContentionStrategy.java | 16 +++++----- .../zookeeper/transaction/ZKTransaction.java | 1 - .../client/zookeeper/BaseClientTest.java | 1 - .../client/zookeeper/CacheClientTest.java | 1 + .../zookeeper/ContentionStrategyTest.java | 30 +++++++++++++++++++ 6 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index d7bccdda2a06c..d77cd7fef0a31 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -141,6 +141,7 @@ public void process(WatchedEvent event) { void deleteNamespace() throws KeeperException, InterruptedException { zooKeeper.delete(rootNode, Constants.VERSION); + rootExist = false; } void setRootNode(final String rootNode) { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index dffc56691e0dc..01dfef4c495b6 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -86,20 +86,20 @@ public void action() throws KeeperException, InterruptedException { private void createBegin(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - this.createCurrentOnly(key, value, createMode); + provider.createCurrentOnly(key, value, createMode); return; } List nodes = provider.getNecessaryPaths(key); for (int i = 0; i < nodes.size(); i++) { - if (this.checkExists(nodes.get(i))){ + if (provider.checkExists(nodes.get(i))){ System.out.println("create exist:" + nodes.get(i)); continue; } System.out.println("create not exist:" + nodes.get(i)); if (i == nodes.size() - 1){ - this.createCurrentOnly(nodes.get(i), value, createMode); + provider.createCurrentOnly(nodes.get(i), value, createMode); } else { - this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); + provider.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); } } } @@ -118,14 +118,14 @@ private void deleteChildren(final String key, final boolean deleteCurrentNode) t List children = provider.getChildren(key); for (int i = 0; i < children.size(); i++) { String child = PathUtil.getRealPath(key, children.get(i)); - if (!this.checkExists(child)){ + if (!provider.checkExists(child)){ System.out.println("delete not exist:" + child); continue; } deleteChildren(child, true); } if (deleteCurrentNode){ - this.deleteOnlyCurrent(key); + provider.deleteOnlyCurrent(key); } } @@ -150,8 +150,8 @@ private void deleteBranch(String key) throws KeeperException, InterruptedExcepti while (!pathStack.empty()){ String node = pathStack.pop(); // contrast cache - if (checkExists(node)){ - this.deleteOnlyCurrent(node); + if (provider.checkExists(node)){ + provider.deleteOnlyCurrent(node); System.out.println("delete : " + node); } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java index 4b80ad42efe6e..c9fbae893de7a 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java @@ -8,7 +8,6 @@ /** * Created by aaa - * todo transaction api */ public class ZKTransaction { private final Transaction transaction; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java index 3fa4d50842205..3bc417c5a8d7e 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java @@ -87,7 +87,6 @@ protected void isExisted(Client client) throws KeeperException, InterruptedExcep protected void get(Client client) throws KeeperException, InterruptedException { client.createAllNeedPath("a/b", "bbb11", CreateMode.PERSISTENT); String key = "a"; - // TODO: cache assert getDirectly(key, client).equals(""); key = "a/b"; assert getDirectly(key, client).equals("bbb11"); diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java index ce8e5d30df352..59b0c02438628 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java @@ -8,6 +8,7 @@ /** * Created by aaa + * todo test check cache content */ public class CacheClientTest extends UsualClientTest { @Override diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java index 63f2ac0419b6a..90d72c1d509b5 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java @@ -1,9 +1,14 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.junit.Test; import java.io.IOException; +import java.util.List; /** * Created by aaa on 18-5-4. @@ -18,4 +23,29 @@ protected Client createClient(final ClientFactory creator) throws IOException, I } //todo test node contention case + + @Test + public void deleteBranch() throws KeeperException, InterruptedException { + String keyB = "a/b/bb"; + testClient.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); + assert testClient.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; + String keyC = "a/c/cc"; + testClient.createAllNeedPath(keyC, "ccc11", CreateMode.PERSISTENT); + assert testClient.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; + testClient.deleteCurrentBranch(keyC); + assert testClient.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) == null; + assert testClient.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), false) != null; + testClient.deleteCurrentBranch(keyB); // because Constants.CHANGING_KEY, root still exist + List children = testClient.getZooKeeper().getChildren(PathUtil.checkPath(TestSupport.ROOT), false); + assert children.size() == 0; + testClient.deleteNamespace(); + assert testClient.getZooKeeper().exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; + testClient.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); + assert testClient.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; + testClient.deleteCurrentBranch(keyB); + children = testClient.getZooKeeper().getChildren(PathUtil.checkPath(TestSupport.ROOT), false); + assert children.size() == 0; + testClient.deleteNamespace(); + assert testClient.getZooKeeper().exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; + } } From 11085a8294891e2cb6e61b5dcb9e0de2410140a4 Mon Sep 17 00:00:00 2001 From: WangKai Date: Mon, 7 May 2018 20:14:07 +0800 Subject: [PATCH 080/351] refactor the methond of datasource config --- .../proxy/backend/ShardingProxyClient.java | 18 ++++++++++-------- .../proxy/config/ShardingRuleRegistry.java | 18 +++++++++++++++--- .../main/resources/conf/sharding-config.yaml | 12 ++++++------ 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java index ea9abf508b96b..a3f53c040c4ab 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java @@ -18,6 +18,7 @@ package io.shardingjdbc.proxy.backend; import com.google.common.collect.Maps; +import com.zaxxer.hikari.HikariConfig; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.Channel; @@ -34,11 +35,10 @@ import io.shardingjdbc.proxy.backend.netty.ClientHandlerInitializer; import io.shardingjdbc.proxy.config.ShardingRuleRegistry; import lombok.Getter; -import org.apache.commons.dbcp2.BasicDataSource; -import javax.sql.DataSource; import java.net.MalformedURLException; import java.net.URL; +import java.util.HashMap; import java.util.Map; /** @@ -55,6 +55,7 @@ public final class ShardingProxyClient { @Getter private Map channelMap = Maps.newHashMap(); + @Getter private Map bootstrapMap = Maps.newHashMap(); /** @@ -63,15 +64,16 @@ public final class ShardingProxyClient { * @throws InterruptedException interrupted exception * @throws MalformedURLException url is illegal. */ - public void start() throws InterruptedException, MalformedURLException { - Map dataSourceMap = ShardingRuleRegistry.getInstance().getDataSourceMap(); - for (Map.Entry each : dataSourceMap.entrySet()) { - URL url = new URL(((BasicDataSource) each.getValue()).getUrl().replaceAll("jdbc:mysql://", "http://")); + public void start() throws MalformedURLException, InterruptedException { + Map dataSourceConfigurationMap = new HashMap<>(); + ShardingRuleRegistry.getInstance(); + for (Map.Entry each : dataSourceConfigurationMap.entrySet()) { + URL url = new URL(each.getValue().getJdbcUrl()); String ip = url.getHost(); int port = url.getPort(); String database = url.getPath().substring(1); - String username = ((BasicDataSource) each.getValue()).getUsername(); - String password = ((BasicDataSource) each.getValue()).getPassword(); + String username = (each.getValue()).getUsername(); + String password = (each.getValue()).getPassword(); Bootstrap bootstrap = new Bootstrap(); if (workerGroup instanceof EpollEventLoopGroup) { groupsEpoll(bootstrap, ip, port, database, username, password); diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java index 2992593bab139..014231b10bc8f 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java @@ -26,6 +26,7 @@ import io.shardingjdbc.core.yaml.sharding.YamlShardingConfigurationForProxy; import io.shardingjdbc.proxy.metadata.ProxyShardingMetaData; import lombok.Getter; + import javax.sql.DataSource; import java.io.File; import java.io.IOException; @@ -48,6 +49,8 @@ public final class ShardingRuleRegistry { private final Map dataSourceMap; + private final Map dataSourceConfigurationMap; + private final ShardingRule shardingRule; private final ShardingMetaData shardingMetaData; @@ -59,10 +62,15 @@ private ShardingRuleRegistry() { } catch (final IOException ex) { throw new ShardingJdbcException(ex); } + dataSourceConfigurationMap = new HashMap<>(128, 1); dataSourceMap = new HashMap<>(128, 1); Map dataSourceParameters = yamlShardingConfigurationForProxy.getDataSources(); for (String each : dataSourceParameters.keySet()) { - dataSourceMap.put(each, getDataSource(dataSourceParameters.get(each))); + if (WITHOUT_JDBC) { + dataSourceConfigurationMap.put(each, getDataSourceConfiguration(dataSourceParameters.get(each))); + } else { + dataSourceMap.put(each, getDataSource(dataSourceParameters.get(each))); + } } shardingRule = yamlShardingConfigurationForProxy.getShardingRule(Collections.emptyList()); try { @@ -73,7 +81,7 @@ private ShardingRuleRegistry() { } } - private DataSource getDataSource(final DataSourceParameter dataSourceParameter) { + private HikariConfig getDataSourceConfiguration(final DataSourceParameter dataSourceParameter) { HikariConfig config = new HikariConfig(); config.setDriverClassName("com.mysql.jdbc.Driver"); config.setJdbcUrl(dataSourceParameter.getUrl()); @@ -86,7 +94,11 @@ private DataSource getDataSource(final DataSourceParameter dataSourceParameter) config.setMaximumPoolSize(100); config.addDataSourceProperty("useServerPrepStmts", "true"); config.addDataSourceProperty("cachePrepStmts", "true"); - return new HikariDataSource(config); + return config; + } + + private DataSource getDataSource(final DataSourceParameter dataSourceParameter) { + return new HikariDataSource(getDataSourceConfiguration(dataSourceParameter)); } /** diff --git a/sharding-proxy/src/main/resources/conf/sharding-config.yaml b/sharding-proxy/src/main/resources/conf/sharding-config.yaml index 575a8e0627cdf..b33cead59f35c 100644 --- a/sharding-proxy/src/main/resources/conf/sharding-config.yaml +++ b/sharding-proxy/src/main/resources/conf/sharding-config.yaml @@ -1,12 +1,12 @@ dataSources: demo_ds_0: - url: jdbc:mysql://localhost:3306/demo_ds_0 - username: root - password: + url: jdbc:mysql://10.7.8.175:13306/demo_ds_0 + username: admin + password: 123456 demo_ds_1: - url: jdbc:mysql://localhost:3306/demo_ds_1 - username: root - password: + url: jdbc:mysql://10.7.8.175:13306/demo_ds_1 + username: admin + password: 123456 shardingRule: tables: From 27964b21ff217011c9a7f61e3d3c283102db0ce7 Mon Sep 17 00:00:00 2001 From: saaav Date: Tue, 8 May 2018 17:26:17 +0800 Subject: [PATCH 081/351] todo --- .../com/saaavsaaa/client/action/IClient.java | 6 +- .../{IStrategy.java => IExecStrategy.java} | 2 +- .../saaavsaaa/client/action/IProvider.java | 4 + .../client/election/LeaderElection.java | 11 +- .../client/utility/section/Callback.java | 8 + .../client/utility/section/ClientTask.java | 1 - .../client/zookeeper/ClientFactory.java | 4 +- .../saaavsaaa/client/zookeeper/Provider.java | 23 ++- .../client/zookeeper/UsualClient.java | 9 +- .../zookeeper/strategy/BaseStrategy.java | 4 +- .../strategy/ContentionStrategy.java | 157 ++++++++++++++++-- .../zookeeper/strategy/StrategyType.java | 2 +- .../zookeeper/strategy/UsualStrategy.java | 8 +- .../client/zookeeper/CacheClientTest.java | 4 - .../client/zookeeper/CacheWathClientTest.java | 2 +- .../client/zookeeper/ClientsTest.java | 4 +- .../zookeeper/ContentionStrategyTest.java | 2 +- .../client/zookeeper/UsualClientTest.java | 2 +- .../zookeeper/UsualWatchClientTest.java | 2 +- 19 files changed, 208 insertions(+), 47 deletions(-) rename src/main/java/com/saaavsaaa/client/action/{IStrategy.java => IExecStrategy.java} (75%) create mode 100644 src/main/java/com/saaavsaaa/client/utility/section/Callback.java diff --git a/src/main/java/com/saaavsaaa/client/action/IClient.java b/src/main/java/com/saaavsaaa/client/action/IClient.java index 655a56fc8c04b..9dea54710c2b8 100644 --- a/src/main/java/com/saaavsaaa/client/action/IClient.java +++ b/src/main/java/com/saaavsaaa/client/action/IClient.java @@ -1,9 +1,7 @@ package com.saaavsaaa.client.action; -import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; -import org.apache.zookeeper.*; - -import java.util.List; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; /** * Created by aaa diff --git a/src/main/java/com/saaavsaaa/client/action/IStrategy.java b/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java similarity index 75% rename from src/main/java/com/saaavsaaa/client/action/IStrategy.java rename to src/main/java/com/saaavsaaa/client/action/IExecStrategy.java index ad4f88560d704..e9c4edce5a933 100644 --- a/src/main/java/com/saaavsaaa/client/action/IStrategy.java +++ b/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java @@ -5,6 +5,6 @@ /** * Created by aaa on 18-5-2. */ -public interface IStrategy extends IClient { +public interface IExecStrategy extends IClient { Provider getProvider(); } diff --git a/src/main/java/com/saaavsaaa/client/action/IProvider.java b/src/main/java/com/saaavsaaa/client/action/IProvider.java index 9849c67e157a0..1ce6a9b4289be 100644 --- a/src/main/java/com/saaavsaaa/client/action/IProvider.java +++ b/src/main/java/com/saaavsaaa/client/action/IProvider.java @@ -2,6 +2,8 @@ import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; +import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import java.util.List; @@ -16,4 +18,6 @@ public interface IProvider extends IAction { Stack getDeletingPaths(final String key); void executeContention(final LeaderElection election) throws KeeperException, InterruptedException; void watch(final String key, final Listener listener); + + void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException; } diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index 64f8146182f79..d02d37546ce74 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.election; +import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.utility.section.Properties; @@ -30,14 +31,15 @@ private boolean contend(final String node, final Provider provider, final Listen /* * listener will be register when the contention of the path is unsuccessful */ - public void executeContention(final Provider provider) throws KeeperException, InterruptedException { + public void executeContention(final String nodeBeCompete, final Provider provider) throws KeeperException, InterruptedException { boolean canBegin; - String contendNode = provider.getRealPath(Constants.CHANGING_KEY); + String realNode = provider.getRealPath(nodeBeCompete); + String contendNode = PathUtil.getRealPath(realNode, Constants.CHANGING_KEY); canBegin = this.contend(contendNode, provider, new Listener() { @Override public void process(WatchedEvent event) { try { - executeContention(provider); + executeContention(realNode, provider); } catch (Exception ee){ System.out.println("Listener Exception executeContention"); ee.printStackTrace(); @@ -49,6 +51,7 @@ public void process(WatchedEvent event) { try { action(); done = true; + callback(); } catch (Exception ee){ System.out.println("action Exception executeContention"); ee.printStackTrace(); @@ -70,5 +73,5 @@ public void waitDone(){ // public abstract void actionWhenUnreached() throws KeeperException, InterruptedException; public abstract void action() throws KeeperException, InterruptedException; - public void callBack(){} + public void callback(){} } diff --git a/src/main/java/com/saaavsaaa/client/utility/section/Callback.java b/src/main/java/com/saaavsaaa/client/utility/section/Callback.java new file mode 100644 index 0000000000000..60cbba7a83000 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/utility/section/Callback.java @@ -0,0 +1,8 @@ +package com.saaavsaaa.client.utility.section; + +/** + * Created by aaa + */ +public interface Callback { + void processResult(); +} diff --git a/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java b/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java index 9946e703cc41c..6205fe12316c1 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java +++ b/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java @@ -1,6 +1,5 @@ package com.saaavsaaa.client.utility.section; -import com.saaavsaaa.client.zookeeper.Client; import com.saaavsaaa.client.zookeeper.Provider; import org.apache.zookeeper.KeeperException; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 091aa6b1163a9..3e9590829c07b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -21,7 +21,7 @@ public class ClientFactory { public ClientFactory(){} - public ClientFactory newUsualClient(final String servers, final int sessionTimeoutMilliseconds) { + public ClientFactory newClient(final String servers, final int sessionTimeoutMilliseconds) { this.servers = servers; this.sessionTimeoutMilliseconds = sessionTimeoutMilliseconds; client = new UsualClient(servers, sessionTimeoutMilliseconds); @@ -31,7 +31,7 @@ public ClientFactory newUsualClient(final String servers, final int sessionTimeo /* * used for create new clients through a existing client */ - ClientFactory newUsualClient() { + ClientFactory newClient() { client = new UsualClient(servers, sessionTimeoutMilliseconds); return this; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java b/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java index bc3ac07abe00c..d148f1d75a263 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java @@ -13,7 +13,7 @@ import java.util.Stack; /** - * Created by aaa on 18-5-3. + * Created by aaa */ public class Provider implements IProvider { protected final Client client; @@ -76,6 +76,15 @@ public void createCurrentOnly(final String key, final String value, final Create } } } + + @Override + public void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { + client.createNamespace(); + if (rootNode.equals(key)){ + return; + } + transaction.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); + } @Override public void update(final String key, final String value) throws KeeperException, InterruptedException { @@ -86,11 +95,17 @@ public void update(final String key, final String value) throws KeeperException, public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { zooKeeper.delete(key, Constants.VERSION); System.out.println("delete : " + key); + if (rootNode.equals(key)){ + client.rootExist = false; //protected + } } @Override public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { zooKeeper.delete(key, Constants.VERSION, callback, ctx); + if (rootNode.equals(key)){ + client.rootExist = false; + } } @@ -113,7 +128,11 @@ public Stack getDeletingPaths(String key) { @Override public void executeContention(final LeaderElection election) throws KeeperException, InterruptedException { - election.executeContention(this); + this.executeContention(rootNode, election); + } + + public void executeContention(final String nodeBeCompete, final LeaderElection election) throws KeeperException, InterruptedException { + election.executeContention(rootNode, this); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index ebb0e9498730a..e15637b65263d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -1,8 +1,8 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.action.IExecStrategy; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import com.saaavsaaa.client.zookeeper.strategy.ContentionStrategy; -import com.saaavsaaa.client.action.IStrategy; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.AsyncCallback; @@ -16,11 +16,12 @@ import java.util.concurrent.ConcurrentHashMap; /** - * Created by aaa on 18-5-2. + * Created by aaa + * todo log */ public class UsualClient extends Client { - protected final Map strategies = new ConcurrentHashMap<>(); - protected IStrategy strategy; + protected final Map strategies = new ConcurrentHashMap<>(); + protected IExecStrategy strategy; UsualClient(final String servers, final int sessionTimeoutMilliseconds) { super(servers, sessionTimeoutMilliseconds); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java index f81dbbcfbb0ee..8032061cf44c4 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java @@ -1,13 +1,13 @@ package com.saaavsaaa.client.zookeeper.strategy; -import com.saaavsaaa.client.action.IStrategy; +import com.saaavsaaa.client.action.IExecStrategy; import com.saaavsaaa.client.zookeeper.Provider; import org.apache.zookeeper.KeeperException; /** * Created by aaa on 18-5-4. */ -public abstract class BaseStrategy implements IStrategy { +public abstract class BaseStrategy implements IExecStrategy { protected final Provider provider; public BaseStrategy(final Provider provider){ this.provider = provider; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index 01dfef4c495b6..a522c5d7eb346 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -3,7 +3,9 @@ import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.utility.section.Callback; import com.saaavsaaa.client.zookeeper.Provider; +import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -13,7 +15,8 @@ /** * Created by aaa - * Don't use it if you don't have to use it + * Don't use it if you don't have to use it. + * The ContentionStrategy is effective only when all the clients of the node which be competitive are using ContentionStrategy. */ public class ContentionStrategy extends UsualStrategy { public ContentionStrategy(final Provider provider) { @@ -33,33 +36,68 @@ public void action() throws KeeperException, InterruptedException { @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - LeaderElection election = new LeaderElection() { + LeaderElection election = buildCreateElection(key, value, createMode, null); + provider.executeContention(election); + election.waitDone(); + } + + private LeaderElection buildCreateElection(final String key, final String value, final CreateMode createMode, final Callback callback){ + return new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { provider.createCurrentOnly(provider.getRealPath(key), value, createMode); } + @Override + public void callback(){ + if (callback != null) { + callback.processResult(); + } + } }; - provider.executeContention(election); } @Override public void update(final String key, final String value) throws KeeperException, InterruptedException { - provider.executeContention(new LeaderElection() { + LeaderElection election = buildUpdateElection(key, value, null); + provider.executeContention(election); + election.waitDone(); + } + + private LeaderElection buildUpdateElection(final String key, final String value, final Callback callback){ + return new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { provider.update(provider.getRealPath(key), value); } - }); + @Override + public void callback(){ + if (callback != null) { + callback.processResult(); + } + } + }; } @Override public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - provider.executeContention(new LeaderElection() { + LeaderElection election = buildDeleteElection(key, null); + provider.executeContention(election); + election.waitDone(); + } + + private LeaderElection buildDeleteElection(final String key, final Callback callback){ + return new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { provider.deleteOnlyCurrent(provider.getRealPath(key)); } - }); + @Override + public void callback(){ + if (callback != null) { + callback.processResult(); + } + } + }; } @Override @@ -74,16 +112,50 @@ public void action() throws KeeperException, InterruptedException { @Override public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - LeaderElection election = new LeaderElection() { + LeaderElection election = buildCreateAllNeedElection(key, value, createMode, null); + provider.executeContention(election); + election.waitDone(); + } + + private LeaderElection buildCreateAllNeedElection(final String key, final String value, final CreateMode createMode, final Callback callback){ + return new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - createBegin(key, value, createMode); + ZKTransaction transaction = provider.transaction(); + createBegin(key, value, createMode, transaction); + transaction.commit(); + } + @Override + public void callback(){ + if (callback != null) { + callback.processResult(); + } } }; - provider.executeContention(election); } + private void createBegin(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { + if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ + provider.createInTransaction(key, value, createMode, transaction); + return; + } + List nodes = provider.getNecessaryPaths(key); + for (int i = 0; i < nodes.size(); i++) { + if (provider.checkExists(nodes.get(i))){ + System.out.println("create exist:" + nodes.get(i)); + continue; + } + System.out.println("create not exist:" + nodes.get(i)); + if (i == nodes.size() - 1){ + provider.createInTransaction(nodes.get(i), value, createMode, transaction); + } else { + provider.createInTransaction(nodes.get(i), Constants.NOTHING_VALUE, createMode, transaction); + } + } + } + + @Deprecated private void createBegin(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ provider.createCurrentOnly(key, value, createMode); @@ -104,16 +176,35 @@ private void createBegin(final String key, final String value, final CreateMode } } + @Override public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { provider.executeContention(new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - deleteChildren(provider.getRealPath(key), true); + ZKTransaction transaction = provider.transaction(); + deleteChildren(provider.getRealPath(key), true, transaction); + transaction.commit(); } }); } + private void deleteChildren(final String key, final boolean deleteCurrentNode, final ZKTransaction transaction) throws KeeperException, InterruptedException { + List children = provider.getChildren(key); + for (int i = 0; i < children.size(); i++) { + String child = PathUtil.getRealPath(key, children.get(i)); + if (!provider.checkExists(child)){ + System.out.println("delete not exist:" + child); + continue; + } + deleteChildren(child, true, transaction); + } + if (deleteCurrentNode){ + transaction.delete(key, Constants.VERSION); + } + } + + @Deprecated private void deleteChildren(final String key, final boolean deleteCurrentNode) throws KeeperException, InterruptedException { List children = provider.getChildren(key); for (int i = 0; i < children.size(); i++) { @@ -134,8 +225,10 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr provider.executeContention(new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { + ZKTransaction transaction = provider.transaction(); try { - deleteBranch(provider.getRealPath(key)); + deleteBranch(provider.getRealPath(key), transaction); + transaction.commit(); } catch (KeeperException.NotEmptyException ee){ System.out.println(key + " exist other children"); return; @@ -144,6 +237,30 @@ public void action() throws KeeperException, InterruptedException { }); } + @Deprecated + private void deleteBranch(String key, final ZKTransaction transaction) throws KeeperException, InterruptedException { + deleteChildren(key, false, transaction); + Stack pathStack = provider.getDeletingPaths(key); + String prePath = key; + while (!pathStack.empty()){ + String node = pathStack.pop(); + // contrast cache + // Performance needs testing + List children = provider.getChildren(node); + boolean canDelete = children.size() == 0 || children.size() == 1; + if (children.size() == 1){ + if (!PathUtil.getRealPath(node, children.get(0)).equals(prePath)){ + canDelete = false; + } + } + if (provider.checkExists(node) && canDelete){ + transaction.delete(node, Constants.VERSION); + System.out.println("delete : " + node); + } + prePath = node; + } + } + private void deleteBranch(String key) throws KeeperException, InterruptedException { deleteChildren(key, false); Stack pathStack = provider.getDeletingPaths(key); @@ -156,4 +273,20 @@ private void deleteBranch(String key) throws KeeperException, InterruptedExcepti } } } + + + + //todo Use arbitrary competitive nodes + //The following is not recommended for the time being, IExecStrategy convert to ContentionStrategy====================================================================== + public void createCurrentOnly(final String key, final String value, final CreateMode createMode, final Callback callback) throws KeeperException, InterruptedException { + provider.executeContention(buildCreateElection(key, value, createMode, callback)); + } + + public void update(final String key, final String value, final Callback callback) throws KeeperException, InterruptedException { + provider.executeContention(buildUpdateElection(key, value, null)); + } + + public void deleteOnlyCurrent(final String key, final Callback callback) throws KeeperException, InterruptedException { + provider.executeContention(buildDeleteElection(key, null)); + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java index 0e3cc52241b5b..0dcacdd4e4712 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.zookeeper.strategy; /** - * Created by aaa on 18-5-2. + * Created by aaa */ public enum StrategyType { USUAL, diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java index 147c4e0000d6e..19a695497ff03 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java @@ -12,7 +12,7 @@ import java.util.List; /** - * Created by aaa on 18-5-2. + * Created by aaa */ public class UsualStrategy extends BaseStrategy { public UsualStrategy(final Provider provider){ @@ -96,7 +96,7 @@ private void deleteChildren(final String path, final boolean deleteCurrentNode) List children; try{ children = provider.getChildren(path); - } catch (KeeperException.NoNodeException e){ // someone else has deleted the node since we checked + } catch (KeeperException.NoNodeException e){ System.out.println(e.getMessage()); return; } @@ -107,9 +107,9 @@ private void deleteChildren(final String path, final boolean deleteCurrentNode) if (deleteCurrentNode){ try{ this.deleteOnlyCurrent(path); - } catch(KeeperException.NotEmptyException e){ //someone has created a new child since we checked ... delete again. + } catch(KeeperException.NotEmptyException e){ deleteChildren(path, true); - } catch(KeeperException.NoNodeException e){ // ignore... someone else has deleted the node since we checked + } catch(KeeperException.NoNodeException e){ System.out.println(e.getMessage()); } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java index 59b0c02438628..7f9500c20d49b 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java @@ -1,9 +1,5 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.utility.section.Listener; -import org.junit.After; -import org.junit.Before; - import java.io.IOException; /** diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java index aca09affd86d8..c54d1f62f0c53 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java @@ -5,7 +5,7 @@ import java.io.IOException; /** - * Created by aaa on 18-5-2. + * Created by aaa */ public class CacheWathClientTest extends UsualClientTest { @Override diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java index cdb8f402664ba..de7f0515d7fec 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java @@ -39,12 +39,12 @@ protected Client createClient(ClientFactory creator) throws IOException, Interru } private Client newClient(ClientFactory creator) throws IOException, InterruptedException { - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); } protected Client newWatchClient(ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); } @Override diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java index 90d72c1d509b5..c46982dd8fc03 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java @@ -17,7 +17,7 @@ public class ContentionStrategyTest extends UsualClientTest { @Override protected Client createClient(final ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); - Client client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + Client client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); client.useStrategy(StrategyType.CONTENTION); return client; } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index 76a2de458f781..3450b3bc0b017 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -12,7 +12,7 @@ public class UsualClientTest extends BaseClientTest { @Override protected Client createClient(final ClientFactory creator) throws IOException, InterruptedException { - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); } @Test diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java index ce31488d98742..024ef80af946a 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java @@ -12,6 +12,6 @@ public class UsualWatchClientTest extends UsualClientTest { @Override protected Client createClient(final ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newUsualClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); } } \ No newline at end of file From 25c3fbd2519c42f6988c7ad591f8fa04f28b62c0 Mon Sep 17 00:00:00 2001 From: saaav Date: Tue, 8 May 2018 19:02:54 +0800 Subject: [PATCH 082/351] log start --- .../com/saaavsaaa/client/zookeeper/Client.java | 18 +++++++++++++++--- .../saaavsaaa/client/zookeeper/Provider.java | 11 +++++++++-- .../client/zookeeper/UsualClient.java | 13 +++++++------ .../zookeeper/ContentionStrategyTest.java | 2 +- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index d77cd7fef0a31..2537c7c270285 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -10,6 +10,8 @@ import com.saaavsaaa.client.zookeeper.strategy.StrategyType; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.List; @@ -21,6 +23,7 @@ * Created by aaa */ public abstract class Client implements IClient { + private static final Logger logger = LoggerFactory.getLogger(Client.class); private static final CountDownLatch CONNECTED = new CountDownLatch(1); protected static final Map watchers = new ConcurrentHashMap<>(); @@ -43,14 +46,16 @@ protected Client(final String servers, final int sessionTimeoutMilliseconds) { } public synchronized void start() throws IOException, InterruptedException { + logger.debug("Client servers:{},sessionTimeOut:{}", servers, sessionTimeOut); zooKeeper = new ZooKeeper(servers, sessionTimeOut, startWatcher()); if (!StringUtil.isNullOrBlank(scheme)) { zooKeeper.addAuthInfo(scheme, auth); + logger.debug("Client scheme:{},auth:{}", scheme, auth); } CONNECTED.await(); } - public abstract void useStrategy(StrategyType strategyType); + public abstract void useExecStrategy(StrategyType strategyType); ZooKeeper getZooKeeper(){ return zooKeeper; @@ -59,13 +64,16 @@ ZooKeeper getZooKeeper(){ private Watcher startWatcher() { return new Watcher(){ public void process(WatchedEvent event) { + logger.debug("Client process event:{},type:{}", event.getPath(), event.getType()); if(Event.KeeperState.SyncConnected == event.getState()){ if(Event.EventType.None == event.getType()){ CONNECTED.countDown(); + logger.debug("Client startWatcher SyncConnected"); } } if (globalListenerRegistered){ watchers.get(Constants.GLOBAL_LISTENER_KEY).process(event); + logger.debug("Client " + Constants.GLOBAL_LISTENER_KEY + " process"); } if (Properties.INSTANCE.watchOn() && watchers.containsKey(event.getPath())){ watchers.get(event.getPath()).process(event); @@ -107,11 +115,13 @@ public void unregisterWatch(final String key){ String path = PathUtil.getRealPath(rootNode, key); if (watchers.containsKey(path)){ watchers.remove(path); + logger.debug("unregisterWatch:{}", path); } } public void close() throws InterruptedException { zooKeeper.close(); + logger.debug("zk closed"); } void createNamespace() throws KeeperException, InterruptedException { @@ -124,8 +134,9 @@ private void createNamespace(final byte[] date) throws KeeperException, Interrup } try { zooKeeper.create(rootNode, date, authorities, CreateMode.PERSISTENT); + logger.debug("create root:{}", rootNode); } catch (KeeperException.NodeExistsException ee){ - System.out.println("root create : " + ee.getMessage()); + logger.warn("root create:{}", ee.getMessage()); rootExist = true; return; } @@ -136,12 +147,13 @@ public void process(WatchedEvent event) { rootExist = false; } })); - System.out.println("----------------------------------------------create root"); + logger.debug("----------------------------------------------create root"); } void deleteNamespace() throws KeeperException, InterruptedException { zooKeeper.delete(rootNode, Constants.VERSION); rootExist = false; + logger.debug("create root:{},rootExist:{}", rootNode, rootExist); } void setRootNode(final String rootNode) { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java b/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java index d148f1d75a263..76e2b016f8be7 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java @@ -8,6 +8,8 @@ import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; import java.util.Stack; @@ -16,6 +18,7 @@ * Created by aaa */ public class Provider implements IProvider { + private static final Logger logger = LoggerFactory.getLogger(Provider.class); protected final Client client; private final ZooKeeper zooKeeper; protected final boolean watched; @@ -67,10 +70,12 @@ public void createCurrentOnly(final String key, final String value, final Create } try { zooKeeper.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); + logger.debug("Provider createCurrentOnly:{}", key); } catch (KeeperException.NoNodeException e) { + logger.error("Provider createCurrentOnly:{}", e.getMessage(), e); // I don't know whether it will happen or not, if root watcher don't update rootExist timely if (e.getMessage().contains(key)) { - System.out.println("rootExist : " + e.getMessage()); + logger.info("Provider createCurrentOnly rootExist:{}", client.rootExist); Thread.sleep(50); this.createCurrentOnly(key, value, createMode); } @@ -81,6 +86,7 @@ public void createCurrentOnly(final String key, final String value, final Create public void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { client.createNamespace(); if (rootNode.equals(key)){ + logger.info("Provider createInTransaction rootNode:{}", key); return; } transaction.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); @@ -94,7 +100,7 @@ public void update(final String key, final String value) throws KeeperException, @Override public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { zooKeeper.delete(key, Constants.VERSION); - System.out.println("delete : " + key); + logger.debug("Provider deleteOnlyCurrent:{}", key); if (rootNode.equals(key)){ client.rootExist = false; //protected } @@ -103,6 +109,7 @@ public void deleteOnlyCurrent(final String key) throws KeeperException, Interrup @Override public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { zooKeeper.delete(key, Constants.VERSION, callback, ctx); + logger.debug("Provider deleteOnlyCurrent:{},ctx:{}", key, ctx); if (rootNode.equals(key)){ client.rootExist = false; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index e15637b65263d..a386ad9a33978 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -5,10 +5,9 @@ import com.saaavsaaa.client.zookeeper.strategy.ContentionStrategy; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; -import org.apache.zookeeper.AsyncCallback; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.List; @@ -20,6 +19,7 @@ * todo log */ public class UsualClient extends Client { + private static final Logger logger = LoggerFactory.getLogger(UsualClient.class); protected final Map strategies = new ConcurrentHashMap<>(); protected IExecStrategy strategy; @@ -30,11 +30,12 @@ public class UsualClient extends Client { @Override public synchronized void start() throws IOException, InterruptedException { super.start(); - useStrategy(StrategyType.USUAL); + useExecStrategy(StrategyType.USUAL); } @Override - public synchronized void useStrategy(StrategyType strategyType) { + public synchronized void useExecStrategy(StrategyType strategyType) { + logger.debug("useExecStrategy:{}", strategyType); if (strategies.containsKey(strategyType)){ strategy = strategies.get(strategyType); return; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java index c46982dd8fc03..558d12b1aa811 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java @@ -18,7 +18,7 @@ public class ContentionStrategyTest extends UsualClientTest { protected Client createClient(final ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); Client client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - client.useStrategy(StrategyType.CONTENTION); + client.useExecStrategy(StrategyType.CONTENTION); return client; } From d520f9b9913c639ca85565e6fb8ef6b1f85bd776 Mon Sep 17 00:00:00 2001 From: WangKai Date: Wed, 9 May 2018 10:49:38 +0800 Subject: [PATCH 083/351] debug --- .../proxy/backend/ShardingProxyClient.java | 48 +++++++++--------- .../netty/ClientHandlerInitializer.java | 7 +-- .../proxy/config/ShardingRuleRegistry.java | 49 ++++++++++--------- 3 files changed, 55 insertions(+), 49 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java index a3f53c040c4ab..d9918ed5da6c5 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java @@ -28,8 +28,10 @@ import io.netty.channel.epoll.EpollChannelOption; import io.netty.channel.epoll.EpollEventLoopGroup; import io.netty.channel.epoll.EpollServerSocketChannel; +import io.netty.channel.epoll.EpollSocketChannel; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.shardingjdbc.proxy.backend.netty.ClientHandlerInitializer; @@ -65,25 +67,28 @@ public final class ShardingProxyClient { * @throws MalformedURLException url is illegal. */ public void start() throws MalformedURLException, InterruptedException { - Map dataSourceConfigurationMap = new HashMap<>(); - ShardingRuleRegistry.getInstance(); - for (Map.Entry each : dataSourceConfigurationMap.entrySet()) { - URL url = new URL(each.getValue().getJdbcUrl()); - String ip = url.getHost(); - int port = url.getPort(); - String database = url.getPath().substring(1); - String username = (each.getValue()).getUsername(); - String password = (each.getValue()).getPassword(); - Bootstrap bootstrap = new Bootstrap(); - if (workerGroup instanceof EpollEventLoopGroup) { - groupsEpoll(bootstrap, ip, port, database, username, password); - } else { - groupsNio(bootstrap, ip, port, database, username, password); + try { + Map dataSourceConfigurationMap = ShardingRuleRegistry.getInstance().getDataSourceConfigurationMap(); + for (Map.Entry each : dataSourceConfigurationMap.entrySet()) { + URL url = new URL(each.getValue().getJdbcUrl().replaceAll("jdbc:mysql:", "http:")); + String ip = url.getHost(); + int port = url.getPort(); + String database = url.getPath().substring(1); + String username = (each.getValue()).getUsername(); + String password = (each.getValue()).getPassword(); + Bootstrap bootstrap = new Bootstrap(); + if (workerGroup instanceof EpollEventLoopGroup) { + groupsEpoll(bootstrap, ip, port, database, username, password); + } else { + groupsNio(bootstrap, ip, port, database, username, password); + } + //TODO use connection pool. + bootstrapMap.put(each.getKey(), bootstrap); + ChannelFuture future = bootstrap.connect(ip, port).sync(); + channelMap.put(each.getKey(), future.channel()); } - //TODO use connection pool. - bootstrapMap.put(each.getKey(), bootstrap); - ChannelFuture future = bootstrap.connect(ip, port).sync(); - channelMap.put(each.getKey(), future.channel()); + }catch (Exception e){ + e.printStackTrace(); } } @@ -96,9 +101,8 @@ public void stop() { private void groupsEpoll(final Bootstrap bootstrap, String ip, int port, String database, String username, String password) { workerGroup = new EpollEventLoopGroup(WORKER_MAX_THREADS); bootstrap.group(workerGroup) - .channel(EpollServerSocketChannel.class) + .channel(EpollSocketChannel.class) .option(EpollChannelOption.TCP_CORK, true) - .option(EpollChannelOption.SO_KEEPALIVE, true) .option(EpollChannelOption.SO_BACKLOG, 128) .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)) @@ -108,9 +112,7 @@ private void groupsEpoll(final Bootstrap bootstrap, String ip, int port, String private void groupsNio(final Bootstrap bootstrap, String ip, int port, String database, String username, String password) { workerGroup = new NioEventLoopGroup(WORKER_MAX_THREADS); bootstrap.group(workerGroup) - .channel(NioServerSocketChannel.class) - .option(ChannelOption.SO_KEEPALIVE, true) - .option(ChannelOption.TCP_NODELAY, true) + .channel(NioSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java index 07500c2f4f7dc..97d419708d2b7 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java @@ -17,6 +17,7 @@ package io.shardingjdbc.proxy.backend.netty; +import io.netty.channel.Channel; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; @@ -31,7 +32,7 @@ * @author wangkai */ @RequiredArgsConstructor -public final class ClientHandlerInitializer extends ChannelInitializer { +public final class ClientHandlerInitializer extends ChannelInitializer { private final String ip; private final int port; @@ -40,8 +41,8 @@ public final class ClientHandlerInitializer extends ChannelInitializer dataSourceMap; + private Map dataSourceMap; - private final Map dataSourceConfigurationMap; + private Map dataSourceConfigurationMap; - private final ShardingRule shardingRule; + private ShardingRule shardingRule; - private final ShardingMetaData shardingMetaData; + private ShardingMetaData shardingMetaData; private ShardingRuleRegistry() { - YamlShardingConfigurationForProxy yamlShardingConfigurationForProxy; try { - yamlShardingConfigurationForProxy = YamlShardingConfigurationForProxy.unmarshal(new File(getClass().getResource("/conf/sharding-config.yaml").toURI().getPath())); - } catch (final IOException | URISyntaxException ex) { - throw new ShardingJdbcException(ex); - } - dataSourceConfigurationMap = new HashMap<>(128, 1); - dataSourceMap = new HashMap<>(128, 1); - Map dataSourceParameters = yamlShardingConfigurationForProxy.getDataSources(); - for (String each : dataSourceParameters.keySet()) { - if (WITHOUT_JDBC) { - dataSourceConfigurationMap.put(each, getDataSourceConfiguration(dataSourceParameters.get(each))); - } else { + YamlShardingConfigurationForProxy yamlShardingConfigurationForProxy; + try { + yamlShardingConfigurationForProxy = YamlShardingConfigurationForProxy.unmarshal(new File(getClass().getResource("/conf/sharding-config.yaml").toURI().getPath())); + } catch (final IOException | URISyntaxException ex) { + throw new ShardingJdbcException(ex); + } + dataSourceConfigurationMap = new HashMap<>(128, 1); + dataSourceMap = new HashMap<>(128, 1); + Map dataSourceParameters = yamlShardingConfigurationForProxy.getDataSources(); + for (String each : dataSourceParameters.keySet()) { + if (WITHOUT_JDBC) { + dataSourceConfigurationMap.put(each, getDataSourceConfiguration(dataSourceParameters.get(each))); + } dataSourceMap.put(each, getDataSource(dataSourceParameters.get(each))); } - } - shardingRule = yamlShardingConfigurationForProxy.getShardingRule(Collections.emptyList()); - try { - shardingMetaData = new ProxyShardingMetaData(dataSourceMap); - shardingMetaData.init(shardingRule); - } catch (final SQLException ex) { - throw new ShardingJdbcException(ex); + shardingRule = yamlShardingConfigurationForProxy.getShardingRule(Collections.emptyList()); + try { + shardingMetaData = new ProxyShardingMetaData(dataSourceMap); + shardingMetaData.init(shardingRule); + } catch (final SQLException ex) { + throw new ShardingJdbcException(ex); + } + }catch (Exception e){ + e.printStackTrace(); } } From de8e378f55617604049e8de9bd0a54785d0b7c7a Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 9 May 2018 12:32:40 +0800 Subject: [PATCH 084/351] adding log --- .../client/zookeeper/CacheClient.java | 18 ++++++--- .../saaavsaaa/client/zookeeper/Client.java | 11 ++++-- .../client/zookeeper/ClientFactory.java | 8 +++- .../client/zookeeper/UsualClient.java | 1 - src/main/resources/log4j.properties | 38 +++++++++++++++++++ 5 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 src/main/resources/log4j.properties diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 859beafb3d358..78205f64d2ebe 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -13,6 +13,8 @@ import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.List; @@ -25,6 +27,7 @@ * Created by aaa */ public final class CacheClient extends UsualClient { + private static final Logger logger = LoggerFactory.getLogger(CacheClient.class); protected PathTree pathTree = null; CacheClient(String servers, int sessionTimeoutMilliseconds) { @@ -37,11 +40,12 @@ public synchronized void start() throws IOException, InterruptedException { try { useCacheStrategy(CacheStrategy.WATCH); } catch (KeeperException e) { - System.out.println("CacheClient useCacheStrategy : " + e.getMessage()); + logger.error("CacheClient useCacheStrategy : " + e.getMessage()); } } void useCacheStrategy(CacheStrategy cacheStrategy) throws KeeperException, InterruptedException { + logger.debug("use cache strategy:{}", cacheStrategy); switch (cacheStrategy){ case WATCH:{ pathTree = new PathTree(rootNode, strategy.getProvider()); @@ -49,6 +53,7 @@ void useCacheStrategy(CacheStrategy cacheStrategy) throws KeeperException, Inter @Override public void process(WatchedEvent event) { String path = event.getPath(); + logger.debug("useCacheStrategy Watch event:{}", event.toString()); switch (event.getType()) { case NodeCreated: case NodeDataChanged: @@ -56,7 +61,7 @@ public void process(WatchedEvent event) { try { pathTree.put(path, strategy.getDataString(path)); } catch (Exception e) { - System.out.println("path tree put error : " + e.getMessage()); + logger.error("path tree put error : " + e.getMessage()); } break; } @@ -72,7 +77,7 @@ public void process(WatchedEvent event) { return; } case ALL:{ - pathTree = getPathTree(rootNode); + pathTree = loadPathTree(); return; } case NONE: @@ -82,12 +87,13 @@ public void process(WatchedEvent event) { } } - public PathTree getPathTree() throws KeeperException, InterruptedException { - return getPathTree(rootNode); + public PathTree loadPathTree() throws KeeperException, InterruptedException { + return loadPathTree(rootNode); } - public PathTree getPathTree(final String treeRoot) throws KeeperException, InterruptedException { + public PathTree loadPathTree(final String treeRoot) throws KeeperException, InterruptedException { PathTree tree = new PathTree(treeRoot, strategy.getProvider()); + logger.debug("load path tree:{}", treeRoot); tree.loading(); return tree; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index 2537c7c270285..44f61434c5ee0 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -64,11 +64,12 @@ ZooKeeper getZooKeeper(){ private Watcher startWatcher() { return new Watcher(){ public void process(WatchedEvent event) { - logger.debug("Client process event:{},type:{}", event.getPath(), event.getType()); + logger.debug("Client process event:{}", event.toString()); if(Event.KeeperState.SyncConnected == event.getState()){ if(Event.EventType.None == event.getType()){ CONNECTED.countDown(); logger.debug("Client startWatcher SyncConnected"); + return; } } if (globalListenerRegistered){ @@ -84,6 +85,7 @@ public void process(WatchedEvent event) { void registerWatch(final Listener globalListener){ if (globalListenerRegistered){ + logger.warn("global listener can only register one"); return; } watchers.put(Constants.GLOBAL_LISTENER_KEY, new Watcher() { @@ -93,6 +95,7 @@ public void process(WatchedEvent event) { } }); globalListenerRegistered = true; + logger.debug("globalListenerRegistered:{}", globalListenerRegistered); } public Watcher registerWatch(final String key, final Listener listener){ @@ -105,6 +108,7 @@ public void process(WatchedEvent event) { } }; watchers.put(path, watcher); + logger.debug("register watcher:{}", path); return watcher; } @@ -130,6 +134,7 @@ void createNamespace() throws KeeperException, InterruptedException { private void createNamespace(final byte[] date) throws KeeperException, InterruptedException { if (rootExist){ + logger.debug("root exist"); return; } try { @@ -147,7 +152,7 @@ public void process(WatchedEvent event) { rootExist = false; } })); - logger.debug("----------------------------------------------create root"); + logger.debug("create root:{}", rootNode); } void deleteNamespace() throws KeeperException, InterruptedException { @@ -169,7 +174,7 @@ void setAuthorities(final String scheme, final byte[] auth) { void setClientFactory(ClientFactory clientFactory) { this.clientFactory = clientFactory; } - public ClientFactory getClientFactory() { + ClientFactory getClientFactory() { return clientFactory; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 3e9590829c07b..12fe60d630fdf 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -2,6 +2,8 @@ import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.Listener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; @@ -9,7 +11,8 @@ * Created by aaa */ public class ClientFactory { -// private static final String CLIENT_EXCLUSIVE_NODE = "ZKC"; + // private static final String CLIENT_EXCLUSIVE_NODE = "ZKC"; + private static final Logger logger = LoggerFactory.getLogger(ClientFactory.class); private Client client; private Listener globalListener; @@ -25,6 +28,7 @@ public ClientFactory newClient(final String servers, final int sessionTimeoutMil this.servers = servers; this.sessionTimeoutMilliseconds = sessionTimeoutMilliseconds; client = new UsualClient(servers, sessionTimeoutMilliseconds); + logger.debug("new usual client"); return this; } @@ -33,6 +37,7 @@ public ClientFactory newClient(final String servers, final int sessionTimeoutMil */ ClientFactory newClient() { client = new UsualClient(servers, sessionTimeoutMilliseconds); + logger.debug("new usual client by a existing client"); return this; } @@ -40,6 +45,7 @@ public ClientFactory newCacheClient(final String servers, final int sessionTimeo this.servers = servers; this.sessionTimeoutMilliseconds = sessionTimeoutMilliseconds; client = new CacheClient(servers, sessionTimeoutMilliseconds); + logger.debug("new cache client"); return this; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index a386ad9a33978..2104362027b4a 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -16,7 +16,6 @@ /** * Created by aaa - * todo log */ public class UsualClient extends Client { private static final Logger logger = LoggerFactory.getLogger(UsualClient.class); diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties new file mode 100644 index 0000000000000..66b06f9ebd7ef --- /dev/null +++ b/src/main/resources/log4j.properties @@ -0,0 +1,38 @@ +log4j.rootLogger=stdout, C + +log4j.appender.C=org.apache.log4j.DailyRollingFileAppender +log4j.appender.C.DatePattern='.'yyyy-MM-dd +log4j.appender.C.File=logs/root.log +log4j.appender.C.layout=org.apache.log4j.PatternLayout +log4j.appender.C.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p][%c] [%t] - %m%n +log4j.appender.C.Append=true + +log4j.appender.INFO=org.apache.log4j.ConsoleAppender +log4j.appender.INFO.layout=org.apache.log4j.PatternLayout +log4j.appender.INFO.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %p [%c] %m%n + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %p [%c] %m%n + +log4j.logger.org.apache.zookeeper=DEBUG,INFO,zk +#don't append into root, only this +log4j.additivity.org.apache.zookeeper=false +log4j.appender.zk=org.apache.log4j.DailyRollingFileAppender +log4j.appender.zk.DatePattern='.'yyyy-MM-dd +log4j.appender.zk.File=logs/zk.log +log4j.appender.zk.layout=org.apache.log4j.PatternLayout +log4j.appender.zk.Append=true +log4j.appender.zk.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p][%c] [%t] - %m%n +log4j.appender.zk.encoding=UTF-8 + +log4j.logger.com.saaavsaaa.client=DEBUG,INFO,client +#don't append into root, only this +log4j.additivity.com.saaavsaaa.client=false +log4j.appender.client=org.apache.log4j.DailyRollingFileAppender +log4j.appender.client.DatePattern='.'yyyy-MM-dd +log4j.appender.client.File=logs/client.log +log4j.appender.client.layout=org.apache.log4j.PatternLayout +log4j.appender.client.Append=true +log4j.appender.client.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p][%c] [%t] - %m%n +log4j.appender.client.encoding=UTF-8 \ No newline at end of file From f592b5aa2affb58e25c3bcbd692bb55f0f086267 Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 9 May 2018 17:40:21 +0800 Subject: [PATCH 085/351] log --- .../client/action/IExecStrategy.java | 2 +- .../saaavsaaa/client/action/IProvider.java | 2 +- .../com/saaavsaaa/client/cache/PathNode.java | 9 +++ .../com/saaavsaaa/client/cache/PathTree.java | 67 +++++++++++++++++-- .../client/election/LeaderElection.java | 12 ++-- .../client/utility/section/ClientTask.java | 12 ++-- .../client/utility/section/Properties.java | 4 -- .../utility/section/WatcherCreator.java | 10 ++- .../client/zookeeper/CacheClient.java | 41 ++---------- .../saaavsaaa/client/zookeeper/Client.java | 6 +- .../strategy/ContentionStrategy.java | 49 ++++++++------ .../zookeeper/strategy/UsualStrategy.java | 22 ++++-- .../zookeeper/transaction/ZKTransaction.java | 10 +++ src/main/resources/log4j.properties | 11 +-- 14 files changed, 157 insertions(+), 100 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java b/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java index e9c4edce5a933..6df729a9acb2b 100644 --- a/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java +++ b/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java @@ -3,7 +3,7 @@ import com.saaavsaaa.client.zookeeper.Provider; /** - * Created by aaa on 18-5-2. + * Created by aaa */ public interface IExecStrategy extends IClient { Provider getProvider(); diff --git a/src/main/java/com/saaavsaaa/client/action/IProvider.java b/src/main/java/com/saaavsaaa/client/action/IProvider.java index 1ce6a9b4289be..076335913ed4e 100644 --- a/src/main/java/com/saaavsaaa/client/action/IProvider.java +++ b/src/main/java/com/saaavsaaa/client/action/IProvider.java @@ -10,7 +10,7 @@ import java.util.Stack; /** - * Created by aaa on 18-5-3. + * Created by aaa */ public interface IProvider extends IAction { String getRealPath(final String path); diff --git a/src/main/java/com/saaavsaaa/client/cache/PathNode.java b/src/main/java/com/saaavsaaa/client/cache/PathNode.java index d44398c1960d1..7b9f326a6531b 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathNode.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathNode.java @@ -1,6 +1,8 @@ package com.saaavsaaa.client.cache; import com.saaavsaaa.client.utility.constant.Constants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Iterator; import java.util.Map; @@ -10,6 +12,7 @@ * Created by aaa */ public class PathNode { + private static final Logger logger = LoggerFactory.getLogger(PathNode.class); private final String nodeKey; private byte[] value; @@ -39,8 +42,10 @@ public void attachChild(final PathNode node) { PathNode set(final Iterator iterator, final String value){ String key = iterator.next(); + logger.debug("PathNode set:{},value:{}", key, value); PathNode node = children.get(key); if (node == null){ + logger.debug("set children haven't:{}", key); node = new PathNode(key); children.put(key, node); } @@ -54,8 +59,10 @@ PathNode set(final Iterator iterator, final String value){ PathNode get(final Iterator iterator){ String key = iterator.next(); + logger.debug("get:{}", key); PathNode node = children.get(key); if (node == null){ + logger.debug("get children haven't:{}", key); return null; } if (iterator.hasNext()){ @@ -75,12 +82,14 @@ public void setValue(byte[] value) { @Deprecated PathNode get(final int index, final String path) { if (children.isEmpty()){ + logger.debug("get children haven't:{},index:{}", path, index); return null; } if (children.containsKey(path)){ return children.get(path); } int nextSeparate = path.indexOf(Constants.PATH_SEPARATOR, index); + logger.debug("get nextSeparate:{}", nextSeparate); if (nextSeparate == -1){ nextSeparate = path.length() - 1; } diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index ca9046eee57b2..e58e29a33e327 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -7,7 +7,10 @@ import com.saaavsaaa.client.utility.section.Properties; import com.saaavsaaa.client.zookeeper.Provider; import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.common.PathUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.Iterator; @@ -16,10 +19,12 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -/** +/* * Created by aaa */ public final class PathTree { + private static final Logger logger = LoggerFactory.getLogger(PathTree.class); + private boolean executorStart = false; private ScheduledExecutorService cacheService; private final Provider provider; private PathNode rootNode; @@ -33,6 +38,7 @@ public PathTree(final String root, final Provider provider) { public synchronized void loading() throws KeeperException, InterruptedException { if (Status == Status.RELEASE){ + logger.debug("loading Status:{}", Status); this.setStatus(PathStatus.CHANGING); PathNode newRoot = new PathNode(rootNode.getKey()); @@ -42,18 +48,22 @@ public synchronized void loading() throws KeeperException, InterruptedException rootNode = newRoot; this.setStatus(PathStatus.RELEASE); + logger.debug("loading release:{}", Status); } else { + logger.info("loading but cache status not release"); try { Thread.sleep(10L); } catch (InterruptedException e) { - System.out.println("cache put status not release"); + logger.error("loading sleep error:{}", e.getMessage(), e); } loading(); } } private void attechIntoNode(final List children, final PathNode pathNode, final Provider provider) throws KeeperException, InterruptedException { + logger.debug("attechIntoNode children:{}", children); if (children.isEmpty()){ + logger.info("attechIntoNode there are no children"); return; } for (String child : children) { @@ -66,22 +76,62 @@ private void attechIntoNode(final List children, final PathNode pathNode } public void refreshPeriodic(final long period){ + if (executorStart){ + throw new IllegalArgumentException("period already set"); + } long threadPeriod = period; if (threadPeriod < 1){ threadPeriod = Properties.INSTANCE.getThreadPeriod(); } + logger.debug("refreshPeriodic:{}", period); cacheService = Executors.newSingleThreadScheduledExecutor(); cacheService.scheduleAtFixedRate(new ClientTask(provider) { @Override public void run(Provider provider) throws KeeperException, InterruptedException { + logger.debug("cacheService run:{}", getStatus()); if (PathStatus.RELEASE == getStatus()) { loading(); } } }, Properties.INSTANCE.getThreadInitialDelay(), threadPeriod, TimeUnit.MILLISECONDS); + executorStart = true; + } + + public void stopRefresh(){ + cacheService.shutdown(); + executorStart = false; + logger.debug("stopRefresh"); } - public void watch(final Listener listener){ + public void watch(Listener listener){ + if (listener == null){ + listener = new Listener() { + @Override + public void process(WatchedEvent event) { + String path = event.getPath(); + logger.debug("PathTree Watch event:{}", event.toString()); + switch (event.getType()) { + case NodeCreated: + case NodeDataChanged: + case NodeChildrenChanged: { + try { + put(path, provider.getDataString(path)); + } catch (Exception e) { + logger.error("PathTree put error : " + e.getMessage()); + } + break; + } + case NodeDeleted: { + delete(path); + break; + } + default: + break; + } + } + }; + } + logger.debug("PathTree Watch:{}", rootNode.getKey()); provider.watch(rootNode.getKey(), listener); } @@ -104,6 +154,7 @@ public byte[] getValue(final String path){ private Iterator keyIterator(final String path){ List nodes = PathUtil.getShortPathNodes(path); + logger.debug("keyIterator path{},nodes:{}", path, nodes); Iterator iterator = nodes.iterator(); iterator.next(); // root return iterator; @@ -113,9 +164,11 @@ public List getChildren(String path) { PathNode node = get(path); List result = new ArrayList<>(); if (node == null){ + logger.info("getChildren null"); return result; } if (node.getChildren().isEmpty()) { + logger.info("getChildren no child"); return result; } Iterator children = node.getChildren().values().iterator(); @@ -126,6 +179,7 @@ public List getChildren(String path) { } private PathNode get(final String path){ + logger.debug("PathTree get:{}", path); PathUtils.validatePath(path); if (path.equals(rootNode.getKey())){ return rootNode; @@ -134,11 +188,14 @@ private PathNode get(final String path){ if (iterator.hasNext()) { return rootNode.get(iterator); //rootNode.get(1, path); } + logger.debug("{} not exist", path); return null; } public synchronized void put(final String path, final String value) { + logger.debug("cache put:{},value:{}", path, value); PathUtils.validatePath(path); + logger.debug("put Status:{}", Status); if (Status == Status.RELEASE){ if (path.equals(rootNode.getKey())){ rootNode.setValue(value.getBytes(Constants.UTF_8)); @@ -149,9 +206,10 @@ public synchronized void put(final String path, final String value) { this.setStatus(PathStatus.RELEASE); } else { try { + logger.debug("put but cache status not release"); Thread.sleep(10L); } catch (InterruptedException e) { - System.out.println("cache put status not release"); + logger.error("put sleep error:{}", e.getMessage(), e); } put(path, value); } @@ -162,5 +220,6 @@ public synchronized void delete(String path) { String prxpath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); PathNode node = get(prxpath); node.getChildren().remove(path); + logger.debug("PathTree delete:{}", path); } } diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index d02d37546ce74..2450ea51000cb 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -9,11 +9,14 @@ import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Created by aaa */ public abstract class LeaderElection { + private static final Logger logger = LoggerFactory.getLogger(LeaderElection.class); private boolean done = false; private boolean contend(final String node, final Provider provider, final Listener listener) throws KeeperException, InterruptedException { @@ -22,6 +25,7 @@ private boolean contend(final String node, final Provider provider, final Listen provider.createCurrentOnly(node, Properties.INSTANCE.getClientId(), CreateMode.EPHEMERAL); success = true; } catch (KeeperException.NodeExistsException e) { + logger.info("contend not success"); // TODO: or changing_key node value == current client id provider.checkExists(node, WatcherCreator.deleteWatcher(node, listener)); } @@ -41,8 +45,7 @@ public void process(WatchedEvent event) { try { executeContention(realNode, provider); } catch (Exception ee){ - System.out.println("Listener Exception executeContention"); - ee.printStackTrace(); + logger.error("Listener Exception executeContention:{}", ee.getMessage(), ee); } } }); @@ -53,8 +56,7 @@ public void process(WatchedEvent event) { done = true; callback(); } catch (Exception ee){ - System.out.println("action Exception executeContention"); - ee.printStackTrace(); + logger.error("action Exception executeContention:{}", ee.getMessage(), ee); } provider.deleteOnlyCurrent(contendNode); } @@ -65,7 +67,7 @@ public void waitDone(){ try { Thread.sleep(10L); } catch (InterruptedException e) { - System.out.println(e.getMessage()); + logger.error("waitDone:{}", e.getMessage(), e); } } } diff --git a/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java b/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java index 6205fe12316c1..3e392d32d601d 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java +++ b/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java @@ -2,12 +2,16 @@ import com.saaavsaaa.client.zookeeper.Provider; import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Created by aaa */ public abstract class ClientTask implements Runnable { + private static final Logger logger = LoggerFactory.getLogger(ClientTask.class); private final Provider provider; + public ClientTask(final Provider provider){ this.provider = provider; } @@ -18,12 +22,8 @@ public ClientTask(final Provider provider){ public void run() { try { run(provider); - } catch (KeeperException e) { - System.out.println("ClientTask"); - e.printStackTrace(); - } catch (InterruptedException e) { - System.out.println("ClientTask"); - e.printStackTrace(); + } catch (Exception e) { + logger.error(e.getMessage(), e); } } } diff --git a/src/main/java/com/saaavsaaa/client/utility/section/Properties.java b/src/main/java/com/saaavsaaa/client/utility/section/Properties.java index f9e9e43c9464e..725604d4051f1 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/Properties.java +++ b/src/main/java/com/saaavsaaa/client/utility/section/Properties.java @@ -20,7 +20,6 @@ public String getClientId(){ // ResourceBundle caches the value in Thread String clientId = bundle.getString("client.id"); if (StringUtil.isNullOrBlank(clientId)){ - System.out.println("client.id doesn't exist"); throw new IllegalArgumentException("client.id doesn't exist"); } return clientId; @@ -29,7 +28,6 @@ public String getClientId(){ public boolean watchOn(){ String result = bundle.getString("client.watch.on"); if (StringUtil.isNullOrBlank(result)){ - System.out.println("client.watch.on doesn't exist"); throw new IllegalArgumentException("client.watch.on doesn't exist"); } return "up".equals(result); @@ -38,7 +36,6 @@ public boolean watchOn(){ public long getThreadInitialDelay(){ String result = bundle.getString("client.thread.delay"); if (StringUtil.isNullOrBlank(result)){ - System.out.println("client.thread.delay doesn't exist"); throw new IllegalArgumentException("client.thread.delay doesn't exist"); } return Long.valueOf(result); @@ -47,7 +44,6 @@ public long getThreadInitialDelay(){ public long getThreadPeriod(){ String result = bundle.getString("client.thread.period"); if (StringUtil.isNullOrBlank(result)){ - System.out.println("client.thread.period doesn't exist"); throw new IllegalArgumentException("client.thread.period doesn't exist"); } return Long.valueOf(result); diff --git a/src/main/java/com/saaavsaaa/client/utility/section/WatcherCreator.java b/src/main/java/com/saaavsaaa/client/utility/section/WatcherCreator.java index f96b2157d4f53..7a38f20ace4a7 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/WatcherCreator.java +++ b/src/main/java/com/saaavsaaa/client/utility/section/WatcherCreator.java @@ -2,6 +2,8 @@ import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import static org.apache.zookeeper.Watcher.Event.EventType.NodeDeleted; @@ -9,18 +11,14 @@ * Created by aaa */ public class WatcherCreator { + private static final Logger logger = LoggerFactory.getLogger(WatcherCreator.class); public static Watcher deleteWatcher(final String path, Listener listener){ return new Watcher() { @Override public void process(WatchedEvent event) { if (path.equals(event.getPath()) && NodeDeleted.equals(event.getType())){ listener.process(event); - - System.out.println("----------------------------------------------delete root"); - System.out.println(event.getPath()); - System.out.println(event.getState()); - System.out.println(event.getType()); - System.out.println("----------------------------------------------delete root"); + logger.debug("delete node event:{}", event.toString()); } } }; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 78205f64d2ebe..a9ed31e8171f4 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -1,14 +1,9 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.cache.CacheStrategy; -import com.saaavsaaa.client.cache.PathStatus; import com.saaavsaaa.client.cache.PathTree; -import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.utility.section.ClientTask; import com.saaavsaaa.client.utility.section.Listener; -import com.saaavsaaa.client.utility.section.Properties; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -18,10 +13,6 @@ import java.io.IOException; import java.util.List; -import java.util.Stack; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; /** * Created by aaa @@ -44,36 +35,13 @@ public synchronized void start() throws IOException, InterruptedException { } } + //todo put it here? void useCacheStrategy(CacheStrategy cacheStrategy) throws KeeperException, InterruptedException { logger.debug("use cache strategy:{}", cacheStrategy); switch (cacheStrategy){ case WATCH:{ pathTree = new PathTree(rootNode, strategy.getProvider()); - pathTree.watch(new Listener() { - @Override - public void process(WatchedEvent event) { - String path = event.getPath(); - logger.debug("useCacheStrategy Watch event:{}", event.toString()); - switch (event.getType()) { - case NodeCreated: - case NodeDataChanged: - case NodeChildrenChanged: { - try { - pathTree.put(path, strategy.getDataString(path)); - } catch (Exception e) { - logger.error("path tree put error : " + e.getMessage()); - } - break; - } - case NodeDeleted: { - pathTree.delete(path); - break; - } - default: - break; - } - } - }); + pathTree.watch(null); return; } case ALL:{ @@ -102,7 +70,6 @@ public PathTree loadPathTree(final String treeRoot) throws KeeperException, Inte public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { strategy.createCurrentOnly(key, value, createMode); pathTree.put(PathUtil.getRealPath(rootNode, key), value); - System.out.println("cache put : " + key); } @@ -123,8 +90,10 @@ public byte[] getData(final String key) throws KeeperException, InterruptedExcep String path = PathUtil.getRealPath(rootNode, key); byte[] data = pathTree.getValue(path); if (data != null){ + logger.debug("getData cache hit:{}", data); return data; } + logger.debug("getData cache not hit:{}", data); return strategy.getData(key); } @@ -133,8 +102,10 @@ public List getChildren(final String key) throws KeeperException, Interr String path = PathUtil.getRealPath(rootNode, key); List keys = pathTree.getChildren(path); if (!keys.isEmpty()){ + logger.debug("getChildren cache hit:{}", keys); return keys; } + logger.debug("getChildren cache not hit:{}", keys); return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java index 44f61434c5ee0..61b37a4911167 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Client.java @@ -28,6 +28,9 @@ public abstract class Client implements IClient { protected static final Map watchers = new ConcurrentHashMap<>(); protected final boolean watched = true; //false + private boolean globalListenerRegistered = false; + protected String rootNode = "/InitValue"; + protected boolean rootExist = false; private final String servers; private final int sessionTimeOut; private String scheme; @@ -35,9 +38,6 @@ public abstract class Client implements IClient { protected ZooKeeper zooKeeper; protected List authorities; - private boolean globalListenerRegistered = false; - protected String rootNode = "/InitValue"; - protected boolean rootExist = false; private ClientFactory clientFactory; protected Client(final String servers, final int sessionTimeoutMilliseconds) { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index a522c5d7eb346..c471c4ef9a84d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -9,35 +9,43 @@ import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; import java.util.Stack; /** * Created by aaa - * Don't use it if you don't have to use it. * The ContentionStrategy is effective only when all the clients of the node which be competitive are using ContentionStrategy. */ public class ContentionStrategy extends UsualStrategy { + private static final Logger logger = LoggerFactory.getLogger(ContentionStrategy.class); public ContentionStrategy(final Provider provider) { super(provider); } @Override + /* + * Don't use it if you don't have to use it. + */ public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { LeaderElection election = new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { provider.getData(provider.getRealPath(key), callback, ctx); + logger.debug("ContentionStrategy getData action:{}", key); } }; provider.executeContention(election); + logger.debug("ContentionStrategy getData executeContention"); } @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { LeaderElection election = buildCreateElection(key, value, createMode, null); provider.executeContention(election); + logger.debug("ContentionStrategy createCurrentOnly executeContention"); election.waitDone(); } @@ -60,6 +68,7 @@ public void callback(){ public void update(final String key, final String value) throws KeeperException, InterruptedException { LeaderElection election = buildUpdateElection(key, value, null); provider.executeContention(election); + logger.debug("ContentionStrategy update executeContention"); election.waitDone(); } @@ -68,6 +77,7 @@ private LeaderElection buildUpdateElection(final String key, final String value, @Override public void action() throws KeeperException, InterruptedException { provider.update(provider.getRealPath(key), value); + logger.debug("ContentionStrategy update action:{},value:{}", key, value); } @Override public void callback(){ @@ -82,6 +92,7 @@ public void callback(){ public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { LeaderElection election = buildDeleteElection(key, null); provider.executeContention(election); + logger.debug("ContentionStrategy deleteOnlyCurrent executeContention"); election.waitDone(); } @@ -106,6 +117,7 @@ public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback @Override public void action() throws KeeperException, InterruptedException { provider.deleteOnlyCurrent(provider.getRealPath(key), callback, ctx); + logger.debug("ContentionStrategy deleteOnlyCurrent action:{},ctx:{}", key, ctx); } }); } @@ -114,6 +126,7 @@ public void action() throws KeeperException, InterruptedException { public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { LeaderElection election = buildCreateAllNeedElection(key, value, createMode, null); provider.executeContention(election); + logger.debug("ContentionStrategy createAllNeedPath executeContention"); election.waitDone(); } @@ -121,6 +134,7 @@ private LeaderElection buildCreateAllNeedElection(final String key, final String return new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { + logger.debug("ContentionStrategy createAllNeedPath action:{}", key); ZKTransaction transaction = provider.transaction(); createBegin(key, value, createMode, transaction); transaction.commit(); @@ -143,10 +157,10 @@ private void createBegin(final String key, final String value, final CreateMode List nodes = provider.getNecessaryPaths(key); for (int i = 0; i < nodes.size(); i++) { if (provider.checkExists(nodes.get(i))){ - System.out.println("create exist:" + nodes.get(i)); + logger.info("create node exist:{}", nodes.get(i)); continue; } - System.out.println("create not exist:" + nodes.get(i)); + logger.debug("node not exist and create:", nodes.get(i)); if (i == nodes.size() - 1){ provider.createInTransaction(nodes.get(i), value, createMode, transaction); } else { @@ -164,10 +178,10 @@ private void createBegin(final String key, final String value, final CreateMode List nodes = provider.getNecessaryPaths(key); for (int i = 0; i < nodes.size(); i++) { if (provider.checkExists(nodes.get(i))){ - System.out.println("create exist:" + nodes.get(i)); + logger.info("create node exist:{}", nodes.get(i)); continue; } - System.out.println("create not exist:" + nodes.get(i)); + logger.debug("create node not exist:", nodes.get(i)); if (i == nodes.size() - 1){ provider.createCurrentOnly(nodes.get(i), value, createMode); } else { @@ -187,6 +201,7 @@ public void action() throws KeeperException, InterruptedException { transaction.commit(); } }); + logger.debug("ContentionStrategy deleteAllChildren executeContention"); } private void deleteChildren(final String key, final boolean deleteCurrentNode, final ZKTransaction transaction) throws KeeperException, InterruptedException { @@ -194,9 +209,10 @@ private void deleteChildren(final String key, final boolean deleteCurrentNode, f for (int i = 0; i < children.size(); i++) { String child = PathUtil.getRealPath(key, children.get(i)); if (!provider.checkExists(child)){ - System.out.println("delete not exist:" + child); + logger.info("delete not exist:{}", child); continue; } + logger.debug("deleteChildren:{}", child); deleteChildren(child, true, transaction); } if (deleteCurrentNode){ @@ -204,15 +220,16 @@ private void deleteChildren(final String key, final boolean deleteCurrentNode, f } } - @Deprecated private void deleteChildren(final String key, final boolean deleteCurrentNode) throws KeeperException, InterruptedException { List children = provider.getChildren(key); + logger.debug("deleteChildren:{}", children); for (int i = 0; i < children.size(); i++) { String child = PathUtil.getRealPath(key, children.get(i)); if (!provider.checkExists(child)){ - System.out.println("delete not exist:" + child); + logger.info("delete not exist:{}", child); continue; } + logger.debug("deleteChildren:{}", child); deleteChildren(child, true); } if (deleteCurrentNode){ @@ -225,16 +242,10 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr provider.executeContention(new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - ZKTransaction transaction = provider.transaction(); - try { - deleteBranch(provider.getRealPath(key), transaction); - transaction.commit(); - } catch (KeeperException.NotEmptyException ee){ - System.out.println(key + " exist other children"); - return; - } + deleteBranch(provider.getRealPath(key)); } }); + logger.debug("ContentionStrategy deleteCurrentBranch executeContention"); } @Deprecated @@ -255,7 +266,6 @@ private void deleteBranch(String key, final ZKTransaction transaction) throws Ke } if (provider.checkExists(node) && canDelete){ transaction.delete(node, Constants.VERSION); - System.out.println("delete : " + node); } prePath = node; } @@ -269,15 +279,16 @@ private void deleteBranch(String key) throws KeeperException, InterruptedExcepti // contrast cache if (provider.checkExists(node)){ provider.deleteOnlyCurrent(node); - System.out.println("delete : " + node); + continue; } + logger.info("deleteBranch node not exist:{}", node); } } //todo Use arbitrary competitive nodes - //The following is not recommended for the time being, IExecStrategy convert to ContentionStrategy====================================================================== + //IExecStrategy convert to ContentionStrategy====================================================================== public void createCurrentOnly(final String key, final String value, final CreateMode createMode, final Callback callback) throws KeeperException, InterruptedException { provider.executeContention(buildCreateElection(key, value, createMode, callback)); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java index 19a695497ff03..f0c50e4306378 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java @@ -8,6 +8,8 @@ import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.Watcher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; @@ -15,6 +17,7 @@ * Created by aaa */ public class UsualStrategy extends BaseStrategy { + private static final Logger logger = LoggerFactory.getLogger(UsualStrategy.class); public UsualStrategy(final Provider provider){ super(provider); } @@ -79,9 +82,9 @@ public void createAllNeedPath(final String key, final String value, final Create } else { this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); } - System.out.println("not exist and create:" + nodes.get(i)); + logger.debug("node not exist and create:", nodes.get(i)); } catch (KeeperException.NodeExistsException ee){ - System.out.println("exist:" + nodes.get(i)); + logger.debug("create node exist:{}", nodes.get(i)); continue; } } @@ -89,15 +92,17 @@ public void createAllNeedPath(final String key, final String value, final Create @Override public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { + logger.debug("deleteAllChildren:{}", key); this.deleteChildren(provider.getRealPath(key), true); } private void deleteChildren(final String path, final boolean deleteCurrentNode) throws KeeperException, InterruptedException { + logger.debug("deleteChildren:{}", path); List children; try{ children = provider.getChildren(path); } catch (KeeperException.NoNodeException e){ - System.out.println(e.getMessage()); + logger.warn("deleteChildren node not exist:{},e:{}", path, e.getMessage()); return; } for (String child : children){ @@ -108,9 +113,10 @@ private void deleteChildren(final String path, final boolean deleteCurrentNode) try{ this.deleteOnlyCurrent(path); } catch(KeeperException.NotEmptyException e){ + logger.warn("deleteCurrentNode exist children:{},e:{}", path, e.getMessage()); deleteChildren(path, true); } catch(KeeperException.NoNodeException e){ - System.out.println(e.getMessage()); + logger.warn("deleteCurrentNode node not exist:{},e:{}", path, e.getMessage()); } } } @@ -120,6 +126,7 @@ private void deleteChildren(final String path, final boolean deleteCurrentNode) */ @Override public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + logger.debug("deleteCurrentBranch:{}", key); if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ this.deleteOnlyCurrent(key); return; @@ -130,12 +137,13 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr try { this.deleteRecursively(superPath); } catch (KeeperException.NotEmptyException ee){ - System.out.println(ee.getMessage()); + logger.warn("deleteCurrentBranch exist children:{},e:{}", path, ee.getMessage()); return; } } private void deleteRecursively(final String path) throws KeeperException, InterruptedException { + logger.debug("deleteRecursively:{}", path); int index = path.lastIndexOf(Constants.PATH_SEPARATOR); if (index == 0){ this.deleteOnlyCurrent(path); @@ -146,8 +154,8 @@ private void deleteRecursively(final String path) throws KeeperException, Interr this.deleteOnlyCurrent(path); this.deleteRecursively(superPath); } catch (KeeperException.NotEmptyException ee){ - List children = this.getChildren(path); - children.forEach((c) -> System.out.println(path + " exist other children " + c)); + logger.warn("deleteRecursively exist children:{},e:{}", path, ee.getMessage()); + logger.info("deleteRecursively {} exist other children:{}", path, this.getChildren(path)); return; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java index c9fbae893de7a..492ba847b7d58 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java @@ -3,6 +3,8 @@ import com.saaavsaaa.client.utility.PathUtil; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; @@ -10,39 +12,47 @@ * Created by aaa */ public class ZKTransaction { + private static final Logger logger = LoggerFactory.getLogger(ZKTransaction.class); private final Transaction transaction; private final String rootNode; public ZKTransaction(final String root, final ZooKeeper zk) { transaction = zk.transaction(); rootNode = root; + logger.debug("ZKTransaction root:{}", rootNode); } public ZKTransaction create(String path, byte[] data, List acl, CreateMode createMode) { this.transaction.create(PathUtil.getRealPath(rootNode, path), data, acl, createMode); + logger.debug("wait create:{},data:{},acl:{},createMode:{}", new Object[]{path, data, acl, createMode}); return this; } public ZKTransaction delete(String path, int version) { this.transaction.delete(PathUtil.getRealPath(rootNode, path), version); + logger.debug("wait delete:{}", path); return this; } public ZKTransaction check(String path, int version) { this.transaction.check(PathUtil.getRealPath(rootNode, path), version); + logger.debug("wait check:{}", path); return this; } public ZKTransaction setData(String path, byte[] data, int version) { this.transaction.setData(PathUtil.getRealPath(rootNode, path), data, version); + logger.debug("wait setData:{},data:{}", path, data); return this; } public List commit() throws InterruptedException, KeeperException { + logger.debug("ZKTransaction commit"); return this.transaction.commit(); } public void commit(AsyncCallback.MultiCallback cb, Object ctx) { this.transaction.commit(cb, ctx); + logger.debug("ZKTransaction commit ctx:{}", ctx); } } diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties index 66b06f9ebd7ef..88315b244bac9 100644 --- a/src/main/resources/log4j.properties +++ b/src/main/resources/log4j.properties @@ -1,11 +1,4 @@ -log4j.rootLogger=stdout, C - -log4j.appender.C=org.apache.log4j.DailyRollingFileAppender -log4j.appender.C.DatePattern='.'yyyy-MM-dd -log4j.appender.C.File=logs/root.log -log4j.appender.C.layout=org.apache.log4j.PatternLayout -log4j.appender.C.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p][%c] [%t] - %m%n -log4j.appender.C.Append=true +log4j.rootLogger=stdout log4j.appender.INFO=org.apache.log4j.ConsoleAppender log4j.appender.INFO.layout=org.apache.log4j.PatternLayout @@ -15,7 +8,7 @@ log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %p [%c] %m%n -log4j.logger.org.apache.zookeeper=DEBUG,INFO,zk +log4j.logger.org.apache.zookeeper=INFO,zk #don't append into root, only this log4j.additivity.org.apache.zookeeper=false log4j.appender.zk=org.apache.log4j.DailyRollingFileAppender From 668ff30fbd8634accb0b1d98d5fe5df277d5dd0b Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 9 May 2018 18:57:20 +0800 Subject: [PATCH 086/351] node --- .../com/saaavsaaa/client/cache/PathNode.java | 3 +- .../com/saaavsaaa/client/cache/PathTree.java | 29 ++++++++++--------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/cache/PathNode.java b/src/main/java/com/saaavsaaa/client/cache/PathNode.java index 7b9f326a6531b..89f55675f261c 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathNode.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathNode.java @@ -13,10 +13,9 @@ */ public class PathNode { private static final Logger logger = LoggerFactory.getLogger(PathNode.class); + private final Map children = new ConcurrentHashMap<>(); private final String nodeKey; - private byte[] value; - private Map children = new ConcurrentHashMap<>(); PathNode(final String key) { this(key, Constants.RELEASE_VALUE); diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index e58e29a33e327..647b596bd0303 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -18,20 +18,21 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; /* * Created by aaa */ public final class PathTree { private static final Logger logger = LoggerFactory.getLogger(PathTree.class); + private final AtomicReference rootNode = new AtomicReference<>(); private boolean executorStart = false; private ScheduledExecutorService cacheService; private final Provider provider; - private PathNode rootNode; private PathStatus Status; public PathTree(final String root, final Provider provider) { - this.rootNode = new PathNode(root); + this.rootNode.set(new PathNode(root)); this.Status = PathStatus.RELEASE; this.provider = provider; } @@ -41,11 +42,11 @@ public synchronized void loading() throws KeeperException, InterruptedException logger.debug("loading Status:{}", Status); this.setStatus(PathStatus.CHANGING); - PathNode newRoot = new PathNode(rootNode.getKey()); - List children = provider.getChildren(rootNode.getKey()); + PathNode newRoot = new PathNode(rootNode.get().getKey()); + List children = provider.getChildren(rootNode.get().getKey()); children.remove(provider.getRealPath(Constants.CHANGING_KEY)); this.attechIntoNode(children, newRoot, provider); - rootNode = newRoot; + rootNode.set(newRoot); this.setStatus(PathStatus.RELEASE); logger.debug("loading release:{}", Status); @@ -131,8 +132,8 @@ public void process(WatchedEvent event) { } }; } - logger.debug("PathTree Watch:{}", rootNode.getKey()); - provider.watch(rootNode.getKey(), listener); + logger.debug("PathTree Watch:{}", rootNode.get().getKey()); + provider.watch(rootNode.get().getKey(), listener); } public PathStatus getStatus() { @@ -144,7 +145,7 @@ public void setStatus(final PathStatus status) { } public PathNode getRootNode() { - return rootNode; + return rootNode.get(); } public byte[] getValue(final String path){ @@ -181,12 +182,12 @@ public List getChildren(String path) { private PathNode get(final String path){ logger.debug("PathTree get:{}", path); PathUtils.validatePath(path); - if (path.equals(rootNode.getKey())){ - return rootNode; + if (path.equals(rootNode.get().getKey())){ + return rootNode.get(); } Iterator iterator = keyIterator(path); if (iterator.hasNext()) { - return rootNode.get(iterator); //rootNode.get(1, path); + return rootNode.get().get(iterator); //rootNode.get(1, path); } logger.debug("{} not exist", path); return null; @@ -197,12 +198,12 @@ public synchronized void put(final String path, final String value) { PathUtils.validatePath(path); logger.debug("put Status:{}", Status); if (Status == Status.RELEASE){ - if (path.equals(rootNode.getKey())){ - rootNode.setValue(value.getBytes(Constants.UTF_8)); + if (path.equals(rootNode.get().getKey())){ + rootNode.set(new PathNode(rootNode.get().getKey(), value.getBytes(Constants.UTF_8))); return; } this.setStatus(PathStatus.CHANGING); - rootNode.set(keyIterator(path), value); + rootNode.get().set(keyIterator(path), value); this.setStatus(PathStatus.RELEASE); } else { try { From e5e7c5f0801858450c52ec08a4cf7e92d52baf28 Mon Sep 17 00:00:00 2001 From: WangKai Date: Wed, 9 May 2018 19:35:49 +0800 Subject: [PATCH 087/351] handshake test success. --- .../proxy/backend/ShardingProxyClient.java | 3 ++ .../backend/mysql/MySQLBackendHandler.java | 43 +++++++++---------- .../mysql/packet/MySQLPacketPayload.java | 11 +++++ .../handshake/HandshakeResponse41Packet.java | 3 +- sharding-proxy/src/main/resources/logback.xml | 2 +- 5 files changed, 37 insertions(+), 25 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java index d9918ed5da6c5..69a0dea6ebc2c 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java @@ -103,6 +103,7 @@ private void groupsEpoll(final Bootstrap bootstrap, String ip, int port, String bootstrap.group(workerGroup) .channel(EpollSocketChannel.class) .option(EpollChannelOption.TCP_CORK, true) + .option(EpollChannelOption.SO_KEEPALIVE, true) .option(EpollChannelOption.SO_BACKLOG, 128) .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)) @@ -113,6 +114,8 @@ private void groupsNio(final Bootstrap bootstrap, String ip, int port, String da workerGroup = new NioEventLoopGroup(WORKER_MAX_THREADS); bootstrap.group(workerGroup) .channel(NioSocketChannel.class) + .option(ChannelOption.SO_KEEPALIVE, true) + .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.SO_BACKLOG, 128) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java index a3e3e8b221185..465afd0160fe2 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java @@ -54,13 +54,13 @@ public class MySQLBackendHandler extends CommandResponsePacketsHandler { @Override public void channelRead(final ChannelHandlerContext context, final Object message) { MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload((ByteBuf) message); - int packetSize = mysqlPacketPayload.readInt3(); + //int packetSize = mysqlPacketPayload.readInt3(); int sequenceId = mysqlPacketPayload.readInt1(); - int header = mysqlPacketPayload.readInt1(); - if (!authorized) { - auth(context, sequenceId, header, mysqlPacketPayload); - } else if (OKPacket.HEADER == header || ErrPacket.HEADER == header || EofPacket.HEADER == header) { + int header = mysqlPacketPayload.readInt1() & 0xFF; + if (OKPacket.HEADER == header || ErrPacket.HEADER == header || EofPacket.HEADER == header) { genericResponsePacket(context, header, mysqlPacketPayload); + } else if (!authorized) { + auth(context, sequenceId, header, mysqlPacketPayload); } else { executeCommandResponsePackets(context, header, mysqlPacketPayload); } @@ -81,9 +81,10 @@ protected void auth(ChannelHandlerContext context, int sequenceId, int header, M mysqlPacketPayload.skipReserved(10); byte[] authPluginDataPart2 = mysqlPacketPayload.readStringNul().getBytes(); byte[] authPluginData = Bytes.concat(authPluginDataPart1, authPluginDataPart2); - byte[] authResponse = byteXOR(SHA1(password.getBytes()), SHA1(Bytes.concat(authPluginData, SHA1(SHA1(password.getBytes()))))); + //byte[] authResponse = byteXOR(SHA1(password.getBytes()), SHA1(Bytes.concat(authPluginData, SHA1(SHA1(password.getBytes()))))); + byte[] authResponse = securePasswordAuthentication(password.getBytes(),authPluginData); //TODO maxSizePactet should be set. - HandshakeResponse41Packet handshakeResponse41Packet = new HandshakeResponse41Packet(sequenceId, CapabilityFlag.calculateHandshakeCapabilityFlagsLower(), 4194304, ServerInfo.CHARSET, username, authResponse, database); + HandshakeResponse41Packet handshakeResponse41Packet = new HandshakeResponse41Packet(sequenceId + 1, CapabilityFlag.calculateHandshakeCapabilityFlagsLower(), 16777215, ServerInfo.CHARSET, username, authResponse, database); context.writeAndFlush(handshakeResponse41Packet); } @@ -129,26 +130,22 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); } - private byte[] SHA1(byte[] decript) { + private final byte[] securePasswordAuthentication(byte[] password, byte[] authPluginData){ try { - MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1"); - digest.update(decript); - return digest.digest(); + MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); + byte[] part1 = sha1.digest(password); + sha1.reset(); + byte[] part2 = sha1.digest(part1); + sha1.reset(); + sha1.update(authPluginData); + byte[] authResponse = sha1.digest(part2); + for (int i = 0; i < authResponse.length; i++) { + authResponse[i] = (byte) (authResponse[i] ^ part1[i]); + } + return authResponse; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; } - - private byte[] byteXOR(byte[] arg1, byte[] arg2) { - byte[] byteXOR = new byte[arg1.length]; - byte temp3; - for (int i = 0; i < arg1.length; i++) { - byte temp1 = arg1[i]; - byte temp2 = arg2[i]; - temp3 = (byte) (temp1 ^ temp2); - byteXOR[i] = temp3; - } - return byteXOR; - } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/MySQLPacketPayload.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/MySQLPacketPayload.java index 04e9cd69a9336..7e1413a981a9b 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/MySQLPacketPayload.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/MySQLPacketPayload.java @@ -34,6 +34,7 @@ * * @author zhangliang * @author zhangyonglun + * @author wangkai */ @RequiredArgsConstructor @Getter @@ -261,6 +262,16 @@ public void writeStringFix(final String value) { byteBuf.writeBytes(value.getBytes()); } + /** + * Write variable length bytes to byte buffers. + * @see Native41 + * + * @param value fixed length bytes + */ + public void writeBytes(final byte[] value) { + byteBuf.writeBytes(value); + } + /** * Read variable length string from byte buffers. * @see FixedLengthString diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java index 1fe2432e038dc..70d74f372a276 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java @@ -98,7 +98,8 @@ private void writeAuthResponse(final MySQLPacketPayload mysqlPacketPayload) { mysqlPacketPayload.writeStringLenenc(new String(authResponse)); } else if (0 != (capabilityFlags & CapabilityFlag.CLIENT_SECURE_CONNECTION.getValue())) { mysqlPacketPayload.writeInt1(authResponse.length); - mysqlPacketPayload.writeStringFix(new String(authResponse)); + mysqlPacketPayload.writeBytes(authResponse); + } else { mysqlPacketPayload.writeStringNul(new String(authResponse)); } diff --git a/sharding-proxy/src/main/resources/logback.xml b/sharding-proxy/src/main/resources/logback.xml index 4b2aaba7a38ba..f7ea96a7a3daf 100644 --- a/sharding-proxy/src/main/resources/logback.xml +++ b/sharding-proxy/src/main/resources/logback.xml @@ -5,7 +5,7 @@ [%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n - + From b808a943a0b97b5b8b6a760633302873e3719934 Mon Sep 17 00:00:00 2001 From: WangKai Date: Wed, 9 May 2018 20:09:32 +0800 Subject: [PATCH 088/351] some code style fix --- .../io/shardingjdbc/proxy/backend/ShardingProxyClient.java | 3 --- .../shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java index 69a0dea6ebc2c..856f34a9bf028 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java @@ -27,10 +27,8 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.epoll.EpollChannelOption; import io.netty.channel.epoll.EpollEventLoopGroup; -import io.netty.channel.epoll.EpollServerSocketChannel; import io.netty.channel.epoll.EpollSocketChannel; import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; @@ -40,7 +38,6 @@ import java.net.MalformedURLException; import java.net.URL; -import java.util.HashMap; import java.util.Map; /** diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java index 465afd0160fe2..7165d8d19fc8f 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java @@ -83,7 +83,7 @@ protected void auth(ChannelHandlerContext context, int sequenceId, int header, M byte[] authPluginData = Bytes.concat(authPluginDataPart1, authPluginDataPart2); //byte[] authResponse = byteXOR(SHA1(password.getBytes()), SHA1(Bytes.concat(authPluginData, SHA1(SHA1(password.getBytes()))))); byte[] authResponse = securePasswordAuthentication(password.getBytes(),authPluginData); - //TODO maxSizePactet should be set. + //TODO maxSizePactet(16MB) should be set. HandshakeResponse41Packet handshakeResponse41Packet = new HandshakeResponse41Packet(sequenceId + 1, CapabilityFlag.calculateHandshakeCapabilityFlagsLower(), 16777215, ServerInfo.CHARSET, username, authResponse, database); context.writeAndFlush(handshakeResponse41Packet); } @@ -130,7 +130,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); } - private final byte[] securePasswordAuthentication(byte[] password, byte[] authPluginData){ + private byte[] securePasswordAuthentication(byte[] password, byte[] authPluginData){ try { MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); byte[] part1 = sha1.digest(password); From aabcb3eb47781dc2ec7985a53d6c1dfbe1296efa Mon Sep 17 00:00:00 2001 From: WangKai Date: Thu, 10 May 2018 11:47:01 +0800 Subject: [PATCH 089/351] fix some code style --- .../java/io/shardingjdbc/proxy/Bootstrap.java | 1 + .../proxy/backend/ShardingProxyClient.java | 48 +++++++++---------- .../proxy/backend/common/BackendHandler.java | 3 -- .../backend/common/BackendHandlerFactory.java | 8 +++- .../common/CommandResponsePacketsHandler.java | 3 +- .../common/SQLExecuteBackendHandler.java | 18 +++++-- .../common/SQLPacketsBackendHandler.java | 22 +++++---- .../backend/mysql/MySQLBackendHandler.java | 20 +++++--- .../netty/ClientHandlerInitializer.java | 5 +- .../proxy/frontend/ShardingProxy.java | 7 +-- .../frontend/common/FrontendHandler.java | 1 - .../proxy/metadata/ProxyShardingMetaData.java | 1 - .../mysql/packet/command/CommandPacket.java | 4 +- .../packet/command/CommandPacketFactory.java | 1 + .../text/fieldlist/ComFieldListPacket.java | 6 +-- .../command/text/query/ComQueryPacket.java | 6 +-- .../handshake/HandshakeResponse41Packet.java | 3 +- .../proxy/util/MySQLResultCache.java | 16 +++++-- .../proxy/util/SynchronizedFuture.java | 23 +++++---- 19 files changed, 121 insertions(+), 75 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/Bootstrap.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/Bootstrap.java index b14c6c1901ee4..a1cad47da0fc4 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/Bootstrap.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/Bootstrap.java @@ -36,6 +36,7 @@ public final class Bootstrap { * * @param args startup arguments * @throws InterruptedException interrupted exception + * @throws MalformedURLException URL exception */ public static void main(final String[] args) throws InterruptedException, MalformedURLException { new ShardingProxy().start(getPort(args)); diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java index 856f34a9bf028..6f3f9dde1f523 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/ShardingProxyClient.java @@ -33,7 +33,7 @@ import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.shardingjdbc.proxy.backend.netty.ClientHandlerInitializer; -import io.shardingjdbc.proxy.config.ShardingRuleRegistry; +import io.shardingjdbc.proxy.config.RuleRegistry; import lombok.Getter; import java.net.MalformedURLException; @@ -54,6 +54,7 @@ public final class ShardingProxyClient { @Getter private Map channelMap = Maps.newHashMap(); + @Getter private Map bootstrapMap = Maps.newHashMap(); @@ -64,38 +65,37 @@ public final class ShardingProxyClient { * @throws MalformedURLException url is illegal. */ public void start() throws MalformedURLException, InterruptedException { - try { - Map dataSourceConfigurationMap = ShardingRuleRegistry.getInstance().getDataSourceConfigurationMap(); - for (Map.Entry each : dataSourceConfigurationMap.entrySet()) { - URL url = new URL(each.getValue().getJdbcUrl().replaceAll("jdbc:mysql:", "http:")); - String ip = url.getHost(); - int port = url.getPort(); - String database = url.getPath().substring(1); - String username = (each.getValue()).getUsername(); - String password = (each.getValue()).getPassword(); - Bootstrap bootstrap = new Bootstrap(); - if (workerGroup instanceof EpollEventLoopGroup) { - groupsEpoll(bootstrap, ip, port, database, username, password); - } else { - groupsNio(bootstrap, ip, port, database, username, password); - } - //TODO use connection pool. - bootstrapMap.put(each.getKey(), bootstrap); - ChannelFuture future = bootstrap.connect(ip, port).sync(); - channelMap.put(each.getKey(), future.channel()); + Map dataSourceConfigurationMap = RuleRegistry.getInstance().getDataSourceConfigurationMap(); + for (Map.Entry each : dataSourceConfigurationMap.entrySet()) { + URL url = new URL(each.getValue().getJdbcUrl().replaceAll("jdbc:mysql:", "http:")); + String ip = url.getHost(); + int port = url.getPort(); + String database = url.getPath().substring(1); + String username = (each.getValue()).getUsername(); + String password = (each.getValue()).getPassword(); + Bootstrap bootstrap = new Bootstrap(); + if (workerGroup instanceof EpollEventLoopGroup) { + groupsEpoll(bootstrap, ip, port, database, username, password); + } else { + groupsNio(bootstrap, ip, port, database, username, password); } - }catch (Exception e){ - e.printStackTrace(); + //TODO use connection pool. + bootstrapMap.put(each.getKey(), bootstrap); + ChannelFuture future = bootstrap.connect(ip, port).sync(); + channelMap.put(each.getKey(), future.channel()); } } + /** + * Stop Sharding-Proxy. + */ public void stop() { if (workerGroup != null) { workerGroup.shutdownGracefully(); } } - private void groupsEpoll(final Bootstrap bootstrap, String ip, int port, String database, String username, String password) { + private void groupsEpoll(final Bootstrap bootstrap, final String ip, final int port, final String database, final String username, final String password) { workerGroup = new EpollEventLoopGroup(WORKER_MAX_THREADS); bootstrap.group(workerGroup) .channel(EpollSocketChannel.class) @@ -107,7 +107,7 @@ private void groupsEpoll(final Bootstrap bootstrap, String ip, int port, String .handler(new ClientHandlerInitializer(ip, port, database, username, password)); } - private void groupsNio(final Bootstrap bootstrap, String ip, int port, String database, String username, String password) { + private void groupsNio(final Bootstrap bootstrap, final String ip, final int port, final String database, final String username, final String password) { workerGroup = new NioEventLoopGroup(WORKER_MAX_THREADS); bootstrap.group(workerGroup) .channel(NioSocketChannel.class) diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandler.java index d3fedc99d2513..35fb4ad0f432f 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandler.java @@ -17,9 +17,6 @@ package io.shardingjdbc.proxy.backend.common; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelInboundHandlerAdapter; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; /** diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java index b3947b9272178..c888b3feaad7a 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/BackendHandlerFactory.java @@ -31,9 +31,15 @@ public final class BackendHandlerFactory { * Create bakcend handler instance. * * @param databaseType database type + * @param ip database ip + * @param port database port + * @param database database name + * @param username access database username + * @param password access database password * @return backend handler instance */ - public static CommandResponsePacketsHandler createBackendHandlerInstance(final DatabaseType databaseType, String ip, int port, String database, String username, String password) { + public static CommandResponsePacketsHandler createBackendHandlerInstance(final DatabaseType databaseType, final String ip, final int port, final String database, final String username, final + String password) { switch (databaseType) { case MySQL: return new MySQLBackendHandler(ip, port, database, username, password); diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java index 029cdcd706565..918f7841a4853 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/CommandResponsePacketsHandler.java @@ -17,7 +17,6 @@ package io.shardingjdbc.proxy.backend.common; -import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload; @@ -29,6 +28,8 @@ */ public abstract class CommandResponsePacketsHandler extends ChannelInboundHandlerAdapter { protected abstract void auth(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload); + protected abstract void genericResponsePacket(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload); + protected abstract void executeCommandResponsePackets(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload); } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java index a0761615bca35..408af28f8e42a 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java @@ -31,7 +31,8 @@ import io.shardingjdbc.core.routing.router.masterslave.MasterSlaveRouter; import io.shardingjdbc.core.routing.router.masterslave.MasterVisitedManager; import io.shardingjdbc.proxy.backend.mysql.MySQLPacketQueryResult; -import io.shardingjdbc.proxy.config.ShardingRuleRegistry; +import io.shardingjdbc.proxy.config.RuleRegistry; +import io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingjdbc.proxy.transport.mysql.constant.ColumnType; import io.shardingjdbc.proxy.transport.mysql.constant.StatusFlag; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; @@ -41,6 +42,7 @@ import io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket; import io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket; import io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket; +import lombok.Getter; import javax.sql.DataSource; import java.sql.Connection; @@ -64,7 +66,8 @@ public class SQLExecuteBackendHandler implements BackendHandler { private static final Integer FETCH_ONE_ROW_A_TIME = Integer.MIN_VALUE; - protected final String sql; + @Getter + private final String sql; private List connections; @@ -80,8 +83,10 @@ public class SQLExecuteBackendHandler implements BackendHandler { private boolean hasMoreResultValueFlag; + @Getter private final DatabaseType databaseType; + @Getter private final boolean showSQL; public SQLExecuteBackendHandler(final String sql, final DatabaseType databaseType, final boolean showSQL) { @@ -99,7 +104,7 @@ public CommandResponsePackets execute() { return RuleRegistry.getInstance().isOnlyMasterSlave() ? executeForMasterSlave() : executeForSharding(); } - private CommandResponsePackets executeForMasterSlave() { + protected CommandResponsePackets executeForMasterSlave() { MasterSlaveRouter masterSlaveRouter = new MasterSlaveRouter(RuleRegistry.getInstance().getMasterSlaveRule()); SQLStatement sqlStatement = new SQLJudgeEngine(sql).judge(); String dataSourceName = masterSlaveRouter.route(sqlStatement.getType()).iterator().next(); @@ -108,7 +113,7 @@ private CommandResponsePackets executeForMasterSlave() { return merge(sqlStatement, result); } - private CommandResponsePackets executeForSharding() { + protected CommandResponsePackets executeForSharding() { StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), databaseType, showSQL); SQLRouteResult routeResult = routingEngine.route(sql); if (routeResult.getExecutionUnits().isEmpty()) { @@ -122,7 +127,7 @@ private CommandResponsePackets executeForSharding() { return merge(routeResult.getSqlStatement(), result); } - protected CommandResponsePackets execute(final SQLStatement sqlStatement, final SQLExecutionUnit sqlExecutionUnit, final String sql) { + protected CommandResponsePackets execute(final SQLStatement sqlStatement, final String dataSourceName, final String sql) { switch (sqlStatement.getType()) { case DQL: case DAL: @@ -198,6 +203,8 @@ private CommandResponsePackets executeCommon(final DataSource dataSource, final } } catch (final SQLException ex) { return new CommandResponsePackets(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage())); + } finally { + MasterVisitedManager.clear(); } } @@ -366,6 +373,7 @@ private void cleanJDBCResources() { if (null != each) { try { each.close(); + MasterVisitedManager.clear(); } catch (final SQLException ignore) { } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java index 93394226998e8..da6b015f78e69 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLPacketsBackendHandler.java @@ -22,7 +22,9 @@ import io.shardingjdbc.core.parsing.parser.sql.SQLStatement; import io.shardingjdbc.core.routing.SQLExecutionUnit; import io.shardingjdbc.core.routing.SQLRouteResult; +import io.shardingjdbc.core.routing.StatementRoutingEngine; import io.shardingjdbc.proxy.backend.ShardingProxyClient; +import io.shardingjdbc.proxy.config.RuleRegistry; import io.shardingjdbc.proxy.transport.mysql.constant.StatusFlag; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; import io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket; @@ -48,15 +50,17 @@ public SQLPacketsBackendHandler(final String sql, final int connectionId, final } @Override - public CommandResponsePackets execute() { - SQLRouteResult routeResult = routingEngine.route(sql); + public CommandResponsePackets executeForSharding() { + StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), getDatabaseType(), + isShowSQL()); + SQLRouteResult routeResult = routingEngine.route(getSql()); if (routeResult.getExecutionUnits().isEmpty()) { return new CommandResponsePackets(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); } synchronizedFuture = new SynchronizedFuture<>(routeResult.getExecutionUnits().size()); MySQLResultCache.getInstance().put(connectionId, synchronizedFuture); for (SQLExecutionUnit each : routeResult.getExecutionUnits()) { - execute(routeResult.getSqlStatement(), each); + execute(routeResult.getSqlStatement(), each.getDataSource(), each.getSqlUnit().getSql()); } //TODO timeout should be set. List result = synchronizedFuture.get(30, TimeUnit.SECONDS); @@ -65,17 +69,19 @@ public CommandResponsePackets execute() { } @Override - protected CommandResponsePackets execute(final SQLStatement sqlStatement, final SQLExecutionUnit sqlExecutionUnit) { - Channel channel = ShardingProxyClient.getInstance().getChannelMap().get(sqlExecutionUnit.getDataSource()); + protected CommandResponsePackets execute(final SQLStatement sqlStatement, final String dataSourceName, final String sql) { + Channel channel = ShardingProxyClient.getInstance().getChannelMap().get(dataSourceName); //MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); switch (sqlStatement.getType()) { case DQL: - executeQuery(channel, sqlExecutionUnit.getSqlUnit().getSql()); + executeQuery(channel, sql); + break; case DML: case DDL: - executeUpdate(channel, sqlExecutionUnit.getSqlUnit().getSql(), sqlStatement); + executeUpdate(channel, sql, sqlStatement); + break; default: - executeCommon(channel, sqlExecutionUnit.getSqlUnit().getSql()); + executeCommon(channel, sql); } return null; } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java index 7165d8d19fc8f..96e5e3b76b8de 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/mysql/MySQLBackendHandler.java @@ -45,10 +45,15 @@ public class MySQLBackendHandler extends CommandResponsePacketsHandler { private boolean authorized; + private final String ip; + private final int port; + private final String database; + private final String username; + private final String password; @Override @@ -67,7 +72,7 @@ public void channelRead(final ChannelHandlerContext context, final Object messag } @Override - protected void auth(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload) { + protected void auth(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { int protocolVersion = header; String serverVersion = mysqlPacketPayload.readStringNul(); int connectionId = mysqlPacketPayload.readInt4(); @@ -82,14 +87,15 @@ protected void auth(ChannelHandlerContext context, int sequenceId, int header, M byte[] authPluginDataPart2 = mysqlPacketPayload.readStringNul().getBytes(); byte[] authPluginData = Bytes.concat(authPluginDataPart1, authPluginDataPart2); //byte[] authResponse = byteXOR(SHA1(password.getBytes()), SHA1(Bytes.concat(authPluginData, SHA1(SHA1(password.getBytes()))))); - byte[] authResponse = securePasswordAuthentication(password.getBytes(),authPluginData); + byte[] authResponse = securePasswordAuthentication(password.getBytes(), authPluginData); //TODO maxSizePactet(16MB) should be set. - HandshakeResponse41Packet handshakeResponse41Packet = new HandshakeResponse41Packet(sequenceId + 1, CapabilityFlag.calculateHandshakeCapabilityFlagsLower(), 16777215, ServerInfo.CHARSET, username, authResponse, database); + HandshakeResponse41Packet handshakeResponse41Packet = new HandshakeResponse41Packet(sequenceId + 1, CapabilityFlag.calculateHandshakeCapabilityFlagsLower(), 16777215, ServerInfo.CHARSET, + username, authResponse, database); context.writeAndFlush(handshakeResponse41Packet); } @Override - protected void genericResponsePacket(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload) { + protected void genericResponsePacket(final ChannelHandlerContext context, final int header, final MySQLPacketPayload mysqlPacketPayload) { switch (header) { case OKPacket.HEADER: if (!authorized) { @@ -114,12 +120,14 @@ protected void genericResponsePacket(ChannelHandlerContext context, int header, statusFlags = mysqlPacketPayload.readInt2(); log.debug("EofPacket[warnings={},statusFlags={}]", warnings, statusFlags); break; + default: + break; } } //TODO @Override - protected void executeCommandResponsePackets(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload) { + protected void executeCommandResponsePackets(final ChannelHandlerContext context, final int header, final MySQLPacketPayload mysqlPacketPayload) { int connectionId = MySQLResultCache.getInstance().getonnectionMap(context.channel().id().asShortText()); MySQLResultCache.getInstance().get(connectionId).setResponse(null); } @@ -130,7 +138,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); } - private byte[] securePasswordAuthentication(byte[] password, byte[] authPluginData){ + private byte[] securePasswordAuthentication(byte[] password, byte[] authPluginData) { try { MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); byte[] part1 = sha1.digest(password); diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java index 97d419708d2b7..dc36614ea86a2 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/netty/ClientHandlerInitializer.java @@ -20,7 +20,6 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; -import io.netty.channel.socket.SocketChannel; import io.shardingjdbc.core.constant.DatabaseType; import io.shardingjdbc.proxy.backend.common.BackendHandlerFactory; import io.shardingjdbc.proxy.transport.common.codec.PacketCodecFactory; @@ -35,9 +34,13 @@ public final class ClientHandlerInitializer extends ChannelInitializer { private final String ip; + private final int port; + private final String database; + private final String username; + private final String password; @Override diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/ShardingProxy.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/ShardingProxy.java index aea7d862b7fb8..08f88728b0f61 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/ShardingProxy.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/ShardingProxy.java @@ -30,7 +30,7 @@ import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.shardingjdbc.proxy.backend.ShardingProxyClient; -import io.shardingjdbc.proxy.config.ShardingRuleRegistry; +import io.shardingjdbc.proxy.config.RuleRegistry; import io.shardingjdbc.proxy.frontend.netty.ServerHandlerInitializer; import java.net.MalformedURLException; @@ -57,10 +57,11 @@ public final class ShardingProxy { * * @param port port * @throws InterruptedException interrupted exception + * @throws MalformedURLException URL exception */ public void start(final int port) throws InterruptedException, MalformedURLException { try { - if (ShardingRuleRegistry.WITHOUT_JDBC) { + if (RuleRegistry.WITHOUT_JDBC) { ShardingProxyClient.getInstance().start(); } ServerBootstrap bootstrap = new ServerBootstrap(); @@ -76,7 +77,7 @@ public void start(final int port) throws InterruptedException, MalformedURLExcep workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); userGroup.shutdownGracefully(); - if (ShardingRuleRegistry.WITHOUT_JDBC) { + if (RuleRegistry.WITHOUT_JDBC) { ShardingProxyClient.getInstance().stop(); } } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/common/FrontendHandler.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/common/FrontendHandler.java index dcdb78358f4d5..4bae0c6c14886 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/common/FrontendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/common/FrontendHandler.java @@ -20,7 +20,6 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; -import io.netty.channel.ChannelOutboundHandlerAdapter; /** * Frontend handler. diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/metadata/ProxyShardingMetaData.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/metadata/ProxyShardingMetaData.java index 6fa346e011de3..1203d47d1315d 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/metadata/ProxyShardingMetaData.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/metadata/ProxyShardingMetaData.java @@ -25,7 +25,6 @@ import lombok.RequiredArgsConstructor; import javax.sql.DataSource; -import java.sql.Connection; import java.sql.SQLException; import java.util.Collection; import java.util.Map; diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacket.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacket.java index 920de08a2507b..59c58893a45d1 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacket.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacket.java @@ -19,6 +19,7 @@ import io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacket; +import lombok.Getter; /** * Command packet. @@ -27,7 +28,8 @@ * @author wangkai */ public abstract class CommandPacket extends MySQLPacket { - protected final int connectionId; + @Getter + private final int connectionId; public CommandPacket(final int sequenceId, final int connectionId) { super(sequenceId); diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacketFactory.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacketFactory.java index 984a663f99639..06a3f15dc3090 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacketFactory.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacketFactory.java @@ -38,6 +38,7 @@ public final class CommandPacketFactory { * Get command Packet. * * @param sequenceId sequence ID + * @param connectionId MYSQL connection id * @param mysqlPacketPayload MySQL packet payload * @return Command packet */ diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java index 6ef2c7c6225e7..800d83473a532 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java @@ -21,7 +21,7 @@ import io.shardingjdbc.core.constant.ShardingConstant; import io.shardingjdbc.proxy.backend.common.SQLExecuteBackendHandler; import io.shardingjdbc.proxy.backend.common.SQLPacketsBackendHandler; -import io.shardingjdbc.proxy.config.ShardingRuleRegistry; +import io.shardingjdbc.proxy.config.RuleRegistry; import io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandPacket; @@ -62,8 +62,8 @@ public CommandResponsePackets execute() { log.debug("field wildcard received for Sharding-Proxy: {}", fieldWildcard); String sql = String.format("SHOW COLUMNS FROM %s FROM %s", table, ShardingConstant.LOGIC_SCHEMA_NAME); // TODO use common database type - if (ShardingRuleRegistry.WITHOUT_JDBC) { - return new SQLPacketsBackendHandler(sql, connectionId, DatabaseType.MySQL, true).execute(); + if (RuleRegistry.WITHOUT_JDBC) { + return new SQLPacketsBackendHandler(sql, getConnectionId(), DatabaseType.MySQL, true).execute(); } else { return new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, true).execute(); } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java index de4bfb8bbf658..84b8ef10c5c03 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java @@ -20,7 +20,7 @@ import io.shardingjdbc.core.constant.DatabaseType; import io.shardingjdbc.proxy.backend.common.SQLExecuteBackendHandler; import io.shardingjdbc.proxy.backend.common.SQLPacketsBackendHandler; -import io.shardingjdbc.proxy.config.ShardingRuleRegistry; +import io.shardingjdbc.proxy.config.RuleRegistry; import io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload; import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandPacket; @@ -60,7 +60,7 @@ public void write(final MySQLPacketPayload mysqlPacketPayload) { @Override public CommandResponsePackets execute() { log.debug("COM_QUERY received for Sharding-Proxy: {}", sql); - if (ShardingRuleRegistry.WITHOUT_JDBC) { + if (RuleRegistry.WITHOUT_JDBC) { return sqlPacketsBackendHandler.execute(); } else { return sqlExecuteBackendHandler.execute(); @@ -74,7 +74,7 @@ public CommandResponsePackets execute() { */ public boolean hasMoreResultValue() { try { - if (ShardingRuleRegistry.WITHOUT_JDBC) { + if (RuleRegistry.WITHOUT_JDBC) { return false; } else { return sqlExecuteBackendHandler.hasMoreResultValue(); diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java index 70d74f372a276..01f1a4e5095c5 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java @@ -55,7 +55,8 @@ public HandshakeResponse41Packet(final MySQLPacketPayload mysqlPacketPayload) { readDatabase(mysqlPacketPayload); } - public HandshakeResponse41Packet(final int sequenceId, final int capabilityFlags, final int maxPacketSize, final int characterSet, final String username, final byte[] authResponse, final String database) { + public HandshakeResponse41Packet(final int sequenceId, final int capabilityFlags, final int maxPacketSize, final int characterSet, final String username, final byte[] authResponse, final String + database) { super(sequenceId); this.capabilityFlags = capabilityFlags; this.maxPacketSize = maxPacketSize; diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java index f598ff866e786..e396f537e5664 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/MySQLResultCache.java @@ -30,6 +30,7 @@ */ public class MySQLResultCache { private static final MySQLResultCache INSTANCE = new MySQLResultCache(); + //TODO expire time should be set. private Cache resultCache = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.SECONDS).build(); @@ -38,41 +39,46 @@ public class MySQLResultCache { private Cache channelCache = CacheBuilder.newBuilder().build(); /** + * put synchronizedFuture by connectionId. * @param connectionId mysql connection id. * @param synchronizedFuture multiple result set. */ - public void put(int connectionId, SynchronizedFuture synchronizedFuture) { + public void put(final int connectionId, final SynchronizedFuture synchronizedFuture) { resultCache.put(connectionId, synchronizedFuture); } /** + * get SynchronizedFuture by connectionId. * @param connectionId mysql connection id. * @return multiple result set. */ - public SynchronizedFuture get(int connectionId) { + public SynchronizedFuture get(final int connectionId) { return resultCache.getIfPresent(connectionId); } /** + * delete SynchronizedFuture by connectionId. * @param connectionId mysql connection id. */ - public void delete(int connectionId) { + public void delete(final int connectionId) { resultCache.invalidate(connectionId); } /** + * put connectionId by channelId. * @param channelId netty channel id. * @param connectionId mysql connection id. */ - public void putConnectionMap(String channelId, int connectionId) { + public void putConnectionMap(final String channelId, final int connectionId) { connectionCache.put(channelId, connectionId); } /** + * get connectionId by channelId. * @param channelId netty channel id. * @return connectionId mysql connection id. */ - public int getonnectionMap(String channelId) { + public int getonnectionMap(final String channelId) { return connectionCache.getIfPresent(channelId); } diff --git a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java index 63313c7b7e65a..ff81e82b0d768 100644 --- a/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java +++ b/sharding-proxy/src/main/java/io/shardingjdbc/proxy/util/SynchronizedFuture.java @@ -37,13 +37,13 @@ public class SynchronizedFuture implements Future> { private long beginTime = System.currentTimeMillis(); - public SynchronizedFuture(int resultSize) { + public SynchronizedFuture(final int resultSize) { latch = new CountDownLatch(resultSize); responses = Lists.newArrayListWithCapacity(resultSize); } @Override - public boolean cancel(boolean mayInterruptIfRunning) { + public boolean cancel(final boolean mayInterruptIfRunning) { return false; } @@ -66,8 +66,14 @@ public List get() throws InterruptedException { return this.responses; } + /** + * wait for responses. + * @param timeout wait timeout. + * @param unit time unit + * @return responses. + */ @Override - public List get(long timeout, TimeUnit unit) { + public List get(final long timeout, final TimeUnit unit) { try { if (latch.await(timeout, unit)) { return this.responses; @@ -78,12 +84,13 @@ public List get(long timeout, TimeUnit unit) { return this.responses; } - public void setResponse(T response) { + /** + * set response and count down. + * @param response sql command result. + */ + public void setResponse(final T response) { this.responses.add(response); latch.countDown(); } - public long getBeginTime() { - return beginTime; - } -} \ No newline at end of file +} From e51911e5c7884ff85503997d90fa9aa9290dfe12 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 10 May 2018 11:51:35 +0800 Subject: [PATCH 090/351] exception --- .../client/zookeeper/strategy/ContentionStrategy.java | 9 +++++++-- src/main/resources/log4j.properties | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index c471c4ef9a84d..9129bb1c59c9a 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -278,8 +278,13 @@ private void deleteBranch(String key) throws KeeperException, InterruptedExcepti String node = pathStack.pop(); // contrast cache if (provider.checkExists(node)){ - provider.deleteOnlyCurrent(node); - continue; + try { + provider.deleteOnlyCurrent(node); + } catch (KeeperException.NotEmptyException ee){ + logger.warn("deleteBranch {} exist other children:{}", node, this.getChildren(node)); + logger.debug(ee.getMessage()); + return; + } } logger.info("deleteBranch node not exist:{}", node); } diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties index 88315b244bac9..dadc8aa5d4ac1 100644 --- a/src/main/resources/log4j.properties +++ b/src/main/resources/log4j.properties @@ -12,7 +12,7 @@ log4j.logger.org.apache.zookeeper=INFO,zk #don't append into root, only this log4j.additivity.org.apache.zookeeper=false log4j.appender.zk=org.apache.log4j.DailyRollingFileAppender -log4j.appender.zk.DatePattern='.'yyyy-MM-dd +log4j.appender.zk.DatePattern='.'yyyy-MM-dd'.log' log4j.appender.zk.File=logs/zk.log log4j.appender.zk.layout=org.apache.log4j.PatternLayout log4j.appender.zk.Append=true @@ -23,7 +23,7 @@ log4j.logger.com.saaavsaaa.client=DEBUG,INFO,client #don't append into root, only this log4j.additivity.com.saaavsaaa.client=false log4j.appender.client=org.apache.log4j.DailyRollingFileAppender -log4j.appender.client.DatePattern='.'yyyy-MM-dd +log4j.appender.client.DatePattern='.'yyyy-MM-dd'.log' log4j.appender.client.File=logs/client.log log4j.appender.client.layout=org.apache.log4j.PatternLayout log4j.appender.client.Append=true From 4964d835ab898cdc5aa823cf0f6777de80386044 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 10 May 2018 17:58:03 +0800 Subject: [PATCH 091/351] IClient --- .../com/saaavsaaa/client/action/IClient.java | 25 +++--- .../client/action/IExecStrategy.java | 2 +- .../saaavsaaa/client/action/IGroupAction.java | 19 ++++ .../{Client.java => BaseClient.java} | 30 ++++--- .../client/zookeeper/ClientFactory.java | 5 +- .../saaavsaaa/client/zookeeper/Provider.java | 4 +- .../client/zookeeper/UsualClient.java | 2 +- .../zookeeper/strategy/VersionStrategy.java | 8 ++ .../client/zookeeper/BaseClientTest.java | 89 ++++++++++--------- .../client/zookeeper/CacheClientTest.java | 4 +- .../client/zookeeper/CacheWathClientTest.java | 3 +- .../client/zookeeper/ClientsTest.java | 43 +++++---- .../zookeeper/ContentionStrategyTest.java | 29 +++--- .../client/zookeeper/UsualClientTest.java | 3 +- .../zookeeper/UsualWatchClientTest.java | 3 +- 15 files changed, 156 insertions(+), 113 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/action/IGroupAction.java rename src/main/java/com/saaavsaaa/client/zookeeper/{Client.java => BaseClient.java} (90%) create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/VersionStrategy.java diff --git a/src/main/java/com/saaavsaaa/client/action/IClient.java b/src/main/java/com/saaavsaaa/client/action/IClient.java index 9dea54710c2b8..97c412ed3a55b 100644 --- a/src/main/java/com/saaavsaaa/client/action/IClient.java +++ b/src/main/java/com/saaavsaaa/client/action/IClient.java @@ -1,31 +1,28 @@ package com.saaavsaaa.client.action; +import com.saaavsaaa.client.utility.section.Listener; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; + +import java.io.IOException; /** * Created by aaa */ -public interface IClient extends IAction{ - - void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException ; - - void deleteAllChildren(final String key) throws KeeperException, InterruptedException ; - - /* - * delete the current node with force and delete the super node whose only child node is current node recursively - */ - void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException ; +public interface IClient extends IAction, IGroupAction{ + void start() throws IOException, InterruptedException; + void close() throws InterruptedException; + Watcher registerWatch(String key, Listener listener); + void unregisterWatch(String key); /* void createNamespace(); void deleteNamespace(); - Watcher registerWatch(String key, Listener listener); - void unregisterWatch(String key); - void close(); + Watcher registerWatch(Listener listener); void setRootNode(String namespace); - void start(); + void setAuthorities(String scheme, byte[] auth); ZooKeeper getZooKeeper(); */ diff --git a/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java b/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java index 6df729a9acb2b..3970688e0c2bc 100644 --- a/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java +++ b/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java @@ -5,6 +5,6 @@ /** * Created by aaa */ -public interface IExecStrategy extends IClient { +public interface IExecStrategy extends IAction, IGroupAction { Provider getProvider(); } diff --git a/src/main/java/com/saaavsaaa/client/action/IGroupAction.java b/src/main/java/com/saaavsaaa/client/action/IGroupAction.java new file mode 100644 index 0000000000000..4d68a5368acd2 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/action/IGroupAction.java @@ -0,0 +1,19 @@ +package com.saaavsaaa.client.action; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; + +/** + * Created by aaa on 18-5-10. + */ +public interface IGroupAction { + + void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException ; + + void deleteAllChildren(final String key) throws KeeperException, InterruptedException ; + + /* + * delete the current node with force and delete the super node whose only child node is current node recursively + */ + void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException ; +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java similarity index 90% rename from src/main/java/com/saaavsaaa/client/zookeeper/Client.java rename to src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java index 61b37a4911167..4d35810a396b2 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Client.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java @@ -22,8 +22,8 @@ /** * Created by aaa */ -public abstract class Client implements IClient { - private static final Logger logger = LoggerFactory.getLogger(Client.class); +public abstract class BaseClient implements IClient { + private static final Logger logger = LoggerFactory.getLogger(BaseClient.class); private static final CountDownLatch CONNECTED = new CountDownLatch(1); protected static final Map watchers = new ConcurrentHashMap<>(); @@ -40,21 +40,28 @@ public abstract class Client implements IClient { protected List authorities; private ClientFactory clientFactory; - protected Client(final String servers, final int sessionTimeoutMilliseconds) { + protected BaseClient(final String servers, final int sessionTimeoutMilliseconds) { this.servers = servers; this.sessionTimeOut = sessionTimeoutMilliseconds; } + @Override public synchronized void start() throws IOException, InterruptedException { - logger.debug("Client servers:{},sessionTimeOut:{}", servers, sessionTimeOut); + logger.debug("BaseClient servers:{},sessionTimeOut:{}", servers, sessionTimeOut); zooKeeper = new ZooKeeper(servers, sessionTimeOut, startWatcher()); if (!StringUtil.isNullOrBlank(scheme)) { zooKeeper.addAuthInfo(scheme, auth); - logger.debug("Client scheme:{},auth:{}", scheme, auth); + logger.debug("BaseClient scheme:{},auth:{}", scheme, auth); } CONNECTED.await(); } + @Override + public void close() throws InterruptedException { + zooKeeper.close(); + logger.debug("zk closed"); + } + public abstract void useExecStrategy(StrategyType strategyType); ZooKeeper getZooKeeper(){ @@ -64,17 +71,17 @@ ZooKeeper getZooKeeper(){ private Watcher startWatcher() { return new Watcher(){ public void process(WatchedEvent event) { - logger.debug("Client process event:{}", event.toString()); + logger.debug("BaseClient process event:{}", event.toString()); if(Event.KeeperState.SyncConnected == event.getState()){ if(Event.EventType.None == event.getType()){ CONNECTED.countDown(); - logger.debug("Client startWatcher SyncConnected"); + logger.debug("BaseClient startWatcher SyncConnected"); return; } } if (globalListenerRegistered){ watchers.get(Constants.GLOBAL_LISTENER_KEY).process(event); - logger.debug("Client " + Constants.GLOBAL_LISTENER_KEY + " process"); + logger.debug("BaseClient " + Constants.GLOBAL_LISTENER_KEY + " process"); } if (Properties.INSTANCE.watchOn() && watchers.containsKey(event.getPath())){ watchers.get(event.getPath()).process(event); @@ -98,6 +105,7 @@ public void process(WatchedEvent event) { logger.debug("globalListenerRegistered:{}", globalListenerRegistered); } + @Override public Watcher registerWatch(final String key, final Listener listener){ String path = PathUtil.getRealPath(rootNode, key); // listener.setKey(path); @@ -112,6 +120,7 @@ public void process(WatchedEvent event) { return watcher; } + @Override public void unregisterWatch(final String key){ if (StringUtil.isNullOrBlank(key)){ throw new IllegalArgumentException("key should not be blank"); @@ -123,11 +132,6 @@ public void unregisterWatch(final String key){ } } - public void close() throws InterruptedException { - zooKeeper.close(); - logger.debug("zk closed"); - } - void createNamespace() throws KeeperException, InterruptedException { createNamespace(Constants.NOTHING_DATA); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 12fe60d630fdf..03a2b64ba5a4c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.Listener; import org.slf4j.Logger; @@ -14,7 +15,7 @@ public class ClientFactory { // private static final String CLIENT_EXCLUSIVE_NODE = "ZKC"; private static final Logger logger = LoggerFactory.getLogger(ClientFactory.class); - private Client client; + private BaseClient client; private Listener globalListener; private String namespace; private String scheme; @@ -54,7 +55,7 @@ public ClientFactory watch(final Listener listener){ return this; } - public synchronized Client start() throws IOException, InterruptedException { + public synchronized IClient start() throws IOException, InterruptedException { client.setClientFactory(this); client.setRootNode(namespace); client.setAuthorities(scheme , auth); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java b/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java index 76e2b016f8be7..793fb400dbbd5 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java @@ -19,13 +19,13 @@ */ public class Provider implements IProvider { private static final Logger logger = LoggerFactory.getLogger(Provider.class); - protected final Client client; + protected final BaseClient client; private final ZooKeeper zooKeeper; protected final boolean watched; protected final List authorities; protected final String rootNode; - Provider(final String rootNode, final Client client, final boolean watched, final List authorities){ + Provider(final String rootNode, final BaseClient client, final boolean watched, final List authorities){ this.rootNode = rootNode; this.client = client; this.zooKeeper = client.getZooKeeper(); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 2104362027b4a..ee02c6e6bba29 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -17,7 +17,7 @@ /** * Created by aaa */ -public class UsualClient extends Client { +public class UsualClient extends BaseClient { private static final Logger logger = LoggerFactory.getLogger(UsualClient.class); protected final Map strategies = new ConcurrentHashMap<>(); protected IExecStrategy strategy; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/VersionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/VersionStrategy.java new file mode 100644 index 0000000000000..21baff39ca301 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/VersionStrategy.java @@ -0,0 +1,8 @@ +package com.saaavsaaa.client.zookeeper.strategy; + +/** + * Created by aaa + * node version + */ +public class VersionStrategy { +} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java index 3bc417c5a8d7e..872cae4e20908 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.utility.PathUtil; @@ -21,15 +22,17 @@ * Created by aaa */ public abstract class BaseClientTest { - protected Client testClient = null; + protected IClient testClient = null; + protected ZooKeeper zooKeeper; @Before public void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); testClient = createClient(creator); + zooKeeper = ((BaseClient)testClient).getZooKeeper(); } - protected abstract Client createClient(ClientFactory creator) throws IOException, InterruptedException; + protected abstract IClient createClient(ClientFactory creator) throws IOException, InterruptedException; @After public void stop() throws InterruptedException { @@ -40,51 +43,51 @@ public void stop() throws InterruptedException { @Ignore @Test public void deleteRoot() throws KeeperException, InterruptedException { - testClient.deleteNamespace(); - assert testClient.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; + ((BaseClient)testClient).deleteNamespace(); + assert zooKeeper.exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; } - protected void createRoot(Client client) throws KeeperException, InterruptedException { - client.createNamespace(); - assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) != null; - client.deleteNamespace(); - assert client.getZooKeeper().exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; + protected void createRoot(IClient client) throws KeeperException, InterruptedException { + ((BaseClient)client).createNamespace(); + assert zooKeeper.exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) != null; + ((BaseClient)client).deleteNamespace(); + assert zooKeeper.exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; } - protected void createChild(Client client) throws KeeperException, InterruptedException { + protected void createChild(IClient client) throws KeeperException, InterruptedException { String key = "a/b/bb"; client.createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) != null; + assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) != null; client.deleteCurrentBranch(key); - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) == null; + assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) == null; } - protected void deleteBranch(Client client) throws KeeperException, InterruptedException { + protected void deleteBranch(IClient client) throws KeeperException, InterruptedException { String keyB = "a/b/bb"; client.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; + assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; String keyC = "a/c/cc"; client.createAllNeedPath(keyC, "ccc11", CreateMode.PERSISTENT); - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; + assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; client.deleteCurrentBranch(keyC); - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) == null; - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), false) != null; + assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) == null; + assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), false) != null; client.deleteCurrentBranch(keyB); - assert client.getZooKeeper().exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; + assert zooKeeper.exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; client.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; + assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; client.deleteCurrentBranch(keyB); - assert client.getZooKeeper().exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; + assert zooKeeper.exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; } - protected void isExisted(Client client) throws KeeperException, InterruptedException { + protected void isExisted(IClient client) throws KeeperException, InterruptedException { String key = "a/b/bb"; client.createAllNeedPath(key, "", CreateMode.PERSISTENT); assert isExisted(key, client); client.deleteCurrentBranch(key); } - protected void get(Client client) throws KeeperException, InterruptedException { + protected void get(IClient client) throws KeeperException, InterruptedException { client.createAllNeedPath("a/b", "bbb11", CreateMode.PERSISTENT); String key = "a"; assert getDirectly(key, client).equals(""); @@ -93,7 +96,7 @@ protected void get(Client client) throws KeeperException, InterruptedException { client.deleteCurrentBranch("a/b"); } - protected void asynGet(Client client) throws KeeperException, InterruptedException { + protected void asynGet(IClient client) throws KeeperException, InterruptedException { CountDownLatch ready = new CountDownLatch(1); String key = "a/b"; String value = "bbb11"; @@ -112,15 +115,15 @@ public void processResult(int rc, String path, Object ctx, byte[] data, Stat sta client.deleteCurrentBranch("a/b"); } - private String getDirectly(String key, Client client) throws KeeperException, InterruptedException { + private String getDirectly(String key, IClient client) throws KeeperException, InterruptedException { return new String(client.getData(key)); } - private boolean isExisted(String key, Client client) throws KeeperException, InterruptedException { + private boolean isExisted(String key, IClient client) throws KeeperException, InterruptedException { return client.checkExists(key); } - protected void getChildrenKeys(Client client) throws KeeperException, InterruptedException { + protected void getChildrenKeys(IClient client) throws KeeperException, InterruptedException { String key = "a/b"; String current = "a"; client.createAllNeedPath(key, "", CreateMode.PERSISTENT); @@ -134,7 +137,7 @@ public int compare(final String o1, final String o2) { client.deleteCurrentBranch(key); } - protected void persist(Client client) throws KeeperException, InterruptedException { + protected void persist(IClient client) throws KeeperException, InterruptedException { String key = "a"; String value = "aa"; String newValue = "aaa"; @@ -151,41 +154,41 @@ protected void persist(Client client) throws KeeperException, InterruptedExcepti client.deleteCurrentBranch(key); } - private void updateWithCheck(String key, String value, Client client) throws KeeperException, InterruptedException { + private void updateWithCheck(String key, String value, IClient client) throws KeeperException, InterruptedException { client.transaction().check(key, Constants.VERSION).setData(key, value.getBytes(Constants.UTF_8), Constants.VERSION).commit(); } - protected void persistEphemeral(Client client) throws KeeperException, InterruptedException { + protected void persistEphemeral(IClient client) throws KeeperException, InterruptedException { String key = "a/b/bb"; String value = "b1b"; client.createAllNeedPath(key, value, CreateMode.PERSISTENT); -// assert client.getZooKeeper().exists(PathUtil.getRealPath(ROOT, key), null).getEphemeralOwner() == 0; +// assert zooKeeper.exists(PathUtil.getRealPath(ROOT, key), null).getEphemeralOwner() == 0; Stat stat = new Stat(); - client.getZooKeeper().getData(PathUtil.getRealPath(TestSupport.ROOT, key), false, stat); + zooKeeper.getData(PathUtil.getRealPath(TestSupport.ROOT, key), false, stat); assert stat.getEphemeralOwner() == 0; client.deleteAllChildren(key); assert !isExisted(key, client); client.createAllNeedPath(key, value, CreateMode.EPHEMERAL); - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key), null).getEphemeralOwner() != 0; // Ephemeral node connection session id + assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, key), null).getEphemeralOwner() != 0; // Ephemeral node connection session id client.deleteCurrentBranch(key); } - protected void delAllChildren(Client client) throws KeeperException, InterruptedException { + protected void delAllChildren(IClient client) throws KeeperException, InterruptedException { String key = "a/b/bb"; client.createAllNeedPath(key, "bb", CreateMode.PERSISTENT); key = "a/c/cc"; client.createAllNeedPath(key, "cc", CreateMode.PERSISTENT); - System.out.println(client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), null).getNumChildren()); // nearest children count - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) != null; + System.out.println(zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), null).getNumChildren()); // nearest children count + assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) != null; client.deleteAllChildren("a"); - assert client.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) == null; - assert client.getZooKeeper().exists("/" + TestSupport.ROOT, false) != null; - client.deleteNamespace(); + assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) == null; + assert zooKeeper.exists("/" + TestSupport.ROOT, false) != null; + ((BaseClient)client).deleteNamespace(); } - protected void watch(Client client) throws KeeperException, InterruptedException { + protected void watch(IClient client) throws KeeperException, InterruptedException { List expected = new ArrayList<>(); expected.add("update_/test/a_value"); expected.add("update_/test/a_value1"); @@ -213,7 +216,7 @@ protected void watch(Client client) throws KeeperException, InterruptedException client.unregisterWatch(key); } - protected Listener buildListener(Client client, List actual){ + protected Listener buildListener(IClient client, List actual){ Listener listener = new Listener() { @Override public void process(WatchedEvent event) { @@ -226,7 +229,7 @@ public void process(WatchedEvent event) { case NodeChildrenChanged: { String result; try { - result = new String(client.getZooKeeper().getData(event.getPath(),false, null)); + result = new String(zooKeeper.getData(event.getPath(),false, null)); System.out.println(); } catch (KeeperException e) { result = e.getMessage(); @@ -252,8 +255,8 @@ public void process(WatchedEvent event) { return listener; } - protected void close(Client client) throws Exception { + protected void close(IClient client) throws Exception { client.close(); - assert client.getZooKeeper().getState() == ZooKeeper.States.CLOSED; + assert zooKeeper.getState() == ZooKeeper.States.CLOSED; } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java index 7f9500c20d49b..4d6c23aca3148 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java @@ -1,5 +1,7 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.action.IClient; + import java.io.IOException; /** @@ -8,7 +10,7 @@ */ public class CacheClientTest extends UsualClientTest { @Override - protected Client createClient(ClientFactory creator) throws IOException, InterruptedException { + protected IClient createClient(ClientFactory creator) throws IOException, InterruptedException { return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java index c54d1f62f0c53..e86f69b3fd890 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.section.Listener; import java.io.IOException; @@ -9,7 +10,7 @@ */ public class CacheWathClientTest extends UsualClientTest { @Override - protected Client createClient(ClientFactory creator) throws IOException, InterruptedException { + protected IClient createClient(ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java index de7f0515d7fec..e9a094a41a65a 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java @@ -1,7 +1,9 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.section.Listener; import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooKeeper; import org.junit.Test; import java.io.IOException; @@ -12,7 +14,7 @@ * Created by aaa */ public class ClientsTest extends BaseClientTest { - private List clients; + private List clients; private final int count = 5; private final int shard = 2; @@ -26,8 +28,8 @@ public void start() throws IOException, InterruptedException { } @Override - protected Client createClient(ClientFactory creator) throws IOException, InterruptedException { - Client client; + protected IClient createClient(ClientFactory creator) throws IOException, InterruptedException { + IClient client; if (clients.size() % shard == 1){ System.out.println("create client"); client = newClient(creator); @@ -35,21 +37,22 @@ protected Client createClient(ClientFactory creator) throws IOException, Interru System.out.println("create watch client"); client = newWatchClient(creator); } + zooKeeper = ((BaseClient)client).getZooKeeper(); return client; } - private Client newClient(ClientFactory creator) throws IOException, InterruptedException { + private IClient newClient(ClientFactory creator) throws IOException, InterruptedException { return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); } - protected Client newWatchClient(ClientFactory creator) throws IOException, InterruptedException { + protected IClient newWatchClient(ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); } @Override public void stop() throws InterruptedException { - for (Client client : clients) { + for (IClient client : clients) { client.close(); } clients = null; @@ -57,85 +60,87 @@ public void stop() throws InterruptedException { @Test public void createRoot() throws KeeperException, InterruptedException { - for (Client client : clients) { + for (IClient client : clients) { super.createRoot(client); } } @Test public void createChild() throws KeeperException, InterruptedException { - for (Client client : clients) { + for (IClient client : clients) { super.createChild(client); } } @Test public void deleteBranch() throws KeeperException, InterruptedException { - for (Client client : clients) { + for (IClient client : clients) { super.deleteBranch(client); } } @Test public void isExisted() throws KeeperException, InterruptedException { - for (Client client : clients) { + for (IClient client : clients) { super.isExisted(client); } } @Test public void get() throws KeeperException, InterruptedException { - for (Client client : clients) { + for (IClient client : clients) { super.get(client); } } @Test public void asynGet() throws KeeperException, InterruptedException { - for (Client client : clients) { + for (IClient client : clients) { super.asynGet(client); } } @Test public void getChildrenKeys() throws KeeperException, InterruptedException { - for (Client client : clients) { + for (IClient client : clients) { super.getChildrenKeys(client); } } @Test public void persist() throws KeeperException, InterruptedException { - for (Client client : clients) { + for (IClient client : clients) { super.persist(client); } } @Test public void persistEphemeral() throws KeeperException, InterruptedException { - for (Client client : clients) { + for (IClient client : clients) { super.persistEphemeral(client); } } @Test public void delAllChildren() throws KeeperException, InterruptedException { - for (Client client : clients) { + for (IClient client : clients) { super.delAllChildren(client); } } @Test public void watch() throws KeeperException, InterruptedException { - for (Client client : clients) { + for (IClient client : clients) { super.watch(client); } } @Test public void close() throws Exception { - for (Client client : clients) { - super.close(client); + for (IClient client : clients) { + ZooKeeper zk = ((BaseClient)client).getZooKeeper(); + client.close(); + assert zk.getState() == ZooKeeper.States.CLOSED; } } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java index 558d12b1aa811..24d256a696940 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; @@ -15,10 +16,10 @@ */ public class ContentionStrategyTest extends UsualClientTest { @Override - protected Client createClient(final ClientFactory creator) throws IOException, InterruptedException { + protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); - Client client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - client.useExecStrategy(StrategyType.CONTENTION); + IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + ((BaseClient)client).useExecStrategy(StrategyType.CONTENTION); return client; } @@ -28,24 +29,24 @@ protected Client createClient(final ClientFactory creator) throws IOException, I public void deleteBranch() throws KeeperException, InterruptedException { String keyB = "a/b/bb"; testClient.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); - assert testClient.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; + assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; String keyC = "a/c/cc"; testClient.createAllNeedPath(keyC, "ccc11", CreateMode.PERSISTENT); - assert testClient.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; + assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; testClient.deleteCurrentBranch(keyC); - assert testClient.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) == null; - assert testClient.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), false) != null; + assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) == null; + assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), false) != null; testClient.deleteCurrentBranch(keyB); // because Constants.CHANGING_KEY, root still exist - List children = testClient.getZooKeeper().getChildren(PathUtil.checkPath(TestSupport.ROOT), false); + List children = zooKeeper.getChildren(PathUtil.checkPath(TestSupport.ROOT), false); assert children.size() == 0; - testClient.deleteNamespace(); - assert testClient.getZooKeeper().exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; + ((BaseClient)testClient).deleteNamespace(); + assert zooKeeper.exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; testClient.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); - assert testClient.getZooKeeper().exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; + assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; testClient.deleteCurrentBranch(keyB); - children = testClient.getZooKeeper().getChildren(PathUtil.checkPath(TestSupport.ROOT), false); + children = zooKeeper.getChildren(PathUtil.checkPath(TestSupport.ROOT), false); assert children.size() == 0; - testClient.deleteNamespace(); - assert testClient.getZooKeeper().exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; + ((BaseClient)testClient).deleteNamespace(); + assert zooKeeper.exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index 3450b3bc0b017..09d9972cceb36 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.action.IClient; import org.apache.zookeeper.KeeperException; import org.junit.Test; @@ -11,7 +12,7 @@ public class UsualClientTest extends BaseClientTest { @Override - protected Client createClient(final ClientFactory creator) throws IOException, InterruptedException { + protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java index 024ef80af946a..2e2d7f6923229 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.section.Listener; import java.io.IOException; @@ -10,7 +11,7 @@ public class UsualWatchClientTest extends UsualClientTest { @Override - protected Client createClient(final ClientFactory creator) throws IOException, InterruptedException { + protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); } From 65cc433c65cc953d73148500392c07eed829ff20 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 11 May 2018 11:44:09 +0800 Subject: [PATCH 092/351] base --- .../client/action/IExecStrategy.java | 4 +- .../com/saaavsaaa/client/cache/PathTree.java | 10 +-- .../client/election/LeaderElection.java | 6 +- .../client/utility/section/ClientTask.java | 8 +-- .../client/zookeeper/ClientFactory.java | 23 +------ .../client/zookeeper/RetryProvider.java | 16 +++++ .../client/zookeeper/UsualClient.java | 6 +- .../zookeeper/{ => base}/BaseClient.java | 10 +-- .../zookeeper/base/BaseClientFactory.java | 32 ++++++++++ .../{Provider.java => base/BaseProvider.java} | 20 +++--- .../{strategy => base}/BaseStrategy.java | 9 ++- .../strategy/ContentionStrategy.java | 4 +- .../zookeeper/strategy/UsualStrategy.java | 5 +- .../client/zookeeper/ClientsTest.java | 5 +- .../zookeeper/ContentionStrategyTest.java | 5 +- .../client/zookeeper/TestSupport.java | 10 +-- .../client/zookeeper/UsualClientTest.java | 1 + .../zookeeper/{ => base}/BaseClientTest.java | 61 +++++++++++-------- 18 files changed, 142 insertions(+), 93 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java rename src/main/java/com/saaavsaaa/client/zookeeper/{ => base}/BaseClient.java (96%) create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java rename src/main/java/com/saaavsaaa/client/zookeeper/{Provider.java => base/BaseProvider.java} (87%) rename src/main/java/com/saaavsaaa/client/zookeeper/{strategy => base}/BaseStrategy.java (66%) rename src/test/java/com/saaavsaaa/client/zookeeper/{ => base}/BaseClientTest.java (77%) diff --git a/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java b/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java index 3970688e0c2bc..142279b8d2ce5 100644 --- a/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java +++ b/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java @@ -1,10 +1,10 @@ package com.saaavsaaa.client.action; -import com.saaavsaaa.client.zookeeper.Provider; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; /** * Created by aaa */ public interface IExecStrategy extends IAction, IGroupAction { - Provider getProvider(); + BaseProvider getProvider(); } diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index 647b596bd0303..bce466f75a026 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -5,7 +5,7 @@ import com.saaavsaaa.client.utility.section.ClientTask; import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.utility.section.Properties; -import com.saaavsaaa.client.zookeeper.Provider; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.common.PathUtils; @@ -28,10 +28,10 @@ public final class PathTree { private final AtomicReference rootNode = new AtomicReference<>(); private boolean executorStart = false; private ScheduledExecutorService cacheService; - private final Provider provider; + private final BaseProvider provider; private PathStatus Status; - public PathTree(final String root, final Provider provider) { + public PathTree(final String root, final BaseProvider provider) { this.rootNode.set(new PathNode(root)); this.Status = PathStatus.RELEASE; this.provider = provider; @@ -61,7 +61,7 @@ public synchronized void loading() throws KeeperException, InterruptedException } } - private void attechIntoNode(final List children, final PathNode pathNode, final Provider provider) throws KeeperException, InterruptedException { + private void attechIntoNode(final List children, final PathNode pathNode, final BaseProvider provider) throws KeeperException, InterruptedException { logger.debug("attechIntoNode children:{}", children); if (children.isEmpty()){ logger.info("attechIntoNode there are no children"); @@ -88,7 +88,7 @@ public void refreshPeriodic(final long period){ cacheService = Executors.newSingleThreadScheduledExecutor(); cacheService.scheduleAtFixedRate(new ClientTask(provider) { @Override - public void run(Provider provider) throws KeeperException, InterruptedException { + public void run(BaseProvider provider) throws KeeperException, InterruptedException { logger.debug("cacheService run:{}", getStatus()); if (PathStatus.RELEASE == getStatus()) { loading(); diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index 2450ea51000cb..3f244e7edb952 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -5,7 +5,7 @@ import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.utility.section.Properties; import com.saaavsaaa.client.utility.section.WatcherCreator; -import com.saaavsaaa.client.zookeeper.Provider; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; @@ -19,7 +19,7 @@ public abstract class LeaderElection { private static final Logger logger = LoggerFactory.getLogger(LeaderElection.class); private boolean done = false; - private boolean contend(final String node, final Provider provider, final Listener listener) throws KeeperException, InterruptedException { + private boolean contend(final String node, final BaseProvider provider, final Listener listener) throws KeeperException, InterruptedException { boolean success = false; try { provider.createCurrentOnly(node, Properties.INSTANCE.getClientId(), CreateMode.EPHEMERAL); @@ -35,7 +35,7 @@ private boolean contend(final String node, final Provider provider, final Listen /* * listener will be register when the contention of the path is unsuccessful */ - public void executeContention(final String nodeBeCompete, final Provider provider) throws KeeperException, InterruptedException { + public void executeContention(final String nodeBeCompete, final BaseProvider provider) throws KeeperException, InterruptedException { boolean canBegin; String realNode = provider.getRealPath(nodeBeCompete); String contendNode = PathUtil.getRealPath(realNode, Constants.CHANGING_KEY); diff --git a/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java b/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java index 3e392d32d601d..5d35f541b55f1 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java +++ b/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java @@ -1,6 +1,6 @@ package com.saaavsaaa.client.utility.section; -import com.saaavsaaa.client.zookeeper.Provider; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; import org.apache.zookeeper.KeeperException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,13 +10,13 @@ */ public abstract class ClientTask implements Runnable { private static final Logger logger = LoggerFactory.getLogger(ClientTask.class); - private final Provider provider; + private final BaseProvider provider; - public ClientTask(final Provider provider){ + public ClientTask(final BaseProvider provider){ this.provider = provider; } - public abstract void run(final Provider provider) throws KeeperException, InterruptedException; + public abstract void run(final BaseProvider provider) throws KeeperException, InterruptedException; @Override public void run() { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 03a2b64ba5a4c..9934602e8b5f6 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -3,6 +3,8 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -11,18 +13,10 @@ /** * Created by aaa */ -public class ClientFactory { +public class ClientFactory extends BaseClientFactory { // private static final String CLIENT_EXCLUSIVE_NODE = "ZKC"; private static final Logger logger = LoggerFactory.getLogger(ClientFactory.class); - private BaseClient client; - private Listener globalListener; - private String namespace; - private String scheme; - private byte[] auth; - private String servers; - private int sessionTimeoutMilliseconds; - public ClientFactory(){} public ClientFactory newClient(final String servers, final int sessionTimeoutMilliseconds) { @@ -55,17 +49,6 @@ public ClientFactory watch(final Listener listener){ return this; } - public synchronized IClient start() throws IOException, InterruptedException { - client.setClientFactory(this); - client.setRootNode(namespace); - client.setAuthorities(scheme , auth); - client.start(); - if (globalListener != null) { - client.registerWatch(globalListener); - } - return client; - } - public ClientFactory setNamespace(String namespace) { if (!namespace.startsWith(Constants.PATH_SEPARATOR)){ namespace = Constants.PATH_SEPARATOR + namespace; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java new file mode 100644 index 0000000000000..1eef8c16e2020 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java @@ -0,0 +1,16 @@ +package com.saaavsaaa.client.zookeeper; + +import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; +import org.apache.zookeeper.data.ACL; + +import java.util.List; + +/** + * Created by aaa + */ +public class RetryProvider extends BaseProvider { + RetryProvider(String rootNode, BaseClient client, boolean watched, List authorities) { + super(rootNode, client, watched, authorities); + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index ee02c6e6bba29..555496fcd669f 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -1,6 +1,8 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.action.IExecStrategy; +import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import com.saaavsaaa.client.zookeeper.strategy.ContentionStrategy; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; @@ -40,9 +42,9 @@ public synchronized void useExecStrategy(StrategyType strategyType) { return; } if (StrategyType.USUAL == strategyType){ - strategy = new UsualStrategy(new Provider(rootNode, this, watched, authorities)); + strategy = new UsualStrategy(new BaseProvider(rootNode, this, watched, authorities)); } else { - strategy = new ContentionStrategy(new Provider(rootNode, this, watched, authorities)); + strategy = new ContentionStrategy(new BaseProvider(rootNode, this, watched, authorities)); } strategies.put(strategyType, strategy); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java similarity index 96% rename from src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java rename to src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index 4d35810a396b2..e70fa8dc895bd 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.zookeeper; +package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.PathUtil; @@ -7,6 +7,7 @@ import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.utility.section.Properties; import com.saaavsaaa.client.utility.section.WatcherCreator; +import com.saaavsaaa.client.zookeeper.ClientFactory; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; @@ -38,7 +39,7 @@ public abstract class BaseClient implements IClient { protected ZooKeeper zooKeeper; protected List authorities; - private ClientFactory clientFactory; + private BaseClientFactory clientFactory; protected BaseClient(final String servers, final int sessionTimeoutMilliseconds) { this.servers = servers; @@ -175,10 +176,11 @@ void setAuthorities(final String scheme, final byte[] auth) { this.authorities = ZooDefs.Ids.CREATOR_ALL_ACL; } - void setClientFactory(ClientFactory clientFactory) { + void setClientFactory(BaseClientFactory clientFactory) { this.clientFactory = clientFactory; } - ClientFactory getClientFactory() { + + BaseClientFactory getClientFactory() { return clientFactory; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java new file mode 100644 index 0000000000000..99b2dd6ffcdef --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java @@ -0,0 +1,32 @@ +package com.saaavsaaa.client.zookeeper.base; + +import com.saaavsaaa.client.action.IClient; +import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.zookeeper.ClientFactory; + +import java.io.IOException; + +/** + * Created by aaa on 18-5-11. + */ +public abstract class BaseClientFactory { + protected BaseClient client; + protected Listener globalListener; + protected String namespace; + protected String scheme; + protected byte[] auth; + + protected String servers; + protected int sessionTimeoutMilliseconds; + + public synchronized IClient start() throws IOException, InterruptedException { + client.setClientFactory(this); + client.setRootNode(namespace); + client.setAuthorities(scheme , auth); + client.start(); + if (globalListener != null) { + client.registerWatch(globalListener); + } + return client; + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java similarity index 87% rename from src/main/java/com/saaavsaaa/client/zookeeper/Provider.java rename to src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java index 793fb400dbbd5..9912486f50f00 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/Provider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.zookeeper; +package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.election.LeaderElection; @@ -17,15 +17,15 @@ /** * Created by aaa */ -public class Provider implements IProvider { - private static final Logger logger = LoggerFactory.getLogger(Provider.class); +public class BaseProvider implements IProvider { + private static final Logger logger = LoggerFactory.getLogger(BaseProvider.class); protected final BaseClient client; private final ZooKeeper zooKeeper; protected final boolean watched; protected final List authorities; protected final String rootNode; - Provider(final String rootNode, final BaseClient client, final boolean watched, final List authorities){ + public BaseProvider(final String rootNode, final BaseClient client, final boolean watched, final List authorities){ this.rootNode = rootNode; this.client = client; this.zooKeeper = client.getZooKeeper(); @@ -70,12 +70,12 @@ public void createCurrentOnly(final String key, final String value, final Create } try { zooKeeper.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); - logger.debug("Provider createCurrentOnly:{}", key); + logger.debug("BaseProvider createCurrentOnly:{}", key); } catch (KeeperException.NoNodeException e) { - logger.error("Provider createCurrentOnly:{}", e.getMessage(), e); + logger.error("BaseProvider createCurrentOnly:{}", e.getMessage(), e); // I don't know whether it will happen or not, if root watcher don't update rootExist timely if (e.getMessage().contains(key)) { - logger.info("Provider createCurrentOnly rootExist:{}", client.rootExist); + logger.info("BaseProvider createCurrentOnly rootExist:{}", client.rootExist); Thread.sleep(50); this.createCurrentOnly(key, value, createMode); } @@ -86,7 +86,7 @@ public void createCurrentOnly(final String key, final String value, final Create public void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { client.createNamespace(); if (rootNode.equals(key)){ - logger.info("Provider createInTransaction rootNode:{}", key); + logger.info("BaseProvider createInTransaction rootNode:{}", key); return; } transaction.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); @@ -100,7 +100,7 @@ public void update(final String key, final String value) throws KeeperException, @Override public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { zooKeeper.delete(key, Constants.VERSION); - logger.debug("Provider deleteOnlyCurrent:{}", key); + logger.debug("BaseProvider deleteOnlyCurrent:{}", key); if (rootNode.equals(key)){ client.rootExist = false; //protected } @@ -109,7 +109,7 @@ public void deleteOnlyCurrent(final String key) throws KeeperException, Interrup @Override public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { zooKeeper.delete(key, Constants.VERSION, callback, ctx); - logger.debug("Provider deleteOnlyCurrent:{},ctx:{}", key, ctx); + logger.debug("BaseProvider deleteOnlyCurrent:{},ctx:{}", key, ctx); if (rootNode.equals(key)){ client.rootExist = false; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java similarity index 66% rename from src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java rename to src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java index 8032061cf44c4..2c915e60213e3 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/BaseStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java @@ -1,15 +1,14 @@ -package com.saaavsaaa.client.zookeeper.strategy; +package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IExecStrategy; -import com.saaavsaaa.client.zookeeper.Provider; import org.apache.zookeeper.KeeperException; /** * Created by aaa on 18-5-4. */ public abstract class BaseStrategy implements IExecStrategy { - protected final Provider provider; - public BaseStrategy(final Provider provider){ + protected final BaseProvider provider; + public BaseStrategy(final BaseProvider provider){ this.provider = provider; } @@ -18,7 +17,7 @@ public String getDataString(final String key) throws KeeperException, Interrupte return new String(getData(key)); } - public Provider getProvider() { + public BaseProvider getProvider() { return provider; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index 9129bb1c59c9a..0574964549f15 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -4,7 +4,7 @@ import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.Callback; -import com.saaavsaaa.client.zookeeper.Provider; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; @@ -21,7 +21,7 @@ */ public class ContentionStrategy extends UsualStrategy { private static final Logger logger = LoggerFactory.getLogger(ContentionStrategy.class); - public ContentionStrategy(final Provider provider) { + public ContentionStrategy(final BaseProvider provider) { super(provider); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java index f0c50e4306378..9d6a720878310 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java @@ -2,7 +2,8 @@ import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.Provider; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; +import com.saaavsaaa.client.zookeeper.base.BaseStrategy; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; @@ -18,7 +19,7 @@ */ public class UsualStrategy extends BaseStrategy { private static final Logger logger = LoggerFactory.getLogger(UsualStrategy.class); - public UsualStrategy(final Provider provider){ + public UsualStrategy(final BaseProvider provider){ super(provider); } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java index e9a094a41a65a..2444d6c66426d 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java @@ -2,6 +2,8 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.zookeeper.base.BaseClientTest; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.ZooKeeper; import org.junit.Test; @@ -37,7 +39,6 @@ protected IClient createClient(ClientFactory creator) throws IOException, Interr System.out.println("create watch client"); client = newWatchClient(creator); } - zooKeeper = ((BaseClient)client).getZooKeeper(); return client; } @@ -138,7 +139,7 @@ public void watch() throws KeeperException, InterruptedException { @Test public void close() throws Exception { for (IClient client : clients) { - ZooKeeper zk = ((BaseClient)client).getZooKeeper(); + ZooKeeper zk = getZooKeeper(client); client.close(); assert zk.getState() == ZooKeeper.States.CLOSED; } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java index 24d256a696940..732ee22667c5e 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java @@ -3,6 +3,7 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -39,14 +40,14 @@ public void deleteBranch() throws KeeperException, InterruptedException { testClient.deleteCurrentBranch(keyB); // because Constants.CHANGING_KEY, root still exist List children = zooKeeper.getChildren(PathUtil.checkPath(TestSupport.ROOT), false); assert children.size() == 0; - ((BaseClient)testClient).deleteNamespace(); + deleteRoot(testClient); assert zooKeeper.exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; testClient.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; testClient.deleteCurrentBranch(keyB); children = zooKeeper.getChildren(PathUtil.checkPath(TestSupport.ROOT), false); assert children.size() == 0; - ((BaseClient)testClient).deleteNamespace(); + deleteRoot(testClient); assert zooKeeper.exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java b/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java index 43d48d13fb692..1a7d7a31089ac 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java @@ -7,12 +7,12 @@ * Created by aaa */ public class TestSupport { - static final String AUTH = "digest"; - static final String SERVERS = "192.168.2.44:2181"; - static final int SESSION_TIMEOUT = 200000;//ms - static final String ROOT = "test"; + public static final String AUTH = "digest"; + public static final String SERVERS = "192.168.2.44:2181"; + public static final int SESSION_TIMEOUT = 200000;//ms + public static final String ROOT = "test"; - static Listener buildListener(){ + public static Listener buildListener(){ Listener listener = new Listener() { @Override public void process(WatchedEvent event) { diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index 09d9972cceb36..82f61a9438efa 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.action.IClient; +import com.saaavsaaa.client.zookeeper.base.BaseClientTest; import org.apache.zookeeper.KeeperException; import org.junit.Test; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java similarity index 77% rename from src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java rename to src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java index 872cae4e20908..33db8e3d24a9d 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/BaseClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java @@ -1,9 +1,11 @@ -package com.saaavsaaa.client.zookeeper; +package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IClient; +import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.Listener; -import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.zookeeper.ClientFactory; +import com.saaavsaaa.client.zookeeper.TestSupport; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import org.junit.After; @@ -29,7 +31,12 @@ public abstract class BaseClientTest { public void start() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); testClient = createClient(creator); - zooKeeper = ((BaseClient)testClient).getZooKeeper(); + getZooKeeper(testClient); + } + + protected ZooKeeper getZooKeeper(IClient client){ + zooKeeper = ((BaseClient)client).getZooKeeper(); + return zooKeeper; } protected abstract IClient createClient(ClientFactory creator) throws IOException, InterruptedException; @@ -43,41 +50,45 @@ public void stop() throws InterruptedException { @Ignore @Test public void deleteRoot() throws KeeperException, InterruptedException { - ((BaseClient)testClient).deleteNamespace(); - assert zooKeeper.exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; + deleteRoot(testClient); + assert getZooKeeper(testClient).exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; + } + + protected void deleteRoot(IClient client) throws KeeperException, InterruptedException { + ((BaseClient)client).deleteNamespace(); } protected void createRoot(IClient client) throws KeeperException, InterruptedException { ((BaseClient)client).createNamespace(); - assert zooKeeper.exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) != null; + assert getZooKeeper(client).exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) != null; ((BaseClient)client).deleteNamespace(); - assert zooKeeper.exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; + assert getZooKeeper(client).exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; } protected void createChild(IClient client) throws KeeperException, InterruptedException { String key = "a/b/bb"; client.createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); - assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) != null; + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) != null; client.deleteCurrentBranch(key); - assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) == null; + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) == null; } protected void deleteBranch(IClient client) throws KeeperException, InterruptedException { String keyB = "a/b/bb"; client.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); - assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; String keyC = "a/c/cc"; client.createAllNeedPath(keyC, "ccc11", CreateMode.PERSISTENT); - assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; client.deleteCurrentBranch(keyC); - assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) == null; - assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), false) != null; + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) == null; + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), false) != null; client.deleteCurrentBranch(keyB); - assert zooKeeper.exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; + assert getZooKeeper(client).exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; client.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); - assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; client.deleteCurrentBranch(keyB); - assert zooKeeper.exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; + assert getZooKeeper(client).exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; } protected void isExisted(IClient client) throws KeeperException, InterruptedException { @@ -162,16 +173,16 @@ protected void persistEphemeral(IClient client) throws KeeperException, Interrup String key = "a/b/bb"; String value = "b1b"; client.createAllNeedPath(key, value, CreateMode.PERSISTENT); -// assert zooKeeper.exists(PathUtil.getRealPath(ROOT, key), null).getEphemeralOwner() == 0; +// assert getZooKeeper(client).exists(PathUtil.getRealPath(ROOT, key), null).getEphemeralOwner() == 0; Stat stat = new Stat(); - zooKeeper.getData(PathUtil.getRealPath(TestSupport.ROOT, key), false, stat); + getZooKeeper(client).getData(PathUtil.getRealPath(TestSupport.ROOT, key), false, stat); assert stat.getEphemeralOwner() == 0; client.deleteAllChildren(key); assert !isExisted(key, client); client.createAllNeedPath(key, value, CreateMode.EPHEMERAL); - assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, key), null).getEphemeralOwner() != 0; // Ephemeral node connection session id + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key), null).getEphemeralOwner() != 0; // Ephemeral node connection session id client.deleteCurrentBranch(key); } @@ -180,11 +191,11 @@ protected void delAllChildren(IClient client) throws KeeperException, Interrupte client.createAllNeedPath(key, "bb", CreateMode.PERSISTENT); key = "a/c/cc"; client.createAllNeedPath(key, "cc", CreateMode.PERSISTENT); - System.out.println(zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), null).getNumChildren()); // nearest children count - assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) != null; + System.out.println(getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), null).getNumChildren()); // nearest children count + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) != null; client.deleteAllChildren("a"); - assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) == null; - assert zooKeeper.exists("/" + TestSupport.ROOT, false) != null; + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) == null; + assert getZooKeeper(client).exists("/" + TestSupport.ROOT, false) != null; ((BaseClient)client).deleteNamespace(); } @@ -229,7 +240,7 @@ public void process(WatchedEvent event) { case NodeChildrenChanged: { String result; try { - result = new String(zooKeeper.getData(event.getPath(),false, null)); + result = new String(getZooKeeper(client).getData(event.getPath(),false, null)); System.out.println(); } catch (KeeperException e) { result = e.getMessage(); @@ -257,6 +268,6 @@ public void process(WatchedEvent event) { protected void close(IClient client) throws Exception { client.close(); - assert zooKeeper.getState() == ZooKeeper.States.CLOSED; + assert getZooKeeper(client).getState() == ZooKeeper.States.CLOSED; } } From 51487983291c45c23b343252acd7a184f20d0c14 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 11 May 2018 15:14:26 +0800 Subject: [PATCH 093/351] callback --- .../client/action/ICallbackProvider.java | 14 ++ .../strategy/ContentionStrategy.java | 69 +-------- .../strategy/TransactionContendStrategy.java | 136 ++++++++++++++++++ 3 files changed, 152 insertions(+), 67 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/action/ICallbackProvider.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java diff --git a/src/main/java/com/saaavsaaa/client/action/ICallbackProvider.java b/src/main/java/com/saaavsaaa/client/action/ICallbackProvider.java new file mode 100644 index 0000000000000..34b366c234387 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/action/ICallbackProvider.java @@ -0,0 +1,14 @@ +package com.saaavsaaa.client.action; + +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; + +/** + * Created by aaa on 18-5-11. + */ +public interface ICallbackProvider { + void exists(String path, AsyncCallback.StatCallback cb, Object ctx); + void getChildren(String path, AsyncCallback.ChildrenCallback cb, Object ctx); + void createCurrentOnly(final String path, byte data[], CreateMode createMode, AsyncCallback.StringCallback cb, Object ctx); + void update(final String path, byte data[], AsyncCallback.StatCallback cb, Object ctx); +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index 0574964549f15..83e1542cb126b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -135,9 +135,7 @@ private LeaderElection buildCreateAllNeedElection(final String key, final String @Override public void action() throws KeeperException, InterruptedException { logger.debug("ContentionStrategy createAllNeedPath action:{}", key); - ZKTransaction transaction = provider.transaction(); - createBegin(key, value, createMode, transaction); - transaction.commit(); + createBegin(key, value, createMode); } @Override public void callback(){ @@ -148,28 +146,6 @@ public void callback(){ }; } - - private void createBegin(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { - if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - provider.createInTransaction(key, value, createMode, transaction); - return; - } - List nodes = provider.getNecessaryPaths(key); - for (int i = 0; i < nodes.size(); i++) { - if (provider.checkExists(nodes.get(i))){ - logger.info("create node exist:{}", nodes.get(i)); - continue; - } - logger.debug("node not exist and create:", nodes.get(i)); - if (i == nodes.size() - 1){ - provider.createInTransaction(nodes.get(i), value, createMode, transaction); - } else { - provider.createInTransaction(nodes.get(i), Constants.NOTHING_VALUE, createMode, transaction); - } - } - } - - @Deprecated private void createBegin(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ provider.createCurrentOnly(key, value, createMode); @@ -196,30 +172,12 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup provider.executeContention(new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - ZKTransaction transaction = provider.transaction(); - deleteChildren(provider.getRealPath(key), true, transaction); - transaction.commit(); + deleteChildren(provider.getRealPath(key), true); } }); logger.debug("ContentionStrategy deleteAllChildren executeContention"); } - private void deleteChildren(final String key, final boolean deleteCurrentNode, final ZKTransaction transaction) throws KeeperException, InterruptedException { - List children = provider.getChildren(key); - for (int i = 0; i < children.size(); i++) { - String child = PathUtil.getRealPath(key, children.get(i)); - if (!provider.checkExists(child)){ - logger.info("delete not exist:{}", child); - continue; - } - logger.debug("deleteChildren:{}", child); - deleteChildren(child, true, transaction); - } - if (deleteCurrentNode){ - transaction.delete(key, Constants.VERSION); - } - } - private void deleteChildren(final String key, final boolean deleteCurrentNode) throws KeeperException, InterruptedException { List children = provider.getChildren(key); logger.debug("deleteChildren:{}", children); @@ -248,29 +206,6 @@ public void action() throws KeeperException, InterruptedException { logger.debug("ContentionStrategy deleteCurrentBranch executeContention"); } - @Deprecated - private void deleteBranch(String key, final ZKTransaction transaction) throws KeeperException, InterruptedException { - deleteChildren(key, false, transaction); - Stack pathStack = provider.getDeletingPaths(key); - String prePath = key; - while (!pathStack.empty()){ - String node = pathStack.pop(); - // contrast cache - // Performance needs testing - List children = provider.getChildren(node); - boolean canDelete = children.size() == 0 || children.size() == 1; - if (children.size() == 1){ - if (!PathUtil.getRealPath(node, children.get(0)).equals(prePath)){ - canDelete = false; - } - } - if (provider.checkExists(node) && canDelete){ - transaction.delete(node, Constants.VERSION); - } - prePath = node; - } - } - private void deleteBranch(String key) throws KeeperException, InterruptedException { deleteChildren(key, false); Stack pathStack = provider.getDeletingPaths(key); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java new file mode 100644 index 0000000000000..c998792486248 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java @@ -0,0 +1,136 @@ +package com.saaavsaaa.client.zookeeper.strategy; + +import com.saaavsaaa.client.election.LeaderElection; +import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.utility.section.Callback; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; +import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Stack; + +/** + * Created by aaa + * since zookeeper 3.4.0 + */ +public class TransactionContendStrategy extends ContentionStrategy { + private static final Logger logger = LoggerFactory.getLogger(TransactionContendStrategy.class); + public TransactionContendStrategy(BaseProvider provider) { + super(provider); + } + + @Override + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + LeaderElection election = buildCreateAllNeedElection(key, value, createMode, null); + provider.executeContention(election); + logger.debug("ContentionStrategy createAllNeedPath executeContention"); + election.waitDone(); + } + + private LeaderElection buildCreateAllNeedElection(final String key, final String value, final CreateMode createMode, final Callback callback){ + return new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + logger.debug("ContentionStrategy createAllNeedPath action:{}", key); + ZKTransaction transaction = provider.transaction(); + createBegin(key, value, createMode, transaction); + transaction.commit(); + } + @Override + public void callback(){ + if (callback != null) { + callback.processResult(); + } + } + }; + } + + private void createBegin(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { + if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ + provider.createInTransaction(key, value, createMode, transaction); + return; + } + List nodes = provider.getNecessaryPaths(key); + for (int i = 0; i < nodes.size(); i++) { + if (provider.checkExists(nodes.get(i))){ + logger.info("create node exist:{}", nodes.get(i)); + continue; + } + logger.debug("node not exist and create:", nodes.get(i)); + if (i == nodes.size() - 1){ + provider.createInTransaction(nodes.get(i), value, createMode, transaction); + } else { + provider.createInTransaction(nodes.get(i), Constants.NOTHING_VALUE, createMode, transaction); + } + } + } + + @Override + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { + provider.executeContention(new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + ZKTransaction transaction = provider.transaction(); + deleteChildren(provider.getRealPath(key), true, transaction); + transaction.commit(); + } + }); + logger.debug("ContentionStrategy deleteAllChildren executeContention"); + } + + private void deleteChildren(final String key, final boolean deleteCurrentNode, final ZKTransaction transaction) throws KeeperException, InterruptedException { + List children = provider.getChildren(key); + for (int i = 0; i < children.size(); i++) { + String child = PathUtil.getRealPath(key, children.get(i)); + if (!provider.checkExists(child)){ + logger.info("delete not exist:{}", child); + continue; + } + logger.debug("deleteChildren:{}", child); + deleteChildren(child, true, transaction); + } + if (deleteCurrentNode){ + transaction.delete(key, Constants.VERSION); + } + } + + @Override + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + provider.executeContention(new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + ZKTransaction transaction = provider.transaction(); + deleteBranch(provider.getRealPath(key), transaction); + transaction.commit(); + } + }); + logger.debug("ContentionStrategy deleteCurrentBranch executeContention"); + } + + private void deleteBranch(String key, final ZKTransaction transaction) throws KeeperException, InterruptedException { + deleteChildren(key, false, transaction); + Stack pathStack = provider.getDeletingPaths(key); + String prePath = key; + while (!pathStack.empty()){ + String node = pathStack.pop(); + // contrast cache + // Performance needs testing + List children = provider.getChildren(node); + boolean canDelete = children.size() == 0 || children.size() == 1; + if (children.size() == 1){ + if (!PathUtil.getRealPath(node, children.get(0)).equals(prePath)){ + canDelete = false; + } + } + if (provider.checkExists(node) && canDelete){ + transaction.delete(node, Constants.VERSION); + } + prePath = node; + } + } +} From 391aeebf1ef6a40159d9a8535ad420a3ca260858 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 11 May 2018 16:49:13 +0800 Subject: [PATCH 094/351] callback --- .../client/zookeeper/RetryProvider.java | 88 +++++++++++++++++++ .../client/zookeeper/base/BaseProvider.java | 22 ++--- 2 files changed, 99 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java index 1eef8c16e2020..195ffe6f74715 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java @@ -2,7 +2,13 @@ import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; @@ -10,7 +16,89 @@ * Created by aaa */ public class RetryProvider extends BaseProvider { + private static final Logger logger = LoggerFactory.getLogger(RetryProvider.class); + RetryProvider(String rootNode, BaseClient client, boolean watched, List authorities) { super(rootNode, client, watched, authorities); } + + @Override + public byte[] getData(final String key) throws KeeperException, InterruptedException { + try { + return zooKeeper.getData(key, watched, null); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("RetryProvider SessionExpiredException getData:{}", key); + return getData(key); + } + } + + @Override + public boolean checkExists(final String key) throws KeeperException, InterruptedException { + try { + return null != zooKeeper.exists(key, watched); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("RetryProvider SessionExpiredException checkExists:{}", key); + return checkExists(key); + } + } + + @Override + public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { + try { + return null != zooKeeper.exists(key, watcher); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("RetryProvider SessionExpiredException checkExists:{}", key); + return checkExists(key, watcher); + } + } + + @Override + public List getChildren(final String key) throws KeeperException, InterruptedException { + try { + return super.getChildren(key); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("RetryProvider SessionExpiredException getChildren:{}", key); + return getChildren(key); + } + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + try { + super.createCurrentOnly(key, value, createMode); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("RetryProvider SessionExpiredException createCurrentOnly:{}", key); + createCurrentOnly(key, value, createMode); + } + } + + @Override + public void update(final String key, final String value) throws KeeperException, InterruptedException { + try { + super.update(key, value); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("RetryProvider SessionExpiredException update:{}", key); + update(key, value); + } + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + try { + super.deleteOnlyCurrent(key); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("RetryProvider SessionExpiredException deleteOnlyCurrent:{}", key); + deleteOnlyCurrent(key); + } + } + + @Override + public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + try { + super.deleteOnlyCurrent(key, callback, ctx); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("RetryProvider SessionExpiredException deleteOnlyCurrent:{}", key); + deleteOnlyCurrent(key, callback, ctx); + } + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java index 9912486f50f00..dd68ad2bdc1ea 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java @@ -20,7 +20,7 @@ public class BaseProvider implements IProvider { private static final Logger logger = LoggerFactory.getLogger(BaseProvider.class); protected final BaseClient client; - private final ZooKeeper zooKeeper; + protected final ZooKeeper zooKeeper; protected final boolean watched; protected final List authorities; protected final String rootNode; @@ -81,16 +81,6 @@ public void createCurrentOnly(final String key, final String value, final Create } } } - - @Override - public void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { - client.createNamespace(); - if (rootNode.equals(key)){ - logger.info("BaseProvider createInTransaction rootNode:{}", key); - return; - } - transaction.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); - } @Override public void update(final String key, final String value) throws KeeperException, InterruptedException { @@ -147,6 +137,16 @@ public void watch(final String key, final Listener listener) { client.registerWatch(getRealPath(key), listener); } + @Override + public void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { + client.createNamespace(); + if (rootNode.equals(key)){ + logger.info("BaseProvider createInTransaction rootNode:{}", key); + return; + } + transaction.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); + } + @Override public ZKTransaction transaction() { return new ZKTransaction(rootNode, zooKeeper); From f427a349a23bedb33abfe5d0d9036ea5be9868b0 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 11 May 2018 18:10:06 +0800 Subject: [PATCH 095/351] retry --- .../com/saaavsaaa/client/cache/PathTree.java | 6 +++- .../client/utility/section/RetryPolicy.java | 28 +++++++++++++++++++ .../client/zookeeper/UsualClient.java | 2 +- 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/utility/section/RetryPolicy.java diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index bce466f75a026..960a61c4fa340 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -116,7 +116,11 @@ public void process(WatchedEvent event) { case NodeDataChanged: case NodeChildrenChanged: { try { - put(path, provider.getDataString(path)); + String value = Constants.NOTHING_VALUE; + if (!path.equals(getRootNode().getKey())){ + value = provider.getDataString(path); + } + put(path, value); } catch (Exception e) { logger.error("PathTree put error : " + e.getMessage()); } diff --git a/src/main/java/com/saaavsaaa/client/utility/section/RetryPolicy.java b/src/main/java/com/saaavsaaa/client/utility/section/RetryPolicy.java new file mode 100644 index 0000000000000..a8e6b064104de --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/utility/section/RetryPolicy.java @@ -0,0 +1,28 @@ +package com.saaavsaaa.client.utility.section; + +/** + * Created by aaa on 18-5-11. + * todo + */ +public enum RetryPolicy { + INSTANCE; + + private int standCount; + + private static final ThreadLocal countHolder = new ThreadLocal<>(); + private static final ThreadLocal sleepHolder = new ThreadLocal<>(); + + public void start(){ + countHolder.set(standCount); + } + + public boolean ContiueExecute(){ + int count = countHolder.get().intValue() - 1; + if (count < 0){ + countHolder.remove(); + } else { + countHolder.set(count); + } + return count > -1; + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 555496fcd669f..7502de6e55675 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -42,7 +42,7 @@ public synchronized void useExecStrategy(StrategyType strategyType) { return; } if (StrategyType.USUAL == strategyType){ - strategy = new UsualStrategy(new BaseProvider(rootNode, this, watched, authorities)); + strategy = new UsualStrategy(new RetryProvider(rootNode, this, watched, authorities)); } else { strategy = new ContentionStrategy(new BaseProvider(rootNode, this, watched, authorities)); } From 0b931ba2e9bf08825261bc25ae2da6e2627b599f Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 11 May 2018 18:52:36 +0800 Subject: [PATCH 096/351] retry --- .../client/utility/section/RetryPolicy.java | 28 ------------------- .../client/zookeeper/RetryProvider.java | 1 + 2 files changed, 1 insertion(+), 28 deletions(-) delete mode 100644 src/main/java/com/saaavsaaa/client/utility/section/RetryPolicy.java diff --git a/src/main/java/com/saaavsaaa/client/utility/section/RetryPolicy.java b/src/main/java/com/saaavsaaa/client/utility/section/RetryPolicy.java deleted file mode 100644 index a8e6b064104de..0000000000000 --- a/src/main/java/com/saaavsaaa/client/utility/section/RetryPolicy.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.saaavsaaa.client.utility.section; - -/** - * Created by aaa on 18-5-11. - * todo - */ -public enum RetryPolicy { - INSTANCE; - - private int standCount; - - private static final ThreadLocal countHolder = new ThreadLocal<>(); - private static final ThreadLocal sleepHolder = new ThreadLocal<>(); - - public void start(){ - countHolder.set(standCount); - } - - public boolean ContiueExecute(){ - int count = countHolder.get().intValue() - 1; - if (count < 0){ - countHolder.remove(); - } else { - countHolder.set(count); - } - return count > -1; - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java index 195ffe6f74715..f8e6d5185d90b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java @@ -14,6 +14,7 @@ /** * Created by aaa + * todo don't block */ public class RetryProvider extends BaseProvider { private static final Logger logger = LoggerFactory.getLogger(RetryProvider.class); From 213976b313eaf085e294da3c93fccfced5f021e8 Mon Sep 17 00:00:00 2001 From: saaav Date: Sat, 12 May 2018 21:26:58 +0800 Subject: [PATCH 097/351] Update README.md --- README.md | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bd1b7aeffd4cd..2499164b6a23a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,39 @@ # ZookeeperClient -test + +----- + + ClientFactory creator = new ClientFactory(); + IClient testClient = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + + Reference unit test + +----- + +Client: + CacheClient + UsualClient + +ExecStrategy: + public enum StrategyType { + USUAL, + CONTENTION + } + + String getDataString(final String key) throws KeeperException, InterruptedException; + byte[] getData(final String key) throws KeeperException, InterruptedException; + void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException; + boolean checkExists(final String key) throws KeeperException, InterruptedException; + boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException; + List getChildren(final String key) throws KeeperException, InterruptedException; + void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException; + void update(final String key, final String value) throws KeeperException, InterruptedException; + void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException; + void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException; + ZKTransaction transaction(); + void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException ; + void deleteAllChildren(final String key) throws KeeperException, InterruptedException ; + + /* + * delete the current node with force and delete the super node whose only child node is current node recursively + */ + void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException ; From 3c556f94df43d2b8b371d23851aefe766df8b557 Mon Sep 17 00:00:00 2001 From: saaav Date: Sat, 12 May 2018 21:27:27 +0800 Subject: [PATCH 098/351] Update README.md --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2499164b6a23a..56de02ebc698c 100644 --- a/README.md +++ b/README.md @@ -9,15 +9,15 @@ ----- -Client: - CacheClient +Client: + CacheClient UsualClient -ExecStrategy: - public enum StrategyType { - USUAL, - CONTENTION - } +ExecStrategy: + public enum StrategyType { + USUAL, + CONTENTION + } String getDataString(final String key) throws KeeperException, InterruptedException; byte[] getData(final String key) throws KeeperException, InterruptedException; From 2e40eaa536d9370fc911f23cdf5920bf14477f63 Mon Sep 17 00:00:00 2001 From: saaav Date: Sat, 12 May 2018 21:28:04 +0800 Subject: [PATCH 099/351] Update README.md --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 56de02ebc698c..e3c5909b4cf83 100644 --- a/README.md +++ b/README.md @@ -9,15 +9,15 @@ ----- -Client: - CacheClient - UsualClient - -ExecStrategy: - public enum StrategyType { - USUAL, - CONTENTION - } + Client: + CacheClient + UsualClient + + ExecStrategy: + public enum StrategyType { + USUAL, + CONTENTION + } String getDataString(final String key) throws KeeperException, InterruptedException; byte[] getData(final String key) throws KeeperException, InterruptedException; From 128ddf5b04fcdac6a64fd1eb0ac1a3850726ce15 Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 14 May 2018 19:00:15 +0800 Subject: [PATCH 100/351] retry --- .../client/action/ICallbackProvider.java | 2 +- .../saaavsaaa/client/action/IGroupAction.java | 2 +- .../saaavsaaa/client/cache/CacheStrategy.java | 2 +- .../client/election/LeaderElection.java | 11 +++++ .../client/utility/retry/DelayRetrial.java | 29 ++++++++++++ .../client/utility/retry/RetrialCenter.java | 39 ++++++++++++++++ .../client/utility/retry/RetryCount.java | 45 +++++++++++++++++++ .../client/zookeeper/ClientFactory.java | 20 ++++++--- .../client/zookeeper/RetryProvider.java | 42 ++++++++++++++--- .../zookeeper/base/BaseClientFactory.java | 2 +- .../client/zookeeper/base/BaseOperation.java | 38 ++++++++++++++++ .../client/zookeeper/base/BaseProvider.java | 4 ++ .../client/zookeeper/base/BaseStrategy.java | 2 +- .../operation/CreateCurrentOperation.java | 33 ++++++++++++++ .../zookeeper/ContentionStrategyTest.java | 2 +- 15 files changed, 255 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java create mode 100644 src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java create mode 100644 src/main/java/com/saaavsaaa/client/utility/retry/RetryCount.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java diff --git a/src/main/java/com/saaavsaaa/client/action/ICallbackProvider.java b/src/main/java/com/saaavsaaa/client/action/ICallbackProvider.java index 34b366c234387..7d0136d2e4a83 100644 --- a/src/main/java/com/saaavsaaa/client/action/ICallbackProvider.java +++ b/src/main/java/com/saaavsaaa/client/action/ICallbackProvider.java @@ -4,7 +4,7 @@ import org.apache.zookeeper.CreateMode; /** - * Created by aaa on 18-5-11. + * Created by aaa */ public interface ICallbackProvider { void exists(String path, AsyncCallback.StatCallback cb, Object ctx); diff --git a/src/main/java/com/saaavsaaa/client/action/IGroupAction.java b/src/main/java/com/saaavsaaa/client/action/IGroupAction.java index 4d68a5368acd2..3e2a7f6a18227 100644 --- a/src/main/java/com/saaavsaaa/client/action/IGroupAction.java +++ b/src/main/java/com/saaavsaaa/client/action/IGroupAction.java @@ -4,7 +4,7 @@ import org.apache.zookeeper.KeeperException; /** - * Created by aaa on 18-5-10. + * Created by aaa */ public interface IGroupAction { diff --git a/src/main/java/com/saaavsaaa/client/cache/CacheStrategy.java b/src/main/java/com/saaavsaaa/client/cache/CacheStrategy.java index 3a523e1b3e59c..b5d446cc17c60 100644 --- a/src/main/java/com/saaavsaaa/client/cache/CacheStrategy.java +++ b/src/main/java/com/saaavsaaa/client/cache/CacheStrategy.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.cache; /** - * Created by aaa on 18-5-7. + * Created by aaa */ public enum CacheStrategy { NONE, diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index 3f244e7edb952..d19966cd396ba 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -2,6 +2,7 @@ import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.utility.retry.RetryCount; import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.utility.section.Properties; import com.saaavsaaa.client.utility.section.WatcherCreator; @@ -14,10 +15,16 @@ /** * Created by aaa + * It is not recommended to be used as a global variable */ public abstract class LeaderElection { private static final Logger logger = LoggerFactory.getLogger(LeaderElection.class); private boolean done = false; + private int retryCount; + + public LeaderElection(){ + retryCount = RetryCount.INSTANCE.getStandCount(); + } private boolean contend(final String node, final BaseProvider provider, final Listener listener) throws KeeperException, InterruptedException { boolean success = false; @@ -43,6 +50,10 @@ public void executeContention(final String nodeBeCompete, final BaseProvider pro @Override public void process(WatchedEvent event) { try { + retryCount--; + if (retryCount < 0){ + throw new Exception("Election node exceed retry count"); + } executeContention(realNode, provider); } catch (Exception ee){ logger.error("Listener Exception executeContention:{}", ee.getMessage(), ee); diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java new file mode 100644 index 0000000000000..284ad5b142a93 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java @@ -0,0 +1,29 @@ +package com.saaavsaaa.client.utility.retry; + +/** + * Created by aaa + */ +public class DelayRetrial { + + private final int retryCount; + private final long baseDelay; + private final long delayIncrease; + + public DelayRetrial(int retryCount, long baseDelay, long delayIncrease) { + this.retryCount = retryCount; + this.baseDelay = baseDelay; + this.delayIncrease = delayIncrease; + } + + public int getRetryCount() { + return retryCount; + } + + public long getBaseDelay() { + return baseDelay; + } + + public long getDelayIncrease() { + return delayIncrease; + } +} diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java b/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java new file mode 100644 index 0000000000000..3bd5b5343dc37 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java @@ -0,0 +1,39 @@ +package com.saaavsaaa.client.utility.retry; + +import com.saaavsaaa.client.zookeeper.base.BaseOperation; + +import java.util.Comparator; +import java.util.concurrent.PriorityBlockingQueue; + +/** + * Created by aaa + */ +public enum RetrialCenter { + INSTANCE; + + private final int CAPACITY = 100; + private final PriorityBlockingQueue queue = new PriorityBlockingQueue<>(CAPACITY, new OperationComparator()); + private DelayRetrial retrial; + + public void init(DelayRetrial retrial) { + if (retrial == null) { + this.retrial = retrial; + } + } + + public void start(){ + + } + + public void add(BaseOperation operation){ + queue.put(operation); + } +} + +class OperationComparator implements Comparator{ + @Override + public int compare(BaseOperation o1, BaseOperation o2) { + long result = o1.getNextExecuteTick() - o2.getNextExecuteTick(); + return (int)result; + } +} diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/RetryCount.java b/src/main/java/com/saaavsaaa/client/utility/retry/RetryCount.java new file mode 100644 index 0000000000000..dfcc7e3150676 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/utility/retry/RetryCount.java @@ -0,0 +1,45 @@ +package com.saaavsaaa.client.utility.retry; + +/** + * Created by aaa + */ +public enum RetryCount { + INSTANCE; + + private static final int MAX_RETRIES_LIMIT = 10; + private static final ThreadLocal count = new ThreadLocal<>(); + private static final ThreadLocal standCount = new ThreadLocal<>(); + + public void init(final int count){ + standCount.set(count); + } + + public void start(){ + count.set(getStandCount()); + } + + public boolean continueExecute() { + if (count.get() == null){ + start(); + } + int current = count.get().intValue() - 1; + if (current < 0){ + count.remove(); + } else { + count.set(current); + } + return current > -1; + } + + public void reset(){ + start(); + } + + public int getStandCount(){ + Integer sc = standCount.get(); + if (sc == null || sc == 0 || sc > MAX_RETRIES_LIMIT){ + standCount.set(MAX_RETRIES_LIMIT); + } + return standCount.get(); + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 9934602e8b5f6..5067e3b00bdd7 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -1,15 +1,14 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.utility.retry.DelayRetrial; +import com.saaavsaaa.client.utility.retry.RetrialCenter; +import com.saaavsaaa.client.utility.retry.RetryCount; import com.saaavsaaa.client.utility.section.Listener; -import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; - /** * Created by aaa */ @@ -57,7 +56,7 @@ public ClientFactory setNamespace(String namespace) { return this; } - public ClientFactory authorization(String scheme, byte[] auth){ + public ClientFactory authorization(final String scheme, final byte[] auth){ if (scheme == null || scheme.trim().length() == 0) { return this; } @@ -65,4 +64,15 @@ public ClientFactory authorization(String scheme, byte[] auth){ this.auth = auth; return this; } + + public ClientFactory setRetryPolicy(final int count){ + RetryCount.INSTANCE.init(count); + return this; + } + + public ClientFactory setRetryPolicy(final DelayRetrial retrial){ + RetrialCenter.INSTANCE.init(retrial); + RetrialCenter.INSTANCE.start(); + return this; + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java index f8e6d5185d90b..0a780bc14c5e8 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java @@ -1,7 +1,10 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.utility.retry.RetrialCenter; +import com.saaavsaaa.client.utility.retry.RetryCount; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; +import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -14,22 +17,31 @@ /** * Created by aaa - * todo don't block */ public class RetryProvider extends BaseProvider { private static final Logger logger = LoggerFactory.getLogger(RetryProvider.class); + RetryProvider(BaseClient client, boolean watched) { + super(client, watched); + } + RetryProvider(String rootNode, BaseClient client, boolean watched, List authorities) { super(rootNode, client, watched, authorities); + RetryCount.INSTANCE.start(); } - + // block @Override public byte[] getData(final String key) throws KeeperException, InterruptedException { try { return zooKeeper.getData(key, watched, null); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException getData:{}", key); - return getData(key); + if (RetryCount.INSTANCE.continueExecute()) { + byte[] data = getData(key); + RetryCount.INSTANCE.reset(); + return data; + } + throw ee; } } @@ -39,7 +51,12 @@ public boolean checkExists(final String key) throws KeeperException, Interrupted return null != zooKeeper.exists(key, watched); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException checkExists:{}", key); - return checkExists(key); + if (RetryCount.INSTANCE.continueExecute()) { + boolean result = checkExists(key); + RetryCount.INSTANCE.reset(); + return result; + } + throw ee; } } @@ -49,7 +66,12 @@ public boolean checkExists(final String key, final Watcher watcher) throws Keepe return null != zooKeeper.exists(key, watcher); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException checkExists:{}", key); - return checkExists(key, watcher); + if (RetryCount.INSTANCE.continueExecute()) { + boolean result = checkExists(key, watcher); + RetryCount.INSTANCE.reset(); + return result; + } + throw ee; } } @@ -59,17 +81,23 @@ public List getChildren(final String key) throws KeeperException, Interr return super.getChildren(key); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException getChildren:{}", key); - return getChildren(key); + if (RetryCount.INSTANCE.continueExecute()) { + List result = getChildren(key); + RetryCount.INSTANCE.reset(); + return result; + } + throw ee; } } + // without block @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { try { super.createCurrentOnly(key, value, createMode); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException createCurrentOnly:{}", key); - createCurrentOnly(key, value, createMode); + RetrialCenter.INSTANCE.add(new CreateCurrentOperation(this, key, value, createMode)); } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java index 99b2dd6ffcdef..26c806119d7ed 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java @@ -7,7 +7,7 @@ import java.io.IOException; /** - * Created by aaa on 18-5-11. + * Created by aaa */ public abstract class BaseClientFactory { protected BaseClient client; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java new file mode 100644 index 0000000000000..7459aa449345b --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java @@ -0,0 +1,38 @@ +package com.saaavsaaa.client.zookeeper.base; + +import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.utility.retry.DelayRetrial; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.data.ACL; + +import java.util.List; + +/** + * Created by aaa on 18-5-14. + */ +public abstract class BaseOperation { + private final long startTick; + protected final IProvider provider; + + protected int retryCount; + private long baseDelay; + private long delayIncrease; + + protected BaseOperation(final BaseProvider baseProvider) { + this.provider = baseProvider; + this.startTick = System.currentTimeMillis(); + } + + protected abstract boolean execute() throws KeeperException, InterruptedException; + + public void setRetrial(final DelayRetrial retrial){ + this.retryCount = retrial.getRetryCount(); + this.baseDelay = retrial.getBaseDelay(); + this.delayIncrease = retrial.getDelayIncrease(); + } + + public long getNextExecuteTick() { +// nextExecuteTick + return 0; + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java index dd68ad2bdc1ea..0abf7e3a31504 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java @@ -33,6 +33,10 @@ public BaseProvider(final String rootNode, final BaseClient client, final boolea this.authorities = authorities; } + public BaseProvider(BaseClient client, boolean watched) { + this(client.rootNode, client, watched, client.authorities); + } + public String getDataString(final String key) throws KeeperException, InterruptedException { return new String(getData(key)); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java index 2c915e60213e3..199e95fe31188 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java @@ -4,7 +4,7 @@ import org.apache.zookeeper.KeeperException; /** - * Created by aaa on 18-5-4. + * Created by aaa */ public abstract class BaseStrategy implements IExecStrategy { protected final BaseProvider provider; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java new file mode 100644 index 0000000000000..45c0345874c5d --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java @@ -0,0 +1,33 @@ +package com.saaavsaaa.client.zookeeper.operation; + +import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.zookeeper.base.BaseOperation; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; + +/** + * Created by aaa + */ +public class CreateCurrentOperation extends BaseOperation { + private final String key; + private final String value; + private final CreateMode createMode; + + public CreateCurrentOperation(BaseProvider baseProvider, String key, String value, CreateMode createMode) { + super(baseProvider); + this.key = key; + this.value = value; + this.createMode = createMode; + } + + @Override + public boolean execute() throws KeeperException, InterruptedException { + try { + provider.createCurrentOnly(key, value, createMode); + return true; + } catch (KeeperException.SessionExpiredException ee) { + return false; + } + } +} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java index 732ee22667c5e..8a25fe9d76b2c 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java @@ -13,7 +13,7 @@ import java.util.List; /** - * Created by aaa on 18-5-4. + * Created by aaa */ public class ContentionStrategyTest extends UsualClientTest { @Override From a1847404a4d13ac5739421c771b17a0244df39b6 Mon Sep 17 00:00:00 2001 From: WangKai Date: Tue, 15 May 2018 17:25:39 +0800 Subject: [PATCH 101/351] Merge remote-tracking branch 'remotes/upstream/dev' into dev # Conflicts: # sharding-proxy/src/main/java/io/shardingjdbc/proxy/Bootstrap.java # sharding-proxy/src/main/java/io/shardingjdbc/proxy/backend/common/SQLExecuteBackendHandler.java # sharding-proxy/src/main/java/io/shardingjdbc/proxy/config/RuleRegistry.java # sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/ShardingProxy.java # sharding-proxy/src/main/java/io/shardingjdbc/proxy/frontend/mysql/MySQLFrontendHandler.java # sharding-proxy/src/main/java/io/shardingjdbc/proxy/metadata/ProxyShardingMetaData.java # sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/MySQLPacketPayload.java # sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacket.java # sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/CommandPacketFactory.java # sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java # sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java # sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/generic/EofPacket.java # sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/generic/ErrPacket.java # sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/generic/OKPacket.java # sharding-proxy/src/main/java/io/shardingjdbc/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java # sharding-proxy/src/main/resources/conf/config.yaml # sharding-proxy/src/main/resources/logback.xml --- .../io/shardingsphere/proxy/Bootstrap.java | 6 +- .../proxy/backend/ShardingProxyClient.java | 2 +- .../backend/common/BackendHandlerFactory.java | 2 +- .../common/CommandResponsePacketsHandler.java | 2 +- .../common/SQLExecuteBackendHandler.java | 13 +++- .../common/SQLPacketsBackendHandler.java | 31 ++++---- .../backend/mysql/MySQLBackendHandler.java | 2 +- .../netty/ClientHandlerInitializer.java | 2 +- .../proxy/config/RuleRegistry.java | 16 +++- .../proxy/frontend/ShardingProxy.java | 15 +++- .../frontend/mysql/MySQLFrontendHandler.java | 7 +- .../mysql/packet/MySQLPacketPayload.java | 11 +++ .../mysql/packet/command/CommandPacket.java | 10 +++ .../packet/command/CommandPacketFactory.java | 12 +-- .../text/fieldlist/ComFieldListPacket.java | 11 ++- .../command/text/query/ComQueryPacket.java | 22 +++++- .../mysql/packet/generic/EofPacket.java | 5 +- .../mysql/packet/generic/ErrPacket.java | 5 +- .../mysql/packet/generic/OKPacket.java | 5 +- .../handshake/HandshakeResponse41Packet.java | 16 +++- .../proxy/util/MySQLResultCache.java | 6 +- .../proxy/util/SynchronizedFuture.java | 2 +- .../src/main/resources/conf/config.yaml | 74 +++++++++---------- sharding-proxy/src/main/resources/logback.xml | 2 +- 24 files changed, 188 insertions(+), 91 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/Bootstrap.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/Bootstrap.java index 1eaaf62150e1b..a93ce6a32580b 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/Bootstrap.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/Bootstrap.java @@ -20,10 +20,13 @@ import io.shardingsphere.proxy.config.RuleRegistry; import io.shardingsphere.proxy.frontend.ShardingProxy; +import java.net.MalformedURLException; + /** * Sharding-Proxy Bootstrap. * * @author zhangliang + * @author wangkai */ public final class Bootstrap { @@ -34,8 +37,9 @@ public final class Bootstrap { * * @param args startup arguments * @throws InterruptedException interrupted exception + * @throws MalformedURLException URL exception */ - public static void main(final String[] args) throws InterruptedException { + public static void main(final String[] args) throws InterruptedException, MalformedURLException { initializeRuleRegistry(); new ShardingProxy().start(getPort(args)); } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java index a9053c12c4a4b..063bba63a4766 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2015 dangdang.com. + * Copyright 2016-2018 shardingsphere.io. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java index 14f4d687b5034..63f3af1cb2ae2 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2015 dangdang.com. + * Copyright 2016-2018 shardingsphere.io. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java index 8c897173717ef..3d09187d57563 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2015 dangdang.com. + * Copyright 2016-2018 shardingsphere.io. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java index 2234bc43504a0..31bd65993ddde 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java @@ -44,6 +44,7 @@ import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket; import io.shardingsphere.proxy.transport.mysql.packet.generic.ErrPacket; import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket; +import lombok.Getter; import javax.sql.DataSource; import java.sql.Connection; @@ -61,11 +62,13 @@ * * @author zhangliang * @author panjuan + * @author wangkai */ -public final class SQLExecuteBackendHandler implements BackendHandler { +public class SQLExecuteBackendHandler implements BackendHandler { private static final Integer FETCH_ONE_ROW_A_TIME = Integer.MIN_VALUE; + @Getter private final String sql; private List connections; @@ -82,8 +85,10 @@ public final class SQLExecuteBackendHandler implements BackendHandler { private boolean hasMoreResultValueFlag; + @Getter private final DatabaseType databaseType; + @Getter private final boolean showSQL; public SQLExecuteBackendHandler(final String sql, final DatabaseType databaseType, final boolean showSQL) { @@ -118,7 +123,7 @@ private CommandResponsePackets executeForMasterSlave() { return merge(sqlStatement, result); } - private CommandResponsePackets executeForSharding() { + protected CommandResponsePackets executeForSharding() { StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), databaseType, showSQL); SQLRouteResult routeResult = routingEngine.route(sql); if (routeResult.getExecutionUnits().isEmpty()) { @@ -134,7 +139,7 @@ private CommandResponsePackets executeForSharding() { return result; } - private CommandResponsePackets execute(final SQLStatement sqlStatement, final String dataSourceName, final String sql) { + protected CommandResponsePackets execute(final SQLStatement sqlStatement, final String dataSourceName, final String sql) { switch (sqlStatement.getType()) { case DQL: case DAL: @@ -270,7 +275,7 @@ private long getGeneratedKey(final Statement statement) throws SQLException { return result; } - private CommandResponsePackets merge(final SQLStatement sqlStatement, final List packets) { + protected CommandResponsePackets merge(final SQLStatement sqlStatement, final List packets) { CommandResponsePackets headPackets = new CommandResponsePackets(); for (CommandResponsePackets each : packets) { headPackets.addPacket(each.getHeadPacket()); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java index 958c15074a1c8..7f0242a0b99fe 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2015 dangdang.com. + * Copyright 2016-2018 shardingsphere.io. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,18 +18,18 @@ package io.shardingsphere.proxy.backend.common; import io.netty.channel.Channel; -import io.shardingjdbc.core.constant.DatabaseType; -import io.shardingjdbc.core.parsing.parser.sql.SQLStatement; -import io.shardingjdbc.core.routing.SQLExecutionUnit; -import io.shardingjdbc.core.routing.SQLRouteResult; -import io.shardingjdbc.core.routing.StatementRoutingEngine; -import io.shardingjdbc.proxy.backend.ShardingProxyClient; -import io.shardingjdbc.proxy.config.RuleRegistry; -import io.shardingjdbc.proxy.transport.mysql.constant.StatusFlag; -import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; -import io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket; -import io.shardingjdbc.proxy.util.MySQLResultCache; -import io.shardingjdbc.proxy.util.SynchronizedFuture; +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.parsing.parser.sql.SQLStatement; +import io.shardingsphere.core.routing.SQLExecutionUnit; +import io.shardingsphere.core.routing.SQLRouteResult; +import io.shardingsphere.core.routing.StatementRoutingEngine; +import io.shardingsphere.proxy.backend.ShardingProxyClient; +import io.shardingsphere.proxy.config.RuleRegistry; +import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; +import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket; +import io.shardingsphere.proxy.util.MySQLResultCache; +import io.shardingsphere.proxy.util.SynchronizedFuture; import java.util.List; import java.util.concurrent.TimeUnit; @@ -50,9 +50,8 @@ public SQLPacketsBackendHandler(final String sql, final int connectionId, final } @Override - public CommandResponsePackets executeForSharding() { - StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), getDatabaseType(), - isShowSQL()); + protected CommandResponsePackets executeForSharding() { + StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), getDatabaseType(), isShowSQL()); SQLRouteResult routeResult = routingEngine.route(getSql()); if (routeResult.getExecutionUnits().isEmpty()) { return new CommandResponsePackets(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java index 7d93edaa8d5fd..35bb80b1b01a4 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2015 dangdang.com. + * Copyright 2016-2018 shardingsphere.io. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/netty/ClientHandlerInitializer.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/netty/ClientHandlerInitializer.java index 1e92fb71a86dc..c2be33ab127b8 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/netty/ClientHandlerInitializer.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/netty/ClientHandlerInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2015 dangdang.com. + * Copyright 2016-2018 shardingsphere.io. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java index 64cd6debc98a8..1be2219714d80 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java @@ -42,14 +42,18 @@ * @author zhangliang * @author zhangyonglun * @author panjuan + * @author wangkai */ @Getter public final class RuleRegistry { + public static final boolean WITHOUT_JDBC = true; private static final RuleRegistry INSTANCE = new RuleRegistry(); private final Map dataSourceMap; + private Map dataSourceConfigurationMap; + private final ShardingRule shardingRule; private final MasterSlaveRule masterSlaveRule; @@ -65,9 +69,13 @@ private RuleRegistry() { } catch (final IOException ex) { throw new ShardingException(ex); } + dataSourceConfigurationMap = new HashMap<>(128, 1); dataSourceMap = new HashMap<>(128, 1); Map dataSourceParameters = yamlProxyConfiguration.getDataSources(); for (String each : dataSourceParameters.keySet()) { + if (WITHOUT_JDBC) { + dataSourceConfigurationMap.put(each, getDataSourceConfiguration(dataSourceParameters.get(each))); + } dataSourceMap.put(each, getDataSource(dataSourceParameters.get(each))); } shardingRule = yamlProxyConfiguration.obtainShardingRule(Collections.emptyList()); @@ -83,7 +91,7 @@ private RuleRegistry() { } } - private DataSource getDataSource(final DataSourceParameter dataSourceParameter) { + private HikariConfig getDataSourceConfiguration(final DataSourceParameter dataSourceParameter) { HikariConfig config = new HikariConfig(); config.setDriverClassName("com.mysql.jdbc.Driver"); config.setJdbcUrl(dataSourceParameter.getUrl()); @@ -96,7 +104,11 @@ private DataSource getDataSource(final DataSourceParameter dataSourceParameter) config.setMaximumPoolSize(100); config.addDataSourceProperty("useServerPrepStmts", "true"); config.addDataSourceProperty("cachePrepStmts", "true"); - return new HikariDataSource(config); + return config; + } + + private DataSource getDataSource(final DataSourceParameter dataSourceParameter) { + return new HikariDataSource(getDataSourceConfiguration(dataSourceParameter)); } /** diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java index 40225a38a80f4..2cc828636f828 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java @@ -31,12 +31,18 @@ import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; +import io.shardingsphere.proxy.backend.ShardingProxyClient; +import io.shardingsphere.proxy.config.RuleRegistry; +import io.shardingsphere.proxy.frontend.netty.ServerHandlerInitializer; + +import java.net.MalformedURLException; /** * Sharding-Proxy. * * @author zhangliang * @author xiaoyu + * @author wangkai */ public final class ShardingProxy { @@ -53,9 +59,13 @@ public final class ShardingProxy { * * @param port port * @throws InterruptedException interrupted exception + * @throws MalformedURLException URL exception */ - public void start(final int port) throws InterruptedException { + public void start(final int port) throws InterruptedException, MalformedURLException { try { + if (RuleRegistry.WITHOUT_JDBC) { + ShardingProxyClient.getInstance().start(); + } ServerBootstrap bootstrap = new ServerBootstrap(); bossGroup = createEventLoopGroup(); if (bossGroup instanceof EpollEventLoopGroup) { @@ -69,6 +79,9 @@ public void start(final int port) throws InterruptedException { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); userGroup.shutdownGracefully(); + if (RuleRegistry.WITHOUT_JDBC) { + ShardingProxyClient.getInstance().stop(); + } } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java index 6ff1e433ba54a..6d3c2c7690cef 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java @@ -20,6 +20,7 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.EventLoopGroup; +import io.shardingsphere.proxy.util.MySQLResultCache; import io.shardingsphere.proxy.frontend.common.FrontendHandler; import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; @@ -37,6 +38,7 @@ * MySQL frontend handler. * * @author zhangliang + * @author wangkai */ @RequiredArgsConstructor public final class MySQLFrontendHandler extends FrontendHandler { @@ -48,6 +50,8 @@ public final class MySQLFrontendHandler extends FrontendHandler { @Override protected void handshake(final ChannelHandlerContext context) { authPluginData = new AuthPluginData(); + int connectionId = ConnectionIdGenerator.getInstance().nextId(); + MySQLResultCache.getInstance().putConnectionMap(context.channel().id().asShortText(),connectionId); context.writeAndFlush(new HandshakePacket(ConnectionIdGenerator.getInstance().nextId(), authPluginData)); } @@ -72,7 +76,8 @@ public void run() { MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload(message); try { int sequenceId = mysqlPacketPayload.readInt1(); - CommandPacket commandPacket = CommandPacketFactory.getCommandPacket(sequenceId, mysqlPacketPayload); + int connectionId = MySQLResultCache.getInstance().getonnectionMap(context.channel().id().asShortText()); + CommandPacket commandPacket = CommandPacketFactory.getCommandPacket(sequenceId, connectionId, mysqlPacketPayload); for (DatabaseProtocolPacket each : commandPacket.execute().getDatabaseProtocolPackets()) { context.writeAndFlush(each); } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java index 298699a603553..c820c8a239a1b 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java @@ -34,6 +34,7 @@ * * @author zhangliang * @author zhangyonglun + * @author wangkai */ @RequiredArgsConstructor @Getter @@ -261,6 +262,16 @@ public void writeStringFix(final String value) { byteBuf.writeBytes(value.getBytes()); } + /** + * Write variable length bytes to byte buffers. + * @see Native41 + * + * @param value fixed length bytes + */ + public void writeBytes(final byte[] value) { + byteBuf.writeBytes(value); + } + /** * Read variable length string from byte buffers. * @see FixedLengthString diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacket.java index 91b6ea6030edc..f29d84325ff16 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacket.java @@ -19,16 +19,26 @@ import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacket; +import lombok.Getter; /** * Command packet. * * @author zhangliang + * @author wangkai */ public abstract class CommandPacket extends MySQLPacket { + @Getter + private final int connectionId; + + public CommandPacket(final int sequenceId, final int connectionId) { + super(sequenceId); + this.connectionId = connectionId; + } public CommandPacket(final int sequenceId) { super(sequenceId); + this.connectionId = 0; } /** diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacketFactory.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacketFactory.java index 9731b61523f75..3657e151fcfa8 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacketFactory.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacketFactory.java @@ -30,17 +30,19 @@ * Command packet factory. * * @author zhangliang + * @author wangkai */ public final class CommandPacketFactory { /** * Get command Packet. - * - * @param sequenceId sequence ID + * + * @param sequenceId sequence ID + * @param connectionId MYSQL connection id * @param mysqlPacketPayload MySQL packet payload * @return Command packet */ - public static CommandPacket getCommandPacket(final int sequenceId, final MySQLPacketPayload mysqlPacketPayload) { + public static CommandPacket getCommandPacket(final int sequenceId, final int connectionId, final MySQLPacketPayload mysqlPacketPayload) { int commandPacketTypeValue = mysqlPacketPayload.readInt1(); CommandPacketType type = CommandPacketType.valueOf(commandPacketTypeValue); switch (type) { @@ -49,9 +51,9 @@ public static CommandPacket getCommandPacket(final int sequenceId, final MySQLPa case COM_INIT_DB: return new ComInitDbPacket(sequenceId, mysqlPacketPayload); case COM_FIELD_LIST: - return new ComFieldListPacket(sequenceId, mysqlPacketPayload); + return new ComFieldListPacket(sequenceId, connectionId, mysqlPacketPayload); case COM_QUERY: - return new ComQueryPacket(sequenceId, mysqlPacketPayload); + return new ComQueryPacket(sequenceId, connectionId, mysqlPacketPayload); case COM_STMT_PREPARE: return new ComStmtPreparePacket(sequenceId, mysqlPacketPayload); case COM_STMT_EXECUTE: diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java index 0436a78d4bee5..d6763195655b5 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java @@ -17,6 +17,8 @@ package io.shardingsphere.proxy.transport.mysql.packet.command.text.fieldlist; +import io.shardingsphere.proxy.backend.common.SQLPacketsBackendHandler; +import io.shardingsphere.proxy.config.RuleRegistry; import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.constant.ShardingConstant; import io.shardingsphere.proxy.backend.common.SQLExecuteBackendHandler; @@ -32,6 +34,7 @@ * @see COM_FIELD_LIST * * @author zhangliang + * @author wangkai */ @Slf4j public final class ComFieldListPacket extends CommandPacket { @@ -40,7 +43,7 @@ public final class ComFieldListPacket extends CommandPacket { private final String fieldWildcard; - public ComFieldListPacket(final int sequenceId, final MySQLPacketPayload mysqlPacketPayload) { + public ComFieldListPacket(final int sequenceId, final int connectionId, final MySQLPacketPayload mysqlPacketPayload) { super(sequenceId); table = mysqlPacketPayload.readStringNul(); fieldWildcard = mysqlPacketPayload.readStringEOF(); @@ -59,7 +62,11 @@ public CommandResponsePackets execute() { log.debug("field wildcard received for Sharding-Proxy: {}", fieldWildcard); String sql = String.format("SHOW COLUMNS FROM %s FROM %s", table, ShardingConstant.LOGIC_SCHEMA_NAME); // TODO use common database type - return new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, true).execute(); + if (RuleRegistry.WITHOUT_JDBC) { + return new SQLPacketsBackendHandler(sql, getConnectionId(), DatabaseType.MySQL, true).execute(); + } else { + return new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, true).execute(); + } } @Override diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java index c1cdadd8da080..0f8f77c33fe41 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java @@ -17,6 +17,8 @@ package io.shardingsphere.proxy.transport.mysql.packet.command.text.query; +import io.shardingsphere.proxy.backend.common.SQLPacketsBackendHandler; +import io.shardingsphere.proxy.config.RuleRegistry; import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.proxy.backend.common.SQLExecuteBackendHandler; import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; @@ -32,6 +34,7 @@ * @see COM_QUERY * * @author zhangliang + * @author wangkai */ @Slf4j public final class ComQueryPacket extends CommandPacket { @@ -40,10 +43,13 @@ public final class ComQueryPacket extends CommandPacket { private final SQLExecuteBackendHandler sqlExecuteBackendHandler; - public ComQueryPacket(final int sequenceId, final MySQLPacketPayload mysqlPacketPayload) { - super(sequenceId); + private final SQLPacketsBackendHandler sqlPacketsBackendHandler; + + public ComQueryPacket(final int sequenceId, final int connectionId, final MySQLPacketPayload mysqlPacketPayload) { + super(sequenceId, connectionId); sql = mysqlPacketPayload.readStringEOF(); sqlExecuteBackendHandler = new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, true); + sqlPacketsBackendHandler = new SQLPacketsBackendHandler(sql, connectionId, DatabaseType.MySQL, true); } @Override @@ -54,7 +60,11 @@ public void write(final MySQLPacketPayload mysqlPacketPayload) { @Override public CommandResponsePackets execute() { log.debug("COM_QUERY received for Sharding-Proxy: {}", sql); - return sqlExecuteBackendHandler.execute(); + if (RuleRegistry.WITHOUT_JDBC) { + return sqlPacketsBackendHandler.execute(); + } else { + return sqlExecuteBackendHandler.execute(); + } } /** @@ -64,7 +74,11 @@ public CommandResponsePackets execute() { */ public boolean hasMoreResultValue() { try { - return sqlExecuteBackendHandler.hasMoreResultValue(); + if (RuleRegistry.WITHOUT_JDBC) { + return false; + } else { + return sqlExecuteBackendHandler.hasMoreResultValue(); + } } catch (final SQLException ex) { return false; } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/EofPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/EofPacket.java index 8a94cbb05fdaf..63cfcee04119a 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/EofPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/EofPacket.java @@ -26,12 +26,13 @@ * EOF packet protocol. * @see EOF Packet * - * @author zhangliang + * @author zhangliang + * @author wangkai */ @Getter public class EofPacket extends MySQLPacket { - private static final int HEADER = 0xfe; + public static final int HEADER = 0xfe; private final int warnings; diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/ErrPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/ErrPacket.java index 843297557674f..05061fc1021d5 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/ErrPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/ErrPacket.java @@ -26,12 +26,13 @@ * ERR packet protocol. * @see ERR Packet * - * @author zhangliang + * @author zhangliang + * @author wangkai */ @Getter public class ErrPacket extends MySQLPacket { - private static final int HEADER = 0xff; + public static final int HEADER = 0xff; private final int errorCode; diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/OKPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/OKPacket.java index 8ce7b8af4c36c..27a1718e3e431 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/OKPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/OKPacket.java @@ -26,12 +26,13 @@ * OK packet protocol. * @see OK Packet * - * @author zhangliang + * @author zhangliang + * @author wangkai */ @Getter public class OKPacket extends MySQLPacket { - private static final int HEADER = 0x00; + public static final int HEADER = 0x00; private final long affectedRows; diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java index 5f810fd526fc2..6e833edaaa491 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/handshake/HandshakeResponse41Packet.java @@ -27,6 +27,7 @@ * @see HandshakeResponse41 * * @author zhangliang + * @author wangkai */ @Getter public final class HandshakeResponse41Packet extends MySQLPacket { @@ -54,6 +55,17 @@ public HandshakeResponse41Packet(final MySQLPacketPayload mysqlPacketPayload) { readDatabase(mysqlPacketPayload); } + public HandshakeResponse41Packet(final int sequenceId, final int capabilityFlags, final int maxPacketSize, final int characterSet, final String username, final byte[] authResponse, final String + database) { + super(sequenceId); + this.capabilityFlags = capabilityFlags; + this.maxPacketSize = maxPacketSize; + this.characterSet = characterSet; + this.username = username; + this.authResponse = authResponse; + this.database = database; + } + private void readAuthResponse(final MySQLPacketPayload mysqlPacketPayload) { if (0 != (capabilityFlags & CapabilityFlag.CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA.getValue())) { authResponse = mysqlPacketPayload.readStringLenenc().getBytes(); @@ -87,14 +99,14 @@ private void writeAuthResponse(final MySQLPacketPayload mysqlPacketPayload) { mysqlPacketPayload.writeStringLenenc(new String(authResponse)); } else if (0 != (capabilityFlags & CapabilityFlag.CLIENT_SECURE_CONNECTION.getValue())) { mysqlPacketPayload.writeInt1(authResponse.length); - mysqlPacketPayload.writeStringFix(new String(authResponse)); + mysqlPacketPayload.writeBytes(authResponse); } else { mysqlPacketPayload.writeStringNul(new String(authResponse)); } } private void writeDatabase(final MySQLPacketPayload mysqlPacketPayload) { - if (0 != (capabilityFlags & CapabilityFlag.CLIENT_CONNECT_WITH_DB.getValue())) { + if (0 != (capabilityFlags & CapabilityFlag.CLIENT_CONNECT_WITH_DB.getValue())) { mysqlPacketPayload.writeStringNul(database); } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java index a31126496655a..aa9a32b8a5b49 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2015 dangdang.com. + * Copyright 2016-2018 shardingsphere.io. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +19,8 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; -import io.shardingjdbc.proxy.transport.mysql.packet.command.CommandResponsePackets; -import io.shardingjdbc.proxy.util.SynchronizedFuture; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; +import io.shardingsphere.proxy.util.SynchronizedFuture; import java.util.concurrent.TimeUnit; diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/SynchronizedFuture.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/SynchronizedFuture.java index 69859b0b24fbd..cd3631729b15c 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/SynchronizedFuture.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/SynchronizedFuture.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2015 dangdang.com. + * Copyright 2016-2018 shardingsphere.io. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/sharding-proxy/src/main/resources/conf/config.yaml b/sharding-proxy/src/main/resources/conf/config.yaml index dd5bee2a59c39..043d362abda58 100644 --- a/sharding-proxy/src/main/resources/conf/config.yaml +++ b/sharding-proxy/src/main/resources/conf/config.yaml @@ -11,43 +11,43 @@ # The example of Sharding rule. -#dataSources: -# ds_0: -# url: jdbc:mysql://127.0.0.1:3306/ds_0 -# username: root -# password: -# ds_1: -# url: jdbc:mysql://127.0.0.1:3306/ds_1 -# username: root -# password: -# -#shardingRule: -# tables: -# t_order: -# actualDataNodes: ds_${0..1}.t_order_${0..1} -# tableStrategy: -# inline: -# shardingColumn: order_id -# algorithmExpression: t_order_${order_id % 2} -# keyGeneratorColumnName: order_id -# t_order_item: -# actualDataNodes: ds_${0..1}.t_order_item_${0..1} -# tableStrategy: -# inline: -# shardingColumn: order_id -# algorithmExpression: t_order_item_${order_id % 2} -# bindingTables: -# - t_order,t_order_item -# defaultDatabaseStrategy: -# inline: -# shardingColumn: user_id -# algorithmExpression: ds_${user_id % 2} -# defaultTableStrategy: -# none: -# defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator -# -# props: -# sql.show: true +dataSources: + ds_0: + url: jdbc:mysql://10.7.8.175:13306/demo_ds_0 + username: admin + password: 123456 + ds_1: + url: jdbc:mysql://10.7.8.175:13306/demo_ds_1 + username: admin + password: 123456 + +shardingRule: + tables: + t_order: + actualDataNodes: ds_${0..1}.t_order_${0..1} + tableStrategy: + inline: + shardingColumn: order_id + algorithmExpression: t_order_${order_id % 2} + keyGeneratorColumnName: order_id + t_order_item: + actualDataNodes: ds_${0..1}.t_order_item_${0..1} + tableStrategy: + inline: + shardingColumn: order_id + algorithmExpression: t_order_item_${order_id % 2} + bindingTables: + - t_order,t_order_item + defaultDatabaseStrategy: + inline: + shardingColumn: user_id + algorithmExpression: ds_${user_id % 2} + defaultTableStrategy: + none: + defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator + + props: + sql.show: true diff --git a/sharding-proxy/src/main/resources/logback.xml b/sharding-proxy/src/main/resources/logback.xml index 409fd9272c5d7..e98f8bd7fcbc3 100644 --- a/sharding-proxy/src/main/resources/logback.xml +++ b/sharding-proxy/src/main/resources/logback.xml @@ -5,7 +5,7 @@ [%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n - + From 2daac91ccb2dfbd74187b770ee7eede98bebd508 Mon Sep 17 00:00:00 2001 From: saaav Date: Tue, 15 May 2018 18:49:49 +0800 Subject: [PATCH 102/351] retry --- .../client/utility/retry/DelayRetrial.java | 13 +++--- .../utility/retry/DelayRetryExecution.java | 43 +++++++++++++++++++ .../client/utility/retry/RetrialCenter.java | 2 +- .../client/zookeeper/base/BaseOperation.java | 31 ++++++------- 4 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java index 284ad5b142a93..dd57ca8c7c6f2 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java +++ b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java @@ -7,12 +7,15 @@ public class DelayRetrial { private final int retryCount; private final long baseDelay; - private final long delayIncrease; + private final long delayUpperBound; - public DelayRetrial(int retryCount, long baseDelay, long delayIncrease) { + /* + * Millis + */ + public DelayRetrial(int retryCount, long baseDelay, long delayUpperBound) { this.retryCount = retryCount; this.baseDelay = baseDelay; - this.delayIncrease = delayIncrease; + this.delayUpperBound = delayUpperBound; } public int getRetryCount() { @@ -23,7 +26,7 @@ public long getBaseDelay() { return baseDelay; } - public long getDelayIncrease() { - return delayIncrease; + public long getDelayUpperBound() { + return delayUpperBound; } } diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java new file mode 100644 index 0000000000000..997c4fbcbe04c --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java @@ -0,0 +1,43 @@ +package com.saaavsaaa.client.utility.retry; + +import java.util.Random; + +/** + * Created by aaa on 18-5-15. + */ +public class DelayRetryExecution { + private final DelayRetrial delayRetrial; + private final Random random; + + private int executeCount = 0; + private long executeTick; + + public DelayRetryExecution(final DelayRetrial delayRetrial) { + this.delayRetrial = delayRetrial; + this.executeTick = System.currentTimeMillis(); + this.random = new Random(); + } + + public boolean hasNext() { + return executeCount < delayRetrial.getRetryCount(); + } + + public long getNextTick() { + if (!hasNext()){ +// throw new IllegalAccessException("has not next"); + return -1; + } + next(); + return executeTick; + } + + private void next() { + executeCount ++; + long sleep = delayRetrial.getBaseDelay() * Math.max(1, this.random.nextInt(1 << delayRetrial.getRetryCount() + 1)); + if (sleep < delayRetrial.getDelayUpperBound()){ + executeTick += sleep; + } else { + executeTick += delayRetrial.getDelayUpperBound(); + } + } +} diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java b/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java index 3bd5b5343dc37..35264d3f71f93 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java +++ b/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java @@ -11,7 +11,7 @@ public enum RetrialCenter { INSTANCE; - private final int CAPACITY = 100; + private final int CAPACITY = 1000; private final PriorityBlockingQueue queue = new PriorityBlockingQueue<>(CAPACITY, new OperationComparator()); private DelayRetrial retrial; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java index 7459aa449345b..12e87bc58367e 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java @@ -1,38 +1,39 @@ package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.utility.retry.DelayRetrial; +import com.saaavsaaa.client.utility.retry.DelayRetryExecution; import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.data.ACL; - -import java.util.List; /** * Created by aaa on 18-5-14. */ public abstract class BaseOperation { - private final long startTick; protected final IProvider provider; - protected int retryCount; - private long baseDelay; - private long delayIncrease; + protected DelayRetryExecution retryExecution; protected BaseOperation(final BaseProvider baseProvider) { this.provider = baseProvider; - this.startTick = System.currentTimeMillis(); } protected abstract boolean execute() throws KeeperException, InterruptedException; - public void setRetrial(final DelayRetrial retrial){ - this.retryCount = retrial.getRetryCount(); - this.baseDelay = retrial.getBaseDelay(); - this.delayIncrease = retrial.getDelayIncrease(); + /* + * @Return whether or not continue enqueue + */ + public boolean executeOperation() throws KeeperException, InterruptedException { + boolean result = execute(); + if (!result && retryExecution.hasNext()){ + return true; + } + return false; + } + + public void setRetrial(final DelayRetryExecution retryExecution){ + this.retryExecution = retryExecution; } public long getNextExecuteTick() { -// nextExecuteTick - return 0; + return retryExecution.getNextTick(); } } From c9762012102782be5a094836b7a3928ec6db4121 Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 16 May 2018 18:52:46 +0800 Subject: [PATCH 103/351] retry --- .../client/utility/retry/DelayRetrial.java | 7 +++ .../utility/retry/DelayRetryExecution.java | 4 -- .../client/utility/retry/RetrialCenter.java | 59 ++++++++++++++----- .../client/zookeeper/base/BaseOperation.java | 41 +++++++++++-- .../operation/CreateCurrentOperation.java | 1 - 5 files changed, 87 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java index dd57ca8c7c6f2..128053d3c42ce 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java +++ b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java @@ -4,6 +4,9 @@ * Created by aaa */ public class DelayRetrial { + private static final int RETRY_COUNT = 29; + private static final long BASE_DELAY = 1; + private static final long DELAY_UPPER_BOUND = 2147483647; private final int retryCount; private final long baseDelay; @@ -29,4 +32,8 @@ public long getBaseDelay() { public long getDelayUpperBound() { return delayUpperBound; } + + public static DelayRetrial newNoInitDelayRetrial(){ + return new DelayRetrial(RETRY_COUNT, BASE_DELAY, DELAY_UPPER_BOUND); + } } diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java index 997c4fbcbe04c..63d1580422ec3 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java +++ b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java @@ -23,10 +23,6 @@ public boolean hasNext() { } public long getNextTick() { - if (!hasNext()){ -// throw new IllegalAccessException("has not next"); - return -1; - } next(); return executeTick; } diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java b/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java index 35264d3f71f93..6a7abb7b3a333 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java +++ b/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java @@ -1,18 +1,28 @@ package com.saaavsaaa.client.utility.retry; import com.saaavsaaa.client.zookeeper.base.BaseOperation; +import org.apache.zookeeper.KeeperException; -import java.util.Comparator; -import java.util.concurrent.PriorityBlockingQueue; +import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicInteger; /** * Created by aaa */ public enum RetrialCenter { INSTANCE; - - private final int CAPACITY = 1000; - private final PriorityBlockingQueue queue = new PriorityBlockingQueue<>(CAPACITY, new OperationComparator()); + + private static final DelayQueue queue = new DelayQueue<>(); + private static final ThreadPoolExecutor retryExecution = new ThreadPoolExecutor(1, 10, 200, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(10), new ThreadFactory() { + private final AtomicInteger threadIndex = new AtomicInteger(0); + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r); + thread.setDaemon(true); + thread.setName("zk-retry-" + threadIndex.incrementAndGet()); + return thread; + } + }); private DelayRetrial retrial; public void init(DelayRetrial retrial) { @@ -22,18 +32,39 @@ public void init(DelayRetrial retrial) { } public void start(){ - + for (;;) { + BaseOperation op; + try { + op = queue.take(); + } catch (InterruptedException e) { + continue; + } + retryExecution.submit(new Runnable() { + @Override + public void run() { + boolean result; + try { + result = op.executeOperation(); + } catch (Exception e) { + result = false; + } + if (result){ + queue.offer(op); + } + } + }); + } + } + + public void stop(){ + retryExecution.shutdown(); } public void add(BaseOperation operation){ + if (retrial == null){ + retrial = DelayRetrial.newNoInitDelayRetrial(); + } + operation.setRetrial(new DelayRetryExecution(retrial)); queue.put(operation); } } - -class OperationComparator implements Comparator{ - @Override - public int compare(BaseOperation o1, BaseOperation o2) { - long result = o1.getNextExecuteTick() - o2.getNextExecuteTick(); - return (int)result; - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java index 12e87bc58367e..f8e1e7067cc9b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java @@ -1,13 +1,22 @@ package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.utility.retry.DelayRetrial; import com.saaavsaaa.client.utility.retry.DelayRetryExecution; import org.apache.zookeeper.KeeperException; +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; + /** * Created by aaa on 18-5-14. */ -public abstract class BaseOperation { +public abstract class BaseOperation implements Delayed { + @Deprecated + private long delayTick; + @Deprecated + private long executeTick; + protected final IProvider provider; protected DelayRetryExecution retryExecution; @@ -16,10 +25,33 @@ protected BaseOperation(final BaseProvider baseProvider) { this.provider = baseProvider; } + @Deprecated + public void setRetrial(final DelayRetrial delayRetrial){ + this.delayTick = delayRetrial.getBaseDelay(); + this.executeTick = System.currentTimeMillis() + delayTick; + } + + public void setRetrial(final DelayRetryExecution retryExecution){ + this.retryExecution = retryExecution; + } + + @Override + public long getDelay(TimeUnit unit) { + return unit.convert(this.retryExecution.getNextTick() - System.currentTimeMillis() , TimeUnit.MILLISECONDS); + } + + /** + * queue precedence + */ + @Override + public int compareTo(Delayed delayed) { + return (int) (this.getDelay(TimeUnit.MILLISECONDS) - delayed.getDelay(TimeUnit.MILLISECONDS)); + } + protected abstract boolean execute() throws KeeperException, InterruptedException; /* - * @Return whether or not continue enqueue + * @Return whether or not continue enqueue */ public boolean executeOperation() throws KeeperException, InterruptedException { boolean result = execute(); @@ -29,10 +61,7 @@ public boolean executeOperation() throws KeeperException, InterruptedException { return false; } - public void setRetrial(final DelayRetryExecution retryExecution){ - this.retryExecution = retryExecution; - } - + @Deprecated public long getNextExecuteTick() { return retryExecution.getNextTick(); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java index 45c0345874c5d..08e07a643234a 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java @@ -1,6 +1,5 @@ package com.saaavsaaa.client.zookeeper.operation; -import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import com.saaavsaaa.client.zookeeper.base.BaseProvider; import org.apache.zookeeper.CreateMode; From c01877a7766a325c5f84b9de7dc345d456281bdb Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 16 May 2018 18:56:15 +0800 Subject: [PATCH 104/351] retry --- .../client/utility/retry/RetrialCenter.java | 3 +-- .../client/zookeeper/base/BaseOperation.java | 13 ------------- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java b/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java index 6a7abb7b3a333..aed6b73eebf4f 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java +++ b/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java @@ -1,7 +1,6 @@ package com.saaavsaaa.client.utility.retry; import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import org.apache.zookeeper.KeeperException; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; @@ -65,6 +64,6 @@ public void add(BaseOperation operation){ retrial = DelayRetrial.newNoInitDelayRetrial(); } operation.setRetrial(new DelayRetryExecution(retrial)); - queue.put(operation); + queue.offer(operation); } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java index f8e1e7067cc9b..3a7bb1b27a207 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java @@ -1,7 +1,6 @@ package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.utility.retry.DelayRetrial; import com.saaavsaaa.client.utility.retry.DelayRetryExecution; import org.apache.zookeeper.KeeperException; @@ -12,25 +11,13 @@ * Created by aaa on 18-5-14. */ public abstract class BaseOperation implements Delayed { - @Deprecated - private long delayTick; - @Deprecated - private long executeTick; - protected final IProvider provider; - protected DelayRetryExecution retryExecution; protected BaseOperation(final BaseProvider baseProvider) { this.provider = baseProvider; } - @Deprecated - public void setRetrial(final DelayRetrial delayRetrial){ - this.delayTick = delayRetrial.getBaseDelay(); - this.executeTick = System.currentTimeMillis() + delayTick; - } - public void setRetrial(final DelayRetryExecution retryExecution){ this.retryExecution = retryExecution; } From f75185277954b4050dacea5f37fba72961a6fd3a Mon Sep 17 00:00:00 2001 From: WangKai Date: Thu, 17 May 2018 17:43:28 +0800 Subject: [PATCH 105/351] Merge remote-tracking branch 'remotes/upstream/dev' into dev # Conflicts: # sharding-proxy/pom.xml # sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java # sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java --- .../packet/command/text/fieldlist/ComFieldListPacket.java | 7 +++---- .../mysql/packet/command/text/query/ComQueryPacket.java | 1 + 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java index cab9e87031bd1..6dbc45d76e3f4 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java @@ -17,11 +17,10 @@ package io.shardingsphere.proxy.transport.mysql.packet.command.text.fieldlist; -import io.shardingsphere.proxy.backend.common.SQLPacketsBackendHandler; -import io.shardingsphere.proxy.config.RuleRegistry; import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.constant.ShardingConstant; import io.shardingsphere.proxy.backend.common.SQLExecuteBackendHandler; +import io.shardingsphere.proxy.backend.common.SQLPacketsBackendHandler; import io.shardingsphere.proxy.config.RuleRegistry; import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; @@ -32,10 +31,10 @@ /** * COM_FIELD_LIST command packet. - * @see COM_FIELD_LIST * * @author zhangliang * @author wangkai + * @see COM_FIELD_LIST */ @Slf4j public final class ComFieldListPacket extends CommandPacket { @@ -45,7 +44,7 @@ public final class ComFieldListPacket extends CommandPacket { private final String fieldWildcard; public ComFieldListPacket(final int sequenceId, final int connectionId, final MySQLPacketPayload mysqlPacketPayload) { - super(sequenceId); + super(sequenceId, connectionId); table = mysqlPacketPayload.readStringNul(); fieldWildcard = mysqlPacketPayload.readStringEOF(); } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java index 196b95b91ec28..ad05c8ca7a317 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java @@ -19,6 +19,7 @@ import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.proxy.backend.common.SQLExecuteBackendHandler; +import io.shardingsphere.proxy.backend.common.SQLPacketsBackendHandler; import io.shardingsphere.proxy.config.RuleRegistry; import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; From d9a9acf11042225e9c49d8d7d80ea4aaf5b5894e Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 17 May 2018 17:44:36 +0800 Subject: [PATCH 106/351] retry --- .../client/action/IExecStrategy.java | 2 +- .../com/saaavsaaa/client/cache/PathTree.java | 10 +- .../utility/retry/DelayRetryExecution.java | 2 +- .../client/utility/retry/RetrialCenter.java | 77 +++++++++--- .../client/utility/section/ClientTask.java | 8 +- .../client/zookeeper/CacheClient.java | 2 - .../client/zookeeper/UsualClient.java | 31 +++-- .../client/zookeeper/base/BaseOperation.java | 11 +- .../client/zookeeper/base/BaseStrategy.java | 7 +- .../operation/CreateAllNeedOperation.java | 38 ++++++ .../operation/CreateCurrentOperation.java | 11 +- .../operation/DeleteAllChildrenOperation.java | 33 +++++ .../DeleteCurrentBranchOperation.java | 33 +++++ .../operation/DeleteCurrentOperation.java | 31 +++++ .../zookeeper/operation/UpdateOperation.java | 34 +++++ .../{ => provider}/RetryProvider.java | 20 +-- .../zookeeper/strategy/AllRetryStrategy.java | 54 ++++++++ .../strategy/ContentionStrategy.java | 5 +- .../zookeeper/strategy/RetryStrategy.java | 119 ++++++++++++++++++ .../zookeeper/strategy/StrategyType.java | 4 +- .../strategy/TransactionContendStrategy.java | 4 +- .../zookeeper/strategy/UsualStrategy.java | 4 +- .../zookeeper/ContentionStrategyTest.java | 2 +- 23 files changed, 467 insertions(+), 75 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java rename src/main/java/com/saaavsaaa/client/zookeeper/{ => provider}/RetryProvider.java (87%) create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java diff --git a/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java b/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java index 142279b8d2ce5..f2a83f6e8eca2 100644 --- a/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java +++ b/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java @@ -6,5 +6,5 @@ * Created by aaa */ public interface IExecStrategy extends IAction, IGroupAction { - BaseProvider getProvider(); + IProvider getProvider(); } diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index 960a61c4fa340..c92126426141b 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -1,11 +1,11 @@ package com.saaavsaaa.client.cache; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.ClientTask; import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.utility.section.Properties; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.common.PathUtils; @@ -28,10 +28,10 @@ public final class PathTree { private final AtomicReference rootNode = new AtomicReference<>(); private boolean executorStart = false; private ScheduledExecutorService cacheService; - private final BaseProvider provider; + private final IProvider provider; private PathStatus Status; - public PathTree(final String root, final BaseProvider provider) { + public PathTree(final String root, final IProvider provider) { this.rootNode.set(new PathNode(root)); this.Status = PathStatus.RELEASE; this.provider = provider; @@ -61,7 +61,7 @@ public synchronized void loading() throws KeeperException, InterruptedException } } - private void attechIntoNode(final List children, final PathNode pathNode, final BaseProvider provider) throws KeeperException, InterruptedException { + private void attechIntoNode(final List children, final PathNode pathNode, final IProvider provider) throws KeeperException, InterruptedException { logger.debug("attechIntoNode children:{}", children); if (children.isEmpty()){ logger.info("attechIntoNode there are no children"); @@ -88,7 +88,7 @@ public void refreshPeriodic(final long period){ cacheService = Executors.newSingleThreadScheduledExecutor(); cacheService.scheduleAtFixedRate(new ClientTask(provider) { @Override - public void run(BaseProvider provider) throws KeeperException, InterruptedException { + public void run(IProvider provider) throws KeeperException, InterruptedException { logger.debug("cacheService run:{}", getStatus()); if (PathStatus.RELEASE == getStatus()) { loading(); diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java index 63d1580422ec3..f13d8836ae9f5 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java +++ b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java @@ -3,7 +3,7 @@ import java.util.Random; /** - * Created by aaa on 18-5-15. + * Created by aaa */ public class DelayRetryExecution { private final DelayRetrial delayRetrial; diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java b/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java index aed6b73eebf4f..45dbee9ae5e3b 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java +++ b/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java @@ -1,6 +1,8 @@ package com.saaavsaaa.client.utility.retry; import com.saaavsaaa.client.zookeeper.base.BaseOperation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; @@ -11,31 +13,52 @@ public enum RetrialCenter { INSTANCE; - private static final DelayQueue queue = new DelayQueue<>(); - private static final ThreadPoolExecutor retryExecution = new ThreadPoolExecutor(1, 10, 200, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(10), new ThreadFactory() { - private final AtomicInteger threadIndex = new AtomicInteger(0); - @Override - public Thread newThread(Runnable r) { - Thread thread = new Thread(r); - thread.setDaemon(true); - thread.setName("zk-retry-" + threadIndex.incrementAndGet()); - return thread; - } - }); + private static final Logger logger = LoggerFactory.getLogger(RetrialCenter.class); + private final DelayQueue queue = new DelayQueue<>(); + private final ThreadPoolExecutor retryExecution; + private final int corePoolSize = Runtime.getRuntime().availableProcessors(); + private final int maximumPoolSize = corePoolSize; + private final long keepAliveTime = 0; + private final int closeDelay = 60; + + private boolean started = false; private DelayRetrial retrial; + private RetrialCenter(){ + retryExecution = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(10), new ThreadFactory() { + private final AtomicInteger threadIndex = new AtomicInteger(0); + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r); + thread.setDaemon(true); + thread.setName("zk-retry-" + threadIndex.incrementAndGet()); + return thread; + } + }); + addDelayedShutdownHook(retryExecution, closeDelay, TimeUnit.SECONDS); + } + public void init(DelayRetrial retrial) { + logger.debug("retrial init"); if (retrial == null) { + logger.debug("retrial real init"); this.retrial = retrial; } } - public void start(){ + public synchronized void start(){ + if (started){ + return; + } + this.started = true; + logger.debug("RetrialCenter start"); for (;;) { - BaseOperation op; + BaseOperation operation; try { - op = queue.take(); + operation = queue.take(); + logger.debug("take operation:{}", operation.toString()); } catch (InterruptedException e) { + logger.error("retry interrupt e:{}", e.getMessage()); continue; } retryExecution.submit(new Runnable() { @@ -43,27 +66,45 @@ public void start(){ public void run() { boolean result; try { - result = op.executeOperation(); + result = operation.executeOperation(); } catch (Exception e) { result = false; + logger.error("retry disrupt operation:{}, e:{}", operation.toString(), e.getMessage()); } if (result){ - queue.offer(op); + queue.offer(operation); + logger.debug("enqueue again operation:{}", operation.toString()); } } }); } } - public void stop(){ - retryExecution.shutdown(); + final void addDelayedShutdownHook(final ExecutorService service, final long terminationTimeout, final TimeUnit timeUnit) { + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + try { + started = false; + logger.debug("RetrialCenter stop"); + service.shutdown(); + service.awaitTermination(terminationTimeout, timeUnit); + } catch (InterruptedException ignored) { + // We're shutting down anyway, so just ignore. + } + } + }); + thread.setName("retry shutdown hook"); + Runtime.getRuntime().addShutdownHook(thread); } public void add(BaseOperation operation){ if (retrial == null){ + logger.debug("retrial no init"); retrial = DelayRetrial.newNoInitDelayRetrial(); } operation.setRetrial(new DelayRetryExecution(retrial)); queue.offer(operation); + logger.debug("enqueue operation:{}", operation.toString()); } } diff --git a/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java b/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java index 5d35f541b55f1..2c3e975088585 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java +++ b/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java @@ -1,6 +1,6 @@ package com.saaavsaaa.client.utility.section; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; +import com.saaavsaaa.client.action.IProvider; import org.apache.zookeeper.KeeperException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,13 +10,13 @@ */ public abstract class ClientTask implements Runnable { private static final Logger logger = LoggerFactory.getLogger(ClientTask.class); - private final BaseProvider provider; + private final IProvider provider; - public ClientTask(final BaseProvider provider){ + public ClientTask(final IProvider provider){ this.provider = provider; } - public abstract void run(final BaseProvider provider) throws KeeperException, InterruptedException; + public abstract void run(final IProvider provider) throws KeeperException, InterruptedException; @Override public void run() { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index a9ed31e8171f4..b39a0e3f45879 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -3,11 +3,9 @@ import com.saaavsaaa.client.cache.CacheStrategy; import com.saaavsaaa.client.cache.PathTree; import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.section.Listener; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.WatchedEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 7502de6e55675..5ce74c8821193 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -3,9 +3,7 @@ import com.saaavsaaa.client.action.IExecStrategy; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; -import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; -import com.saaavsaaa.client.zookeeper.strategy.ContentionStrategy; -import com.saaavsaaa.client.zookeeper.strategy.StrategyType; +import com.saaavsaaa.client.zookeeper.strategy.*; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.*; import org.slf4j.Logger; @@ -41,11 +39,30 @@ public synchronized void useExecStrategy(StrategyType strategyType) { strategy = strategies.get(strategyType); return; } - if (StrategyType.USUAL == strategyType){ - strategy = new UsualStrategy(new RetryProvider(rootNode, this, watched, authorities)); - } else { - strategy = new ContentionStrategy(new BaseProvider(rootNode, this, watched, authorities)); + + switch (strategyType){ + case USUAL:{ + strategy = new UsualStrategy(new BaseProvider(rootNode, this, watched, authorities)); + break; + } + case CONTEND:{ + strategy = new ContentionStrategy(new BaseProvider(rootNode, this, watched, authorities)); + break; + } + case RETRY:{ + strategy = new RetryStrategy(rootNode, this, watched, authorities); + break; + } + case ALL_RETRY:{ + strategy = new AllRetryStrategy(rootNode, this, watched, authorities); + break; + } + default:{ + strategy = new UsualStrategy(new BaseProvider(rootNode, this, watched, authorities)); + break; + } } + strategies.put(strategyType, strategy); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java index 3a7bb1b27a207..c52c86bbb6519 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java @@ -8,14 +8,14 @@ import java.util.concurrent.TimeUnit; /** - * Created by aaa on 18-5-14. + * Created by aaa */ public abstract class BaseOperation implements Delayed { protected final IProvider provider; protected DelayRetryExecution retryExecution; - protected BaseOperation(final BaseProvider baseProvider) { - this.provider = baseProvider; + protected BaseOperation(final IProvider provider) { + this.provider = provider; } public void setRetrial(final DelayRetryExecution retryExecution){ @@ -47,9 +47,4 @@ public boolean executeOperation() throws KeeperException, InterruptedException { } return false; } - - @Deprecated - public long getNextExecuteTick() { - return retryExecution.getNextTick(); - } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java index 199e95fe31188..89e615c551bb5 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java @@ -1,14 +1,15 @@ package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IExecStrategy; +import com.saaavsaaa.client.action.IProvider; import org.apache.zookeeper.KeeperException; /** * Created by aaa */ public abstract class BaseStrategy implements IExecStrategy { - protected final BaseProvider provider; - public BaseStrategy(final BaseProvider provider){ + protected final IProvider provider; + public BaseStrategy(final IProvider provider){ this.provider = provider; } @@ -17,7 +18,7 @@ public String getDataString(final String key) throws KeeperException, Interrupte return new String(getData(key)); } - public BaseProvider getProvider() { + public IProvider getProvider() { return provider; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java new file mode 100644 index 0000000000000..7341118115d3f --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java @@ -0,0 +1,38 @@ +package com.saaavsaaa.client.zookeeper.operation; + +import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.zookeeper.base.BaseOperation; +import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; + +/** + * Created by aaa + */ +public class CreateAllNeedOperation extends BaseOperation { + private final String key; + private final String value; + private final CreateMode createMode; + + public CreateAllNeedOperation(final IProvider provider, final String key, final String value, final CreateMode createMode) { + super(provider); + this.key = key; + this.value = value; + this.createMode = createMode; + } + + @Override + protected boolean execute() throws KeeperException, InterruptedException { + try { + new UsualStrategy(provider).createAllNeedPath(key, value, createMode); + return true; + } catch (KeeperException.SessionExpiredException ee) { + return false; + } + } + + @Override + public String toString(){ + return String.format("CreateAllNeedOperation key:%s,value:%s,createMode:%s", key, value, createMode.name()); + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java index 08e07a643234a..2957d2b94c411 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.zookeeper.operation; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -13,8 +13,8 @@ public class CreateCurrentOperation extends BaseOperation { private final String value; private final CreateMode createMode; - public CreateCurrentOperation(BaseProvider baseProvider, String key, String value, CreateMode createMode) { - super(baseProvider); + public CreateCurrentOperation(final IProvider provider, final String key, final String value, final CreateMode createMode) { + super(provider); this.key = key; this.value = value; this.createMode = createMode; @@ -29,4 +29,9 @@ public boolean execute() throws KeeperException, InterruptedException { return false; } } + + @Override + public String toString(){ + return String.format("CreateCurrentOperation key:%s,value:%s,createMode:%s", key, value, createMode.name()); + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java new file mode 100644 index 0000000000000..3790ae489f32d --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java @@ -0,0 +1,33 @@ +package com.saaavsaaa.client.zookeeper.operation; + +import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.zookeeper.base.BaseOperation; +import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; +import org.apache.zookeeper.KeeperException; + +/** + * Created by aaa + */ +public class DeleteAllChildrenOperation extends BaseOperation { + private final String key; + + public DeleteAllChildrenOperation(final IProvider provider, final String key) { + super(provider); + this.key = key; + } + + @Override + protected boolean execute() throws KeeperException, InterruptedException { + try { + new UsualStrategy(provider).deleteAllChildren(key); + return true; + } catch (KeeperException.SessionExpiredException ee) { + return false; + } + } + + @Override + public String toString(){ + return String.format("DeleteAllChildrenOperation key:%s", key); + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java new file mode 100644 index 0000000000000..53e5f5e9282bb --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java @@ -0,0 +1,33 @@ +package com.saaavsaaa.client.zookeeper.operation; + +import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.zookeeper.base.BaseOperation; +import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; +import org.apache.zookeeper.KeeperException; + +/** + * Created by aaa + */ +public class DeleteCurrentBranchOperation extends BaseOperation { + private final String key; + + public DeleteCurrentBranchOperation(final IProvider provider, final String key) { + super(provider); + this.key = key; + } + + @Override + protected boolean execute() throws KeeperException, InterruptedException { + try { + new UsualStrategy(provider).deleteCurrentBranch(key); + return true; + } catch (KeeperException.SessionExpiredException ee) { + return false; + } + } + + @Override + public String toString(){ + return String.format("DeleteCurrentBranchOperation key:%s", key); + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java new file mode 100644 index 0000000000000..ebb7d39c42f33 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java @@ -0,0 +1,31 @@ +package com.saaavsaaa.client.zookeeper.operation; + +import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.zookeeper.base.BaseOperation; +import org.apache.zookeeper.KeeperException; + +/** + * Created by aaa + */ +public class DeleteCurrentOperation extends BaseOperation { + private final String key; + public DeleteCurrentOperation(final IProvider provider, final String key) { + super(provider); + this.key = key; + } + + @Override + protected boolean execute() throws KeeperException, InterruptedException { + try { + provider.deleteOnlyCurrent(key); + return true; + } catch (KeeperException.SessionExpiredException ee) { + return false; + } + } + + @Override + public String toString(){ + return String.format("DeleteCurrentOperation key:%s", key); + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java new file mode 100644 index 0000000000000..41aab86ebcc18 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java @@ -0,0 +1,34 @@ +package com.saaavsaaa.client.zookeeper.operation; + +import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.zookeeper.base.BaseOperation; +import org.apache.zookeeper.KeeperException; + +/** + * Created by aaa + */ +public class UpdateOperation extends BaseOperation { + private final String key; + private final String value; + + public UpdateOperation(final IProvider provider, final String key, final String value) { + super(provider); + this.key = key; + this.value = value; + } + + @Override + protected boolean execute() throws KeeperException, InterruptedException { + try { + provider.update(key, value); + return true; + } catch (KeeperException.SessionExpiredException ee) { + return false; + } + } + + @Override + public String toString(){ + return String.format("UpdateOperation key:%s,value:%s", key, value); + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java similarity index 87% rename from src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java rename to src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java index 0a780bc14c5e8..cd08337851230 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/RetryProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java @@ -1,11 +1,12 @@ -package com.saaavsaaa.client.zookeeper; +package com.saaavsaaa.client.zookeeper.provider; import com.saaavsaaa.client.utility.retry.RetrialCenter; import com.saaavsaaa.client.utility.retry.RetryCount; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; -import org.apache.zookeeper.AsyncCallback; +import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentOperation; +import com.saaavsaaa.client.zookeeper.operation.UpdateOperation; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.Watcher; @@ -18,6 +19,7 @@ /** * Created by aaa */ +@Deprecated public class RetryProvider extends BaseProvider { private static final Logger logger = LoggerFactory.getLogger(RetryProvider.class); @@ -107,7 +109,7 @@ public void update(final String key, final String value) throws KeeperException, super.update(key, value); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException update:{}", key); - update(key, value); + RetrialCenter.INSTANCE.add(new UpdateOperation(this, key, value)); } } @@ -117,17 +119,7 @@ public void deleteOnlyCurrent(final String key) throws KeeperException, Interrup super.deleteOnlyCurrent(key); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException deleteOnlyCurrent:{}", key); - deleteOnlyCurrent(key); - } - } - - @Override - public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - try { - super.deleteOnlyCurrent(key, callback, ctx); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryProvider SessionExpiredException deleteOnlyCurrent:{}", key); - deleteOnlyCurrent(key, callback, ctx); + RetrialCenter.INSTANCE.add(new DeleteCurrentOperation(this, key)); } } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java new file mode 100644 index 0000000000000..a3f9b93d4f280 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java @@ -0,0 +1,54 @@ +package com.saaavsaaa.client.zookeeper.strategy; + +import com.saaavsaaa.client.utility.retry.RetrialCenter; +import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.zookeeper.operation.CreateAllNeedOperation; +import com.saaavsaaa.client.zookeeper.operation.DeleteAllChildrenOperation; +import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentBranchOperation; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/** + * Created by aaa + */ +public class AllRetryStrategy extends RetryStrategy { + private static final Logger logger = LoggerFactory.getLogger(AllRetryStrategy.class); + public AllRetryStrategy(String rootNode, BaseClient client, boolean watched, List authorities) { + super(rootNode, client, watched, authorities); + } + + @Override + public void createAllNeedPath(String key, String value, CreateMode createMode) throws KeeperException, InterruptedException { + try { + super.createAllNeedPath(key, value, createMode); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("AllRetryStrategy SessionExpiredException CreateAllNeedOperation:{}", key); + RetrialCenter.INSTANCE.add(new CreateAllNeedOperation(provider, key, value, createMode)); + } + } + + @Override + public void deleteAllChildren(String key) throws KeeperException, InterruptedException { + try { + super.deleteAllChildren(key); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("AllRetryStrategy SessionExpiredException deleteAllChildren:{}", key); + RetrialCenter.INSTANCE.add(new DeleteAllChildrenOperation(provider, key)); + } + } + + @Override + public void deleteCurrentBranch(String key) throws KeeperException, InterruptedException { + try { + super.deleteCurrentBranch(key); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("AllRetryStrategy SessionExpiredException deleteCurrentBranch:{}", key); + RetrialCenter.INSTANCE.add(new DeleteCurrentBranchOperation(provider, key)); + } + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index 83e1542cb126b..789c8fcb97646 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -1,11 +1,10 @@ package com.saaavsaaa.client.zookeeper.strategy; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.Callback; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; -import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -21,7 +20,7 @@ */ public class ContentionStrategy extends UsualStrategy { private static final Logger logger = LoggerFactory.getLogger(ContentionStrategy.class); - public ContentionStrategy(final BaseProvider provider) { + public ContentionStrategy(final IProvider provider) { super(provider); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java new file mode 100644 index 0000000000000..57355d0ab1386 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java @@ -0,0 +1,119 @@ +package com.saaavsaaa.client.zookeeper.strategy; + +import com.saaavsaaa.client.utility.retry.RetrialCenter; +import com.saaavsaaa.client.utility.retry.RetryCount; +import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; +import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; +import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentOperation; +import com.saaavsaaa.client.zookeeper.operation.UpdateOperation; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/** + * Created by aaa + */ +public class RetryStrategy extends UsualStrategy { + private static final Logger logger = LoggerFactory.getLogger(RetryStrategy.class); + + public RetryStrategy(String rootNode, BaseClient client, boolean watched, List authorities){ + super(new BaseProvider(rootNode, client, watched, authorities)); + RetryCount.INSTANCE.start(); + } + + @Override + public byte[] getData(String key) throws KeeperException, InterruptedException { + try { + return provider.getData(key); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("RetryStrategy SessionExpiredException getData:{}", key); + if (RetryCount.INSTANCE.continueExecute()) { + byte[] data = getData(key); + RetryCount.INSTANCE.reset(); + return data; + } + throw ee; + } + } + + @Override + public boolean checkExists(String key) throws KeeperException, InterruptedException { + try { + return provider.checkExists(key); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("RetryStrategy SessionExpiredException checkExists:{}", key); + if (RetryCount.INSTANCE.continueExecute()) { + boolean result = checkExists(key); + RetryCount.INSTANCE.reset(); + return result; + } + throw ee; + } + } + + @Override + public boolean checkExists(String key, Watcher watcher) throws KeeperException, InterruptedException { + try { + return provider.checkExists(key, watcher); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("RetryStrategy SessionExpiredException checkExists:{}", key); + if (RetryCount.INSTANCE.continueExecute()) { + boolean result = checkExists(key, watcher); + RetryCount.INSTANCE.reset(); + return result; + } + throw ee; + } + } + + @Override + public List getChildren(String key) throws KeeperException, InterruptedException { + try { + return provider.getChildren(key); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("RetryStrategy SessionExpiredException getChildren:{}", key); + if (RetryCount.INSTANCE.continueExecute()) { + List result = getChildren(key); + RetryCount.INSTANCE.reset(); + return result; + } + throw ee; + } + } + + @Override + public void createCurrentOnly(String key, String value, CreateMode createMode) throws KeeperException, InterruptedException { + try { + provider.createCurrentOnly(key, value, createMode); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("RetryStrategy SessionExpiredException createCurrentOnly:{}", key); + RetrialCenter.INSTANCE.add(new CreateCurrentOperation(provider, key, value, createMode)); + } + } + + @Override + public void update(String key, String value) throws KeeperException, InterruptedException { + try { + provider.update(key, value); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("RetryStrategy SessionExpiredException update:{}", key); + RetrialCenter.INSTANCE.add(new UpdateOperation(provider, key, value)); + } + } + + @Override + public void deleteOnlyCurrent(String key) throws KeeperException, InterruptedException { + try { + provider.deleteOnlyCurrent(key); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("RetryStrategy SessionExpiredException deleteOnlyCurrent:{}", key); + RetrialCenter.INSTANCE.add(new DeleteCurrentOperation(provider, key)); + } + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java index 0dcacdd4e4712..73cd97946d78f 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java @@ -5,5 +5,7 @@ */ public enum StrategyType { USUAL, - CONTENTION + CONTEND, + RETRY, + ALL_RETRY } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java index c998792486248..4d0bd52377fa8 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java @@ -1,10 +1,10 @@ package com.saaavsaaa.client.zookeeper.strategy; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.utility.section.Callback; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -20,7 +20,7 @@ */ public class TransactionContendStrategy extends ContentionStrategy { private static final Logger logger = LoggerFactory.getLogger(TransactionContendStrategy.class); - public TransactionContendStrategy(BaseProvider provider) { + public TransactionContendStrategy(IProvider provider) { super(provider); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java index 9d6a720878310..e69874bf75c3f 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java @@ -1,8 +1,8 @@ package com.saaavsaaa.client.zookeeper.strategy; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.base.BaseStrategy; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.AsyncCallback; @@ -19,7 +19,7 @@ */ public class UsualStrategy extends BaseStrategy { private static final Logger logger = LoggerFactory.getLogger(UsualStrategy.class); - public UsualStrategy(final BaseProvider provider){ + public UsualStrategy(final IProvider provider){ super(provider); } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java index 8a25fe9d76b2c..7d1c1395e6aca 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java @@ -20,7 +20,7 @@ public class ContentionStrategyTest extends UsualClientTest { protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - ((BaseClient)client).useExecStrategy(StrategyType.CONTENTION); + ((BaseClient)client).useExecStrategy(StrategyType.CONTEND); return client; } From fda6c45cbef2dc14f4e23119c9e181f507e9601a Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 17 May 2018 18:36:55 +0800 Subject: [PATCH 107/351] retry test start --- .../zookeeper/strategy/RetryStrategy.java | 65 ++++++++++--------- .../client/zookeeper/RetryStrategyTest.java | 21 ++++++ 2 files changed, 57 insertions(+), 29 deletions(-) create mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/RetryStrategyTest.java diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java index 57355d0ab1386..9c686566c2af2 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java @@ -22,19 +22,20 @@ public class RetryStrategy extends UsualStrategy { private static final Logger logger = LoggerFactory.getLogger(RetryStrategy.class); - public RetryStrategy(String rootNode, BaseClient client, boolean watched, List authorities){ + public RetryStrategy(final String rootNode, final BaseClient client, final boolean watched, final List authorities){ super(new BaseProvider(rootNode, client, watched, authorities)); RetryCount.INSTANCE.start(); } @Override - public byte[] getData(String key) throws KeeperException, InterruptedException { + public byte[] getData(final String key) throws KeeperException, InterruptedException { + String path = provider.getRealPath(key); try { - return provider.getData(key); + return provider.getData(path); } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryStrategy SessionExpiredException getData:{}", key); + logger.warn("RetryStrategy SessionExpiredException getData:{}", path); if (RetryCount.INSTANCE.continueExecute()) { - byte[] data = getData(key); + byte[] data = getData(path); RetryCount.INSTANCE.reset(); return data; } @@ -43,13 +44,14 @@ public byte[] getData(String key) throws KeeperException, InterruptedException { } @Override - public boolean checkExists(String key) throws KeeperException, InterruptedException { + public boolean checkExists(final String key) throws KeeperException, InterruptedException { + String path = provider.getRealPath(key); try { - return provider.checkExists(key); + return provider.checkExists(path); } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryStrategy SessionExpiredException checkExists:{}", key); + logger.warn("RetryStrategy SessionExpiredException checkExists:{}", path); if (RetryCount.INSTANCE.continueExecute()) { - boolean result = checkExists(key); + boolean result = checkExists(path); RetryCount.INSTANCE.reset(); return result; } @@ -58,13 +60,14 @@ public boolean checkExists(String key) throws KeeperException, InterruptedExcept } @Override - public boolean checkExists(String key, Watcher watcher) throws KeeperException, InterruptedException { + public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { + String path = provider.getRealPath(key); try { - return provider.checkExists(key, watcher); + return provider.checkExists(path, watcher); } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryStrategy SessionExpiredException checkExists:{}", key); + logger.warn("RetryStrategy SessionExpiredException checkExists:{}", path); if (RetryCount.INSTANCE.continueExecute()) { - boolean result = checkExists(key, watcher); + boolean result = checkExists(path, watcher); RetryCount.INSTANCE.reset(); return result; } @@ -73,13 +76,14 @@ public boolean checkExists(String key, Watcher watcher) throws KeeperException, } @Override - public List getChildren(String key) throws KeeperException, InterruptedException { + public List getChildren(final String key) throws KeeperException, InterruptedException { + String path = provider.getRealPath(key); try { - return provider.getChildren(key); + return provider.getChildren(path); } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryStrategy SessionExpiredException getChildren:{}", key); + logger.warn("RetryStrategy SessionExpiredException getChildren:{}", path); if (RetryCount.INSTANCE.continueExecute()) { - List result = getChildren(key); + List result = getChildren(path); RetryCount.INSTANCE.reset(); return result; } @@ -88,32 +92,35 @@ public List getChildren(String key) throws KeeperException, InterruptedE } @Override - public void createCurrentOnly(String key, String value, CreateMode createMode) throws KeeperException, InterruptedException { + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + String path = provider.getRealPath(key); try { - provider.createCurrentOnly(key, value, createMode); + provider.createCurrentOnly(path, value, createMode); } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryStrategy SessionExpiredException createCurrentOnly:{}", key); - RetrialCenter.INSTANCE.add(new CreateCurrentOperation(provider, key, value, createMode)); + logger.warn("RetryStrategy SessionExpiredException createCurrentOnly:{}", path); + RetrialCenter.INSTANCE.add(new CreateCurrentOperation(provider, path, value, createMode)); } } @Override - public void update(String key, String value) throws KeeperException, InterruptedException { + public void update(final String key, final String value) throws KeeperException, InterruptedException { + String path = provider.getRealPath(key); try { - provider.update(key, value); + provider.update(path, value); } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryStrategy SessionExpiredException update:{}", key); - RetrialCenter.INSTANCE.add(new UpdateOperation(provider, key, value)); + logger.warn("RetryStrategy SessionExpiredException update:{}", path); + RetrialCenter.INSTANCE.add(new UpdateOperation(provider, path, value)); } } @Override - public void deleteOnlyCurrent(String key) throws KeeperException, InterruptedException { + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + String path = provider.getRealPath(key); try { - provider.deleteOnlyCurrent(key); + provider.deleteOnlyCurrent(path); } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryStrategy SessionExpiredException deleteOnlyCurrent:{}", key); - RetrialCenter.INSTANCE.add(new DeleteCurrentOperation(provider, key)); + logger.warn("RetryStrategy SessionExpiredException deleteOnlyCurrent:{}", path); + RetrialCenter.INSTANCE.add(new DeleteCurrentOperation(provider, path)); } } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/RetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/RetryStrategyTest.java new file mode 100644 index 0000000000000..3ac9e38307ca0 --- /dev/null +++ b/src/test/java/com/saaavsaaa/client/zookeeper/RetryStrategyTest.java @@ -0,0 +1,21 @@ +package com.saaavsaaa.client.zookeeper; + +import com.saaavsaaa.client.action.IClient; +import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.zookeeper.strategy.StrategyType; + +import java.io.IOException; + +/** + * Created by aaa + */ +public class RetryStrategyTest extends UsualClientTest{ + @Override + protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { + Listener listener = TestSupport.buildListener(); + IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + ((BaseClient)client).useExecStrategy(StrategyType.RETRY); + return client; + } +} From 1eaf1f8f3dca4891699ceff7a9a6a2655f0211a0 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 18 May 2018 12:31:09 +0800 Subject: [PATCH 108/351] test --- .../client/utility/retry/DelayRetrial.java | 3 +- .../client/utility/retry/RetryCount.java | 6 ++-- .../client/retry/RetrialCenterTest.java | 28 +++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/saaavsaaa/client/retry/RetrialCenterTest.java diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java index 128053d3c42ce..351744548eda4 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java +++ b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java @@ -4,7 +4,6 @@ * Created by aaa */ public class DelayRetrial { - private static final int RETRY_COUNT = 29; private static final long BASE_DELAY = 1; private static final long DELAY_UPPER_BOUND = 2147483647; @@ -34,6 +33,6 @@ public long getDelayUpperBound() { } public static DelayRetrial newNoInitDelayRetrial(){ - return new DelayRetrial(RETRY_COUNT, BASE_DELAY, DELAY_UPPER_BOUND); + return new DelayRetrial(RetryCount.INSTANCE.getStandCount(), BASE_DELAY, DELAY_UPPER_BOUND); } } diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/RetryCount.java b/src/main/java/com/saaavsaaa/client/utility/retry/RetryCount.java index dfcc7e3150676..c1314ae76df3a 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/RetryCount.java +++ b/src/main/java/com/saaavsaaa/client/utility/retry/RetryCount.java @@ -6,7 +6,7 @@ public enum RetryCount { INSTANCE; - private static final int MAX_RETRIES_LIMIT = 10; + private static final int RETRY_COUNT_BOUND = 29; private static final ThreadLocal count = new ThreadLocal<>(); private static final ThreadLocal standCount = new ThreadLocal<>(); @@ -37,8 +37,8 @@ public void reset(){ public int getStandCount(){ Integer sc = standCount.get(); - if (sc == null || sc == 0 || sc > MAX_RETRIES_LIMIT){ - standCount.set(MAX_RETRIES_LIMIT); + if (sc == null || sc > RETRY_COUNT_BOUND){ + standCount.set(RETRY_COUNT_BOUND); } return standCount.get(); } diff --git a/src/test/java/com/saaavsaaa/client/retry/RetrialCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/RetrialCenterTest.java new file mode 100644 index 0000000000000..5554846c1151c --- /dev/null +++ b/src/test/java/com/saaavsaaa/client/retry/RetrialCenterTest.java @@ -0,0 +1,28 @@ +package com.saaavsaaa.client.retry; + +import com.saaavsaaa.client.utility.retry.RetrialCenter; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +/** + * Created by aaa + */ +public class RetrialCenterTest { + @Before + public void start(){ + RetrialCenter.INSTANCE.start(); + } + + @After + public void stop(){ + + } + + @Ignore + @Test + public void nothing(){ + + } +} From 31ea8881133979722aa6562f7ac3bf3f2f7e20d9 Mon Sep 17 00:00:00 2001 From: WangKai Date: Fri, 18 May 2018 13:57:42 +0800 Subject: [PATCH 109/351] fix some code style --- .../proxy/backend/mysql/MySQLBackendHandler.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java index 35bb80b1b01a4..41fe251e084d6 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java @@ -133,12 +133,12 @@ protected void executeCommandResponsePackets(final ChannelHandlerContext context } @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { + public void channelInactive(final ChannelHandlerContext ctx) throws Exception { //TODO delete connection map. super.channelInactive(ctx); } - private byte[] securePasswordAuthentication(byte[] password, byte[] authPluginData) { + private byte[] securePasswordAuthentication(final byte[] password, final byte[] authPluginData) { try { MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); byte[] part1 = sha1.digest(password); From 7da164d75b7179ba862c12ba6f69e6b7c744b98d Mon Sep 17 00:00:00 2001 From: WangKai Date: Fri, 18 May 2018 14:14:51 +0800 Subject: [PATCH 110/351] added throw new UnsupportedOperationException() for master-slave model --- .../backend/common/BackendHandlerFactory.java | 3 +-- .../common/SQLExecuteBackendHandler.java | 18 ++++++++---------- .../common/SQLPacketsBackendHandler.java | 5 +++++ .../frontend/mysql/MySQLFrontendHandler.java | 4 ++-- .../proxy/util/MySQLResultCache.java | 6 +++++- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java index 63f3af1cb2ae2..4f0bfe1910f5c 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java @@ -18,7 +18,6 @@ package io.shardingsphere.proxy.backend.common; import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.proxy.backend.common.CommandResponsePacketsHandler; import io.shardingsphere.proxy.backend.mysql.MySQLBackendHandler; /** @@ -40,7 +39,7 @@ public final class BackendHandlerFactory { * @return backend handler instance */ public static CommandResponsePacketsHandler createBackendHandlerInstance(final DatabaseType databaseType, final String ip, final int port, final String database, final String username, final - String password) { + String password) { switch (databaseType) { case MySQL: return new MySQLBackendHandler(ip, port, database, username, password); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java index 509499ae74990..d1320140eb815 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java @@ -19,8 +19,6 @@ import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.exception.ShardingConfigurationException; -import io.shardingsphere.core.exception.ShardingException; import io.shardingsphere.core.merger.MergeEngineFactory; import io.shardingsphere.core.merger.MergedResult; import io.shardingsphere.core.merger.QueryResult; @@ -71,7 +69,7 @@ public class SQLExecuteBackendHandler implements BackendHandler { @Getter private final String sql; - + private List connections; private List resultSets; @@ -115,7 +113,7 @@ public CommandResponsePackets execute() { } } - private CommandResponsePackets executeForMasterSlave() { + protected CommandResponsePackets executeForMasterSlave() { MasterSlaveRouter masterSlaveRouter = new MasterSlaveRouter(RuleRegistry.getInstance().getMasterSlaveRule()); SQLStatement sqlStatement = new SQLJudgeEngine(sql).judge(); String dataSourceName = masterSlaveRouter.route(sqlStatement.getType()).iterator().next(); @@ -169,8 +167,8 @@ private CommandResponsePackets executeQuery(final DataSource dataSource, final S private CommandResponsePackets executeUpdate(final DataSource dataSource, final String sql, final SQLStatement sqlStatement) { try ( - Connection connection = dataSource.getConnection(); - Statement statement = connection.createStatement()) { + Connection connection = dataSource.getConnection(); + Statement statement = connection.createStatement()) { int affectedRows; long lastInsertId = 0; if (sqlStatement instanceof InsertStatement) { @@ -206,8 +204,8 @@ private CommandResponsePackets executeUpdate(final DataSource dataSource, final private CommandResponsePackets executeCommon(final DataSource dataSource, final String sql) { try ( - Connection connection = dataSource.getConnection(); - Statement statement = connection.createStatement()) { + Connection connection = dataSource.getConnection(); + Statement statement = connection.createStatement()) { boolean hasResultSet = statement.execute(sql); if (hasResultSet) { return getCommonDatabaseProtocolPackets(statement.getResultSet()); @@ -252,8 +250,8 @@ private CommandResponsePackets getCommonDatabaseProtocolPackets(final ResultSet result.addPacket(new FieldCountPacket(++currentSequenceId, columnCount)); for (int i = 1; i <= columnCount; i++) { result.addPacket(new ColumnDefinition41Packet(++currentSequenceId, resultSetMetaData.getSchemaName(i), resultSetMetaData.getTableName(i), - resultSetMetaData.getTableName(i), resultSetMetaData.getColumnLabel(i), resultSetMetaData.getColumnName(i), - resultSetMetaData.getColumnDisplaySize(i), ColumnType.valueOfJDBCType(resultSetMetaData.getColumnType(i)), 0)); + resultSetMetaData.getTableName(i), resultSetMetaData.getColumnLabel(i), resultSetMetaData.getColumnName(i), + resultSetMetaData.getColumnDisplaySize(i), ColumnType.valueOfJDBCType(resultSetMetaData.getColumnType(i)), 0)); } result.addPacket(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue())); while (resultSet.next()) { diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java index 7f0242a0b99fe..c768be2ea1026 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java @@ -67,6 +67,11 @@ protected CommandResponsePackets executeForSharding() { return merge(routeResult.getSqlStatement(), result); } + @Override + protected CommandResponsePackets executeForMasterSlave() { + throw new UnsupportedOperationException(); + } + @Override protected CommandResponsePackets execute(final SQLStatement sqlStatement, final String dataSourceName, final String sql) { Channel channel = ShardingProxyClient.getInstance().getChannelMap().get(dataSourceName); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java index 6d3c2c7690cef..020d59b2c1d8c 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java @@ -20,7 +20,6 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.EventLoopGroup; -import io.shardingsphere.proxy.util.MySQLResultCache; import io.shardingsphere.proxy.frontend.common.FrontendHandler; import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; @@ -32,6 +31,7 @@ import io.shardingsphere.proxy.transport.mysql.packet.handshake.ConnectionIdGenerator; import io.shardingsphere.proxy.transport.mysql.packet.handshake.HandshakePacket; import io.shardingsphere.proxy.transport.mysql.packet.handshake.HandshakeResponse41Packet; +import io.shardingsphere.proxy.util.MySQLResultCache; import lombok.RequiredArgsConstructor; /** @@ -51,7 +51,7 @@ public final class MySQLFrontendHandler extends FrontendHandler { protected void handshake(final ChannelHandlerContext context) { authPluginData = new AuthPluginData(); int connectionId = ConnectionIdGenerator.getInstance().nextId(); - MySQLResultCache.getInstance().putConnectionMap(context.channel().id().asShortText(),connectionId); + MySQLResultCache.getInstance().putConnectionMap(context.channel().id().asShortText(), connectionId); context.writeAndFlush(new HandshakePacket(ConnectionIdGenerator.getInstance().nextId(), authPluginData)); } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java index aa9a32b8a5b49..6e39703f99f10 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java @@ -20,7 +20,6 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; -import io.shardingsphere.proxy.util.SynchronizedFuture; import java.util.concurrent.TimeUnit; @@ -41,6 +40,7 @@ public class MySQLResultCache { /** * put synchronizedFuture by connectionId. + * * @param connectionId mysql connection id. * @param synchronizedFuture multiple result set. */ @@ -50,6 +50,7 @@ public void put(final int connectionId, final SynchronizedFuture get(final int connectionId) { /** * delete SynchronizedFuture by connectionId. + * * @param connectionId mysql connection id. */ public void delete(final int connectionId) { @@ -67,6 +69,7 @@ public void delete(final int connectionId) { /** * put connectionId by channelId. + * * @param channelId netty channel id. * @param connectionId mysql connection id. */ @@ -76,6 +79,7 @@ public void putConnectionMap(final String channelId, final int connectionId) { /** * get connectionId by channelId. + * * @param channelId netty channel id. * @return connectionId mysql connection id. */ From 1133e24f35e8aa6ca5a0fde5bedb0b3f4e53070f Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 18 May 2018 15:20:15 +0800 Subject: [PATCH 111/351] test --- .../client/election/LeaderElection.java | 3 +- .../client/zookeeper/UsualClient.java | 6 +-- .../client/zookeeper/base/BaseProvider.java | 11 ++--- .../zookeeper/provider/RetryProvider.java | 2 +- .../zookeeper/strategy/RetryStrategy.java | 2 +- .../client/retry/RetrialCenterTest.java | 40 ++++++++++++++++++- 6 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index d19966cd396ba..fe8e52b978dbf 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -52,7 +52,8 @@ public void process(WatchedEvent event) { try { retryCount--; if (retryCount < 0){ - throw new Exception("Election node exceed retry count"); + logger.info("Election node exceed retry count"); + return; } executeContention(realNode, provider); } catch (Exception ee){ diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 5ce74c8821193..5aba8ddcb893a 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -42,11 +42,11 @@ public synchronized void useExecStrategy(StrategyType strategyType) { switch (strategyType){ case USUAL:{ - strategy = new UsualStrategy(new BaseProvider(rootNode, this, watched, authorities)); + strategy = new UsualStrategy(new BaseProvider(this, watched)); break; } case CONTEND:{ - strategy = new ContentionStrategy(new BaseProvider(rootNode, this, watched, authorities)); + strategy = new ContentionStrategy(new BaseProvider(this, watched)); break; } case RETRY:{ @@ -58,7 +58,7 @@ public synchronized void useExecStrategy(StrategyType strategyType) { break; } default:{ - strategy = new UsualStrategy(new BaseProvider(rootNode, this, watched, authorities)); + strategy = new UsualStrategy(new BaseProvider(this, watched)); break; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java index 0abf7e3a31504..3d31979041a1d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.zookeeper.base; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; @@ -25,7 +26,11 @@ public class BaseProvider implements IProvider { protected final List authorities; protected final String rootNode; - public BaseProvider(final String rootNode, final BaseClient client, final boolean watched, final List authorities){ + public BaseProvider(IClient client, boolean watched) { + this(((BaseClient)client).rootNode, ((BaseClient)client), watched, ((BaseClient)client).authorities); + } + + private BaseProvider(final String rootNode, final BaseClient client, final boolean watched, final List authorities){ this.rootNode = rootNode; this.client = client; this.zooKeeper = client.getZooKeeper(); @@ -33,10 +38,6 @@ public BaseProvider(final String rootNode, final BaseClient client, final boolea this.authorities = authorities; } - public BaseProvider(BaseClient client, boolean watched) { - this(client.rootNode, client, watched, client.authorities); - } - public String getDataString(final String key) throws KeeperException, InterruptedException { return new String(getData(key)); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java index cd08337851230..709b10f324a15 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java @@ -28,7 +28,7 @@ public class RetryProvider extends BaseProvider { } RetryProvider(String rootNode, BaseClient client, boolean watched, List authorities) { - super(rootNode, client, watched, authorities); + super(client, watched); RetryCount.INSTANCE.start(); } // block diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java index 9c686566c2af2..4f6d5a8a9f463 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java @@ -23,7 +23,7 @@ public class RetryStrategy extends UsualStrategy { private static final Logger logger = LoggerFactory.getLogger(RetryStrategy.class); public RetryStrategy(final String rootNode, final BaseClient client, final boolean watched, final List authorities){ - super(new BaseProvider(rootNode, client, watched, authorities)); + super(new BaseProvider(client, watched)); RetryCount.INSTANCE.start(); } diff --git a/src/test/java/com/saaavsaaa/client/retry/RetrialCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/RetrialCenterTest.java index 5554846c1151c..2bb089fdaf573 100644 --- a/src/test/java/com/saaavsaaa/client/retry/RetrialCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/RetrialCenterTest.java @@ -1,20 +1,46 @@ package com.saaavsaaa.client.retry; +import com.saaavsaaa.client.action.IClient; +import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.retry.RetrialCenter; +import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.zookeeper.ClientFactory; +import com.saaavsaaa.client.zookeeper.TestSupport; +import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.zookeeper.base.BaseClientTest; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; +import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; +import com.saaavsaaa.client.zookeeper.strategy.StrategyType; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooKeeper; import org.junit.After; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; +import java.io.IOException; + /** * Created by aaa */ -public class RetrialCenterTest { +public class RetrialCenterTest{ + private IProvider provider; + @Before - public void start(){ + public void start() throws IOException, InterruptedException { + provider = new BaseProvider(createClient(), true); RetrialCenter.INSTANCE.start(); } + protected IClient createClient() throws IOException, InterruptedException { + ClientFactory creator = new ClientFactory(); + Listener listener = TestSupport.buildListener(); + IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + ((BaseClient)client).useExecStrategy(StrategyType.RETRY); + return client; + } + @After public void stop(){ @@ -25,4 +51,14 @@ public void stop(){ public void nothing(){ } + +// @Test + public void create(){ + String key = "a/b/bb"; +// RetrialCenter.INSTANCE.add(new CreateCurrentOperation(provider, path, value, createMode)); + /*client.createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key)*//*"/" + ROOT + "/" + key*//*, false) != null; + client.deleteCurrentBranch(key); + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key)*//*"/" + ROOT + "/" + key*//*, false) == null;*/ + } } From 4265f206893fa4a6087b9d67bb2a2f2dff745d7d Mon Sep 17 00:00:00 2001 From: WangKai Date: Fri, 18 May 2018 16:28:21 +0800 Subject: [PATCH 112/351] fix some code style --- .../proxy/backend/common/BackendHandlerFactory.java | 2 +- .../proxy/backend/common/SQLPacketsBackendHandler.java | 3 ++- .../proxy/backend/common/StatementExecuteBackendHandler.java | 2 -- .../java/io/shardingsphere/proxy/frontend/ShardingProxy.java | 4 +--- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java index 4f0bfe1910f5c..e81cb302c4303 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java @@ -39,7 +39,7 @@ public final class BackendHandlerFactory { * @return backend handler instance */ public static CommandResponsePacketsHandler createBackendHandlerInstance(final DatabaseType databaseType, final String ip, final int port, final String database, final String username, final - String password) { + String password) { switch (databaseType) { case MySQL: return new MySQLBackendHandler(ip, port, database, username, password); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java index c768be2ea1026..408456686288c 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java @@ -51,7 +51,8 @@ public SQLPacketsBackendHandler(final String sql, final int connectionId, final @Override protected CommandResponsePackets executeForSharding() { - StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), getDatabaseType(), isShowSQL()); + StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), getDatabaseType(), + isShowSQL()); SQLRouteResult routeResult = routingEngine.route(getSql()); if (routeResult.getExecutionUnits().isEmpty()) { return new CommandResponsePackets(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java index 43053e1ac77d6..11f69f503da79 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java @@ -19,8 +19,6 @@ import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.exception.ShardingConfigurationException; -import io.shardingsphere.core.exception.ShardingException; import io.shardingsphere.core.merger.MergeEngineFactory; import io.shardingsphere.core.merger.MergedResult; import io.shardingsphere.core.merger.QueryResult; diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java index 2cc828636f828..f2760e204cbd1 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java @@ -17,8 +17,6 @@ package io.shardingsphere.proxy.frontend; -import io.shardingsphere.proxy.frontend.netty.ServerHandlerInitializer; - import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.ChannelFuture; @@ -58,7 +56,7 @@ public final class ShardingProxy { * Start Sharding-Proxy. * * @param port port - * @throws InterruptedException interrupted exception + * @throws InterruptedException interrupted exception * @throws MalformedURLException URL exception */ public void start(final int port) throws InterruptedException, MalformedURLException { From 64fe8a215d1e4588043f394e5fdcbaa738159526 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 18 May 2018 17:58:24 +0800 Subject: [PATCH 113/351] retry center --- .../{DelayRetrial.java => DelayRetry.java} | 15 ++++-- .../utility/retry/DelayRetryExecution.java | 14 +++--- .../client/utility/retry/RetryCenter.java | 48 +++++++++++++++++++ .../client/utility/retry/RetryCount.java | 6 ++- .../{RetrialCenter.java => RetryThread.java} | 47 +++++------------- .../client/zookeeper/ClientFactory.java | 10 ++-- .../client/zookeeper/base/BaseOperation.java | 3 +- .../operation/CreateCurrentOperation.java | 2 +- .../zookeeper/provider/RetryProvider.java | 10 ++-- .../zookeeper/strategy/AllRetryStrategy.java | 8 ++-- .../zookeeper/strategy/RetryStrategy.java | 8 ++-- ...alCenterTest.java => RetryCenterTest.java} | 32 +++++++------ 12 files changed, 121 insertions(+), 82 deletions(-) rename src/main/java/com/saaavsaaa/client/utility/retry/{DelayRetrial.java => DelayRetry.java} (59%) create mode 100644 src/main/java/com/saaavsaaa/client/utility/retry/RetryCenter.java rename src/main/java/com/saaavsaaa/client/utility/retry/{RetrialCenter.java => RetryThread.java} (72%) rename src/test/java/com/saaavsaaa/client/retry/{RetrialCenterTest.java => RetryCenterTest.java} (64%) diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetry.java similarity index 59% rename from src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java rename to src/main/java/com/saaavsaaa/client/utility/retry/DelayRetry.java index 351744548eda4..700177091c462 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetrial.java +++ b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetry.java @@ -3,9 +3,8 @@ /** * Created by aaa */ -public class DelayRetrial { +public class DelayRetry { private static final long BASE_DELAY = 1; - private static final long DELAY_UPPER_BOUND = 2147483647; private final int retryCount; private final long baseDelay; @@ -14,7 +13,13 @@ public class DelayRetrial { /* * Millis */ - public DelayRetrial(int retryCount, long baseDelay, long delayUpperBound) { + public DelayRetry(int retryCount, long baseDelay) { + this.retryCount = retryCount; + this.baseDelay = baseDelay; + this.delayUpperBound = Integer.MAX_VALUE; + } + + public DelayRetry(int retryCount, long baseDelay, long delayUpperBound) { this.retryCount = retryCount; this.baseDelay = baseDelay; this.delayUpperBound = delayUpperBound; @@ -32,7 +37,7 @@ public long getDelayUpperBound() { return delayUpperBound; } - public static DelayRetrial newNoInitDelayRetrial(){ - return new DelayRetrial(RetryCount.INSTANCE.getStandCount(), BASE_DELAY, DELAY_UPPER_BOUND); + public static DelayRetry newNoInitDelayRetrial(){ + return new DelayRetry(RetryCount.INSTANCE.getStandCount(), BASE_DELAY); } } diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java index f13d8836ae9f5..bfea83fddb002 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java +++ b/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java @@ -6,20 +6,20 @@ * Created by aaa */ public class DelayRetryExecution { - private final DelayRetrial delayRetrial; + private final DelayRetry delayRetry; private final Random random; private int executeCount = 0; private long executeTick; - public DelayRetryExecution(final DelayRetrial delayRetrial) { - this.delayRetrial = delayRetrial; + public DelayRetryExecution(final DelayRetry delayRetry) { + this.delayRetry = delayRetry; this.executeTick = System.currentTimeMillis(); this.random = new Random(); } public boolean hasNext() { - return executeCount < delayRetrial.getRetryCount(); + return executeCount < delayRetry.getRetryCount(); } public long getNextTick() { @@ -29,11 +29,11 @@ public long getNextTick() { private void next() { executeCount ++; - long sleep = delayRetrial.getBaseDelay() * Math.max(1, this.random.nextInt(1 << delayRetrial.getRetryCount() + 1)); - if (sleep < delayRetrial.getDelayUpperBound()){ + long sleep = delayRetry.getBaseDelay() * Math.max(1, this.random.nextInt(1 << delayRetry.getRetryCount() + 1)); + if (sleep < delayRetry.getDelayUpperBound()){ executeTick += sleep; } else { - executeTick += delayRetrial.getDelayUpperBound(); + executeTick += delayRetry.getDelayUpperBound(); } } } diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/RetryCenter.java b/src/main/java/com/saaavsaaa/client/utility/retry/RetryCenter.java new file mode 100644 index 0000000000000..6b339a038360e --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/utility/retry/RetryCenter.java @@ -0,0 +1,48 @@ +package com.saaavsaaa.client.utility.retry; + +import com.saaavsaaa.client.zookeeper.base.BaseOperation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.DelayQueue; + +/** + * Created by aaa + */ +public enum RetryCenter { + INSTANCE; + + private static final Logger logger = LoggerFactory.getLogger(RetryCenter.class); + private final DelayQueue queue = new DelayQueue<>(); + private final RetryThread retryThread = new RetryThread(queue); + + private boolean started = false; + private DelayRetry retrial; + + public void init(DelayRetry retrial) { + logger.debug("retrial init"); + if (retrial == null) { + logger.debug("retrial real init"); + this.retrial = retrial; + } + } + + public synchronized void start(){ + if (started){ + return; + } + this.started = true; + retryThread.setName("retry-thread"); + retryThread.start(); + } + + public void add(BaseOperation operation){ + if (retrial == null){ + logger.debug("retrial no init"); + retrial = DelayRetry.newNoInitDelayRetrial(); + } + operation.setRetrial(new DelayRetryExecution(retrial)); + queue.offer(operation); + logger.debug("enqueue operation:{}", operation.toString()); + } +} diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/RetryCount.java b/src/main/java/com/saaavsaaa/client/utility/retry/RetryCount.java index c1314ae76df3a..1b3b7e2f2135a 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/RetryCount.java +++ b/src/main/java/com/saaavsaaa/client/utility/retry/RetryCount.java @@ -37,7 +37,11 @@ public void reset(){ public int getStandCount(){ Integer sc = standCount.get(); - if (sc == null || sc > RETRY_COUNT_BOUND){ + if (sc == null){ + standCount.set(3); + return 3; + } + if (sc > RETRY_COUNT_BOUND){ standCount.set(RETRY_COUNT_BOUND); } return standCount.get(); diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java b/src/main/java/com/saaavsaaa/client/utility/retry/RetryThread.java similarity index 72% rename from src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java rename to src/main/java/com/saaavsaaa/client/utility/retry/RetryThread.java index 45dbee9ae5e3b..94bbbb6983850 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/RetrialCenter.java +++ b/src/main/java/com/saaavsaaa/client/utility/retry/RetryThread.java @@ -10,21 +10,17 @@ /** * Created by aaa */ -public enum RetrialCenter { - INSTANCE; - - private static final Logger logger = LoggerFactory.getLogger(RetrialCenter.class); - private final DelayQueue queue = new DelayQueue<>(); +public class RetryThread extends Thread { + private static final Logger logger = LoggerFactory.getLogger(RetryThread.class); private final ThreadPoolExecutor retryExecution; private final int corePoolSize = Runtime.getRuntime().availableProcessors(); private final int maximumPoolSize = corePoolSize; private final long keepAliveTime = 0; private final int closeDelay = 60; + private final DelayQueue queue; - private boolean started = false; - private DelayRetrial retrial; - - private RetrialCenter(){ + public RetryThread(DelayQueue queue) { + this.queue = queue; retryExecution = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(10), new ThreadFactory() { private final AtomicInteger threadIndex = new AtomicInteger(0); @Override @@ -37,21 +33,10 @@ public Thread newThread(Runnable r) { }); addDelayedShutdownHook(retryExecution, closeDelay, TimeUnit.SECONDS); } - - public void init(DelayRetrial retrial) { - logger.debug("retrial init"); - if (retrial == null) { - logger.debug("retrial real init"); - this.retrial = retrial; - } - } - - public synchronized void start(){ - if (started){ - return; - } - this.started = true; - logger.debug("RetrialCenter start"); + + @Override + public void run(){ + logger.debug("RetryThread start"); for (;;) { BaseOperation operation; try { @@ -85,8 +70,8 @@ final void addDelayedShutdownHook(final ExecutorService service, final long term @Override public void run() { try { - started = false; - logger.debug("RetrialCenter stop"); + logger.debug("RetryCenter stop"); + queue.clear(); service.shutdown(); service.awaitTermination(terminationTimeout, timeUnit); } catch (InterruptedException ignored) { @@ -97,14 +82,4 @@ public void run() { thread.setName("retry shutdown hook"); Runtime.getRuntime().addShutdownHook(thread); } - - public void add(BaseOperation operation){ - if (retrial == null){ - logger.debug("retrial no init"); - retrial = DelayRetrial.newNoInitDelayRetrial(); - } - operation.setRetrial(new DelayRetryExecution(retrial)); - queue.offer(operation); - logger.debug("enqueue operation:{}", operation.toString()); - } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 5067e3b00bdd7..a7614a59aeed3 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -1,8 +1,8 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.utility.retry.DelayRetrial; -import com.saaavsaaa.client.utility.retry.RetrialCenter; +import com.saaavsaaa.client.utility.retry.DelayRetry; +import com.saaavsaaa.client.utility.retry.RetryCenter; import com.saaavsaaa.client.utility.retry.RetryCount; import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; @@ -70,9 +70,9 @@ public ClientFactory setRetryPolicy(final int count){ return this; } - public ClientFactory setRetryPolicy(final DelayRetrial retrial){ - RetrialCenter.INSTANCE.init(retrial); - RetrialCenter.INSTANCE.start(); + public ClientFactory setRetryPolicy(final DelayRetry retrial){ + RetryCenter.INSTANCE.init(retrial); + RetryCenter.INSTANCE.start(); return this; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java index c52c86bbb6519..444b2b24898d9 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java @@ -24,7 +24,8 @@ public void setRetrial(final DelayRetryExecution retryExecution){ @Override public long getDelay(TimeUnit unit) { - return unit.convert(this.retryExecution.getNextTick() - System.currentTimeMillis() , TimeUnit.MILLISECONDS); + long result = unit.convert(this.retryExecution.getNextTick() - System.currentTimeMillis() , TimeUnit.MILLISECONDS); + return result; } /** diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java index 2957d2b94c411..abc67468ed00a 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java @@ -15,7 +15,7 @@ public class CreateCurrentOperation extends BaseOperation { public CreateCurrentOperation(final IProvider provider, final String key, final String value, final CreateMode createMode) { super(provider); - this.key = key; + this.key = provider.getRealPath(key); this.value = value; this.createMode = createMode; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java index 709b10f324a15..410e15adbe6e0 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java @@ -1,6 +1,6 @@ package com.saaavsaaa.client.zookeeper.provider; -import com.saaavsaaa.client.utility.retry.RetrialCenter; +import com.saaavsaaa.client.utility.retry.RetryCenter; import com.saaavsaaa.client.utility.retry.RetryCount; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; @@ -99,7 +99,9 @@ public void createCurrentOnly(final String key, final String value, final Create super.createCurrentOnly(key, value, createMode); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException createCurrentOnly:{}", key); - RetrialCenter.INSTANCE.add(new CreateCurrentOperation(this, key, value, createMode)); + RetryCenter.INSTANCE.add(new CreateCurrentOperation(this, key, value, createMode)); + } catch (KeeperException.ConnectionLossException ee){ + } } @@ -109,7 +111,7 @@ public void update(final String key, final String value) throws KeeperException, super.update(key, value); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException update:{}", key); - RetrialCenter.INSTANCE.add(new UpdateOperation(this, key, value)); + RetryCenter.INSTANCE.add(new UpdateOperation(this, key, value)); } } @@ -119,7 +121,7 @@ public void deleteOnlyCurrent(final String key) throws KeeperException, Interrup super.deleteOnlyCurrent(key); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException deleteOnlyCurrent:{}", key); - RetrialCenter.INSTANCE.add(new DeleteCurrentOperation(this, key)); + RetryCenter.INSTANCE.add(new DeleteCurrentOperation(this, key)); } } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java index a3f9b93d4f280..938da65bd72a6 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java @@ -1,6 +1,6 @@ package com.saaavsaaa.client.zookeeper.strategy; -import com.saaavsaaa.client.utility.retry.RetrialCenter; +import com.saaavsaaa.client.utility.retry.RetryCenter; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.operation.CreateAllNeedOperation; import com.saaavsaaa.client.zookeeper.operation.DeleteAllChildrenOperation; @@ -28,7 +28,7 @@ public void createAllNeedPath(String key, String value, CreateMode createMode) t super.createAllNeedPath(key, value, createMode); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AllRetryStrategy SessionExpiredException CreateAllNeedOperation:{}", key); - RetrialCenter.INSTANCE.add(new CreateAllNeedOperation(provider, key, value, createMode)); + RetryCenter.INSTANCE.add(new CreateAllNeedOperation(provider, key, value, createMode)); } } @@ -38,7 +38,7 @@ public void deleteAllChildren(String key) throws KeeperException, InterruptedExc super.deleteAllChildren(key); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AllRetryStrategy SessionExpiredException deleteAllChildren:{}", key); - RetrialCenter.INSTANCE.add(new DeleteAllChildrenOperation(provider, key)); + RetryCenter.INSTANCE.add(new DeleteAllChildrenOperation(provider, key)); } } @@ -48,7 +48,7 @@ public void deleteCurrentBranch(String key) throws KeeperException, InterruptedE super.deleteCurrentBranch(key); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AllRetryStrategy SessionExpiredException deleteCurrentBranch:{}", key); - RetrialCenter.INSTANCE.add(new DeleteCurrentBranchOperation(provider, key)); + RetryCenter.INSTANCE.add(new DeleteCurrentBranchOperation(provider, key)); } } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java index 4f6d5a8a9f463..70d6099306ec2 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java @@ -1,6 +1,6 @@ package com.saaavsaaa.client.zookeeper.strategy; -import com.saaavsaaa.client.utility.retry.RetrialCenter; +import com.saaavsaaa.client.utility.retry.RetryCenter; import com.saaavsaaa.client.utility.retry.RetryCount; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; @@ -98,7 +98,7 @@ public void createCurrentOnly(final String key, final String value, final Create provider.createCurrentOnly(path, value, createMode); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryStrategy SessionExpiredException createCurrentOnly:{}", path); - RetrialCenter.INSTANCE.add(new CreateCurrentOperation(provider, path, value, createMode)); + RetryCenter.INSTANCE.add(new CreateCurrentOperation(provider, path, value, createMode)); } } @@ -109,7 +109,7 @@ public void update(final String key, final String value) throws KeeperException, provider.update(path, value); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryStrategy SessionExpiredException update:{}", path); - RetrialCenter.INSTANCE.add(new UpdateOperation(provider, path, value)); + RetryCenter.INSTANCE.add(new UpdateOperation(provider, path, value)); } } @@ -120,7 +120,7 @@ public void deleteOnlyCurrent(final String key) throws KeeperException, Interrup provider.deleteOnlyCurrent(path); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryStrategy SessionExpiredException deleteOnlyCurrent:{}", path); - RetrialCenter.INSTANCE.add(new DeleteCurrentOperation(provider, path)); + RetryCenter.INSTANCE.add(new DeleteCurrentOperation(provider, path)); } } } diff --git a/src/test/java/com/saaavsaaa/client/retry/RetrialCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/RetryCenterTest.java similarity index 64% rename from src/test/java/com/saaavsaaa/client/retry/RetrialCenterTest.java rename to src/test/java/com/saaavsaaa/client/retry/RetryCenterTest.java index 2bb089fdaf573..ce5e9aee3eb0d 100644 --- a/src/test/java/com/saaavsaaa/client/retry/RetrialCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/RetryCenterTest.java @@ -3,17 +3,17 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.retry.RetrialCenter; +import com.saaavsaaa.client.utility.retry.DelayRetry; +import com.saaavsaaa.client.utility.retry.RetryCenter; import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.zookeeper.ClientFactory; import com.saaavsaaa.client.zookeeper.TestSupport; import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.base.BaseClientTest; import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.KeeperException; import org.junit.After; import org.junit.Before; import org.junit.Ignore; @@ -24,13 +24,14 @@ /** * Created by aaa */ -public class RetrialCenterTest{ +public class RetryCenterTest { private IProvider provider; @Before public void start() throws IOException, InterruptedException { - provider = new BaseProvider(createClient(), true); - RetrialCenter.INSTANCE.start(); + provider = new BaseProvider(createClient(), false); + RetryCenter.INSTANCE.init(new DelayRetry(1, 1)); + RetryCenter.INSTANCE.start(); } protected IClient createClient() throws IOException, InterruptedException { @@ -52,13 +53,16 @@ public void nothing(){ } -// @Test - public void create(){ - String key = "a/b/bb"; -// RetrialCenter.INSTANCE.add(new CreateCurrentOperation(provider, path, value, createMode)); - /*client.createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key)*//*"/" + ROOT + "/" + key*//*, false) != null; - client.deleteCurrentBranch(key); - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key)*//*"/" + ROOT + "/" + key*//*, false) == null;*/ + @Test + public void create() throws InterruptedException, KeeperException { + String key = "a"; + String value = "bbb11"; + Thread.sleep(1000); + RetryCenter.INSTANCE.add(new CreateCurrentOperation(provider, key, value, CreateMode.PERSISTENT)); + Thread.sleep(1000); + String path = PathUtil.getRealPath(TestSupport.ROOT, key); + assert provider.checkExists(path); + provider.deleteOnlyCurrent(path); + assert !provider.checkExists(path); } } From 29bbd2a8e468824ed72e72738978eb438c5434d6 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 18 May 2018 18:16:55 +0800 Subject: [PATCH 114/351] connection check --- src/main/java/com/saaavsaaa/client/action/IClient.java | 4 +--- src/main/java/com/saaavsaaa/client/action/IProvider.java | 2 +- src/main/java/com/saaavsaaa/client/cache/PathTree.java | 6 +++--- .../com/saaavsaaa/client/election/LeaderElection.java | 8 ++++---- .../saaavsaaa/client/{utility => }/retry/DelayRetry.java | 2 +- .../client/{utility => }/retry/DelayRetryExecution.java | 2 +- .../saaavsaaa/client/{utility => }/retry/RetryCenter.java | 2 +- .../saaavsaaa/client/{utility => }/retry/RetryCount.java | 2 +- .../saaavsaaa/client/{utility => }/retry/RetryThread.java | 2 +- .../saaavsaaa/client/{utility => }/section/Callback.java | 2 +- .../client/{utility => }/section/ClientTask.java | 2 +- .../saaavsaaa/client/{utility => }/section/Listener.java | 2 +- .../client/{utility => }/section/Properties.java | 2 +- .../client/{utility => }/section/WatcherCreator.java | 2 +- .../java/com/saaavsaaa/client/section/ZKConnection.java | 8 ++++++++ .../com/saaavsaaa/client/zookeeper/ClientFactory.java | 8 ++++---- .../com/saaavsaaa/client/zookeeper/base/BaseClient.java | 7 +++---- .../client/zookeeper/base/BaseClientFactory.java | 3 +-- .../saaavsaaa/client/zookeeper/base/BaseOperation.java | 2 +- .../com/saaavsaaa/client/zookeeper/base/BaseProvider.java | 2 +- .../client/zookeeper/provider/RetryProvider.java | 4 ++-- .../client/zookeeper/strategy/AllRetryStrategy.java | 2 +- .../client/zookeeper/strategy/ContentionStrategy.java | 2 +- .../client/zookeeper/strategy/RetryStrategy.java | 4 ++-- .../zookeeper/strategy/TransactionContendStrategy.java | 2 +- .../java/com/saaavsaaa/client/retry/RetryCenterTest.java | 4 +--- .../saaavsaaa/client/zookeeper/CacheWathClientTest.java | 2 +- .../java/com/saaavsaaa/client/zookeeper/ClientsTest.java | 3 +-- .../client/zookeeper/ContentionStrategyTest.java | 2 +- .../com/saaavsaaa/client/zookeeper/RetryStrategyTest.java | 2 +- .../java/com/saaavsaaa/client/zookeeper/TestSupport.java | 2 +- .../saaavsaaa/client/zookeeper/UsualWatchClientTest.java | 2 +- .../saaavsaaa/client/zookeeper/base/BaseClientTest.java | 2 +- 33 files changed, 52 insertions(+), 51 deletions(-) rename src/main/java/com/saaavsaaa/client/{utility => }/retry/DelayRetry.java (95%) rename src/main/java/com/saaavsaaa/client/{utility => }/retry/DelayRetryExecution.java (95%) rename src/main/java/com/saaavsaaa/client/{utility => }/retry/RetryCenter.java (96%) rename src/main/java/com/saaavsaaa/client/{utility => }/retry/RetryCount.java (96%) rename src/main/java/com/saaavsaaa/client/{utility => }/retry/RetryThread.java (98%) rename src/main/java/com/saaavsaaa/client/{utility => }/section/Callback.java (64%) rename src/main/java/com/saaavsaaa/client/{utility => }/section/ClientTask.java (94%) rename src/main/java/com/saaavsaaa/client/{utility => }/section/Listener.java (75%) rename src/main/java/com/saaavsaaa/client/{utility => }/section/Properties.java (96%) rename src/main/java/com/saaavsaaa/client/{utility => }/section/WatcherCreator.java (94%) create mode 100644 src/main/java/com/saaavsaaa/client/section/ZKConnection.java diff --git a/src/main/java/com/saaavsaaa/client/action/IClient.java b/src/main/java/com/saaavsaaa/client/action/IClient.java index 97c412ed3a55b..ae540eb26595e 100644 --- a/src/main/java/com/saaavsaaa/client/action/IClient.java +++ b/src/main/java/com/saaavsaaa/client/action/IClient.java @@ -1,8 +1,6 @@ package com.saaavsaaa.client.action; -import com.saaavsaaa.client.utility.section.Listener; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; +import com.saaavsaaa.client.section.Listener; import org.apache.zookeeper.Watcher; import java.io.IOException; diff --git a/src/main/java/com/saaavsaaa/client/action/IProvider.java b/src/main/java/com/saaavsaaa/client/action/IProvider.java index 076335913ed4e..8ebc6c78270f3 100644 --- a/src/main/java/com/saaavsaaa/client/action/IProvider.java +++ b/src/main/java/com/saaavsaaa/client/action/IProvider.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.action; import com.saaavsaaa.client.election.LeaderElection; -import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index c92126426141b..490277b66365c 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -3,9 +3,9 @@ import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.utility.section.ClientTask; -import com.saaavsaaa.client.utility.section.Listener; -import com.saaavsaaa.client.utility.section.Properties; +import com.saaavsaaa.client.section.ClientTask; +import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.section.Properties; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.common.PathUtils; diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index fe8e52b978dbf..749f982ca93b9 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -2,10 +2,10 @@ import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.utility.retry.RetryCount; -import com.saaavsaaa.client.utility.section.Listener; -import com.saaavsaaa.client.utility.section.Properties; -import com.saaavsaaa.client.utility.section.WatcherCreator; +import com.saaavsaaa.client.retry.RetryCount; +import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.section.Properties; +import com.saaavsaaa.client.section.WatcherCreator; import com.saaavsaaa.client.zookeeper.base.BaseProvider; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetry.java b/src/main/java/com/saaavsaaa/client/retry/DelayRetry.java similarity index 95% rename from src/main/java/com/saaavsaaa/client/utility/retry/DelayRetry.java rename to src/main/java/com/saaavsaaa/client/retry/DelayRetry.java index 700177091c462..849ff840ed4aa 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetry.java +++ b/src/main/java/com/saaavsaaa/client/retry/DelayRetry.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.utility.retry; +package com.saaavsaaa.client.retry; /** * Created by aaa diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java b/src/main/java/com/saaavsaaa/client/retry/DelayRetryExecution.java similarity index 95% rename from src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java rename to src/main/java/com/saaavsaaa/client/retry/DelayRetryExecution.java index bfea83fddb002..a2e1c553c5fe3 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/DelayRetryExecution.java +++ b/src/main/java/com/saaavsaaa/client/retry/DelayRetryExecution.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.utility.retry; +package com.saaavsaaa.client.retry; import java.util.Random; diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/RetryCenter.java b/src/main/java/com/saaavsaaa/client/retry/RetryCenter.java similarity index 96% rename from src/main/java/com/saaavsaaa/client/utility/retry/RetryCenter.java rename to src/main/java/com/saaavsaaa/client/retry/RetryCenter.java index 6b339a038360e..a564b2b6be69c 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/RetryCenter.java +++ b/src/main/java/com/saaavsaaa/client/retry/RetryCenter.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.utility.retry; +package com.saaavsaaa.client.retry; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import org.slf4j.Logger; diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/RetryCount.java b/src/main/java/com/saaavsaaa/client/retry/RetryCount.java similarity index 96% rename from src/main/java/com/saaavsaaa/client/utility/retry/RetryCount.java rename to src/main/java/com/saaavsaaa/client/retry/RetryCount.java index 1b3b7e2f2135a..ef4f81c12873e 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/RetryCount.java +++ b/src/main/java/com/saaavsaaa/client/retry/RetryCount.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.utility.retry; +package com.saaavsaaa.client.retry; /** * Created by aaa diff --git a/src/main/java/com/saaavsaaa/client/utility/retry/RetryThread.java b/src/main/java/com/saaavsaaa/client/retry/RetryThread.java similarity index 98% rename from src/main/java/com/saaavsaaa/client/utility/retry/RetryThread.java rename to src/main/java/com/saaavsaaa/client/retry/RetryThread.java index 94bbbb6983850..f47a1b215714b 100644 --- a/src/main/java/com/saaavsaaa/client/utility/retry/RetryThread.java +++ b/src/main/java/com/saaavsaaa/client/retry/RetryThread.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.utility.retry; +package com.saaavsaaa.client.retry; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import org.slf4j.Logger; diff --git a/src/main/java/com/saaavsaaa/client/utility/section/Callback.java b/src/main/java/com/saaavsaaa/client/section/Callback.java similarity index 64% rename from src/main/java/com/saaavsaaa/client/utility/section/Callback.java rename to src/main/java/com/saaavsaaa/client/section/Callback.java index 60cbba7a83000..01f566ca9b4b7 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/Callback.java +++ b/src/main/java/com/saaavsaaa/client/section/Callback.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.utility.section; +package com.saaavsaaa.client.section; /** * Created by aaa diff --git a/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java b/src/main/java/com/saaavsaaa/client/section/ClientTask.java similarity index 94% rename from src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java rename to src/main/java/com/saaavsaaa/client/section/ClientTask.java index 2c3e975088585..280434afbef72 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/ClientTask.java +++ b/src/main/java/com/saaavsaaa/client/section/ClientTask.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.utility.section; +package com.saaavsaaa.client.section; import com.saaavsaaa.client.action.IProvider; import org.apache.zookeeper.KeeperException; diff --git a/src/main/java/com/saaavsaaa/client/utility/section/Listener.java b/src/main/java/com/saaavsaaa/client/section/Listener.java similarity index 75% rename from src/main/java/com/saaavsaaa/client/utility/section/Listener.java rename to src/main/java/com/saaavsaaa/client/section/Listener.java index 879d29ede6057..ab1f4431169d0 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/Listener.java +++ b/src/main/java/com/saaavsaaa/client/section/Listener.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.utility.section; +package com.saaavsaaa.client.section; import org.apache.zookeeper.WatchedEvent; diff --git a/src/main/java/com/saaavsaaa/client/utility/section/Properties.java b/src/main/java/com/saaavsaaa/client/section/Properties.java similarity index 96% rename from src/main/java/com/saaavsaaa/client/utility/section/Properties.java rename to src/main/java/com/saaavsaaa/client/section/Properties.java index 725604d4051f1..d2753655a6026 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/Properties.java +++ b/src/main/java/com/saaavsaaa/client/section/Properties.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.utility.section; +package com.saaavsaaa.client.section; import com.saaavsaaa.client.utility.StringUtil; diff --git a/src/main/java/com/saaavsaaa/client/utility/section/WatcherCreator.java b/src/main/java/com/saaavsaaa/client/section/WatcherCreator.java similarity index 94% rename from src/main/java/com/saaavsaaa/client/utility/section/WatcherCreator.java rename to src/main/java/com/saaavsaaa/client/section/WatcherCreator.java index 7a38f20ace4a7..61a662f4fb6fd 100644 --- a/src/main/java/com/saaavsaaa/client/utility/section/WatcherCreator.java +++ b/src/main/java/com/saaavsaaa/client/section/WatcherCreator.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.utility.section; +package com.saaavsaaa.client.section; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; diff --git a/src/main/java/com/saaavsaaa/client/section/ZKConnection.java b/src/main/java/com/saaavsaaa/client/section/ZKConnection.java new file mode 100644 index 0000000000000..19e8a8ad7bbff --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/section/ZKConnection.java @@ -0,0 +1,8 @@ +package com.saaavsaaa.client.section; + +/** + * Created by aaa + */ +public class ZKConnection { + +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index a7614a59aeed3..2114a91fecc1d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -1,10 +1,10 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.utility.retry.DelayRetry; -import com.saaavsaaa.client.utility.retry.RetryCenter; -import com.saaavsaaa.client.utility.retry.RetryCount; -import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.retry.DelayRetry; +import com.saaavsaaa.client.retry.RetryCenter; +import com.saaavsaaa.client.retry.RetryCount; +import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index e70fa8dc895bd..ead4a3c6579e0 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -4,10 +4,9 @@ import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.StringUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.utility.section.Listener; -import com.saaavsaaa.client.utility.section.Properties; -import com.saaavsaaa.client.utility.section.WatcherCreator; -import com.saaavsaaa.client.zookeeper.ClientFactory; +import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.section.Properties; +import com.saaavsaaa.client.section.WatcherCreator; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java index 26c806119d7ed..9cba791323fc0 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java @@ -1,8 +1,7 @@ package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.utility.section.Listener; -import com.saaavsaaa.client.zookeeper.ClientFactory; +import com.saaavsaaa.client.section.Listener; import java.io.IOException; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java index 444b2b24898d9..11d55ee9a40b5 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.utility.retry.DelayRetryExecution; +import com.saaavsaaa.client.retry.DelayRetryExecution; import org.apache.zookeeper.KeeperException; import java.util.concurrent.Delayed; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java index 3d31979041a1d..ac8bc3bd81b1e 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java @@ -5,7 +5,7 @@ import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java index 410e15adbe6e0..987f79f507509 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.zookeeper.provider; -import com.saaavsaaa.client.utility.retry.RetryCenter; -import com.saaavsaaa.client.utility.retry.RetryCount; +import com.saaavsaaa.client.retry.RetryCenter; +import com.saaavsaaa.client.retry.RetryCount; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java index 938da65bd72a6..e858f787bfbc3 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java @@ -1,6 +1,6 @@ package com.saaavsaaa.client.zookeeper.strategy; -import com.saaavsaaa.client.utility.retry.RetryCenter; +import com.saaavsaaa.client.retry.RetryCenter; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.operation.CreateAllNeedOperation; import com.saaavsaaa.client.zookeeper.operation.DeleteAllChildrenOperation; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index 789c8fcb97646..4b1bb0e7ff133 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -4,7 +4,7 @@ import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.utility.section.Callback; +import com.saaavsaaa.client.section.Callback; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java index 70d6099306ec2..cfd413108b1c0 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.zookeeper.strategy; -import com.saaavsaaa.client.utility.retry.RetryCenter; -import com.saaavsaaa.client.utility.retry.RetryCount; +import com.saaavsaaa.client.retry.RetryCenter; +import com.saaavsaaa.client.retry.RetryCount; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java index 4d0bd52377fa8..7e553a394cc8a 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java @@ -4,7 +4,7 @@ import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.utility.section.Callback; +import com.saaavsaaa.client.section.Callback; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; diff --git a/src/test/java/com/saaavsaaa/client/retry/RetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/RetryCenterTest.java index ce5e9aee3eb0d..b4eabee1c0c6c 100644 --- a/src/test/java/com/saaavsaaa/client/retry/RetryCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/RetryCenterTest.java @@ -2,10 +2,8 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.retry.DelayRetry; -import com.saaavsaaa.client.utility.retry.RetryCenter; -import com.saaavsaaa.client.utility.section.Listener; import com.saaavsaaa.client.zookeeper.ClientFactory; import com.saaavsaaa.client.zookeeper.TestSupport; import com.saaavsaaa.client.zookeeper.base.BaseClient; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java index e86f69b3fd890..13501cd1ba7cb 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.section.Listener; import java.io.IOException; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java index 2444d6c66426d..55f1ce4deb7e5 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java @@ -1,8 +1,7 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.utility.section.Listener; -import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.zookeeper.base.BaseClientTest; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.ZooKeeper; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java index 7d1c1395e6aca..ca6a55941b725 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java @@ -2,7 +2,7 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; import org.apache.zookeeper.CreateMode; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/RetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/RetryStrategyTest.java index 3ac9e38307ca0..e96f3798dd855 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/RetryStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/RetryStrategyTest.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java b/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java index 1a7d7a31089ac..9249bf8c3fd54 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java @@ -1,6 +1,6 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.section.Listener; import org.apache.zookeeper.WatchedEvent; /** diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java index 2e2d7f6923229..ad98e993af4fc 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.section.Listener; import java.io.IOException; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java index 33db8e3d24a9d..e3818d1b29880 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java @@ -3,7 +3,7 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.utility.section.Listener; +import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.zookeeper.ClientFactory; import com.saaavsaaa.client.zookeeper.TestSupport; import org.apache.zookeeper.*; From 34ae5a21b76690b33c210f2a6beb9ee5e6f6a09d Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 21 May 2018 19:04:12 +0800 Subject: [PATCH 115/351] retry only live delay policy start --- .../client/election/LeaderElection.java | 15 ++--- ...RetryCenter.java => AsyncRetryCenter.java} | 9 +-- .../saaavsaaa/client/retry/DelayRetry.java | 12 ++-- .../client/retry/DelayRetryExecution.java | 6 +- .../saaavsaaa/client/retry/RetryCount.java | 1 + .../saaavsaaa/client/retry/RetryThread.java | 2 +- .../saaavsaaa/client/section/Callable.java | 10 +++ .../saaavsaaa/client/section/Connection.java | 41 ++++++++++++ .../saaavsaaa/client/section/Properties.java | 8 +++ .../client/section/ZKConnection.java | 8 --- .../client/zookeeper/ClientFactory.java | 13 +--- .../client/zookeeper/UsualClient.java | 13 ++-- .../operation/CreateAllNeedOperation.java | 4 +- .../operation/CreateCurrentOperation.java | 4 +- .../operation/DeleteAllChildrenOperation.java | 4 +- .../DeleteCurrentBranchOperation.java | 4 +- .../operation/DeleteCurrentOperation.java | 4 +- .../zookeeper/operation/UpdateOperation.java | 4 +- .../zookeeper/provider/RetryProvider.java | 8 +-- .../strategy/AllAsyncRetryStrategy.java | 51 ++++++++++++++ .../zookeeper/strategy/AllRetryStrategy.java | 54 --------------- .../strategy/AsyncRetryStrategy.java | 57 ++++++++++++++++ .../zookeeper/strategy/StrategyType.java | 5 +- ...ryStrategy.java => SyncRetryStrategy.java} | 67 +++++++------------ ...terTest.java => AsyncRetryCenterTest.java} | 11 ++- ...yTest.java => AsyncRetryStrategyTest.java} | 4 +- 26 files changed, 260 insertions(+), 159 deletions(-) rename src/main/java/com/saaavsaaa/client/retry/{RetryCenter.java => AsyncRetryCenter.java} (87%) create mode 100644 src/main/java/com/saaavsaaa/client/section/Callable.java create mode 100644 src/main/java/com/saaavsaaa/client/section/Connection.java delete mode 100644 src/main/java/com/saaavsaaa/client/section/ZKConnection.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java rename src/main/java/com/saaavsaaa/client/zookeeper/strategy/{RetryStrategy.java => SyncRetryStrategy.java} (57%) rename src/test/java/com/saaavsaaa/client/retry/{RetryCenterTest.java => AsyncRetryCenterTest.java} (85%) rename src/test/java/com/saaavsaaa/client/zookeeper/{RetryStrategyTest.java => AsyncRetryStrategyTest.java} (84%) diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index 749f982ca93b9..a015cf28ff742 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -1,12 +1,11 @@ package com.saaavsaaa.client.election; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.retry.RetryCount; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.section.Properties; import com.saaavsaaa.client.section.WatcherCreator; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; +import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; @@ -23,13 +22,13 @@ public abstract class LeaderElection { private int retryCount; public LeaderElection(){ - retryCount = RetryCount.INSTANCE.getStandCount(); + retryCount = Properties.INSTANCE.getNodeElectionCount(); } - private boolean contend(final String node, final BaseProvider provider, final Listener listener) throws KeeperException, InterruptedException { + private boolean contend(final String node, final IProvider provider, final Listener listener) throws KeeperException, InterruptedException { boolean success = false; try { - provider.createCurrentOnly(node, Properties.INSTANCE.getClientId(), CreateMode.EPHEMERAL); + provider.createCurrentOnly(node, Properties.INSTANCE.getClientId(), CreateMode.EPHEMERAL); // todo EPHEMERAL_SEQUENTIAL check index value success = true; } catch (KeeperException.NodeExistsException e) { logger.info("contend not success"); @@ -42,7 +41,7 @@ private boolean contend(final String node, final BaseProvider provider, final Li /* * listener will be register when the contention of the path is unsuccessful */ - public void executeContention(final String nodeBeCompete, final BaseProvider provider) throws KeeperException, InterruptedException { + public void executeContention(final String nodeBeCompete, final IProvider provider) throws KeeperException, InterruptedException { boolean canBegin; String realNode = provider.getRealPath(nodeBeCompete); String contendNode = PathUtil.getRealPath(realNode, Constants.CHANGING_KEY); diff --git a/src/main/java/com/saaavsaaa/client/retry/RetryCenter.java b/src/main/java/com/saaavsaaa/client/retry/AsyncRetryCenter.java similarity index 87% rename from src/main/java/com/saaavsaaa/client/retry/RetryCenter.java rename to src/main/java/com/saaavsaaa/client/retry/AsyncRetryCenter.java index a564b2b6be69c..1f462f3854f29 100644 --- a/src/main/java/com/saaavsaaa/client/retry/RetryCenter.java +++ b/src/main/java/com/saaavsaaa/client/retry/AsyncRetryCenter.java @@ -9,10 +9,10 @@ /** * Created by aaa */ -public enum RetryCenter { +public enum AsyncRetryCenter { INSTANCE; - private static final Logger logger = LoggerFactory.getLogger(RetryCenter.class); + private static final Logger logger = LoggerFactory.getLogger(AsyncRetryCenter.class); private final DelayQueue queue = new DelayQueue<>(); private final RetryThread retryThread = new RetryThread(queue); @@ -21,10 +21,7 @@ public enum RetryCenter { public void init(DelayRetry retrial) { logger.debug("retrial init"); - if (retrial == null) { - logger.debug("retrial real init"); - this.retrial = retrial; - } + this.retrial = retrial; } public synchronized void start(){ diff --git a/src/main/java/com/saaavsaaa/client/retry/DelayRetry.java b/src/main/java/com/saaavsaaa/client/retry/DelayRetry.java index 849ff840ed4aa..44cac7f892ea4 100644 --- a/src/main/java/com/saaavsaaa/client/retry/DelayRetry.java +++ b/src/main/java/com/saaavsaaa/client/retry/DelayRetry.java @@ -4,7 +4,9 @@ * Created by aaa */ public class DelayRetry { - private static final long BASE_DELAY = 1; + private static final long BASE_DELAY = 10; + private static final int BASE_COUNT = 3; + private static final int RETRY_COUNT_BOUND = 29; private final int retryCount; private final long baseDelay; @@ -13,10 +15,8 @@ public class DelayRetry { /* * Millis */ - public DelayRetry(int retryCount, long baseDelay) { - this.retryCount = retryCount; - this.baseDelay = baseDelay; - this.delayUpperBound = Integer.MAX_VALUE; + public DelayRetry(long baseDelay) { + this(RETRY_COUNT_BOUND, baseDelay, Integer.MAX_VALUE); } public DelayRetry(int retryCount, long baseDelay, long delayUpperBound) { @@ -38,6 +38,6 @@ public long getDelayUpperBound() { } public static DelayRetry newNoInitDelayRetrial(){ - return new DelayRetry(RetryCount.INSTANCE.getStandCount(), BASE_DELAY); + return new DelayRetry(BASE_COUNT, BASE_DELAY, BASE_DELAY); } } diff --git a/src/main/java/com/saaavsaaa/client/retry/DelayRetryExecution.java b/src/main/java/com/saaavsaaa/client/retry/DelayRetryExecution.java index a2e1c553c5fe3..719c470b5a1f1 100644 --- a/src/main/java/com/saaavsaaa/client/retry/DelayRetryExecution.java +++ b/src/main/java/com/saaavsaaa/client/retry/DelayRetryExecution.java @@ -12,6 +12,10 @@ public class DelayRetryExecution { private int executeCount = 0; private long executeTick; + public DelayRetryExecution(){ + this(DelayRetry.newNoInitDelayRetrial()); + } + public DelayRetryExecution(final DelayRetry delayRetry) { this.delayRetry = delayRetry; this.executeTick = System.currentTimeMillis(); @@ -30,7 +34,7 @@ public long getNextTick() { private void next() { executeCount ++; long sleep = delayRetry.getBaseDelay() * Math.max(1, this.random.nextInt(1 << delayRetry.getRetryCount() + 1)); - if (sleep < delayRetry.getDelayUpperBound()){ + if (sleep > delayRetry.getDelayUpperBound()){ executeTick += sleep; } else { executeTick += delayRetry.getDelayUpperBound(); diff --git a/src/main/java/com/saaavsaaa/client/retry/RetryCount.java b/src/main/java/com/saaavsaaa/client/retry/RetryCount.java index ef4f81c12873e..ccf64905c7746 100644 --- a/src/main/java/com/saaavsaaa/client/retry/RetryCount.java +++ b/src/main/java/com/saaavsaaa/client/retry/RetryCount.java @@ -3,6 +3,7 @@ /** * Created by aaa */ +@Deprecated public enum RetryCount { INSTANCE; diff --git a/src/main/java/com/saaavsaaa/client/retry/RetryThread.java b/src/main/java/com/saaavsaaa/client/retry/RetryThread.java index f47a1b215714b..625cdacce9ffc 100644 --- a/src/main/java/com/saaavsaaa/client/retry/RetryThread.java +++ b/src/main/java/com/saaavsaaa/client/retry/RetryThread.java @@ -70,7 +70,7 @@ final void addDelayedShutdownHook(final ExecutorService service, final long term @Override public void run() { try { - logger.debug("RetryCenter stop"); + logger.debug("AsyncRetryCenter stop"); queue.clear(); service.shutdown(); service.awaitTermination(terminationTimeout, timeUnit); diff --git a/src/main/java/com/saaavsaaa/client/section/Callable.java b/src/main/java/com/saaavsaaa/client/section/Callable.java new file mode 100644 index 0000000000000..8889d5d097272 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/section/Callable.java @@ -0,0 +1,10 @@ +package com.saaavsaaa.client.section; + +import org.apache.zookeeper.KeeperException; + +/** + * Created by aaa on 18-5-21. + */ +public interface Callable { + void call() throws KeeperException, InterruptedException; +} diff --git a/src/main/java/com/saaavsaaa/client/section/Connection.java b/src/main/java/com/saaavsaaa/client/section/Connection.java new file mode 100644 index 0000000000000..daffa087ec0ef --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/section/Connection.java @@ -0,0 +1,41 @@ +package com.saaavsaaa.client.section; + +import com.saaavsaaa.client.action.IClient; +import org.apache.zookeeper.KeeperException; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Created by aaa + */ +public class Connection { + + //is need reset + private static final Map exceptionResets = new ConcurrentHashMap<>(); + private final IClient client; + + static { + exceptionResets.put(KeeperException.Code.SESSIONEXPIRED.intValue(), true); + exceptionResets.put(KeeperException.Code.SESSIONMOVED.intValue(), true); + exceptionResets.put(KeeperException.Code.CONNECTIONLOSS.intValue(), false); + exceptionResets.put(KeeperException.Code.OPERATIONTIMEOUT.intValue(), false); + } + + public Connection(final IClient client){ + this.client = client; + } + + public static void check(KeeperException e) throws KeeperException { + int code = e.code().intValue(); + if (!exceptionResets.containsKey(code)){ + throw e; + } + boolean reset = exceptionResets.get(code); + if (reset){ + + } else { + // block + } + } +} diff --git a/src/main/java/com/saaavsaaa/client/section/Properties.java b/src/main/java/com/saaavsaaa/client/section/Properties.java index d2753655a6026..ab17e65002570 100644 --- a/src/main/java/com/saaavsaaa/client/section/Properties.java +++ b/src/main/java/com/saaavsaaa/client/section/Properties.java @@ -48,4 +48,12 @@ public long getThreadPeriod(){ } return Long.valueOf(result); } + + public int getNodeElectionCount(){ + String result = bundle.getString("node.election.count"); + if (StringUtil.isNullOrBlank(result)){ + throw new IllegalArgumentException("node.election.count doesn't exist"); + } + return Integer.valueOf(result); + } } diff --git a/src/main/java/com/saaavsaaa/client/section/ZKConnection.java b/src/main/java/com/saaavsaaa/client/section/ZKConnection.java deleted file mode 100644 index 19e8a8ad7bbff..0000000000000 --- a/src/main/java/com/saaavsaaa/client/section/ZKConnection.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.saaavsaaa.client.section; - -/** - * Created by aaa - */ -public class ZKConnection { - -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 2114a91fecc1d..e8f73ee8655c4 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -1,10 +1,9 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.retry.AsyncRetryCenter; import com.saaavsaaa.client.retry.DelayRetry; -import com.saaavsaaa.client.retry.RetryCenter; -import com.saaavsaaa.client.retry.RetryCount; import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -65,14 +64,8 @@ public ClientFactory authorization(final String scheme, final byte[] auth){ return this; } - public ClientFactory setRetryPolicy(final int count){ - RetryCount.INSTANCE.init(count); - return this; - } - public ClientFactory setRetryPolicy(final DelayRetry retrial){ - RetryCenter.INSTANCE.init(retrial); - RetryCenter.INSTANCE.start(); + AsyncRetryCenter.INSTANCE.init(retrial); return this; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 5aba8ddcb893a..a5d2277e74f01 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -49,12 +49,17 @@ public synchronized void useExecStrategy(StrategyType strategyType) { strategy = new ContentionStrategy(new BaseProvider(this, watched)); break; } - case RETRY:{ - strategy = new RetryStrategy(rootNode, this, watched, authorities); + + case SYNC_RETRY:{ + strategy = new SyncRetryStrategy(this, watched); + break; + } + case ASYNC_RETRY:{ + strategy = new AsyncRetryStrategy(this, watched); break; } - case ALL_RETRY:{ - strategy = new AllRetryStrategy(rootNode, this, watched, authorities); + case ALL_ASYNC_RETRY:{ + strategy = new AllAsyncRetryStrategy(this, watched); break; } default:{ diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java index 7341118115d3f..577ee08d9854b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.zookeeper.operation; import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.CreateMode; @@ -26,7 +27,8 @@ protected boolean execute() throws KeeperException, InterruptedException { try { new UsualStrategy(provider).createAllNeedPath(key, value, createMode); return true; - } catch (KeeperException.SessionExpiredException ee) { + } catch (KeeperException ee) { + Connection.check(ee); return false; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java index abc67468ed00a..d845f1b8c2128 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.zookeeper.operation; import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -25,7 +26,8 @@ public boolean execute() throws KeeperException, InterruptedException { try { provider.createCurrentOnly(key, value, createMode); return true; - } catch (KeeperException.SessionExpiredException ee) { + } catch (KeeperException ee) { + Connection.check(ee); return false; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java index 3790ae489f32d..287d41888e37b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.zookeeper.operation; import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.KeeperException; @@ -21,7 +22,8 @@ protected boolean execute() throws KeeperException, InterruptedException { try { new UsualStrategy(provider).deleteAllChildren(key); return true; - } catch (KeeperException.SessionExpiredException ee) { + } catch (KeeperException ee) { + Connection.check(ee); return false; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java index 53e5f5e9282bb..61865162ce1e7 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.zookeeper.operation; import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.KeeperException; @@ -21,7 +22,8 @@ protected boolean execute() throws KeeperException, InterruptedException { try { new UsualStrategy(provider).deleteCurrentBranch(key); return true; - } catch (KeeperException.SessionExpiredException ee) { + } catch (KeeperException ee) { + Connection.check(ee); return false; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java index ebb7d39c42f33..d3d717243b625 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.zookeeper.operation; import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import org.apache.zookeeper.KeeperException; @@ -19,7 +20,8 @@ protected boolean execute() throws KeeperException, InterruptedException { try { provider.deleteOnlyCurrent(key); return true; - } catch (KeeperException.SessionExpiredException ee) { + } catch (KeeperException ee) { + Connection.check(ee); return false; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java index 41aab86ebcc18..53704d214319b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.zookeeper.operation; import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import org.apache.zookeeper.KeeperException; @@ -22,7 +23,8 @@ protected boolean execute() throws KeeperException, InterruptedException { try { provider.update(key, value); return true; - } catch (KeeperException.SessionExpiredException ee) { + } catch (KeeperException ee) { + Connection.check(ee); return false; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java index 987f79f507509..fb003c2effd55 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java @@ -1,6 +1,6 @@ package com.saaavsaaa.client.zookeeper.provider; -import com.saaavsaaa.client.retry.RetryCenter; +import com.saaavsaaa.client.retry.AsyncRetryCenter; import com.saaavsaaa.client.retry.RetryCount; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; @@ -99,7 +99,7 @@ public void createCurrentOnly(final String key, final String value, final Create super.createCurrentOnly(key, value, createMode); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException createCurrentOnly:{}", key); - RetryCenter.INSTANCE.add(new CreateCurrentOperation(this, key, value, createMode)); + AsyncRetryCenter.INSTANCE.add(new CreateCurrentOperation(this, key, value, createMode)); } catch (KeeperException.ConnectionLossException ee){ } @@ -111,7 +111,7 @@ public void update(final String key, final String value) throws KeeperException, super.update(key, value); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException update:{}", key); - RetryCenter.INSTANCE.add(new UpdateOperation(this, key, value)); + AsyncRetryCenter.INSTANCE.add(new UpdateOperation(this, key, value)); } } @@ -121,7 +121,7 @@ public void deleteOnlyCurrent(final String key) throws KeeperException, Interrup super.deleteOnlyCurrent(key); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException deleteOnlyCurrent:{}", key); - RetryCenter.INSTANCE.add(new DeleteCurrentOperation(this, key)); + AsyncRetryCenter.INSTANCE.add(new DeleteCurrentOperation(this, key)); } } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java new file mode 100644 index 0000000000000..aad2ae44a4b63 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java @@ -0,0 +1,51 @@ +package com.saaavsaaa.client.zookeeper.strategy; + +import com.saaavsaaa.client.retry.AsyncRetryCenter; +import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.zookeeper.operation.CreateAllNeedOperation; +import com.saaavsaaa.client.zookeeper.operation.DeleteAllChildrenOperation; +import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentBranchOperation; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by aaa + */ +public class AllAsyncRetryStrategy extends AsyncRetryStrategy { + private static final Logger logger = LoggerFactory.getLogger(AllAsyncRetryStrategy.class); + public AllAsyncRetryStrategy(final BaseClient client, final boolean watched) { + super(client, watched); + } + + @Override + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + try { + super.createAllNeedPath(key, value, createMode); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("AllAsyncRetryStrategy SessionExpiredException CreateAllNeedOperation:{}", key); + AsyncRetryCenter.INSTANCE.add(new CreateAllNeedOperation(provider, key, value, createMode)); + } + } + + @Override + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { + try { + super.deleteAllChildren(key); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("AllAsyncRetryStrategy SessionExpiredException deleteAllChildren:{}", key); + AsyncRetryCenter.INSTANCE.add(new DeleteAllChildrenOperation(provider, key)); + } + } + + @Override + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + try { + super.deleteCurrentBranch(key); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("AllAsyncRetryStrategy SessionExpiredException deleteCurrentBranch:{}", key); + AsyncRetryCenter.INSTANCE.add(new DeleteCurrentBranchOperation(provider, key)); + } + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java deleted file mode 100644 index e858f787bfbc3..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllRetryStrategy.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.saaavsaaa.client.zookeeper.strategy; - -import com.saaavsaaa.client.retry.RetryCenter; -import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.operation.CreateAllNeedOperation; -import com.saaavsaaa.client.zookeeper.operation.DeleteAllChildrenOperation; -import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentBranchOperation; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.data.ACL; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -/** - * Created by aaa - */ -public class AllRetryStrategy extends RetryStrategy { - private static final Logger logger = LoggerFactory.getLogger(AllRetryStrategy.class); - public AllRetryStrategy(String rootNode, BaseClient client, boolean watched, List authorities) { - super(rootNode, client, watched, authorities); - } - - @Override - public void createAllNeedPath(String key, String value, CreateMode createMode) throws KeeperException, InterruptedException { - try { - super.createAllNeedPath(key, value, createMode); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("AllRetryStrategy SessionExpiredException CreateAllNeedOperation:{}", key); - RetryCenter.INSTANCE.add(new CreateAllNeedOperation(provider, key, value, createMode)); - } - } - - @Override - public void deleteAllChildren(String key) throws KeeperException, InterruptedException { - try { - super.deleteAllChildren(key); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("AllRetryStrategy SessionExpiredException deleteAllChildren:{}", key); - RetryCenter.INSTANCE.add(new DeleteAllChildrenOperation(provider, key)); - } - } - - @Override - public void deleteCurrentBranch(String key) throws KeeperException, InterruptedException { - try { - super.deleteCurrentBranch(key); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("AllRetryStrategy SessionExpiredException deleteCurrentBranch:{}", key); - RetryCenter.INSTANCE.add(new DeleteCurrentBranchOperation(provider, key)); - } - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java new file mode 100644 index 0000000000000..af39c6f8161c1 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java @@ -0,0 +1,57 @@ +package com.saaavsaaa.client.zookeeper.strategy; + +import com.saaavsaaa.client.retry.AsyncRetryCenter; +import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; +import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; +import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentOperation; +import com.saaavsaaa.client.zookeeper.operation.UpdateOperation; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by aaa + */ +public class AsyncRetryStrategy extends SyncRetryStrategy { + private static final Logger logger = LoggerFactory.getLogger(AsyncRetryStrategy.class); + + public AsyncRetryStrategy(final BaseClient client, final boolean watched){ + super(new BaseProvider(client, watched)); + AsyncRetryCenter.INSTANCE.start(); + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + String path = provider.getRealPath(key); + try { + provider.createCurrentOnly(path, value, createMode); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("AsyncRetryStrategy SessionExpiredException createCurrentOnly:{}", path); + AsyncRetryCenter.INSTANCE.add(new CreateCurrentOperation(provider, path, value, createMode)); + } + } + + @Override + public void update(final String key, final String value) throws KeeperException, InterruptedException { + String path = provider.getRealPath(key); + try { + provider.update(path, value); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("AsyncRetryStrategy SessionExpiredException update:{}", path); + AsyncRetryCenter.INSTANCE.add(new UpdateOperation(provider, path, value)); + } + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + String path = provider.getRealPath(key); + try { + provider.deleteOnlyCurrent(path); + } catch (KeeperException.SessionExpiredException ee){ + logger.warn("AsyncRetryStrategy SessionExpiredException deleteOnlyCurrent:{}", path); + AsyncRetryCenter.INSTANCE.add(new DeleteCurrentOperation(provider, path)); + } + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java index 73cd97946d78f..96bec8f43154b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java @@ -6,6 +6,7 @@ public enum StrategyType { USUAL, CONTEND, - RETRY, - ALL_RETRY + SYNC_RETRY, + ASYNC_RETRY, + ALL_ASYNC_RETRY } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java similarity index 57% rename from src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java rename to src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java index cfd413108b1c0..eef95a0f34227 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/RetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java @@ -1,16 +1,13 @@ package com.saaavsaaa.client.zookeeper.strategy; -import com.saaavsaaa.client.retry.RetryCenter; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.RetryCount; +import com.saaavsaaa.client.section.Callable; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; -import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; -import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentOperation; -import com.saaavsaaa.client.zookeeper.operation.UpdateOperation; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.Watcher; -import org.apache.zookeeper.data.ACL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,21 +16,26 @@ /** * Created by aaa */ -public class RetryStrategy extends UsualStrategy { - private static final Logger logger = LoggerFactory.getLogger(RetryStrategy.class); +public class SyncRetryStrategy extends UsualStrategy{ + private static final Logger logger = LoggerFactory.getLogger(SyncRetryStrategy.class); - public RetryStrategy(final String rootNode, final BaseClient client, final boolean watched, final List authorities){ - super(new BaseProvider(client, watched)); + public SyncRetryStrategy(final IProvider provider){ + super(provider); RetryCount.INSTANCE.start(); } + public SyncRetryStrategy(final BaseClient client, final boolean watched){ + this(new BaseProvider(client, watched)); + } + @Override public byte[] getData(final String key) throws KeeperException, InterruptedException { String path = provider.getRealPath(key); try { return provider.getData(path); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryStrategy SessionExpiredException getData:{}", path); + } catch (KeeperException ee){ + logger.warn("AsyncRetryStrategy KeeperException getData:{}", path); + if (RetryCount.INSTANCE.continueExecute()) { byte[] data = getData(path); RetryCount.INSTANCE.reset(); @@ -49,7 +51,7 @@ public boolean checkExists(final String key) throws KeeperException, Interrupted try { return provider.checkExists(path); } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryStrategy SessionExpiredException checkExists:{}", path); + logger.warn("AsyncRetryStrategy KeeperException checkExists:{}", path); if (RetryCount.INSTANCE.continueExecute()) { boolean result = checkExists(path); RetryCount.INSTANCE.reset(); @@ -65,7 +67,7 @@ public boolean checkExists(final String key, final Watcher watcher) throws Keepe try { return provider.checkExists(path, watcher); } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryStrategy SessionExpiredException checkExists:{}", path); + logger.warn("AsyncRetryStrategy KeeperException checkExists:{}", path); if (RetryCount.INSTANCE.continueExecute()) { boolean result = checkExists(path, watcher); RetryCount.INSTANCE.reset(); @@ -81,7 +83,7 @@ public List getChildren(final String key) throws KeeperException, Interr try { return provider.getChildren(path); } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryStrategy SessionExpiredException getChildren:{}", path); + logger.warn("AsyncRetryStrategy KeeperException getChildren:{}", path); if (RetryCount.INSTANCE.continueExecute()) { List result = getChildren(path); RetryCount.INSTANCE.reset(); @@ -94,33 +96,16 @@ public List getChildren(final String key) throws KeeperException, Interr @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { String path = provider.getRealPath(key); - try { - provider.createCurrentOnly(path, value, createMode); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryStrategy SessionExpiredException createCurrentOnly:{}", path); - RetryCenter.INSTANCE.add(new CreateCurrentOperation(provider, path, value, createMode)); - } - } - - @Override - public void update(final String key, final String value) throws KeeperException, InterruptedException { - String path = provider.getRealPath(key); - try { - provider.update(path, value); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryStrategy SessionExpiredException update:{}", path); - RetryCenter.INSTANCE.add(new UpdateOperation(provider, path, value)); - } - } - - @Override - public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - String path = provider.getRealPath(key); - try { - provider.deleteOnlyCurrent(path); + Callable callable = new Callable() { + @Override + public void call() throws KeeperException, InterruptedException { + provider.createCurrentOnly(path, value, createMode); + } + }; + /*try { + } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryStrategy SessionExpiredException deleteOnlyCurrent:{}", path); - RetryCenter.INSTANCE.add(new DeleteCurrentOperation(provider, path)); - } + logger.warn("AsyncRetryStrategy SessionExpiredException createCurrentOnly:{}", path); + }*/ } } diff --git a/src/test/java/com/saaavsaaa/client/retry/RetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java similarity index 85% rename from src/test/java/com/saaavsaaa/client/retry/RetryCenterTest.java rename to src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java index b4eabee1c0c6c..0deb9378ac3a2 100644 --- a/src/test/java/com/saaavsaaa/client/retry/RetryCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java @@ -22,21 +22,21 @@ /** * Created by aaa */ -public class RetryCenterTest { +public class AsyncRetryCenterTest { private IProvider provider; @Before public void start() throws IOException, InterruptedException { provider = new BaseProvider(createClient(), false); - RetryCenter.INSTANCE.init(new DelayRetry(1, 1)); - RetryCenter.INSTANCE.start(); + AsyncRetryCenter.INSTANCE.init(DelayRetry.newNoInitDelayRetrial()); + AsyncRetryCenter.INSTANCE.start(); } protected IClient createClient() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); Listener listener = TestSupport.buildListener(); IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - ((BaseClient)client).useExecStrategy(StrategyType.RETRY); + ((BaseClient)client).useExecStrategy(StrategyType.ASYNC_RETRY); return client; } @@ -55,8 +55,7 @@ public void nothing(){ public void create() throws InterruptedException, KeeperException { String key = "a"; String value = "bbb11"; - Thread.sleep(1000); - RetryCenter.INSTANCE.add(new CreateCurrentOperation(provider, key, value, CreateMode.PERSISTENT)); + AsyncRetryCenter.INSTANCE.add(new CreateCurrentOperation(provider, key, value, CreateMode.PERSISTENT)); Thread.sleep(1000); String path = PathUtil.getRealPath(TestSupport.ROOT, key); assert provider.checkExists(path); diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/RetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java similarity index 84% rename from src/test/java/com/saaavsaaa/client/zookeeper/RetryStrategyTest.java rename to src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java index e96f3798dd855..40f59867d4064 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/RetryStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java @@ -10,12 +10,12 @@ /** * Created by aaa */ -public class RetryStrategyTest extends UsualClientTest{ +public class AsyncRetryStrategyTest extends UsualClientTest{ @Override protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - ((BaseClient)client).useExecStrategy(StrategyType.RETRY); + ((BaseClient)client).useExecStrategy(StrategyType.ASYNC_RETRY); return client; } } From 1651bec731ae42fbac405f5641386d8b4bf3767d Mon Sep 17 00:00:00 2001 From: saaav Date: Tue, 22 May 2018 17:26:44 +0800 Subject: [PATCH 116/351] retry queue next --- .../com/saaavsaaa/client/cache/PathTree.java | 2 +- .../client/election/LeaderElection.java | 2 +- .../client/retry/AsyncRetryCenter.java | 20 +++++++------ .../client/retry/DelayRetryExecution.java | 26 ++++++++++------- .../{DelayRetry.java => RetryPolicy.java} | 10 +++---- .../saaavsaaa/client/retry/RetryThread.java | 9 +++--- .../client/section/ClientContext.java | 22 +++++++++++++++ .../{section => utility}/Properties.java | 2 +- .../client/zookeeper/ClientFactory.java | 8 +++--- .../client/zookeeper/base/BaseClient.java | 19 +++++++++++-- .../zookeeper/base/BaseClientFactory.java | 4 +++ .../client/zookeeper/base/BaseOperation.java | 8 +++++- .../strategy/AsyncRetryStrategy.java | 3 +- .../zookeeper/strategy/SyncRetryStrategy.java | 10 +++---- .../client/retry/AsyncRetryCenterTest.java | 5 ++-- .../retry/TestCreateCurrentOperation.java | 28 +++++++++++++++++++ 16 files changed, 131 insertions(+), 47 deletions(-) rename src/main/java/com/saaavsaaa/client/retry/{DelayRetry.java => RetryPolicy.java} (74%) create mode 100644 src/main/java/com/saaavsaaa/client/section/ClientContext.java rename src/main/java/com/saaavsaaa/client/{section => utility}/Properties.java (97%) create mode 100644 src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index 490277b66365c..2e730de6e76b0 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -5,7 +5,7 @@ import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.section.ClientTask; import com.saaavsaaa.client.section.Listener; -import com.saaavsaaa.client.section.Properties; +import com.saaavsaaa.client.utility.Properties; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.common.PathUtils; diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index a015cf28ff742..deefc824be6cd 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -2,7 +2,7 @@ import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.section.Listener; -import com.saaavsaaa.client.section.Properties; +import com.saaavsaaa.client.utility.Properties; import com.saaavsaaa.client.section.WatcherCreator; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; diff --git a/src/main/java/com/saaavsaaa/client/retry/AsyncRetryCenter.java b/src/main/java/com/saaavsaaa/client/retry/AsyncRetryCenter.java index 1f462f3854f29..d88c6645f608e 100644 --- a/src/main/java/com/saaavsaaa/client/retry/AsyncRetryCenter.java +++ b/src/main/java/com/saaavsaaa/client/retry/AsyncRetryCenter.java @@ -17,11 +17,15 @@ public enum AsyncRetryCenter { private final RetryThread retryThread = new RetryThread(queue); private boolean started = false; - private DelayRetry retrial; + private RetryPolicy retryPolicy; - public void init(DelayRetry retrial) { - logger.debug("retrial init"); - this.retrial = retrial; + public void init(RetryPolicy retryPolicy) { + logger.debug("retryPolicy init"); + if (retryPolicy == null){ + logger.warn("retryPolicy is null and auto init with RetryPolicy.newNoInitDelayPolicy"); + retryPolicy = RetryPolicy.newNoInitDelayPolicy(); + } + this.retryPolicy = retryPolicy; } public synchronized void start(){ @@ -34,11 +38,11 @@ public synchronized void start(){ } public void add(BaseOperation operation){ - if (retrial == null){ - logger.debug("retrial no init"); - retrial = DelayRetry.newNoInitDelayRetrial(); + if (retryPolicy == null){ + logger.warn("retryPolicy no init and auto init with RetryPolicy.newNoInitDelayPolicy"); + retryPolicy = RetryPolicy.newNoInitDelayPolicy(); } - operation.setRetrial(new DelayRetryExecution(retrial)); + operation.setRetrial(new DelayRetryExecution(retryPolicy)); queue.offer(operation); logger.debug("enqueue operation:{}", operation.toString()); } diff --git a/src/main/java/com/saaavsaaa/client/retry/DelayRetryExecution.java b/src/main/java/com/saaavsaaa/client/retry/DelayRetryExecution.java index 719c470b5a1f1..feddf207eed70 100644 --- a/src/main/java/com/saaavsaaa/client/retry/DelayRetryExecution.java +++ b/src/main/java/com/saaavsaaa/client/retry/DelayRetryExecution.java @@ -1,43 +1,49 @@ package com.saaavsaaa.client.retry; +import com.saaavsaaa.client.section.Callable; + import java.util.Random; /** * Created by aaa */ public class DelayRetryExecution { - private final DelayRetry delayRetry; + private final RetryPolicy retryPolicy; private final Random random; private int executeCount = 0; private long executeTick; public DelayRetryExecution(){ - this(DelayRetry.newNoInitDelayRetrial()); + this(RetryPolicy.newNoInitDelayPolicy()); } - public DelayRetryExecution(final DelayRetry delayRetry) { - this.delayRetry = delayRetry; + public DelayRetryExecution(final RetryPolicy retryPolicy) { + this.retryPolicy = retryPolicy; this.executeTick = System.currentTimeMillis(); this.random = new Random(); + next(); } public boolean hasNext() { - return executeCount < delayRetry.getRetryCount(); + return executeCount < retryPolicy.getRetryCount(); } public long getNextTick() { - next(); return executeTick; } - private void next() { + public void next() { executeCount ++; - long sleep = delayRetry.getBaseDelay() * Math.max(1, this.random.nextInt(1 << delayRetry.getRetryCount() + 1)); - if (sleep > delayRetry.getDelayUpperBound()){ + long sleep = retryPolicy.getBaseDelay() * Math.max(1, this.random.nextInt(1 << retryPolicy.getRetryCount() + 1)); + if (sleep < retryPolicy.getDelayUpperBound()){ executeTick += sleep; } else { - executeTick += delayRetry.getDelayUpperBound(); + executeTick += retryPolicy.getDelayUpperBound(); } } + + public void call(final Callable callable){ + + } } diff --git a/src/main/java/com/saaavsaaa/client/retry/DelayRetry.java b/src/main/java/com/saaavsaaa/client/retry/RetryPolicy.java similarity index 74% rename from src/main/java/com/saaavsaaa/client/retry/DelayRetry.java rename to src/main/java/com/saaavsaaa/client/retry/RetryPolicy.java index 44cac7f892ea4..47704554f71ef 100644 --- a/src/main/java/com/saaavsaaa/client/retry/DelayRetry.java +++ b/src/main/java/com/saaavsaaa/client/retry/RetryPolicy.java @@ -3,7 +3,7 @@ /** * Created by aaa */ -public class DelayRetry { +public class RetryPolicy { private static final long BASE_DELAY = 10; private static final int BASE_COUNT = 3; private static final int RETRY_COUNT_BOUND = 29; @@ -15,11 +15,11 @@ public class DelayRetry { /* * Millis */ - public DelayRetry(long baseDelay) { + public RetryPolicy(long baseDelay) { this(RETRY_COUNT_BOUND, baseDelay, Integer.MAX_VALUE); } - public DelayRetry(int retryCount, long baseDelay, long delayUpperBound) { + public RetryPolicy(int retryCount, long baseDelay, long delayUpperBound) { this.retryCount = retryCount; this.baseDelay = baseDelay; this.delayUpperBound = delayUpperBound; @@ -37,7 +37,7 @@ public long getDelayUpperBound() { return delayUpperBound; } - public static DelayRetry newNoInitDelayRetrial(){ - return new DelayRetry(BASE_COUNT, BASE_DELAY, BASE_DELAY); + public static RetryPolicy newNoInitDelayPolicy(){ + return new RetryPolicy(BASE_COUNT, BASE_DELAY, Integer.MAX_VALUE); } } diff --git a/src/main/java/com/saaavsaaa/client/retry/RetryThread.java b/src/main/java/com/saaavsaaa/client/retry/RetryThread.java index 625cdacce9ffc..e7e513a31ef10 100644 --- a/src/main/java/com/saaavsaaa/client/retry/RetryThread.java +++ b/src/main/java/com/saaavsaaa/client/retry/RetryThread.java @@ -12,7 +12,7 @@ */ public class RetryThread extends Thread { private static final Logger logger = LoggerFactory.getLogger(RetryThread.class); - private final ThreadPoolExecutor retryExecution; + private final ThreadPoolExecutor retryExecutor; private final int corePoolSize = Runtime.getRuntime().availableProcessors(); private final int maximumPoolSize = corePoolSize; private final long keepAliveTime = 0; @@ -21,17 +21,18 @@ public class RetryThread extends Thread { public RetryThread(DelayQueue queue) { this.queue = queue; - retryExecution = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(10), new ThreadFactory() { + retryExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(10), new ThreadFactory() { private final AtomicInteger threadIndex = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r); thread.setDaemon(true); thread.setName("zk-retry-" + threadIndex.incrementAndGet()); + logger.debug("new thread:{}", thread.getName()); return thread; } }); - addDelayedShutdownHook(retryExecution, closeDelay, TimeUnit.SECONDS); + addDelayedShutdownHook(retryExecutor, closeDelay, TimeUnit.SECONDS); } @Override @@ -46,7 +47,7 @@ public void run(){ logger.error("retry interrupt e:{}", e.getMessage()); continue; } - retryExecution.submit(new Runnable() { + retryExecutor.submit(new Runnable() { @Override public void run() { boolean result; diff --git a/src/main/java/com/saaavsaaa/client/section/ClientContext.java b/src/main/java/com/saaavsaaa/client/section/ClientContext.java new file mode 100644 index 0000000000000..2e377cd298bd0 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/section/ClientContext.java @@ -0,0 +1,22 @@ +package com.saaavsaaa.client.section; + +import com.saaavsaaa.client.retry.RetryPolicy; + +/** + * Created by aaa + */ +public final class ClientContext { + private final RetryPolicy retryPolicy; + + public ClientContext(final RetryPolicy retryPolicy) { + this.retryPolicy = retryPolicy; + } + + public ClientContext() { + retryPolicy = null; + } + + public RetryPolicy getRetryPolicy() { + return retryPolicy; + } +} diff --git a/src/main/java/com/saaavsaaa/client/section/Properties.java b/src/main/java/com/saaavsaaa/client/utility/Properties.java similarity index 97% rename from src/main/java/com/saaavsaaa/client/section/Properties.java rename to src/main/java/com/saaavsaaa/client/utility/Properties.java index ab17e65002570..b39024961fae2 100644 --- a/src/main/java/com/saaavsaaa/client/section/Properties.java +++ b/src/main/java/com/saaavsaaa/client/utility/Properties.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.section; +package com.saaavsaaa.client.utility; import com.saaavsaaa.client.utility.StringUtil; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index e8f73ee8655c4..e6f5566da4d64 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.retry.AsyncRetryCenter; -import com.saaavsaaa.client.retry.DelayRetry; +import com.saaavsaaa.client.retry.RetryPolicy; +import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; @@ -64,8 +64,8 @@ public ClientFactory authorization(final String scheme, final byte[] auth){ return this; } - public ClientFactory setRetryPolicy(final DelayRetry retrial){ - AsyncRetryCenter.INSTANCE.init(retrial); + public ClientFactory setRetryPolicy(final RetryPolicy retryPolicy){ + this.context = new ClientContext(retryPolicy); return this; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index ead4a3c6579e0..3f1fac6702c1b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -1,11 +1,13 @@ package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IClient; +import com.saaavsaaa.client.retry.RetryPolicy; +import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.StringUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.section.Listener; -import com.saaavsaaa.client.section.Properties; +import com.saaavsaaa.client.utility.Properties; import com.saaavsaaa.client.section.WatcherCreator; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; import org.apache.zookeeper.*; @@ -35,6 +37,7 @@ public abstract class BaseClient implements IClient { private final int sessionTimeOut; private String scheme; private byte[] auth; + protected ClientContext context; protected ZooKeeper zooKeeper; protected List authorities; @@ -143,7 +146,7 @@ private void createNamespace(final byte[] date) throws KeeperException, Interrup } try { zooKeeper.create(rootNode, date, authorities, CreateMode.PERSISTENT); - logger.debug("create root:{}", rootNode); + logger.debug("creating root:{}", rootNode); } catch (KeeperException.NodeExistsException ee){ logger.warn("root create:{}", ee.getMessage()); rootExist = true; @@ -156,7 +159,7 @@ public void process(WatchedEvent event) { rootExist = false; } })); - logger.debug("create root:{}", rootNode); + logger.debug("created root:{}", rootNode); } void deleteNamespace() throws KeeperException, InterruptedException { @@ -175,6 +178,16 @@ void setAuthorities(final String scheme, final byte[] auth) { this.authorities = ZooDefs.Ids.CREATOR_ALL_ACL; } + void setContext(final ClientContext context){ + this.context = context; + } + public ClientContext getContext(){ + if (context == null){ + context = new ClientContext(); + } + return context; + } + void setClientFactory(BaseClientFactory clientFactory) { this.clientFactory = clientFactory; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java index 9cba791323fc0..41064f9e53195 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java @@ -1,6 +1,8 @@ package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IClient; +import com.saaavsaaa.client.retry.RetryPolicy; +import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.section.Listener; import java.io.IOException; @@ -14,6 +16,7 @@ public abstract class BaseClientFactory { protected String namespace; protected String scheme; protected byte[] auth; + protected ClientContext context; protected String servers; protected int sessionTimeoutMilliseconds; @@ -22,6 +25,7 @@ public synchronized IClient start() throws IOException, InterruptedException { client.setClientFactory(this); client.setRootNode(namespace); client.setAuthorities(scheme , auth); + client.setContext(context); client.start(); if (globalListener != null) { client.registerWatch(globalListener); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java index 11d55ee9a40b5..9213e06f6e71f 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java @@ -3,6 +3,8 @@ import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.DelayRetryExecution; import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; @@ -11,6 +13,7 @@ * Created by aaa */ public abstract class BaseOperation implements Delayed { + private static final Logger logger = LoggerFactory.getLogger(BaseOperation.class); protected final IProvider provider; protected DelayRetryExecution retryExecution; @@ -24,7 +27,9 @@ public void setRetrial(final DelayRetryExecution retryExecution){ @Override public long getDelay(TimeUnit unit) { - long result = unit.convert(this.retryExecution.getNextTick() - System.currentTimeMillis() , TimeUnit.MILLISECONDS); + long absoluteBlock = this.retryExecution.getNextTick() - System.currentTimeMillis(); + logger.debug("queue getDelay block:{}", absoluteBlock); + long result = unit.convert(absoluteBlock, TimeUnit.MILLISECONDS); return result; } @@ -44,6 +49,7 @@ public int compareTo(Delayed delayed) { public boolean executeOperation() throws KeeperException, InterruptedException { boolean result = execute(); if (!result && retryExecution.hasNext()){ + retryExecution.next(); return true; } return false; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java index af39c6f8161c1..99cf8d3bfa9cf 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java @@ -18,7 +18,8 @@ public class AsyncRetryStrategy extends SyncRetryStrategy { private static final Logger logger = LoggerFactory.getLogger(AsyncRetryStrategy.class); public AsyncRetryStrategy(final BaseClient client, final boolean watched){ - super(new BaseProvider(client, watched)); + super(client, watched); + AsyncRetryCenter.INSTANCE.init(retryPolicy); AsyncRetryCenter.INSTANCE.start(); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java index eef95a0f34227..27bb0f5b01a9c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java @@ -2,6 +2,7 @@ import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.RetryCount; +import com.saaavsaaa.client.retry.RetryPolicy; import com.saaavsaaa.client.section.Callable; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; @@ -18,14 +19,11 @@ */ public class SyncRetryStrategy extends UsualStrategy{ private static final Logger logger = LoggerFactory.getLogger(SyncRetryStrategy.class); - - public SyncRetryStrategy(final IProvider provider){ - super(provider); - RetryCount.INSTANCE.start(); - } + protected final RetryPolicy retryPolicy; public SyncRetryStrategy(final BaseClient client, final boolean watched){ - this(new BaseProvider(client, watched)); + super(new BaseProvider(client, watched)); + retryPolicy = client.getContext().getRetryPolicy(); } @Override diff --git a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java index 0deb9378ac3a2..27adcc5c875e9 100644 --- a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java @@ -28,7 +28,7 @@ public class AsyncRetryCenterTest { @Before public void start() throws IOException, InterruptedException { provider = new BaseProvider(createClient(), false); - AsyncRetryCenter.INSTANCE.init(DelayRetry.newNoInitDelayRetrial()); + AsyncRetryCenter.INSTANCE.init(new RetryPolicy(3, 3, 10)); AsyncRetryCenter.INSTANCE.start(); } @@ -55,11 +55,12 @@ public void nothing(){ public void create() throws InterruptedException, KeeperException { String key = "a"; String value = "bbb11"; - AsyncRetryCenter.INSTANCE.add(new CreateCurrentOperation(provider, key, value, CreateMode.PERSISTENT)); + AsyncRetryCenter.INSTANCE.add(new TestCreateCurrentOperation(provider, key, value, CreateMode.PERSISTENT)); Thread.sleep(1000); String path = PathUtil.getRealPath(TestSupport.ROOT, key); assert provider.checkExists(path); provider.deleteOnlyCurrent(path); + provider.deleteOnlyCurrent(provider.getRealPath(TestSupport.ROOT)); assert !provider.checkExists(path); } } diff --git a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java new file mode 100644 index 0000000000000..5d596d7bfc5e8 --- /dev/null +++ b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java @@ -0,0 +1,28 @@ +package com.saaavsaaa.client.retry; + +import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.section.Connection; +import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; + +/** + * Created by aaa + */ +public class TestCreateCurrentOperation extends CreateCurrentOperation { + private int count = 0; + + public TestCreateCurrentOperation(IProvider provider, String key, String value, CreateMode createMode) { + super(provider, key, value, createMode); + } + + @Override + public boolean execute() throws KeeperException, InterruptedException { + if (count < 2){ + count++; + return false; + } + super.execute(); + return true; + } +} From c18db1341c16a32042db887c85ffb2179b6fed30 Mon Sep 17 00:00:00 2001 From: saaav Date: Tue, 22 May 2018 18:59:39 +0800 Subject: [PATCH 117/351] provider -> client --- .../saaavsaaa/client/section/Callable.java | 4 ++-- .../saaavsaaa/client/section/Connection.java | 5 +++-- .../client/zookeeper/base/BaseOperation.java | 20 ++++++++++++++----- .../operation/CreateAllNeedOperation.java | 16 ++++++--------- .../operation/CreateCurrentOperation.java | 19 ++++++++---------- .../operation/DeleteAllChildrenOperation.java | 16 ++++++--------- .../DeleteCurrentBranchOperation.java | 16 ++++++--------- .../operation/DeleteCurrentOperation.java | 17 +++++++--------- .../zookeeper/operation/UpdateOperation.java | 17 +++++++--------- .../zookeeper/provider/RetryProvider.java | 6 +++--- .../strategy/AllAsyncRetryStrategy.java | 6 +++--- .../strategy/AsyncRetryStrategy.java | 6 +++--- .../zookeeper/strategy/SyncRetryStrategy.java | 13 +++++++++++- .../client/retry/AsyncRetryCenterTest.java | 6 +++--- .../retry/TestCreateCurrentOperation.java | 11 +++++----- 15 files changed, 89 insertions(+), 89 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/section/Callable.java b/src/main/java/com/saaavsaaa/client/section/Callable.java index 8889d5d097272..3070a5d5ff177 100644 --- a/src/main/java/com/saaavsaaa/client/section/Callable.java +++ b/src/main/java/com/saaavsaaa/client/section/Callable.java @@ -5,6 +5,6 @@ /** * Created by aaa on 18-5-21. */ -public interface Callable { - void call() throws KeeperException, InterruptedException; +public interface Callable { + T call() throws KeeperException, InterruptedException; } diff --git a/src/main/java/com/saaavsaaa/client/section/Connection.java b/src/main/java/com/saaavsaaa/client/section/Connection.java index daffa087ec0ef..f36558b8a06aa 100644 --- a/src/main/java/com/saaavsaaa/client/section/Connection.java +++ b/src/main/java/com/saaavsaaa/client/section/Connection.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.section; import com.saaavsaaa.client.action.IClient; +import com.saaavsaaa.client.zookeeper.base.BaseClient; import org.apache.zookeeper.KeeperException; import java.util.Map; @@ -26,14 +27,14 @@ public Connection(final IClient client){ this.client = client; } - public static void check(KeeperException e) throws KeeperException { + public void check(KeeperException e) throws KeeperException { int code = e.code().intValue(); if (!exceptionResets.containsKey(code)){ throw e; } boolean reset = exceptionResets.get(code); if (reset){ - +// this.client = ((BaseClient)client).getContext() } else { // block } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java index 9213e06f6e71f..d43d361a003e1 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java @@ -1,7 +1,10 @@ package com.saaavsaaa.client.zookeeper.base; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.DelayRetryExecution; +import com.saaavsaaa.client.section.Connection; +import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.KeeperException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,11 +17,11 @@ */ public abstract class BaseOperation implements Delayed { private static final Logger logger = LoggerFactory.getLogger(BaseOperation.class); - protected final IProvider provider; + protected final IClient client; protected DelayRetryExecution retryExecution; - protected BaseOperation(final IProvider provider) { - this.provider = provider; + protected BaseOperation(final IClient client) { + this.client = client; } public void setRetrial(final DelayRetryExecution retryExecution){ @@ -41,13 +44,20 @@ public int compareTo(Delayed delayed) { return (int) (this.getDelay(TimeUnit.MILLISECONDS) - delayed.getDelay(TimeUnit.MILLISECONDS)); } - protected abstract boolean execute() throws KeeperException, InterruptedException; + protected abstract void execute() throws KeeperException, InterruptedException; /* * @Return whether or not continue enqueue */ public boolean executeOperation() throws KeeperException, InterruptedException { - boolean result = execute(); + boolean result; + try { + execute(); + result = true; + } catch (KeeperException ee) { + new Connection(client).check(ee); + result = false; + } if (!result && retryExecution.hasNext()){ retryExecution.next(); return true; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java index 577ee08d9854b..1a000521f694b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java @@ -1,8 +1,10 @@ package com.saaavsaaa.client.zookeeper.operation; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -15,22 +17,16 @@ public class CreateAllNeedOperation extends BaseOperation { private final String value; private final CreateMode createMode; - public CreateAllNeedOperation(final IProvider provider, final String key, final String value, final CreateMode createMode) { - super(provider); + public CreateAllNeedOperation(final IClient client, final String key, final String value, final CreateMode createMode) { + super(client); this.key = key; this.value = value; this.createMode = createMode; } @Override - protected boolean execute() throws KeeperException, InterruptedException { - try { - new UsualStrategy(provider).createAllNeedPath(key, value, createMode); - return true; - } catch (KeeperException ee) { - Connection.check(ee); - return false; - } + protected void execute() throws KeeperException, InterruptedException { + new UsualStrategy(new BaseProvider(client, false)).createAllNeedPath(key, value, createMode); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java index d845f1b8c2128..733ec978c28e6 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java @@ -1,8 +1,10 @@ package com.saaavsaaa.client.zookeeper.operation; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -14,22 +16,17 @@ public class CreateCurrentOperation extends BaseOperation { private final String value; private final CreateMode createMode; - public CreateCurrentOperation(final IProvider provider, final String key, final String value, final CreateMode createMode) { - super(provider); - this.key = provider.getRealPath(key); + public CreateCurrentOperation(final IClient client, final String key, final String value, final CreateMode createMode) { + super(client); + this.key = key; this.value = value; this.createMode = createMode; } @Override - public boolean execute() throws KeeperException, InterruptedException { - try { - provider.createCurrentOnly(key, value, createMode); - return true; - } catch (KeeperException ee) { - Connection.check(ee); - return false; - } + public void execute() throws KeeperException, InterruptedException { + IProvider provider = new BaseProvider(client, false); + provider.createCurrentOnly(provider.getRealPath(key), value, createMode); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java index 287d41888e37b..7b80c20f6e608 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java @@ -1,8 +1,10 @@ package com.saaavsaaa.client.zookeeper.operation; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.KeeperException; @@ -12,20 +14,14 @@ public class DeleteAllChildrenOperation extends BaseOperation { private final String key; - public DeleteAllChildrenOperation(final IProvider provider, final String key) { - super(provider); + public DeleteAllChildrenOperation(final IClient client, final String key) { + super(client); this.key = key; } @Override - protected boolean execute() throws KeeperException, InterruptedException { - try { - new UsualStrategy(provider).deleteAllChildren(key); - return true; - } catch (KeeperException ee) { - Connection.check(ee); - return false; - } + protected void execute() throws KeeperException, InterruptedException { + new UsualStrategy(new BaseProvider(client, false)).deleteAllChildren(key); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java index 61865162ce1e7..9559382b40dad 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java @@ -1,8 +1,10 @@ package com.saaavsaaa.client.zookeeper.operation; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.KeeperException; @@ -12,20 +14,14 @@ public class DeleteCurrentBranchOperation extends BaseOperation { private final String key; - public DeleteCurrentBranchOperation(final IProvider provider, final String key) { - super(provider); + public DeleteCurrentBranchOperation(final IClient client, final String key) { + super(client); this.key = key; } @Override - protected boolean execute() throws KeeperException, InterruptedException { - try { - new UsualStrategy(provider).deleteCurrentBranch(key); - return true; - } catch (KeeperException ee) { - Connection.check(ee); - return false; - } + protected void execute() throws KeeperException, InterruptedException { + new UsualStrategy(new BaseProvider(client, false)).deleteCurrentBranch(key); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java index d3d717243b625..0f50578992aac 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java @@ -1,8 +1,10 @@ package com.saaavsaaa.client.zookeeper.operation; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; import org.apache.zookeeper.KeeperException; /** @@ -10,20 +12,15 @@ */ public class DeleteCurrentOperation extends BaseOperation { private final String key; - public DeleteCurrentOperation(final IProvider provider, final String key) { - super(provider); + public DeleteCurrentOperation(final IClient client, final String key) { + super(client); this.key = key; } @Override - protected boolean execute() throws KeeperException, InterruptedException { - try { - provider.deleteOnlyCurrent(key); - return true; - } catch (KeeperException ee) { - Connection.check(ee); - return false; - } + protected void execute() throws KeeperException, InterruptedException { + IProvider provider = new BaseProvider(client, false); + provider.deleteOnlyCurrent(provider.getRealPath(key)); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java index 53704d214319b..ef16090ff1ed6 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java @@ -1,8 +1,10 @@ package com.saaavsaaa.client.zookeeper.operation; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; import org.apache.zookeeper.KeeperException; /** @@ -12,21 +14,16 @@ public class UpdateOperation extends BaseOperation { private final String key; private final String value; - public UpdateOperation(final IProvider provider, final String key, final String value) { - super(provider); + public UpdateOperation(final IClient client, final String key, final String value) { + super(client); this.key = key; this.value = value; } @Override - protected boolean execute() throws KeeperException, InterruptedException { - try { - provider.update(key, value); - return true; - } catch (KeeperException ee) { - Connection.check(ee); - return false; - } + protected void execute() throws KeeperException, InterruptedException { + IProvider provider = new BaseProvider(client, false); + provider.update(provider.getRealPath(key), value); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java index fb003c2effd55..b3d306f2d4356 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java @@ -99,7 +99,7 @@ public void createCurrentOnly(final String key, final String value, final Create super.createCurrentOnly(key, value, createMode); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException createCurrentOnly:{}", key); - AsyncRetryCenter.INSTANCE.add(new CreateCurrentOperation(this, key, value, createMode)); + AsyncRetryCenter.INSTANCE.add(new CreateCurrentOperation(client, key, value, createMode)); } catch (KeeperException.ConnectionLossException ee){ } @@ -111,7 +111,7 @@ public void update(final String key, final String value) throws KeeperException, super.update(key, value); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException update:{}", key); - AsyncRetryCenter.INSTANCE.add(new UpdateOperation(this, key, value)); + AsyncRetryCenter.INSTANCE.add(new UpdateOperation(client, key, value)); } } @@ -121,7 +121,7 @@ public void deleteOnlyCurrent(final String key) throws KeeperException, Interrup super.deleteOnlyCurrent(key); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException deleteOnlyCurrent:{}", key); - AsyncRetryCenter.INSTANCE.add(new DeleteCurrentOperation(this, key)); + AsyncRetryCenter.INSTANCE.add(new DeleteCurrentOperation(client, key)); } } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java index aad2ae44a4b63..b79ed3ebd2c23 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java @@ -25,7 +25,7 @@ public void createAllNeedPath(final String key, final String value, final Create super.createAllNeedPath(key, value, createMode); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AllAsyncRetryStrategy SessionExpiredException CreateAllNeedOperation:{}", key); - AsyncRetryCenter.INSTANCE.add(new CreateAllNeedOperation(provider, key, value, createMode)); + AsyncRetryCenter.INSTANCE.add(new CreateAllNeedOperation(client, key, value, createMode)); } } @@ -35,7 +35,7 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup super.deleteAllChildren(key); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AllAsyncRetryStrategy SessionExpiredException deleteAllChildren:{}", key); - AsyncRetryCenter.INSTANCE.add(new DeleteAllChildrenOperation(provider, key)); + AsyncRetryCenter.INSTANCE.add(new DeleteAllChildrenOperation(client, key)); } } @@ -45,7 +45,7 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr super.deleteCurrentBranch(key); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AllAsyncRetryStrategy SessionExpiredException deleteCurrentBranch:{}", key); - AsyncRetryCenter.INSTANCE.add(new DeleteCurrentBranchOperation(provider, key)); + AsyncRetryCenter.INSTANCE.add(new DeleteCurrentBranchOperation(client, key)); } } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java index 99cf8d3bfa9cf..4be99c4b0fa50 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java @@ -30,7 +30,7 @@ public void createCurrentOnly(final String key, final String value, final Create provider.createCurrentOnly(path, value, createMode); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AsyncRetryStrategy SessionExpiredException createCurrentOnly:{}", path); - AsyncRetryCenter.INSTANCE.add(new CreateCurrentOperation(provider, path, value, createMode)); + AsyncRetryCenter.INSTANCE.add(new CreateCurrentOperation(client, path, value, createMode)); } } @@ -41,7 +41,7 @@ public void update(final String key, final String value) throws KeeperException, provider.update(path, value); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AsyncRetryStrategy SessionExpiredException update:{}", path); - AsyncRetryCenter.INSTANCE.add(new UpdateOperation(provider, path, value)); + AsyncRetryCenter.INSTANCE.add(new UpdateOperation(client, path, value)); } } @@ -52,7 +52,7 @@ public void deleteOnlyCurrent(final String key) throws KeeperException, Interrup provider.deleteOnlyCurrent(path); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AsyncRetryStrategy SessionExpiredException deleteOnlyCurrent:{}", path); - AsyncRetryCenter.INSTANCE.add(new DeleteCurrentOperation(provider, path)); + AsyncRetryCenter.INSTANCE.add(new DeleteCurrentOperation(client, path)); } } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java index 27bb0f5b01a9c..f771719e45662 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.zookeeper.strategy; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.RetryCount; import com.saaavsaaa.client.retry.RetryPolicy; @@ -20,14 +21,23 @@ public class SyncRetryStrategy extends UsualStrategy{ private static final Logger logger = LoggerFactory.getLogger(SyncRetryStrategy.class); protected final RetryPolicy retryPolicy; + protected final IClient client; public SyncRetryStrategy(final BaseClient client, final boolean watched){ super(new BaseProvider(client, watched)); + this.client = client; retryPolicy = client.getContext().getRetryPolicy(); } @Override public byte[] getData(final String key) throws KeeperException, InterruptedException { + Callable callable = new Callable() { + @Override + public byte[] call() throws KeeperException, InterruptedException { + return provider.getData(provider.getRealPath(key)); + } + }; + String path = provider.getRealPath(key); try { return provider.getData(path); @@ -96,8 +106,9 @@ public void createCurrentOnly(final String key, final String value, final Create String path = provider.getRealPath(key); Callable callable = new Callable() { @Override - public void call() throws KeeperException, InterruptedException { + public Object call() throws KeeperException, InterruptedException { provider.createCurrentOnly(path, value, createMode); + return null; } }; /*try { diff --git a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java index 27adcc5c875e9..58b47ca5ad138 100644 --- a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java @@ -8,7 +8,6 @@ import com.saaavsaaa.client.zookeeper.TestSupport; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; -import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -24,6 +23,7 @@ */ public class AsyncRetryCenterTest { private IProvider provider; + private IClient client; @Before public void start() throws IOException, InterruptedException { @@ -35,7 +35,7 @@ public void start() throws IOException, InterruptedException { protected IClient createClient() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); Listener listener = TestSupport.buildListener(); - IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); ((BaseClient)client).useExecStrategy(StrategyType.ASYNC_RETRY); return client; } @@ -55,7 +55,7 @@ public void nothing(){ public void create() throws InterruptedException, KeeperException { String key = "a"; String value = "bbb11"; - AsyncRetryCenter.INSTANCE.add(new TestCreateCurrentOperation(provider, key, value, CreateMode.PERSISTENT)); + AsyncRetryCenter.INSTANCE.add(new TestCreateCurrentOperation(client, key, value, CreateMode.PERSISTENT)); Thread.sleep(1000); String path = PathUtil.getRealPath(TestSupport.ROOT, key); assert provider.checkExists(path); diff --git a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java index 5d596d7bfc5e8..7b6240bed195a 100644 --- a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java +++ b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.retry; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -12,17 +12,16 @@ public class TestCreateCurrentOperation extends CreateCurrentOperation { private int count = 0; - public TestCreateCurrentOperation(IProvider provider, String key, String value, CreateMode createMode) { - super(provider, key, value, createMode); + public TestCreateCurrentOperation(final IClient client, String key, String value, CreateMode createMode) { + super(client, key, value, createMode); } @Override - public boolean execute() throws KeeperException, InterruptedException { + public void execute() throws KeeperException, InterruptedException { if (count < 2){ count++; - return false; + throw new KeeperException.SessionExpiredException(); } super.execute(); - return true; } } From c4b8b95bf390369628c8cac545a95507831b6e99 Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 23 May 2018 19:04:28 +0800 Subject: [PATCH 118/351] clearing transient save --- .../com/saaavsaaa/client/action/IAction.java | 1 - .../com/saaavsaaa/client/action/IClient.java | 2 + .../saaavsaaa/client/action/IProvider.java | 16 +++++- .../com/saaavsaaa/client/cache/PathTree.java | 29 ++++++---- .../client/election/LeaderElection.java | 6 +- .../client/section/ClientContext.java | 20 +++++-- .../saaavsaaa/client/section/Connection.java | 6 +- .../client/zookeeper/CacheClient.java | 10 ++-- .../client/zookeeper/ClientFactory.java | 2 +- .../client/zookeeper/UsualClient.java | 40 ++++++++++--- .../client/zookeeper/base/BaseClient.java | 22 +++---- .../zookeeper/base/BaseClientFactory.java | 5 +- .../client/zookeeper/base/BaseOperation.java | 9 +-- .../client/zookeeper/base/BaseProvider.java | 57 ++++++------------- .../client/zookeeper/base/BaseStrategy.java | 3 + .../operation/CreateAllNeedOperation.java | 7 ++- .../operation/CreateCurrentOperation.java | 9 +-- .../operation/DeleteAllChildrenOperation.java | 11 ++-- .../DeleteCurrentBranchOperation.java | 11 ++-- .../operation/DeleteCurrentOperation.java | 12 ++-- .../zookeeper/operation/UpdateOperation.java | 10 ++-- .../zookeeper/provider/RetryProvider.java | 51 +++-------------- .../strategy/AllAsyncRetryStrategy.java | 12 ++-- .../strategy/AsyncRetryStrategy.java | 18 +++--- .../strategy/ContentionStrategy.java | 24 ++++---- .../zookeeper/strategy/SyncRetryStrategy.java | 15 +++-- .../strategy/TransactionContendStrategy.java | 14 +++-- .../zookeeper/strategy/UsualStrategy.java | 15 ++--- .../client/retry/AsyncRetryCenterTest.java | 18 +++--- .../retry/TestCreateCurrentOperation.java | 7 +-- 30 files changed, 225 insertions(+), 237 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/action/IAction.java b/src/main/java/com/saaavsaaa/client/action/IAction.java index c3cd562b3556a..cd918ce6acefe 100644 --- a/src/main/java/com/saaavsaaa/client/action/IAction.java +++ b/src/main/java/com/saaavsaaa/client/action/IAction.java @@ -22,5 +22,4 @@ public interface IAction { void update(final String key, final String value) throws KeeperException, InterruptedException; void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException; void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException; - ZKTransaction transaction(); } diff --git a/src/main/java/com/saaavsaaa/client/action/IClient.java b/src/main/java/com/saaavsaaa/client/action/IClient.java index ae540eb26595e..44b5a48061a92 100644 --- a/src/main/java/com/saaavsaaa/client/action/IClient.java +++ b/src/main/java/com/saaavsaaa/client/action/IClient.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.action; import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.Watcher; import java.io.IOException; @@ -14,6 +15,7 @@ public interface IClient extends IAction, IGroupAction{ Watcher registerWatch(String key, Listener listener); void unregisterWatch(String key); + ZKTransaction transaction(); /* void createNamespace(); void deleteNamespace(); diff --git a/src/main/java/com/saaavsaaa/client/action/IProvider.java b/src/main/java/com/saaavsaaa/client/action/IProvider.java index 8ebc6c78270f3..074d988392429 100644 --- a/src/main/java/com/saaavsaaa/client/action/IProvider.java +++ b/src/main/java/com/saaavsaaa/client/action/IProvider.java @@ -3,8 +3,10 @@ import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; +import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; import java.util.List; import java.util.Stack; @@ -12,12 +14,22 @@ /** * Created by aaa */ -public interface IProvider extends IAction { +public interface IProvider { + String getDataString(final String key) throws KeeperException, InterruptedException; + byte[] getData(final String key) throws KeeperException, InterruptedException; + void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException; + boolean exists(final String key) throws KeeperException, InterruptedException; + boolean exists(final String key, final Watcher watcher) throws KeeperException, InterruptedException; + List getChildren(final String key) throws KeeperException, InterruptedException; + void create(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException; + void update(final String key, final String value) throws KeeperException, InterruptedException; + void delete(final String key) throws KeeperException, InterruptedException; + void delete(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException; + String getRealPath(final String path); List getNecessaryPaths(final String key); Stack getDeletingPaths(final String key); void executeContention(final LeaderElection election) throws KeeperException, InterruptedException; - void watch(final String key, final Listener listener); void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException; } diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index 2e730de6e76b0..f39da00d1a168 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -1,11 +1,13 @@ package com.saaavsaaa.client.cache; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.section.ClientTask; import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.utility.Properties; +import com.saaavsaaa.client.zookeeper.base.BaseClient; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.common.PathUtils; @@ -22,19 +24,22 @@ /* * Created by aaa + * todo provider */ public final class PathTree { private static final Logger logger = LoggerFactory.getLogger(PathTree.class); private final AtomicReference rootNode = new AtomicReference<>(); private boolean executorStart = false; private ScheduledExecutorService cacheService; + private final IClient client; private final IProvider provider; private PathStatus Status; - public PathTree(final String root, final IProvider provider) { + public PathTree(final String root, final IClient client) { this.rootNode.set(new PathNode(root)); this.Status = PathStatus.RELEASE; - this.provider = provider; + this.client = client; + this.provider = ((BaseClient)client).getContext().getProvider(); } public synchronized void loading() throws KeeperException, InterruptedException { @@ -44,8 +49,8 @@ public synchronized void loading() throws KeeperException, InterruptedException PathNode newRoot = new PathNode(rootNode.get().getKey()); List children = provider.getChildren(rootNode.get().getKey()); - children.remove(provider.getRealPath(Constants.CHANGING_KEY)); - this.attechIntoNode(children, newRoot, provider); + children.remove(PathUtil.getRealPath(rootNode.get().getKey(), Constants.CHANGING_KEY)); + this.attechIntoNode(children, newRoot); rootNode.set(newRoot); this.setStatus(PathStatus.RELEASE); @@ -61,7 +66,7 @@ public synchronized void loading() throws KeeperException, InterruptedException } } - private void attechIntoNode(final List children, final PathNode pathNode, final IProvider provider) throws KeeperException, InterruptedException { + private void attechIntoNode(final List children, final PathNode pathNode) throws KeeperException, InterruptedException { logger.debug("attechIntoNode children:{}", children); if (children.isEmpty()){ logger.info("attechIntoNode there are no children"); @@ -72,7 +77,7 @@ private void attechIntoNode(final List children, final PathNode pathNode PathNode current = new PathNode(PathUtil.checkPath(child), provider.getData(childPath)); pathNode.attachChild(current); List subs = provider.getChildren(childPath); - this.attechIntoNode(subs, current, provider); + this.attechIntoNode(subs, current); } } @@ -86,12 +91,16 @@ public void refreshPeriodic(final long period){ } logger.debug("refreshPeriodic:{}", period); cacheService = Executors.newSingleThreadScheduledExecutor(); - cacheService.scheduleAtFixedRate(new ClientTask(provider) { + cacheService.scheduleAtFixedRate(new Runnable() { @Override - public void run(IProvider provider) throws KeeperException, InterruptedException { + public void run() { logger.debug("cacheService run:{}", getStatus()); if (PathStatus.RELEASE == getStatus()) { - loading(); + try { + loading(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } } } }, Properties.INSTANCE.getThreadInitialDelay(), threadPeriod, TimeUnit.MILLISECONDS); @@ -137,7 +146,7 @@ public void process(WatchedEvent event) { }; } logger.debug("PathTree Watch:{}", rootNode.get().getKey()); - provider.watch(rootNode.get().getKey(), listener); + client.registerWatch(rootNode.get().getKey(), listener); } public PathStatus getStatus() { diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index deefc824be6cd..198cebac035b4 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -28,12 +28,12 @@ public LeaderElection(){ private boolean contend(final String node, final IProvider provider, final Listener listener) throws KeeperException, InterruptedException { boolean success = false; try { - provider.createCurrentOnly(node, Properties.INSTANCE.getClientId(), CreateMode.EPHEMERAL); // todo EPHEMERAL_SEQUENTIAL check index value + provider.create(node, Properties.INSTANCE.getClientId(), CreateMode.EPHEMERAL); // todo EPHEMERAL_SEQUENTIAL check index value success = true; } catch (KeeperException.NodeExistsException e) { logger.info("contend not success"); // TODO: or changing_key node value == current client id - provider.checkExists(node, WatcherCreator.deleteWatcher(node, listener)); + provider.exists(node, WatcherCreator.deleteWatcher(node, listener)); } return success; } @@ -69,7 +69,7 @@ public void process(WatchedEvent event) { } catch (Exception ee){ logger.error("action Exception executeContention:{}", ee.getMessage(), ee); } - provider.deleteOnlyCurrent(contendNode); + provider.delete(contendNode); } } diff --git a/src/main/java/com/saaavsaaa/client/section/ClientContext.java b/src/main/java/com/saaavsaaa/client/section/ClientContext.java index 2e377cd298bd0..ab36865d1731b 100644 --- a/src/main/java/com/saaavsaaa/client/section/ClientContext.java +++ b/src/main/java/com/saaavsaaa/client/section/ClientContext.java @@ -1,22 +1,32 @@ package com.saaavsaaa.client.section; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.RetryPolicy; +import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; /** * Created by aaa */ public final class ClientContext { private final RetryPolicy retryPolicy; + private final BaseClientFactory clientFactory; + private final IProvider provider; - public ClientContext(final RetryPolicy retryPolicy) { + public ClientContext(final RetryPolicy retryPolicy, final BaseClientFactory clientFactory, final IProvider provider) { this.retryPolicy = retryPolicy; - } - - public ClientContext() { - retryPolicy = null; + this.clientFactory = clientFactory; + this.provider = provider; } public RetryPolicy getRetryPolicy() { return retryPolicy; } + + public BaseClientFactory getClientFactory() { + return clientFactory; + } + + public IProvider getProvider() { + return provider; + } } diff --git a/src/main/java/com/saaavsaaa/client/section/Connection.java b/src/main/java/com/saaavsaaa/client/section/Connection.java index f36558b8a06aa..8399347749d62 100644 --- a/src/main/java/com/saaavsaaa/client/section/Connection.java +++ b/src/main/java/com/saaavsaaa/client/section/Connection.java @@ -14,7 +14,7 @@ public class Connection { //is need reset private static final Map exceptionResets = new ConcurrentHashMap<>(); - private final IClient client; + private final ClientContext context; static { exceptionResets.put(KeeperException.Code.SESSIONEXPIRED.intValue(), true); @@ -23,8 +23,8 @@ public class Connection { exceptionResets.put(KeeperException.Code.OPERATIONTIMEOUT.intValue(), false); } - public Connection(final IClient client){ - this.client = client; + public Connection(final ClientContext context){ + this.context = context; } public void check(KeeperException e) throws KeeperException { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index b39a0e3f45879..085324452929b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -38,7 +38,7 @@ void useCacheStrategy(CacheStrategy cacheStrategy) throws KeeperException, Inter logger.debug("use cache strategy:{}", cacheStrategy); switch (cacheStrategy){ case WATCH:{ - pathTree = new PathTree(rootNode, strategy.getProvider()); + pathTree = new PathTree(rootNode, this); pathTree.watch(null); return; } @@ -58,7 +58,7 @@ public PathTree loadPathTree() throws KeeperException, InterruptedException { } public PathTree loadPathTree(final String treeRoot) throws KeeperException, InterruptedException { - PathTree tree = new PathTree(treeRoot, strategy.getProvider()); + PathTree tree = new PathTree(treeRoot, this); logger.debug("load path tree:{}", treeRoot); tree.loading(); return tree; @@ -66,20 +66,20 @@ public PathTree loadPathTree(final String treeRoot) throws KeeperException, Inte @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - strategy.createCurrentOnly(key, value, createMode); + super.createCurrentOnly(key, value, createMode); pathTree.put(PathUtil.getRealPath(rootNode, key), value); } @Override public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - strategy.deleteOnlyCurrent(key); + super.deleteOnlyCurrent(key); pathTree.delete(PathUtil.getRealPath(rootNode, key)); } @Override public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - strategy.deleteOnlyCurrent(key, callback, ctx); + super.deleteOnlyCurrent(key, callback, ctx); pathTree.delete(PathUtil.getRealPath(rootNode, key)); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index e6f5566da4d64..4a3ca48f290c2 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -65,7 +65,7 @@ public ClientFactory authorization(final String scheme, final byte[] auth){ } public ClientFactory setRetryPolicy(final RetryPolicy retryPolicy){ - this.context = new ClientContext(retryPolicy); + this.retryPolicy = retryPolicy; return this; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index a5d2277e74f01..d0ae2a7efe143 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.action.IExecStrategy; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.strategy.*; @@ -40,30 +41,31 @@ public synchronized void useExecStrategy(StrategyType strategyType) { return; } + IProvider provider = getContext().getProvider(); switch (strategyType){ case USUAL:{ - strategy = new UsualStrategy(new BaseProvider(this, watched)); + strategy = new UsualStrategy(provider); break; } case CONTEND:{ - strategy = new ContentionStrategy(new BaseProvider(this, watched)); + strategy = new ContentionStrategy(provider); break; } case SYNC_RETRY:{ - strategy = new SyncRetryStrategy(this, watched); + strategy = new SyncRetryStrategy(provider, context.getRetryPolicy()); break; } case ASYNC_RETRY:{ - strategy = new AsyncRetryStrategy(this, watched); + strategy = new AsyncRetryStrategy(provider, context); break; } case ALL_ASYNC_RETRY:{ - strategy = new AllAsyncRetryStrategy(this, watched); + strategy = new AllAsyncRetryStrategy(provider, context); break; } default:{ - strategy = new UsualStrategy(new BaseProvider(this, watched)); + strategy = new UsualStrategy(provider); break; } } @@ -103,11 +105,19 @@ public List getChildren(final String key) throws KeeperException, Interr @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + this.createNamespace(); + if (rootNode.equals(key)){ + return; + } strategy.createCurrentOnly(key, value, createMode); } @Override public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + this.createNamespace(); + if (rootNode.equals(key)){ + return; + } strategy.createAllNeedPath(key, value, createMode); } @@ -118,26 +128,42 @@ public void update(final String key, final String value) throws KeeperException, @Override public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + if (rootNode.equals(key)){ + deleteNamespace(); + return; + } strategy.deleteOnlyCurrent(key); } @Override public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + if (rootNode.equals(key)){ + deleteNamespace(); + return; + } strategy.deleteOnlyCurrent(key, callback, ctx); } @Override public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { strategy.deleteAllChildren(key); + if (rootNode.equals(key)){ + rootExist = false; + logger.debug("deleteAllChildren delete root:{}", rootNode); + } } @Override public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { strategy.deleteCurrentBranch(key); + if (!strategy.checkExists(rootNode)){ + rootExist = false; + logger.debug("deleteCurrentBranch delete root:{}", rootNode); + } } @Override public ZKTransaction transaction() { - return strategy.transaction(); + return new ZKTransaction(rootNode, zooKeeper); } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index 3f1fac6702c1b..7aa262f54e771 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IClient; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.RetryPolicy; import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.utility.PathUtil; @@ -41,7 +42,7 @@ public abstract class BaseClient implements IClient { protected ZooKeeper zooKeeper; protected List authorities; - private BaseClientFactory clientFactory; +// private BaseClientFactory clientFactory; protected BaseClient(final String servers, final int sessionTimeoutMilliseconds) { this.servers = servers; @@ -135,7 +136,7 @@ public void unregisterWatch(final String key){ } } - void createNamespace() throws KeeperException, InterruptedException { + protected void createNamespace() throws KeeperException, InterruptedException { createNamespace(Constants.NOTHING_DATA); } @@ -162,10 +163,10 @@ public void process(WatchedEvent event) { logger.debug("created root:{}", rootNode); } - void deleteNamespace() throws KeeperException, InterruptedException { + protected void deleteNamespace() throws KeeperException, InterruptedException { zooKeeper.delete(rootNode, Constants.VERSION); rootExist = false; - logger.debug("create root:{},rootExist:{}", rootNode, rootExist); + logger.debug("delete root:{},rootExist:{}", rootNode, rootExist); } void setRootNode(final String rootNode) { @@ -178,21 +179,14 @@ void setAuthorities(final String scheme, final byte[] auth) { this.authorities = ZooDefs.Ids.CREATOR_ALL_ACL; } - void setContext(final ClientContext context){ - this.context = context; + void setContext(final RetryPolicy retryPolicy, final BaseClientFactory clientFactory){ + this.context = new ClientContext(retryPolicy, clientFactory, new BaseProvider(rootNode, zooKeeper, watched, authorities)); } public ClientContext getContext(){ - if (context == null){ - context = new ClientContext(); - } return context; } - void setClientFactory(BaseClientFactory clientFactory) { - this.clientFactory = clientFactory; - } - BaseClientFactory getClientFactory() { - return clientFactory; + return context.getClientFactory(); } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java index 41064f9e53195..58a5312a4e7e4 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java @@ -16,16 +16,15 @@ public abstract class BaseClientFactory { protected String namespace; protected String scheme; protected byte[] auth; - protected ClientContext context; + protected RetryPolicy retryPolicy; protected String servers; protected int sessionTimeoutMilliseconds; public synchronized IClient start() throws IOException, InterruptedException { - client.setClientFactory(this); client.setRootNode(namespace); client.setAuthorities(scheme , auth); - client.setContext(context); + client.setContext(retryPolicy, this); // wait expand client.start(); if (globalListener != null) { client.registerWatch(globalListener); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java index d43d361a003e1..c11f10fe974d6 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java @@ -3,6 +3,7 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.DelayRetryExecution; +import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.KeeperException; @@ -17,11 +18,11 @@ */ public abstract class BaseOperation implements Delayed { private static final Logger logger = LoggerFactory.getLogger(BaseOperation.class); - protected final IClient client; + protected final ClientContext context; protected DelayRetryExecution retryExecution; - protected BaseOperation(final IClient client) { - this.client = client; + protected BaseOperation(final ClientContext context) { + this.context = context; } public void setRetrial(final DelayRetryExecution retryExecution){ @@ -55,7 +56,7 @@ public boolean executeOperation() throws KeeperException, InterruptedException { execute(); result = true; } catch (KeeperException ee) { - new Connection(client).check(ee); + new Connection(context).check(ee); result = false; } if (!result && retryExecution.hasNext()){ diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java index ac8bc3bd81b1e..93a8205d2be98 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java @@ -1,11 +1,9 @@ package com.saaavsaaa.client.zookeeper.base; -import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; @@ -20,20 +18,14 @@ */ public class BaseProvider implements IProvider { private static final Logger logger = LoggerFactory.getLogger(BaseProvider.class); - protected final BaseClient client; protected final ZooKeeper zooKeeper; protected final boolean watched; protected final List authorities; protected final String rootNode; - public BaseProvider(IClient client, boolean watched) { - this(((BaseClient)client).rootNode, ((BaseClient)client), watched, ((BaseClient)client).authorities); - } - - private BaseProvider(final String rootNode, final BaseClient client, final boolean watched, final List authorities){ + public BaseProvider(final String rootNode, final ZooKeeper zooKeeper, final boolean watched, final List authorities){ this.rootNode = rootNode; - this.client = client; - this.zooKeeper = client.getZooKeeper(); + this.zooKeeper = zooKeeper; this.watched = watched; this.authorities = authorities; } @@ -53,12 +45,12 @@ public void getData(final String key, final AsyncCallback.DataCallback callback, } @Override - public boolean checkExists(final String key) throws KeeperException, InterruptedException { + public boolean exists(final String key) throws KeeperException, InterruptedException { return null != zooKeeper.exists(key, watched); } @Override - public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { + public boolean exists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { return null != zooKeeper.exists(key, watcher); } @@ -68,21 +60,17 @@ public List getChildren(final String key) throws KeeperException, Interr } @Override - public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - client.createNamespace(); - if (rootNode.equals(key)){ - return; - } + public void create(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { try { zooKeeper.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); logger.debug("BaseProvider createCurrentOnly:{}", key); } catch (KeeperException.NoNodeException e) { logger.error("BaseProvider createCurrentOnly:{}", e.getMessage(), e); // I don't know whether it will happen or not, if root watcher don't update rootExist timely - if (e.getMessage().contains(key)) { - logger.info("BaseProvider createCurrentOnly rootExist:{}", client.rootExist); + if (!exists(rootNode)){ + logger.info("BaseProvider createCurrentOnly root not exist"); Thread.sleep(50); - this.createCurrentOnly(key, value, createMode); + this.create(key, value, createMode); } } } @@ -93,21 +81,15 @@ public void update(final String key, final String value) throws KeeperException, } @Override - public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + public void delete(final String key) throws KeeperException, InterruptedException { zooKeeper.delete(key, Constants.VERSION); logger.debug("BaseProvider deleteOnlyCurrent:{}", key); - if (rootNode.equals(key)){ - client.rootExist = false; //protected - } } @Override - public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + public void delete(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { zooKeeper.delete(key, Constants.VERSION, callback, ctx); logger.debug("BaseProvider deleteOnlyCurrent:{},ctx:{}", key, ctx); - if (rootNode.equals(key)){ - client.rootExist = false; - } } @@ -137,23 +119,16 @@ public void executeContention(final String nodeBeCompete, final LeaderElection e election.executeContention(rootNode, this); } - @Override - public void watch(final String key, final Listener listener) { - client.registerWatch(getRealPath(key), listener); - } - @Override public void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { - client.createNamespace(); - if (rootNode.equals(key)){ - logger.info("BaseProvider createInTransaction rootNode:{}", key); - return; - } transaction.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); } - @Override - public ZKTransaction transaction() { - return new ZKTransaction(rootNode, zooKeeper); + public String getRootNode(){ + return rootNode; + } + + public ZooKeeper getZooKeeper(){ + return zooKeeper; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java index 89e615c551bb5..d3856ae75b1a1 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java @@ -2,6 +2,9 @@ import com.saaavsaaa.client.action.IExecStrategy; import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; +import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; /** diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java index 1a000521f694b..4157aaa7920e5 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java @@ -2,6 +2,7 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import com.saaavsaaa.client.zookeeper.base.BaseProvider; @@ -17,8 +18,8 @@ public class CreateAllNeedOperation extends BaseOperation { private final String value; private final CreateMode createMode; - public CreateAllNeedOperation(final IClient client, final String key, final String value, final CreateMode createMode) { - super(client); + public CreateAllNeedOperation(final ClientContext context, final String key, final String value, final CreateMode createMode) { + super(context); this.key = key; this.value = value; this.createMode = createMode; @@ -26,7 +27,7 @@ public CreateAllNeedOperation(final IClient client, final String key, final Stri @Override protected void execute() throws KeeperException, InterruptedException { - new UsualStrategy(new BaseProvider(client, false)).createAllNeedPath(key, value, createMode); + new UsualStrategy(context.getProvider()).createAllNeedPath(key, value, createMode); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java index 733ec978c28e6..762ea216c3909 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java @@ -2,6 +2,7 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import com.saaavsaaa.client.zookeeper.base.BaseProvider; @@ -16,8 +17,8 @@ public class CreateCurrentOperation extends BaseOperation { private final String value; private final CreateMode createMode; - public CreateCurrentOperation(final IClient client, final String key, final String value, final CreateMode createMode) { - super(client); + public CreateCurrentOperation(final ClientContext context, final String key, final String value, final CreateMode createMode) { + super(context); this.key = key; this.value = value; this.createMode = createMode; @@ -25,8 +26,8 @@ public CreateCurrentOperation(final IClient client, final String key, final Stri @Override public void execute() throws KeeperException, InterruptedException { - IProvider provider = new BaseProvider(client, false); - provider.createCurrentOnly(provider.getRealPath(key), value, createMode); + IProvider provider = context.getProvider(); + provider.create(provider.getRealPath(key), value, createMode); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java index 7b80c20f6e608..a20e8e1baa714 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java @@ -1,10 +1,7 @@ package com.saaavsaaa.client.zookeeper.operation; -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.section.Connection; +import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.KeeperException; @@ -14,14 +11,14 @@ public class DeleteAllChildrenOperation extends BaseOperation { private final String key; - public DeleteAllChildrenOperation(final IClient client, final String key) { - super(client); + public DeleteAllChildrenOperation(final ClientContext context, final String key) { + super(context); this.key = key; } @Override protected void execute() throws KeeperException, InterruptedException { - new UsualStrategy(new BaseProvider(client, false)).deleteAllChildren(key); + new UsualStrategy(context.getProvider()).deleteAllChildren(key); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java index 9559382b40dad..918c4a6c5b18a 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java @@ -1,10 +1,7 @@ package com.saaavsaaa.client.zookeeper.operation; -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.section.Connection; +import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.KeeperException; @@ -14,14 +11,14 @@ public class DeleteCurrentBranchOperation extends BaseOperation { private final String key; - public DeleteCurrentBranchOperation(final IClient client, final String key) { - super(client); + public DeleteCurrentBranchOperation(final ClientContext context, final String key) { + super(context); this.key = key; } @Override protected void execute() throws KeeperException, InterruptedException { - new UsualStrategy(new BaseProvider(client, false)).deleteCurrentBranch(key); + new UsualStrategy(context.getProvider()).deleteCurrentBranch(key); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java index 0f50578992aac..81d45ecb4185b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java @@ -1,10 +1,8 @@ package com.saaavsaaa.client.zookeeper.operation; -import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.section.Connection; +import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; import org.apache.zookeeper.KeeperException; /** @@ -12,15 +10,15 @@ */ public class DeleteCurrentOperation extends BaseOperation { private final String key; - public DeleteCurrentOperation(final IClient client, final String key) { - super(client); + public DeleteCurrentOperation(final ClientContext context, final String key) { + super(context); this.key = key; } @Override protected void execute() throws KeeperException, InterruptedException { - IProvider provider = new BaseProvider(client, false); - provider.deleteOnlyCurrent(provider.getRealPath(key)); + IProvider provider = context.getProvider(); + provider.delete(provider.getRealPath(key)); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java index ef16090ff1ed6..bc0be7ebfb9b5 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java @@ -1,10 +1,8 @@ package com.saaavsaaa.client.zookeeper.operation; -import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.section.Connection; +import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; import org.apache.zookeeper.KeeperException; /** @@ -14,15 +12,15 @@ public class UpdateOperation extends BaseOperation { private final String key; private final String value; - public UpdateOperation(final IClient client, final String key, final String value) { - super(client); + public UpdateOperation(final ClientContext context, final String key, final String value) { + super(context); this.key = key; this.value = value; } @Override protected void execute() throws KeeperException, InterruptedException { - IProvider provider = new BaseProvider(client, false); + IProvider provider = context.getProvider(); provider.update(provider.getRealPath(key), value); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java index b3d306f2d4356..27082ea9aa358 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java @@ -10,6 +10,7 @@ import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.ACL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,14 +24,11 @@ public class RetryProvider extends BaseProvider { private static final Logger logger = LoggerFactory.getLogger(RetryProvider.class); - RetryProvider(BaseClient client, boolean watched) { - super(client, watched); - } - - RetryProvider(String rootNode, BaseClient client, boolean watched, List authorities) { - super(client, watched); + public RetryProvider(String rootNode, ZooKeeper zooKeeper, boolean watched, List authorities) { + super(rootNode, zooKeeper, watched, authorities); RetryCount.INSTANCE.start(); } + // block @Override public byte[] getData(final String key) throws KeeperException, InterruptedException { @@ -48,13 +46,13 @@ public byte[] getData(final String key) throws KeeperException, InterruptedExcep } @Override - public boolean checkExists(final String key) throws KeeperException, InterruptedException { + public boolean exists(final String key) throws KeeperException, InterruptedException { try { return null != zooKeeper.exists(key, watched); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException checkExists:{}", key); if (RetryCount.INSTANCE.continueExecute()) { - boolean result = checkExists(key); + boolean result = exists(key); RetryCount.INSTANCE.reset(); return result; } @@ -63,13 +61,13 @@ public boolean checkExists(final String key) throws KeeperException, Interrupted } @Override - public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { + public boolean exists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { try { return null != zooKeeper.exists(key, watcher); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException checkExists:{}", key); if (RetryCount.INSTANCE.continueExecute()) { - boolean result = checkExists(key, watcher); + boolean result = exists(key, watcher); RetryCount.INSTANCE.reset(); return result; } @@ -91,37 +89,4 @@ public List getChildren(final String key) throws KeeperException, Interr throw ee; } } - - // without block - @Override - public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - try { - super.createCurrentOnly(key, value, createMode); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryProvider SessionExpiredException createCurrentOnly:{}", key); - AsyncRetryCenter.INSTANCE.add(new CreateCurrentOperation(client, key, value, createMode)); - } catch (KeeperException.ConnectionLossException ee){ - - } - } - - @Override - public void update(final String key, final String value) throws KeeperException, InterruptedException { - try { - super.update(key, value); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryProvider SessionExpiredException update:{}", key); - AsyncRetryCenter.INSTANCE.add(new UpdateOperation(client, key, value)); - } - } - - @Override - public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - try { - super.deleteOnlyCurrent(key); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryProvider SessionExpiredException deleteOnlyCurrent:{}", key); - AsyncRetryCenter.INSTANCE.add(new DeleteCurrentOperation(client, key)); - } - } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java index b79ed3ebd2c23..bf723a299365b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java @@ -1,6 +1,8 @@ package com.saaavsaaa.client.zookeeper.strategy; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.AsyncRetryCenter; +import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.operation.CreateAllNeedOperation; import com.saaavsaaa.client.zookeeper.operation.DeleteAllChildrenOperation; @@ -15,8 +17,8 @@ */ public class AllAsyncRetryStrategy extends AsyncRetryStrategy { private static final Logger logger = LoggerFactory.getLogger(AllAsyncRetryStrategy.class); - public AllAsyncRetryStrategy(final BaseClient client, final boolean watched) { - super(client, watched); + public AllAsyncRetryStrategy(final IProvider provider, final ClientContext context) { + super(provider, context); } @Override @@ -25,7 +27,7 @@ public void createAllNeedPath(final String key, final String value, final Create super.createAllNeedPath(key, value, createMode); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AllAsyncRetryStrategy SessionExpiredException CreateAllNeedOperation:{}", key); - AsyncRetryCenter.INSTANCE.add(new CreateAllNeedOperation(client, key, value, createMode)); + AsyncRetryCenter.INSTANCE.add(new CreateAllNeedOperation(context, key, value, createMode)); } } @@ -35,7 +37,7 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup super.deleteAllChildren(key); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AllAsyncRetryStrategy SessionExpiredException deleteAllChildren:{}", key); - AsyncRetryCenter.INSTANCE.add(new DeleteAllChildrenOperation(client, key)); + AsyncRetryCenter.INSTANCE.add(new DeleteAllChildrenOperation(context, key)); } } @@ -45,7 +47,7 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr super.deleteCurrentBranch(key); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AllAsyncRetryStrategy SessionExpiredException deleteCurrentBranch:{}", key); - AsyncRetryCenter.INSTANCE.add(new DeleteCurrentBranchOperation(client, key)); + AsyncRetryCenter.INSTANCE.add(new DeleteCurrentBranchOperation(context, key)); } } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java index 4be99c4b0fa50..55f5a530358fe 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java @@ -1,6 +1,8 @@ package com.saaavsaaa.client.zookeeper.strategy; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.AsyncRetryCenter; +import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; @@ -16,9 +18,11 @@ */ public class AsyncRetryStrategy extends SyncRetryStrategy { private static final Logger logger = LoggerFactory.getLogger(AsyncRetryStrategy.class); + protected final ClientContext context; - public AsyncRetryStrategy(final BaseClient client, final boolean watched){ - super(client, watched); + public AsyncRetryStrategy(final IProvider provider, final ClientContext context){ + super(provider, context.getRetryPolicy()); + this.context = context; AsyncRetryCenter.INSTANCE.init(retryPolicy); AsyncRetryCenter.INSTANCE.start(); } @@ -27,10 +31,10 @@ public AsyncRetryStrategy(final BaseClient client, final boolean watched){ public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { String path = provider.getRealPath(key); try { - provider.createCurrentOnly(path, value, createMode); + provider.create(path, value, createMode); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AsyncRetryStrategy SessionExpiredException createCurrentOnly:{}", path); - AsyncRetryCenter.INSTANCE.add(new CreateCurrentOperation(client, path, value, createMode)); + AsyncRetryCenter.INSTANCE.add(new CreateCurrentOperation(context, path, value, createMode)); } } @@ -41,7 +45,7 @@ public void update(final String key, final String value) throws KeeperException, provider.update(path, value); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AsyncRetryStrategy SessionExpiredException update:{}", path); - AsyncRetryCenter.INSTANCE.add(new UpdateOperation(client, path, value)); + AsyncRetryCenter.INSTANCE.add(new UpdateOperation(context, path, value)); } } @@ -49,10 +53,10 @@ public void update(final String key, final String value) throws KeeperException, public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { String path = provider.getRealPath(key); try { - provider.deleteOnlyCurrent(path); + provider.delete(path); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AsyncRetryStrategy SessionExpiredException deleteOnlyCurrent:{}", path); - AsyncRetryCenter.INSTANCE.add(new DeleteCurrentOperation(client, path)); + AsyncRetryCenter.INSTANCE.add(new DeleteCurrentOperation(context, path)); } } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index 4b1bb0e7ff133..2956b86a399c0 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -52,7 +52,7 @@ private LeaderElection buildCreateElection(final String key, final String value, return new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - provider.createCurrentOnly(provider.getRealPath(key), value, createMode); + provider.create(provider.getRealPath(key), value, createMode); } @Override public void callback(){ @@ -99,7 +99,7 @@ private LeaderElection buildDeleteElection(final String key, final Callback call return new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - provider.deleteOnlyCurrent(provider.getRealPath(key)); + provider.delete(provider.getRealPath(key)); } @Override public void callback(){ @@ -115,7 +115,7 @@ public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback provider.executeContention(new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - provider.deleteOnlyCurrent(provider.getRealPath(key), callback, ctx); + provider.delete(provider.getRealPath(key), callback, ctx); logger.debug("ContentionStrategy deleteOnlyCurrent action:{},ctx:{}", key, ctx); } }); @@ -134,7 +134,7 @@ private LeaderElection buildCreateAllNeedElection(final String key, final String @Override public void action() throws KeeperException, InterruptedException { logger.debug("ContentionStrategy createAllNeedPath action:{}", key); - createBegin(key, value, createMode); + createBegin(provider.getRealPath(key), value, createMode); } @Override public void callback(){ @@ -147,20 +147,20 @@ public void callback(){ private void createBegin(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - provider.createCurrentOnly(key, value, createMode); + provider.create(key, value, createMode); return; } List nodes = provider.getNecessaryPaths(key); for (int i = 0; i < nodes.size(); i++) { - if (provider.checkExists(nodes.get(i))){ + if (provider.exists(nodes.get(i))){ logger.info("create node exist:{}", nodes.get(i)); continue; } logger.debug("create node not exist:", nodes.get(i)); if (i == nodes.size() - 1){ - provider.createCurrentOnly(nodes.get(i), value, createMode); + provider.create(nodes.get(i), value, createMode); } else { - provider.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); + provider.create(nodes.get(i), Constants.NOTHING_VALUE, createMode); } } } @@ -182,7 +182,7 @@ private void deleteChildren(final String key, final boolean deleteCurrentNode) t logger.debug("deleteChildren:{}", children); for (int i = 0; i < children.size(); i++) { String child = PathUtil.getRealPath(key, children.get(i)); - if (!provider.checkExists(child)){ + if (!provider.exists(child)){ logger.info("delete not exist:{}", child); continue; } @@ -190,7 +190,7 @@ private void deleteChildren(final String key, final boolean deleteCurrentNode) t deleteChildren(child, true); } if (deleteCurrentNode){ - provider.deleteOnlyCurrent(key); + provider.delete(key); } } @@ -211,9 +211,9 @@ private void deleteBranch(String key) throws KeeperException, InterruptedExcepti while (!pathStack.empty()){ String node = pathStack.pop(); // contrast cache - if (provider.checkExists(node)){ + if (provider.exists(node)){ try { - provider.deleteOnlyCurrent(node); + provider.delete(node); } catch (KeeperException.NotEmptyException ee){ logger.warn("deleteBranch {} exist other children:{}", node, this.getChildren(node)); logger.debug(ee.getMessage()); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java index f771719e45662..3b30bf9a7edf5 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java @@ -5,6 +5,7 @@ import com.saaavsaaa.client.retry.RetryCount; import com.saaavsaaa.client.retry.RetryPolicy; import com.saaavsaaa.client.section.Callable; +import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; import org.apache.zookeeper.CreateMode; @@ -21,12 +22,10 @@ public class SyncRetryStrategy extends UsualStrategy{ private static final Logger logger = LoggerFactory.getLogger(SyncRetryStrategy.class); protected final RetryPolicy retryPolicy; - protected final IClient client; - public SyncRetryStrategy(final BaseClient client, final boolean watched){ - super(new BaseProvider(client, watched)); - this.client = client; - retryPolicy = client.getContext().getRetryPolicy(); + public SyncRetryStrategy(final IProvider provider, final RetryPolicy retryPolicy) { + super(provider); + this.retryPolicy = retryPolicy; } @Override @@ -57,7 +56,7 @@ public byte[] call() throws KeeperException, InterruptedException { public boolean checkExists(final String key) throws KeeperException, InterruptedException { String path = provider.getRealPath(key); try { - return provider.checkExists(path); + return super.checkExists(path); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AsyncRetryStrategy KeeperException checkExists:{}", path); if (RetryCount.INSTANCE.continueExecute()) { @@ -73,7 +72,7 @@ public boolean checkExists(final String key) throws KeeperException, Interrupted public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { String path = provider.getRealPath(key); try { - return provider.checkExists(path, watcher); + return super.checkExists(path, watcher); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AsyncRetryStrategy KeeperException checkExists:{}", path); if (RetryCount.INSTANCE.continueExecute()) { @@ -107,7 +106,7 @@ public void createCurrentOnly(final String key, final String value, final Create Callable callable = new Callable() { @Override public Object call() throws KeeperException, InterruptedException { - provider.createCurrentOnly(path, value, createMode); + provider.create(path, value, createMode); return null; } }; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java index 7e553a394cc8a..95d31fe448f93 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java @@ -5,6 +5,7 @@ import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.section.Callback; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -16,6 +17,7 @@ /** * Created by aaa + * nothing use * since zookeeper 3.4.0 */ public class TransactionContendStrategy extends ContentionStrategy { @@ -37,7 +39,7 @@ private LeaderElection buildCreateAllNeedElection(final String key, final String @Override public void action() throws KeeperException, InterruptedException { logger.debug("ContentionStrategy createAllNeedPath action:{}", key); - ZKTransaction transaction = provider.transaction(); + ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getZooKeeper()); createBegin(key, value, createMode, transaction); transaction.commit(); } @@ -57,7 +59,7 @@ private void createBegin(final String key, final String value, final CreateMode } List nodes = provider.getNecessaryPaths(key); for (int i = 0; i < nodes.size(); i++) { - if (provider.checkExists(nodes.get(i))){ + if (provider.exists(nodes.get(i))){ logger.info("create node exist:{}", nodes.get(i)); continue; } @@ -75,7 +77,7 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup provider.executeContention(new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - ZKTransaction transaction = provider.transaction(); + ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getZooKeeper()); deleteChildren(provider.getRealPath(key), true, transaction); transaction.commit(); } @@ -87,7 +89,7 @@ private void deleteChildren(final String key, final boolean deleteCurrentNode, f List children = provider.getChildren(key); for (int i = 0; i < children.size(); i++) { String child = PathUtil.getRealPath(key, children.get(i)); - if (!provider.checkExists(child)){ + if (!provider.exists(child)){ logger.info("delete not exist:{}", child); continue; } @@ -104,7 +106,7 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr provider.executeContention(new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - ZKTransaction transaction = provider.transaction(); + ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getZooKeeper()); deleteBranch(provider.getRealPath(key), transaction); transaction.commit(); } @@ -127,7 +129,7 @@ private void deleteBranch(String key, final ZKTransaction transaction) throws Ke canDelete = false; } } - if (provider.checkExists(node) && canDelete){ + if (provider.exists(node) && canDelete){ transaction.delete(node, Constants.VERSION); } prePath = node; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java index e69874bf75c3f..78ca05d41a858 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java @@ -35,12 +35,12 @@ public void getData(final String key, final AsyncCallback.DataCallback callback, @Override public boolean checkExists(final String key) throws KeeperException, InterruptedException { - return provider.checkExists(provider.getRealPath(key)); + return provider.exists(provider.getRealPath(key)); } @Override public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { - return provider.checkExists(provider.getRealPath(key), watcher); + return provider.exists(provider.getRealPath(key), watcher); } @Override @@ -50,7 +50,7 @@ public List getChildren(final String key) throws KeeperException, Interr @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - provider.createCurrentOnly(provider.getRealPath(key), value, createMode); + provider.create(provider.getRealPath(key), value, createMode); } @Override @@ -60,12 +60,12 @@ public void update(final String key, final String value) throws KeeperException, @Override public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - provider.deleteOnlyCurrent(provider.getRealPath(key)); + provider.delete(provider.getRealPath(key)); } @Override public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - provider.deleteOnlyCurrent(provider.getRealPath(key), callback, ctx); + provider.delete(provider.getRealPath(key), callback, ctx); } @Override @@ -160,9 +160,4 @@ private void deleteRecursively(final String path) throws KeeperException, Interr return; } } - - @Override - public ZKTransaction transaction() { - return provider.transaction(); - } } diff --git a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java index 58b47ca5ad138..5b5666924a7fa 100644 --- a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java @@ -2,12 +2,12 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.zookeeper.ClientFactory; import com.saaavsaaa.client.zookeeper.TestSupport; import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -23,11 +23,11 @@ */ public class AsyncRetryCenterTest { private IProvider provider; - private IClient client; + private ClientContext context; @Before public void start() throws IOException, InterruptedException { - provider = new BaseProvider(createClient(), false); + provider = ((BaseClient)createClient()).getContext().getProvider(); AsyncRetryCenter.INSTANCE.init(new RetryPolicy(3, 3, 10)); AsyncRetryCenter.INSTANCE.start(); } @@ -35,7 +35,7 @@ public void start() throws IOException, InterruptedException { protected IClient createClient() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); Listener listener = TestSupport.buildListener(); - client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); ((BaseClient)client).useExecStrategy(StrategyType.ASYNC_RETRY); return client; } @@ -55,12 +55,12 @@ public void nothing(){ public void create() throws InterruptedException, KeeperException { String key = "a"; String value = "bbb11"; - AsyncRetryCenter.INSTANCE.add(new TestCreateCurrentOperation(client, key, value, CreateMode.PERSISTENT)); + AsyncRetryCenter.INSTANCE.add(new TestCreateCurrentOperation(context, key, value, CreateMode.PERSISTENT)); Thread.sleep(1000); String path = PathUtil.getRealPath(TestSupport.ROOT, key); - assert provider.checkExists(path); - provider.deleteOnlyCurrent(path); - provider.deleteOnlyCurrent(provider.getRealPath(TestSupport.ROOT)); - assert !provider.checkExists(path); + assert provider.exists(path); + provider.delete(path); + provider.delete(provider.getRealPath(TestSupport.ROOT)); + assert !provider.exists(path); } } diff --git a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java index 7b6240bed195a..06a9d13f65512 100644 --- a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java +++ b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java @@ -1,7 +1,6 @@ package com.saaavsaaa.client.retry; -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -12,8 +11,8 @@ public class TestCreateCurrentOperation extends CreateCurrentOperation { private int count = 0; - public TestCreateCurrentOperation(final IClient client, String key, String value, CreateMode createMode) { - super(client, key, value, createMode); + public TestCreateCurrentOperation(final ClientContext context, String key, String value, CreateMode createMode) { + super(context, key, value, createMode); } @Override From f49ccff9d26045500424ac7768683049bf87713f Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 23 May 2018 19:04:57 +0800 Subject: [PATCH 119/351] retry --- src/main/resources/client.properties | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/resources/client.properties b/src/main/resources/client.properties index dd54fbac2701b..a4df6245998e0 100644 --- a/src/main/resources/client.properties +++ b/src/main/resources/client.properties @@ -1,4 +1,5 @@ client.id=1 client.watch.on=up client.thread.delay=1000 -client.thread.period=3000 \ No newline at end of file +client.thread.period=3000 +node.election.count=3 \ No newline at end of file From 022e0379da87390693a3c93529103455f5398f88 Mon Sep 17 00:00:00 2001 From: linjiaqi Date: Wed, 23 May 2018 22:14:06 +0800 Subject: [PATCH 120/351] support proxy execute command --- .../common/CommandResponsePacketsHandler.java | 70 +- .../common/SQLPacketsBackendHandler.java | 219 ++-- .../backend/mysql/MySQLBackendHandler.java | 373 +++--- .../frontend/mysql/MySQLFrontendHandler.java | 186 +-- .../transport/mysql/packet/MySQLPacket.java | 92 +- .../mysql/packet/MySQLPacketPayload.java | 1109 +++++++++-------- .../command/CommandResponsePackets.java | 145 ++- .../text/fieldlist/ComFieldListPacket.java | 162 +-- .../command/text/query/ComQueryPacket.java | 188 +-- .../proxy/util/MySQLResultCache.java | 196 +-- .../proxy/util/SynchronizedFuture.java | 264 ++-- 11 files changed, 1578 insertions(+), 1426 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java index 3d09187d57563..38edadcc42fb6 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java @@ -1,35 +1,35 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.proxy.backend.common; - -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelInboundHandlerAdapter; -import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; - -/** - * SQL execute backend handler. - * - * @author wangkai - */ -public abstract class CommandResponsePacketsHandler extends ChannelInboundHandlerAdapter { - protected abstract void auth(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload); - - protected abstract void genericResponsePacket(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload); - - protected abstract void executeCommandResponsePackets(ChannelHandlerContext context, int header, MySQLPacketPayload mysqlPacketPayload); -} +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.backend.common; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; + +/** + * SQL execute backend handler. + * + * @author wangkai + */ +public abstract class CommandResponsePacketsHandler extends ChannelInboundHandlerAdapter { + protected abstract void auth(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload); + + protected abstract void genericResponsePacket(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload); + + protected abstract void executeCommandResponsePackets(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload); +} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java index 408456686288c..bef89cfa100a8 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java @@ -1,107 +1,112 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.proxy.backend.common; - -import io.netty.channel.Channel; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.parsing.parser.sql.SQLStatement; -import io.shardingsphere.core.routing.SQLExecutionUnit; -import io.shardingsphere.core.routing.SQLRouteResult; -import io.shardingsphere.core.routing.StatementRoutingEngine; -import io.shardingsphere.proxy.backend.ShardingProxyClient; -import io.shardingsphere.proxy.config.RuleRegistry; -import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; -import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; -import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket; -import io.shardingsphere.proxy.util.MySQLResultCache; -import io.shardingsphere.proxy.util.SynchronizedFuture; - -import java.util.List; -import java.util.concurrent.TimeUnit; - -/** - * SQL packets backend handler. - * - * @author wangkai - */ -public final class SQLPacketsBackendHandler extends SQLExecuteBackendHandler { - private SynchronizedFuture synchronizedFuture; - - private int connectionId; - - public SQLPacketsBackendHandler(final String sql, final int connectionId, final DatabaseType databaseType, final boolean showSQL) { - super(sql, databaseType, showSQL); - this.connectionId = connectionId; - } - - @Override - protected CommandResponsePackets executeForSharding() { - StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), getDatabaseType(), - isShowSQL()); - SQLRouteResult routeResult = routingEngine.route(getSql()); - if (routeResult.getExecutionUnits().isEmpty()) { - return new CommandResponsePackets(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); - } - synchronizedFuture = new SynchronizedFuture<>(routeResult.getExecutionUnits().size()); - MySQLResultCache.getInstance().put(connectionId, synchronizedFuture); - for (SQLExecutionUnit each : routeResult.getExecutionUnits()) { - execute(routeResult.getSqlStatement(), each.getDataSource(), each.getSqlUnit().getSql()); - } - //TODO timeout should be set. - List result = synchronizedFuture.get(30, TimeUnit.SECONDS); - MySQLResultCache.getInstance().delete(connectionId); - return merge(routeResult.getSqlStatement(), result); - } - - @Override - protected CommandResponsePackets executeForMasterSlave() { - throw new UnsupportedOperationException(); - } - - @Override - protected CommandResponsePackets execute(final SQLStatement sqlStatement, final String dataSourceName, final String sql) { - Channel channel = ShardingProxyClient.getInstance().getChannelMap().get(dataSourceName); - //MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); - switch (sqlStatement.getType()) { - case DQL: - executeQuery(channel, sql); - break; - case DML: - case DDL: - executeUpdate(channel, sql, sqlStatement); - break; - default: - executeCommon(channel, sql); - } - return null; - } - - //TODO - private void executeQuery(final Channel channel, final String sql) { - channel.writeAndFlush(""); - - } - - //TODO - private void executeUpdate(final Channel channel, final String sql, final SQLStatement sqlStatement) { - } - - //TODO - private void executeCommon(final Channel channel, final String sql) { - } -} +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.backend.common; + +import io.netty.channel.Channel; +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.parsing.parser.sql.SQLStatement; +import io.shardingsphere.core.routing.SQLExecutionUnit; +import io.shardingsphere.core.routing.SQLRouteResult; +import io.shardingsphere.core.routing.StatementRoutingEngine; +import io.shardingsphere.proxy.backend.ShardingProxyClient; +import io.shardingsphere.proxy.config.RuleRegistry; +import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; +import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket; +import io.shardingsphere.proxy.util.MySQLResultCache; +import io.shardingsphere.proxy.util.SynchronizedFuture; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * SQL packets backend handler. + * + * @author wangkai + */ +public final class SQLPacketsBackendHandler extends SQLExecuteBackendHandler { + private SynchronizedFuture synchronizedFuture; + + private final CommandPacket commandPacket; + + private int connectionId; + + public SQLPacketsBackendHandler(final CommandPacket commandPacket, final String sql, final int connectionId, final DatabaseType databaseType, final boolean showSQL) { + super(sql, databaseType, showSQL); + this.commandPacket = commandPacket; + this.connectionId = connectionId; + } + + @Override + protected CommandResponsePackets executeForSharding() { + StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), getDatabaseType(), + isShowSQL()); + SQLRouteResult routeResult = routingEngine.route(getSql()); + if (routeResult.getExecutionUnits().isEmpty()) { + return new CommandResponsePackets(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); + } + synchronizedFuture = new SynchronizedFuture<>(routeResult.getExecutionUnits().size()); + MySQLResultCache.getInstance().put(connectionId, synchronizedFuture); + for (SQLExecutionUnit each : routeResult.getExecutionUnits()) { + execute(routeResult.getSqlStatement(), each.getDataSource(), each.getSqlUnit().getSql()); + } + //TODO timeout should be set. + CommandResponsePackets result = synchronizedFuture.get(30, TimeUnit.SECONDS); + MySQLResultCache.getInstance().delete(connectionId); + return result; + } + + @Override + protected CommandResponsePackets executeForMasterSlave() { + throw new UnsupportedOperationException(); + } + + @Override + protected CommandResponsePackets execute(final SQLStatement sqlStatement, final String dataSourceName, final String sql) { + Channel channel = ShardingProxyClient.getInstance().getChannelMap().get(dataSourceName); + //MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); + switch (sqlStatement.getType()) { + case DQL: + executeQuery(channel, sql); + break; + case DML: + case DDL: + executeUpdate(channel, sql, sqlStatement); + break; + default: + executeCommon(channel, sql); + } + return null; + } + + private void executeQuery(final Channel channel, final String sql) { + MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); + channel.writeAndFlush(commandPacket); + } + + private void executeUpdate(final Channel channel, final String sql, final SQLStatement sqlStatement) { + MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); + channel.writeAndFlush(commandPacket); + } + + private void executeCommon(final Channel channel, final String sql) { + MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); + channel.writeAndFlush(commandPacket); + } +} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java index 41fe251e084d6..1555dac9c49e6 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java @@ -1,159 +1,214 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.proxy.backend.mysql; - -import com.google.common.primitives.Bytes; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.shardingsphere.proxy.backend.common.CommandResponsePacketsHandler; -import io.shardingsphere.proxy.transport.mysql.constant.CapabilityFlag; -import io.shardingsphere.proxy.transport.mysql.constant.ServerInfo; -import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; -import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket; -import io.shardingsphere.proxy.transport.mysql.packet.generic.ErrPacket; -import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket; -import io.shardingsphere.proxy.transport.mysql.packet.handshake.HandshakeResponse41Packet; -import io.shardingsphere.proxy.util.MySQLResultCache; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; - -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -/** - * Backend handler. - * - * @author wangkai - */ -@Slf4j -@RequiredArgsConstructor -public class MySQLBackendHandler extends CommandResponsePacketsHandler { - - private boolean authorized; - - private final String ip; - - private final int port; - - private final String database; - - private final String username; - - private final String password; - - @Override - public void channelRead(final ChannelHandlerContext context, final Object message) { - MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload((ByteBuf) message); - //int packetSize = mysqlPacketPayload.readInt3(); - int sequenceId = mysqlPacketPayload.readInt1(); - int header = mysqlPacketPayload.readInt1() & 0xFF; - if (OKPacket.HEADER == header || ErrPacket.HEADER == header || EofPacket.HEADER == header) { - genericResponsePacket(context, header, mysqlPacketPayload); - } else if (!authorized) { - auth(context, sequenceId, header, mysqlPacketPayload); - } else { - executeCommandResponsePackets(context, header, mysqlPacketPayload); - } - } - - @Override - protected void auth(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { - int protocolVersion = header; - String serverVersion = mysqlPacketPayload.readStringNul(); - int connectionId = mysqlPacketPayload.readInt4(); - MySQLResultCache.getInstance().putConnectionMap(context.channel().id().asShortText(), connectionId); - byte[] authPluginDataPart1 = mysqlPacketPayload.readStringNul().getBytes(); - int capabilityFlagsLower = mysqlPacketPayload.readInt2(); - int charset = mysqlPacketPayload.readInt1(); - int statusFlag = mysqlPacketPayload.readInt2(); - int capabilityFlagsUpper = mysqlPacketPayload.readInt2(); - int authPluginDataLength = mysqlPacketPayload.readInt1(); - mysqlPacketPayload.skipReserved(10); - byte[] authPluginDataPart2 = mysqlPacketPayload.readStringNul().getBytes(); - byte[] authPluginData = Bytes.concat(authPluginDataPart1, authPluginDataPart2); - //byte[] authResponse = byteXOR(SHA1(password.getBytes()), SHA1(Bytes.concat(authPluginData, SHA1(SHA1(password.getBytes()))))); - byte[] authResponse = securePasswordAuthentication(password.getBytes(), authPluginData); - //TODO maxSizePactet(16MB) should be set. - HandshakeResponse41Packet handshakeResponse41Packet = new HandshakeResponse41Packet(sequenceId + 1, CapabilityFlag.calculateHandshakeCapabilityFlagsLower(), 16777215, ServerInfo.CHARSET, - username, authResponse, database); - context.writeAndFlush(handshakeResponse41Packet); - } - - @Override - protected void genericResponsePacket(final ChannelHandlerContext context, final int header, final MySQLPacketPayload mysqlPacketPayload) { - switch (header) { - case OKPacket.HEADER: - if (!authorized) { - authorized = true; - } - long affectedRows = mysqlPacketPayload.readIntLenenc(); - long lastInsertId = mysqlPacketPayload.readIntLenenc(); - int statusFlags = mysqlPacketPayload.readInt2(); - int warnings = mysqlPacketPayload.readInt2(); - String info = mysqlPacketPayload.readStringEOF(); - log.debug("OKPacket[affectedRows={},lastInsertId={},statusFlags={},warnings={},info={}]", affectedRows, lastInsertId, statusFlags, warnings, info); - break; - case ErrPacket.HEADER: - int errorCode = mysqlPacketPayload.readInt2(); - String sqlStateMarker = mysqlPacketPayload.readStringFix(1); - String sqlState = mysqlPacketPayload.readStringFix(5); - String errorMessage = mysqlPacketPayload.readStringEOF(); - log.debug("ErrPacket[errorCode={},sqlStateMarker={},sqlState={},errorMessage={}]", errorCode, sqlStateMarker, sqlState, errorMessage); - break; - case EofPacket.HEADER: - warnings = mysqlPacketPayload.readInt2(); - statusFlags = mysqlPacketPayload.readInt2(); - log.debug("EofPacket[warnings={},statusFlags={}]", warnings, statusFlags); - break; - default: - break; - } - } - - //TODO - @Override - protected void executeCommandResponsePackets(final ChannelHandlerContext context, final int header, final MySQLPacketPayload mysqlPacketPayload) { - int connectionId = MySQLResultCache.getInstance().getonnectionMap(context.channel().id().asShortText()); - MySQLResultCache.getInstance().get(connectionId).setResponse(null); - } - - @Override - public void channelInactive(final ChannelHandlerContext ctx) throws Exception { - //TODO delete connection map. - super.channelInactive(ctx); - } - - private byte[] securePasswordAuthentication(final byte[] password, final byte[] authPluginData) { - try { - MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); - byte[] part1 = sha1.digest(password); - sha1.reset(); - byte[] part2 = sha1.digest(part1); - sha1.reset(); - sha1.update(authPluginData); - byte[] authResponse = sha1.digest(part2); - for (int i = 0; i < authResponse.length; i++) { - authResponse[i] = (byte) (authResponse[i] ^ part1[i]); - } - return authResponse; - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } - return null; - } -} +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.backend.mysql; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.List; + +import com.google.common.primitives.Bytes; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.shardingsphere.proxy.backend.common.CommandResponsePacketsHandler; +import io.shardingsphere.proxy.transport.mysql.constant.CapabilityFlag; +import io.shardingsphere.proxy.transport.mysql.constant.ColumnType; +import io.shardingsphere.proxy.transport.mysql.constant.ServerInfo; +import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; +import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.FieldCountPacket; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket; +import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket; +import io.shardingsphere.proxy.transport.mysql.packet.generic.ErrPacket; +import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket; +import io.shardingsphere.proxy.transport.mysql.packet.handshake.HandshakeResponse41Packet; +import io.shardingsphere.proxy.util.MySQLResultCache; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +/** + * Backend handler. + * + * @author wangkai + */ +@Slf4j +@RequiredArgsConstructor +public class MySQLBackendHandler extends CommandResponsePacketsHandler { + + private boolean authorized; + + private final String ip; + + private final int port; + + private final String database; + + private final String username; + + private final String password; + + @Override + public void channelRead(final ChannelHandlerContext context, final Object message) { + MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload((ByteBuf) message); + //int packetSize = mysqlPacketPayload.readInt3(); + int sequenceId = mysqlPacketPayload.readInt1(); + int header = mysqlPacketPayload.readInt1() & 0xFF; + if (OKPacket.HEADER == header || ErrPacket.HEADER == header || EofPacket.HEADER == header) { + genericResponsePacket(context, sequenceId, header, mysqlPacketPayload); + } else if (!authorized) { + auth(context, sequenceId, header, mysqlPacketPayload); + } else { + executeCommandResponsePackets(context, sequenceId, header, mysqlPacketPayload); + } + } + + @Override + protected void auth(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { + int protocolVersion = header; + String serverVersion = mysqlPacketPayload.readStringNul(); + int connectionId = mysqlPacketPayload.readInt4(); + MySQLResultCache.getInstance().putConnectionMap(context.channel().id().asShortText(), connectionId); + byte[] authPluginDataPart1 = mysqlPacketPayload.readStringNul().getBytes(); + int capabilityFlagsLower = mysqlPacketPayload.readInt2(); + int charset = mysqlPacketPayload.readInt1(); + int statusFlag = mysqlPacketPayload.readInt2(); + int capabilityFlagsUpper = mysqlPacketPayload.readInt2(); + int authPluginDataLength = mysqlPacketPayload.readInt1(); + mysqlPacketPayload.skipReserved(10); + byte[] authPluginDataPart2 = mysqlPacketPayload.readStringNul().getBytes(); + byte[] authPluginData = Bytes.concat(authPluginDataPart1, authPluginDataPart2); + //byte[] authResponse = byteXOR(SHA1(password.getBytes()), SHA1(Bytes.concat(authPluginData, SHA1(SHA1(password.getBytes()))))); + byte[] authResponse = securePasswordAuthentication(password.getBytes(), authPluginData); + //TODO maxSizePactet(16MB) should be set. + HandshakeResponse41Packet handshakeResponse41Packet = new HandshakeResponse41Packet(sequenceId + 1, CapabilityFlag.calculateHandshakeCapabilityFlagsLower(), 16777215, ServerInfo.CHARSET, + username, authResponse, database); + context.writeAndFlush(handshakeResponse41Packet); + } + + @Override + protected void genericResponsePacket(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { + CommandResponsePackets commandResponsePackets = null; + switch (header) { + case OKPacket.HEADER: + if (!authorized) { + authorized = true; + } + long affectedRows = mysqlPacketPayload.readIntLenenc(); + long lastInsertId = mysqlPacketPayload.readIntLenenc(); + int statusFlags = mysqlPacketPayload.readInt2(); + int warnings = mysqlPacketPayload.readInt2(); + String info = mysqlPacketPayload.readStringEOF(); + log.debug("OKPacket[affectedRows={},lastInsertId={},statusFlags={},warnings={},info={}]", affectedRows, lastInsertId, statusFlags, warnings, info); + commandResponsePackets = new CommandResponsePackets(new OKPacket(sequenceId + 1, affectedRows, lastInsertId, statusFlags, warnings, info)); + break; + case ErrPacket.HEADER: + int errorCode = mysqlPacketPayload.readInt2(); + String sqlStateMarker = mysqlPacketPayload.readStringFix(1); + String sqlState = mysqlPacketPayload.readStringFix(5); + String errorMessage = mysqlPacketPayload.readStringEOF(); + log.debug("ErrPacket[errorCode={},sqlStateMarker={},sqlState={},errorMessage={}]", errorCode, sqlStateMarker, sqlState, errorMessage); + commandResponsePackets = new CommandResponsePackets(new ErrPacket(sequenceId + 1, errorCode, sqlStateMarker, sqlState, errorMessage)); + break; + case EofPacket.HEADER: + warnings = mysqlPacketPayload.readInt2(); + statusFlags = mysqlPacketPayload.readInt2(); + log.debug("EofPacket[warnings={},statusFlags={}]", warnings, statusFlags); + commandResponsePackets = new CommandResponsePackets(new EofPacket(sequenceId + 1, warnings, statusFlags)); + break; + default: + break; + } + int connectionId = MySQLResultCache.getInstance().getConnectionMap(context.channel().id().asShortText()); + if (MySQLResultCache.getInstance().get(connectionId) != null) { + MySQLResultCache.getInstance().get(connectionId).setResponse(commandResponsePackets); + } + } + + //TODO + @Override + protected void executeCommandResponsePackets(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { +CommandResponsePackets result = new CommandResponsePackets(); + + //TODO + int columnCount = header <= 0xfb ? header : (int) mysqlPacketPayload.readIntLenenc(); + if (0 == columnCount) { + result.addPacket(new OKPacket(sequenceId + 1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); + } else { + result.addPacket(new FieldCountPacket(sequenceId + 1, columnCount)); + for (int i = 1; i <= columnCount; i++) { + String catalog = mysqlPacketPayload.readStringLenenc(); + String schema = mysqlPacketPayload.readStringLenenc(); + String table = mysqlPacketPayload.readStringLenenc(); + String orgTable = mysqlPacketPayload.readStringLenenc(); + String name = mysqlPacketPayload.readStringLenenc(); + String orgName = mysqlPacketPayload.readStringLenenc(); + mysqlPacketPayload.skipReserved(1 + 2); // fixed value and charset + int columnLength = mysqlPacketPayload.readInt4(); + int columnType = mysqlPacketPayload.readInt1(); + mysqlPacketPayload.skipReserved(2); // field flags + int decimals = mysqlPacketPayload.readInt1(); + mysqlPacketPayload.skipReserved(2); // filler value 0x00 0x00 + result.addPacket(new ColumnDefinition41Packet(sequenceId + 1, schema, table, orgTable, name, orgName, + columnLength, ColumnType.valueOfJDBCType(columnType), decimals)); + } + result.addPacket(new EofPacket(sequenceId + 1, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue())); + + if (mysqlPacketPayload.isReadable()) { + while (mysqlPacketPayload.isReadable()) { + List data = new ArrayList<>(columnCount); + for (int i = 1; i <= columnCount; i++) { + data.add(mysqlPacketPayload.readStringLenenc()); + } + result.addPacket(new TextResultSetRowPacket(sequenceId + 1, data)); + } + result.addPacket(new EofPacket(sequenceId + 1, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue())); + } + } + + int connectionId = MySQLResultCache.getInstance().getConnectionMap(context.channel().id().asShortText()); + MySQLResultCache.getInstance().get(connectionId).setResponse(result); + } + + @Override + public void channelInactive(final ChannelHandlerContext ctx) throws Exception { + //TODO delete connection map. + super.channelInactive(ctx); + } + + private byte[] securePasswordAuthentication(final byte[] password, final byte[] authPluginData) { + try { + MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); + byte[] part1 = sha1.digest(password); + sha1.reset(); + byte[] part2 = sha1.digest(part1); + sha1.reset(); + sha1.update(authPluginData); + byte[] authResponse = sha1.digest(part2); + for (int i = 0; i < authResponse.length; i++) { + authResponse[i] = (byte) (authResponse[i] ^ part1[i]); + } + return authResponse; + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java index 020d59b2c1d8c..aab6f40fb9d8b 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java @@ -1,93 +1,93 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.proxy.frontend.mysql; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.EventLoopGroup; -import io.shardingsphere.proxy.frontend.common.FrontendHandler; -import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; -import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; -import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; -import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket; -import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacketFactory; -import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket; -import io.shardingsphere.proxy.transport.mysql.packet.handshake.AuthPluginData; -import io.shardingsphere.proxy.transport.mysql.packet.handshake.ConnectionIdGenerator; -import io.shardingsphere.proxy.transport.mysql.packet.handshake.HandshakePacket; -import io.shardingsphere.proxy.transport.mysql.packet.handshake.HandshakeResponse41Packet; -import io.shardingsphere.proxy.util.MySQLResultCache; -import lombok.RequiredArgsConstructor; - -/** - * MySQL frontend handler. - * - * @author zhangliang - * @author wangkai - */ -@RequiredArgsConstructor -public final class MySQLFrontendHandler extends FrontendHandler { - - private final EventLoopGroup eventLoopGroup; - - private AuthPluginData authPluginData; - - @Override - protected void handshake(final ChannelHandlerContext context) { - authPluginData = new AuthPluginData(); - int connectionId = ConnectionIdGenerator.getInstance().nextId(); - MySQLResultCache.getInstance().putConnectionMap(context.channel().id().asShortText(), connectionId); - context.writeAndFlush(new HandshakePacket(ConnectionIdGenerator.getInstance().nextId(), authPluginData)); - } - - @Override - protected void auth(final ChannelHandlerContext context, final ByteBuf message) { - MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload(message); - try { - // TODO use authPluginData to auth - HandshakeResponse41Packet response41 = new HandshakeResponse41Packet(mysqlPacketPayload); - context.writeAndFlush(new OKPacket(response41.getSequenceId() + 1, 0L, 0L, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); - } finally { - mysqlPacketPayload.getByteBuf().release(); - } - } - - @Override - protected void executeCommand(final ChannelHandlerContext context, final ByteBuf message) { - eventLoopGroup.execute(new Runnable() { - - @Override - public void run() { - MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload(message); - try { - int sequenceId = mysqlPacketPayload.readInt1(); - int connectionId = MySQLResultCache.getInstance().getonnectionMap(context.channel().id().asShortText()); - CommandPacket commandPacket = CommandPacketFactory.getCommandPacket(sequenceId, connectionId, mysqlPacketPayload); - for (DatabaseProtocolPacket each : commandPacket.execute().getDatabaseProtocolPackets()) { - context.writeAndFlush(each); - } - while (commandPacket.hasMoreResultValue()) { - context.writeAndFlush(commandPacket.getResultValue()); - } - } finally { - mysqlPacketPayload.getByteBuf().release(); - } - } - }); - } -} +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.frontend.mysql; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.EventLoopGroup; +import io.shardingsphere.proxy.frontend.common.FrontendHandler; +import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; +import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; +import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacketFactory; +import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket; +import io.shardingsphere.proxy.transport.mysql.packet.handshake.AuthPluginData; +import io.shardingsphere.proxy.transport.mysql.packet.handshake.ConnectionIdGenerator; +import io.shardingsphere.proxy.transport.mysql.packet.handshake.HandshakePacket; +import io.shardingsphere.proxy.transport.mysql.packet.handshake.HandshakeResponse41Packet; +import io.shardingsphere.proxy.util.MySQLResultCache; +import lombok.RequiredArgsConstructor; + +/** + * MySQL frontend handler. + * + * @author zhangliang + * @author wangkai + */ +@RequiredArgsConstructor +public final class MySQLFrontendHandler extends FrontendHandler { + + private final EventLoopGroup eventLoopGroup; + + private AuthPluginData authPluginData; + + @Override + protected void handshake(final ChannelHandlerContext context) { + authPluginData = new AuthPluginData(); + int connectionId = ConnectionIdGenerator.getInstance().nextId(); + MySQLResultCache.getInstance().putConnectionMap(context.channel().id().asShortText(), connectionId); + context.writeAndFlush(new HandshakePacket(ConnectionIdGenerator.getInstance().nextId(), authPluginData)); + } + + @Override + protected void auth(final ChannelHandlerContext context, final ByteBuf message) { + MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload(message); + try { + // TODO use authPluginData to auth + HandshakeResponse41Packet response41 = new HandshakeResponse41Packet(mysqlPacketPayload); + context.writeAndFlush(new OKPacket(response41.getSequenceId() + 1, 0L, 0L, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); + } finally { + mysqlPacketPayload.getByteBuf().release(); + } + } + + @Override + protected void executeCommand(final ChannelHandlerContext context, final ByteBuf message) { + eventLoopGroup.execute(new Runnable() { + + @Override + public void run() { + MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload(message); + try { + int sequenceId = mysqlPacketPayload.readInt1(); + int connectionId = MySQLResultCache.getInstance().getConnectionMap(context.channel().id().asShortText()); + CommandPacket commandPacket = CommandPacketFactory.getCommandPacket(sequenceId, connectionId, mysqlPacketPayload); + for (DatabaseProtocolPacket each : commandPacket.execute().getDatabaseProtocolPackets()) { + context.writeAndFlush(each); + } + while (commandPacket.hasMoreResultValue()) { + context.writeAndFlush(commandPacket.getResultValue()); + } + } finally { + mysqlPacketPayload.getByteBuf().release(); + } + } + }); + } +} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacket.java index 36aacca123362..2eded13ad29d5 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacket.java @@ -1,45 +1,47 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.proxy.transport.mysql.packet; - -import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -/** - * MySQL packet. - * - * @author zhangliang - */ -@RequiredArgsConstructor -@Getter -public abstract class MySQLPacket implements DatabaseProtocolPacket { - - public static final int PAYLOAD_LENGTH = 3; - - public static final int SEQUENCE_LENGTH = 1; - - private final int sequenceId; - - /** - * Write packet to byte buffer. - * - * @param mysqlPacketPayload packet payload to be write - */ - public abstract void write(MySQLPacketPayload mysqlPacketPayload); -} +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.transport.mysql.packet; + +import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +/** + * MySQL packet. + * + * @author zhangliang + */ +@AllArgsConstructor +@Getter +@Setter +public abstract class MySQLPacket implements DatabaseProtocolPacket { + + public static final int PAYLOAD_LENGTH = 3; + + public static final int SEQUENCE_LENGTH = 1; + + private int sequenceId; + + /** + * Write packet to byte buffer. + * + * @param mysqlPacketPayload packet payload to be write + */ + public abstract void write(MySQLPacketPayload mysqlPacketPayload); +} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java index c820c8a239a1b..67a58ddb373d4 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java @@ -1,550 +1,559 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.proxy.transport.mysql.packet; - -import com.google.common.base.Strings; -import io.netty.buffer.ByteBuf; -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -import java.sql.Timestamp; -import java.util.Calendar; -import java.util.Date; - -/** - * Payload operation for MySQL packet data types. - * - * @see describing packets - * @see binary protocol value - * - * @author zhangliang - * @author zhangyonglun - * @author wangkai - */ -@RequiredArgsConstructor -@Getter -public final class MySQLPacketPayload { - - private final ByteBuf byteBuf; - - /** - * Read 1 byte fixed length integer from byte buffers. - * @see FixedLengthInteger - * - * @return 1 byte fixed length integer - */ - public int readInt1() { - return byteBuf.readByte(); - } - - /** - * Write 1 byte fixed length integer to byte buffers. - * @see FixedLengthInteger - * - * @param value 1 byte fixed length integer - */ - public void writeInt1(final int value) { - byteBuf.writeByte(value); - } - - /** - * Read 2 byte fixed length integer from byte buffers. - * @see FixedLengthInteger - * - * @return 2 byte fixed length integer - */ - public int readInt2() { - return byteBuf.readShortLE(); - } - - /** - * Write 2 byte fixed length integer to byte buffers. - * @see FixedLengthInteger - * - * @param value 2 byte fixed length integer - */ - public void writeInt2(final int value) { - byteBuf.writeShortLE(value); - } - - /** - * Read 3 byte fixed length integer from byte buffers. - * @see FixedLengthInteger - * - * @return 3 byte fixed length integer - */ - public int readInt3() { - return byteBuf.readMediumLE(); - } - - /** - * Write 3 byte fixed length integer to byte buffers. - * @see FixedLengthInteger - * - * @param value 3 byte fixed length integer - */ - public void writeInt3(final int value) { - byteBuf.writeMediumLE(value); - } - - /** - * Read 4 byte fixed length integer from byte buffers. - * @see FixedLengthInteger - * - * @return 4 byte fixed length integer - */ - public int readInt4() { - return byteBuf.readIntLE(); - } - - /** - * Write 4 byte fixed length integer to byte buffers. - * @see FixedLengthInteger - * - * @param value 4 byte fixed length integer - */ - public void writeInt4(final int value) { - byteBuf.writeIntLE(value); - - } - - /** - * Read 6 byte fixed length integer from byte buffers. - * @see FixedLengthInteger - * - * @return 6 byte fixed length integer - */ - public int readInt6() { - // TODO - return 0; - } - - /** - * Write 6 byte fixed length integer to byte buffers. - * @see FixedLengthInteger - * - * @param value 6 byte fixed length integer - */ - public void writeInt6(final int value) { - // TODO - } - - /** - * Read 8 byte fixed length integer from byte buffers. - * @see FixedLengthInteger - * - * @return 8 byte fixed length integer - */ - public long readInt8() { - return byteBuf.readLongLE(); - } - - /** - * Write 8 byte fixed length integer to byte buffers. - * @see FixedLengthInteger - * - * @param value 8 byte fixed length integer - */ - public void writeInt8(final long value) { - byteBuf.writeLongLE(value); - - } - - /** - * Read length encoded integer from byte buffers. - * @see LengthEncodedInteger - * - * @return length encoded integer - */ - public long readIntLenenc() { - int firstByte = byteBuf.readByte(); - if (firstByte <= 0xfb) { - return firstByte; - } - if (0xfc == firstByte) { - return byteBuf.readShortLE(); - } - if (0xfd == firstByte) { - return byteBuf.readMediumLE(); - } - return byteBuf.readLongLE(); - } - - /** - * Write length encoded integer to byte buffers. - * @see LengthEncodedInteger - * - * @param value length encoded integer - */ - public void writeIntLenenc(final long value) { - if (value < 251) { - byteBuf.writeByte((int) value); - return; - } - if (value >= 251 && value < Math.pow(2, 16)) { - byteBuf.writeByte(0xfc); - byteBuf.writeShortLE((int) value); - return; - } - if (value <= Math.pow(2, 16) && value < Math.pow(2, 24)) { - byteBuf.writeByte(0xfd); - byteBuf.writeInt((int) value); - return; - } - byteBuf.writeByte(0xfe); - byteBuf.writeLongLE(value); - } - - /** - * Read fixed length string from byte buffers. - * @see FixedLengthString - * - * @return fixed length string - */ - public String readStringLenenc() { - int length = (int) readIntLenenc(); - byte[] result = new byte[length]; - byteBuf.readBytes(result); - return new String(result); - } - - /** - * Write fixed length string to byte buffers. - * @see FixedLengthString - * - * @param value fixed length string - */ - public void writeStringLenenc(final String value) { - if (Strings.isNullOrEmpty(value)) { - byteBuf.writeByte(0); - return; - } - writeIntLenenc(value.getBytes().length); - byteBuf.writeBytes(value.getBytes()); - } - - /** - * Read fixed length string from byte buffers. - * @see FixedLengthString - * - * @param length length of fixed string - * - * @return fixed length string - */ - public String readStringFix(final int length) { - byte[] result = new byte[length]; - byteBuf.readBytes(result); - return new String(result); - } - - /** - * Write variable length string to byte buffers. - * @see FixedLengthString - * - * @param value fixed length string - */ - public void writeStringFix(final String value) { - byteBuf.writeBytes(value.getBytes()); - } - - /** - * Write variable length bytes to byte buffers. - * @see Native41 - * - * @param value fixed length bytes - */ - public void writeBytes(final byte[] value) { - byteBuf.writeBytes(value); - } - - /** - * Read variable length string from byte buffers. - * @see FixedLengthString - * - * @return variable length string - */ - public String readStringVar() { - // TODO - return ""; - } - - /** - * Write fixed length string to byte buffers. - * @see FixedLengthString - * - * @param value variable length string - */ - public void writeStringVar(final String value) { - // TODO - } - - /** - * Read null terminated string from byte buffers. - * @see NulTerminatedString - * - * @return null terminated string - */ - public String readStringNul() { - byte[] result = new byte[byteBuf.bytesBefore((byte) 0)]; - byteBuf.readBytes(result); - byteBuf.skipBytes(1); - return new String(result); - } - - /** - * Write null terminated string to byte buffers. - * @see NulTerminatedString - * - * @param value null terminated string - */ - public void writeStringNul(final String value) { - byteBuf.writeBytes(value.getBytes()); - byteBuf.writeByte(0); - } - - /** - * Read rest of packet string from byte buffers. - * @see RestOfPacketString - * - * @return rest of packet string - */ - public String readStringEOF() { - byte[] result = new byte[byteBuf.readableBytes()]; - byteBuf.readBytes(result); - return new String(result); - } - - /** - * Write rest of packet string to byte buffers. - * @see RestOfPacketString - * - * @param value rest of packet string - */ - public void writeStringEOF(final String value) { - byteBuf.writeBytes(value.getBytes()); - } - - /** - * Skip reserved from byte buffers. - * - * @param length length of reserved - */ - public void skipReserved(final int length) { - byteBuf.skipBytes(length); - } - - /** - * Write null for reserved to byte buffers. - * - * @param length length of reserved - */ - public void writeReserved(final int length) { - for (int i = 0; i < length; i++) { - byteBuf.writeByte(0); - } - } - - /** - * Read 4 byte float from byte buffers. - * @see MYSQL_TYPE_FLOAT - * - * @return 4 byte float - */ - public float readFloat() { - return byteBuf.readFloatLE(); - } - - /** - * Write 4 byte float to byte buffers. - * @see MYSQL_TYPE_FLOAT - * - * @param value 4 byte float - */ - public void writeFloat(final float value) { - byteBuf.writeFloatLE(value); - } - - /** - * Read 8 byte double from byte buffers. - * @see MYSQL_TYPE_DOUBLE - * - * @return 8 byte double - */ - public double readDouble() { - return byteBuf.readDoubleLE(); - } - - /** - * Write 8 byte double to byte buffers. - * @see MYSQL_TYPE_DOUBLE - * - * @param value 8 byte double - */ - public void writeDouble(final double value) { - byteBuf.writeDoubleLE(value); - } - - /** - * Read date from byte buffers. - * @see MYSQL_TYPE_DATE - * - * @return timestamp - */ - public Timestamp readDate() { - Timestamp timestamp; - Calendar calendar = Calendar.getInstance(); - int length = readInt1(); - switch (length) { - case 0: - timestamp = new Timestamp(0); - break; - case 4: - calendar.set(readInt2(), readInt1() - 1, readInt1()); - timestamp = new Timestamp(calendar.getTimeInMillis()); - break; - case 7: - calendar.set(readInt2(), readInt1() - 1, readInt1(), - readInt1(), readInt1(), readInt1()); - timestamp = new Timestamp(calendar.getTimeInMillis()); - break; - case 11: - calendar.set(readInt2(), readInt1() - 1, readInt1(), - readInt1(), readInt1(), readInt1()); - timestamp = new Timestamp(calendar.getTimeInMillis()); - timestamp.setNanos(readInt4()); - break; - default: - throw new IllegalArgumentException(String.format("Wrong length '%d' of MYSQL_TYPE_TIME", length)); - } - return timestamp; - } - - /** - * Write date to byte buffers. - * @see MYSQL_TYPE_DATE - * - * @param timestamp timestamp - */ - public void writeDate(final Timestamp timestamp) { - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(timestamp.getTime()); - int year = calendar.get(Calendar.YEAR); - int month = calendar.get(Calendar.MONTH) + 1; - int day = calendar.get(Calendar.DAY_OF_MONTH); - int hour = calendar.get(Calendar.HOUR_OF_DAY); - int minute = calendar.get(Calendar.MINUTE); - int second = calendar.get(Calendar.SECOND); - int millisecond = timestamp.getNanos(); - boolean isDateValueAbsent = 0 == year && 0 == month && 0 == day; - boolean isTimeValueAbsent = 0 == hour && 0 == minute && 0 == second; - boolean isMillisecondValueAbsent = 0 == millisecond; - if (isDateValueAbsent && isTimeValueAbsent && isMillisecondValueAbsent) { - writeInt1(0); - } else if (isTimeValueAbsent && isMillisecondValueAbsent) { - writeInt1(4); - writeInt2(year); - writeInt1(month); - writeInt1(day); - } else if (isMillisecondValueAbsent) { - writeInt1(7); - writeInt2(year); - writeInt1(month); - writeInt1(day); - writeInt1(hour); - writeInt1(minute); - writeInt1(second); - } else { - writeInt1(11); - writeInt2(year); - writeInt1(month); - writeInt1(day); - writeInt1(hour); - writeInt1(minute); - writeInt1(second); - writeInt4(millisecond); - } - } - - /** - * Read time from byte buffers. - * @see MYSQL_TYPE_TIME - * - * @return timestamp - */ - public Timestamp readTime() { - Timestamp timestamp; - Calendar calendar = Calendar.getInstance(); - int length = readInt1(); - readInt1(); - readInt4(); - switch (length) { - case 0: - timestamp = new Timestamp(0); - break; - case 8: - calendar.set(0, 0, 0, readInt1(), readInt1(), readInt1()); - timestamp = new Timestamp(calendar.getTimeInMillis()); - timestamp.setNanos(0); - break; - case 12: - calendar.set(0, 0, 0, readInt1(), readInt1(), readInt1()); - timestamp = new Timestamp(calendar.getTimeInMillis()); - timestamp.setNanos(readInt4()); - break; - default: - throw new IllegalArgumentException(String.format("Wrong length '%d' of MYSQL_TYPE_DATE", length)); - } - return timestamp; - } - - /** - * Write time to byte buffers. - * @see MYSQL_TYPE_TIME - * - * @param date date - */ - public void writeTime(final Date date) { - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(date.getTime()); - int hour = calendar.get(Calendar.HOUR_OF_DAY); - int minute = calendar.get(Calendar.MINUTE); - int second = calendar.get(Calendar.SECOND); - Timestamp timestamp = new Timestamp(date.getTime()); - int millisecond = timestamp.getNanos(); - boolean isTimeValueAbsent = 0 == hour && 0 == minute && 0 == second; - boolean isMillisecondValueAbsent = 0 == millisecond; - if (isTimeValueAbsent && isMillisecondValueAbsent) { - writeInt1(0); - } else if (isMillisecondValueAbsent) { - writeInt1(8); - writeInt1(0); - writeInt4(0); - writeInt1(hour); - writeInt1(minute); - writeInt1(second); - } else { - writeInt1(12); - writeInt1(0); - writeInt4(0); - writeInt1(hour); - writeInt1(minute); - writeInt1(second); - writeInt4(millisecond); - } - } -} +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.transport.mysql.packet; + +import com.google.common.base.Strings; +import io.netty.buffer.ByteBuf; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import java.sql.Timestamp; +import java.util.Calendar; +import java.util.Date; + +/** + * Payload operation for MySQL packet data types. + * + * @see describing packets + * @see binary protocol value + * + * @author zhangliang + * @author zhangyonglun + * @author wangkai + */ +@RequiredArgsConstructor +@Getter +public final class MySQLPacketPayload { + + private final ByteBuf byteBuf; + + /** + * Returns true if and only if (this.writerIndex - this.readerIndex) is greater than 0. + * + * @return the byteBuf is readable + */ + public boolean isReadable() { + return byteBuf.isReadable(); + } + + /** + * Read 1 byte fixed length integer from byte buffers. + * @see FixedLengthInteger + * + * @return 1 byte fixed length integer + */ + public int readInt1() { + return byteBuf.readByte(); + } + + /** + * Write 1 byte fixed length integer to byte buffers. + * @see FixedLengthInteger + * + * @param value 1 byte fixed length integer + */ + public void writeInt1(final int value) { + byteBuf.writeByte(value); + } + + /** + * Read 2 byte fixed length integer from byte buffers. + * @see FixedLengthInteger + * + * @return 2 byte fixed length integer + */ + public int readInt2() { + return byteBuf.readShortLE(); + } + + /** + * Write 2 byte fixed length integer to byte buffers. + * @see FixedLengthInteger + * + * @param value 2 byte fixed length integer + */ + public void writeInt2(final int value) { + byteBuf.writeShortLE(value); + } + + /** + * Read 3 byte fixed length integer from byte buffers. + * @see FixedLengthInteger + * + * @return 3 byte fixed length integer + */ + public int readInt3() { + return byteBuf.readMediumLE(); + } + + /** + * Write 3 byte fixed length integer to byte buffers. + * @see FixedLengthInteger + * + * @param value 3 byte fixed length integer + */ + public void writeInt3(final int value) { + byteBuf.writeMediumLE(value); + } + + /** + * Read 4 byte fixed length integer from byte buffers. + * @see FixedLengthInteger + * + * @return 4 byte fixed length integer + */ + public int readInt4() { + return byteBuf.readIntLE(); + } + + /** + * Write 4 byte fixed length integer to byte buffers. + * @see FixedLengthInteger + * + * @param value 4 byte fixed length integer + */ + public void writeInt4(final int value) { + byteBuf.writeIntLE(value); + + } + + /** + * Read 6 byte fixed length integer from byte buffers. + * @see FixedLengthInteger + * + * @return 6 byte fixed length integer + */ + public int readInt6() { + // TODO + return 0; + } + + /** + * Write 6 byte fixed length integer to byte buffers. + * @see FixedLengthInteger + * + * @param value 6 byte fixed length integer + */ + public void writeInt6(final int value) { + // TODO + } + + /** + * Read 8 byte fixed length integer from byte buffers. + * @see FixedLengthInteger + * + * @return 8 byte fixed length integer + */ + public long readInt8() { + return byteBuf.readLongLE(); + } + + /** + * Write 8 byte fixed length integer to byte buffers. + * @see FixedLengthInteger + * + * @param value 8 byte fixed length integer + */ + public void writeInt8(final long value) { + byteBuf.writeLongLE(value); + + } + + /** + * Read length encoded integer from byte buffers. + * @see LengthEncodedInteger + * + * @return length encoded integer + */ + public long readIntLenenc() { + int firstByte = byteBuf.readByte(); + if (firstByte <= 0xfb) { + return firstByte; + } + if (0xfc == firstByte) { + return byteBuf.readShortLE(); + } + if (0xfd == firstByte) { + return byteBuf.readMediumLE(); + } + return byteBuf.readLongLE(); + } + + /** + * Write length encoded integer to byte buffers. + * @see LengthEncodedInteger + * + * @param value length encoded integer + */ + public void writeIntLenenc(final long value) { + if (value < 251) { + byteBuf.writeByte((int) value); + return; + } + if (value >= 251 && value < Math.pow(2, 16)) { + byteBuf.writeByte(0xfc); + byteBuf.writeShortLE((int) value); + return; + } + if (value <= Math.pow(2, 16) && value < Math.pow(2, 24)) { + byteBuf.writeByte(0xfd); + byteBuf.writeInt((int) value); + return; + } + byteBuf.writeByte(0xfe); + byteBuf.writeLongLE(value); + } + + /** + * Read fixed length string from byte buffers. + * @see FixedLengthString + * + * @return fixed length string + */ + public String readStringLenenc() { + int length = (int) readIntLenenc(); + byte[] result = new byte[length]; + byteBuf.readBytes(result); + return new String(result); + } + + /** + * Write fixed length string to byte buffers. + * @see FixedLengthString + * + * @param value fixed length string + */ + public void writeStringLenenc(final String value) { + if (Strings.isNullOrEmpty(value)) { + byteBuf.writeByte(0); + return; + } + writeIntLenenc(value.getBytes().length); + byteBuf.writeBytes(value.getBytes()); + } + + /** + * Read fixed length string from byte buffers. + * @see FixedLengthString + * + * @param length length of fixed string + * + * @return fixed length string + */ + public String readStringFix(final int length) { + byte[] result = new byte[length]; + byteBuf.readBytes(result); + return new String(result); + } + + /** + * Write variable length string to byte buffers. + * @see FixedLengthString + * + * @param value fixed length string + */ + public void writeStringFix(final String value) { + byteBuf.writeBytes(value.getBytes()); + } + + /** + * Write variable length bytes to byte buffers. + * @see Native41 + * + * @param value fixed length bytes + */ + public void writeBytes(final byte[] value) { + byteBuf.writeBytes(value); + } + + /** + * Read variable length string from byte buffers. + * @see FixedLengthString + * + * @return variable length string + */ + public String readStringVar() { + // TODO + return ""; + } + + /** + * Write fixed length string to byte buffers. + * @see FixedLengthString + * + * @param value variable length string + */ + public void writeStringVar(final String value) { + // TODO + } + + /** + * Read null terminated string from byte buffers. + * @see NulTerminatedString + * + * @return null terminated string + */ + public String readStringNul() { + byte[] result = new byte[byteBuf.bytesBefore((byte) 0)]; + byteBuf.readBytes(result); + byteBuf.skipBytes(1); + return new String(result); + } + + /** + * Write null terminated string to byte buffers. + * @see NulTerminatedString + * + * @param value null terminated string + */ + public void writeStringNul(final String value) { + byteBuf.writeBytes(value.getBytes()); + byteBuf.writeByte(0); + } + + /** + * Read rest of packet string from byte buffers. + * @see RestOfPacketString + * + * @return rest of packet string + */ + public String readStringEOF() { + byte[] result = new byte[byteBuf.readableBytes()]; + byteBuf.readBytes(result); + return new String(result); + } + + /** + * Write rest of packet string to byte buffers. + * @see RestOfPacketString + * + * @param value rest of packet string + */ + public void writeStringEOF(final String value) { + byteBuf.writeBytes(value.getBytes()); + } + + /** + * Skip reserved from byte buffers. + * + * @param length length of reserved + */ + public void skipReserved(final int length) { + byteBuf.skipBytes(length); + } + + /** + * Write null for reserved to byte buffers. + * + * @param length length of reserved + */ + public void writeReserved(final int length) { + for (int i = 0; i < length; i++) { + byteBuf.writeByte(0); + } + } + + /** + * Read 4 byte float from byte buffers. + * @see MYSQL_TYPE_FLOAT + * + * @return 4 byte float + */ + public float readFloat() { + return byteBuf.readFloatLE(); + } + + /** + * Write 4 byte float to byte buffers. + * @see MYSQL_TYPE_FLOAT + * + * @param value 4 byte float + */ + public void writeFloat(final float value) { + byteBuf.writeFloatLE(value); + } + + /** + * Read 8 byte double from byte buffers. + * @see MYSQL_TYPE_DOUBLE + * + * @return 8 byte double + */ + public double readDouble() { + return byteBuf.readDoubleLE(); + } + + /** + * Write 8 byte double to byte buffers. + * @see MYSQL_TYPE_DOUBLE + * + * @param value 8 byte double + */ + public void writeDouble(final double value) { + byteBuf.writeDoubleLE(value); + } + + /** + * Read date from byte buffers. + * @see MYSQL_TYPE_DATE + * + * @return timestamp + */ + public Timestamp readDate() { + Timestamp timestamp; + Calendar calendar = Calendar.getInstance(); + int length = readInt1(); + switch (length) { + case 0: + timestamp = new Timestamp(0); + break; + case 4: + calendar.set(readInt2(), readInt1() - 1, readInt1()); + timestamp = new Timestamp(calendar.getTimeInMillis()); + break; + case 7: + calendar.set(readInt2(), readInt1() - 1, readInt1(), + readInt1(), readInt1(), readInt1()); + timestamp = new Timestamp(calendar.getTimeInMillis()); + break; + case 11: + calendar.set(readInt2(), readInt1() - 1, readInt1(), + readInt1(), readInt1(), readInt1()); + timestamp = new Timestamp(calendar.getTimeInMillis()); + timestamp.setNanos(readInt4()); + break; + default: + throw new IllegalArgumentException(String.format("Wrong length '%d' of MYSQL_TYPE_TIME", length)); + } + return timestamp; + } + + /** + * Write date to byte buffers. + * @see MYSQL_TYPE_DATE + * + * @param timestamp timestamp + */ + public void writeDate(final Timestamp timestamp) { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(timestamp.getTime()); + int year = calendar.get(Calendar.YEAR); + int month = calendar.get(Calendar.MONTH) + 1; + int day = calendar.get(Calendar.DAY_OF_MONTH); + int hour = calendar.get(Calendar.HOUR_OF_DAY); + int minute = calendar.get(Calendar.MINUTE); + int second = calendar.get(Calendar.SECOND); + int millisecond = timestamp.getNanos(); + boolean isDateValueAbsent = 0 == year && 0 == month && 0 == day; + boolean isTimeValueAbsent = 0 == hour && 0 == minute && 0 == second; + boolean isMillisecondValueAbsent = 0 == millisecond; + if (isDateValueAbsent && isTimeValueAbsent && isMillisecondValueAbsent) { + writeInt1(0); + } else if (isTimeValueAbsent && isMillisecondValueAbsent) { + writeInt1(4); + writeInt2(year); + writeInt1(month); + writeInt1(day); + } else if (isMillisecondValueAbsent) { + writeInt1(7); + writeInt2(year); + writeInt1(month); + writeInt1(day); + writeInt1(hour); + writeInt1(minute); + writeInt1(second); + } else { + writeInt1(11); + writeInt2(year); + writeInt1(month); + writeInt1(day); + writeInt1(hour); + writeInt1(minute); + writeInt1(second); + writeInt4(millisecond); + } + } + + /** + * Read time from byte buffers. + * @see MYSQL_TYPE_TIME + * + * @return timestamp + */ + public Timestamp readTime() { + Timestamp timestamp; + Calendar calendar = Calendar.getInstance(); + int length = readInt1(); + readInt1(); + readInt4(); + switch (length) { + case 0: + timestamp = new Timestamp(0); + break; + case 8: + calendar.set(0, 0, 0, readInt1(), readInt1(), readInt1()); + timestamp = new Timestamp(calendar.getTimeInMillis()); + timestamp.setNanos(0); + break; + case 12: + calendar.set(0, 0, 0, readInt1(), readInt1(), readInt1()); + timestamp = new Timestamp(calendar.getTimeInMillis()); + timestamp.setNanos(readInt4()); + break; + default: + throw new IllegalArgumentException(String.format("Wrong length '%d' of MYSQL_TYPE_DATE", length)); + } + return timestamp; + } + + /** + * Write time to byte buffers. + * @see MYSQL_TYPE_TIME + * + * @param date date + */ + public void writeTime(final Date date) { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(date.getTime()); + int hour = calendar.get(Calendar.HOUR_OF_DAY); + int minute = calendar.get(Calendar.MINUTE); + int second = calendar.get(Calendar.SECOND); + Timestamp timestamp = new Timestamp(date.getTime()); + int millisecond = timestamp.getNanos(); + boolean isTimeValueAbsent = 0 == hour && 0 == minute && 0 == second; + boolean isMillisecondValueAbsent = 0 == millisecond; + if (isTimeValueAbsent && isMillisecondValueAbsent) { + writeInt1(0); + } else if (isMillisecondValueAbsent) { + writeInt1(8); + writeInt1(0); + writeInt4(0); + writeInt1(hour); + writeInt1(minute); + writeInt1(second); + } else { + writeInt1(12); + writeInt1(0); + writeInt4(0); + writeInt1(hour); + writeInt1(minute); + writeInt1(second); + writeInt4(millisecond); + } + } +} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandResponsePackets.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandResponsePackets.java index 829fca7acd53b..7b98063ac5a12 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandResponsePackets.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandResponsePackets.java @@ -1,68 +1,77 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.proxy.transport.mysql.packet.command; - -import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; -import lombok.Getter; -import lombok.NoArgsConstructor; - -import java.util.Collection; -import java.util.LinkedList; - -/** - * Command response packet. - * - * @author zhangyonglun - */ -@NoArgsConstructor -@Getter -public final class CommandResponsePackets { - - private final Collection databaseProtocolPackets = new LinkedList<>(); - - public CommandResponsePackets(final DatabaseProtocolPacket databaseProtocolPacket) { - databaseProtocolPackets.add(databaseProtocolPacket); - } - - /** - * Add packet. - * - * @param databaseProtocolPacket database protocol packet - */ - public void addPacket(final DatabaseProtocolPacket databaseProtocolPacket) { - databaseProtocolPackets.add(databaseProtocolPacket); - } - - /** - * Get head packet. - * - * @return head database protocol packet - */ - public DatabaseProtocolPacket getHeadPacket() { - return databaseProtocolPackets.iterator().next(); - } - - /** - * Size of databaseProtocolPackets. - * - * @return size - */ - public int size() { - return databaseProtocolPackets.size(); - } -} +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.transport.mysql.packet.command; + +import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.Collection; +import java.util.LinkedList; + +/** + * Command response packet. + * + * @author zhangyonglun + */ +@NoArgsConstructor +@Getter +public final class CommandResponsePackets { + + private final Collection databaseProtocolPackets = new LinkedList<>(); + + public CommandResponsePackets(final DatabaseProtocolPacket databaseProtocolPacket) { + databaseProtocolPackets.add(databaseProtocolPacket); + } + + /** + * Add packet. + * + * @param databaseProtocolPacket database protocol packet + */ + public void addPacket(final DatabaseProtocolPacket databaseProtocolPacket) { + databaseProtocolPackets.add(databaseProtocolPacket); + } + + /** + * Add packets. + * + * @param databaseProtocolPackets database protocol packets + */ + public void addPackets(final Collection databaseProtocolPackets) { + databaseProtocolPackets.addAll(databaseProtocolPackets); + } + + /** + * Get head packet. + * + * @return head database protocol packet + */ + public DatabaseProtocolPacket getHeadPacket() { + return databaseProtocolPackets.iterator().next(); + } + + /** + * Size of databaseProtocolPackets. + * + * @return size + */ + public int size() { + return databaseProtocolPackets.size(); + } +} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java index 6dbc45d76e3f4..f012bd521b000 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java @@ -1,81 +1,81 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.proxy.transport.mysql.packet.command.text.fieldlist; - -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.ShardingConstant; -import io.shardingsphere.proxy.backend.common.SQLExecuteBackendHandler; -import io.shardingsphere.proxy.backend.common.SQLPacketsBackendHandler; -import io.shardingsphere.proxy.config.RuleRegistry; -import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; -import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; -import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket; -import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacketType; -import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; -import lombok.extern.slf4j.Slf4j; - -/** - * COM_FIELD_LIST command packet. - * - * @author zhangliang - * @author wangkai - * @see COM_FIELD_LIST - */ -@Slf4j -public final class ComFieldListPacket extends CommandPacket { - - private final String table; - - private final String fieldWildcard; - - public ComFieldListPacket(final int sequenceId, final int connectionId, final MySQLPacketPayload mysqlPacketPayload) { - super(sequenceId, connectionId); - table = mysqlPacketPayload.readStringNul(); - fieldWildcard = mysqlPacketPayload.readStringEOF(); - } - - @Override - public void write(final MySQLPacketPayload mysqlPacketPayload) { - mysqlPacketPayload.writeInt1(CommandPacketType.COM_FIELD_LIST.getValue()); - mysqlPacketPayload.writeStringNul(table); - mysqlPacketPayload.writeStringEOF(fieldWildcard); - } - - @Override - public CommandResponsePackets execute() { - log.debug("table name received for Sharding-Proxy: {}", table); - log.debug("field wildcard received for Sharding-Proxy: {}", fieldWildcard); - String sql = String.format("SHOW COLUMNS FROM %s FROM %s", table, ShardingConstant.LOGIC_SCHEMA_NAME); - // TODO use common database type - if (RuleRegistry.WITHOUT_JDBC) { - return new SQLPacketsBackendHandler(sql, getConnectionId(), DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()).execute(); - } else { - return new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()).execute(); - } - } - - @Override - public boolean hasMoreResultValue() { - return false; - } - - @Override - public DatabaseProtocolPacket getResultValue() { - return null; - } -} +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.transport.mysql.packet.command.text.fieldlist; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.constant.ShardingConstant; +import io.shardingsphere.proxy.backend.common.SQLExecuteBackendHandler; +import io.shardingsphere.proxy.backend.common.SQLPacketsBackendHandler; +import io.shardingsphere.proxy.config.RuleRegistry; +import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; +import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacketType; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; +import lombok.extern.slf4j.Slf4j; + +/** + * COM_FIELD_LIST command packet. + * + * @author zhangliang + * @author wangkai + * @see COM_FIELD_LIST + */ +@Slf4j +public final class ComFieldListPacket extends CommandPacket { + + private final String table; + + private final String fieldWildcard; + + public ComFieldListPacket(final int sequenceId, final int connectionId, final MySQLPacketPayload mysqlPacketPayload) { + super(sequenceId, connectionId); + table = mysqlPacketPayload.readStringNul(); + fieldWildcard = mysqlPacketPayload.readStringEOF(); + } + + @Override + public void write(final MySQLPacketPayload mysqlPacketPayload) { + mysqlPacketPayload.writeInt1(CommandPacketType.COM_FIELD_LIST.getValue()); + mysqlPacketPayload.writeStringNul(table); + mysqlPacketPayload.writeStringEOF(fieldWildcard); + } + + @Override + public CommandResponsePackets execute() { + log.debug("table name received for Sharding-Proxy: {}", table); + log.debug("field wildcard received for Sharding-Proxy: {}", fieldWildcard); + String sql = String.format("SHOW COLUMNS FROM %s FROM %s", table, ShardingConstant.LOGIC_SCHEMA_NAME); + // TODO use common database type + if (RuleRegistry.WITHOUT_JDBC) { + return new SQLPacketsBackendHandler(this, sql, getConnectionId(), DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()).execute(); + } else { + return new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()).execute(); + } + } + + @Override + public boolean hasMoreResultValue() { + return false; + } + + @Override + public DatabaseProtocolPacket getResultValue() { + return null; + } +} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java index ad05c8ca7a317..aa73f62bfa373 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java @@ -1,94 +1,94 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.proxy.transport.mysql.packet.command.text.query; - -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.proxy.backend.common.SQLExecuteBackendHandler; -import io.shardingsphere.proxy.backend.common.SQLPacketsBackendHandler; -import io.shardingsphere.proxy.config.RuleRegistry; -import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; -import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; -import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket; -import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; -import lombok.extern.slf4j.Slf4j; - -import java.sql.SQLException; - -/** - * COM_QUERY command packet. - * @see COM_QUERY - * - * @author zhangliang - */ -@Slf4j -public final class ComQueryPacket extends CommandPacket { - - private final String sql; - - private final SQLExecuteBackendHandler sqlExecuteBackendHandler; - - private final SQLPacketsBackendHandler sqlPacketsBackendHandler; - - public ComQueryPacket(final int sequenceId, final int connectionId, final MySQLPacketPayload mysqlPacketPayload) { - super(sequenceId, connectionId); - sql = mysqlPacketPayload.readStringEOF(); - sqlExecuteBackendHandler = new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()); - sqlPacketsBackendHandler = new SQLPacketsBackendHandler(sql, connectionId, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()); - } - - @Override - public void write(final MySQLPacketPayload mysqlPacketPayload) { - mysqlPacketPayload.writeStringEOF(sql); - } - - @Override - public CommandResponsePackets execute() { - log.debug("COM_QUERY received for Sharding-Proxy: {}", sql); - if (RuleRegistry.WITHOUT_JDBC) { - return sqlPacketsBackendHandler.execute(); - } else { - return sqlExecuteBackendHandler.execute(); - } - } - - /** - * Has more Result value. - * - * @return has more result value - */ - public boolean hasMoreResultValue() { - try { - if (RuleRegistry.WITHOUT_JDBC) { - return false; - } else { - return sqlExecuteBackendHandler.hasMoreResultValue(); - } - } catch (final SQLException ex) { - return false; - } - } - - /** - * Get result value. - * - * @return database protocol packet - */ - public DatabaseProtocolPacket getResultValue() { - return sqlExecuteBackendHandler.getResultValue(); - } -} +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.transport.mysql.packet.command.text.query; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.proxy.backend.common.SQLExecuteBackendHandler; +import io.shardingsphere.proxy.backend.common.SQLPacketsBackendHandler; +import io.shardingsphere.proxy.config.RuleRegistry; +import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; +import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; +import lombok.extern.slf4j.Slf4j; + +import java.sql.SQLException; + +/** + * COM_QUERY command packet. + * @see COM_QUERY + * + * @author zhangliang + */ +@Slf4j +public final class ComQueryPacket extends CommandPacket { + + private final String sql; + + private final SQLExecuteBackendHandler sqlExecuteBackendHandler; + + private final SQLPacketsBackendHandler sqlPacketsBackendHandler; + + public ComQueryPacket(final int sequenceId, final int connectionId, final MySQLPacketPayload mysqlPacketPayload) { + super(sequenceId, connectionId); + sql = mysqlPacketPayload.readStringEOF(); + sqlExecuteBackendHandler = new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()); + sqlPacketsBackendHandler = new SQLPacketsBackendHandler(this, sql, connectionId, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()); + } + + @Override + public void write(final MySQLPacketPayload mysqlPacketPayload) { + mysqlPacketPayload.writeStringEOF(sql); + } + + @Override + public CommandResponsePackets execute() { + log.debug("COM_QUERY received for Sharding-Proxy: {}", sql); + if (RuleRegistry.WITHOUT_JDBC) { + return sqlPacketsBackendHandler.execute(); + } else { + return sqlExecuteBackendHandler.execute(); + } + } + + /** + * Has more Result value. + * + * @return has more result value + */ + public boolean hasMoreResultValue() { + try { + if (RuleRegistry.WITHOUT_JDBC) { + return false; + } else { + return sqlExecuteBackendHandler.hasMoreResultValue(); + } + } catch (final SQLException ex) { + return false; + } + } + + /** + * Get result value. + * + * @return database protocol packet + */ + public DatabaseProtocolPacket getResultValue() { + return sqlExecuteBackendHandler.getResultValue(); + } +} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java index 6e39703f99f10..7b3bd9edc60eb 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java @@ -1,98 +1,98 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.proxy.util; - -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; - -import java.util.concurrent.TimeUnit; - -/** - * cache for SynchronizedFuture. - * - * @author wangkai - */ -public class MySQLResultCache { - private static final MySQLResultCache INSTANCE = new MySQLResultCache(); - - //TODO expire time should be set. - private Cache resultCache = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.SECONDS).build(); - - private Cache connectionCache = CacheBuilder.newBuilder().build(); - - private Cache channelCache = CacheBuilder.newBuilder().build(); - - /** - * put synchronizedFuture by connectionId. - * - * @param connectionId mysql connection id. - * @param synchronizedFuture multiple result set. - */ - public void put(final int connectionId, final SynchronizedFuture synchronizedFuture) { - resultCache.put(connectionId, synchronizedFuture); - } - - /** - * get SynchronizedFuture by connectionId. - * - * @param connectionId mysql connection id. - * @return multiple result set. - */ - public SynchronizedFuture get(final int connectionId) { - return resultCache.getIfPresent(connectionId); - } - - /** - * delete SynchronizedFuture by connectionId. - * - * @param connectionId mysql connection id. - */ - public void delete(final int connectionId) { - resultCache.invalidate(connectionId); - } - - /** - * put connectionId by channelId. - * - * @param channelId netty channel id. - * @param connectionId mysql connection id. - */ - public void putConnectionMap(final String channelId, final int connectionId) { - connectionCache.put(channelId, connectionId); - } - - /** - * get connectionId by channelId. - * - * @param channelId netty channel id. - * @return connectionId mysql connection id. - */ - public int getonnectionMap(final String channelId) { - return connectionCache.getIfPresent(channelId); - } - - /** - * Get instance of MySQLResultCache. - * - * @return instance of MySQLResultCache - */ - public static MySQLResultCache getInstance() { - return INSTANCE; - } -} +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.util; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; + +import java.util.concurrent.TimeUnit; + +/** + * cache for SynchronizedFuture. + * + * @author wangkai + */ +public class MySQLResultCache { + private static final MySQLResultCache INSTANCE = new MySQLResultCache(); + + //TODO expire time should be set. + private Cache resultCache = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.SECONDS).build(); + + private Cache connectionCache = CacheBuilder.newBuilder().build(); + + private Cache channelCache = CacheBuilder.newBuilder().build(); + + /** + * put synchronizedFuture by connectionId. + * + * @param connectionId mysql connection id. + * @param synchronizedFuture multiple result set. + */ + public void put(final int connectionId, final SynchronizedFuture synchronizedFuture) { + resultCache.put(connectionId, synchronizedFuture); + } + + /** + * get SynchronizedFuture by connectionId. + * + * @param connectionId mysql connection id. + * @return multiple result set. + */ + public SynchronizedFuture get(final int connectionId) { + return resultCache.getIfPresent(connectionId); + } + + /** + * delete SynchronizedFuture by connectionId. + * + * @param connectionId mysql connection id. + */ + public void delete(final int connectionId) { + resultCache.invalidate(connectionId); + } + + /** + * put connectionId by channelId. + * + * @param channelId netty channel id. + * @param connectionId mysql connection id. + */ + public void putConnectionMap(final String channelId, final int connectionId) { + connectionCache.put(channelId, connectionId); + } + + /** + * get connectionId by channelId. + * + * @param channelId netty channel id. + * @return connectionId mysql connection id. + */ + public int getConnectionMap(final String channelId) { + return connectionCache.getIfPresent(channelId); + } + + /** + * Get instance of MySQLResultCache. + * + * @return instance of MySQLResultCache + */ + public static MySQLResultCache getInstance() { + return INSTANCE; + } +} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/SynchronizedFuture.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/SynchronizedFuture.java index cd3631729b15c..f2984825d8d41 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/SynchronizedFuture.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/SynchronizedFuture.java @@ -1,96 +1,168 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.proxy.util; - -import com.google.common.collect.Lists; - -import java.util.List; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; - -/** - * sync get multiple netty return. - * - * @author wangkai - */ -public class SynchronizedFuture implements Future> { - - private CountDownLatch latch; - - private List responses; - - private long beginTime = System.currentTimeMillis(); - - public SynchronizedFuture(final int resultSize) { - latch = new CountDownLatch(resultSize); - responses = Lists.newArrayListWithCapacity(resultSize); - } - - @Override - public boolean cancel(final boolean mayInterruptIfRunning) { - return false; - } - - @Override - public boolean isCancelled() { - return false; - } - - @Override - public boolean isDone() { - if (null != responses && responses.size() > 0) { - return true; - } - return false; - } - - @Override - public List get() throws InterruptedException { - latch.await(); - return this.responses; - } - - /** - * wait for responses. - * @param timeout wait timeout. - * @param unit time unit - * @return responses. - */ - @Override - public List get(final long timeout, final TimeUnit unit) { - try { - if (latch.await(timeout, unit)) { - return this.responses; - } - } catch (InterruptedException e) { - //TODO - } - return this.responses; - } - - /** - * set response and count down. - * @param response sql command result. - */ - public void setResponse(final T response) { - this.responses.add(response); - latch.countDown(); - } - -} +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.util; + +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import com.google.common.collect.Lists; + +import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; +import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; +import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacket; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.FieldCountPacket; +import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket; +import io.shardingsphere.proxy.transport.mysql.packet.generic.ErrPacket; +import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket; + +/** + * sync get multiple netty return. + * + * @author wangkai + */ +public class SynchronizedFuture implements Future { + private boolean merged; + + private CountDownLatch latch; + + private List responses; + + private long beginTime = System.currentTimeMillis(); + + public SynchronizedFuture(final int resultSize) { + latch = new CountDownLatch(resultSize); + responses = Lists.newArrayListWithCapacity(resultSize); + } + + @Override + public boolean cancel(final boolean mayInterruptIfRunning) { + return false; + } + + @Override + public boolean isCancelled() { + return false; + } + + @Override + public boolean isDone() { + return merged ? true : false; + } + + @Override + public CommandResponsePackets get() throws InterruptedException { + latch.await(); + return merge(); + } + + /** + * wait for responses. + * @param timeout wait timeout. + * @param unit time unit + * @return responses. + */ + @Override + public CommandResponsePackets get(final long timeout, final TimeUnit unit) { + try { + latch.await(timeout, unit); + } catch (InterruptedException e) { + //TODO + } + return merge(); + } + + /** + * set response and count down. + * @param response sql command result. + */ + public void setResponse(final CommandResponsePackets response) { + responses.add(response); + latch.countDown(); + } + + private CommandResponsePackets merge() { + boolean isOkPacket = false; + int affectedRows = 0; + long lastInsertId = 0; + int sequenceId = 0; + CommandResponsePackets headPackets = new CommandResponsePackets(); + for (CommandResponsePackets each : responses) { + headPackets.addPacket(each.getHeadPacket()); + } + + for (DatabaseProtocolPacket each : headPackets.getDatabaseProtocolPackets()) { + if (each instanceof ErrPacket) { + return new CommandResponsePackets(each); + } + if (each instanceof OKPacket) { + isOkPacket = true; + OKPacket okPacket = (OKPacket) each; + affectedRows += okPacket.getAffectedRows(); + lastInsertId = okPacket.getLastInsertId(); + sequenceId = okPacket.getSequenceId(); + } + } + if (isOkPacket) { + return new CommandResponsePackets(new OKPacket(sequenceId, affectedRows, lastInsertId, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); + } + + CommandResponsePackets result = new CommandResponsePackets(); + FieldCountPacket fieldCountPacket = (FieldCountPacket) headPackets.getHeadPacket(); + result.addPacket(fieldCountPacket); + boolean isHeadReaded = false; + for (CommandResponsePackets each : responses) { + Iterator databaseProtocolPackets = each.getDatabaseProtocolPackets().iterator(); + + List columnDefinitionAndEOFPackets = Collections.emptyList(); + for (int i = 0; i < fieldCountPacket.getColumnCount(); i++) { + MySQLPacket columnDefinitionPacket = (MySQLPacket) databaseProtocolPackets.next(); + columnDefinitionPacket.setSequenceId(++sequenceId); + columnDefinitionAndEOFPackets.add(columnDefinitionPacket); // ColumnDefinition41Packet + } + MySQLPacket eofPacket = (MySQLPacket) databaseProtocolPackets.next(); + eofPacket.setSequenceId(++sequenceId); + columnDefinitionAndEOFPackets.add(eofPacket); // EofPacket + + List textResultSetRowPackets = Collections.emptyList(); + if (databaseProtocolPackets.hasNext()) { + while (databaseProtocolPackets.hasNext()) { + MySQLPacket textResultSetRowPacket = (MySQLPacket) databaseProtocolPackets.next(); + textResultSetRowPacket.setSequenceId(++sequenceId); + textResultSetRowPackets.add(textResultSetRowPacket); // TextResultSetRowPacket + } + textResultSetRowPackets.remove(textResultSetRowPackets.size() - 1); // remove EofPacket + } + + if (!isHeadReaded) { + isHeadReaded = true; + result.addPackets(columnDefinitionAndEOFPackets); + } else { + result.addPackets(textResultSetRowPackets); + } + } + result.addPacket(new EofPacket(++sequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue())); + + merged = true; + return result; + } +} From d3f75fb59c419cb7a3de03bf491bd288367f4e23 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 24 May 2018 12:05:42 +0800 Subject: [PATCH 121/351] provider --- .../com/saaavsaaa/client/section/ClientContext.java | 9 ++++++++- .../saaavsaaa/client/zookeeper/base/BaseClient.java | 5 +++-- .../client/zookeeper/base/BaseClientFactory.java | 2 +- .../client/retry/AsyncRetryCenterTest.java | 13 +++++++------ 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/section/ClientContext.java b/src/main/java/com/saaavsaaa/client/section/ClientContext.java index ab36865d1731b..ac5af1d69202a 100644 --- a/src/main/java/com/saaavsaaa/client/section/ClientContext.java +++ b/src/main/java/com/saaavsaaa/client/section/ClientContext.java @@ -10,7 +10,11 @@ public final class ClientContext { private final RetryPolicy retryPolicy; private final BaseClientFactory clientFactory; - private final IProvider provider; + private IProvider provider; + + public ClientContext(final RetryPolicy retryPolicy, final BaseClientFactory clientFactory) { + this(retryPolicy, clientFactory, null); + } public ClientContext(final RetryPolicy retryPolicy, final BaseClientFactory clientFactory, final IProvider provider) { this.retryPolicy = retryPolicy; @@ -29,4 +33,7 @@ public BaseClientFactory getClientFactory() { public IProvider getProvider() { return provider; } + public void setProvider(IProvider provider){ + this.provider = provider; + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index 7aa262f54e771..4d28eb7cf467f 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -58,6 +58,7 @@ public synchronized void start() throws IOException, InterruptedException { logger.debug("BaseClient scheme:{},auth:{}", scheme, auth); } CONNECTED.await(); + context.setProvider(new BaseProvider(rootNode, zooKeeper, watched, authorities)); } @Override @@ -179,8 +180,8 @@ void setAuthorities(final String scheme, final byte[] auth) { this.authorities = ZooDefs.Ids.CREATOR_ALL_ACL; } - void setContext(final RetryPolicy retryPolicy, final BaseClientFactory clientFactory){ - this.context = new ClientContext(retryPolicy, clientFactory, new BaseProvider(rootNode, zooKeeper, watched, authorities)); + void setContext(final ClientContext context){ + this.context = context; } public ClientContext getContext(){ return context; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java index 58a5312a4e7e4..498d0a000979c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java @@ -22,9 +22,9 @@ public abstract class BaseClientFactory { protected int sessionTimeoutMilliseconds; public synchronized IClient start() throws IOException, InterruptedException { + client.setContext(new ClientContext(retryPolicy, this)); // wait expand client.setRootNode(namespace); client.setAuthorities(scheme , auth); - client.setContext(retryPolicy, this); // wait expand client.start(); if (globalListener != null) { client.registerWatch(globalListener); diff --git a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java index 5b5666924a7fa..6e8a7452f84af 100644 --- a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java @@ -5,6 +5,7 @@ import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.ClientFactory; import com.saaavsaaa.client.zookeeper.TestSupport; import com.saaavsaaa.client.zookeeper.base.BaseClient; @@ -22,12 +23,11 @@ * Created by aaa */ public class AsyncRetryCenterTest { - private IProvider provider; private ClientContext context; @Before public void start() throws IOException, InterruptedException { - provider = ((BaseClient)createClient()).getContext().getProvider(); + context = ((BaseClient)createClient()).getContext(); AsyncRetryCenter.INSTANCE.init(new RetryPolicy(3, 3, 10)); AsyncRetryCenter.INSTANCE.start(); } @@ -55,12 +55,13 @@ public void nothing(){ public void create() throws InterruptedException, KeeperException { String key = "a"; String value = "bbb11"; + context.getProvider().create("/" + TestSupport.ROOT, Constants.NOTHING_VALUE, CreateMode.PERSISTENT); AsyncRetryCenter.INSTANCE.add(new TestCreateCurrentOperation(context, key, value, CreateMode.PERSISTENT)); Thread.sleep(1000); String path = PathUtil.getRealPath(TestSupport.ROOT, key); - assert provider.exists(path); - provider.delete(path); - provider.delete(provider.getRealPath(TestSupport.ROOT)); - assert !provider.exists(path); + assert context.getProvider().exists(path); + context.getProvider().delete(path); + context.getProvider().delete(context.getProvider().getRealPath(TestSupport.ROOT)); + assert !context.getProvider().exists(path); } } From 6cc81456fe9b2252c5343a86e9fc207a553d1c11 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 24 May 2018 15:33:11 +0800 Subject: [PATCH 122/351] listener --- .../com/saaavsaaa/client/action/IClient.java | 2 +- .../com/saaavsaaa/client/cache/PathTree.java | 2 +- .../client/election/LeaderElection.java | 4 +-- .../saaavsaaa/client/section/Listener.java | 27 ++++++++++++++-- .../client/section/WatcherCreator.java | 4 +-- .../client/zookeeper/base/BaseClient.java | 32 +++++++------------ .../client/retry/AsyncRetryCenterTest.java | 6 ---- .../client/zookeeper/TestSupport.java | 2 +- .../client/zookeeper/base/BaseClientTest.java | 17 ++++++---- 9 files changed, 55 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/action/IClient.java b/src/main/java/com/saaavsaaa/client/action/IClient.java index 44b5a48061a92..730ebd591fa90 100644 --- a/src/main/java/com/saaavsaaa/client/action/IClient.java +++ b/src/main/java/com/saaavsaaa/client/action/IClient.java @@ -12,7 +12,7 @@ public interface IClient extends IAction, IGroupAction{ void start() throws IOException, InterruptedException; void close() throws InterruptedException; - Watcher registerWatch(String key, Listener listener); + void registerWatch(String key, Listener listener); void unregisterWatch(String key); ZKTransaction transaction(); diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index f39da00d1a168..82edfa127704b 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -115,7 +115,7 @@ public void stopRefresh(){ public void watch(Listener listener){ if (listener == null){ - listener = new Listener() { + listener = new Listener(rootNode.get().getKey()) { @Override public void process(WatchedEvent event) { String path = event.getPath(); diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index 198cebac035b4..6d986e8d93c2c 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -33,7 +33,7 @@ private boolean contend(final String node, final IProvider provider, final Liste } catch (KeeperException.NodeExistsException e) { logger.info("contend not success"); // TODO: or changing_key node value == current client id - provider.exists(node, WatcherCreator.deleteWatcher(node, listener)); + provider.exists(node, WatcherCreator.deleteWatcher(listener)); } return success; } @@ -45,7 +45,7 @@ public void executeContention(final String nodeBeCompete, final IProvider provid boolean canBegin; String realNode = provider.getRealPath(nodeBeCompete); String contendNode = PathUtil.getRealPath(realNode, Constants.CHANGING_KEY); - canBegin = this.contend(contendNode, provider, new Listener() { + canBegin = this.contend(contendNode, provider, new Listener(contendNode) { @Override public void process(WatchedEvent event) { try { diff --git a/src/main/java/com/saaavsaaa/client/section/Listener.java b/src/main/java/com/saaavsaaa/client/section/Listener.java index ab1f4431169d0..a08f16e5cdf9a 100644 --- a/src/main/java/com/saaavsaaa/client/section/Listener.java +++ b/src/main/java/com/saaavsaaa/client/section/Listener.java @@ -5,6 +5,29 @@ /** * Created by aaa */ -public interface Listener { - void process(WatchedEvent event); +public abstract class Listener { + private final String key; + private String path; + + public Listener(){ + this(null); + } + public Listener(final String path){ + this.path = path; + this.key = path + System.currentTimeMillis(); + } + + public abstract void process(WatchedEvent event); + + public String getPath() { + return path; + } + + public void setPath(final String path){ + this.path = path; + } + + public String getKey() { + return key; + } } diff --git a/src/main/java/com/saaavsaaa/client/section/WatcherCreator.java b/src/main/java/com/saaavsaaa/client/section/WatcherCreator.java index 61a662f4fb6fd..ecaf3c0518276 100644 --- a/src/main/java/com/saaavsaaa/client/section/WatcherCreator.java +++ b/src/main/java/com/saaavsaaa/client/section/WatcherCreator.java @@ -12,11 +12,11 @@ */ public class WatcherCreator { private static final Logger logger = LoggerFactory.getLogger(WatcherCreator.class); - public static Watcher deleteWatcher(final String path, Listener listener){ + public static Watcher deleteWatcher(Listener listener){ return new Watcher() { @Override public void process(WatchedEvent event) { - if (path.equals(event.getPath()) && NodeDeleted.equals(event.getType())){ + if (listener.getPath().equals(event.getPath()) && NodeDeleted.equals(event.getType())){ listener.process(event); logger.debug("delete node event:{}", event.toString()); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index 4d28eb7cf467f..98042f746d1a4 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -28,7 +28,7 @@ public abstract class BaseClient implements IClient { private static final Logger logger = LoggerFactory.getLogger(BaseClient.class); private static final CountDownLatch CONNECTED = new CountDownLatch(1); - protected static final Map watchers = new ConcurrentHashMap<>(); + protected static final Map watchers = new ConcurrentHashMap<>(); protected final boolean watched = true; //false private boolean globalListenerRegistered = false; @@ -88,8 +88,12 @@ public void process(WatchedEvent event) { watchers.get(Constants.GLOBAL_LISTENER_KEY).process(event); logger.debug("BaseClient " + Constants.GLOBAL_LISTENER_KEY + " process"); } - if (Properties.INSTANCE.watchOn() && watchers.containsKey(event.getPath())){ - watchers.get(event.getPath()).process(event); + if (Properties.INSTANCE.watchOn()){ + for (Listener listener : watchers.values()) { + if (listener.getPath() == null || listener.getPath().equals(event.getPath())){ + listener.process(event); + } + } } } }; @@ -100,29 +104,17 @@ void registerWatch(final Listener globalListener){ logger.warn("global listener can only register one"); return; } - watchers.put(Constants.GLOBAL_LISTENER_KEY, new Watcher() { - @Override - public void process(WatchedEvent event) { - globalListener.process(event); - } - }); + watchers.put(Constants.GLOBAL_LISTENER_KEY, globalListener); globalListenerRegistered = true; logger.debug("globalListenerRegistered:{}", globalListenerRegistered); } @Override - public Watcher registerWatch(final String key, final Listener listener){ + public void registerWatch(final String key, final Listener listener){ String path = PathUtil.getRealPath(rootNode, key); -// listener.setKey(path); - Watcher watcher = new Watcher() { - @Override - public void process(WatchedEvent event) { - listener.process(event); - } - }; - watchers.put(path, watcher); + listener.setPath(path); + watchers.put(listener.getKey(), listener); logger.debug("register watcher:{}", path); - return watcher; } @Override @@ -155,7 +147,7 @@ private void createNamespace(final byte[] date) throws KeeperException, Interrup return; } rootExist = true; - zooKeeper.exists(rootNode, WatcherCreator.deleteWatcher(rootNode, new Listener() { + zooKeeper.exists(rootNode, WatcherCreator.deleteWatcher(new Listener(rootNode) { @Override public void process(WatchedEvent event) { rootExist = false; diff --git a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java index 6e8a7452f84af..7940e6d5ccfc0 100644 --- a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java @@ -45,12 +45,6 @@ public void stop(){ } - @Ignore - @Test - public void nothing(){ - - } - @Test public void create() throws InterruptedException, KeeperException { String key = "a"; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java b/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java index 9249bf8c3fd54..a37af67c69080 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java @@ -13,7 +13,7 @@ public class TestSupport { public static final String ROOT = "test"; public static Listener buildListener(){ - Listener listener = new Listener() { + Listener listener = new Listener(null) { @Override public void process(WatchedEvent event) { System.out.println("=========================================================="); diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java index e3818d1b29880..bfd9b27175686 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java @@ -210,9 +210,14 @@ protected void watch(IClient client) throws KeeperException, InterruptedExceptio Listener listener = buildListener(client, actual); String key = "a"; - Watcher watcher = client.registerWatch(key, listener); + client.registerWatch(key, listener); client.createCurrentOnly(key, "aaa", CreateMode.EPHEMERAL); - client.checkExists(key, watcher); + client.checkExists(key, new Watcher() { + @Override + public void process(WatchedEvent event) { + listener.process(event); + } + }); client.update(key, "value"); System.out.println(new String(client.getData(key))); assert client.getDataString(key).equals("value"); @@ -220,15 +225,15 @@ protected void watch(IClient client) throws KeeperException, InterruptedExceptio assert client.getDataString(key).equals("value1"); client.update(key, "value2"); assert client.getDataString(key).equals("value2"); - client.deleteCurrentBranch(key); Thread.sleep(100); + client.deleteCurrentBranch(key); assert expected.size() == actual.size(); assert expected.containsAll(actual); client.unregisterWatch(key); } protected Listener buildListener(IClient client, List actual){ - Listener listener = new Listener() { + Listener listener = new Listener(null) { @Override public void process(WatchedEvent event) { System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); @@ -241,10 +246,10 @@ public void process(WatchedEvent event) { String result; try { result = new String(getZooKeeper(client).getData(event.getPath(),false, null)); - System.out.println(); + System.out.println(result); } catch (KeeperException e) { result = e.getMessage(); - e.printStackTrace(); + System.out.println(result); } catch (InterruptedException e) { result = e.getMessage(); e.printStackTrace(); From d39152d8751fb6d418f65efbabb158c5ecb8e134 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 24 May 2018 15:47:10 +0800 Subject: [PATCH 123/351] clear stop --- src/main/java/com/saaavsaaa/client/cache/PathTree.java | 10 +++++++++- .../saaavsaaa/client/zookeeper/base/BaseClient.java | 8 ++++---- .../client/zookeeper/base/BaseClientTest.java | 4 ++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index 82edfa127704b..5cd0c1a84da96 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -145,8 +145,16 @@ public void process(WatchedEvent event) { } }; } - logger.debug("PathTree Watch:{}", rootNode.get().getKey()); + final String key = listener.getKey(); + logger.debug("PathTree Watch:{}", key); client.registerWatch(rootNode.get().getKey(), listener); + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + @Override + public void run() { + logger.debug("PathTree Unregister Watch:{}", key); + client.unregisterWatch(key); + } + })); } public PathStatus getStatus() { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index 98042f746d1a4..c707887b0f2bf 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -122,10 +122,10 @@ public void unregisterWatch(final String key){ if (StringUtil.isNullOrBlank(key)){ throw new IllegalArgumentException("key should not be blank"); } - String path = PathUtil.getRealPath(rootNode, key); - if (watchers.containsKey(path)){ - watchers.remove(path); - logger.debug("unregisterWatch:{}", path); +// String path = PathUtil.getRealPath(rootNode, key); + if (watchers.containsKey(key)){ + watchers.remove(key); + logger.debug("unregisterWatch:{}", key); } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java index bfd9b27175686..3e51d3342adbf 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java @@ -229,7 +229,7 @@ public void process(WatchedEvent event) { client.deleteCurrentBranch(key); assert expected.size() == actual.size(); assert expected.containsAll(actual); - client.unregisterWatch(key); + client.unregisterWatch(listener.getKey()); } protected Listener buildListener(IClient client, List actual){ @@ -249,7 +249,7 @@ public void process(WatchedEvent event) { System.out.println(result); } catch (KeeperException e) { result = e.getMessage(); - System.out.println(result); + e.printStackTrace(); } catch (InterruptedException e) { result = e.getMessage(); e.printStackTrace(); From c63d7ea9d4ff260e08a5197bb51d67f8446624f9 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 24 May 2018 18:35:23 +0800 Subject: [PATCH 124/351] sync retry begin --- .../com/saaavsaaa/client/cache/PathTree.java | 164 +++++++++++------- .../client/retry/AsyncRetryCenter.java | 24 +-- .../client/retry/DelayPolicyExecutor.java | 48 +++++ .../client/retry/DelayRetryExecution.java | 49 ------ ...RetryPolicy.java => DelayRetryPolicy.java} | 10 +- .../saaavsaaa/client/section/Callable.java | 64 ++++++- .../client/section/ClientContext.java | 16 +- .../client/zookeeper/CacheClient.java | 2 +- .../client/zookeeper/ClientFactory.java | 7 +- .../client/zookeeper/UsualClient.java | 9 +- .../client/zookeeper/base/BaseClient.java | 4 +- .../zookeeper/base/BaseClientFactory.java | 8 +- .../client/zookeeper/base/BaseOperation.java | 21 ++- .../strategy/AllAsyncRetryStrategy.java | 4 +- .../strategy/AsyncRetryStrategy.java | 8 +- .../zookeeper/strategy/SyncRetryStrategy.java | 96 ++++------ .../client/retry/AsyncRetryCenterTest.java | 4 +- 17 files changed, 291 insertions(+), 247 deletions(-) create mode 100644 src/main/java/com/saaavsaaa/client/retry/DelayPolicyExecutor.java delete mode 100644 src/main/java/com/saaavsaaa/client/retry/DelayRetryExecution.java rename src/main/java/com/saaavsaaa/client/retry/{RetryPolicy.java => DelayRetryPolicy.java} (72%) diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index 5cd0c1a84da96..9a9d2d627d863 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -21,6 +21,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.locks.ReentrantLock; /* * Created by aaa @@ -28,6 +29,7 @@ */ public final class PathTree { private static final Logger logger = LoggerFactory.getLogger(PathTree.class); + private final transient ReentrantLock lock = new ReentrantLock(); private final AtomicReference rootNode = new AtomicReference<>(); private boolean executorStart = false; private ScheduledExecutorService cacheService; @@ -42,27 +44,33 @@ public PathTree(final String root, final IClient client) { this.provider = ((BaseClient)client).getContext().getProvider(); } - public synchronized void loading() throws KeeperException, InterruptedException { - if (Status == Status.RELEASE){ - logger.debug("loading Status:{}", Status); - this.setStatus(PathStatus.CHANGING); - - PathNode newRoot = new PathNode(rootNode.get().getKey()); - List children = provider.getChildren(rootNode.get().getKey()); - children.remove(PathUtil.getRealPath(rootNode.get().getKey(), Constants.CHANGING_KEY)); - this.attechIntoNode(children, newRoot); - rootNode.set(newRoot); - - this.setStatus(PathStatus.RELEASE); - logger.debug("loading release:{}", Status); - } else { - logger.info("loading but cache status not release"); - try { - Thread.sleep(10L); - } catch (InterruptedException e) { - logger.error("loading sleep error:{}", e.getMessage(), e); + public void loading() throws KeeperException, InterruptedException { + final ReentrantLock lock = this.lock; + lock.lockInterruptibly(); + try { + if (Status == Status.RELEASE) { + logger.debug("loading Status:{}", Status); + this.setStatus(PathStatus.CHANGING); + + PathNode newRoot = new PathNode(rootNode.get().getKey()); + List children = provider.getChildren(rootNode.get().getKey()); + children.remove(PathUtil.getRealPath(rootNode.get().getKey(), Constants.CHANGING_KEY)); + this.attechIntoNode(children, newRoot); + rootNode.set(newRoot); + + this.setStatus(PathStatus.RELEASE); + logger.debug("loading release:{}", Status); + } else { + logger.info("loading but cache status not release"); + try { + Thread.sleep(10L); + } catch (InterruptedException e) { + logger.error("loading sleep error:{}", e.getMessage(), e); + } + loading(); } - loading(); + } finally { + lock.unlock(); } } @@ -82,29 +90,41 @@ private void attechIntoNode(final List children, final PathNode pathNode } public void refreshPeriodic(final long period){ - if (executorStart){ - throw new IllegalArgumentException("period already set"); - } - long threadPeriod = period; - if (threadPeriod < 1){ - threadPeriod = Properties.INSTANCE.getThreadPeriod(); - } - logger.debug("refreshPeriodic:{}", period); - cacheService = Executors.newSingleThreadScheduledExecutor(); - cacheService.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { - logger.debug("cacheService run:{}", getStatus()); - if (PathStatus.RELEASE == getStatus()) { - try { - loading(); - } catch (Exception e) { - logger.error(e.getMessage(), e); + final ReentrantLock lock = this.lock; + lock.lock(); + try { + if (executorStart) { + throw new IllegalArgumentException("period already set"); + } + long threadPeriod = period; + if (threadPeriod < 1) { + threadPeriod = Properties.INSTANCE.getThreadPeriod(); + } + logger.debug("refreshPeriodic:{}", period); + cacheService = Executors.newSingleThreadScheduledExecutor(); + cacheService.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + logger.debug("cacheService run:{}", getStatus()); + if (PathStatus.RELEASE == getStatus()) { + try { + loading(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } } } - } - }, Properties.INSTANCE.getThreadInitialDelay(), threadPeriod, TimeUnit.MILLISECONDS); - executorStart = true; + }, Properties.INSTANCE.getThreadInitialDelay(), threadPeriod, TimeUnit.MILLISECONDS); + executorStart = true; + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + @Override + public void run() { + stopRefresh(); + } + })); + } finally { + lock.unlock(); + } } public void stopRefresh(){ @@ -214,34 +234,46 @@ private PathNode get(final String path){ return null; } - public synchronized void put(final String path, final String value) { - logger.debug("cache put:{},value:{}", path, value); - PathUtils.validatePath(path); - logger.debug("put Status:{}", Status); - if (Status == Status.RELEASE){ - if (path.equals(rootNode.get().getKey())){ - rootNode.set(new PathNode(rootNode.get().getKey(), value.getBytes(Constants.UTF_8))); - return; - } - this.setStatus(PathStatus.CHANGING); - rootNode.get().set(keyIterator(path), value); - this.setStatus(PathStatus.RELEASE); - } else { - try { - logger.debug("put but cache status not release"); - Thread.sleep(10L); - } catch (InterruptedException e) { - logger.error("put sleep error:{}", e.getMessage(), e); + public void put(final String path, final String value) { + final ReentrantLock lock = this.lock; + lock.lock(); + try { + logger.debug("cache put:{},value:{}", path, value); + PathUtils.validatePath(path); + logger.debug("put Status:{}", Status); + if (Status == Status.RELEASE) { + if (path.equals(rootNode.get().getKey())) { + rootNode.set(new PathNode(rootNode.get().getKey(), value.getBytes(Constants.UTF_8))); + return; + } + this.setStatus(PathStatus.CHANGING); + rootNode.get().set(keyIterator(path), value); + this.setStatus(PathStatus.RELEASE); + } else { + try { + logger.debug("put but cache status not release"); + Thread.sleep(10L); + } catch (InterruptedException e) { + logger.error("put sleep error:{}", e.getMessage(), e); + } + put(path, value); } - put(path, value); + } finally { + lock.unlock(); } } - public synchronized void delete(String path) { - PathUtils.validatePath(path); - String prxpath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); - PathNode node = get(prxpath); - node.getChildren().remove(path); - logger.debug("PathTree delete:{}", path); + public void delete(String path) { + final ReentrantLock lock = this.lock; + lock.lock(); + try { + PathUtils.validatePath(path); + String prxpath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); + PathNode node = get(prxpath); + node.getChildren().remove(path); + logger.debug("PathTree delete:{}", path); + } finally { + lock.unlock(); + } } } diff --git a/src/main/java/com/saaavsaaa/client/retry/AsyncRetryCenter.java b/src/main/java/com/saaavsaaa/client/retry/AsyncRetryCenter.java index d88c6645f608e..8b4416e5208f6 100644 --- a/src/main/java/com/saaavsaaa/client/retry/AsyncRetryCenter.java +++ b/src/main/java/com/saaavsaaa/client/retry/AsyncRetryCenter.java @@ -17,32 +17,32 @@ public enum AsyncRetryCenter { private final RetryThread retryThread = new RetryThread(queue); private boolean started = false; - private RetryPolicy retryPolicy; + private DelayRetryPolicy delayRetryPolicy; - public void init(RetryPolicy retryPolicy) { - logger.debug("retryPolicy init"); - if (retryPolicy == null){ - logger.warn("retryPolicy is null and auto init with RetryPolicy.newNoInitDelayPolicy"); - retryPolicy = RetryPolicy.newNoInitDelayPolicy(); + public void init(DelayRetryPolicy delayRetryPolicy) { + logger.debug("delayRetryPolicy init"); + if (delayRetryPolicy == null){ + logger.warn("delayRetryPolicy is null and auto init with DelayRetryPolicy.newNoInitDelayPolicy"); + delayRetryPolicy = DelayRetryPolicy.newNoInitDelayPolicy(); } - this.retryPolicy = retryPolicy; + this.delayRetryPolicy = delayRetryPolicy; } public synchronized void start(){ if (started){ return; } - this.started = true; retryThread.setName("retry-thread"); retryThread.start(); + this.started = true; } public void add(BaseOperation operation){ - if (retryPolicy == null){ - logger.warn("retryPolicy no init and auto init with RetryPolicy.newNoInitDelayPolicy"); - retryPolicy = RetryPolicy.newNoInitDelayPolicy(); + if (delayRetryPolicy == null){ + logger.warn("delayRetryPolicy no init and auto init with DelayRetryPolicy.newNoInitDelayPolicy"); + delayRetryPolicy = DelayRetryPolicy.newNoInitDelayPolicy(); } - operation.setRetrial(new DelayRetryExecution(retryPolicy)); + operation.setRetrial(new DelayPolicyExecutor(delayRetryPolicy)); queue.offer(operation); logger.debug("enqueue operation:{}", operation.toString()); } diff --git a/src/main/java/com/saaavsaaa/client/retry/DelayPolicyExecutor.java b/src/main/java/com/saaavsaaa/client/retry/DelayPolicyExecutor.java new file mode 100644 index 0000000000000..e06a0109dba34 --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/retry/DelayPolicyExecutor.java @@ -0,0 +1,48 @@ +package com.saaavsaaa.client.retry; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Random; + +/** + * Created by aaa + */ +public class DelayPolicyExecutor { + private static final Logger logger = LoggerFactory.getLogger(DelayPolicyExecutor.class); + private final DelayRetryPolicy delayRetryPolicy; + private final Random random; + + private int executeCount = 0; + private long executeTick; + + public DelayPolicyExecutor(){ + this(DelayRetryPolicy.newNoInitDelayPolicy()); + } + + public DelayPolicyExecutor(final DelayRetryPolicy delayRetryPolicy) { + this.delayRetryPolicy = delayRetryPolicy; + this.executeTick = System.currentTimeMillis(); + this.random = new Random(); + next(); + } + + public boolean hasNext() { + return executeCount < delayRetryPolicy.getRetryCount(); + } + + public long getNextTick() { + return executeTick; + } + + public void next() { + executeCount ++; + long sleep = delayRetryPolicy.getBaseDelay() * Math.max(1, this.random.nextInt(1 << delayRetryPolicy.getRetryCount() + 1)); + if (sleep < delayRetryPolicy.getDelayUpperBound()){ + executeTick += sleep; + } else { + executeTick += delayRetryPolicy.getDelayUpperBound(); + } + logger.debug("next executeCount:{}, executeTick:{}", executeCount, executeTick); + } +} diff --git a/src/main/java/com/saaavsaaa/client/retry/DelayRetryExecution.java b/src/main/java/com/saaavsaaa/client/retry/DelayRetryExecution.java deleted file mode 100644 index feddf207eed70..0000000000000 --- a/src/main/java/com/saaavsaaa/client/retry/DelayRetryExecution.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.saaavsaaa.client.retry; - -import com.saaavsaaa.client.section.Callable; - -import java.util.Random; - -/** - * Created by aaa - */ -public class DelayRetryExecution { - private final RetryPolicy retryPolicy; - private final Random random; - - private int executeCount = 0; - private long executeTick; - - public DelayRetryExecution(){ - this(RetryPolicy.newNoInitDelayPolicy()); - } - - public DelayRetryExecution(final RetryPolicy retryPolicy) { - this.retryPolicy = retryPolicy; - this.executeTick = System.currentTimeMillis(); - this.random = new Random(); - next(); - } - - public boolean hasNext() { - return executeCount < retryPolicy.getRetryCount(); - } - - public long getNextTick() { - return executeTick; - } - - public void next() { - executeCount ++; - long sleep = retryPolicy.getBaseDelay() * Math.max(1, this.random.nextInt(1 << retryPolicy.getRetryCount() + 1)); - if (sleep < retryPolicy.getDelayUpperBound()){ - executeTick += sleep; - } else { - executeTick += retryPolicy.getDelayUpperBound(); - } - } - - public void call(final Callable callable){ - - } -} diff --git a/src/main/java/com/saaavsaaa/client/retry/RetryPolicy.java b/src/main/java/com/saaavsaaa/client/retry/DelayRetryPolicy.java similarity index 72% rename from src/main/java/com/saaavsaaa/client/retry/RetryPolicy.java rename to src/main/java/com/saaavsaaa/client/retry/DelayRetryPolicy.java index 47704554f71ef..d754d6cdc9151 100644 --- a/src/main/java/com/saaavsaaa/client/retry/RetryPolicy.java +++ b/src/main/java/com/saaavsaaa/client/retry/DelayRetryPolicy.java @@ -3,7 +3,7 @@ /** * Created by aaa */ -public class RetryPolicy { +public class DelayRetryPolicy { private static final long BASE_DELAY = 10; private static final int BASE_COUNT = 3; private static final int RETRY_COUNT_BOUND = 29; @@ -15,11 +15,11 @@ public class RetryPolicy { /* * Millis */ - public RetryPolicy(long baseDelay) { + public DelayRetryPolicy(long baseDelay) { this(RETRY_COUNT_BOUND, baseDelay, Integer.MAX_VALUE); } - public RetryPolicy(int retryCount, long baseDelay, long delayUpperBound) { + public DelayRetryPolicy(int retryCount, long baseDelay, long delayUpperBound) { this.retryCount = retryCount; this.baseDelay = baseDelay; this.delayUpperBound = delayUpperBound; @@ -37,7 +37,7 @@ public long getDelayUpperBound() { return delayUpperBound; } - public static RetryPolicy newNoInitDelayPolicy(){ - return new RetryPolicy(BASE_COUNT, BASE_DELAY, Integer.MAX_VALUE); + public static DelayRetryPolicy newNoInitDelayPolicy(){ + return new DelayRetryPolicy(BASE_COUNT, BASE_DELAY, Integer.MAX_VALUE); } } diff --git a/src/main/java/com/saaavsaaa/client/section/Callable.java b/src/main/java/com/saaavsaaa/client/section/Callable.java index 3070a5d5ff177..81c4d31c6e410 100644 --- a/src/main/java/com/saaavsaaa/client/section/Callable.java +++ b/src/main/java/com/saaavsaaa/client/section/Callable.java @@ -1,10 +1,68 @@ package com.saaavsaaa.client.section; +import com.saaavsaaa.client.retry.DelayPolicyExecutor; +import com.saaavsaaa.client.retry.DelayRetryPolicy; import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** - * Created by aaa on 18-5-21. + * Created by aaa */ -public interface Callable { - T call() throws KeeperException, InterruptedException; +public abstract class Callable { + private static final Logger logger = LoggerFactory.getLogger(Callable.class); + private final Connection connection; + protected final DelayPolicyExecutor delayPolicyExecutor; + private T result; + public Callable(final ClientContext context){ + this.connection = new Connection(context); + DelayRetryPolicy delayRetryPolicy = context.getDelayRetryPolicy(); + if (delayRetryPolicy == null){ + logger.warn("Callable constructor context's delayRetryPolicy is null"); + delayRetryPolicy = DelayRetryPolicy.newNoInitDelayPolicy(); + } + this.delayPolicyExecutor = new DelayPolicyExecutor(delayRetryPolicy); + } + public abstract void call() throws KeeperException, InterruptedException; + + public void setResult(T result) { + this.result = result; + } + public T getResult() throws KeeperException, InterruptedException { + exec(); + return result; + } + + public void exec() throws KeeperException, InterruptedException { + try { + call(); + } catch (KeeperException e) { + logger.warn("exec KeeperException:{}", e.getMessage()); + delayPolicyExecutor.next(); + connection.check(e); + execDelay(); + } catch (InterruptedException e) { + throw e; + } + } + + private void execDelay() throws KeeperException, InterruptedException { + for (;;) { + long delay = delayPolicyExecutor.getNextTick() - System.currentTimeMillis(); + if (delay > 0){ + try { + logger.debug("exec delay:{}", delay); + Thread.sleep(delay); + } catch (InterruptedException ee) { + throw ee; + } + } else { + if (delayPolicyExecutor.hasNext()) { + logger.debug("exec hasNext"); + exec(); + } + break; + } + } + } } diff --git a/src/main/java/com/saaavsaaa/client/section/ClientContext.java b/src/main/java/com/saaavsaaa/client/section/ClientContext.java index ac5af1d69202a..f9581669bf33a 100644 --- a/src/main/java/com/saaavsaaa/client/section/ClientContext.java +++ b/src/main/java/com/saaavsaaa/client/section/ClientContext.java @@ -1,29 +1,29 @@ package com.saaavsaaa.client.section; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.retry.RetryPolicy; +import com.saaavsaaa.client.retry.DelayRetryPolicy; import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; /** * Created by aaa */ public final class ClientContext { - private final RetryPolicy retryPolicy; + private final DelayRetryPolicy delayRetryPolicy; private final BaseClientFactory clientFactory; private IProvider provider; - public ClientContext(final RetryPolicy retryPolicy, final BaseClientFactory clientFactory) { - this(retryPolicy, clientFactory, null); + public ClientContext(final DelayRetryPolicy delayRetryPolicy, final BaseClientFactory clientFactory) { + this(delayRetryPolicy, clientFactory, null); } - public ClientContext(final RetryPolicy retryPolicy, final BaseClientFactory clientFactory, final IProvider provider) { - this.retryPolicy = retryPolicy; + public ClientContext(final DelayRetryPolicy delayRetryPolicy, final BaseClientFactory clientFactory, final IProvider provider) { + this.delayRetryPolicy = delayRetryPolicy; this.clientFactory = clientFactory; this.provider = provider; } - public RetryPolicy getRetryPolicy() { - return retryPolicy; + public DelayRetryPolicy getDelayRetryPolicy() { + return delayRetryPolicy; } public BaseClientFactory getClientFactory() { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 085324452929b..6a310a2d324e4 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -24,7 +24,7 @@ public final class CacheClient extends UsualClient { } @Override - public synchronized void start() throws IOException, InterruptedException { + public void start() throws IOException, InterruptedException { super.start(); try { useCacheStrategy(CacheStrategy.WATCH); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 4a3ca48f290c2..3692effe93623 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -1,7 +1,6 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.retry.RetryPolicy; -import com.saaavsaaa.client.section.ClientContext; +import com.saaavsaaa.client.retry.DelayRetryPolicy; import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; @@ -64,8 +63,8 @@ public ClientFactory authorization(final String scheme, final byte[] auth){ return this; } - public ClientFactory setRetryPolicy(final RetryPolicy retryPolicy){ - this.retryPolicy = retryPolicy; + public ClientFactory setRetryPolicy(final DelayRetryPolicy delayRetryPolicy){ + this.delayRetryPolicy = delayRetryPolicy; return this; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index d0ae2a7efe143..076236f9b44c8 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -3,7 +3,6 @@ import com.saaavsaaa.client.action.IExecStrategy; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.strategy.*; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.*; @@ -28,7 +27,7 @@ public class UsualClient extends BaseClient { } @Override - public synchronized void start() throws IOException, InterruptedException { + public void start() throws IOException, InterruptedException { super.start(); useExecStrategy(StrategyType.USUAL); } @@ -53,15 +52,15 @@ public synchronized void useExecStrategy(StrategyType strategyType) { } case SYNC_RETRY:{ - strategy = new SyncRetryStrategy(provider, context.getRetryPolicy()); + strategy = new SyncRetryStrategy(context); break; } case ASYNC_RETRY:{ - strategy = new AsyncRetryStrategy(provider, context); + strategy = new AsyncRetryStrategy(context); break; } case ALL_ASYNC_RETRY:{ - strategy = new AllAsyncRetryStrategy(provider, context); + strategy = new AllAsyncRetryStrategy(context); break; } default:{ diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index c707887b0f2bf..38ca3afb1df8b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -1,8 +1,6 @@ package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.retry.RetryPolicy; import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.StringUtil; @@ -50,7 +48,7 @@ protected BaseClient(final String servers, final int sessionTimeoutMilliseconds) } @Override - public synchronized void start() throws IOException, InterruptedException { + public void start() throws IOException, InterruptedException { logger.debug("BaseClient servers:{},sessionTimeOut:{}", servers, sessionTimeOut); zooKeeper = new ZooKeeper(servers, sessionTimeOut, startWatcher()); if (!StringUtil.isNullOrBlank(scheme)) { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java index 498d0a000979c..c960667ab0235 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.retry.RetryPolicy; +import com.saaavsaaa.client.retry.DelayRetryPolicy; import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.section.Listener; @@ -16,13 +16,13 @@ public abstract class BaseClientFactory { protected String namespace; protected String scheme; protected byte[] auth; - protected RetryPolicy retryPolicy; + protected DelayRetryPolicy delayRetryPolicy; protected String servers; protected int sessionTimeoutMilliseconds; - public synchronized IClient start() throws IOException, InterruptedException { - client.setContext(new ClientContext(retryPolicy, this)); // wait expand + public IClient start() throws IOException, InterruptedException { + client.setContext(new ClientContext(delayRetryPolicy, this)); // wait expand client.setRootNode(namespace); client.setAuthorities(scheme , auth); client.start(); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java index c11f10fe974d6..74e9e67fffc8b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java @@ -1,11 +1,8 @@ package com.saaavsaaa.client.zookeeper.base; -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.retry.DelayRetryExecution; +import com.saaavsaaa.client.retry.DelayPolicyExecutor; import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.section.Connection; -import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.KeeperException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,19 +16,21 @@ public abstract class BaseOperation implements Delayed { private static final Logger logger = LoggerFactory.getLogger(BaseOperation.class); protected final ClientContext context; - protected DelayRetryExecution retryExecution; + private final Connection connection; + protected DelayPolicyExecutor delayPolicyExecutor; protected BaseOperation(final ClientContext context) { this.context = context; + connection = new Connection(context); } - public void setRetrial(final DelayRetryExecution retryExecution){ - this.retryExecution = retryExecution; + public void setRetrial(final DelayPolicyExecutor delayPolicyExecutor){ + this.delayPolicyExecutor = delayPolicyExecutor; } @Override public long getDelay(TimeUnit unit) { - long absoluteBlock = this.retryExecution.getNextTick() - System.currentTimeMillis(); + long absoluteBlock = this.delayPolicyExecutor.getNextTick() - System.currentTimeMillis(); logger.debug("queue getDelay block:{}", absoluteBlock); long result = unit.convert(absoluteBlock, TimeUnit.MILLISECONDS); return result; @@ -56,11 +55,11 @@ public boolean executeOperation() throws KeeperException, InterruptedException { execute(); result = true; } catch (KeeperException ee) { - new Connection(context).check(ee); + connection.check(ee); result = false; } - if (!result && retryExecution.hasNext()){ - retryExecution.next(); + if (!result && delayPolicyExecutor.hasNext()){ + delayPolicyExecutor.next(); return true; } return false; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java index bf723a299365b..c2e8c1362acc2 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java @@ -17,8 +17,8 @@ */ public class AllAsyncRetryStrategy extends AsyncRetryStrategy { private static final Logger logger = LoggerFactory.getLogger(AllAsyncRetryStrategy.class); - public AllAsyncRetryStrategy(final IProvider provider, final ClientContext context) { - super(provider, context); + public AllAsyncRetryStrategy(final ClientContext context) { + super(context); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java index 55f5a530358fe..c40a9904de774 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java @@ -3,8 +3,6 @@ import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.AsyncRetryCenter; import com.saaavsaaa.client.section.ClientContext; -import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentOperation; import com.saaavsaaa.client.zookeeper.operation.UpdateOperation; @@ -20,10 +18,10 @@ public class AsyncRetryStrategy extends SyncRetryStrategy { private static final Logger logger = LoggerFactory.getLogger(AsyncRetryStrategy.class); protected final ClientContext context; - public AsyncRetryStrategy(final IProvider provider, final ClientContext context){ - super(provider, context.getRetryPolicy()); + public AsyncRetryStrategy(final ClientContext context){ + super(context); this.context = context; - AsyncRetryCenter.INSTANCE.init(retryPolicy); + AsyncRetryCenter.INSTANCE.init(context.getDelayRetryPolicy()); AsyncRetryCenter.INSTANCE.start(); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java index 3b30bf9a7edf5..1eb8e5aec45ae 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java @@ -1,13 +1,10 @@ package com.saaavsaaa.client.zookeeper.strategy; -import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.retry.DelayRetryPolicy; import com.saaavsaaa.client.retry.RetryCount; -import com.saaavsaaa.client.retry.RetryPolicy; import com.saaavsaaa.client.section.Callable; import com.saaavsaaa.client.section.ClientContext; -import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.Watcher; @@ -21,99 +18,66 @@ */ public class SyncRetryStrategy extends UsualStrategy{ private static final Logger logger = LoggerFactory.getLogger(SyncRetryStrategy.class); - protected final RetryPolicy retryPolicy; + protected final ClientContext context; - public SyncRetryStrategy(final IProvider provider, final RetryPolicy retryPolicy) { - super(provider); - this.retryPolicy = retryPolicy; + public SyncRetryStrategy(final ClientContext context) { + super(context.getProvider()); + this.context = context; } @Override public byte[] getData(final String key) throws KeeperException, InterruptedException { - Callable callable = new Callable() { + Callable callable = new Callable(context) { @Override - public byte[] call() throws KeeperException, InterruptedException { - return provider.getData(provider.getRealPath(key)); + public void call() throws KeeperException, InterruptedException { + setResult(provider.getData(provider.getRealPath(key))); } }; - - String path = provider.getRealPath(key); - try { - return provider.getData(path); - } catch (KeeperException ee){ - logger.warn("AsyncRetryStrategy KeeperException getData:{}", path); - - if (RetryCount.INSTANCE.continueExecute()) { - byte[] data = getData(path); - RetryCount.INSTANCE.reset(); - return data; - } - throw ee; - } + return callable.getResult(); } @Override public boolean checkExists(final String key) throws KeeperException, InterruptedException { - String path = provider.getRealPath(key); - try { - return super.checkExists(path); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("AsyncRetryStrategy KeeperException checkExists:{}", path); - if (RetryCount.INSTANCE.continueExecute()) { - boolean result = checkExists(path); - RetryCount.INSTANCE.reset(); - return result; + Callable callable = new Callable(context) { + @Override + public void call() throws KeeperException, InterruptedException { + setResult(provider.exists(provider.getRealPath(key))); } - throw ee; - } + }; + return callable.getResult(); } @Override public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { - String path = provider.getRealPath(key); - try { - return super.checkExists(path, watcher); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("AsyncRetryStrategy KeeperException checkExists:{}", path); - if (RetryCount.INSTANCE.continueExecute()) { - boolean result = checkExists(path, watcher); - RetryCount.INSTANCE.reset(); - return result; + Callable callable = new Callable(context) { + @Override + public void call() throws KeeperException, InterruptedException { + setResult(provider.exists(provider.getRealPath(key), watcher)); } - throw ee; - } + }; + return callable.getResult(); } @Override public List getChildren(final String key) throws KeeperException, InterruptedException { - String path = provider.getRealPath(key); - try { - return provider.getChildren(path); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("AsyncRetryStrategy KeeperException getChildren:{}", path); - if (RetryCount.INSTANCE.continueExecute()) { - List result = getChildren(path); - RetryCount.INSTANCE.reset(); - return result; + Callable> callable = new Callable(context) { + @Override + public void call() throws KeeperException, InterruptedException { + setResult(provider.getChildren(provider.getRealPath(key))); } - throw ee; - } + }; + return callable.getResult(); } @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { String path = provider.getRealPath(key); - Callable callable = new Callable() { + Callable callable = new Callable(context) { @Override - public Object call() throws KeeperException, InterruptedException { + public void call() throws KeeperException, InterruptedException { provider.create(path, value, createMode); - return null; } }; - /*try { - - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("AsyncRetryStrategy SessionExpiredException createCurrentOnly:{}", path); - }*/ + callable.exec(); } } diff --git a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java index 7940e6d5ccfc0..4bd48d3c46c91 100644 --- a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java @@ -1,7 +1,6 @@ package com.saaavsaaa.client.retry; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.utility.PathUtil; @@ -14,7 +13,6 @@ import org.apache.zookeeper.KeeperException; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import java.io.IOException; @@ -28,7 +26,7 @@ public class AsyncRetryCenterTest { @Before public void start() throws IOException, InterruptedException { context = ((BaseClient)createClient()).getContext(); - AsyncRetryCenter.INSTANCE.init(new RetryPolicy(3, 3, 10)); + AsyncRetryCenter.INSTANCE.init(new DelayRetryPolicy(3, 3, 10)); AsyncRetryCenter.INSTANCE.start(); } From 095c6fca46c72975af8de06aa35a158727016924 Mon Sep 17 00:00:00 2001 From: WangKai Date: Fri, 25 May 2018 16:06:19 +0800 Subject: [PATCH 125/351] supported connection pool --- .../proxy/backend/ShardingProxyClient.java | 102 +++++++++++++----- .../common/SQLPacketsBackendHandler.java | 34 +++--- .../backend/mysql/MySQLBackendHandler.java | 1 + 3 files changed, 98 insertions(+), 39 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java index 063bba63a4766..76a59cc34e35f 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java @@ -22,19 +22,24 @@ import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.epoll.EpollChannelOption; import io.netty.channel.epoll.EpollEventLoopGroup; import io.netty.channel.epoll.EpollSocketChannel; import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.pool.AbstractChannelPoolMap; +import io.netty.channel.pool.ChannelPoolHandler; +import io.netty.channel.pool.ChannelPoolMap; +import io.netty.channel.pool.FixedChannelPool; +import io.netty.channel.pool.SimpleChannelPool; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.shardingsphere.proxy.backend.netty.ClientHandlerInitializer; import io.shardingsphere.proxy.config.RuleRegistry; import lombok.Getter; +import lombok.extern.slf4j.Slf4j; import java.net.MalformedURLException; import java.net.URL; @@ -45,6 +50,7 @@ * * @author wangkai */ +@Slf4j public final class ShardingProxyClient { private static final ShardingProxyClient INSTANCE = new ShardingProxyClient(); @@ -53,10 +59,9 @@ public final class ShardingProxyClient { private EventLoopGroup workerGroup; @Getter - private Map channelMap = Maps.newHashMap(); + private ChannelPoolMap poolMap; - @Getter - private Map bootstrapMap = Maps.newHashMap(); + private Map dataScourceConfigMap = Maps.newHashMap(); /** * Start Sharding-Proxy. @@ -68,22 +73,27 @@ public void start() throws MalformedURLException, InterruptedException { Map dataSourceConfigurationMap = RuleRegistry.getInstance().getDataSourceConfigurationMap(); for (Map.Entry each : dataSourceConfigurationMap.entrySet()) { URL url = new URL(each.getValue().getJdbcUrl().replaceAll("jdbc:mysql:", "http:")); - String ip = url.getHost(); - int port = url.getPort(); - String database = url.getPath().substring(1); - String username = (each.getValue()).getUsername(); - String password = (each.getValue()).getPassword(); - Bootstrap bootstrap = new Bootstrap(); - if (workerGroup instanceof EpollEventLoopGroup) { - groupsEpoll(bootstrap, ip, port, database, username, password); - } else { - groupsNio(bootstrap, ip, port, database, username, password); - } - //TODO use connection pool. - bootstrapMap.put(each.getKey(), bootstrap); - ChannelFuture future = bootstrap.connect(ip, port).sync(); - channelMap.put(each.getKey(), future.channel()); + final String ip = url.getHost(); + final int port = url.getPort(); + final String database = url.getPath().substring(1); + final String username = (each.getValue()).getUsername(); + final String password = (each.getValue()).getPassword(); + dataScourceConfigMap.put(each.getKey(), new DataScourceConfig(ip, port, database, username, password)); } + final Bootstrap bootstrap = new Bootstrap(); + if (workerGroup instanceof EpollEventLoopGroup) { + groupsEpoll(bootstrap); + } else { + groupsNio(bootstrap); + } + poolMap = new AbstractChannelPoolMap() { + @Override + protected SimpleChannelPool newPool(String datasourceName) { + DataScourceConfig dataScourceConfig = dataScourceConfigMap.get(datasourceName); + //TODO maxConnection should be set. + return new FixedChannelPool(bootstrap.remoteAddress(dataScourceConfig.ip, dataScourceConfig.port), new NettyChannelPoolHandler(dataScourceConfig), 10); + } + }; } /** @@ -95,19 +105,17 @@ public void stop() { } } - private void groupsEpoll(final Bootstrap bootstrap, final String ip, final int port, final String database, final String username, final String password) { + private void groupsEpoll(final Bootstrap bootstrap) { workerGroup = new EpollEventLoopGroup(WORKER_MAX_THREADS); bootstrap.group(workerGroup) .channel(EpollSocketChannel.class) .option(EpollChannelOption.TCP_CORK, true) .option(EpollChannelOption.SO_KEEPALIVE, true) .option(EpollChannelOption.SO_BACKLOG, 128) - .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) - .handler(new LoggingHandler(LogLevel.INFO)) - .handler(new ClientHandlerInitializer(ip, port, database, username, password)); + .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); } - private void groupsNio(final Bootstrap bootstrap, final String ip, final int port, final String database, final String username, final String password) { + private void groupsNio(final Bootstrap bootstrap) { workerGroup = new NioEventLoopGroup(WORKER_MAX_THREADS); bootstrap.group(workerGroup) .channel(NioSocketChannel.class) @@ -115,9 +123,7 @@ private void groupsNio(final Bootstrap bootstrap, final String ip, final int por .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.SO_BACKLOG, 128) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100) - .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) - .handler(new LoggingHandler(LogLevel.INFO)) - .handler(new ClientHandlerInitializer(ip, port, database, username, password)); + .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); } /** @@ -128,4 +134,46 @@ private void groupsNio(final Bootstrap bootstrap, final String ip, final int por public static ShardingProxyClient getInstance() { return INSTANCE; } + + class DataScourceConfig { + private final String ip; + private final int port; + private final String database; + private final String username; + private final String password; + + public DataScourceConfig(String ip, int port, String database, String username, String password) { + this.ip = ip; + this.port = port; + this.database = database; + this.username = username; + this.password = password; + } + } + + class NettyChannelPoolHandler implements ChannelPoolHandler { + private final DataScourceConfig dataScourceConfig; + + public NettyChannelPoolHandler(final DataScourceConfig dataScourceConfig) { + this.dataScourceConfig = dataScourceConfig; + } + + @Override + public void channelReleased(Channel channel) throws Exception { + log.info("channelReleased. Channel ID: {}" + channel.id().asShortText()); + } + + @Override + public void channelAcquired(Channel channel) throws Exception { + log.info("channelAcquired. Channel ID: {}" + channel.id().asShortText()); + } + + @Override + public void channelCreated(Channel channel) throws Exception { + channel.pipeline() + .addLast(new LoggingHandler(LogLevel.INFO)) + .addLast(new ClientHandlerInitializer(dataScourceConfig.ip, dataScourceConfig.port, dataScourceConfig.database, dataScourceConfig.username, dataScourceConfig.password)); + log.info("channelCreated. Channel ID: {}" + channel.id().asShortText()); + } + } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java index 408456686288c..2b3b939feba1c 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java @@ -18,6 +18,7 @@ package io.shardingsphere.proxy.backend.common; import io.netty.channel.Channel; +import io.netty.channel.pool.SimpleChannelPool; import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.parsing.parser.sql.SQLStatement; import io.shardingsphere.core.routing.SQLExecutionUnit; @@ -75,18 +76,27 @@ protected CommandResponsePackets executeForMasterSlave() { @Override protected CommandResponsePackets execute(final SQLStatement sqlStatement, final String dataSourceName, final String sql) { - Channel channel = ShardingProxyClient.getInstance().getChannelMap().get(dataSourceName); - //MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); - switch (sqlStatement.getType()) { - case DQL: - executeQuery(channel, sql); - break; - case DML: - case DDL: - executeUpdate(channel, sql, sqlStatement); - break; - default: - executeCommon(channel, sql); + SimpleChannelPool pool = null; + Channel channel = null; + try{ + pool = ShardingProxyClient.getInstance().getPoolMap().get(dataSourceName); + channel = pool.acquire().getNow(); + //MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); + switch (sqlStatement.getType()) { + case DQL: + executeQuery(channel, sql); + break; + case DML: + case DDL: + executeUpdate(channel, sql, sqlStatement); + break; + default: + executeCommon(channel, sql); + } + }finally { + if(null != pool && null != channel){ + pool.release(channel); + } } return null; } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java index 41fe251e084d6..29e65727fc9c0 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java @@ -129,6 +129,7 @@ protected void genericResponsePacket(final ChannelHandlerContext context, final @Override protected void executeCommandResponsePackets(final ChannelHandlerContext context, final int header, final MySQLPacketPayload mysqlPacketPayload) { int connectionId = MySQLResultCache.getInstance().getonnectionMap(context.channel().id().asShortText()); + MySQLResultCache.getInstance().get(connectionId).setResponse(null); } From 3d73e95003b848f54d18c65b7e0d101d60e11518 Mon Sep 17 00:00:00 2001 From: WangKai Date: Fri, 25 May 2018 16:10:24 +0800 Subject: [PATCH 126/351] supported connection pool --- .../common/SQLPacketsBackendHandler.java | 55 ++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java index 408456686288c..a6b360c511393 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java @@ -18,6 +18,7 @@ package io.shardingsphere.proxy.backend.common; import io.netty.channel.Channel; +import io.netty.channel.pool.SimpleChannelPool; import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.parsing.parser.sql.SQLStatement; import io.shardingsphere.core.routing.SQLExecutionUnit; @@ -26,6 +27,7 @@ import io.shardingsphere.proxy.backend.ShardingProxyClient; import io.shardingsphere.proxy.config.RuleRegistry; import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket; import io.shardingsphere.proxy.util.MySQLResultCache; @@ -42,10 +44,13 @@ public final class SQLPacketsBackendHandler extends SQLExecuteBackendHandler { private SynchronizedFuture synchronizedFuture; + private final CommandPacket commandPacket; + private int connectionId; - public SQLPacketsBackendHandler(final String sql, final int connectionId, final DatabaseType databaseType, final boolean showSQL) { + public SQLPacketsBackendHandler(final CommandPacket commandPacket, final String sql, final int connectionId, final DatabaseType databaseType, final boolean showSQL) { super(sql, databaseType, showSQL); + this.commandPacket = commandPacket; this.connectionId = connectionId; } @@ -63,9 +68,9 @@ protected CommandResponsePackets executeForSharding() { execute(routeResult.getSqlStatement(), each.getDataSource(), each.getSqlUnit().getSql()); } //TODO timeout should be set. - List result = synchronizedFuture.get(30, TimeUnit.SECONDS); + CommandResponsePackets result = synchronizedFuture.get(30, TimeUnit.SECONDS); MySQLResultCache.getInstance().delete(connectionId); - return merge(routeResult.getSqlStatement(), result); + return result; } @Override @@ -75,33 +80,43 @@ protected CommandResponsePackets executeForMasterSlave() { @Override protected CommandResponsePackets execute(final SQLStatement sqlStatement, final String dataSourceName, final String sql) { - Channel channel = ShardingProxyClient.getInstance().getChannelMap().get(dataSourceName); - //MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); - switch (sqlStatement.getType()) { - case DQL: - executeQuery(channel, sql); - break; - case DML: - case DDL: - executeUpdate(channel, sql, sqlStatement); - break; - default: - executeCommon(channel, sql); + SimpleChannelPool pool = null; + Channel channel = null; + try{ + pool = ShardingProxyClient.getInstance().getPoolMap().get(dataSourceName); + channel = pool.acquire().getNow(); + //MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); + switch (sqlStatement.getType()) { + case DQL: + executeQuery(channel, sql); + break; + case DML: + case DDL: + executeUpdate(channel, sql, sqlStatement); + break; + default: + executeCommon(channel, sql); + } + }finally { + if(null != pool && null != channel){ + pool.release(channel); + } } return null; } - //TODO private void executeQuery(final Channel channel, final String sql) { - channel.writeAndFlush(""); - + MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); + channel.writeAndFlush(commandPacket); } - //TODO private void executeUpdate(final Channel channel, final String sql, final SQLStatement sqlStatement) { + MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); + channel.writeAndFlush(commandPacket); } - //TODO private void executeCommon(final Channel channel, final String sql) { + MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); + channel.writeAndFlush(commandPacket); } } From 489194142dd2c931ebb43dbc089cb83eb40c125b Mon Sep 17 00:00:00 2001 From: WangKai Date: Fri, 25 May 2018 16:47:24 +0800 Subject: [PATCH 127/351] supported flow mysql packets --- .../core/merger/QueryResult.java | 2 +- .../proxy/backend/mysql/ColumnDefine.java | 12 ++ .../backend/mysql/MySQLBackendHandler.java | 36 ++++- .../proxy/backend/mysql/MySQLQueryResult.java | 131 ++++++++++++++++++ 4 files changed, 173 insertions(+), 8 deletions(-) create mode 100644 sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/ColumnDefine.java create mode 100644 sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java diff --git a/sharding-core/src/main/java/io/shardingsphere/core/merger/QueryResult.java b/sharding-core/src/main/java/io/shardingsphere/core/merger/QueryResult.java index fd0550ba89c99..6fbbd1ea54c7f 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/merger/QueryResult.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/merger/QueryResult.java @@ -34,7 +34,7 @@ public interface QueryResult { * @return has next data * @throws SQLException SQL Exception */ - boolean next() throws SQLException; + boolean next() throws SQLException, InterruptedException; /** * Get column count. diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/ColumnDefine.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/ColumnDefine.java new file mode 100644 index 0000000000000..91a0badad9133 --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/ColumnDefine.java @@ -0,0 +1,12 @@ +package io.shardingsphere.proxy.backend.mysql; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class ColumnDefine { + @Getter + private int index; + @Getter + private String name; +} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java index 32f8fa0b31982..724be192dc2e3 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java @@ -17,13 +17,26 @@ package io.shardingsphere.proxy.backend.mysql; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.BlockingQueue; + import com.google.common.primitives.Bytes; + import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.shardingsphere.proxy.backend.common.CommandResponsePacketsHandler; import io.shardingsphere.proxy.transport.mysql.constant.CapabilityFlag; +import io.shardingsphere.proxy.transport.mysql.constant.ColumnType; import io.shardingsphere.proxy.transport.mysql.constant.ServerInfo; +import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.FieldCountPacket; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket; import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket; import io.shardingsphere.proxy.transport.mysql.packet.generic.ErrPacket; import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket; @@ -32,9 +45,6 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - /** * Backend handler. * @@ -56,6 +66,10 @@ public class MySQLBackendHandler extends CommandResponsePacketsHandler { private final String password; + private List columnDefines; + + private BlockingQueue resultSet; + @Override public void channelRead(final ChannelHandlerContext context, final Object message) { MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload((ByteBuf) message); @@ -63,11 +77,11 @@ public void channelRead(final ChannelHandlerContext context, final Object messag int sequenceId = mysqlPacketPayload.readInt1(); int header = mysqlPacketPayload.readInt1() & 0xFF; if (OKPacket.HEADER == header || ErrPacket.HEADER == header || EofPacket.HEADER == header) { - genericResponsePacket(context, header, mysqlPacketPayload); + genericResponsePacket(context, sequenceId, header, mysqlPacketPayload); } else if (!authorized) { auth(context, sequenceId, header, mysqlPacketPayload); } else { - executeCommandResponsePackets(context, header, mysqlPacketPayload); + executeCommandResponsePackets(context, sequenceId, header, mysqlPacketPayload); } } @@ -95,7 +109,8 @@ protected void auth(final ChannelHandlerContext context, final int sequenceId, f } @Override - protected void genericResponsePacket(final ChannelHandlerContext context, final int header, final MySQLPacketPayload mysqlPacketPayload) { + protected void genericResponsePacket(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { + CommandResponsePackets commandResponsePackets = null; switch (header) { case OKPacket.HEADER: if (!authorized) { @@ -107,6 +122,7 @@ protected void genericResponsePacket(final ChannelHandlerContext context, final int warnings = mysqlPacketPayload.readInt2(); String info = mysqlPacketPayload.readStringEOF(); log.debug("OKPacket[affectedRows={},lastInsertId={},statusFlags={},warnings={},info={}]", affectedRows, lastInsertId, statusFlags, warnings, info); + commandResponsePackets = new CommandResponsePackets(new OKPacket(sequenceId + 1, affectedRows, lastInsertId, statusFlags, warnings, info)); break; case ErrPacket.HEADER: int errorCode = mysqlPacketPayload.readInt2(); @@ -114,21 +130,27 @@ protected void genericResponsePacket(final ChannelHandlerContext context, final String sqlState = mysqlPacketPayload.readStringFix(5); String errorMessage = mysqlPacketPayload.readStringEOF(); log.debug("ErrPacket[errorCode={},sqlStateMarker={},sqlState={},errorMessage={}]", errorCode, sqlStateMarker, sqlState, errorMessage); + commandResponsePackets = new CommandResponsePackets(new ErrPacket(sequenceId + 1, errorCode, sqlStateMarker, sqlState, errorMessage)); break; case EofPacket.HEADER: warnings = mysqlPacketPayload.readInt2(); statusFlags = mysqlPacketPayload.readInt2(); log.debug("EofPacket[warnings={},statusFlags={}]", warnings, statusFlags); + commandResponsePackets = new CommandResponsePackets(new EofPacket(sequenceId + 1, warnings, statusFlags)); break; default: break; } + int connectionId = MySQLResultCache.getInstance().getConnectionMap(context.channel().id().asShortText()); + if (MySQLResultCache.getInstance().get(connectionId) != null) { + MySQLResultCache.getInstance().get(connectionId).setResponse(commandResponsePackets); + } } //TODO @Override protected void executeCommandResponsePackets(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { -CommandResponsePackets result = new CommandResponsePackets(); + CommandResponsePackets result = new CommandResponsePackets(); //TODO int columnCount = header <= 0xfb ? header : (int) mysqlPacketPayload.readIntLenenc(); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java new file mode 100644 index 0000000000000..ee5d484e25cc2 --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java @@ -0,0 +1,131 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.backend.mysql; + +import io.netty.buffer.ByteBuf; +import io.shardingsphere.core.merger.QueryResult; +import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.FieldCountPacket; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket; +import lombok.RequiredArgsConstructor; + +import java.io.InputStream; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.LinkedBlockingQueue; + +/** + * MySQL packet query result. + * + * @author wangkai + */ + +public final class MySQLQueryResult implements QueryResult { + + private final int columnCount; + + private final Map columnIndexAndLabelMap; + + private final Map columnLabelAndIndexMap; + + private final BlockingQueue resultSet; + + private TextResultSetRowPacket currentRow; + + public MySQLQueryResult(final List columnDefines, final BlockingQueue resultSet) { + columnCount = columnDefines.size(); + columnIndexAndLabelMap = new HashMap<>(columnCount, 1); + columnLabelAndIndexMap = new HashMap<>(columnCount, 1); + for(int i = 0; i<=columnCount;i++){ + columnIndexAndLabelMap.put(i, columnDefines.get(i).getName()); + columnLabelAndIndexMap.put(columnDefines.get(i).getName(), i); + } + columnDefines.clear(); + this.resultSet = resultSet; + } + + @Override + public boolean next() throws InterruptedException { + byte[] result = resultSet.take(); +// if(result == "eof"){ + resultSet.clear(); +// reutnr false; +// }else{ +// currentRow = new TextResultSetRowPacket(++currentSequenceId, result); +// return true; +// } + return false; + } + + @Override + public int getColumnCount() { + return columnCount; + } + + @Override + public String getColumnLabel(final int columnIndex) { + return columnIndexAndLabelMap.get(columnIndex); + } + + @Override + public Object getValue(final int columnIndex, final Class type) { + return currentRow.getData().get(columnIndex - 1); + } + + @Override + public Object getValue(final String columnLabel, final Class type) { + return currentRow.getData().get(columnLabelAndIndexMap.get(columnLabel)); + } + + @Override + public Object getCalendarValue(final int columnIndex, final Class type, final Calendar calendar) { + return currentRow.getData().get(columnIndex - 1); + } + + @Override + public Object getCalendarValue(final String columnLabel, final Class type, final Calendar calendar) { + return currentRow.getData().get(columnLabelAndIndexMap.get(columnLabel)); + } + + @Override + public InputStream getInputStream(final int columnIndex, final String type) { + return (InputStream) currentRow.getData().get(columnIndex - 1); + } + + @Override + public InputStream getInputStream(final String columnLabel, final String type) { + return (InputStream) currentRow.getData().get(columnLabelAndIndexMap.get(columnLabel)); + } + + // TODO + @Override + public boolean wasNull() { + return false; + } +} From b213bc47d76f4f1aa161591e99f4725a391bc1c1 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 25 May 2018 18:56:29 +0800 Subject: [PATCH 128/351] connect not ready --- .../com/saaavsaaa/client/action/IClient.java | 2 +- .../com/saaavsaaa/client/cache/PathTree.java | 51 ++++++++++++++++ .../saaavsaaa/client/section/Callable.java | 2 +- .../client/section/ClientContext.java | 19 +++++- .../saaavsaaa/client/section/Connection.java | 39 ++++++++++-- .../client/zookeeper/CacheClient.java | 10 ++- .../client/zookeeper/ClientFactory.java | 12 +++- .../client/zookeeper/UsualClient.java | 6 ++ .../client/zookeeper/base/BaseClient.java | 44 +++++++------ .../zookeeper/base/BaseClientFactory.java | 2 + .../zookeeper/strategy/SyncRetryStrategy.java | 61 +++++++++++++++++-- .../client/retry/AsyncRetryCenterTest.java | 13 ++-- .../retry/TestCreateCurrentOperation.java | 3 +- .../zookeeper/SyncRetryStrategyTest.java | 21 +++++++ 14 files changed, 246 insertions(+), 39 deletions(-) create mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java diff --git a/src/main/java/com/saaavsaaa/client/action/IClient.java b/src/main/java/com/saaavsaaa/client/action/IClient.java index 730ebd591fa90..8bad6833c4754 100644 --- a/src/main/java/com/saaavsaaa/client/action/IClient.java +++ b/src/main/java/com/saaavsaaa/client/action/IClient.java @@ -11,7 +11,7 @@ */ public interface IClient extends IAction, IGroupAction{ void start() throws IOException, InterruptedException; - void close() throws InterruptedException; + void close(); void registerWatch(String key, Listener listener); void unregisterWatch(String key); diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index 9a9d2d627d863..32befb0e7bd60 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -36,6 +36,7 @@ public final class PathTree { private final IClient client; private final IProvider provider; private PathStatus Status; + private boolean closed = false; public PathTree(final String root, final IClient client) { this.rootNode.set(new PathNode(root)); @@ -47,6 +48,9 @@ public PathTree(final String root, final IClient client) { public void loading() throws KeeperException, InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); + if (closed){ + return; + } try { if (Status == Status.RELEASE) { logger.debug("loading Status:{}", Status); @@ -75,6 +79,9 @@ public void loading() throws KeeperException, InterruptedException { } private void attechIntoNode(final List children, final PathNode pathNode) throws KeeperException, InterruptedException { + if (closed){ + return; + } logger.debug("attechIntoNode children:{}", children); if (children.isEmpty()){ logger.info("attechIntoNode there are no children"); @@ -92,6 +99,9 @@ private void attechIntoNode(final List children, final PathNode pathNode public void refreshPeriodic(final long period){ final ReentrantLock lock = this.lock; lock.lock(); + if (closed){ + return; + } try { if (executorStart) { throw new IllegalArgumentException("period already set"); @@ -134,6 +144,9 @@ public void stopRefresh(){ } public void watch(Listener listener){ + if (closed){ + return; + } if (listener == null){ listener = new Listener(rootNode.get().getKey()) { @Override @@ -190,6 +203,9 @@ public PathNode getRootNode() { } public byte[] getValue(final String path){ + if (closed){ + return null; + } PathNode node = get(path); return null == node ? null : node.getValue(); } @@ -203,6 +219,9 @@ private Iterator keyIterator(final String path){ } public List getChildren(String path) { + if (closed){ + return null; + } PathNode node = get(path); List result = new ArrayList<>(); if (node == null){ @@ -237,6 +256,9 @@ private PathNode get(final String path){ public void put(final String path, final String value) { final ReentrantLock lock = this.lock; lock.lock(); + if (closed){ + return; + } try { logger.debug("cache put:{},value:{}", path, value); PathUtils.validatePath(path); @@ -266,6 +288,9 @@ public void put(final String path, final String value) { public void delete(String path) { final ReentrantLock lock = this.lock; lock.lock(); + if (closed){ + return; + } try { PathUtils.validatePath(path); String prxpath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); @@ -276,4 +301,30 @@ public void delete(String path) { lock.unlock(); } } + + public void close(){ + final ReentrantLock lock = this.lock; + lock.lock(); + this.closed = true; + try { + if (executorStart){ + stopRefresh(); + } + deleteAllChildren(rootNode.get()); + } catch (Exception ee){ + logger.warn("PathTree close:{}", ee.getMessage()); + } finally { + lock.unlock(); + } + } + + private void deleteAllChildren(PathNode node){ + if (node.getChildren().isEmpty()){ + return; + } + for (String one : node.getChildren().keySet()) { + deleteAllChildren(node.getChildren().get(one)); + node.getChildren().remove(one); + } + } } diff --git a/src/main/java/com/saaavsaaa/client/section/Callable.java b/src/main/java/com/saaavsaaa/client/section/Callable.java index 81c4d31c6e410..13df7d6d49d8c 100644 --- a/src/main/java/com/saaavsaaa/client/section/Callable.java +++ b/src/main/java/com/saaavsaaa/client/section/Callable.java @@ -46,7 +46,7 @@ public void exec() throws KeeperException, InterruptedException { } } - private void execDelay() throws KeeperException, InterruptedException { + protected void execDelay() throws KeeperException, InterruptedException { for (;;) { long delay = delayPolicyExecutor.getNextTick() - System.currentTimeMillis(); if (delay > 0){ diff --git a/src/main/java/com/saaavsaaa/client/section/ClientContext.java b/src/main/java/com/saaavsaaa/client/section/ClientContext.java index f9581669bf33a..a78d06e293999 100644 --- a/src/main/java/com/saaavsaaa/client/section/ClientContext.java +++ b/src/main/java/com/saaavsaaa/client/section/ClientContext.java @@ -4,14 +4,19 @@ import com.saaavsaaa.client.retry.DelayRetryPolicy; import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + /** * Created by aaa */ public final class ClientContext { - private final DelayRetryPolicy delayRetryPolicy; - private final BaseClientFactory clientFactory; + private static final Map watchers = new ConcurrentHashMap<>(); + private DelayRetryPolicy delayRetryPolicy; + private BaseClientFactory clientFactory; private IProvider provider; + public ClientContext(final DelayRetryPolicy delayRetryPolicy, final BaseClientFactory clientFactory) { this(delayRetryPolicy, clientFactory, null); } @@ -36,4 +41,14 @@ public IProvider getProvider() { public void setProvider(IProvider provider){ this.provider = provider; } + + public Map getWatchers(){ + return watchers; + } + + public void close() { + this.delayRetryPolicy = null; + this.clientFactory = null; + this.provider = null; + } } diff --git a/src/main/java/com/saaavsaaa/client/section/Connection.java b/src/main/java/com/saaavsaaa/client/section/Connection.java index 8399347749d62..e7651ae11a03c 100644 --- a/src/main/java/com/saaavsaaa/client/section/Connection.java +++ b/src/main/java/com/saaavsaaa/client/section/Connection.java @@ -3,18 +3,23 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.zookeeper.base.BaseClient; import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.IOException; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; /** * Created by aaa */ public class Connection { - + private static final Logger logger = LoggerFactory.getLogger(Connection.class); //is need reset private static final Map exceptionResets = new ConcurrentHashMap<>(); - private final ClientContext context; + private ClientContext context; static { exceptionResets.put(KeeperException.Code.SESSIONEXPIRED.intValue(), true); @@ -33,10 +38,32 @@ public void check(KeeperException e) throws KeeperException { throw e; } boolean reset = exceptionResets.get(code); - if (reset){ -// this.client = ((BaseClient)client).getContext() - } else { - // block + try { + if (reset){ + resetConnection(); + } else { + // block + block(); + } + } catch (Exception ee){ + logger.error("check reconnect:{}", ee.getMessage(), ee); } } + + private void resetConnection() throws IOException, InterruptedException { + IClient client = context.getClientFactory().newClientByOriginal(true).start(); + this.context = ((BaseClient)client).getContext(); + } + + private void block() throws InterruptedException { + final CountDownLatch autoReconnect = new CountDownLatch(1); + Listener listener = new Listener() { + @Override + public void process(WatchedEvent event) { + autoReconnect.countDown(); + } + }; + context.getWatchers().put(listener.getKey(), listener); + autoReconnect.await(); + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 6a310a2d324e4..41562fe4dca7e 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -14,6 +14,7 @@ /** * Created by aaa + * partially prepared products */ public final class CacheClient extends UsualClient { private static final Logger logger = LoggerFactory.getLogger(CacheClient.class); @@ -33,12 +34,19 @@ public void start() throws IOException, InterruptedException { } } + @Override + public void close(){ + super.close(); + this.strategies.clear(); + this.pathTree.close(); + } + //todo put it here? void useCacheStrategy(CacheStrategy cacheStrategy) throws KeeperException, InterruptedException { logger.debug("use cache strategy:{}", cacheStrategy); switch (cacheStrategy){ case WATCH:{ - pathTree = new PathTree(rootNode, this); + pathTree = new PathTree(rootNode, this); pathTree.watch(null); return; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 3692effe93623..f4efbf244111c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -1,5 +1,6 @@ package com.saaavsaaa.client.zookeeper; +import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.retry.DelayRetryPolicy; import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.utility.constant.Constants; @@ -26,13 +27,22 @@ public ClientFactory newClient(final String servers, final int sessionTimeoutMil /* * used for create new clients through a existing client + * this client is not perhaps the client */ - ClientFactory newClient() { + @Override + public synchronized BaseClientFactory newClientByOriginal(boolean closeOriginal) { + IClient oldClient = this.client; client = new UsualClient(servers, sessionTimeoutMilliseconds); + if (closeOriginal){ + oldClient.close(); + } logger.debug("new usual client by a existing client"); return this; } + /* + * partially prepared products + */ public ClientFactory newCacheClient(final String servers, final int sessionTimeoutMilliseconds) { this.servers = servers; this.sessionTimeoutMilliseconds = sessionTimeoutMilliseconds; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 076236f9b44c8..9c6903902d6e4 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -32,6 +32,12 @@ public void start() throws IOException, InterruptedException { useExecStrategy(StrategyType.USUAL); } + @Override + public void close(){ + super.close(); + this.strategies.clear(); + } + @Override public synchronized void useExecStrategy(StrategyType strategyType) { logger.debug("useExecStrategy:{}", strategyType); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index 38ca3afb1df8b..7f2710df83d09 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -26,10 +26,10 @@ public abstract class BaseClient implements IClient { private static final Logger logger = LoggerFactory.getLogger(BaseClient.class); private static final CountDownLatch CONNECTED = new CountDownLatch(1); - protected static final Map watchers = new ConcurrentHashMap<>(); +// protected static final Map watchers = new ConcurrentHashMap<>(); protected final boolean watched = true; //false - private boolean globalListenerRegistered = false; + private Listener globalListener; protected String rootNode = "/InitValue"; protected boolean rootExist = false; private final String servers; @@ -60,9 +60,14 @@ public void start() throws IOException, InterruptedException { } @Override - public void close() throws InterruptedException { - zooKeeper.close(); - logger.debug("zk closed"); + public void close() { + try { + zooKeeper.close(); + logger.debug("zk closed"); + this.context.close(); + } catch (Exception ee){ + logger.warn("BaseClient close:{}", ee.getMessage()); + } } public abstract void useExecStrategy(StrategyType strategyType); @@ -75,19 +80,25 @@ private Watcher startWatcher() { return new Watcher(){ public void process(WatchedEvent event) { logger.debug("BaseClient process event:{}", event.toString()); - if(Event.KeeperState.SyncConnected == event.getState()){ - if(Event.EventType.None == event.getType()){ + if(Event.EventType.None == event.getType()){ + if(Event.KeeperState.SyncConnected == event.getState()){ CONNECTED.countDown(); logger.debug("BaseClient startWatcher SyncConnected"); return; + } else if (Event.KeeperState.Expired == event.getState()){ + try { + start(); + } catch (Exception ee){ + logger.error("event state Expired:{}", ee.getMessage(), ee); + } } } - if (globalListenerRegistered){ - watchers.get(Constants.GLOBAL_LISTENER_KEY).process(event); + if (globalListener != null){ + globalListener.process(event); logger.debug("BaseClient " + Constants.GLOBAL_LISTENER_KEY + " process"); } if (Properties.INSTANCE.watchOn()){ - for (Listener listener : watchers.values()) { + for (Listener listener : context.getWatchers().values()) { if (listener.getPath() == null || listener.getPath().equals(event.getPath())){ listener.process(event); } @@ -98,20 +109,19 @@ public void process(WatchedEvent event) { } void registerWatch(final Listener globalListener){ - if (globalListenerRegistered){ + if (globalListener != null){ logger.warn("global listener can only register one"); return; } - watchers.put(Constants.GLOBAL_LISTENER_KEY, globalListener); - globalListenerRegistered = true; - logger.debug("globalListenerRegistered:{}", globalListenerRegistered); + this.globalListener = globalListener; + logger.debug("globalListenerRegistered:{}", globalListener.getKey()); } @Override public void registerWatch(final String key, final Listener listener){ String path = PathUtil.getRealPath(rootNode, key); listener.setPath(path); - watchers.put(listener.getKey(), listener); + context.getWatchers().put(listener.getKey(), listener); logger.debug("register watcher:{}", path); } @@ -121,8 +131,8 @@ public void unregisterWatch(final String key){ throw new IllegalArgumentException("key should not be blank"); } // String path = PathUtil.getRealPath(rootNode, key); - if (watchers.containsKey(key)){ - watchers.remove(key); + if (context.getWatchers().containsKey(key)){ + context.getWatchers().remove(key); logger.debug("unregisterWatch:{}", key); } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java index c960667ab0235..44aaebbe78b2c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java @@ -31,4 +31,6 @@ public IClient start() throws IOException, InterruptedException { } return client; } + + public abstract BaseClientFactory newClientByOriginal(boolean closeOriginal); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java index 1eb8e5aec45ae..4e84dc927111d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java @@ -1,8 +1,5 @@ package com.saaavsaaa.client.zookeeper.strategy; -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.retry.DelayRetryPolicy; -import com.saaavsaaa.client.retry.RetryCount; import com.saaavsaaa.client.section.Callable; import com.saaavsaaa.client.section.ClientContext; import org.apache.zookeeper.CreateMode; @@ -71,11 +68,65 @@ public void call() throws KeeperException, InterruptedException { @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - String path = provider.getRealPath(key); Callable callable = new Callable(context) { @Override public void call() throws KeeperException, InterruptedException { - provider.create(path, value, createMode); + provider.create(provider.getRealPath(key), value, createMode); + } + }; + callable.exec(); + } + + @Override + public void update(final String key, final String value) throws KeeperException, InterruptedException { + Callable callable = new Callable(context) { + @Override + public void call() throws KeeperException, InterruptedException { + provider.update(provider.getRealPath(key), value); + } + }; + callable.exec(); + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + Callable callable = new Callable(context) { + @Override + public void call() throws KeeperException, InterruptedException { + provider.delete(provider.getRealPath(key)); + } + }; + callable.exec(); + } + + @Override + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + Callable callable = new Callable(context) { + @Override + public void call() throws KeeperException, InterruptedException { + new UsualStrategy(provider).createAllNeedPath(key, value, createMode); + } + }; + callable.exec(); + } + + @Override + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { + Callable callable = new Callable(context) { + @Override + public void call() throws KeeperException, InterruptedException { + new UsualStrategy(provider).deleteAllChildren(key); + } + }; + callable.exec(); + } + + @Override + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + Callable callable = new Callable(context) { + @Override + public void call() throws KeeperException, InterruptedException { + new UsualStrategy(provider).deleteCurrentBranch(key); } }; callable.exec(); diff --git a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java index 4bd48d3c46c91..bc4f51263f553 100644 --- a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java @@ -22,10 +22,12 @@ */ public class AsyncRetryCenterTest { private ClientContext context; + private BaseClient client; @Before public void start() throws IOException, InterruptedException { - context = ((BaseClient)createClient()).getContext(); + client = ((BaseClient)createClient()); + context = client.getContext(); AsyncRetryCenter.INSTANCE.init(new DelayRetryPolicy(3, 3, 10)); AsyncRetryCenter.INSTANCE.start(); } @@ -47,11 +49,14 @@ public void stop(){ public void create() throws InterruptedException, KeeperException { String key = "a"; String value = "bbb11"; - context.getProvider().create("/" + TestSupport.ROOT, Constants.NOTHING_VALUE, CreateMode.PERSISTENT); + if (!context.getProvider().exists("/" + TestSupport.ROOT)) { + context.getProvider().create("/" + TestSupport.ROOT, Constants.NOTHING_VALUE, CreateMode.PERSISTENT); + } AsyncRetryCenter.INSTANCE.add(new TestCreateCurrentOperation(context, key, value, CreateMode.PERSISTENT)); - Thread.sleep(1000); + Thread.sleep(2000); String path = PathUtil.getRealPath(TestSupport.ROOT, key); - assert context.getProvider().exists(path); +// assert context.getProvider().exists(path); + assert client.checkExists(path); context.getProvider().delete(path); context.getProvider().delete(context.getProvider().getRealPath(TestSupport.ROOT)); assert !context.getProvider().exists(path); diff --git a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java index 06a9d13f65512..ce5de8ceeb1e7 100644 --- a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java +++ b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java @@ -19,7 +19,8 @@ public TestCreateCurrentOperation(final ClientContext context, String key, Strin public void execute() throws KeeperException, InterruptedException { if (count < 2){ count++; - throw new KeeperException.SessionExpiredException(); +// throw new KeeperException.SessionExpiredException(); + throw new KeeperException.ConnectionLossException(); } super.execute(); } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java new file mode 100644 index 0000000000000..8e52661dfda56 --- /dev/null +++ b/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java @@ -0,0 +1,21 @@ +package com.saaavsaaa.client.zookeeper; + +import com.saaavsaaa.client.action.IClient; +import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.zookeeper.strategy.StrategyType; + +import java.io.IOException; + +/** + * Created by aaa + */ +public class SyncRetryStrategyTest extends UsualClientTest{ + @Override + protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { + Listener listener = TestSupport.buildListener(); + IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + ((BaseClient)client).useExecStrategy(StrategyType.SYNC_RETRY); + return client; + } +} From e0f1b6407107a85628fb55b0abf0fdaca75ffe56 Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 28 May 2018 18:29:12 +0800 Subject: [PATCH 129/351] retry async test --- .../com/saaavsaaa/client/section/ClientContext.java | 11 ++++++++++- .../java/com/saaavsaaa/client/section/Connection.java | 8 ++++++-- .../saaavsaaa/client/zookeeper/base/BaseClient.java | 1 + .../saaavsaaa/client/retry/AsyncRetryCenterTest.java | 5 +++-- .../client/retry/TestCreateCurrentOperation.java | 7 ++++++- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/section/ClientContext.java b/src/main/java/com/saaavsaaa/client/section/ClientContext.java index a78d06e293999..f73bb97a08400 100644 --- a/src/main/java/com/saaavsaaa/client/section/ClientContext.java +++ b/src/main/java/com/saaavsaaa/client/section/ClientContext.java @@ -11,7 +11,7 @@ * Created by aaa */ public final class ClientContext { - private static final Map watchers = new ConcurrentHashMap<>(); + private final Map watchers = new ConcurrentHashMap<>(); private DelayRetryPolicy delayRetryPolicy; private BaseClientFactory clientFactory; private IProvider provider; @@ -50,5 +50,14 @@ public void close() { this.delayRetryPolicy = null; this.clientFactory = null; this.provider = null; + this.watchers.clear(); + } + + public void updateContext(final ClientContext context){ + this.delayRetryPolicy = context.getDelayRetryPolicy(); + this.clientFactory = context.clientFactory; + this.provider = context.getProvider(); + this.watchers.clear(); + this.watchers.putAll(context.getWatchers()); } } diff --git a/src/main/java/com/saaavsaaa/client/section/Connection.java b/src/main/java/com/saaavsaaa/client/section/Connection.java index e7651ae11a03c..9fbe48751b2eb 100644 --- a/src/main/java/com/saaavsaaa/client/section/Connection.java +++ b/src/main/java/com/saaavsaaa/client/section/Connection.java @@ -43,7 +43,7 @@ public void check(KeeperException e) throws KeeperException { resetConnection(); } else { // block - block(); +// block(); } } catch (Exception ee){ logger.error("check reconnect:{}", ee.getMessage(), ee); @@ -51,16 +51,20 @@ public void check(KeeperException e) throws KeeperException { } private void resetConnection() throws IOException, InterruptedException { + logger.debug("resetConnection......................................................"); IClient client = context.getClientFactory().newClientByOriginal(true).start(); - this.context = ((BaseClient)client).getContext(); + this.context.updateContext(((BaseClient)client).getContext()); + logger.debug("......................................................connection reset"); } private void block() throws InterruptedException { + logger.debug("block auto reconnection"); final CountDownLatch autoReconnect = new CountDownLatch(1); Listener listener = new Listener() { @Override public void process(WatchedEvent event) { autoReconnect.countDown(); + logger.debug("block reconnected"); } }; context.getWatchers().put(listener.getKey(), listener); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index 7f2710df83d09..99c508034717b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -87,6 +87,7 @@ public void process(WatchedEvent event) { return; } else if (Event.KeeperState.Expired == event.getState()){ try { + logger.warn("startWatcher Event.KeeperState.Expired"); start(); } catch (Exception ee){ logger.error("event state Expired:{}", ee.getMessage(), ee); diff --git a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java index bc4f51263f553..7aeeb4b4526e1 100644 --- a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java @@ -50,13 +50,14 @@ public void create() throws InterruptedException, KeeperException { String key = "a"; String value = "bbb11"; if (!context.getProvider().exists("/" + TestSupport.ROOT)) { + System.out.println("exist root"); context.getProvider().create("/" + TestSupport.ROOT, Constants.NOTHING_VALUE, CreateMode.PERSISTENT); } AsyncRetryCenter.INSTANCE.add(new TestCreateCurrentOperation(context, key, value, CreateMode.PERSISTENT)); Thread.sleep(2000); String path = PathUtil.getRealPath(TestSupport.ROOT, key); -// assert context.getProvider().exists(path); - assert client.checkExists(path); + assert context.getProvider().exists(path); +// assert client.checkExists(path); context.getProvider().delete(path); context.getProvider().delete(context.getProvider().getRealPath(TestSupport.ROOT)); assert !context.getProvider().exists(path); diff --git a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java index ce5de8ceeb1e7..e6ad2cad4140f 100644 --- a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java +++ b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.retry; import com.saaavsaaa.client.section.ClientContext; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -20,8 +21,12 @@ public void execute() throws KeeperException, InterruptedException { if (count < 2){ count++; // throw new KeeperException.SessionExpiredException(); - throw new KeeperException.ConnectionLossException(); +// throw new KeeperException.ConnectionLossException(); + + System.out.println("test injectSessionExpiration==================================================" + count); + ((BaseProvider)context.getProvider()).getZooKeeper().getTestable().injectSessionExpiration(); } + System.out.println("test ================================================" + count); super.execute(); } } From f1a0e1dd16fe7c04e6b4596a7c8acb344c402f16 Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 28 May 2018 18:39:00 +0800 Subject: [PATCH 130/351] listener --- .../java/com/saaavsaaa/client/zookeeper/base/BaseClient.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index 99c508034717b..5641e8bdc07f5 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -101,6 +101,7 @@ public void process(WatchedEvent event) { if (Properties.INSTANCE.watchOn()){ for (Listener listener : context.getWatchers().values()) { if (listener.getPath() == null || listener.getPath().equals(event.getPath())){ + logger.debug("listener process:{}, listener:{}", listener.getPath(), listener.getKey()); listener.process(event); } } @@ -110,7 +111,7 @@ public void process(WatchedEvent event) { } void registerWatch(final Listener globalListener){ - if (globalListener != null){ + if (this.globalListener != null){ logger.warn("global listener can only register one"); return; } From f321d040cc77ac32897acd938f90a0134144f4b0 Mon Sep 17 00:00:00 2001 From: saaav Date: Tue, 29 May 2018 19:10:18 +0800 Subject: [PATCH 131/351] clear --- .../client/{section => action}/Callback.java | 2 +- .../com/saaavsaaa/client/action/IClient.java | 5 +- .../saaavsaaa/client/action/IProvider.java | 4 +- .../com/saaavsaaa/client/cache/PathTree.java | 7 +- .../client/election/LeaderElection.java | 4 +- .../client/zookeeper/CacheClient.java | 7 +- .../client/zookeeper/ClientFactory.java | 26 +-- .../client/zookeeper/UsualClient.java | 57 +------ .../client/zookeeper/base/BaseClient.java | 157 ++++++++---------- .../zookeeper/base/BaseClientFactory.java | 12 +- .../client/zookeeper/base/BaseContext.java | 42 +++++ .../client/zookeeper/base/BaseOperation.java | 15 +- .../client/zookeeper/base/BaseProvider.java | 41 +++-- .../client/zookeeper/base/Holder.java | 94 +++++++++++ .../operation/CreateAllNeedOperation.java | 12 +- .../operation/CreateCurrentOperation.java | 11 +- .../operation/DeleteAllChildrenOperation.java | 8 +- .../DeleteCurrentBranchOperation.java | 8 +- .../operation/DeleteCurrentOperation.java | 6 +- .../zookeeper/operation/UpdateOperation.java | 6 +- .../zookeeper/provider/RetryProvider.java | 11 +- .../{ => zookeeper}/section/Callable.java | 17 +- .../section/ClientContext.java | 35 ++-- .../{ => zookeeper}/section/ClientTask.java | 2 +- .../{ => zookeeper}/section/Connection.java | 18 +- .../{ => zookeeper}/section/Listener.java | 2 +- .../section/WatcherCreator.java | 2 +- .../strategy/AllAsyncRetryStrategy.java | 14 +- .../strategy/AsyncRetryStrategy.java | 17 +- .../strategy/ContentionStrategy.java | 2 +- .../zookeeper/strategy/SyncRetryStrategy.java | 40 +++-- .../strategy/TransactionContendStrategy.java | 8 +- .../zookeeper/transaction/ZKTransaction.java | 5 +- .../client/retry/AsyncRetryCenterTest.java | 29 ++-- .../saaavsaaa/client/retry/TestCallable.java | 34 ++++ .../retry/TestCreateCurrentOperation.java | 9 +- .../zookeeper/AsyncRetryStrategyTest.java | 2 +- .../client/zookeeper/CacheWathClientTest.java | 2 +- .../client/zookeeper/ClientsTest.java | 2 +- .../zookeeper/ContentionStrategyTest.java | 2 +- .../zookeeper/SyncRetryStrategyTest.java | 109 +++++++++++- .../client/zookeeper/TestSupport.java | 2 +- .../zookeeper/UsualWatchClientTest.java | 2 +- .../client/zookeeper/base/BaseClientTest.java | 4 +- 44 files changed, 558 insertions(+), 336 deletions(-) rename src/main/java/com/saaavsaaa/client/{section => action}/Callback.java (68%) create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/base/BaseContext.java create mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java rename src/main/java/com/saaavsaaa/client/{ => zookeeper}/section/Callable.java (78%) rename src/main/java/com/saaavsaaa/client/{ => zookeeper}/section/ClientContext.java (55%) rename src/main/java/com/saaavsaaa/client/{ => zookeeper}/section/ClientTask.java (93%) rename src/main/java/com/saaavsaaa/client/{ => zookeeper}/section/Connection.java (82%) rename src/main/java/com/saaavsaaa/client/{ => zookeeper}/section/Listener.java (92%) rename src/main/java/com/saaavsaaa/client/{ => zookeeper}/section/WatcherCreator.java (94%) create mode 100644 src/test/java/com/saaavsaaa/client/retry/TestCallable.java diff --git a/src/main/java/com/saaavsaaa/client/section/Callback.java b/src/main/java/com/saaavsaaa/client/action/Callback.java similarity index 68% rename from src/main/java/com/saaavsaaa/client/section/Callback.java rename to src/main/java/com/saaavsaaa/client/action/Callback.java index 01f566ca9b4b7..5f2454b53b69a 100644 --- a/src/main/java/com/saaavsaaa/client/section/Callback.java +++ b/src/main/java/com/saaavsaaa/client/action/Callback.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.section; +package com.saaavsaaa.client.action; /** * Created by aaa diff --git a/src/main/java/com/saaavsaaa/client/action/IClient.java b/src/main/java/com/saaavsaaa/client/action/IClient.java index 8bad6833c4754..8c48ac8cf7ac4 100644 --- a/src/main/java/com/saaavsaaa/client/action/IClient.java +++ b/src/main/java/com/saaavsaaa/client/action/IClient.java @@ -1,8 +1,8 @@ package com.saaavsaaa.client.action; -import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.zookeeper.section.Listener; +import com.saaavsaaa.client.zookeeper.strategy.StrategyType; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; -import org.apache.zookeeper.Watcher; import java.io.IOException; @@ -14,6 +14,7 @@ public interface IClient extends IAction, IGroupAction{ void close(); void registerWatch(String key, Listener listener); void unregisterWatch(String key); + void useExecStrategy(StrategyType strategyType); ZKTransaction transaction(); /* diff --git a/src/main/java/com/saaavsaaa/client/action/IProvider.java b/src/main/java/com/saaavsaaa/client/action/IProvider.java index 074d988392429..1bb4058534435 100644 --- a/src/main/java/com/saaavsaaa/client/action/IProvider.java +++ b/src/main/java/com/saaavsaaa/client/action/IProvider.java @@ -1,13 +1,13 @@ package com.saaavsaaa.client.action; import com.saaavsaaa.client.election.LeaderElection; -import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.Watcher; +import java.io.IOException; import java.util.List; import java.util.Stack; @@ -32,4 +32,6 @@ public interface IProvider { void executeContention(final LeaderElection election) throws KeeperException, InterruptedException; void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException; + + void checkConnection(final KeeperException e) throws KeeperException, InterruptedException; } diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index 32befb0e7bd60..81c637ebd8705 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -3,11 +3,10 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.section.ClientTask; -import com.saaavsaaa.client.section.Listener; import com.saaavsaaa.client.utility.Properties; +import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.zookeeper.section.Listener; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.common.PathUtils; @@ -42,7 +41,7 @@ public PathTree(final String root, final IClient client) { this.rootNode.set(new PathNode(root)); this.Status = PathStatus.RELEASE; this.client = client; - this.provider = ((BaseClient)client).getContext().getProvider(); + this.provider = ((BaseClient)client).getStrategy().getProvider(); } public void loading() throws KeeperException, InterruptedException { diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index 6d986e8d93c2c..67ca4261e7515 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -1,9 +1,9 @@ package com.saaavsaaa.client.election; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.utility.Properties; -import com.saaavsaaa.client.section.WatcherCreator; +import com.saaavsaaa.client.zookeeper.section.WatcherCreator; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import org.apache.zookeeper.CreateMode; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 41562fe4dca7e..ef7ed6d27314d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -3,6 +3,7 @@ import com.saaavsaaa.client.cache.CacheStrategy; import com.saaavsaaa.client.cache.PathTree; import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.zookeeper.base.BaseContext; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -20,8 +21,8 @@ public final class CacheClient extends UsualClient { private static final Logger logger = LoggerFactory.getLogger(CacheClient.class); protected PathTree pathTree = null; - CacheClient(String servers, int sessionTimeoutMilliseconds) { - super(servers, sessionTimeoutMilliseconds); + CacheClient(final BaseContext context) { + super(context); } @Override @@ -112,6 +113,6 @@ public List getChildren(final String key) throws KeeperException, Interr return keys; } logger.debug("getChildren cache not hit:{}", keys); - return zooKeeper.getChildren(PathUtil.getRealPath(rootNode, key), false); + return strategy.getChildren(PathUtil.getRealPath(rootNode, key)); } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index f4efbf244111c..a31159a891eb8 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -2,12 +2,15 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.retry.DelayRetryPolicy; -import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.zookeeper.section.ClientContext; +import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; + /** * Created by aaa */ @@ -18,9 +21,8 @@ public class ClientFactory extends BaseClientFactory { public ClientFactory(){} public ClientFactory newClient(final String servers, final int sessionTimeoutMilliseconds) { - this.servers = servers; - this.sessionTimeoutMilliseconds = sessionTimeoutMilliseconds; - client = new UsualClient(servers, sessionTimeoutMilliseconds); + this.context = new ClientContext(servers, sessionTimeoutMilliseconds); + client = new UsualClient(context); logger.debug("new usual client"); return this; } @@ -29,10 +31,9 @@ public ClientFactory newClient(final String servers, final int sessionTimeoutMil * used for create new clients through a existing client * this client is not perhaps the client */ - @Override public synchronized BaseClientFactory newClientByOriginal(boolean closeOriginal) { IClient oldClient = this.client; - client = new UsualClient(servers, sessionTimeoutMilliseconds); + client = new UsualClient(context); if (closeOriginal){ oldClient.close(); } @@ -44,9 +45,8 @@ public synchronized BaseClientFactory newClientByOriginal(boolean closeOriginal) * partially prepared products */ public ClientFactory newCacheClient(final String servers, final int sessionTimeoutMilliseconds) { - this.servers = servers; - this.sessionTimeoutMilliseconds = sessionTimeoutMilliseconds; - client = new CacheClient(servers, sessionTimeoutMilliseconds); + this.context = new ClientContext(servers, sessionTimeoutMilliseconds); + client = new CacheClient(context); logger.debug("new cache client"); return this; } @@ -74,7 +74,13 @@ public ClientFactory authorization(final String scheme, final byte[] auth){ } public ClientFactory setRetryPolicy(final DelayRetryPolicy delayRetryPolicy){ - this.delayRetryPolicy = delayRetryPolicy; + ((ClientContext)context).setDelayRetryPolicy(delayRetryPolicy); return this; } + + @Override + public IClient start() throws IOException, InterruptedException { + ((ClientContext)context).setClientFactory(this); + return super.start(); + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index 9c6903902d6e4..c14e80b4754d2 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -3,6 +3,9 @@ import com.saaavsaaa.client.action.IExecStrategy; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.zookeeper.base.BaseContext; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; +import com.saaavsaaa.client.zookeeper.section.ClientContext; import com.saaavsaaa.client.zookeeper.strategy.*; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.*; @@ -19,11 +22,9 @@ */ public class UsualClient extends BaseClient { private static final Logger logger = LoggerFactory.getLogger(UsualClient.class); - protected final Map strategies = new ConcurrentHashMap<>(); - protected IExecStrategy strategy; - UsualClient(final String servers, final int sessionTimeoutMilliseconds) { - super(servers, sessionTimeoutMilliseconds); + UsualClient(final BaseContext context) { + super(context); } @Override @@ -32,52 +33,6 @@ public void start() throws IOException, InterruptedException { useExecStrategy(StrategyType.USUAL); } - @Override - public void close(){ - super.close(); - this.strategies.clear(); - } - - @Override - public synchronized void useExecStrategy(StrategyType strategyType) { - logger.debug("useExecStrategy:{}", strategyType); - if (strategies.containsKey(strategyType)){ - strategy = strategies.get(strategyType); - return; - } - - IProvider provider = getContext().getProvider(); - switch (strategyType){ - case USUAL:{ - strategy = new UsualStrategy(provider); - break; - } - case CONTEND:{ - strategy = new ContentionStrategy(provider); - break; - } - - case SYNC_RETRY:{ - strategy = new SyncRetryStrategy(context); - break; - } - case ASYNC_RETRY:{ - strategy = new AsyncRetryStrategy(context); - break; - } - case ALL_ASYNC_RETRY:{ - strategy = new AllAsyncRetryStrategy(context); - break; - } - default:{ - strategy = new UsualStrategy(provider); - break; - } - } - - strategies.put(strategyType, strategy); - } - @Override public String getDataString(final String key) throws KeeperException, InterruptedException { return strategy.getDataString(key); @@ -169,6 +124,6 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr @Override public ZKTransaction transaction() { - return new ZKTransaction(rootNode, zooKeeper); + return new ZKTransaction(rootNode, holder); } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index 5641e8bdc07f5..36c90e2ac679a 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -1,15 +1,19 @@ package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.section.ClientContext; +import com.saaavsaaa.client.action.IExecStrategy; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.StringUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.section.Listener; -import com.saaavsaaa.client.utility.Properties; -import com.saaavsaaa.client.section.WatcherCreator; -import com.saaavsaaa.client.zookeeper.strategy.StrategyType; -import org.apache.zookeeper.*; +import com.saaavsaaa.client.zookeeper.section.ClientContext; +import com.saaavsaaa.client.zookeeper.section.Listener; +import com.saaavsaaa.client.zookeeper.section.WatcherCreator; +import com.saaavsaaa.client.zookeeper.strategy.*; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.data.ACL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,104 +22,86 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CountDownLatch; /** * Created by aaa */ public abstract class BaseClient implements IClient { private static final Logger logger = LoggerFactory.getLogger(BaseClient.class); - private static final CountDownLatch CONNECTED = new CountDownLatch(1); -// protected static final Map watchers = new ConcurrentHashMap<>(); protected final boolean watched = true; //false - private Listener globalListener; - protected String rootNode = "/InitValue"; - protected boolean rootExist = false; - private final String servers; - private final int sessionTimeOut; - private String scheme; - private byte[] auth; - protected ClientContext context; + protected final Map strategies = new ConcurrentHashMap<>(); - protected ZooKeeper zooKeeper; + protected IExecStrategy strategy; + protected BaseContext context; protected List authorities; -// private BaseClientFactory clientFactory; + protected Holder holder; + + protected String rootNode = "/InitValue"; + protected boolean rootExist = false; - protected BaseClient(final String servers, final int sessionTimeoutMilliseconds) { - this.servers = servers; - this.sessionTimeOut = sessionTimeoutMilliseconds; + protected BaseClient(final BaseContext context) { + this.context = context; } @Override public void start() throws IOException, InterruptedException { - logger.debug("BaseClient servers:{},sessionTimeOut:{}", servers, sessionTimeOut); - zooKeeper = new ZooKeeper(servers, sessionTimeOut, startWatcher()); - if (!StringUtil.isNullOrBlank(scheme)) { - zooKeeper.addAuthInfo(scheme, auth); - logger.debug("BaseClient scheme:{},auth:{}", scheme, auth); - } - CONNECTED.await(); - context.setProvider(new BaseProvider(rootNode, zooKeeper, watched, authorities)); + holder = new Holder(getContext()); + holder.start(); } @Override public void close() { - try { - zooKeeper.close(); - logger.debug("zk closed"); - this.context.close(); - } catch (Exception ee){ - logger.warn("BaseClient close:{}", ee.getMessage()); - } - } - - public abstract void useExecStrategy(StrategyType strategyType); - - ZooKeeper getZooKeeper(){ - return zooKeeper; + holder.close(); + context.close(); + this.strategies.clear(); } - private Watcher startWatcher() { - return new Watcher(){ - public void process(WatchedEvent event) { - logger.debug("BaseClient process event:{}", event.toString()); - if(Event.EventType.None == event.getType()){ - if(Event.KeeperState.SyncConnected == event.getState()){ - CONNECTED.countDown(); - logger.debug("BaseClient startWatcher SyncConnected"); - return; - } else if (Event.KeeperState.Expired == event.getState()){ - try { - logger.warn("startWatcher Event.KeeperState.Expired"); - start(); - } catch (Exception ee){ - logger.error("event state Expired:{}", ee.getMessage(), ee); - } - } - } - if (globalListener != null){ - globalListener.process(event); - logger.debug("BaseClient " + Constants.GLOBAL_LISTENER_KEY + " process"); - } - if (Properties.INSTANCE.watchOn()){ - for (Listener listener : context.getWatchers().values()) { - if (listener.getPath() == null || listener.getPath().equals(event.getPath())){ - logger.debug("listener process:{}, listener:{}", listener.getPath(), listener.getKey()); - listener.process(event); - } - } - } + @Override + public synchronized void useExecStrategy(StrategyType strategyType) { + logger.debug("useExecStrategy:{}", strategyType); + if (strategies.containsKey(strategyType)){ + strategy = strategies.get(strategyType); + return; + } + + IProvider provider = new BaseProvider(rootNode, holder, watched, authorities); + switch (strategyType){ + case USUAL:{ + strategy = new UsualStrategy(provider); + break; + } + case CONTEND:{ + strategy = new ContentionStrategy(provider); + break; + } + case SYNC_RETRY:{ + strategy = new SyncRetryStrategy(provider, ((ClientContext)context).getDelayRetryPolicy()); + break; } - }; + case ASYNC_RETRY:{ + strategy = new AsyncRetryStrategy(provider, ((ClientContext)context).getDelayRetryPolicy()); + break; + } + case ALL_ASYNC_RETRY:{ + strategy = new AllAsyncRetryStrategy(provider, ((ClientContext)context).getDelayRetryPolicy()); + break; + } + default:{ + strategy = new UsualStrategy(provider); + break; + } + } + + strategies.put(strategyType, strategy); } void registerWatch(final Listener globalListener){ - if (this.globalListener != null){ + if (context.globalListener != null){ logger.warn("global listener can only register one"); return; } - this.globalListener = globalListener; + context.globalListener = globalListener; logger.debug("globalListenerRegistered:{}", globalListener.getKey()); } @@ -149,7 +135,7 @@ private void createNamespace(final byte[] date) throws KeeperException, Interrup return; } try { - zooKeeper.create(rootNode, date, authorities, CreateMode.PERSISTENT); + holder.zooKeeper.create(rootNode, date, authorities, CreateMode.PERSISTENT); logger.debug("creating root:{}", rootNode); } catch (KeeperException.NodeExistsException ee){ logger.warn("root create:{}", ee.getMessage()); @@ -157,7 +143,7 @@ private void createNamespace(final byte[] date) throws KeeperException, Interrup return; } rootExist = true; - zooKeeper.exists(rootNode, WatcherCreator.deleteWatcher(new Listener(rootNode) { + holder.zooKeeper.exists(rootNode, WatcherCreator.deleteWatcher(new Listener(rootNode) { @Override public void process(WatchedEvent event) { rootExist = false; @@ -167,7 +153,7 @@ public void process(WatchedEvent event) { } protected void deleteNamespace() throws KeeperException, InterruptedException { - zooKeeper.delete(rootNode, Constants.VERSION); + holder.zooKeeper.delete(rootNode, Constants.VERSION); rootExist = false; logger.debug("delete root:{},rootExist:{}", rootNode, rootExist); } @@ -177,19 +163,16 @@ void setRootNode(final String rootNode) { } void setAuthorities(final String scheme, final byte[] auth) { - this.scheme = scheme; - this.auth = auth; + context.scheme = scheme; + context.auth = auth; this.authorities = ZooDefs.Ids.CREATOR_ALL_ACL; } - void setContext(final ClientContext context){ - this.context = context; - } - public ClientContext getContext(){ + public BaseContext getContext(){ return context; } - BaseClientFactory getClientFactory() { - return context.getClientFactory(); + public IExecStrategy getStrategy() { + return strategy; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java index 44aaebbe78b2c..30355e7816313 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java @@ -2,8 +2,8 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.retry.DelayRetryPolicy; -import com.saaavsaaa.client.section.ClientContext; -import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.zookeeper.section.ClientContext; +import com.saaavsaaa.client.zookeeper.section.Listener; import java.io.IOException; @@ -16,13 +16,9 @@ public abstract class BaseClientFactory { protected String namespace; protected String scheme; protected byte[] auth; - protected DelayRetryPolicy delayRetryPolicy; - - protected String servers; - protected int sessionTimeoutMilliseconds; + protected BaseContext context; public IClient start() throws IOException, InterruptedException { - client.setContext(new ClientContext(delayRetryPolicy, this)); // wait expand client.setRootNode(namespace); client.setAuthorities(scheme , auth); client.start(); @@ -31,6 +27,4 @@ public IClient start() throws IOException, InterruptedException { } return client; } - - public abstract BaseClientFactory newClientByOriginal(boolean closeOriginal); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseContext.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseContext.java new file mode 100644 index 0000000000000..9b4e2bc576f7a --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseContext.java @@ -0,0 +1,42 @@ +package com.saaavsaaa.client.zookeeper.base; + +import com.saaavsaaa.client.zookeeper.section.Listener; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Created by aaa + */ +public abstract class BaseContext { + protected String servers; + protected int sessionTimeOut; + protected String scheme; + protected byte[] auth; + protected Listener globalListener; + protected final Map watchers = new ConcurrentHashMap<>(); + + public String getServers() { + return servers; + } + + public int getSessionTimeOut() { + return sessionTimeOut; + } + + public String getScheme() { + return scheme; + } + + public byte[] getAuth() { + return auth; + } + + public Map getWatchers(){ + return watchers; + } + + public void close() { + this.watchers.clear(); + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java index 74e9e67fffc8b..22fdef858853d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java @@ -1,8 +1,9 @@ package com.saaavsaaa.client.zookeeper.base; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.DelayPolicyExecutor; -import com.saaavsaaa.client.section.ClientContext; -import com.saaavsaaa.client.section.Connection; +import com.saaavsaaa.client.zookeeper.section.ClientContext; +import com.saaavsaaa.client.zookeeper.section.Connection; import org.apache.zookeeper.KeeperException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,13 +16,11 @@ */ public abstract class BaseOperation implements Delayed { private static final Logger logger = LoggerFactory.getLogger(BaseOperation.class); - protected final ClientContext context; - private final Connection connection; + protected final IProvider provider; protected DelayPolicyExecutor delayPolicyExecutor; - protected BaseOperation(final ClientContext context) { - this.context = context; - connection = new Connection(context); + protected BaseOperation(final IProvider provider) { + this.provider = provider; } public void setRetrial(final DelayPolicyExecutor delayPolicyExecutor){ @@ -55,7 +54,7 @@ public boolean executeOperation() throws KeeperException, InterruptedException { execute(); result = true; } catch (KeeperException ee) { - connection.check(ee); + provider.checkConnection(ee); result = false; } if (!result && delayPolicyExecutor.hasNext()){ diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java index 93a8205d2be98..3e9888b4700ee 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java @@ -2,6 +2,7 @@ import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.election.LeaderElection; +import com.saaavsaaa.client.zookeeper.section.Connection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; @@ -10,6 +11,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; import java.util.List; import java.util.Stack; @@ -18,14 +20,14 @@ */ public class BaseProvider implements IProvider { private static final Logger logger = LoggerFactory.getLogger(BaseProvider.class); - protected final ZooKeeper zooKeeper; + protected final Holder holder; protected final boolean watched; protected final List authorities; protected final String rootNode; - public BaseProvider(final String rootNode, final ZooKeeper zooKeeper, final boolean watched, final List authorities){ + public BaseProvider(final String rootNode, final Holder holder, final boolean watched, final List authorities){ this.rootNode = rootNode; - this.zooKeeper = zooKeeper; + this.holder = holder; this.watched = watched; this.authorities = authorities; } @@ -36,33 +38,33 @@ public String getDataString(final String key) throws KeeperException, Interrupte @Override public byte[] getData(final String key) throws KeeperException, InterruptedException { - return zooKeeper.getData(key, watched, null); + return holder.getZooKeeper().getData(key, watched, null); } @Override public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { - zooKeeper.getData(key, watched, callback, ctx); + holder.getZooKeeper().getData(key, watched, callback, ctx); } @Override public boolean exists(final String key) throws KeeperException, InterruptedException { - return null != zooKeeper.exists(key, watched); + return null != holder.getZooKeeper().exists(key, watched); } @Override public boolean exists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { - return null != zooKeeper.exists(key, watcher); + return null != holder.getZooKeeper().exists(key, watcher); } @Override public List getChildren(final String key) throws KeeperException, InterruptedException { - return zooKeeper.getChildren(key, watched); + return holder.getZooKeeper().getChildren(key, watched); } @Override public void create(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { try { - zooKeeper.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); + holder.getZooKeeper().create(key, value.getBytes(Constants.UTF_8), authorities, createMode); logger.debug("BaseProvider createCurrentOnly:{}", key); } catch (KeeperException.NoNodeException e) { logger.error("BaseProvider createCurrentOnly:{}", e.getMessage(), e); @@ -77,18 +79,18 @@ public void create(final String key, final String value, final CreateMode create @Override public void update(final String key, final String value) throws KeeperException, InterruptedException { - zooKeeper.setData(key, value.getBytes(Constants.UTF_8), Constants.VERSION); + holder.getZooKeeper().setData(key, value.getBytes(Constants.UTF_8), Constants.VERSION); } @Override public void delete(final String key) throws KeeperException, InterruptedException { - zooKeeper.delete(key, Constants.VERSION); + holder.getZooKeeper().delete(key, Constants.VERSION); logger.debug("BaseProvider deleteOnlyCurrent:{}", key); } @Override public void delete(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - zooKeeper.delete(key, Constants.VERSION, callback, ctx); + holder.getZooKeeper().delete(key, Constants.VERSION, callback, ctx); logger.debug("BaseProvider deleteOnlyCurrent:{},ctx:{}", key, ctx); } @@ -124,11 +126,22 @@ public void createInTransaction(final String key, final String value, final Crea transaction.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); } + @Override + public void checkConnection(final KeeperException e) throws KeeperException, InterruptedException { + if (Connection.needReset(e)){ + try { + holder.reset(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + } + public String getRootNode(){ return rootNode; } - public ZooKeeper getZooKeeper(){ - return zooKeeper; + public Holder getHolder(){ + return holder; } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java new file mode 100644 index 0000000000000..1e69c6beb6b4d --- /dev/null +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java @@ -0,0 +1,94 @@ +package com.saaavsaaa.client.zookeeper.base; + +import com.saaavsaaa.client.utility.Properties; +import com.saaavsaaa.client.utility.StringUtil; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.zookeeper.section.Listener; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; + +/** + * Created by aaa + */ +public class Holder { + private static final Logger logger = LoggerFactory.getLogger(Holder.class); + private static final CountDownLatch CONNECTED = new CountDownLatch(1); + + protected ZooKeeper zooKeeper; + protected final BaseContext context; + + Holder(final BaseContext context){ + this.context = context; + } + + public void start() throws IOException, InterruptedException { + logger.debug("Holder servers:{},sessionTimeOut:{}", context.servers, context.sessionTimeOut); + zooKeeper = new ZooKeeper(context.servers, context.sessionTimeOut, startWatcher()); + if (!StringUtil.isNullOrBlank(context.scheme)) { + zooKeeper.addAuthInfo(context.scheme, context.auth); + logger.debug("Holder scheme:{},auth:{}", context.scheme, context.auth); + } + CONNECTED.await(); + } + + private Watcher startWatcher() { + return new Watcher(){ + public void process(WatchedEvent event) { + logger.debug("BaseClient process event:{}", event.toString()); + if(Event.EventType.None == event.getType()){ + if(Event.KeeperState.SyncConnected == event.getState()){ + CONNECTED.countDown(); + logger.debug("BaseClient startWatcher SyncConnected"); + return; + } else if (Event.KeeperState.Expired == event.getState()){ + try { + logger.warn("startWatcher Event.KeeperState.Expired"); + start(); + } catch (Exception ee){ + logger.error("event state Expired:{}", ee.getMessage(), ee); + } + } + } + if (context.globalListener != null){ + context.globalListener.process(event); + logger.debug("BaseClient " + Constants.GLOBAL_LISTENER_KEY + " process"); + } + if (Properties.INSTANCE.watchOn()){ + for (Listener listener : context.getWatchers().values()) { + if (listener.getPath() == null || listener.getPath().equals(event.getPath())){ + logger.debug("listener process:{}, listener:{}", listener.getPath(), listener.getKey()); + listener.process(event); + } + } + } + } + }; + } + + public void reset() throws IOException, InterruptedException { + logger.debug("zk reset...................................."); + close(); + start(); + logger.debug("....................................zk reset"); + } + + public void close() { + try { + zooKeeper.close(); + logger.debug("zk closed"); + this.context.close(); + } catch (Exception ee){ + logger.warn("Holder close:{}", ee.getMessage()); + } + } + + public ZooKeeper getZooKeeper() { + return zooKeeper; + } +} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java index 4157aaa7920e5..85ef79034c4c7 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java @@ -1,11 +1,9 @@ package com.saaavsaaa.client.zookeeper.operation; -import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.section.ClientContext; -import com.saaavsaaa.client.section.Connection; +import com.saaavsaaa.client.zookeeper.section.ClientContext; +import com.saaavsaaa.client.zookeeper.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -18,8 +16,8 @@ public class CreateAllNeedOperation extends BaseOperation { private final String value; private final CreateMode createMode; - public CreateAllNeedOperation(final ClientContext context, final String key, final String value, final CreateMode createMode) { - super(context); + public CreateAllNeedOperation(final IProvider provider, final String key, final String value, final CreateMode createMode) { + super(provider); this.key = key; this.value = value; this.createMode = createMode; @@ -27,7 +25,7 @@ public CreateAllNeedOperation(final ClientContext context, final String key, fin @Override protected void execute() throws KeeperException, InterruptedException { - new UsualStrategy(context.getProvider()).createAllNeedPath(key, value, createMode); + new UsualStrategy(provider).createAllNeedPath(key, value, createMode); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java index 762ea216c3909..86ef220d46519 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java @@ -1,11 +1,9 @@ package com.saaavsaaa.client.zookeeper.operation; -import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.section.ClientContext; -import com.saaavsaaa.client.section.Connection; +import com.saaavsaaa.client.zookeeper.section.ClientContext; +import com.saaavsaaa.client.zookeeper.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -17,8 +15,8 @@ public class CreateCurrentOperation extends BaseOperation { private final String value; private final CreateMode createMode; - public CreateCurrentOperation(final ClientContext context, final String key, final String value, final CreateMode createMode) { - super(context); + public CreateCurrentOperation(final IProvider provider, final String key, final String value, final CreateMode createMode) { + super(provider); this.key = key; this.value = value; this.createMode = createMode; @@ -26,7 +24,6 @@ public CreateCurrentOperation(final ClientContext context, final String key, fin @Override public void execute() throws KeeperException, InterruptedException { - IProvider provider = context.getProvider(); provider.create(provider.getRealPath(key), value, createMode); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java index a20e8e1baa714..6587ada50ffba 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java @@ -1,6 +1,6 @@ package com.saaavsaaa.client.zookeeper.operation; -import com.saaavsaaa.client.section.ClientContext; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.KeeperException; @@ -11,14 +11,14 @@ public class DeleteAllChildrenOperation extends BaseOperation { private final String key; - public DeleteAllChildrenOperation(final ClientContext context, final String key) { - super(context); + public DeleteAllChildrenOperation(final IProvider provider, final String key) { + super(provider); this.key = key; } @Override protected void execute() throws KeeperException, InterruptedException { - new UsualStrategy(context.getProvider()).deleteAllChildren(key); + new UsualStrategy(provider).deleteAllChildren(key); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java index 918c4a6c5b18a..3f633b0ec3074 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java @@ -1,6 +1,6 @@ package com.saaavsaaa.client.zookeeper.operation; -import com.saaavsaaa.client.section.ClientContext; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.KeeperException; @@ -11,14 +11,14 @@ public class DeleteCurrentBranchOperation extends BaseOperation { private final String key; - public DeleteCurrentBranchOperation(final ClientContext context, final String key) { - super(context); + public DeleteCurrentBranchOperation(final IProvider provider, final String key) { + super(provider); this.key = key; } @Override protected void execute() throws KeeperException, InterruptedException { - new UsualStrategy(context.getProvider()).deleteCurrentBranch(key); + new UsualStrategy(provider).deleteCurrentBranch(key); } @Override diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java index 81d45ecb4185b..31c635b28b169 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java @@ -1,7 +1,6 @@ package com.saaavsaaa.client.zookeeper.operation; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import org.apache.zookeeper.KeeperException; @@ -10,14 +9,13 @@ */ public class DeleteCurrentOperation extends BaseOperation { private final String key; - public DeleteCurrentOperation(final ClientContext context, final String key) { - super(context); + public DeleteCurrentOperation(final IProvider provider, final String key) { + super(provider); this.key = key; } @Override protected void execute() throws KeeperException, InterruptedException { - IProvider provider = context.getProvider(); provider.delete(provider.getRealPath(key)); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java index bc0be7ebfb9b5..2658aa50fd390 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java @@ -1,7 +1,6 @@ package com.saaavsaaa.client.zookeeper.operation; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.section.ClientContext; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import org.apache.zookeeper.KeeperException; @@ -12,15 +11,14 @@ public class UpdateOperation extends BaseOperation { private final String key; private final String value; - public UpdateOperation(final ClientContext context, final String key, final String value) { - super(context); + public UpdateOperation(final IProvider provider, final String key, final String value) { + super(provider); this.key = key; this.value = value; } @Override protected void execute() throws KeeperException, InterruptedException { - IProvider provider = context.getProvider(); provider.update(provider.getRealPath(key), value); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java index 27082ea9aa358..13780debac502 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java @@ -4,6 +4,7 @@ import com.saaavsaaa.client.retry.RetryCount; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; +import com.saaavsaaa.client.zookeeper.base.Holder; import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentOperation; import com.saaavsaaa.client.zookeeper.operation.UpdateOperation; @@ -24,8 +25,8 @@ public class RetryProvider extends BaseProvider { private static final Logger logger = LoggerFactory.getLogger(RetryProvider.class); - public RetryProvider(String rootNode, ZooKeeper zooKeeper, boolean watched, List authorities) { - super(rootNode, zooKeeper, watched, authorities); + public RetryProvider(String rootNode, Holder holder, boolean watched, List authorities) { + super(rootNode, holder, watched, authorities); RetryCount.INSTANCE.start(); } @@ -33,7 +34,7 @@ public RetryProvider(String rootNode, ZooKeeper zooKeeper, boolean watched, List @Override public byte[] getData(final String key) throws KeeperException, InterruptedException { try { - return zooKeeper.getData(key, watched, null); + return holder.getZooKeeper().getData(key, watched, null); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException getData:{}", key); if (RetryCount.INSTANCE.continueExecute()) { @@ -48,7 +49,7 @@ public byte[] getData(final String key) throws KeeperException, InterruptedExcep @Override public boolean exists(final String key) throws KeeperException, InterruptedException { try { - return null != zooKeeper.exists(key, watched); + return null != holder.getZooKeeper().exists(key, watched); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException checkExists:{}", key); if (RetryCount.INSTANCE.continueExecute()) { @@ -63,7 +64,7 @@ public boolean exists(final String key) throws KeeperException, InterruptedExcep @Override public boolean exists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { try { - return null != zooKeeper.exists(key, watcher); + return null != holder.getZooKeeper().exists(key, watcher); } catch (KeeperException.SessionExpiredException ee){ logger.warn("RetryProvider SessionExpiredException checkExists:{}", key); if (RetryCount.INSTANCE.continueExecute()) { diff --git a/src/main/java/com/saaavsaaa/client/section/Callable.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/Callable.java similarity index 78% rename from src/main/java/com/saaavsaaa/client/section/Callable.java rename to src/main/java/com/saaavsaaa/client/zookeeper/section/Callable.java index 13df7d6d49d8c..27749b4505109 100644 --- a/src/main/java/com/saaavsaaa/client/section/Callable.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/section/Callable.java @@ -1,5 +1,6 @@ -package com.saaavsaaa.client.section; +package com.saaavsaaa.client.zookeeper.section; +import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.DelayPolicyExecutor; import com.saaavsaaa.client.retry.DelayRetryPolicy; import org.apache.zookeeper.KeeperException; @@ -11,17 +12,13 @@ */ public abstract class Callable { private static final Logger logger = LoggerFactory.getLogger(Callable.class); - private final Connection connection; + protected final DelayPolicyExecutor delayPolicyExecutor; + protected final IProvider provider; private T result; - public Callable(final ClientContext context){ - this.connection = new Connection(context); - DelayRetryPolicy delayRetryPolicy = context.getDelayRetryPolicy(); - if (delayRetryPolicy == null){ - logger.warn("Callable constructor context's delayRetryPolicy is null"); - delayRetryPolicy = DelayRetryPolicy.newNoInitDelayPolicy(); - } + public Callable(final IProvider provider, final DelayRetryPolicy delayRetryPolicy){ this.delayPolicyExecutor = new DelayPolicyExecutor(delayRetryPolicy); + this.provider = provider; } public abstract void call() throws KeeperException, InterruptedException; @@ -39,7 +36,7 @@ public void exec() throws KeeperException, InterruptedException { } catch (KeeperException e) { logger.warn("exec KeeperException:{}", e.getMessage()); delayPolicyExecutor.next(); - connection.check(e); + provider.checkConnection(e); execDelay(); } catch (InterruptedException e) { throw e; diff --git a/src/main/java/com/saaavsaaa/client/section/ClientContext.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/ClientContext.java similarity index 55% rename from src/main/java/com/saaavsaaa/client/section/ClientContext.java rename to src/main/java/com/saaavsaaa/client/zookeeper/section/ClientContext.java index f73bb97a08400..c7271093b63c6 100644 --- a/src/main/java/com/saaavsaaa/client/section/ClientContext.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/section/ClientContext.java @@ -1,8 +1,9 @@ -package com.saaavsaaa.client.section; +package com.saaavsaaa.client.zookeeper.section; import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.DelayRetryPolicy; import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; +import com.saaavsaaa.client.zookeeper.base.BaseContext; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -10,21 +11,24 @@ /** * Created by aaa */ -public final class ClientContext { - private final Map watchers = new ConcurrentHashMap<>(); +public final class ClientContext extends BaseContext { + private DelayRetryPolicy delayRetryPolicy; private BaseClientFactory clientFactory; - private IProvider provider; - public ClientContext(final DelayRetryPolicy delayRetryPolicy, final BaseClientFactory clientFactory) { - this(delayRetryPolicy, clientFactory, null); + public ClientContext(final String servers, final int sessionTimeoutMilliseconds) { + super(); + super.servers = servers; + super.sessionTimeOut = sessionTimeoutMilliseconds; } - public ClientContext(final DelayRetryPolicy delayRetryPolicy, final BaseClientFactory clientFactory, final IProvider provider) { + public void setDelayRetryPolicy(DelayRetryPolicy delayRetryPolicy) { this.delayRetryPolicy = delayRetryPolicy; + } + + public void setClientFactory(BaseClientFactory clientFactory) { this.clientFactory = clientFactory; - this.provider = provider; } public DelayRetryPolicy getDelayRetryPolicy() { @@ -35,28 +39,15 @@ public BaseClientFactory getClientFactory() { return clientFactory; } - public IProvider getProvider() { - return provider; - } - public void setProvider(IProvider provider){ - this.provider = provider; - } - - public Map getWatchers(){ - return watchers; - } - public void close() { + super.close(); this.delayRetryPolicy = null; this.clientFactory = null; - this.provider = null; - this.watchers.clear(); } public void updateContext(final ClientContext context){ this.delayRetryPolicy = context.getDelayRetryPolicy(); this.clientFactory = context.clientFactory; - this.provider = context.getProvider(); this.watchers.clear(); this.watchers.putAll(context.getWatchers()); } diff --git a/src/main/java/com/saaavsaaa/client/section/ClientTask.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/ClientTask.java similarity index 93% rename from src/main/java/com/saaavsaaa/client/section/ClientTask.java rename to src/main/java/com/saaavsaaa/client/zookeeper/section/ClientTask.java index 280434afbef72..38b59a34df087 100644 --- a/src/main/java/com/saaavsaaa/client/section/ClientTask.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/section/ClientTask.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.section; +package com.saaavsaaa.client.zookeeper.section; import com.saaavsaaa.client.action.IProvider; import org.apache.zookeeper.KeeperException; diff --git a/src/main/java/com/saaavsaaa/client/section/Connection.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/Connection.java similarity index 82% rename from src/main/java/com/saaavsaaa/client/section/Connection.java rename to src/main/java/com/saaavsaaa/client/zookeeper/section/Connection.java index 9fbe48751b2eb..af58bacaac692 100644 --- a/src/main/java/com/saaavsaaa/client/section/Connection.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/section/Connection.java @@ -1,7 +1,5 @@ -package com.saaavsaaa.client.section; +package com.saaavsaaa.client.zookeeper.section; -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.zookeeper.base.BaseClient; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.slf4j.Logger; @@ -28,10 +26,20 @@ public class Connection { exceptionResets.put(KeeperException.Code.OPERATIONTIMEOUT.intValue(), false); } + @Deprecated public Connection(final ClientContext context){ this.context = context; } + public static boolean needReset(KeeperException e) throws KeeperException { + int code = e.code().intValue(); + if (!exceptionResets.containsKey(code)){ + throw e; + } + return exceptionResets.get(code); + } + + @Deprecated public void check(KeeperException e) throws KeeperException { int code = e.code().intValue(); if (!exceptionResets.containsKey(code)){ @@ -52,8 +60,8 @@ public void check(KeeperException e) throws KeeperException { private void resetConnection() throws IOException, InterruptedException { logger.debug("resetConnection......................................................"); - IClient client = context.getClientFactory().newClientByOriginal(true).start(); - this.context.updateContext(((BaseClient)client).getContext()); +// IClient client = context.getClientFactory().newClientByOriginal(true).start(); +// this.context.updateContext(((BaseClient)client).getContext()); logger.debug("......................................................connection reset"); } diff --git a/src/main/java/com/saaavsaaa/client/section/Listener.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/Listener.java similarity index 92% rename from src/main/java/com/saaavsaaa/client/section/Listener.java rename to src/main/java/com/saaavsaaa/client/zookeeper/section/Listener.java index a08f16e5cdf9a..5596dae8bb245 100644 --- a/src/main/java/com/saaavsaaa/client/section/Listener.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/section/Listener.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.section; +package com.saaavsaaa.client.zookeeper.section; import org.apache.zookeeper.WatchedEvent; diff --git a/src/main/java/com/saaavsaaa/client/section/WatcherCreator.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/WatcherCreator.java similarity index 94% rename from src/main/java/com/saaavsaaa/client/section/WatcherCreator.java rename to src/main/java/com/saaavsaaa/client/zookeeper/section/WatcherCreator.java index ecaf3c0518276..bc5321f9e3b42 100644 --- a/src/main/java/com/saaavsaaa/client/section/WatcherCreator.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/section/WatcherCreator.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.section; +package com.saaavsaaa.client.zookeeper.section; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java index c2e8c1362acc2..d1fa0f64c9f79 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java @@ -2,8 +2,8 @@ import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.AsyncRetryCenter; -import com.saaavsaaa.client.section.ClientContext; -import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.retry.DelayRetryPolicy; +import com.saaavsaaa.client.zookeeper.section.ClientContext; import com.saaavsaaa.client.zookeeper.operation.CreateAllNeedOperation; import com.saaavsaaa.client.zookeeper.operation.DeleteAllChildrenOperation; import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentBranchOperation; @@ -17,8 +17,8 @@ */ public class AllAsyncRetryStrategy extends AsyncRetryStrategy { private static final Logger logger = LoggerFactory.getLogger(AllAsyncRetryStrategy.class); - public AllAsyncRetryStrategy(final ClientContext context) { - super(context); + public AllAsyncRetryStrategy(final IProvider provider, final DelayRetryPolicy delayRetryPolicy) { + super(provider, delayRetryPolicy); } @Override @@ -27,7 +27,7 @@ public void createAllNeedPath(final String key, final String value, final Create super.createAllNeedPath(key, value, createMode); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AllAsyncRetryStrategy SessionExpiredException CreateAllNeedOperation:{}", key); - AsyncRetryCenter.INSTANCE.add(new CreateAllNeedOperation(context, key, value, createMode)); + AsyncRetryCenter.INSTANCE.add(new CreateAllNeedOperation(provider, key, value, createMode)); } } @@ -37,7 +37,7 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup super.deleteAllChildren(key); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AllAsyncRetryStrategy SessionExpiredException deleteAllChildren:{}", key); - AsyncRetryCenter.INSTANCE.add(new DeleteAllChildrenOperation(context, key)); + AsyncRetryCenter.INSTANCE.add(new DeleteAllChildrenOperation(provider, key)); } } @@ -47,7 +47,7 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr super.deleteCurrentBranch(key); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AllAsyncRetryStrategy SessionExpiredException deleteCurrentBranch:{}", key); - AsyncRetryCenter.INSTANCE.add(new DeleteCurrentBranchOperation(context, key)); + AsyncRetryCenter.INSTANCE.add(new DeleteCurrentBranchOperation(provider, key)); } } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java index c40a9904de774..dec5603cd694c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java @@ -2,7 +2,8 @@ import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.AsyncRetryCenter; -import com.saaavsaaa.client.section.ClientContext; +import com.saaavsaaa.client.retry.DelayRetryPolicy; +import com.saaavsaaa.client.zookeeper.section.ClientContext; import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentOperation; import com.saaavsaaa.client.zookeeper.operation.UpdateOperation; @@ -16,12 +17,10 @@ */ public class AsyncRetryStrategy extends SyncRetryStrategy { private static final Logger logger = LoggerFactory.getLogger(AsyncRetryStrategy.class); - protected final ClientContext context; - public AsyncRetryStrategy(final ClientContext context){ - super(context); - this.context = context; - AsyncRetryCenter.INSTANCE.init(context.getDelayRetryPolicy()); + public AsyncRetryStrategy(final IProvider provider, final DelayRetryPolicy delayRetryPolicy){ + super(provider, delayRetryPolicy); + AsyncRetryCenter.INSTANCE.init(this.delayRetryPolicy); AsyncRetryCenter.INSTANCE.start(); } @@ -32,7 +31,7 @@ public void createCurrentOnly(final String key, final String value, final Create provider.create(path, value, createMode); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AsyncRetryStrategy SessionExpiredException createCurrentOnly:{}", path); - AsyncRetryCenter.INSTANCE.add(new CreateCurrentOperation(context, path, value, createMode)); + AsyncRetryCenter.INSTANCE.add(new CreateCurrentOperation(provider, path, value, createMode)); } } @@ -43,7 +42,7 @@ public void update(final String key, final String value) throws KeeperException, provider.update(path, value); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AsyncRetryStrategy SessionExpiredException update:{}", path); - AsyncRetryCenter.INSTANCE.add(new UpdateOperation(context, path, value)); + AsyncRetryCenter.INSTANCE.add(new UpdateOperation(provider, path, value)); } } @@ -54,7 +53,7 @@ public void deleteOnlyCurrent(final String key) throws KeeperException, Interrup provider.delete(path); } catch (KeeperException.SessionExpiredException ee){ logger.warn("AsyncRetryStrategy SessionExpiredException deleteOnlyCurrent:{}", path); - AsyncRetryCenter.INSTANCE.add(new DeleteCurrentOperation(context, path)); + AsyncRetryCenter.INSTANCE.add(new DeleteCurrentOperation(provider, path)); } } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java index 2956b86a399c0..525a6d75a73e8 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java @@ -4,7 +4,7 @@ import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.section.Callback; +import com.saaavsaaa.client.action.Callback; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java index 4e84dc927111d..912528855dc7c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java @@ -1,7 +1,9 @@ package com.saaavsaaa.client.zookeeper.strategy; -import com.saaavsaaa.client.section.Callable; -import com.saaavsaaa.client.section.ClientContext; +import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.retry.DelayRetryPolicy; +import com.saaavsaaa.client.zookeeper.section.Callable; +import com.saaavsaaa.client.zookeeper.section.ClientContext; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.Watcher; @@ -15,16 +17,22 @@ */ public class SyncRetryStrategy extends UsualStrategy{ private static final Logger logger = LoggerFactory.getLogger(SyncRetryStrategy.class); - protected final ClientContext context; + protected final DelayRetryPolicy delayRetryPolicy; - public SyncRetryStrategy(final ClientContext context) { - super(context.getProvider()); - this.context = context; + public SyncRetryStrategy(final IProvider provider, final DelayRetryPolicy delayRetryPolicy) { + super(provider); + if (delayRetryPolicy == null){ + logger.warn("Callable constructor context's delayRetryPolicy is null"); + this.delayRetryPolicy = DelayRetryPolicy.newNoInitDelayPolicy(); + } else { + this.delayRetryPolicy = delayRetryPolicy; + } +// this.delayRetryPolicy = delayRetryPolicy == null? DelayRetryPolicy.newNoInitDelayPolicy() : delayRetryPolicy; } @Override public byte[] getData(final String key) throws KeeperException, InterruptedException { - Callable callable = new Callable(context) { + Callable callable = new Callable(provider, delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { setResult(provider.getData(provider.getRealPath(key))); @@ -35,7 +43,7 @@ public void call() throws KeeperException, InterruptedException { @Override public boolean checkExists(final String key) throws KeeperException, InterruptedException { - Callable callable = new Callable(context) { + Callable callable = new Callable(provider, delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { setResult(provider.exists(provider.getRealPath(key))); @@ -46,7 +54,7 @@ public void call() throws KeeperException, InterruptedException { @Override public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { - Callable callable = new Callable(context) { + Callable callable = new Callable(provider, delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { setResult(provider.exists(provider.getRealPath(key), watcher)); @@ -57,7 +65,7 @@ public void call() throws KeeperException, InterruptedException { @Override public List getChildren(final String key) throws KeeperException, InterruptedException { - Callable> callable = new Callable(context) { + Callable> callable = new Callable(provider, delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { setResult(provider.getChildren(provider.getRealPath(key))); @@ -68,7 +76,7 @@ public void call() throws KeeperException, InterruptedException { @Override public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - Callable callable = new Callable(context) { + Callable callable = new Callable(provider, delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { provider.create(provider.getRealPath(key), value, createMode); @@ -79,7 +87,7 @@ public void call() throws KeeperException, InterruptedException { @Override public void update(final String key, final String value) throws KeeperException, InterruptedException { - Callable callable = new Callable(context) { + Callable callable = new Callable(provider, delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { provider.update(provider.getRealPath(key), value); @@ -90,7 +98,7 @@ public void call() throws KeeperException, InterruptedException { @Override public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - Callable callable = new Callable(context) { + Callable callable = new Callable(provider, delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { provider.delete(provider.getRealPath(key)); @@ -101,7 +109,7 @@ public void call() throws KeeperException, InterruptedException { @Override public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - Callable callable = new Callable(context) { + Callable callable = new Callable(provider, delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { new UsualStrategy(provider).createAllNeedPath(key, value, createMode); @@ -112,7 +120,7 @@ public void call() throws KeeperException, InterruptedException { @Override public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { - Callable callable = new Callable(context) { + Callable callable = new Callable(provider, delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { new UsualStrategy(provider).deleteAllChildren(key); @@ -123,7 +131,7 @@ public void call() throws KeeperException, InterruptedException { @Override public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - Callable callable = new Callable(context) { + Callable callable = new Callable(provider, delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { new UsualStrategy(provider).deleteCurrentBranch(key); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java index 95d31fe448f93..40bc169c66882 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java @@ -4,7 +4,7 @@ import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.section.Callback; +import com.saaavsaaa.client.action.Callback; import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.CreateMode; @@ -39,7 +39,7 @@ private LeaderElection buildCreateAllNeedElection(final String key, final String @Override public void action() throws KeeperException, InterruptedException { logger.debug("ContentionStrategy createAllNeedPath action:{}", key); - ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getZooKeeper()); + ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getHolder()); createBegin(key, value, createMode, transaction); transaction.commit(); } @@ -77,7 +77,7 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup provider.executeContention(new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getZooKeeper()); + ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getHolder()); deleteChildren(provider.getRealPath(key), true, transaction); transaction.commit(); } @@ -106,7 +106,7 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr provider.executeContention(new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { - ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getZooKeeper()); + ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getHolder()); deleteBranch(provider.getRealPath(key), transaction); transaction.commit(); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java index 492ba847b7d58..f5b3684ee3dc7 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.zookeeper.transaction; import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.zookeeper.base.Holder; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; import org.slf4j.Logger; @@ -16,8 +17,8 @@ public class ZKTransaction { private final Transaction transaction; private final String rootNode; - public ZKTransaction(final String root, final ZooKeeper zk) { - transaction = zk.transaction(); + public ZKTransaction(final String root, final Holder holder) { + transaction = holder.getZooKeeper().transaction(); rootNode = root; logger.debug("ZKTransaction root:{}", rootNode); } diff --git a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java index 7aeeb4b4526e1..ffb41219f6ef0 100644 --- a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java @@ -1,8 +1,9 @@ package com.saaavsaaa.client.retry; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.section.ClientContext; -import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.zookeeper.section.ClientContext; +import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.ClientFactory; @@ -21,13 +22,13 @@ * Created by aaa */ public class AsyncRetryCenterTest { - private ClientContext context; - private BaseClient client; + private IProvider provider; + private IClient client; @Before public void start() throws IOException, InterruptedException { - client = ((BaseClient)createClient()); - context = client.getContext(); + client = createClient(); + provider = ((BaseClient)client).getStrategy().getProvider(); AsyncRetryCenter.INSTANCE.init(new DelayRetryPolicy(3, 3, 10)); AsyncRetryCenter.INSTANCE.start(); } @@ -36,7 +37,7 @@ protected IClient createClient() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); Listener listener = TestSupport.buildListener(); IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - ((BaseClient)client).useExecStrategy(StrategyType.ASYNC_RETRY); + client.useExecStrategy(StrategyType.ASYNC_RETRY); return client; } @@ -49,17 +50,17 @@ public void stop(){ public void create() throws InterruptedException, KeeperException { String key = "a"; String value = "bbb11"; - if (!context.getProvider().exists("/" + TestSupport.ROOT)) { + if (!provider.exists("/" + TestSupport.ROOT)) { System.out.println("exist root"); - context.getProvider().create("/" + TestSupport.ROOT, Constants.NOTHING_VALUE, CreateMode.PERSISTENT); + provider.create("/" + TestSupport.ROOT, Constants.NOTHING_VALUE, CreateMode.PERSISTENT); } - AsyncRetryCenter.INSTANCE.add(new TestCreateCurrentOperation(context, key, value, CreateMode.PERSISTENT)); + AsyncRetryCenter.INSTANCE.add(new TestCreateCurrentOperation(provider, key, value, CreateMode.PERSISTENT)); Thread.sleep(2000); String path = PathUtil.getRealPath(TestSupport.ROOT, key); - assert context.getProvider().exists(path); + assert provider.exists(path); // assert client.checkExists(path); - context.getProvider().delete(path); - context.getProvider().delete(context.getProvider().getRealPath(TestSupport.ROOT)); - assert !context.getProvider().exists(path); + provider.delete(path); + provider.delete(provider.getRealPath(TestSupport.ROOT)); + assert !provider.exists(path); } } diff --git a/src/test/java/com/saaavsaaa/client/retry/TestCallable.java b/src/test/java/com/saaavsaaa/client/retry/TestCallable.java new file mode 100644 index 0000000000000..f6e2bcbc6be8a --- /dev/null +++ b/src/test/java/com/saaavsaaa/client/retry/TestCallable.java @@ -0,0 +1,34 @@ +package com.saaavsaaa.client.retry; + +import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.zookeeper.section.Callable; +import com.saaavsaaa.client.zookeeper.section.ClientContext; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; +import org.apache.zookeeper.KeeperException; + +/** + * Created by aaa + */ +public abstract class TestCallable extends Callable { + private int count = 0; + + public TestCallable(final IProvider provider, final DelayRetryPolicy delayRetryPolicy) { + super(provider, delayRetryPolicy); + } + + @Override + public void call() throws KeeperException, InterruptedException { + if (count < 2){ + count++; +// throw new KeeperException.SessionExpiredException(); +// throw new KeeperException.ConnectionLossException(); + + System.out.println("TestCallable injectSessionExpiration==================================================" + count); + ((BaseProvider)provider).getHolder().getZooKeeper().getTestable().injectSessionExpiration(); + } + System.out.println("TestCallable ================================================" + count); + test(); + } + + public abstract void test() throws KeeperException, InterruptedException; +} diff --git a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java index e6ad2cad4140f..ec461f150cf19 100644 --- a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java +++ b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.retry; -import com.saaavsaaa.client.section.ClientContext; +import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.zookeeper.section.ClientContext; import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; import org.apache.zookeeper.CreateMode; @@ -12,8 +13,8 @@ public class TestCreateCurrentOperation extends CreateCurrentOperation { private int count = 0; - public TestCreateCurrentOperation(final ClientContext context, String key, String value, CreateMode createMode) { - super(context, key, value, createMode); + public TestCreateCurrentOperation(final IProvider provider, String key, String value, CreateMode createMode) { + super(provider, key, value, createMode); } @Override @@ -24,7 +25,7 @@ public void execute() throws KeeperException, InterruptedException { // throw new KeeperException.ConnectionLossException(); System.out.println("test injectSessionExpiration==================================================" + count); - ((BaseProvider)context.getProvider()).getZooKeeper().getTestable().injectSessionExpiration(); + ((BaseProvider)provider).getHolder().getZooKeeper().getTestable().injectSessionExpiration(); } System.out.println("test ================================================" + count); super.execute(); diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java index 40f59867d4064..0e1ba599ae770 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java index 13501cd1ba7cb..d760ec01c8bdc 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.zookeeper.section.Listener; import java.io.IOException; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java index 55f1ce4deb7e5..c37425a17d9eb 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.zookeeper.base.BaseClientTest; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.ZooKeeper; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java index ca6a55941b725..4afec2a531a0b 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java @@ -2,7 +2,7 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; import org.apache.zookeeper.CreateMode; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java index 8e52661dfda56..26419007e7cc3 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java @@ -1,9 +1,19 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.retry.AsyncRetryCenter; +import com.saaavsaaa.client.retry.DelayRetryPolicy; +import com.saaavsaaa.client.retry.TestCallable; +import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.zookeeper.strategy.StrategyType; +import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.junit.Before; +import org.junit.Test; import java.io.IOException; @@ -11,11 +21,102 @@ * Created by aaa */ public class SyncRetryStrategyTest extends UsualClientTest{ - @Override - protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { + private IProvider provider; + private IClient client; + + @Before + public void start() throws IOException, InterruptedException { + client = createClient(); + provider = ((BaseClient)client).getStrategy().getProvider(); + AsyncRetryCenter.INSTANCE.init(new DelayRetryPolicy(3, 3, 10)); + AsyncRetryCenter.INSTANCE.start(); + } + + protected IClient createClient() throws IOException, InterruptedException { + ClientFactory creator = new ClientFactory(); Listener listener = TestSupport.buildListener(); IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - ((BaseClient)client).useExecStrategy(StrategyType.SYNC_RETRY); + client.useExecStrategy(StrategyType.SYNC_RETRY); return client; } + + @Test + public void createChild() throws KeeperException, InterruptedException { + String key = "a/b/bb"; + TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { + @Override + public void test() throws KeeperException, InterruptedException { + new UsualStrategy(provider).createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); + } + }; + callable.exec(); + + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) != null; + new UsualStrategy(provider).deleteCurrentBranch(key); + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) == null; + } + + @Test + public void deleteBranch() throws KeeperException, InterruptedException { + String keyB = "a/b/bb"; + String value = "bbb11"; + new UsualStrategy(provider).createAllNeedPath(keyB, value, CreateMode.PERSISTENT); + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; + String keyC = "a/c/cc"; + new UsualStrategy(provider).createAllNeedPath(keyC, "ccc11", CreateMode.PERSISTENT); + assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; + + TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { + @Override + public void test() throws KeeperException, InterruptedException { + new UsualStrategy(provider).deleteCurrentBranch(keyC); + } + }; + callable.exec(); + } + + @Test + public void isExisted() throws KeeperException, InterruptedException { + super.isExisted(testClient); + } + + @Test + public void get() throws KeeperException, InterruptedException { + super.get(testClient); + } + + @Test + public void asynGet() throws KeeperException, InterruptedException { + super.asynGet(testClient); + } + + @Test + public void getChildrenKeys() throws KeeperException, InterruptedException { + super.getChildrenKeys(testClient); + } + + @Test + public void persist() throws KeeperException, InterruptedException { + super.persist(testClient); + } + + @Test + public void persistEphemeral() throws KeeperException, InterruptedException { + super.persistEphemeral(testClient); + } + + @Test + public void delAllChildren() throws KeeperException, InterruptedException { + super.delAllChildren(testClient); + } + + @Test + public void watch() throws KeeperException, InterruptedException { + super.watch(testClient); + } + + @Test + public void close() throws Exception { + super.close(testClient); + } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java b/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java index a37af67c69080..a146217ff99ea 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java @@ -1,6 +1,6 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.zookeeper.section.Listener; import org.apache.zookeeper.WatchedEvent; /** diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java index ad98e993af4fc..f4ad7bcb1911c 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.zookeeper.section.Listener; import java.io.IOException; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java index 3e51d3342adbf..7a7806c03e27d 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java @@ -3,7 +3,7 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.section.Listener; +import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.zookeeper.ClientFactory; import com.saaavsaaa.client.zookeeper.TestSupport; import org.apache.zookeeper.*; @@ -35,7 +35,7 @@ public void start() throws IOException, InterruptedException { } protected ZooKeeper getZooKeeper(IClient client){ - zooKeeper = ((BaseClient)client).getZooKeeper(); + zooKeeper = ((BaseClient)client).holder.getZooKeeper(); return zooKeeper; } From bdbdd4ac319321236e7a035eee168f74faefd3b0 Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 30 May 2018 11:15:59 +0800 Subject: [PATCH 132/351] add Holder class and restructure --- .../com/saaavsaaa/client/action/IAction.java | 1 - .../com/saaavsaaa/client/action/IClient.java | 2 +- .../saaavsaaa/client/action/IExecStrategy.java | 2 -- .../com/saaavsaaa/client/action/IProvider.java | 2 +- .../saaavsaaa/client/utility/Properties.java | 2 -- .../client/zookeeper/UsualClient.java | 8 +------- .../client/zookeeper/base/BaseClient.java | 1 + .../zookeeper/base/BaseClientFactory.java | 2 -- .../client/zookeeper/base/BaseOperation.java | 2 -- .../client/zookeeper/base/BaseProvider.java | 18 ++++++++++-------- .../client/zookeeper/base/BaseStrategy.java | 3 --- .../operation/CreateAllNeedOperation.java | 2 -- .../operation/CreateCurrentOperation.java | 2 -- .../zookeeper/provider/RetryProvider.java | 7 ------- .../zookeeper/section/ClientContext.java | 4 ---- .../{strategy => section}/StrategyType.java | 2 +- .../strategy/AllAsyncRetryStrategy.java | 1 - .../zookeeper/strategy/AsyncRetryStrategy.java | 1 - .../zookeeper/strategy/SyncRetryStrategy.java | 1 - .../zookeeper/strategy/UsualStrategy.java | 1 - .../zookeeper/strategy/VersionStrategy.java | 2 +- .../client/retry/AsyncRetryCenterTest.java | 3 +-- .../saaavsaaa/client/retry/TestCallable.java | 3 +-- .../retry/TestCreateCurrentOperation.java | 1 - .../zookeeper/AsyncRetryStrategyTest.java | 2 +- .../zookeeper/ContentionStrategyTest.java | 2 +- .../zookeeper/SyncRetryStrategyTest.java | 2 +- 27 files changed, 21 insertions(+), 58 deletions(-) rename src/main/java/com/saaavsaaa/client/zookeeper/{strategy => section}/StrategyType.java (73%) diff --git a/src/main/java/com/saaavsaaa/client/action/IAction.java b/src/main/java/com/saaavsaaa/client/action/IAction.java index cd918ce6acefe..50bfc2bb37861 100644 --- a/src/main/java/com/saaavsaaa/client/action/IAction.java +++ b/src/main/java/com/saaavsaaa/client/action/IAction.java @@ -1,6 +1,5 @@ package com.saaavsaaa.client.action; -import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; diff --git a/src/main/java/com/saaavsaaa/client/action/IClient.java b/src/main/java/com/saaavsaaa/client/action/IClient.java index 8c48ac8cf7ac4..d3e94626271f3 100644 --- a/src/main/java/com/saaavsaaa/client/action/IClient.java +++ b/src/main/java/com/saaavsaaa/client/action/IClient.java @@ -1,7 +1,7 @@ package com.saaavsaaa.client.action; import com.saaavsaaa.client.zookeeper.section.Listener; -import com.saaavsaaa.client.zookeeper.strategy.StrategyType; +import com.saaavsaaa.client.zookeeper.section.StrategyType; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import java.io.IOException; diff --git a/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java b/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java index f2a83f6e8eca2..aa36f466edf94 100644 --- a/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java +++ b/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java @@ -1,7 +1,5 @@ package com.saaavsaaa.client.action; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; - /** * Created by aaa */ diff --git a/src/main/java/com/saaavsaaa/client/action/IProvider.java b/src/main/java/com/saaavsaaa/client/action/IProvider.java index 1bb4058534435..6960cfd2c58c7 100644 --- a/src/main/java/com/saaavsaaa/client/action/IProvider.java +++ b/src/main/java/com/saaavsaaa/client/action/IProvider.java @@ -33,5 +33,5 @@ public interface IProvider { void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException; - void checkConnection(final KeeperException e) throws KeeperException, InterruptedException; + void checkConnection(final KeeperException e); } diff --git a/src/main/java/com/saaavsaaa/client/utility/Properties.java b/src/main/java/com/saaavsaaa/client/utility/Properties.java index b39024961fae2..c3b5fbae57e16 100644 --- a/src/main/java/com/saaavsaaa/client/utility/Properties.java +++ b/src/main/java/com/saaavsaaa/client/utility/Properties.java @@ -1,7 +1,5 @@ package com.saaavsaaa.client.utility; -import com.saaavsaaa.client.utility.StringUtil; - import java.util.ResourceBundle; /** diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java index c14e80b4754d2..7ec76b96a9d29 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java @@ -1,12 +1,8 @@ package com.saaavsaaa.client.zookeeper; -import com.saaavsaaa.client.action.IExecStrategy; -import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseContext; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; -import com.saaavsaaa.client.zookeeper.section.ClientContext; -import com.saaavsaaa.client.zookeeper.strategy.*; +import com.saaavsaaa.client.zookeeper.section.StrategyType; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.*; import org.slf4j.Logger; @@ -14,8 +10,6 @@ import java.io.IOException; import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; /** * Created by aaa diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index 36c90e2ac679a..71f75f916f27a 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -8,6 +8,7 @@ import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.section.ClientContext; import com.saaavsaaa.client.zookeeper.section.Listener; +import com.saaavsaaa.client.zookeeper.section.StrategyType; import com.saaavsaaa.client.zookeeper.section.WatcherCreator; import com.saaavsaaa.client.zookeeper.strategy.*; import org.apache.zookeeper.CreateMode; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java index 30355e7816313..b2991dd456816 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java @@ -1,8 +1,6 @@ package com.saaavsaaa.client.zookeeper.base; import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.retry.DelayRetryPolicy; -import com.saaavsaaa.client.zookeeper.section.ClientContext; import com.saaavsaaa.client.zookeeper.section.Listener; import java.io.IOException; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java index 22fdef858853d..1b2939c453a29 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java @@ -2,8 +2,6 @@ import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.DelayPolicyExecutor; -import com.saaavsaaa.client.zookeeper.section.ClientContext; -import com.saaavsaaa.client.zookeeper.section.Connection; import org.apache.zookeeper.KeeperException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java index 3e9888b4700ee..b11e71bb5c1c0 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java @@ -2,16 +2,18 @@ import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.election.LeaderElection; -import com.saaavsaaa.client.zookeeper.section.Connection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.zookeeper.section.Connection; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; -import org.apache.zookeeper.*; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; import org.apache.zookeeper.data.ACL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; import java.util.List; import java.util.Stack; @@ -127,13 +129,13 @@ public void createInTransaction(final String key, final String value, final Crea } @Override - public void checkConnection(final KeeperException e) throws KeeperException, InterruptedException { - if (Connection.needReset(e)){ - try { + public void checkConnection(final KeeperException e) { + try { + if (Connection.needReset(e)){ holder.reset(); - } catch (IOException e1) { - e1.printStackTrace(); } + } catch (Exception ee) { + logger.error("checkConnection error:{} KeeperException:{}", ee.getMessage(), e.getMessage(), ee); } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java index d3856ae75b1a1..89e615c551bb5 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java @@ -2,9 +2,6 @@ import com.saaavsaaa.client.action.IExecStrategy; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; -import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; /** diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java index 85ef79034c4c7..d1e29e939f99d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java @@ -1,8 +1,6 @@ package com.saaavsaaa.client.zookeeper.operation; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.zookeeper.section.ClientContext; -import com.saaavsaaa.client.zookeeper.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.CreateMode; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java index 86ef220d46519..bf0c5001bdd83 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java @@ -1,8 +1,6 @@ package com.saaavsaaa.client.zookeeper.operation; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.zookeeper.section.ClientContext; -import com.saaavsaaa.client.zookeeper.section.Connection; import com.saaavsaaa.client.zookeeper.base.BaseOperation; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java index 13780debac502..86db346973797 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java @@ -1,17 +1,10 @@ package com.saaavsaaa.client.zookeeper.provider; -import com.saaavsaaa.client.retry.AsyncRetryCenter; import com.saaavsaaa.client.retry.RetryCount; -import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.base.Holder; -import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; -import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentOperation; -import com.saaavsaaa.client.zookeeper.operation.UpdateOperation; -import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.Watcher; -import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.ACL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/section/ClientContext.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/ClientContext.java index c7271093b63c6..fd2dcb919baf2 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/section/ClientContext.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/section/ClientContext.java @@ -1,13 +1,9 @@ package com.saaavsaaa.client.zookeeper.section; -import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.DelayRetryPolicy; import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; import com.saaavsaaa.client.zookeeper.base.BaseContext; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - /** * Created by aaa */ diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/StrategyType.java similarity index 73% rename from src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java rename to src/main/java/com/saaavsaaa/client/zookeeper/section/StrategyType.java index 96bec8f43154b..9685b100a968c 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/StrategyType.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/section/StrategyType.java @@ -1,4 +1,4 @@ -package com.saaavsaaa.client.zookeeper.strategy; +package com.saaavsaaa.client.zookeeper.section; /** * Created by aaa diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java index d1fa0f64c9f79..eb9c2be447bab 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java @@ -3,7 +3,6 @@ import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.AsyncRetryCenter; import com.saaavsaaa.client.retry.DelayRetryPolicy; -import com.saaavsaaa.client.zookeeper.section.ClientContext; import com.saaavsaaa.client.zookeeper.operation.CreateAllNeedOperation; import com.saaavsaaa.client.zookeeper.operation.DeleteAllChildrenOperation; import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentBranchOperation; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java index dec5603cd694c..20db38a188ecf 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java @@ -3,7 +3,6 @@ import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.AsyncRetryCenter; import com.saaavsaaa.client.retry.DelayRetryPolicy; -import com.saaavsaaa.client.zookeeper.section.ClientContext; import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentOperation; import com.saaavsaaa.client.zookeeper.operation.UpdateOperation; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java index 912528855dc7c..b797df32c9dfe 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java @@ -3,7 +3,6 @@ import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.DelayRetryPolicy; import com.saaavsaaa.client.zookeeper.section.Callable; -import com.saaavsaaa.client.zookeeper.section.ClientContext; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.Watcher; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java index 78ca05d41a858..7814ed963f41e 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java @@ -4,7 +4,6 @@ import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.base.BaseStrategy; -import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/VersionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/VersionStrategy.java index 21baff39ca301..b1a0326fd44f3 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/VersionStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/VersionStrategy.java @@ -2,7 +2,7 @@ /** * Created by aaa - * node version + * todo node version */ public class VersionStrategy { } diff --git a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java index ffb41219f6ef0..ecf4f253b0845 100644 --- a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java @@ -2,14 +2,13 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.zookeeper.section.ClientContext; import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.ClientFactory; import com.saaavsaaa.client.zookeeper.TestSupport; import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.strategy.StrategyType; +import com.saaavsaaa.client.zookeeper.section.StrategyType; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.junit.After; diff --git a/src/test/java/com/saaavsaaa/client/retry/TestCallable.java b/src/test/java/com/saaavsaaa/client/retry/TestCallable.java index f6e2bcbc6be8a..bc688bf85106c 100644 --- a/src/test/java/com/saaavsaaa/client/retry/TestCallable.java +++ b/src/test/java/com/saaavsaaa/client/retry/TestCallable.java @@ -1,9 +1,8 @@ package com.saaavsaaa.client.retry; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.zookeeper.section.Callable; -import com.saaavsaaa.client.zookeeper.section.ClientContext; import com.saaavsaaa.client.zookeeper.base.BaseProvider; +import com.saaavsaaa.client.zookeeper.section.Callable; import org.apache.zookeeper.KeeperException; /** diff --git a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java index ec461f150cf19..001b0e515a247 100644 --- a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java +++ b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java @@ -1,7 +1,6 @@ package com.saaavsaaa.client.retry; import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.zookeeper.section.ClientContext; import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; import org.apache.zookeeper.CreateMode; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java index 0e1ba599ae770..50ecf72d9f78b 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java @@ -3,7 +3,7 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.strategy.StrategyType; +import com.saaavsaaa.client.zookeeper.section.StrategyType; import java.io.IOException; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java index 4afec2a531a0b..5bc0d419e2939 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java @@ -4,7 +4,7 @@ import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.strategy.StrategyType; +import com.saaavsaaa.client.zookeeper.section.StrategyType; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.junit.Test; diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java index 26419007e7cc3..17c3636c2a1ab 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java @@ -8,7 +8,7 @@ import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.section.Listener; -import com.saaavsaaa.client.zookeeper.strategy.StrategyType; +import com.saaavsaaa.client.zookeeper.section.StrategyType; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; From 001317b998419a2f694c8379b6e2db1ba66db30d Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 30 May 2018 14:45:09 +0800 Subject: [PATCH 133/351] retry count 3 --- .../client/retry/DelayPolicyExecutor.java | 2 +- .../client/zookeeper/base/BaseProvider.java | 14 ++++++++++++-- .../client/zookeeper/base/Holder.java | 2 +- .../zookeeper/strategy/UsualStrategy.java | 2 ++ .../zookeeper/SyncRetryStrategyTest.java | 18 ++++++++++-------- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/retry/DelayPolicyExecutor.java b/src/main/java/com/saaavsaaa/client/retry/DelayPolicyExecutor.java index e06a0109dba34..6195826122f17 100644 --- a/src/main/java/com/saaavsaaa/client/retry/DelayPolicyExecutor.java +++ b/src/main/java/com/saaavsaaa/client/retry/DelayPolicyExecutor.java @@ -24,7 +24,7 @@ public DelayPolicyExecutor(final DelayRetryPolicy delayRetryPolicy) { this.delayRetryPolicy = delayRetryPolicy; this.executeTick = System.currentTimeMillis(); this.random = new Random(); - next(); +// next(); } public boolean hasNext() { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java index b11e71bb5c1c0..837ca3f1f53c1 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java @@ -16,6 +16,7 @@ import java.util.List; import java.util.Stack; +import java.util.concurrent.atomic.AtomicInteger; /** * Created by aaa @@ -65,6 +66,13 @@ public List getChildren(final String key) throws KeeperException, Interr @Override public void create(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + holder.getZooKeeper().create(key, value.getBytes(Constants.UTF_8), authorities, createMode); + logger.debug("BaseProvider createCurrentOnly:{}", key); +// create(key, value, createMode, new AtomicInteger()); + } + + @Deprecated + private void create(final String key, final String value, final CreateMode createMode, final AtomicInteger count) throws KeeperException, InterruptedException { try { holder.getZooKeeper().create(key, value.getBytes(Constants.UTF_8), authorities, createMode); logger.debug("BaseProvider createCurrentOnly:{}", key); @@ -72,9 +80,11 @@ public void create(final String key, final String value, final CreateMode create logger.error("BaseProvider createCurrentOnly:{}", e.getMessage(), e); // I don't know whether it will happen or not, if root watcher don't update rootExist timely if (!exists(rootNode)){ - logger.info("BaseProvider createCurrentOnly root not exist"); + logger.info("BaseProvider createCurrentOnly root not exist:{}", count.get()); Thread.sleep(50); - this.create(key, value, createMode); + if (count.incrementAndGet() < 3) { + this.create(key, value, createMode, count); + } } } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java index 1e69c6beb6b4d..49769ff08a29d 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java @@ -49,7 +49,7 @@ public void process(WatchedEvent event) { } else if (Event.KeeperState.Expired == event.getState()){ try { logger.warn("startWatcher Event.KeeperState.Expired"); - start(); + reset(); } catch (Exception ee){ logger.error("event state Expired:{}", ee.getMessage(), ee); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java index 7814ed963f41e..3ea8385106071 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java @@ -139,6 +139,8 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr } catch (KeeperException.NotEmptyException ee){ logger.warn("deleteCurrentBranch exist children:{},e:{}", path, ee.getMessage()); return; + } catch (KeeperException.NoNodeException ee){ + logger.warn("deleteCurrentBranch NoNodeException:{},e:{}", superPath, ee.getMessage()); } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java index 17c3636c2a1ab..d0f9b074a8827 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java @@ -22,12 +22,11 @@ */ public class SyncRetryStrategyTest extends UsualClientTest{ private IProvider provider; - private IClient client; @Before public void start() throws IOException, InterruptedException { - client = createClient(); - provider = ((BaseClient)client).getStrategy().getProvider(); + testClient = createClient(); + provider = ((BaseClient)testClient).getStrategy().getProvider(); AsyncRetryCenter.INSTANCE.init(new DelayRetryPolicy(3, 3, 10)); AsyncRetryCenter.INSTANCE.start(); } @@ -43,17 +42,20 @@ protected IClient createClient() throws IOException, InterruptedException { @Test public void createChild() throws KeeperException, InterruptedException { String key = "a/b/bb"; + new UsualStrategy(provider).deleteCurrentBranch(key); TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { @Override public void test() throws KeeperException, InterruptedException { - new UsualStrategy(provider).createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); + testClient.useExecStrategy(StrategyType.USUAL); + testClient.createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); + testClient.useExecStrategy(StrategyType.SYNC_RETRY); } }; callable.exec(); - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) != null; + assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) != null; new UsualStrategy(provider).deleteCurrentBranch(key); - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) == null; + assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) == null; } @Test @@ -61,10 +63,10 @@ public void deleteBranch() throws KeeperException, InterruptedException { String keyB = "a/b/bb"; String value = "bbb11"; new UsualStrategy(provider).createAllNeedPath(keyB, value, CreateMode.PERSISTENT); - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; + assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; String keyC = "a/c/cc"; new UsualStrategy(provider).createAllNeedPath(keyC, "ccc11", CreateMode.PERSISTENT); - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; + assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { @Override From 547baeda4c0b64b60762650c05c6015866e5c0c9 Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 30 May 2018 15:59:32 +0800 Subject: [PATCH 134/351] retry test --- .../client/zookeeper/section/Callable.java | 4 +- .../zookeeper/SyncRetryStrategyTest.java | 157 +++++++++++++++--- 2 files changed, 135 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/section/Callable.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/Callable.java index 27749b4505109..20aafe71724c4 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/section/Callable.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/section/Callable.java @@ -26,7 +26,9 @@ public void setResult(T result) { this.result = result; } public T getResult() throws KeeperException, InterruptedException { - exec(); + if (result == null) { + exec(); + } return result; } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java index d0f9b074a8827..1a50714e5d4be 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java @@ -7,15 +7,20 @@ import com.saaavsaaa.client.retry.TestCallable; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.zookeeper.base.BaseClient; +import com.saaavsaaa.client.zookeeper.section.Callable; import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.zookeeper.section.StrategyType; import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.data.Stat; import org.junit.Before; import org.junit.Test; import java.io.IOException; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; /** * Created by aaa @@ -62,63 +67,165 @@ public void test() throws KeeperException, InterruptedException { public void deleteBranch() throws KeeperException, InterruptedException { String keyB = "a/b/bb"; String value = "bbb11"; - new UsualStrategy(provider).createAllNeedPath(keyB, value, CreateMode.PERSISTENT); + testClient.useExecStrategy(StrategyType.USUAL); + testClient.createAllNeedPath(keyB, value, CreateMode.PERSISTENT); + testClient.useExecStrategy(StrategyType.SYNC_RETRY); + assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; String keyC = "a/c/cc"; new UsualStrategy(provider).createAllNeedPath(keyC, "ccc11", CreateMode.PERSISTENT); assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; + TestCallable callable = getDeleteBranch(keyC); + callable.exec(); + + assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) == null; + assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), false) != null; + + callable = getDeleteBranch(keyB); + callable.exec(); + + assert getZooKeeper(testClient).exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; + testClient.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); + assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; + + callable.exec(); + + assert getZooKeeper(testClient).exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; + } + + private TestCallable getDeleteBranch(final String key){ TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { @Override public void test() throws KeeperException, InterruptedException { - new UsualStrategy(provider).deleteCurrentBranch(keyC); + testClient.useExecStrategy(StrategyType.USUAL); + testClient.deleteCurrentBranch(key); + testClient.useExecStrategy(StrategyType.SYNC_RETRY); } }; - callable.exec(); + return callable; } @Test public void isExisted() throws KeeperException, InterruptedException { - super.isExisted(testClient); + String key = "a/b/bb"; + testClient.useExecStrategy(StrategyType.USUAL); + testClient.createAllNeedPath(key, "", CreateMode.PERSISTENT); + testClient.useExecStrategy(StrategyType.SYNC_RETRY); + + TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { + @Override + public void test() throws KeeperException, InterruptedException { + setResult(provider.exists(provider.getRealPath(key))); + } + }; + System.out.println(callable.getResult()); + assert callable.getResult().equals(true); + + testClient.useExecStrategy(StrategyType.USUAL); + testClient.deleteCurrentBranch(key); + testClient.useExecStrategy(StrategyType.SYNC_RETRY); } @Test public void get() throws KeeperException, InterruptedException { - super.get(testClient); + String key = "a/b"; + testClient.useExecStrategy(StrategyType.USUAL); + testClient.createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); + testClient.useExecStrategy(StrategyType.SYNC_RETRY); + + TestCallable callable = getData("a"); + assert callable.getResult().equals(""); + callable = getData(key); + assert callable.getResult().equals("bbb11"); + + testClient.useExecStrategy(StrategyType.USUAL); + testClient.deleteCurrentBranch(key); + testClient.useExecStrategy(StrategyType.SYNC_RETRY); } - @Test - public void asynGet() throws KeeperException, InterruptedException { - super.asynGet(testClient); + private TestCallable getData(final String key){ + TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { + @Override + public void test() throws KeeperException, InterruptedException { + setResult(new String(provider.getData(provider.getRealPath(key)))); + } + }; + return callable; } @Test public void getChildrenKeys() throws KeeperException, InterruptedException { - super.getChildrenKeys(testClient); - } + String key = "a/b"; + String current = "a"; + + testClient.useExecStrategy(StrategyType.USUAL); + testClient.createAllNeedPath(key, "", CreateMode.PERSISTENT); + testClient.createAllNeedPath("a/c", "", CreateMode.PERSISTENT); + testClient.useExecStrategy(StrategyType.SYNC_RETRY); - @Test - public void persist() throws KeeperException, InterruptedException { - super.persist(testClient); + TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { + @Override + public void test() throws KeeperException, InterruptedException { + setResult(provider.getChildren(provider.getRealPath(current))); + } + }; + List result = (List) callable.getResult(); + Collections.sort(result, new Comparator() { + public int compare(final String o1, final String o2) { + return o2.compareTo(o1); + } + }); + assert result.get(0).equals("c"); + assert result.get(1).equals("b"); + + testClient.useExecStrategy(StrategyType.USUAL); + testClient.deleteCurrentBranch(key); + testClient.useExecStrategy(StrategyType.SYNC_RETRY); } @Test - public void persistEphemeral() throws KeeperException, InterruptedException { - super.persistEphemeral(testClient); - } + public void update() throws KeeperException, InterruptedException { + String key = "a"; + String value = "aa"; + String newValue = "aaa"; + testClient.deleteCurrentBranch(key); + testClient.createAllNeedPath(key, value, CreateMode.PERSISTENT); + String data = testClient.getDataString(key); + System.out.println(data); + assert data.equals(value); + + TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { + @Override + public void test() throws KeeperException, InterruptedException { + provider.update(provider.getRealPath(key), newValue); + } + }; + callable.exec(); - @Test - public void delAllChildren() throws KeeperException, InterruptedException { - super.delAllChildren(testClient); + assert testClient.getDataString(key).equals(newValue); + testClient.deleteCurrentBranch(key); } @Test - public void watch() throws KeeperException, InterruptedException { - super.watch(testClient); - } + public void delAllChildren() throws KeeperException, InterruptedException { + String key = "a/b/bb"; + testClient.createAllNeedPath(key, "bb", CreateMode.PERSISTENT); + key = "a/c/cc"; + testClient.createAllNeedPath(key, "cc", CreateMode.PERSISTENT); + System.out.println(getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), null).getNumChildren()); // nearest children count + assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) != null; - @Test - public void close() throws Exception { - super.close(testClient); + TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { + @Override + public void test() throws KeeperException, InterruptedException { + new UsualStrategy(provider).deleteAllChildren("a"); + } + }; + callable.exec(); + + assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) == null; + assert getZooKeeper(testClient).exists("/" + TestSupport.ROOT, false) != null; + super.deleteRoot(testClient); } } From 1764b1815481ad3a8b8e5e8aed3c74c4648c8767 Mon Sep 17 00:00:00 2001 From: saaav Date: Wed, 30 May 2018 18:11:19 +0800 Subject: [PATCH 135/351] dependent --- build.gradle | 4 +++- .../com/saaavsaaa/client/zookeeper/base/BaseProvider.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 4254cd5abb824..9c8acc203d716 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ group 'com.saaavsaaa' //version '1.0-SNAPSHOT' -version '1.2' +version '1.6' apply plugin: 'idea' apply plugin: 'java' @@ -33,6 +33,8 @@ dependencies { compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.4.11' testCompile group: 'junit', name: 'junit', version: '4.11' + // https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper + testCompile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.5.3-beta' } uploadArchives { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java index 837ca3f1f53c1..7126c4e64e2dc 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java @@ -145,7 +145,7 @@ public void checkConnection(final KeeperException e) { holder.reset(); } } catch (Exception ee) { - logger.error("checkConnection error:{} KeeperException:{}", ee.getMessage(), e.getMessage(), ee); + logger.error("checkConnection input KeeperException:{}", e.getMessage(), ee); } } From 9017c899f0ad5db7bfedafa1ea06bb3acbc6a530 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 31 May 2018 16:24:20 +0800 Subject: [PATCH 136/351] adapt ss --- .../com/saaavsaaa/client/action/IClient.java | 2 + .../com/saaavsaaa/client/cache/PathTree.java | 68 ++++++++++--------- .../client/zookeeper/CacheClient.java | 2 +- .../client/zookeeper/ClientFactory.java | 10 ++- .../client/zookeeper/base/BaseClient.java | 22 ++++++ .../client/zookeeper/base/Holder.java | 7 ++ .../zookeeper/transaction/ZKTransaction.java | 20 ++++-- .../zookeeper/AsyncRetryStrategyTest.java | 12 +++- .../zookeeper/SyncRetryStrategyTest.java | 2 - 9 files changed, 101 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/action/IClient.java b/src/main/java/com/saaavsaaa/client/action/IClient.java index d3e94626271f3..c9d1cfd81cd80 100644 --- a/src/main/java/com/saaavsaaa/client/action/IClient.java +++ b/src/main/java/com/saaavsaaa/client/action/IClient.java @@ -5,12 +5,14 @@ import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import java.io.IOException; +import java.util.concurrent.TimeUnit; /** * Created by aaa */ public interface IClient extends IAction, IGroupAction{ void start() throws IOException, InterruptedException; + boolean start(int wait, TimeUnit units) throws IOException, InterruptedException; void close(); void registerWatch(String key, Listener listener); void unregisterWatch(String key); diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index 81c637ebd8705..43062f4692729 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -142,40 +142,41 @@ public void stopRefresh(){ logger.debug("stopRefresh"); } - public void watch(Listener listener){ - if (closed){ - return; - } - if (listener == null){ - listener = new Listener(rootNode.get().getKey()) { - @Override - public void process(WatchedEvent event) { - String path = event.getPath(); - logger.debug("PathTree Watch event:{}", event.toString()); - switch (event.getType()) { - case NodeCreated: - case NodeDataChanged: - case NodeChildrenChanged: { - try { - String value = Constants.NOTHING_VALUE; - if (!path.equals(getRootNode().getKey())){ - value = provider.getDataString(path); - } - put(path, value); - } catch (Exception e) { - logger.error("PathTree put error : " + e.getMessage()); + public void watch(){ + watch(new Listener(rootNode.get().getKey()) { + @Override + public void process(WatchedEvent event) { + String path = event.getPath(); + logger.debug("PathTree Watch event:{}", event.toString()); + switch (event.getType()) { + case NodeCreated: + case NodeDataChanged: + case NodeChildrenChanged: { + try { + String value = Constants.NOTHING_VALUE; + if (!path.equals(getRootNode().getKey())){ + value = provider.getDataString(path); } - break; - } - case NodeDeleted: { - delete(path); - break; + put(path, value); + } catch (Exception e) { + logger.error("PathTree put error : " + e.getMessage()); } - default: - break; + break; + } + case NodeDeleted: { + delete(path); + break; } + default: + break; } - }; + } + }); + } + + public void watch(final Listener listener){ + if (closed){ + return; } final String key = listener.getKey(); logger.debug("PathTree Watch:{}", key); @@ -285,6 +286,7 @@ public void put(final String path, final String value) { } public void delete(String path) { + logger.debug("PathTree begin delete:{}", path); final ReentrantLock lock = this.lock; lock.lock(); if (closed){ @@ -292,10 +294,10 @@ public void delete(String path) { } try { PathUtils.validatePath(path); - String prxpath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); - PathNode node = get(prxpath); +// String prxpath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); + PathNode node = get(path); node.getChildren().remove(path); - logger.debug("PathTree delete:{}", path); + logger.debug("PathTree end delete:{}", path); } finally { lock.unlock(); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index ef7ed6d27314d..79fa31e8eef49 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -48,7 +48,7 @@ void useCacheStrategy(CacheStrategy cacheStrategy) throws KeeperException, Inter switch (cacheStrategy){ case WATCH:{ pathTree = new PathTree(rootNode, this); - pathTree.watch(null); + pathTree.watch(); return; } case ALL:{ diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index a31159a891eb8..a7d882783e9f1 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -17,11 +17,16 @@ public class ClientFactory extends BaseClientFactory { // private static final String CLIENT_EXCLUSIVE_NODE = "ZKC"; private static final Logger logger = LoggerFactory.getLogger(ClientFactory.class); + private DelayRetryPolicy delayRetryPolicy; public ClientFactory(){} public ClientFactory newClient(final String servers, final int sessionTimeoutMilliseconds) { - this.context = new ClientContext(servers, sessionTimeoutMilliseconds); + int wait = sessionTimeoutMilliseconds; + if (sessionTimeoutMilliseconds == 0){ + wait = 60 * 1000; + } + this.context = new ClientContext(servers, wait); client = new UsualClient(context); logger.debug("new usual client"); return this; @@ -74,12 +79,13 @@ public ClientFactory authorization(final String scheme, final byte[] auth){ } public ClientFactory setRetryPolicy(final DelayRetryPolicy delayRetryPolicy){ - ((ClientContext)context).setDelayRetryPolicy(delayRetryPolicy); + this.delayRetryPolicy = delayRetryPolicy; return this; } @Override public IClient start() throws IOException, InterruptedException { + ((ClientContext)context).setDelayRetryPolicy(delayRetryPolicy); ((ClientContext)context).setClientFactory(this); return super.start(); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index 71f75f916f27a..581ec4e0936c1 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; /** * Created by aaa @@ -51,6 +52,27 @@ public void start() throws IOException, InterruptedException { holder.start(); } + public boolean start(int wait, TimeUnit units) throws IOException, InterruptedException { + holder = new Holder(getContext()); + holder.start(); + return blockUntilConnected(wait, units); + } + + //copy curator + private synchronized boolean blockUntilConnected(int wait, TimeUnit units) throws InterruptedException { + long startTime = System.currentTimeMillis(); + long maxWaitTimeMs = units != null ? TimeUnit.MILLISECONDS.convert(wait, units) : 0; + + for (;;){ + long waitTime = maxWaitTimeMs - (System.currentTimeMillis() - startTime); + if ( waitTime <= 0 ) + { + return holder.isConnected(); + } + wait(waitTime); + } + } + @Override public void close() { holder.close(); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java index 49769ff08a29d..d3862434740e5 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java @@ -22,6 +22,7 @@ public class Holder { protected ZooKeeper zooKeeper; protected final BaseContext context; + private boolean connected = false; Holder(final BaseContext context){ this.context = context; @@ -44,9 +45,11 @@ public void process(WatchedEvent event) { if(Event.EventType.None == event.getType()){ if(Event.KeeperState.SyncConnected == event.getState()){ CONNECTED.countDown(); + connected = true; logger.debug("BaseClient startWatcher SyncConnected"); return; } else if (Event.KeeperState.Expired == event.getState()){ + connected = false; try { logger.warn("startWatcher Event.KeeperState.Expired"); reset(); @@ -91,4 +94,8 @@ public void close() { public ZooKeeper getZooKeeper() { return zooKeeper; } + + public boolean isConnected() { + return connected; + } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java index f5b3684ee3dc7..f4f8b73b935eb 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.zookeeper.transaction; import com.saaavsaaa.client.utility.PathUtil; +import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.base.Holder; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; @@ -23,25 +24,34 @@ public ZKTransaction(final String root, final Holder holder) { logger.debug("ZKTransaction root:{}", rootNode); } - public ZKTransaction create(String path, byte[] data, List acl, CreateMode createMode) { + public ZKTransaction create(final String path, final byte[] data, final List acl, final CreateMode createMode) { this.transaction.create(PathUtil.getRealPath(rootNode, path), data, acl, createMode); logger.debug("wait create:{},data:{},acl:{},createMode:{}", new Object[]{path, data, acl, createMode}); return this; } - public ZKTransaction delete(String path, int version) { + public ZKTransaction delete(final String path){ + return delete(path, Constants.VERSION); + } + public ZKTransaction delete(final String path, final int version) { this.transaction.delete(PathUtil.getRealPath(rootNode, path), version); logger.debug("wait delete:{}", path); return this; } - public ZKTransaction check(String path, int version) { + public ZKTransaction check(final String path){ + return check(path, Constants.VERSION); + } + public ZKTransaction check(final String path, final int version) { this.transaction.check(PathUtil.getRealPath(rootNode, path), version); logger.debug("wait check:{}", path); return this; } - public ZKTransaction setData(String path, byte[] data, int version) { + public ZKTransaction setData(final String path, final byte[] data){ + return setData(path, data, Constants.VERSION); + } + public ZKTransaction setData(final String path, final byte[] data, final int version) { this.transaction.setData(PathUtil.getRealPath(rootNode, path), data, version); logger.debug("wait setData:{},data:{}", path, data); return this; @@ -52,7 +62,7 @@ public List commit() throws InterruptedException, KeeperException { return this.transaction.commit(); } - public void commit(AsyncCallback.MultiCallback cb, Object ctx) { + public void commit(final AsyncCallback.MultiCallback cb, final Object ctx) { this.transaction.commit(cb, ctx); logger.debug("ZKTransaction commit ctx:{}", ctx); } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java index 50ecf72d9f78b..9a734d4068dbd 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java @@ -1,9 +1,12 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.action.IClient; +import com.saaavsaaa.client.retry.AsyncRetryCenter; +import com.saaavsaaa.client.retry.DelayRetryPolicy; import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.section.StrategyType; +import org.junit.Before; import java.io.IOException; @@ -11,11 +14,18 @@ * Created by aaa */ public class AsyncRetryStrategyTest extends UsualClientTest{ + @Before + public void start() throws IOException, InterruptedException { + super.start(); + AsyncRetryCenter.INSTANCE.init(new DelayRetryPolicy(3, 3, 10)); + AsyncRetryCenter.INSTANCE.start(); + } + @Override protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - ((BaseClient)client).useExecStrategy(StrategyType.ASYNC_RETRY); + client.useExecStrategy(StrategyType.ASYNC_RETRY); return client; } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java index 1a50714e5d4be..a525bb09692d0 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java @@ -32,8 +32,6 @@ public class SyncRetryStrategyTest extends UsualClientTest{ public void start() throws IOException, InterruptedException { testClient = createClient(); provider = ((BaseClient)testClient).getStrategy().getProvider(); - AsyncRetryCenter.INSTANCE.init(new DelayRetryPolicy(3, 3, 10)); - AsyncRetryCenter.INSTANCE.start(); } protected IClient createClient() throws IOException, InterruptedException { From 058ef844b3ebf94a27e219964f3c32f37bc441ee Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 31 May 2018 16:48:05 +0800 Subject: [PATCH 137/351] check -> reset --- .../com/saaavsaaa/client/action/IProvider.java | 3 +-- .../client/zookeeper/base/BaseOperation.java | 9 +++++++-- .../client/zookeeper/base/BaseProvider.java | 9 +++------ .../saaavsaaa/client/zookeeper/base/Holder.java | 1 + .../client/zookeeper/section/Callable.java | 6 +++++- .../client/retry/AsyncRetryCenterTest.java | 15 ++++++++++++--- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/action/IProvider.java b/src/main/java/com/saaavsaaa/client/action/IProvider.java index 6960cfd2c58c7..b200987e15c41 100644 --- a/src/main/java/com/saaavsaaa/client/action/IProvider.java +++ b/src/main/java/com/saaavsaaa/client/action/IProvider.java @@ -7,7 +7,6 @@ import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.Watcher; -import java.io.IOException; import java.util.List; import java.util.Stack; @@ -33,5 +32,5 @@ public interface IProvider { void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException; - void checkConnection(final KeeperException e); + void resetConnection(); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java index 1b2939c453a29..71818b754bfc5 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java @@ -2,6 +2,7 @@ import com.saaavsaaa.client.action.IProvider; import com.saaavsaaa.client.retry.DelayPolicyExecutor; +import com.saaavsaaa.client.zookeeper.section.Connection; import org.apache.zookeeper.KeeperException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,8 +53,12 @@ public boolean executeOperation() throws KeeperException, InterruptedException { execute(); result = true; } catch (KeeperException ee) { - provider.checkConnection(ee); - result = false; + if (Connection.needReset(ee)){ + provider.resetConnection(); + result = false; + } else { + throw ee; + } } if (!result && delayPolicyExecutor.hasNext()){ delayPolicyExecutor.next(); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java index 7126c4e64e2dc..b7c0c992a6863 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java @@ -4,7 +4,6 @@ import com.saaavsaaa.client.election.LeaderElection; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.section.Connection; import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; @@ -139,13 +138,11 @@ public void createInTransaction(final String key, final String value, final Crea } @Override - public void checkConnection(final KeeperException e) { + public void resetConnection() { try { - if (Connection.needReset(e)){ - holder.reset(); - } + holder.reset(); } catch (Exception ee) { - logger.error("checkConnection input KeeperException:{}", e.getMessage(), ee); + logger.error("resetConnection Exception:{}", ee.getMessage(), ee); } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java index d3862434740e5..314ba0341bbf9 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java @@ -84,6 +84,7 @@ public void reset() throws IOException, InterruptedException { public void close() { try { zooKeeper.close(); + connected = false; logger.debug("zk closed"); this.context.close(); } catch (Exception ee){ diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/section/Callable.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/Callable.java index 20aafe71724c4..4380804d2fdf2 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/section/Callable.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/section/Callable.java @@ -38,7 +38,11 @@ public void exec() throws KeeperException, InterruptedException { } catch (KeeperException e) { logger.warn("exec KeeperException:{}", e.getMessage()); delayPolicyExecutor.next(); - provider.checkConnection(e); + if (Connection.needReset(e)){ + provider.resetConnection(); + } else { + throw e; + } execDelay(); } catch (InterruptedException e) { throw e; diff --git a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java index ecf4f253b0845..8efe37dc0ecdd 100644 --- a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java @@ -2,6 +2,7 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.action.IProvider; +import com.saaavsaaa.client.zookeeper.base.BaseProvider; import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.utility.PathUtil; import com.saaavsaaa.client.utility.constant.Constants; @@ -42,7 +43,13 @@ protected IClient createClient() throws IOException, InterruptedException { @After public void stop(){ - + client.close(); + } + + @Test + public void close() throws Exception { + client.close(); + assert !((BaseProvider)provider).getHolder().isConnected(); } @Test @@ -58,8 +65,10 @@ public void create() throws InterruptedException, KeeperException { String path = PathUtil.getRealPath(TestSupport.ROOT, key); assert provider.exists(path); // assert client.checkExists(path); - provider.delete(path); - provider.delete(provider.getRealPath(TestSupport.ROOT)); + client.useExecStrategy(StrategyType.USUAL); + client.deleteAllChildren(path); + client.deleteCurrentBranch(path); + client.useExecStrategy(StrategyType.ASYNC_RETRY); assert !provider.exists(path); } } From beab7a7c69a46711b3147e6d6abbcae7fa62f771 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 31 May 2018 17:10:26 +0800 Subject: [PATCH 138/351] adapt ss --- .../client/zookeeper/ClientFactory.java | 7 ++++++- .../client/zookeeper/base/BaseClient.java | 4 ++-- .../client/zookeeper/base/BaseClientFactory.java | 5 ++++- .../client/retry/AsyncRetryCenterTest.java | 3 ++- .../client/zookeeper/AsyncRetryStrategyTest.java | 3 ++- .../client/zookeeper/CacheClientTest.java | 3 ++- .../client/zookeeper/CacheWathClientTest.java | 3 ++- .../saaavsaaa/client/zookeeper/ClientsTest.java | 16 ++++++++++++++-- .../client/zookeeper/ContentionStrategyTest.java | 3 ++- .../client/zookeeper/SyncRetryStrategyTest.java | 3 ++- .../client/zookeeper/UsualClientTest.java | 3 ++- .../client/zookeeper/UsualWatchClientTest.java | 3 ++- .../client/zookeeper/base/BaseClientTest.java | 6 +++++- 13 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index a7d882783e9f1..03d277f06b889 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -6,10 +6,13 @@ import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.ACL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.util.List; /** * Created by aaa @@ -69,12 +72,14 @@ public ClientFactory setNamespace(String namespace) { return this; } - public ClientFactory authorization(final String scheme, final byte[] auth){ + public ClientFactory authorization(final String scheme, final byte[] auth, final List authorities){ if (scheme == null || scheme.trim().length() == 0) { + this.authorities = ZooDefs.Ids.OPEN_ACL_UNSAFE; return this; } this.scheme = scheme; this.auth = auth; + this.authorities = authorities; return this; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index 581ec4e0936c1..1f8e2d56c8349 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -185,10 +185,10 @@ void setRootNode(final String rootNode) { this.rootNode = rootNode; } - void setAuthorities(final String scheme, final byte[] auth) { + void setAuthorities(final String scheme, final byte[] auth, final List authorities) { context.scheme = scheme; context.auth = auth; - this.authorities = ZooDefs.Ids.CREATOR_ALL_ACL; + this.authorities = authorities; } public BaseContext getContext(){ diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java index b2991dd456816..74bd4f532d049 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java @@ -2,8 +2,10 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.zookeeper.section.Listener; +import org.apache.zookeeper.data.ACL; import java.io.IOException; +import java.util.List; /** * Created by aaa @@ -14,11 +16,12 @@ public abstract class BaseClientFactory { protected String namespace; protected String scheme; protected byte[] auth; + protected List authorities; protected BaseContext context; public IClient start() throws IOException, InterruptedException { client.setRootNode(namespace); - client.setAuthorities(scheme , auth); + client.setAuthorities(scheme , auth, authorities); client.start(); if (globalListener != null) { client.registerWatch(globalListener); diff --git a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java index 8efe37dc0ecdd..da0cca4bc114a 100644 --- a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java @@ -12,6 +12,7 @@ import com.saaavsaaa.client.zookeeper.section.StrategyType; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -36,7 +37,7 @@ public void start() throws IOException, InterruptedException { protected IClient createClient() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); Listener listener = TestSupport.buildListener(); - IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); client.useExecStrategy(StrategyType.ASYNC_RETRY); return client; } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java index 9a734d4068dbd..8d6f966b15e39 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java @@ -6,6 +6,7 @@ import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.section.StrategyType; +import org.apache.zookeeper.ZooDefs; import org.junit.Before; import java.io.IOException; @@ -24,7 +25,7 @@ public void start() throws IOException, InterruptedException { @Override protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); - IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); client.useExecStrategy(StrategyType.ASYNC_RETRY); return client; } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java index 4d6c23aca3148..ea17944865b3c 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java @@ -1,6 +1,7 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.action.IClient; +import org.apache.zookeeper.ZooDefs; import java.io.IOException; @@ -11,6 +12,6 @@ public class CacheClientTest extends UsualClientTest { @Override protected IClient createClient(ClientFactory creator) throws IOException, InterruptedException { - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java index d760ec01c8bdc..8d0df3ca25729 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java @@ -2,6 +2,7 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.zookeeper.section.Listener; +import org.apache.zookeeper.ZooDefs; import java.io.IOException; @@ -12,6 +13,6 @@ public class CacheWathClientTest extends UsualClientTest { @Override protected IClient createClient(ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java index c37425a17d9eb..c4bcfca10da2e 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java @@ -1,9 +1,12 @@ package com.saaavsaaa.client.zookeeper; import com.saaavsaaa.client.action.IClient; +import com.saaavsaaa.client.utility.constant.Constants; +import com.saaavsaaa.client.zookeeper.base.BaseClient; import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.zookeeper.base.BaseClientTest; import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.ZooKeeper; import org.junit.Test; @@ -42,12 +45,12 @@ protected IClient createClient(ClientFactory creator) throws IOException, Interr } private IClient newClient(ClientFactory creator) throws IOException, InterruptedException { - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); } protected IClient newWatchClient(ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); } @Override @@ -143,4 +146,13 @@ public void close() throws Exception { assert zk.getState() == ZooKeeper.States.CLOSED; } } + + @Test + public void deleteRoot() throws KeeperException, InterruptedException { + for (IClient client : clients) { + createRootOnly(client); + deleteRoot(client); + assert getZooKeeper(client).exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; + } + } } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java index 5bc0d419e2939..9bc0b550b439d 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java @@ -7,6 +7,7 @@ import com.saaavsaaa.client.zookeeper.section.StrategyType; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs; import org.junit.Test; import java.io.IOException; @@ -19,7 +20,7 @@ public class ContentionStrategyTest extends UsualClientTest { @Override protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); - IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); ((BaseClient)client).useExecStrategy(StrategyType.CONTEND); return client; } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java index a525bb09692d0..23f3aaefbb5fe 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java @@ -13,6 +13,7 @@ import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.data.Stat; import org.junit.Before; import org.junit.Test; @@ -37,7 +38,7 @@ public void start() throws IOException, InterruptedException { protected IClient createClient() throws IOException, InterruptedException { ClientFactory creator = new ClientFactory(); Listener listener = TestSupport.buildListener(); - IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); client.useExecStrategy(StrategyType.SYNC_RETRY); return client; } diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java index 82f61a9438efa..95ecccc323e02 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java @@ -3,6 +3,7 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.zookeeper.base.BaseClientTest; import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs; import org.junit.Test; import java.io.IOException; @@ -14,7 +15,7 @@ public class UsualClientTest extends BaseClientTest { @Override protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); } @Test diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java index f4ad7bcb1911c..9d709ccb35f1c 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java @@ -2,6 +2,7 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.zookeeper.section.Listener; +import org.apache.zookeeper.ZooDefs; import java.io.IOException; @@ -13,6 +14,6 @@ public class UsualWatchClientTest extends UsualClientTest { @Override protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { Listener listener = TestSupport.buildListener(); - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); + return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); } } \ No newline at end of file diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java index 7a7806c03e27d..67458b52b150a 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java @@ -47,13 +47,17 @@ public void stop() throws InterruptedException { testClient = null; } - @Ignore @Test public void deleteRoot() throws KeeperException, InterruptedException { + ((BaseClient)testClient).createNamespace(); deleteRoot(testClient); assert getZooKeeper(testClient).exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; } + protected void createRootOnly(IClient client) throws KeeperException, InterruptedException { + ((BaseClient)client).createNamespace(); + } + protected void deleteRoot(IClient client) throws KeeperException, InterruptedException { ((BaseClient)client).deleteNamespace(); } From 6bf0da608d2a568d214d8311914800ad96657df4 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 31 May 2018 17:12:10 +0800 Subject: [PATCH 139/351] t --- README.md | 42 +++++------------------------------------- 1 file changed, 5 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index e3c5909b4cf83..b37dac1eb5c67 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,7 @@ # ZookeeperClient ------ - - ClientFactory creator = new ClientFactory(); - IClient testClient = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes()).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - - Reference unit test - ------ - - Client: - CacheClient - UsualClient - - ExecStrategy: - public enum StrategyType { - USUAL, - CONTENTION - } - - String getDataString(final String key) throws KeeperException, InterruptedException; - byte[] getData(final String key) throws KeeperException, InterruptedException; - void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException; - boolean checkExists(final String key) throws KeeperException, InterruptedException; - boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException; - List getChildren(final String key) throws KeeperException, InterruptedException; - void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException; - void update(final String key, final String value) throws KeeperException, InterruptedException; - void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException; - void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException; - ZKTransaction transaction(); - void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException ; - void deleteAllChildren(final String key) throws KeeperException, InterruptedException ; - - /* - * delete the current node with force and delete the super node whose only child node is current node recursively - */ - void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException ; +UsualStrategy +SyncRetryStrategy +ContentionStrategy +AsyncRetryStrategy +AllAsyncRetryStrategy \ No newline at end of file From 84fea0f7053b787d0889f7692a34f96996370064 Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 31 May 2018 18:25:13 +0800 Subject: [PATCH 140/351] create --- .../saaavsaaa/client/utility/constant/Constants.java | 1 + .../com/saaavsaaa/client/zookeeper/ClientFactory.java | 11 +++-------- .../client/zookeeper/base/BaseClientFactory.java | 5 +++++ .../client/zookeeper/strategy/UsualStrategy.java | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/utility/constant/Constants.java b/src/main/java/com/saaavsaaa/client/utility/constant/Constants.java index 23d80e6ebb67b..9a50d60835641 100644 --- a/src/main/java/com/saaavsaaa/client/utility/constant/Constants.java +++ b/src/main/java/com/saaavsaaa/client/utility/constant/Constants.java @@ -7,6 +7,7 @@ */ public final class Constants { public static final int VERSION = -1; + public static final int WAIT = 60 * 1000; public static final byte[] NOTHING_DATA = new byte[0]; public static final String NOTHING_VALUE = ""; public static final Charset UTF_8 = Charset.forName("UTF-8"); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java index 03d277f06b889..a3739c3c0796e 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java @@ -2,11 +2,10 @@ import com.saaavsaaa.client.action.IClient; import com.saaavsaaa.client.retry.DelayRetryPolicy; -import com.saaavsaaa.client.zookeeper.section.ClientContext; -import com.saaavsaaa.client.zookeeper.section.Listener; import com.saaavsaaa.client.utility.constant.Constants; import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; -import org.apache.zookeeper.ZooDefs; +import com.saaavsaaa.client.zookeeper.section.ClientContext; +import com.saaavsaaa.client.zookeeper.section.Listener; import org.apache.zookeeper.data.ACL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,7 +26,7 @@ public ClientFactory(){} public ClientFactory newClient(final String servers, final int sessionTimeoutMilliseconds) { int wait = sessionTimeoutMilliseconds; if (sessionTimeoutMilliseconds == 0){ - wait = 60 * 1000; + wait = Constants.WAIT; } this.context = new ClientContext(servers, wait); client = new UsualClient(context); @@ -73,10 +72,6 @@ public ClientFactory setNamespace(String namespace) { } public ClientFactory authorization(final String scheme, final byte[] auth, final List authorities){ - if (scheme == null || scheme.trim().length() == 0) { - this.authorities = ZooDefs.Ids.OPEN_ACL_UNSAFE; - return this; - } this.scheme = scheme; this.auth = auth; this.authorities = authorities; diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java index 74bd4f532d049..bc0060d3720b5 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java @@ -7,6 +7,8 @@ import java.io.IOException; import java.util.List; +import static org.apache.zookeeper.ZooDefs.Ids.OPEN_ACL_UNSAFE; + /** * Created by aaa */ @@ -21,6 +23,9 @@ public abstract class BaseClientFactory { public IClient start() throws IOException, InterruptedException { client.setRootNode(namespace); + if(scheme == null) { + authorities = OPEN_ACL_UNSAFE; + } client.setAuthorities(scheme , auth, authorities); client.start(); if (globalListener != null) { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java index 3ea8385106071..ee93a2b1d6ade 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java @@ -80,7 +80,7 @@ public void createAllNeedPath(final String key, final String value, final Create if (i == nodes.size() - 1){ this.createCurrentOnly(nodes.get(i), value, createMode); } else { - this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, createMode); + this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, CreateMode.PERSISTENT); } logger.debug("node not exist and create:", nodes.get(i)); } catch (KeeperException.NodeExistsException ee){ From e5748d3bfa48e2e2d6c5b8c4fd767a9df1e77131 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 1 Jun 2018 18:24:24 +0800 Subject: [PATCH 141/351] adapt ss --- .../java/com/saaavsaaa/client/action/IClient.java | 2 +- .../java/com/saaavsaaa/client/cache/PathTree.java | 7 ++++--- .../saaavsaaa/client/election/LeaderElection.java | 4 ++-- .../com/saaavsaaa/client/retry/RetryThread.java | 2 +- .../saaavsaaa/client/zookeeper/CacheClient.java | 2 +- .../client/zookeeper/base/BaseClient.java | 15 +++++---------- 6 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/action/IClient.java b/src/main/java/com/saaavsaaa/client/action/IClient.java index c9d1cfd81cd80..9b2d93f532774 100644 --- a/src/main/java/com/saaavsaaa/client/action/IClient.java +++ b/src/main/java/com/saaavsaaa/client/action/IClient.java @@ -12,7 +12,7 @@ */ public interface IClient extends IAction, IGroupAction{ void start() throws IOException, InterruptedException; - boolean start(int wait, TimeUnit units) throws IOException, InterruptedException; + boolean blockUntilConnected(int wait, TimeUnit units) throws InterruptedException; void close(); void registerWatch(String key, Listener listener); void unregisterWatch(String key); diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java index 43062f4692729..8c6391c76399d 100644 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ b/src/main/java/com/saaavsaaa/client/cache/PathTree.java @@ -44,7 +44,7 @@ public PathTree(final String root, final IClient client) { this.provider = ((BaseClient)client).getStrategy().getProvider(); } - public void loading() throws KeeperException, InterruptedException { + public void load() throws KeeperException, InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); if (closed){ @@ -62,6 +62,7 @@ public void loading() throws KeeperException, InterruptedException { rootNode.set(newRoot); this.setStatus(PathStatus.RELEASE); +// watch(); logger.debug("loading release:{}", Status); } else { logger.info("loading but cache status not release"); @@ -70,7 +71,7 @@ public void loading() throws KeeperException, InterruptedException { } catch (InterruptedException e) { logger.error("loading sleep error:{}", e.getMessage(), e); } - loading(); + load(); } } finally { lock.unlock(); @@ -117,7 +118,7 @@ public void run() { logger.debug("cacheService run:{}", getStatus()); if (PathStatus.RELEASE == getStatus()) { try { - loading(); + load(); } catch (Exception e) { logger.error(e.getMessage(), e); } diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java index 67ca4261e7515..d520a34b5106d 100644 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java @@ -43,8 +43,8 @@ private boolean contend(final String node, final IProvider provider, final Liste */ public void executeContention(final String nodeBeCompete, final IProvider provider) throws KeeperException, InterruptedException { boolean canBegin; - String realNode = provider.getRealPath(nodeBeCompete); - String contendNode = PathUtil.getRealPath(realNode, Constants.CHANGING_KEY); + final String realNode = provider.getRealPath(nodeBeCompete); + final String contendNode = PathUtil.getRealPath(realNode, Constants.CHANGING_KEY); canBegin = this.contend(contendNode, provider, new Listener(contendNode) { @Override public void process(WatchedEvent event) { diff --git a/src/main/java/com/saaavsaaa/client/retry/RetryThread.java b/src/main/java/com/saaavsaaa/client/retry/RetryThread.java index e7e513a31ef10..57fa8b73617f3 100644 --- a/src/main/java/com/saaavsaaa/client/retry/RetryThread.java +++ b/src/main/java/com/saaavsaaa/client/retry/RetryThread.java @@ -39,7 +39,7 @@ public Thread newThread(Runnable r) { public void run(){ logger.debug("RetryThread start"); for (;;) { - BaseOperation operation; + final BaseOperation operation; try { operation = queue.take(); logger.debug("take operation:{}", operation.toString()); diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java index 79fa31e8eef49..f44c07b2859e8 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java @@ -69,7 +69,7 @@ public PathTree loadPathTree() throws KeeperException, InterruptedException { public PathTree loadPathTree(final String treeRoot) throws KeeperException, InterruptedException { PathTree tree = new PathTree(treeRoot, this); logger.debug("load path tree:{}", treeRoot); - tree.loading(); + tree.load(); return tree; } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index 1f8e2d56c8349..fbec49af5d225 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -52,25 +52,20 @@ public void start() throws IOException, InterruptedException { holder.start(); } - public boolean start(int wait, TimeUnit units) throws IOException, InterruptedException { - holder = new Holder(getContext()); - holder.start(); - return blockUntilConnected(wait, units); - } - //copy curator - private synchronized boolean blockUntilConnected(int wait, TimeUnit units) throws InterruptedException { + @Override + public synchronized boolean blockUntilConnected(int wait, TimeUnit units) throws InterruptedException { long startTime = System.currentTimeMillis(); long maxWaitTimeMs = units != null ? TimeUnit.MILLISECONDS.convert(wait, units) : 0; - for (;;){ + while (!holder.isConnected()){ long waitTime = maxWaitTimeMs - (System.currentTimeMillis() - startTime); - if ( waitTime <= 0 ) - { + if (waitTime <= 0){ return holder.isConnected(); } wait(waitTime); } + return true; } @Override From 4c2ed074353a5064aca7725edfa1623ed0b9d8e8 Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 4 Jun 2018 14:23:45 +0800 Subject: [PATCH 142/351] exist --- .../com/saaavsaaa/client/zookeeper/base/BaseClient.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index fbec49af5d225..55a9b43f39a38 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -153,14 +153,16 @@ private void createNamespace(final byte[] date) throws KeeperException, Interrup return; } try { - holder.zooKeeper.create(rootNode, date, authorities, CreateMode.PERSISTENT); + if (null == holder.getZooKeeper().exists(rootNode, false)){ + holder.zooKeeper.create(rootNode, date, authorities, CreateMode.PERSISTENT); + } + rootExist = true; logger.debug("creating root:{}", rootNode); } catch (KeeperException.NodeExistsException ee){ logger.warn("root create:{}", ee.getMessage()); rootExist = true; return; } - rootExist = true; holder.zooKeeper.exists(rootNode, WatcherCreator.deleteWatcher(new Listener(rootNode) { @Override public void process(WatchedEvent event) { From 030c7f53c731e20078c446f19ce79e078198c9e5 Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 4 Jun 2018 15:13:09 +0800 Subject: [PATCH 143/351] wait --- .../com/saaavsaaa/client/zookeeper/base/BaseClient.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java index 55a9b43f39a38..913d022f691e3 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java @@ -52,18 +52,16 @@ public void start() throws IOException, InterruptedException { holder.start(); } - //copy curator @Override public synchronized boolean blockUntilConnected(int wait, TimeUnit units) throws InterruptedException { - long startTime = System.currentTimeMillis(); - long maxWaitTimeMs = units != null ? TimeUnit.MILLISECONDS.convert(wait, units) : 0; + long maxWait = units != null ? TimeUnit.MILLISECONDS.convert(wait, units) : 0; while (!holder.isConnected()){ - long waitTime = maxWaitTimeMs - (System.currentTimeMillis() - startTime); + long waitTime = maxWait - 30; if (waitTime <= 0){ return holder.isConnected(); } - wait(waitTime); + wait(30); } return true; } From 06f4ba95a030d0b8638691995aaaf3477d1a1697 Mon Sep 17 00:00:00 2001 From: linjiaqi Date: Thu, 7 Jun 2018 16:00:24 +0800 Subject: [PATCH 144/351] shardingsphere netty backend feature --- .../proxy/backend/ShardingProxyClient.java | 102 ++++---- .../backend/common/BackendHandlerFactory.java | 14 +- .../common/CommandResponsePacketsHandler.java | 3 + .../common/NettyChannelPoolHandler.java | 53 ++++ .../common/SQLExecuteBackendHandler.java | 45 ++-- .../common/SQLPacketsBackendHandler.java | 228 ++++++++++++++---- .../proxy/backend/constant/AuthType.java | 50 ++++ .../proxy/backend/mysql/ColumnDefine.java | 12 - .../backend/mysql/MySQLBackendHandler.java | 201 ++++++++------- .../proxy/backend/mysql/MySQLQueryResult.java | 141 ++++++++--- .../netty/ClientHandlerInitializer.java | 15 +- .../proxy/config/DataScourceConfig.java | 40 +++ .../frontend/mysql/MySQLFrontendHandler.java | 4 +- .../transport/common/codec/PacketCodec.java | 4 +- .../command/text/query/ComQueryPacket.java | 20 +- .../proxy/util/MySQLResultCache.java | 20 +- .../proxy/util/SynchronizedFuture.java | 98 +------- 17 files changed, 659 insertions(+), 391 deletions(-) create mode 100644 sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/NettyChannelPoolHandler.java create mode 100644 sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/constant/AuthType.java delete mode 100644 sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/ColumnDefine.java create mode 100644 sharding-proxy/src/main/java/io/shardingsphere/proxy/config/DataScourceConfig.java diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java index 76a59cc34e35f..ea6faa829f70b 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java @@ -17,8 +17,16 @@ package io.shardingsphere.proxy.backend; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + import com.google.common.collect.Maps; import com.zaxxer.hikari.HikariConfig; + import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.Channel; @@ -29,26 +37,21 @@ import io.netty.channel.epoll.EpollSocketChannel; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.pool.AbstractChannelPoolMap; -import io.netty.channel.pool.ChannelPoolHandler; import io.netty.channel.pool.ChannelPoolMap; import io.netty.channel.pool.FixedChannelPool; import io.netty.channel.pool.SimpleChannelPool; import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.logging.LogLevel; -import io.netty.handler.logging.LoggingHandler; -import io.shardingsphere.proxy.backend.netty.ClientHandlerInitializer; +import io.shardingsphere.proxy.backend.common.NettyChannelPoolHandler; +import io.shardingsphere.proxy.config.DataScourceConfig; import io.shardingsphere.proxy.config.RuleRegistry; import lombok.Getter; import lombok.extern.slf4j.Slf4j; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Map; - /** * Sharding-Proxy Client. * * @author wangkai + * @author linjiaqi */ @Slf4j public final class ShardingProxyClient { @@ -56,6 +59,10 @@ public final class ShardingProxyClient { private static final int WORKER_MAX_THREADS = Runtime.getRuntime().availableProcessors(); + private static final int MAX_CONNECTIONS = 10; + + private static final int CONNECT_TIMEOUT = 30; + private EventLoopGroup workerGroup; @Getter @@ -66,8 +73,8 @@ public final class ShardingProxyClient { /** * Start Sharding-Proxy. * - * @throws InterruptedException interrupted exception * @throws MalformedURLException url is illegal. + * @throws InterruptedException interrupted exception */ public void start() throws MalformedURLException, InterruptedException { Map dataSourceConfigurationMap = RuleRegistry.getInstance().getDataSourceConfigurationMap(); @@ -86,14 +93,7 @@ public void start() throws MalformedURLException, InterruptedException { } else { groupsNio(bootstrap); } - poolMap = new AbstractChannelPoolMap() { - @Override - protected SimpleChannelPool newPool(String datasourceName) { - DataScourceConfig dataScourceConfig = dataScourceConfigMap.get(datasourceName); - //TODO maxConnection should be set. - return new FixedChannelPool(bootstrap.remoteAddress(dataScourceConfig.ip, dataScourceConfig.port), new NettyChannelPoolHandler(dataScourceConfig), 10); - } - }; + initPoolMap(bootstrap); } /** @@ -126,6 +126,32 @@ private void groupsNio(final Bootstrap bootstrap) { .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); } + private void initPoolMap(final Bootstrap bootstrap) throws InterruptedException { + poolMap = new AbstractChannelPoolMap() { + @Override + protected SimpleChannelPool newPool(final String datasourceName) { + DataScourceConfig dataScourceConfig = dataScourceConfigMap.get(datasourceName); + //TODO maxConnection should be set. + return new FixedChannelPool(bootstrap.remoteAddress(dataScourceConfig.getIp(), dataScourceConfig.getPort()), new NettyChannelPoolHandler(dataScourceConfig), MAX_CONNECTIONS); + } + }; + + for (String each : dataScourceConfigMap.keySet()) { + SimpleChannelPool pool = poolMap.get(each); + Channel[] channels = new Channel[MAX_CONNECTIONS]; + for (int i = 0; i < MAX_CONNECTIONS; i++) { + try { + channels[i] = pool.acquire().get(CONNECT_TIMEOUT, TimeUnit.SECONDS); + } catch (ExecutionException | TimeoutException e) { + log.error(e.getMessage(), e); + } + } + for (int i = 0; i < MAX_CONNECTIONS; i++) { + pool.release(channels[i]); + } + } + } + /** * Get instance of sharding-proxy client. * @@ -134,46 +160,4 @@ private void groupsNio(final Bootstrap bootstrap) { public static ShardingProxyClient getInstance() { return INSTANCE; } - - class DataScourceConfig { - private final String ip; - private final int port; - private final String database; - private final String username; - private final String password; - - public DataScourceConfig(String ip, int port, String database, String username, String password) { - this.ip = ip; - this.port = port; - this.database = database; - this.username = username; - this.password = password; - } - } - - class NettyChannelPoolHandler implements ChannelPoolHandler { - private final DataScourceConfig dataScourceConfig; - - public NettyChannelPoolHandler(final DataScourceConfig dataScourceConfig) { - this.dataScourceConfig = dataScourceConfig; - } - - @Override - public void channelReleased(Channel channel) throws Exception { - log.info("channelReleased. Channel ID: {}" + channel.id().asShortText()); - } - - @Override - public void channelAcquired(Channel channel) throws Exception { - log.info("channelAcquired. Channel ID: {}" + channel.id().asShortText()); - } - - @Override - public void channelCreated(Channel channel) throws Exception { - channel.pipeline() - .addLast(new LoggingHandler(LogLevel.INFO)) - .addLast(new ClientHandlerInitializer(dataScourceConfig.ip, dataScourceConfig.port, dataScourceConfig.database, dataScourceConfig.username, dataScourceConfig.password)); - log.info("channelCreated. Channel ID: {}" + channel.id().asShortText()); - } - } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java index e81cb302c4303..b38c85290c839 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java @@ -19,30 +19,26 @@ import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.proxy.backend.mysql.MySQLBackendHandler; +import io.shardingsphere.proxy.config.DataScourceConfig; /** * Backend handler factory. * * @author wangkai + * @author linjiaqi */ public final class BackendHandlerFactory { - /** * Create bakcend handler instance. * * @param databaseType database type - * @param ip database ip - * @param port database port - * @param database database name - * @param username access database username - * @param password access database password + * @param dataScourceConfig dataScourceConfig * @return backend handler instance */ - public static CommandResponsePacketsHandler createBackendHandlerInstance(final DatabaseType databaseType, final String ip, final int port, final String database, final String username, final - String password) { + public static CommandResponsePacketsHandler createBackendHandlerInstance(final DatabaseType databaseType, final DataScourceConfig dataScourceConfig) { switch (databaseType) { case MySQL: - return new MySQLBackendHandler(ip, port, database, username, password); + return new MySQLBackendHandler(dataScourceConfig); default: throw new UnsupportedOperationException(String.format("Cannot support database type '%s'", databaseType)); } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java index 38edadcc42fb6..e51f9511ecf9c 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java @@ -25,10 +25,13 @@ * SQL execute backend handler. * * @author wangkai + * @author linjiaqi */ public abstract class CommandResponsePacketsHandler extends ChannelInboundHandlerAdapter { protected abstract void auth(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload); + protected abstract void endOfFilePacket(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload); + protected abstract void genericResponsePacket(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload); protected abstract void executeCommandResponsePackets(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/NettyChannelPoolHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/NettyChannelPoolHandler.java new file mode 100644 index 0000000000000..b427d28e98109 --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/NettyChannelPoolHandler.java @@ -0,0 +1,53 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.backend.common; + +import io.netty.channel.Channel; +import io.netty.channel.pool.ChannelPoolHandler; +import io.shardingsphere.proxy.backend.netty.ClientHandlerInitializer; +import io.shardingsphere.proxy.config.DataScourceConfig; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +/** + * NettyChannelPoolHandler. + * + * @author wangkai + * @author linjiaqi + */ +@RequiredArgsConstructor +@Slf4j +public class NettyChannelPoolHandler implements ChannelPoolHandler { + private final DataScourceConfig dataScourceConfig; + + @Override + public void channelReleased(final Channel channel) throws Exception { + log.info("channelReleased. Channel ID: {}" + channel.id().asShortText()); + } + + @Override + public void channelAcquired(final Channel channel) throws Exception { + log.info("channelAcquired. Channel ID: {}" + channel.id().asShortText()); + } + + @Override + public void channelCreated(final Channel channel) throws Exception { + log.info("channelCreated. Channel ID: {}" + channel.id().asShortText()); + channel.pipeline().addLast(new ClientHandlerInitializer(dataScourceConfig)); + } +} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java index d1320140eb815..c3c5d8ae93899 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java @@ -62,6 +62,7 @@ * @author zhangliang * @author panjuan * @author wangkai + * @author linjiaqi */ public class SQLExecuteBackendHandler implements BackendHandler { @@ -102,17 +103,27 @@ public SQLExecuteBackendHandler(final String sql, final DatabaseType databaseTyp @Override public CommandResponsePackets execute() { - try { - if (RuleRegistry.getInstance().isOnlyMasterSlave()) { - return executeForMasterSlave(); - } else { - return executeForSharding(); - } - } catch (final Exception ex) { - return new CommandResponsePackets(new ErrPacket(1, 0, "", "", ex.getMessage())); + if (RuleRegistry.getInstance().isOnlyMasterSlave()) { + return executeForMasterSlave(); + } else { + return executeForSharding(); } } - + + protected CommandResponsePackets execute(final SQLStatement sqlStatement, final String dataSourceName, final String sql) { + switch (sqlStatement.getType()) { + case DQL: + case DAL: + return executeQuery(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql); + case DML: + case DDL: + return RuleRegistry.getInstance().isOnlyMasterSlave() ? executeUpdate(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql) + : executeUpdate(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql, sqlStatement); + default: + return executeCommon(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql); + } + } + protected CommandResponsePackets executeForMasterSlave() { MasterSlaveRouter masterSlaveRouter = new MasterSlaveRouter(RuleRegistry.getInstance().getMasterSlaveRule()); SQLStatement sqlStatement = new SQLJudgeEngine(sql).judge(); @@ -137,21 +148,7 @@ protected CommandResponsePackets executeForSharding() { ProxyShardingRefreshHandler.build(routeResult).execute(); return result; } - - protected CommandResponsePackets execute(final SQLStatement sqlStatement, final String dataSourceName, final String sql) { - switch (sqlStatement.getType()) { - case DQL: - case DAL: - return executeQuery(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql); - case DML: - case DDL: - return RuleRegistry.getInstance().isOnlyMasterSlave() ? executeUpdate(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql) - : executeUpdate(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql, sqlStatement); - default: - return executeCommon(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql); - } - } - + private CommandResponsePackets executeQuery(final DataSource dataSource, final String sql) { try { Connection connection = dataSource.getConnection(); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java index a6b360c511393..88d5bd282dc3a 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java @@ -17,106 +17,244 @@ package io.shardingsphere.proxy.backend.common; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import com.google.common.collect.Lists; + import io.netty.channel.Channel; import io.netty.channel.pool.SimpleChannelPool; import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.constant.SQLType; +import io.shardingsphere.core.merger.MergeEngineFactory; +import io.shardingsphere.core.merger.MergedResult; +import io.shardingsphere.core.merger.QueryResult; +import io.shardingsphere.core.parsing.SQLJudgeEngine; import io.shardingsphere.core.parsing.parser.sql.SQLStatement; import io.shardingsphere.core.routing.SQLExecutionUnit; import io.shardingsphere.core.routing.SQLRouteResult; import io.shardingsphere.core.routing.StatementRoutingEngine; +import io.shardingsphere.core.routing.router.masterslave.MasterSlaveRouter; import io.shardingsphere.proxy.backend.ShardingProxyClient; +import io.shardingsphere.proxy.backend.mysql.MySQLQueryResult; import io.shardingsphere.proxy.config.RuleRegistry; +import io.shardingsphere.proxy.metadata.ProxyShardingRefreshHandler; +import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.ComQueryPacket; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket; +import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket; +import io.shardingsphere.proxy.transport.mysql.packet.generic.ErrPacket; import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket; import io.shardingsphere.proxy.util.MySQLResultCache; import io.shardingsphere.proxy.util.SynchronizedFuture; - -import java.util.List; -import java.util.concurrent.TimeUnit; +import lombok.extern.slf4j.Slf4j; /** * SQL packets backend handler. * * @author wangkai + * @author linjiaqi */ -public final class SQLPacketsBackendHandler extends SQLExecuteBackendHandler { - private SynchronizedFuture synchronizedFuture; +@Slf4j +public final class SQLPacketsBackendHandler implements BackendHandler { + private static final int CONNECT_TIMEOUT = 30; + + private SynchronizedFuture> synchronizedFuture; private final CommandPacket commandPacket; + private final String sql; + private int connectionId; + private final DatabaseType databaseType; + + private final boolean showSQL; + + private MergedResult mergedResult; + + private int currentSequenceId; + + private int columnCount; + + private boolean isMerged; + + private boolean hasMoreResultValueFlag; + public SQLPacketsBackendHandler(final CommandPacket commandPacket, final String sql, final int connectionId, final DatabaseType databaseType, final boolean showSQL) { - super(sql, databaseType, showSQL); this.commandPacket = commandPacket; + this.sql = sql; this.connectionId = connectionId; + this.databaseType = databaseType; + this.showSQL = showSQL; + isMerged = false; + hasMoreResultValueFlag = true; } @Override + public CommandResponsePackets execute() { + if (RuleRegistry.getInstance().isOnlyMasterSlave()) { + return executeForMasterSlave(); + } else { + return executeForSharding(); + } + } + + protected CommandResponsePackets executeForMasterSlave() { + MasterSlaveRouter masterSlaveRouter = new MasterSlaveRouter(RuleRegistry.getInstance().getMasterSlaveRule()); + SQLStatement sqlStatement = new SQLJudgeEngine(sql).judge(); + String dataSourceName = masterSlaveRouter.route(sqlStatement.getType()).iterator().next(); + + synchronizedFuture = new SynchronizedFuture<>(1); + MySQLResultCache.getInstance().putFuture(connectionId, synchronizedFuture); + executeCommand(dataSourceName, connectionId, commandPacket); + //TODO timeout should be set. + List queryResults = synchronizedFuture.get(CONNECT_TIMEOUT, TimeUnit.SECONDS); + MySQLResultCache.getInstance().deleteFuture(connectionId); + + List packets = new LinkedList<>(); + for (QueryResult each : queryResults) { + packets.add(((MySQLQueryResult) each).getCommandResponsePackets()); + } + return merge(sqlStatement, packets, queryResults); + } + protected CommandResponsePackets executeForSharding() { - StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), getDatabaseType(), - isShowSQL()); - SQLRouteResult routeResult = routingEngine.route(getSql()); + StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), databaseType, showSQL); + SQLRouteResult routeResult = routingEngine.route(sql); if (routeResult.getExecutionUnits().isEmpty()) { return new CommandResponsePackets(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); } + synchronizedFuture = new SynchronizedFuture<>(routeResult.getExecutionUnits().size()); - MySQLResultCache.getInstance().put(connectionId, synchronizedFuture); + MySQLResultCache.getInstance().putFuture(connectionId, synchronizedFuture); for (SQLExecutionUnit each : routeResult.getExecutionUnits()) { - execute(routeResult.getSqlStatement(), each.getDataSource(), each.getSqlUnit().getSql()); + if (commandPacket instanceof ComQueryPacket) { + ((ComQueryPacket) commandPacket).setSql(each.getSqlUnit().getSql()); + } + executeCommand(each.getDataSource(), connectionId, commandPacket); } //TODO timeout should be set. - CommandResponsePackets result = synchronizedFuture.get(30, TimeUnit.SECONDS); - MySQLResultCache.getInstance().delete(connectionId); + List queryResults = synchronizedFuture.get(CONNECT_TIMEOUT, TimeUnit.SECONDS); + MySQLResultCache.getInstance().deleteFuture(connectionId); + + List packets = Lists.newArrayListWithCapacity(queryResults.size()); + for (QueryResult each : queryResults) { + MySQLQueryResult queryResult = (MySQLQueryResult) each; + if (currentSequenceId == 0) { + currentSequenceId = queryResult.getCurrentSequenceId(); + } + if (columnCount == 0) { + columnCount = queryResult.getColumnCount(); + } + packets.add(queryResult.getCommandResponsePackets()); + } + + CommandResponsePackets result = merge(routeResult.getSqlStatement(), packets, queryResults); + ProxyShardingRefreshHandler.build(routeResult).execute(); return result; } - - @Override - protected CommandResponsePackets executeForMasterSlave() { - throw new UnsupportedOperationException(); + + protected CommandResponsePackets merge(final SQLStatement sqlStatement, final List packets, final List queryResults) { + CommandResponsePackets headPackets = new CommandResponsePackets(); + for (CommandResponsePackets each : packets) { + headPackets.addPacket(each.getHeadPacket()); + } + for (DatabaseProtocolPacket each : headPackets.getDatabaseProtocolPackets()) { + if (each instanceof ErrPacket) { + return new CommandResponsePackets(each); + } + } + if (SQLType.DML == sqlStatement.getType()) { + return mergeDML(headPackets); + } + if (SQLType.DQL == sqlStatement.getType() || SQLType.DAL == sqlStatement.getType()) { + return mergeDQLorDAL(sqlStatement, packets, queryResults); + } + return packets.get(0); } - @Override - protected CommandResponsePackets execute(final SQLStatement sqlStatement, final String dataSourceName, final String sql) { + private void executeCommand(final String dataSourceName, final int connectionId, final CommandPacket commandPacket) { SimpleChannelPool pool = null; Channel channel = null; - try{ + try { pool = ShardingProxyClient.getInstance().getPoolMap().get(dataSourceName); - channel = pool.acquire().getNow(); - //MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); - switch (sqlStatement.getType()) { - case DQL: - executeQuery(channel, sql); - break; - case DML: - case DDL: - executeUpdate(channel, sql, sqlStatement); - break; - default: - executeCommon(channel, sql); - } - }finally { - if(null != pool && null != channel){ + channel = pool.acquire().get(CONNECT_TIMEOUT, TimeUnit.SECONDS); + MySQLResultCache.getInstance().putConnection(channel.id().asShortText(), connectionId); + channel.writeAndFlush(commandPacket); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + log.error(e.getMessage(), e); + } finally { + if (null != pool && null != channel) { pool.release(channel); } } - return null; + } + + private CommandResponsePackets mergeDML(final CommandResponsePackets firstPackets) { + int affectedRows = 0; + long lastInsertId = 0; + for (DatabaseProtocolPacket each : firstPackets.getDatabaseProtocolPackets()) { + if (each instanceof OKPacket) { + OKPacket okPacket = (OKPacket) each; + affectedRows += okPacket.getAffectedRows(); + lastInsertId = okPacket.getLastInsertId(); + } + } + return new CommandResponsePackets(new OKPacket(1, affectedRows, lastInsertId, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); } - private void executeQuery(final Channel channel, final String sql) { - MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); - channel.writeAndFlush(commandPacket); + private CommandResponsePackets mergeDQLorDAL(final SQLStatement sqlStatement, final List packets, final List queryResults) { + try { + mergedResult = MergeEngineFactory.newInstance(RuleRegistry.getInstance().getShardingRule(), queryResults, sqlStatement).merge(); + isMerged = true; + } catch (final SQLException ex) { + return new CommandResponsePackets(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage())); + } + return packets.get(0); } - private void executeUpdate(final Channel channel, final String sql, final SQLStatement sqlStatement) { - MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); - channel.writeAndFlush(commandPacket); + /** + * Has more Result value. + * + * @return has more result value + * @throws SQLException sql exception + */ + public boolean hasMoreResultValue() throws SQLException { + if (!isMerged || !hasMoreResultValueFlag) { + return false; + } + if (!mergedResult.next()) { + hasMoreResultValueFlag = false; + } + return true; } - private void executeCommon(final Channel channel, final String sql) { - MySQLResultCache.getInstance().putConnectionMap(channel.id().asShortText(), connectionId); - channel.writeAndFlush(commandPacket); + /** + * Get result value. + * + * @return database protocol packet + */ + public DatabaseProtocolPacket getResultValue() { + if (!hasMoreResultValueFlag) { + return new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()); + } + try { + List data = new ArrayList<>(columnCount); + for (int i = 1; i <= columnCount; i++) { + data.add(mergedResult.getValue(i, Object.class)); + } + return new TextResultSetRowPacket(++currentSequenceId, data); + } catch (final SQLException ex) { + return new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage()); + } } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/constant/AuthType.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/constant/AuthType.java new file mode 100644 index 0000000000000..3d64dbe50c6f0 --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/constant/AuthType.java @@ -0,0 +1,50 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.backend.constant; + +/** + * Auth Type. + * + * @author linjiaqi + */ +public enum AuthType { + + /** + * Database UN_AUTH. + * + */ + UN_AUTH, + + /** + * Database AUTHING. + * + */ + AUTHING, + + /** + * Database AUTH_SUCCESS. + * + */ + AUTH_SUCCESS, + + /** + * Database AUTH_FAILED. + * + */ + AUTH_FAILED +} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/ColumnDefine.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/ColumnDefine.java deleted file mode 100644 index 91a0badad9133..0000000000000 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/ColumnDefine.java +++ /dev/null @@ -1,12 +0,0 @@ -package io.shardingsphere.proxy.backend.mysql; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -@RequiredArgsConstructor -public class ColumnDefine { - @Getter - private int index; - @Getter - private String name; -} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java index 724be192dc2e3..e05cb5f36a840 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java @@ -21,21 +21,19 @@ import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.BlockingQueue; import com.google.common.primitives.Bytes; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.shardingsphere.proxy.backend.common.CommandResponsePacketsHandler; +import io.shardingsphere.proxy.backend.constant.AuthType; +import io.shardingsphere.proxy.config.DataScourceConfig; import io.shardingsphere.proxy.transport.mysql.constant.CapabilityFlag; import io.shardingsphere.proxy.transport.mysql.constant.ColumnType; import io.shardingsphere.proxy.transport.mysql.constant.ServerInfo; -import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; -import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet; -import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.FieldCountPacket; import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket; import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket; import io.shardingsphere.proxy.transport.mysql.packet.generic.ErrPacket; @@ -49,72 +47,75 @@ * Backend handler. * * @author wangkai + * @author linjiaqi */ @Slf4j @RequiredArgsConstructor public class MySQLBackendHandler extends CommandResponsePacketsHandler { + private final DataScourceConfig dataScourceConfig; - private boolean authorized; + private AuthType authType = AuthType.UN_AUTH; - private final String ip; - - private final int port; - - private final String database; - - private final String username; - - private final String password; - - private List columnDefines; - - private BlockingQueue resultSet; + private MySQLQueryResult mysqlQueryResult; @Override public void channelRead(final ChannelHandlerContext context, final Object message) { MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload((ByteBuf) message); - //int packetSize = mysqlPacketPayload.readInt3(); int sequenceId = mysqlPacketPayload.readInt1(); + mysqlPacketPayload.getByteBuf().markReaderIndex(); int header = mysqlPacketPayload.readInt1() & 0xFF; - if (OKPacket.HEADER == header || ErrPacket.HEADER == header || EofPacket.HEADER == header) { - genericResponsePacket(context, sequenceId, header, mysqlPacketPayload); - } else if (!authorized) { + + if (AuthType.UN_AUTH == authType) { auth(context, sequenceId, header, mysqlPacketPayload); + } else if (AuthType.AUTHING == authType) { + genericResponsePacket(context, sequenceId, header, mysqlPacketPayload); + } else if (AuthType.AUTH_FAILED == authType) { + log.error("mysql auth failed, cannot handle channel read message"); } else { - executeCommandResponsePackets(context, sequenceId, header, mysqlPacketPayload); + if (EofPacket.HEADER == header) { + endOfFilePacket(context, sequenceId, header, mysqlPacketPayload); + } else if (OKPacket.HEADER == header || ErrPacket.HEADER == header) { + genericResponsePacket(context, sequenceId, header, mysqlPacketPayload); + } else { + executeCommandResponsePackets(context, sequenceId, header, mysqlPacketPayload); + } } } @Override protected void auth(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { - int protocolVersion = header; - String serverVersion = mysqlPacketPayload.readStringNul(); - int connectionId = mysqlPacketPayload.readInt4(); - MySQLResultCache.getInstance().putConnectionMap(context.channel().id().asShortText(), connectionId); - byte[] authPluginDataPart1 = mysqlPacketPayload.readStringNul().getBytes(); - int capabilityFlagsLower = mysqlPacketPayload.readInt2(); - int charset = mysqlPacketPayload.readInt1(); - int statusFlag = mysqlPacketPayload.readInt2(); - int capabilityFlagsUpper = mysqlPacketPayload.readInt2(); - int authPluginDataLength = mysqlPacketPayload.readInt1(); - mysqlPacketPayload.skipReserved(10); - byte[] authPluginDataPart2 = mysqlPacketPayload.readStringNul().getBytes(); - byte[] authPluginData = Bytes.concat(authPluginDataPart1, authPluginDataPart2); - //byte[] authResponse = byteXOR(SHA1(password.getBytes()), SHA1(Bytes.concat(authPluginData, SHA1(SHA1(password.getBytes()))))); - byte[] authResponse = securePasswordAuthentication(password.getBytes(), authPluginData); - //TODO maxSizePactet(16MB) should be set. - HandshakeResponse41Packet handshakeResponse41Packet = new HandshakeResponse41Packet(sequenceId + 1, CapabilityFlag.calculateHandshakeCapabilityFlagsLower(), 16777215, ServerInfo.CHARSET, - username, authResponse, database); - context.writeAndFlush(handshakeResponse41Packet); + try { + int protocolVersion = header; + String serverVersion = mysqlPacketPayload.readStringNul(); + int connectionId = mysqlPacketPayload.readInt4(); + MySQLResultCache.getInstance().putConnection(context.channel().id().asShortText(), connectionId); + byte[] authPluginDataPart1 = mysqlPacketPayload.readStringNul().getBytes(); + int capabilityFlagsLower = mysqlPacketPayload.readInt2(); + int charset = mysqlPacketPayload.readInt1(); + int statusFlag = mysqlPacketPayload.readInt2(); + int capabilityFlagsUpper = mysqlPacketPayload.readInt2(); + int authPluginDataLength = mysqlPacketPayload.readInt1(); + mysqlPacketPayload.skipReserved(10); + byte[] authPluginDataPart2 = mysqlPacketPayload.readStringNul().getBytes(); + byte[] authPluginData = Bytes.concat(authPluginDataPart1, authPluginDataPart2); + byte[] authResponse = securePasswordAuthentication(dataScourceConfig.getPassword().getBytes(), authPluginData); + //TODO maxSizePactet(16MB) should be set. + HandshakeResponse41Packet handshakeResponse41Packet = new HandshakeResponse41Packet(sequenceId + 1, CapabilityFlag.calculateHandshakeCapabilityFlagsLower(), 16777215, ServerInfo.CHARSET, + dataScourceConfig.getUsername(), authResponse, dataScourceConfig.getDatabase()); + context.writeAndFlush(handshakeResponse41Packet); + } finally { + authType = AuthType.AUTHING; + mysqlPacketPayload.getByteBuf().release(); + } } @Override protected void genericResponsePacket(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { - CommandResponsePackets commandResponsePackets = null; + mysqlQueryResult = new MySQLQueryResult(); switch (header) { case OKPacket.HEADER: - if (!authorized) { - authorized = true; + if (authType == AuthType.AUTHING) { + authType = AuthType.AUTH_SUCCESS; } long affectedRows = mysqlPacketPayload.readIntLenenc(); long lastInsertId = mysqlPacketPayload.readIntLenenc(); @@ -122,74 +123,88 @@ protected void genericResponsePacket(final ChannelHandlerContext context, final int warnings = mysqlPacketPayload.readInt2(); String info = mysqlPacketPayload.readStringEOF(); log.debug("OKPacket[affectedRows={},lastInsertId={},statusFlags={},warnings={},info={}]", affectedRows, lastInsertId, statusFlags, warnings, info); - commandResponsePackets = new CommandResponsePackets(new OKPacket(sequenceId + 1, affectedRows, lastInsertId, statusFlags, warnings, info)); + mysqlQueryResult.setGenericResponse(new OKPacket(sequenceId, affectedRows, lastInsertId, statusFlags, warnings, info)); break; case ErrPacket.HEADER: + if (authType == AuthType.AUTHING) { + authType = AuthType.AUTH_FAILED; + } int errorCode = mysqlPacketPayload.readInt2(); String sqlStateMarker = mysqlPacketPayload.readStringFix(1); String sqlState = mysqlPacketPayload.readStringFix(5); String errorMessage = mysqlPacketPayload.readStringEOF(); log.debug("ErrPacket[errorCode={},sqlStateMarker={},sqlState={},errorMessage={}]", errorCode, sqlStateMarker, sqlState, errorMessage); - commandResponsePackets = new CommandResponsePackets(new ErrPacket(sequenceId + 1, errorCode, sqlStateMarker, sqlState, errorMessage)); - break; - case EofPacket.HEADER: - warnings = mysqlPacketPayload.readInt2(); - statusFlags = mysqlPacketPayload.readInt2(); - log.debug("EofPacket[warnings={},statusFlags={}]", warnings, statusFlags); - commandResponsePackets = new CommandResponsePackets(new EofPacket(sequenceId + 1, warnings, statusFlags)); + mysqlQueryResult.setGenericResponse(new ErrPacket(sequenceId, errorCode, sqlStateMarker, sqlState, errorMessage)); break; default: break; } - int connectionId = MySQLResultCache.getInstance().getConnectionMap(context.channel().id().asShortText()); - if (MySQLResultCache.getInstance().get(connectionId) != null) { - MySQLResultCache.getInstance().get(connectionId).setResponse(commandResponsePackets); + + try { + int connectionId = MySQLResultCache.getInstance().getConnection(context.channel().id().asShortText()); + if (MySQLResultCache.getInstance().getFuture(connectionId) != null) { + MySQLResultCache.getInstance().getFuture(connectionId).setResponse(mysqlQueryResult); + } + } finally { + mysqlQueryResult = null; + mysqlPacketPayload.getByteBuf().release(); } } - //TODO @Override - protected void executeCommandResponsePackets(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { - CommandResponsePackets result = new CommandResponsePackets(); - - //TODO - int columnCount = header <= 0xfb ? header : (int) mysqlPacketPayload.readIntLenenc(); - if (0 == columnCount) { - result.addPacket(new OKPacket(sequenceId + 1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); + protected void endOfFilePacket(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { + int warnings = mysqlPacketPayload.readInt2(); + int statusFlags = mysqlPacketPayload.readInt2(); + log.debug("EofPacket[warnings={},statusFlags={}]", warnings, statusFlags); + EofPacket eofPacket = new EofPacket(sequenceId, warnings, statusFlags); + if (mysqlQueryResult.isColumnFinished()) { + mysqlQueryResult.setRowFinished(eofPacket); + mysqlQueryResult = null; + mysqlPacketPayload.getByteBuf().release(); } else { - result.addPacket(new FieldCountPacket(sequenceId + 1, columnCount)); - for (int i = 1; i <= columnCount; i++) { - String catalog = mysqlPacketPayload.readStringLenenc(); - String schema = mysqlPacketPayload.readStringLenenc(); - String table = mysqlPacketPayload.readStringLenenc(); - String orgTable = mysqlPacketPayload.readStringLenenc(); - String name = mysqlPacketPayload.readStringLenenc(); - String orgName = mysqlPacketPayload.readStringLenenc(); - mysqlPacketPayload.skipReserved(1 + 2); // fixed value and charset - int columnLength = mysqlPacketPayload.readInt4(); - int columnType = mysqlPacketPayload.readInt1(); - mysqlPacketPayload.skipReserved(2); // field flags - int decimals = mysqlPacketPayload.readInt1(); - mysqlPacketPayload.skipReserved(2); // filler value 0x00 0x00 - result.addPacket(new ColumnDefinition41Packet(sequenceId + 1, schema, table, orgTable, name, orgName, - columnLength, ColumnType.valueOfJDBCType(columnType), decimals)); + mysqlQueryResult.setColumnFinished(eofPacket); + int connectionId = MySQLResultCache.getInstance().getConnection(context.channel().id().asShortText()); + if (MySQLResultCache.getInstance().getFuture(connectionId) != null) { + MySQLResultCache.getInstance().getFuture(connectionId).setResponse(mysqlQueryResult); } - result.addPacket(new EofPacket(sequenceId + 1, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue())); + } + } + + @Override + protected void executeCommandResponsePackets(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { + if (mysqlQueryResult == null) { + mysqlQueryResult = new MySQLQueryResult(sequenceId, header); + } else if (mysqlQueryResult.needColumnDefinition()) { + mysqlPacketPayload.getByteBuf().resetReaderIndex(); + String catalog = mysqlPacketPayload.readStringLenenc(); + String schema = mysqlPacketPayload.readStringLenenc(); + String table = mysqlPacketPayload.readStringLenenc(); + String orgTable = mysqlPacketPayload.readStringLenenc(); + String name = mysqlPacketPayload.readStringLenenc(); + String orgName = mysqlPacketPayload.readStringLenenc(); + // fixed value and charset + mysqlPacketPayload.skipReserved(1 + 2); + int columnLength = mysqlPacketPayload.readInt4(); + int columnType = mysqlPacketPayload.readInt1(); + // field flags + mysqlPacketPayload.skipReserved(2); + int decimals = mysqlPacketPayload.readInt1(); + // filler value 0x00 0x00 + mysqlPacketPayload.skipReserved(2); - if (mysqlPacketPayload.isReadable()) { - while (mysqlPacketPayload.isReadable()) { - List data = new ArrayList<>(columnCount); - for (int i = 1; i <= columnCount; i++) { - data.add(mysqlPacketPayload.readStringLenenc()); - } - result.addPacket(new TextResultSetRowPacket(sequenceId + 1, data)); - } - result.addPacket(new EofPacket(sequenceId + 1, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue())); + ColumnDefinition41Packet columnDefinition = new ColumnDefinition41Packet(sequenceId, schema, table, orgTable, name, orgName, + columnLength, ColumnType.valueOfJDBCType(columnType), decimals); + mysqlQueryResult.addColumnDefinition(columnDefinition); + } else { + mysqlPacketPayload.getByteBuf().resetReaderIndex(); + List data = new ArrayList<>(mysqlQueryResult.getColumnCount()); + for (int i = 1; i <= mysqlQueryResult.getColumnCount(); i++) { + data.add(mysqlPacketPayload.readStringLenenc()); } + + TextResultSetRowPacket textResultSetRow = new TextResultSetRowPacket(sequenceId, data); + mysqlQueryResult.addTextResultSetRow(textResultSetRow); } - - int connectionId = MySQLResultCache.getInstance().getConnectionMap(context.channel().id().asShortText()); - MySQLResultCache.getInstance().get(connectionId).setResponse(result); } @Override diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java index ee5d484e25cc2..1c90e95682d11 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java @@ -17,69 +17,146 @@ package io.shardingsphere.proxy.backend.mysql; -import io.netty.buffer.ByteBuf; -import io.shardingsphere.core.merger.QueryResult; -import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; -import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; -import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet; -import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.FieldCountPacket; -import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket; -import lombok.RequiredArgsConstructor; - import java.io.InputStream; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.BlockingQueue; -import java.util.concurrent.ConcurrentLinkedDeque; -import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.LinkedBlockingQueue; +import com.google.common.collect.Lists; + +import io.shardingsphere.core.merger.QueryResult; +import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacket; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.FieldCountPacket; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket; +import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; + /** * MySQL packet query result. * * @author wangkai + * @author linjiaqi */ +@Slf4j public final class MySQLQueryResult implements QueryResult { + @Getter + private final CommandResponsePackets commandResponsePackets; private final int columnCount; private final Map columnIndexAndLabelMap; private final Map columnLabelAndIndexMap; + + private final List columnDefinitions; + + private final BlockingQueue resultSet; - private final BlockingQueue resultSet; + @Getter + private int currentSequenceId; private TextResultSetRowPacket currentRow; - public MySQLQueryResult(final List columnDefines, final BlockingQueue resultSet) { - columnCount = columnDefines.size(); + @Getter + private boolean columnFinished; + + public MySQLQueryResult() { + commandResponsePackets = new CommandResponsePackets(); + columnCount = 0; + columnIndexAndLabelMap = null; + columnLabelAndIndexMap = null; + columnDefinitions = null; + resultSet = null; + } + + public MySQLQueryResult(final int sequenceId, final int columnCount) { + commandResponsePackets = new CommandResponsePackets(new FieldCountPacket(sequenceId, columnCount)); + this.columnCount = columnCount; columnIndexAndLabelMap = new HashMap<>(columnCount, 1); columnLabelAndIndexMap = new HashMap<>(columnCount, 1); - for(int i = 0; i<=columnCount;i++){ - columnIndexAndLabelMap.put(i, columnDefines.get(i).getName()); - columnLabelAndIndexMap.put(columnDefines.get(i).getName(), i); + columnDefinitions = Lists.newArrayListWithCapacity(columnCount); + currentSequenceId = sequenceId; + resultSet = new LinkedBlockingQueue<>(); + } + + /** + * Set GenericResponse to the CommandResponsePackets. + * @param mysqlPacket mysqlPacket + */ + public void setGenericResponse(final MySQLPacket mysqlPacket) { + commandResponsePackets.addPacket(mysqlPacket); + } + + /** + * Whether the QueryResult is needed to add ColumnDefinition. + * @return whether the columnCount is larger than columnDefinitions's size + */ + public boolean needColumnDefinition() { + return columnCount > columnDefinitions.size(); + } + + /** + * Add ColumnDefinition to the QueryResult. + * @param columnDefinition columnDefinition + */ + public void addColumnDefinition(final ColumnDefinition41Packet columnDefinition) { + commandResponsePackets.addPacket(columnDefinition); + columnDefinitions.add(columnDefinition); + columnIndexAndLabelMap.put(columnDefinitions.indexOf(columnDefinition) + 1, columnDefinition.getName()); + columnLabelAndIndexMap.put(columnDefinition.getName(), columnDefinitions.indexOf(columnDefinition) + 1); + currentSequenceId++; + } + + /** + * Add TextResultSetRow to the QueryResult. + * @param textResultSetRow textResultSetRow + */ + public void addTextResultSetRow(final TextResultSetRowPacket textResultSetRow) { + put(textResultSetRow); + } + + /** + * Set Column Finished. + * @param eofPacket eofPacket + */ + public void setColumnFinished(final EofPacket eofPacket) { + commandResponsePackets.addPacket(eofPacket); + currentSequenceId++; + columnFinished = true; + } + + /** + * Set Row Finished. + * @param eofPacket eofPacket + */ + public void setRowFinished(final EofPacket eofPacket) { + put(eofPacket); + } + + private void put(final MySQLPacket mysqlPacket) { + try { + resultSet.put(mysqlPacket); + } catch (InterruptedException e) { + log.error(e.getMessage(), e); } - columnDefines.clear(); - this.resultSet = resultSet; } @Override - public boolean next() throws InterruptedException { - byte[] result = resultSet.take(); -// if(result == "eof"){ - resultSet.clear(); -// reutnr false; -// }else{ -// currentRow = new TextResultSetRowPacket(++currentSequenceId, result); -// return true; -// } + public boolean next() { + try { + MySQLPacket mysqlPacket = resultSet.take(); + currentRow = (mysqlPacket instanceof TextResultSetRowPacket) ? (TextResultSetRowPacket) mysqlPacket : null; + return currentRow == null ? false : true; + } catch (InterruptedException e) { + log.error(e.getMessage(), e); + } return false; } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/netty/ClientHandlerInitializer.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/netty/ClientHandlerInitializer.java index c2be33ab127b8..2a97052fa7731 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/netty/ClientHandlerInitializer.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/netty/ClientHandlerInitializer.java @@ -22,6 +22,7 @@ import io.netty.channel.ChannelPipeline; import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.proxy.backend.common.BackendHandlerFactory; +import io.shardingsphere.proxy.config.DataScourceConfig; import io.shardingsphere.proxy.transport.common.codec.PacketCodecFactory; import lombok.RequiredArgsConstructor; @@ -29,25 +30,17 @@ * Channel initializer. * * @author wangkai + * @author linjiaqi */ @RequiredArgsConstructor public final class ClientHandlerInitializer extends ChannelInitializer { - - private final String ip; - - private final int port; - - private final String database; - - private final String username; - - private final String password; + private final DataScourceConfig dataScourceConfig; @Override protected void initChannel(final Channel channel) { ChannelPipeline pipeline = channel.pipeline(); // TODO load database type from yaml or startup arguments pipeline.addLast(PacketCodecFactory.createPacketCodecInstance(DatabaseType.MySQL)); - pipeline.addLast(BackendHandlerFactory.createBackendHandlerInstance(DatabaseType.MySQL, ip, port, database, username, password)); + pipeline.addLast(BackendHandlerFactory.createBackendHandlerInstance(DatabaseType.MySQL, dataScourceConfig)); } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/DataScourceConfig.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/DataScourceConfig.java new file mode 100644 index 0000000000000..4cf0a4e719b1b --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/DataScourceConfig.java @@ -0,0 +1,40 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.config; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +/** + * DataScourceConfig. + * + * @author linjiaqi + */ +@RequiredArgsConstructor +@Getter +public class DataScourceConfig { + private final String ip; + + private final int port; + + private final String database; + + private final String username; + + private final String password; +} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java index aab6f40fb9d8b..64291126c67c1 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java @@ -51,7 +51,7 @@ public final class MySQLFrontendHandler extends FrontendHandler { protected void handshake(final ChannelHandlerContext context) { authPluginData = new AuthPluginData(); int connectionId = ConnectionIdGenerator.getInstance().nextId(); - MySQLResultCache.getInstance().putConnectionMap(context.channel().id().asShortText(), connectionId); + MySQLResultCache.getInstance().putConnection(context.channel().id().asShortText(), connectionId); context.writeAndFlush(new HandshakePacket(ConnectionIdGenerator.getInstance().nextId(), authPluginData)); } @@ -76,7 +76,7 @@ public void run() { MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload(message); try { int sequenceId = mysqlPacketPayload.readInt1(); - int connectionId = MySQLResultCache.getInstance().getConnectionMap(context.channel().id().asShortText()); + int connectionId = MySQLResultCache.getInstance().getConnection(context.channel().id().asShortText()); CommandPacket commandPacket = CommandPacketFactory.getCommandPacket(sequenceId, connectionId, mysqlPacketPayload); for (DatabaseProtocolPacket each : commandPacket.execute().getDatabaseProtocolPackets()) { context.writeAndFlush(each); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/common/codec/PacketCodec.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/common/codec/PacketCodec.java index 10b1517783cc4..c3782da230469 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/common/codec/PacketCodec.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/common/codec/PacketCodec.java @@ -41,7 +41,7 @@ protected void decode(final ChannelHandlerContext context, final ByteBuf in, fin return; } if (log.isDebugEnabled()) { - log.debug("Read from client: \n {}", ByteBufUtil.prettyHexDump(in)); + log.debug("Read from client {} : \n {}", context.channel().id().asShortText(), ByteBufUtil.prettyHexDump(in)); } doDecode(context, in, out, readableBytes); } @@ -54,7 +54,7 @@ protected void decode(final ChannelHandlerContext context, final ByteBuf in, fin protected void encode(final ChannelHandlerContext context, final T message, final ByteBuf out) { doEncode(context, message, out); if (log.isDebugEnabled()) { - log.debug("Write to client: \n {}", ByteBufUtil.prettyHexDump(out)); + log.debug("Write to client {} : \n {}", context.channel().id().asShortText(), ByteBufUtil.prettyHexDump(out)); } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java index aa73f62bfa373..d6adf3aa640b8 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java @@ -17,6 +17,8 @@ package io.shardingsphere.proxy.transport.mysql.packet.command.text.query; +import java.sql.SQLException; + import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.proxy.backend.common.SQLExecuteBackendHandler; import io.shardingsphere.proxy.backend.common.SQLPacketsBackendHandler; @@ -24,21 +26,22 @@ import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket; +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacketType; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; -import java.sql.SQLException; - /** * COM_QUERY command packet. * @see COM_QUERY * * @author zhangliang + * @author linjiaqi */ @Slf4j public final class ComQueryPacket extends CommandPacket { - - private final String sql; + @Setter + private String sql; private final SQLExecuteBackendHandler sqlExecuteBackendHandler; @@ -53,6 +56,7 @@ public ComQueryPacket(final int sequenceId, final int connectionId, final MySQLP @Override public void write(final MySQLPacketPayload mysqlPacketPayload) { + mysqlPacketPayload.writeInt1(CommandPacketType.COM_QUERY.getValue()); mysqlPacketPayload.writeStringEOF(sql); } @@ -74,7 +78,7 @@ public CommandResponsePackets execute() { public boolean hasMoreResultValue() { try { if (RuleRegistry.WITHOUT_JDBC) { - return false; + return sqlPacketsBackendHandler.hasMoreResultValue(); } else { return sqlExecuteBackendHandler.hasMoreResultValue(); } @@ -89,6 +93,10 @@ public boolean hasMoreResultValue() { * @return database protocol packet */ public DatabaseProtocolPacket getResultValue() { - return sqlExecuteBackendHandler.getResultValue(); + if (RuleRegistry.WITHOUT_JDBC) { + return sqlPacketsBackendHandler.getResultValue(); + } else { + return sqlExecuteBackendHandler.getResultValue(); + } } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java index 7b3bd9edc60eb..171c7849f0399 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/MySQLResultCache.java @@ -17,11 +17,13 @@ package io.shardingsphere.proxy.util; +import java.util.List; +import java.util.concurrent.TimeUnit; + import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; -import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; -import java.util.concurrent.TimeUnit; +import io.shardingsphere.core.merger.QueryResult; /** * cache for SynchronizedFuture. @@ -32,19 +34,17 @@ public class MySQLResultCache { private static final MySQLResultCache INSTANCE = new MySQLResultCache(); //TODO expire time should be set. - private Cache resultCache = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.SECONDS).build(); + private Cache>> resultCache = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.SECONDS).build(); private Cache connectionCache = CacheBuilder.newBuilder().build(); - private Cache channelCache = CacheBuilder.newBuilder().build(); - /** * put synchronizedFuture by connectionId. * * @param connectionId mysql connection id. * @param synchronizedFuture multiple result set. */ - public void put(final int connectionId, final SynchronizedFuture synchronizedFuture) { + public void putFuture(final int connectionId, final SynchronizedFuture> synchronizedFuture) { resultCache.put(connectionId, synchronizedFuture); } @@ -54,7 +54,7 @@ public void put(final int connectionId, final SynchronizedFuture get(final int connectionId) { + public SynchronizedFuture> getFuture(final int connectionId) { return resultCache.getIfPresent(connectionId); } @@ -63,7 +63,7 @@ public SynchronizedFuture get(final int connectionId) { * * @param connectionId mysql connection id. */ - public void delete(final int connectionId) { + public void deleteFuture(final int connectionId) { resultCache.invalidate(connectionId); } @@ -73,7 +73,7 @@ public void delete(final int connectionId) { * @param channelId netty channel id. * @param connectionId mysql connection id. */ - public void putConnectionMap(final String channelId, final int connectionId) { + public void putConnection(final String channelId, final int connectionId) { connectionCache.put(channelId, connectionId); } @@ -83,7 +83,7 @@ public void putConnectionMap(final String channelId, final int connectionId) { * @param channelId netty channel id. * @return connectionId mysql connection id. */ - public int getConnectionMap(final String channelId) { + public int getConnection(final String channelId) { return connectionCache.getIfPresent(channelId); } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/SynchronizedFuture.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/SynchronizedFuture.java index f2984825d8d41..bd330a3ddfd12 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/SynchronizedFuture.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/util/SynchronizedFuture.java @@ -17,8 +17,6 @@ package io.shardingsphere.proxy.util; -import java.util.Collections; -import java.util.Iterator; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Future; @@ -26,28 +24,22 @@ import com.google.common.collect.Lists; -import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; -import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; -import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacket; -import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; -import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.FieldCountPacket; -import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket; -import io.shardingsphere.proxy.transport.mysql.packet.generic.ErrPacket; -import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket; +import io.shardingsphere.core.merger.QueryResult; +import lombok.extern.slf4j.Slf4j; /** * sync get multiple netty return. * * @author wangkai + * @author linjiaqi */ -public class SynchronizedFuture implements Future { +@Slf4j +public class SynchronizedFuture implements Future> { private boolean merged; private CountDownLatch latch; - private List responses; - - private long beginTime = System.currentTimeMillis(); + private List responses; public SynchronizedFuture(final int resultSize) { latch = new CountDownLatch(resultSize); @@ -70,9 +62,9 @@ public boolean isDone() { } @Override - public CommandResponsePackets get() throws InterruptedException { + public List get() throws InterruptedException { latch.await(); - return merge(); + return responses; } /** @@ -82,87 +74,21 @@ public CommandResponsePackets get() throws InterruptedException { * @return responses. */ @Override - public CommandResponsePackets get(final long timeout, final TimeUnit unit) { + public List get(final long timeout, final TimeUnit unit) { try { latch.await(timeout, unit); } catch (InterruptedException e) { - //TODO + log.error(e.getMessage(), e); } - return merge(); + return responses; } /** * set response and count down. * @param response sql command result. */ - public void setResponse(final CommandResponsePackets response) { + public void setResponse(final QueryResult response) { responses.add(response); latch.countDown(); } - - private CommandResponsePackets merge() { - boolean isOkPacket = false; - int affectedRows = 0; - long lastInsertId = 0; - int sequenceId = 0; - CommandResponsePackets headPackets = new CommandResponsePackets(); - for (CommandResponsePackets each : responses) { - headPackets.addPacket(each.getHeadPacket()); - } - - for (DatabaseProtocolPacket each : headPackets.getDatabaseProtocolPackets()) { - if (each instanceof ErrPacket) { - return new CommandResponsePackets(each); - } - if (each instanceof OKPacket) { - isOkPacket = true; - OKPacket okPacket = (OKPacket) each; - affectedRows += okPacket.getAffectedRows(); - lastInsertId = okPacket.getLastInsertId(); - sequenceId = okPacket.getSequenceId(); - } - } - if (isOkPacket) { - return new CommandResponsePackets(new OKPacket(sequenceId, affectedRows, lastInsertId, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); - } - - CommandResponsePackets result = new CommandResponsePackets(); - FieldCountPacket fieldCountPacket = (FieldCountPacket) headPackets.getHeadPacket(); - result.addPacket(fieldCountPacket); - boolean isHeadReaded = false; - for (CommandResponsePackets each : responses) { - Iterator databaseProtocolPackets = each.getDatabaseProtocolPackets().iterator(); - - List columnDefinitionAndEOFPackets = Collections.emptyList(); - for (int i = 0; i < fieldCountPacket.getColumnCount(); i++) { - MySQLPacket columnDefinitionPacket = (MySQLPacket) databaseProtocolPackets.next(); - columnDefinitionPacket.setSequenceId(++sequenceId); - columnDefinitionAndEOFPackets.add(columnDefinitionPacket); // ColumnDefinition41Packet - } - MySQLPacket eofPacket = (MySQLPacket) databaseProtocolPackets.next(); - eofPacket.setSequenceId(++sequenceId); - columnDefinitionAndEOFPackets.add(eofPacket); // EofPacket - - List textResultSetRowPackets = Collections.emptyList(); - if (databaseProtocolPackets.hasNext()) { - while (databaseProtocolPackets.hasNext()) { - MySQLPacket textResultSetRowPacket = (MySQLPacket) databaseProtocolPackets.next(); - textResultSetRowPacket.setSequenceId(++sequenceId); - textResultSetRowPackets.add(textResultSetRowPacket); // TextResultSetRowPacket - } - textResultSetRowPackets.remove(textResultSetRowPackets.size() - 1); // remove EofPacket - } - - if (!isHeadReaded) { - isHeadReaded = true; - result.addPackets(columnDefinitionAndEOFPackets); - } else { - result.addPackets(textResultSetRowPackets); - } - } - result.addPacket(new EofPacket(++sequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue())); - - merged = true; - return result; - } } From e018c01e1306e3f6432d3c05a43eee3000d2d52c Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 7 Jun 2018 16:51:32 +0800 Subject: [PATCH 145/351] test exclude transaction : support all org.apache.zookeeper version --- build.gradle | 13 +- .../saaavsaaa/client/action/IProvider.java | 2 +- .../client/zookeeper/base/BaseProvider.java | 4 +- .../strategy/TransactionContendStrategy.java | 218 +++++++++--------- .../zookeeper/transaction/ZKTransaction.java | 8 +- .../client/retry/AsyncRetryCenterTest.java | 4 +- .../saaavsaaa/client/retry/TestCallable.java | 1 + .../retry/TestCreateCurrentOperation.java | 1 + .../client/zookeeper/base/BaseClientTest.java | 3 +- 9 files changed, 133 insertions(+), 121 deletions(-) diff --git a/build.gradle b/build.gradle index 9c8acc203d716..1e10bbb175ab8 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ group 'com.saaavsaaa' //version '1.0-SNAPSHOT' -version '1.6' +version '2.1-RELEASE' apply plugin: 'idea' apply plugin: 'java' @@ -30,11 +30,20 @@ repositories { dependencies { // https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper - compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.4.11' +// compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.3.0' +// compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.4.12' + compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.5.3-beta' + + // https://mvnrepository.com/artifact/org.slf4j/slf4j-api +// compile group: 'org.slf4j', name: 'slf4j-api', version: '1.6.1' + testCompile group: 'junit', name: 'junit', version: '4.11' // https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper +// testCompile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.4.12' testCompile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.5.3-beta' +// testCompile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.3.0' +// testCompile group: 'org.slf4j', name: 'slf4j-api', version: '1.6.1' } uploadArchives { diff --git a/src/main/java/com/saaavsaaa/client/action/IProvider.java b/src/main/java/com/saaavsaaa/client/action/IProvider.java index b200987e15c41..6ca594389d7c5 100644 --- a/src/main/java/com/saaavsaaa/client/action/IProvider.java +++ b/src/main/java/com/saaavsaaa/client/action/IProvider.java @@ -30,7 +30,7 @@ public interface IProvider { Stack getDeletingPaths(final String key); void executeContention(final LeaderElection election) throws KeeperException, InterruptedException; - void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException; +// void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException; void resetConnection(); } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java index b7c0c992a6863..7beaa54f88c9b 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java @@ -132,10 +132,10 @@ public void executeContention(final String nodeBeCompete, final LeaderElection e election.executeContention(rootNode, this); } - @Override + /*@Override public void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { transaction.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); - } + }*/ @Override public void resetConnection() { diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java index 40bc169c66882..4027d4dcbd9dd 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java @@ -26,113 +26,113 @@ public TransactionContendStrategy(IProvider provider) { super(provider); } - @Override - public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - LeaderElection election = buildCreateAllNeedElection(key, value, createMode, null); - provider.executeContention(election); - logger.debug("ContentionStrategy createAllNeedPath executeContention"); - election.waitDone(); - } - - private LeaderElection buildCreateAllNeedElection(final String key, final String value, final CreateMode createMode, final Callback callback){ - return new LeaderElection() { - @Override - public void action() throws KeeperException, InterruptedException { - logger.debug("ContentionStrategy createAllNeedPath action:{}", key); - ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getHolder()); - createBegin(key, value, createMode, transaction); - transaction.commit(); - } - @Override - public void callback(){ - if (callback != null) { - callback.processResult(); - } - } - }; - } - - private void createBegin(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { - if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - provider.createInTransaction(key, value, createMode, transaction); - return; - } - List nodes = provider.getNecessaryPaths(key); - for (int i = 0; i < nodes.size(); i++) { - if (provider.exists(nodes.get(i))){ - logger.info("create node exist:{}", nodes.get(i)); - continue; - } - logger.debug("node not exist and create:", nodes.get(i)); - if (i == nodes.size() - 1){ - provider.createInTransaction(nodes.get(i), value, createMode, transaction); - } else { - provider.createInTransaction(nodes.get(i), Constants.NOTHING_VALUE, createMode, transaction); - } - } - } - - @Override - public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { - provider.executeContention(new LeaderElection() { - @Override - public void action() throws KeeperException, InterruptedException { - ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getHolder()); - deleteChildren(provider.getRealPath(key), true, transaction); - transaction.commit(); - } - }); - logger.debug("ContentionStrategy deleteAllChildren executeContention"); - } - - private void deleteChildren(final String key, final boolean deleteCurrentNode, final ZKTransaction transaction) throws KeeperException, InterruptedException { - List children = provider.getChildren(key); - for (int i = 0; i < children.size(); i++) { - String child = PathUtil.getRealPath(key, children.get(i)); - if (!provider.exists(child)){ - logger.info("delete not exist:{}", child); - continue; - } - logger.debug("deleteChildren:{}", child); - deleteChildren(child, true, transaction); - } - if (deleteCurrentNode){ - transaction.delete(key, Constants.VERSION); - } - } - - @Override - public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - provider.executeContention(new LeaderElection() { - @Override - public void action() throws KeeperException, InterruptedException { - ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getHolder()); - deleteBranch(provider.getRealPath(key), transaction); - transaction.commit(); - } - }); - logger.debug("ContentionStrategy deleteCurrentBranch executeContention"); - } - - private void deleteBranch(String key, final ZKTransaction transaction) throws KeeperException, InterruptedException { - deleteChildren(key, false, transaction); - Stack pathStack = provider.getDeletingPaths(key); - String prePath = key; - while (!pathStack.empty()){ - String node = pathStack.pop(); - // contrast cache - // Performance needs testing - List children = provider.getChildren(node); - boolean canDelete = children.size() == 0 || children.size() == 1; - if (children.size() == 1){ - if (!PathUtil.getRealPath(node, children.get(0)).equals(prePath)){ - canDelete = false; - } - } - if (provider.exists(node) && canDelete){ - transaction.delete(node, Constants.VERSION); - } - prePath = node; - } - } +// @Override +// public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { +// LeaderElection election = buildCreateAllNeedElection(key, value, createMode, null); +// provider.executeContention(election); +// logger.debug("ContentionStrategy createAllNeedPath executeContention"); +// election.waitDone(); +// } +// +// private LeaderElection buildCreateAllNeedElection(final String key, final String value, final CreateMode createMode, final Callback callback){ +// return new LeaderElection() { +// @Override +// public void action() throws KeeperException, InterruptedException { +// logger.debug("ContentionStrategy createAllNeedPath action:{}", key); +// ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getHolder()); +// createBegin(key, value, createMode, transaction); +// transaction.commit(); +// } +// @Override +// public void callback(){ +// if (callback != null) { +// callback.processResult(); +// } +// } +// }; +// } +// +// private void createBegin(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { +// if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ +// provider.createInTransaction(key, value, createMode, transaction); +// return; +// } +// List nodes = provider.getNecessaryPaths(key); +// for (int i = 0; i < nodes.size(); i++) { +// if (provider.exists(nodes.get(i))){ +// logger.info("create node exist:{}", nodes.get(i)); +// continue; +// } +// logger.debug("node not exist and create:", nodes.get(i)); +// if (i == nodes.size() - 1){ +// provider.createInTransaction(nodes.get(i), value, createMode, transaction); +// } else { +// provider.createInTransaction(nodes.get(i), Constants.NOTHING_VALUE, createMode, transaction); +// } +// } +// } +// +// @Override +// public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { +// provider.executeContention(new LeaderElection() { +// @Override +// public void action() throws KeeperException, InterruptedException { +// ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getHolder()); +// deleteChildren(provider.getRealPath(key), true, transaction); +// transaction.commit(); +// } +// }); +// logger.debug("ContentionStrategy deleteAllChildren executeContention"); +// } +// +// private void deleteChildren(final String key, final boolean deleteCurrentNode, final ZKTransaction transaction) throws KeeperException, InterruptedException { +// List children = provider.getChildren(key); +// for (int i = 0; i < children.size(); i++) { +// String child = PathUtil.getRealPath(key, children.get(i)); +// if (!provider.exists(child)){ +// logger.info("delete not exist:{}", child); +// continue; +// } +// logger.debug("deleteChildren:{}", child); +// deleteChildren(child, true, transaction); +// } +// if (deleteCurrentNode){ +// transaction.delete(key, Constants.VERSION); +// } +// } +// +// @Override +// public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { +// provider.executeContention(new LeaderElection() { +// @Override +// public void action() throws KeeperException, InterruptedException { +// ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getHolder()); +// deleteBranch(provider.getRealPath(key), transaction); +// transaction.commit(); +// } +// }); +// logger.debug("ContentionStrategy deleteCurrentBranch executeContention"); +// } +// +// private void deleteBranch(String key, final ZKTransaction transaction) throws KeeperException, InterruptedException { +// deleteChildren(key, false, transaction); +// Stack pathStack = provider.getDeletingPaths(key); +// String prePath = key; +// while (!pathStack.empty()){ +// String node = pathStack.pop(); +// // contrast cache +// // Performance needs testing +// List children = provider.getChildren(node); +// boolean canDelete = children.size() == 0 || children.size() == 1; +// if (children.size() == 1){ +// if (!PathUtil.getRealPath(node, children.get(0)).equals(prePath)){ +// canDelete = false; +// } +// } +// if (provider.exists(node) && canDelete){ +// transaction.delete(node, Constants.VERSION); +// } +// prePath = node; +// } +// } } diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java index f4f8b73b935eb..a78fbed62a931 100644 --- a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java +++ b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java @@ -15,16 +15,16 @@ */ public class ZKTransaction { private static final Logger logger = LoggerFactory.getLogger(ZKTransaction.class); - private final Transaction transaction; +// private final Transaction transaction; private final String rootNode; public ZKTransaction(final String root, final Holder holder) { - transaction = holder.getZooKeeper().transaction(); +// transaction = holder.getZooKeeper().transaction(); rootNode = root; logger.debug("ZKTransaction root:{}", rootNode); } - public ZKTransaction create(final String path, final byte[] data, final List acl, final CreateMode createMode) { + /*public ZKTransaction create(final String path, final byte[] data, final List acl, final CreateMode createMode) { this.transaction.create(PathUtil.getRealPath(rootNode, path), data, acl, createMode); logger.debug("wait create:{},data:{},acl:{},createMode:{}", new Object[]{path, data, acl, createMode}); return this; @@ -65,5 +65,5 @@ public List commit() throws InterruptedException, KeeperException { public void commit(final AsyncCallback.MultiCallback cb, final Object ctx) { this.transaction.commit(cb, ctx); logger.debug("ZKTransaction commit ctx:{}", ctx); - } + }*/ } diff --git a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java index da0cca4bc114a..4dee0d0d9e413 100644 --- a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java +++ b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java @@ -64,8 +64,8 @@ public void create() throws InterruptedException, KeeperException { AsyncRetryCenter.INSTANCE.add(new TestCreateCurrentOperation(provider, key, value, CreateMode.PERSISTENT)); Thread.sleep(2000); String path = PathUtil.getRealPath(TestSupport.ROOT, key); - assert provider.exists(path); -// assert client.checkExists(path); +// assert provider.exists(path); + assert client.checkExists(path); client.useExecStrategy(StrategyType.USUAL); client.deleteAllChildren(path); client.deleteCurrentBranch(path); diff --git a/src/test/java/com/saaavsaaa/client/retry/TestCallable.java b/src/test/java/com/saaavsaaa/client/retry/TestCallable.java index bc688bf85106c..a2413b7640999 100644 --- a/src/test/java/com/saaavsaaa/client/retry/TestCallable.java +++ b/src/test/java/com/saaavsaaa/client/retry/TestCallable.java @@ -24,6 +24,7 @@ public void call() throws KeeperException, InterruptedException { System.out.println("TestCallable injectSessionExpiration==================================================" + count); ((BaseProvider)provider).getHolder().getZooKeeper().getTestable().injectSessionExpiration(); +// ((BaseProvider)provider).getHolder().close(); } System.out.println("TestCallable ================================================" + count); test(); diff --git a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java index 001b0e515a247..1f3a23c20172a 100644 --- a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java +++ b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java @@ -25,6 +25,7 @@ public void execute() throws KeeperException, InterruptedException { System.out.println("test injectSessionExpiration==================================================" + count); ((BaseProvider)provider).getHolder().getZooKeeper().getTestable().injectSessionExpiration(); +// ((BaseProvider)provider).getHolder().close(); } System.out.println("test ================================================" + count); super.execute(); diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java index 67458b52b150a..89341f099c927 100644 --- a/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java +++ b/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java @@ -170,7 +170,8 @@ protected void persist(IClient client) throws KeeperException, InterruptedExcept } private void updateWithCheck(String key, String value, IClient client) throws KeeperException, InterruptedException { - client.transaction().check(key, Constants.VERSION).setData(key, value.getBytes(Constants.UTF_8), Constants.VERSION).commit(); + client.update(key, value); +// client.transaction().check(key, Constants.VERSION).setData(key, value.getBytes(Constants.UTF_8), Constants.VERSION).commit(); } protected void persistEphemeral(IClient client) throws KeeperException, InterruptedException { From 787e495cfd3c7d3485c949e507edf4497213d14d Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 7 Jun 2018 17:50:07 +0800 Subject: [PATCH 146/351] contribute to ss --- .../resources/code/OrchestrationFacade.java | 170 +++++++ .../code/ZookeeperConfiguration.java | 83 ++++ .../newzk/NewZookeeperRegistryCenter.java | 254 +++++++++++ .../code/newzk/client/action/Callback.java | 29 ++ .../code/newzk/client/action/IAction.java | 134 ++++++ .../code/newzk/client/action/IClient.java | 93 ++++ .../newzk/client/action/IExecStrategy.java | 31 ++ .../newzk/client/action/IGroupAction.java | 56 +++ .../code/newzk/client/action/IProvider.java | 189 ++++++++ .../newzk/client/cache/CacheStrategy.java | 27 ++ .../code/newzk/client/cache/PathNode.java | 112 +++++ .../code/newzk/client/cache/PathStatus.java | 26 ++ .../code/newzk/client/cache/PathTree.java | 414 ++++++++++++++++++ .../newzk/client/election/LeaderElection.java | 129 ++++++ .../newzk/client/retry/AsyncRetryCenter.java | 84 ++++ .../client/retry/DelayPolicyExecutor.java | 83 ++++ .../newzk/client/retry/DelayRetryPolicy.java | 64 +++ .../code/newzk/client/retry/RetryThread.java | 117 +++++ .../code/newzk/client/utility/Constants.java | 55 +++ .../code/newzk/client/utility/PathUtil.java | 187 ++++++++ .../code/newzk/client/utility/StringUtil.java | 34 ++ .../newzk/client/zookeeper/CacheClient.java | 137 ++++++ .../newzk/client/zookeeper/ClientFactory.java | 135 ++++++ .../newzk/client/zookeeper/UsualClient.java | 193 ++++++++ .../client/zookeeper/base/BaseClient.java | 172 ++++++++ .../zookeeper/base/BaseClientFactory.java | 69 +++ .../client/zookeeper/base/BaseContext.java | 51 +++ .../client/zookeeper/base/BaseOperation.java | 92 ++++ .../client/zookeeper/base/BaseProvider.java | 158 +++++++ .../client/zookeeper/base/BaseStrategy.java | 39 ++ .../newzk/client/zookeeper/base/Holder.java | 141 ++++++ .../operation/CreateAllNeedOperation.java | 54 +++ .../operation/CreateCurrentOperation.java | 53 +++ .../operation/DeleteAllChildrenOperation.java | 44 ++ .../DeleteCurrentBranchOperation.java | 47 ++ .../operation/DeleteCurrentOperation.java | 46 ++ .../zookeeper/operation/UpdateOperation.java | 47 ++ .../client/zookeeper/section/Callable.java | 109 +++++ .../zookeeper/section/ClientContext.java | 61 +++ .../client/zookeeper/section/ClientTask.java | 54 +++ .../client/zookeeper/section/Connection.java | 54 +++ .../client/zookeeper/section/Listener.java | 49 +++ .../zookeeper/section/StrategyType.java | 29 ++ .../zookeeper/section/WatcherCreator.java | 48 ++ .../strategy/AsyncRetryStrategy.java | 104 +++++ .../strategy/ContentionStrategy.java | 260 +++++++++++ .../zookeeper/strategy/SyncRetryStrategy.java | 163 +++++++ .../strategy/TransactionContendStrategy.java | 155 +++++++ .../zookeeper/strategy/UsualStrategy.java | 180 ++++++++ .../zookeeper/transaction/ZKTransaction.java | 145 ++++++ 50 files changed, 5260 insertions(+) create mode 100644 src/main/resources/code/OrchestrationFacade.java create mode 100644 src/main/resources/code/ZookeeperConfiguration.java create mode 100644 src/main/resources/code/newzk/NewZookeeperRegistryCenter.java create mode 100644 src/main/resources/code/newzk/client/action/Callback.java create mode 100644 src/main/resources/code/newzk/client/action/IAction.java create mode 100644 src/main/resources/code/newzk/client/action/IClient.java create mode 100644 src/main/resources/code/newzk/client/action/IExecStrategy.java create mode 100644 src/main/resources/code/newzk/client/action/IGroupAction.java create mode 100644 src/main/resources/code/newzk/client/action/IProvider.java create mode 100644 src/main/resources/code/newzk/client/cache/CacheStrategy.java create mode 100644 src/main/resources/code/newzk/client/cache/PathNode.java create mode 100644 src/main/resources/code/newzk/client/cache/PathStatus.java create mode 100644 src/main/resources/code/newzk/client/cache/PathTree.java create mode 100644 src/main/resources/code/newzk/client/election/LeaderElection.java create mode 100644 src/main/resources/code/newzk/client/retry/AsyncRetryCenter.java create mode 100644 src/main/resources/code/newzk/client/retry/DelayPolicyExecutor.java create mode 100644 src/main/resources/code/newzk/client/retry/DelayRetryPolicy.java create mode 100644 src/main/resources/code/newzk/client/retry/RetryThread.java create mode 100644 src/main/resources/code/newzk/client/utility/Constants.java create mode 100644 src/main/resources/code/newzk/client/utility/PathUtil.java create mode 100644 src/main/resources/code/newzk/client/utility/StringUtil.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/CacheClient.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/ClientFactory.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/UsualClient.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/base/BaseClient.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/base/BaseClientFactory.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/base/BaseContext.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/base/BaseOperation.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/base/BaseProvider.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/base/BaseStrategy.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/base/Holder.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/operation/CreateAllNeedOperation.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/operation/CreateCurrentOperation.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/operation/DeleteCurrentBranchOperation.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/operation/DeleteCurrentOperation.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/operation/UpdateOperation.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/section/Callable.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/section/ClientContext.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/section/ClientTask.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/section/Connection.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/section/Listener.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/section/StrategyType.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/section/WatcherCreator.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/strategy/ContentionStrategy.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/strategy/SyncRetryStrategy.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/strategy/TransactionContendStrategy.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/strategy/UsualStrategy.java create mode 100644 src/main/resources/code/newzk/client/zookeeper/transaction/ZKTransaction.java diff --git a/src/main/resources/code/OrchestrationFacade.java b/src/main/resources/code/OrchestrationFacade.java new file mode 100644 index 0000000000000..b257be8b9f000 --- /dev/null +++ b/src/main/resources/code/OrchestrationFacade.java @@ -0,0 +1,170 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.internal; + +import com.google.common.base.Preconditions; +import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration; +import io.shardingsphere.core.api.config.ShardingRuleConfiguration; +import io.shardingsphere.core.jdbc.core.datasource.MasterSlaveDataSource; +import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; +import io.shardingsphere.jdbc.orchestration.api.config.OrchestrationConfiguration; +import io.shardingsphere.jdbc.orchestration.internal.config.ConfigurationService; +import io.shardingsphere.jdbc.orchestration.internal.listener.ListenerFactory; +import io.shardingsphere.jdbc.orchestration.internal.state.datasource.DataSourceService; +import io.shardingsphere.jdbc.orchestration.internal.state.instance.InstanceStateService; +import io.shardingsphere.jdbc.orchestration.reg.api.RegistryCenter; +import io.shardingsphere.jdbc.orchestration.reg.api.RegistryCenterConfiguration; +import io.shardingsphere.jdbc.orchestration.reg.etcd.EtcdConfiguration; +import io.shardingsphere.jdbc.orchestration.reg.etcd.EtcdRegistryCenter; +import io.shardingsphere.jdbc.orchestration.reg.newzk.NewZookeeperRegistryCenter; +import io.shardingsphere.jdbc.orchestration.reg.zookeeper.ZookeeperConfiguration; +import io.shardingsphere.jdbc.orchestration.reg.zookeeper.ZookeeperRegistryCenter; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; + +import javax.sql.DataSource; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; + +/** + * Orchestration service facade. + * + * @author zhangliang + * @author caohao + */ +@Slf4j +public final class OrchestrationFacade implements AutoCloseable { + + private final boolean isOverwrite; + + @Getter + private final ConfigurationService configService; + + private final InstanceStateService instanceStateService; + + private final DataSourceService dataSourceService; + + private final ListenerFactory listenerManager; + + private final RegistryCenter regCenter; + + public OrchestrationFacade(final OrchestrationConfiguration config) { + regCenter = createRegistryCenter(config.getRegCenterConfig()); + isOverwrite = config.isOverwrite(); + configService = new ConfigurationService(config.getName(), regCenter); + instanceStateService = new InstanceStateService(config.getName(), regCenter); + dataSourceService = new DataSourceService(config.getName(), regCenter); + listenerManager = new ListenerFactory(config.getName(), regCenter); + } + + private RegistryCenter createRegistryCenter(final RegistryCenterConfiguration regCenterConfig) { + Preconditions.checkNotNull(regCenterConfig, "Registry center configuration cannot be null."); + if (regCenterConfig instanceof ZookeeperConfiguration) { + return getZookeeperRegistryCenter((ZookeeperConfiguration) regCenterConfig); + } + if (regCenterConfig instanceof EtcdConfiguration) { + return new EtcdRegistryCenter((EtcdConfiguration) regCenterConfig); + } + throw new UnsupportedOperationException(regCenterConfig.getClass().getName()); + } + + private RegistryCenter getZookeeperRegistryCenter(final ZookeeperConfiguration regCenterConfig){ + if (regCenterConfig.isUseNative()){ + return new NewZookeeperRegistryCenter(regCenterConfig); + } else { + return new ZookeeperRegistryCenter(regCenterConfig); + } + } + + /** + * Initialize for sharding orchestration. + * + * @param dataSourceMap data source map + * @param shardingRuleConfig sharding rule configuration + * @param configMap config map + * @param props sharding properties + * @param shardingDataSource sharding data source + */ + public void init(final Map dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig, + final Map configMap, final Properties props, final ShardingDataSource shardingDataSource) { + if (shardingRuleConfig.getMasterSlaveRuleConfigs().isEmpty()) { + reviseShardingRuleConfigurationForMasterSlave(dataSourceMap, shardingRuleConfig); + } + configService.persistShardingConfiguration(getActualDataSourceMapForMasterSlave(dataSourceMap), shardingRuleConfig, configMap, props, isOverwrite); + instanceStateService.persistShardingInstanceOnline(); + dataSourceService.persistDataSourcesNode(); + listenerManager.initShardingListeners(shardingDataSource); + } + + /** + * Initialize for master-slave orchestration. + * + * @param dataSourceMap data source map + * @param masterSlaveRuleConfig master-slave rule configuration + * @param configMap config map + * @param masterSlaveDataSource master-slave source + */ + public void init(final Map dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, + final Map configMap, final MasterSlaveDataSource masterSlaveDataSource) { + configService.persistMasterSlaveConfiguration(dataSourceMap, masterSlaveRuleConfig, configMap, isOverwrite); + instanceStateService.persistMasterSlaveInstanceOnline(); + dataSourceService.persistDataSourcesNode(); + listenerManager.initMasterSlaveListeners(masterSlaveDataSource); + } + + private void reviseShardingRuleConfigurationForMasterSlave(final Map dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig) { + for (Entry entry : dataSourceMap.entrySet()) { + if (entry.getValue() instanceof MasterSlaveDataSource) { + MasterSlaveDataSource masterSlaveDataSource = (MasterSlaveDataSource) entry.getValue(); + shardingRuleConfig.getMasterSlaveRuleConfigs().add(getMasterSlaveRuleConfiguration(masterSlaveDataSource)); + } + } + } + + private Map getActualDataSourceMapForMasterSlave(final Map dataSourceMap) { + Map result = new LinkedHashMap<>(); + for (Entry entry : dataSourceMap.entrySet()) { + if (entry.getValue() instanceof MasterSlaveDataSource) { + MasterSlaveDataSource masterSlaveDataSource = (MasterSlaveDataSource) entry.getValue(); + result.putAll(masterSlaveDataSource.getAllDataSources()); + } else { + result.put(entry.getKey(), entry.getValue()); + } + } + return result; + } + + private MasterSlaveRuleConfiguration getMasterSlaveRuleConfiguration(final MasterSlaveDataSource masterSlaveDataSource) { + return new MasterSlaveRuleConfiguration( + masterSlaveDataSource.getMasterSlaveRule().getName(), masterSlaveDataSource.getMasterSlaveRule().getMasterDataSourceName(), + masterSlaveDataSource.getMasterSlaveRule().getSlaveDataSourceNames(), masterSlaveDataSource.getMasterSlaveRule().getLoadBalanceAlgorithm()); + } + + @Override + public void close() { + try { + regCenter.close(); + // CHECKSTYLE:OFF + } catch (final Exception ex) { + // CHECKSTYLE:ON + log.warn("RegCenter exception for: {}", ex.getMessage()); + } + } +} diff --git a/src/main/resources/code/ZookeeperConfiguration.java b/src/main/resources/code/ZookeeperConfiguration.java new file mode 100644 index 0000000000000..9b9794962fa4a --- /dev/null +++ b/src/main/resources/code/ZookeeperConfiguration.java @@ -0,0 +1,83 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.zookeeper; + +import io.shardingsphere.jdbc.orchestration.reg.api.RegistryCenterConfiguration; +import lombok.Getter; +import lombok.Setter; + +/** + * Zookeeper based registry center configuration. + * + * @author zhangliang + */ +@Getter +@Setter +public final class ZookeeperConfiguration implements RegistryCenterConfiguration { + + /** + * Zookeeper server list. + * + *

Include ip address and port, multiple servers split by comma. Etc: {@code host1:2181,host2:2181}

+ */ + private String serverLists; + + /** + * Namespace of zookeeper. + */ + private String namespace; + + /** + * Base sleep time milliseconds. + */ + private int baseSleepTimeMilliseconds = 1000; + + /** + * Max sleep time milliseconds. + */ + private int maxSleepTimeMilliseconds = 3000; + + /** + * Max retries. + */ + private int maxRetries = 3; + + /** + * Session timeout milliseconds. + */ + private int sessionTimeoutMilliseconds; + + /** + * Connection timeout milliseconds. + */ + private int connectionTimeoutMilliseconds; + + /** + * Digest for zookeeper. + * + *

Default is not need digest

+ */ + private String digest; + + /** + * RegCenter for zookeeper. + * + *

Default is ZookeeperRegistryCenter

+ */ + private boolean useNative; +} diff --git a/src/main/resources/code/newzk/NewZookeeperRegistryCenter.java b/src/main/resources/code/newzk/NewZookeeperRegistryCenter.java new file mode 100644 index 0000000000000..7754d1999057a --- /dev/null +++ b/src/main/resources/code/newzk/NewZookeeperRegistryCenter.java @@ -0,0 +1,254 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk; + +import com.google.common.base.Charsets; +import com.google.common.base.Strings; +import io.shardingsphere.jdbc.orchestration.reg.api.RegistryCenter; +import io.shardingsphere.jdbc.orchestration.reg.exception.RegExceptionHandler; +import io.shardingsphere.jdbc.orchestration.reg.listener.DataChangedEvent; +import io.shardingsphere.jdbc.orchestration.reg.listener.EventListener; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.StrategyType; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.ZooDefs; + +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.TimeUnit; + +/** + * Zookeeper native based registry center. + * + * @author lidongbo + */ +public final class NewZookeeperRegistryCenter implements RegistryCenter { + + private final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IClient client; + + private final Map caches = new HashMap<>(); + + public NewZookeeperRegistryCenter(final io.shardingsphere.jdbc.orchestration.reg.zookeeper.ZookeeperConfiguration zkConfig) { + io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory creator = buildCreator(zkConfig); + client = initClient(creator, zkConfig); + } + + private io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory buildCreator(final io.shardingsphere.jdbc.orchestration.reg.zookeeper.ZookeeperConfiguration zkConfig) { + io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory creator = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory(); + creator.setClientNamespace(zkConfig.getNamespace()) + .newClient(zkConfig.getServerLists(), zkConfig.getSessionTimeoutMilliseconds()) + .setRetryPolicy(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.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); + } + return creator; + } + + private io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IClient initClient(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory creator, final io.shardingsphere.jdbc.orchestration.reg.zookeeper.ZookeeperConfiguration zkConfig) { + io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IClient newClient = null; + try { + newClient = creator.start(); + // block, slowly + if (!newClient.blockUntilConnected(zkConfig.getMaxSleepTimeMilliseconds() * zkConfig.getMaxRetries(), TimeUnit.MILLISECONDS)) { + newClient.close(); + throw new KeeperException.OperationTimeoutException(); + } + newClient.useExecStrategy(StrategyType.SYNC_RETRY); + // CHECKSTYLE:OFF + } catch (Exception e) { + // CHECKSTYLE:ON + RegExceptionHandler.handleException(e); + } + return newClient; + } + + @Override + public String get(final String key) { + io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathTree cache = findTreeCache(key); + if (null == cache) { + return getDirectly(key); + } + byte[] resultInCache = cache.getValue(key); + if (null != resultInCache) { + return null == resultInCache ? null : new String(resultInCache, Charsets.UTF_8); + } + return getDirectly(key); + } + + private io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathTree findTreeCache(final String key) { + for (Entry entry : caches.entrySet()) { + if (key.startsWith(entry.getKey())) { + return entry.getValue(); + } + } + return null; + } + + @Override + public String getDirectly(final String key) { + try { + return new String(client.getData(key), Charsets.UTF_8); + // CHECKSTYLE:OFF + } catch (final Exception ex) { + // CHECKSTYLE:ON + RegExceptionHandler.handleException(ex); + return null; + } + } + + @Override + public boolean isExisted(final String key) { + try { + return client.checkExists(key); + // CHECKSTYLE:OFF + } catch (final Exception ex) { + // CHECKSTYLE:ON + RegExceptionHandler.handleException(ex); + return false; + } + } + + @Override + public List getChildrenKeys(final String key) { + try { + List result = client.getChildren(key); + Collections.sort(result, new Comparator() { + + @Override + public int compare(final String o1, final String o2) { + return o2.compareTo(o1); + } + }); + return result; + // CHECKSTYLE:OFF + } catch (final Exception ex) { + // CHECKSTYLE:ON + RegExceptionHandler.handleException(ex); + return Collections.emptyList(); + } + } + + @Override + public void persist(final String key, final String value) { + try { + if (!isExisted(key)) { + client.createAllNeedPath(key, value, CreateMode.PERSISTENT); + } else { + update(key, value); + } + // CHECKSTYLE:OFF + } catch (final Exception ex) { + // CHECKSTYLE:ON + RegExceptionHandler.handleException(ex); + } + } + + @Override + public void update(final String key, final String value) { + try { + client.transaction().check(key, -1).setData(key, value.getBytes(Charsets.UTF_8)).commit(); + // CHECKSTYLE:OFF + } catch (final Exception ex) { + // CHECKSTYLE:ON + RegExceptionHandler.handleException(ex); + } + } + + @Override + public void persistEphemeral(final String key, final String value) { + try { + if (isExisted(key)) { + client.deleteAllChildren(key); + } + client.createAllNeedPath(key, value, CreateMode.EPHEMERAL); + // CHECKSTYLE:OFF + } catch (final Exception ex) { + // CHECKSTYLE:ON + RegExceptionHandler.handleException(ex); + } + } + + @Override + public void watch(final String key, final EventListener eventListener) { + final String path = key + "/"; + if (!caches.containsKey(path)) { + addCacheData(key); + } + io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathTree cache = caches.get(path); + cache.watch(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener() { + @Override + public void process(final WatchedEvent event) { + if (!io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.StringUtil.isNullOrBlank(event.getPath())) { + eventListener.onChange(new DataChangedEvent(getEventType(event), event.getPath(), getWithoutCache(event.getPath()))); + } + } + + private DataChangedEvent.Type getEventType(final WatchedEvent event) { + switch (event.getType()) { + case NodeDataChanged: + case NodeChildrenChanged: + return DataChangedEvent.Type.UPDATED; + case NodeDeleted: + return DataChangedEvent.Type.DELETED; + default: + return DataChangedEvent.Type.IGNORED; + } + } + }); + } + + private synchronized String getWithoutCache(final String key) { + try { + client.useExecStrategy(StrategyType.USUAL); + byte[] data = client.getData(key); + client.useExecStrategy(StrategyType.SYNC_RETRY); + return null == data ? null : new String(data, Charsets.UTF_8); + // CHECKSTYLE:OFF + } catch (final Exception ex) { + // CHECKSTYLE:ON + RegExceptionHandler.handleException(ex); + return null; + } + } + + private void addCacheData(final String cachePath) { + io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathTree cache = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathTree(cachePath, client); + try { + cache.load(); + cache.watch(); + // CHECKSTYLE:OFF + } catch (final Exception ex) { + // CHECKSTYLE:ON + RegExceptionHandler.handleException(ex); + } + caches.put(cachePath + "/", cache); + } + + @Override + public void close() { + for (Entry each : caches.entrySet()) { + each.getValue().close(); + } + client.close(); + } +} diff --git a/src/main/resources/code/newzk/client/action/Callback.java b/src/main/resources/code/newzk/client/action/Callback.java new file mode 100644 index 0000000000000..ee513a8f697f0 --- /dev/null +++ b/src/main/resources/code/newzk/client/action/Callback.java @@ -0,0 +1,29 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.action; + +/* + * @author lidongbo + */ +public interface Callback { + + /** + * process callback result. + */ + void processResult(); +} diff --git a/src/main/resources/code/newzk/client/action/IAction.java b/src/main/resources/code/newzk/client/action/IAction.java new file mode 100644 index 0000000000000..f7929220b4572 --- /dev/null +++ b/src/main/resources/code/newzk/client/action/IAction.java @@ -0,0 +1,134 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.action; + +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; + +import java.util.List; + +/* + * @author lidongbo + */ +public interface IAction { + + /** + * get string type data. + * + * @param key key + * @return data String + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + String getDataString(String key) throws KeeperException, InterruptedException; + + /** + * get string type data. + * + * @param key key + * @return data + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + byte[] getData(String key) throws KeeperException, InterruptedException; + + /** + * get string type data. + * + * @param key key + * @param callback callback + * @param ctx ctx + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + void getData(String key, AsyncCallback.DataCallback callback, Object ctx) throws KeeperException, InterruptedException; + + /** + * check exist. + * + * @param key key + * @return exist + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + boolean checkExists(String key) throws KeeperException, InterruptedException; + + /** + * check exist. + * + * @param key key + * @param watcher watcher + * @return exist + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + boolean checkExists(String key, Watcher watcher) throws KeeperException, InterruptedException; + + /** + * get children's keys. + * + * @param key key + * @return children keys + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + List getChildren(String key) throws KeeperException, InterruptedException; + + /** + * only create target node. + * + * @param key key + * @param value value + * @param createMode createMode + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + void createCurrentOnly(String key, String value, CreateMode createMode) throws KeeperException, InterruptedException; + + /** + * update. + * + * @param key key + * @param value value + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + void update(String key, String value) throws KeeperException, InterruptedException; + + /** + * only delete target node.. + * + * @param key key + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + void deleteOnlyCurrent(String key) throws KeeperException, InterruptedException; + + /** + * only delete target node.. + * + * @param key key + * @param callback callback + * @param ctx ctx + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + void deleteOnlyCurrent(String key, AsyncCallback.VoidCallback callback, Object ctx) throws KeeperException, InterruptedException; +} diff --git a/src/main/resources/code/newzk/client/action/IClient.java b/src/main/resources/code/newzk/client/action/IClient.java new file mode 100644 index 0000000000000..aad9963578780 --- /dev/null +++ b/src/main/resources/code/newzk/client/action/IClient.java @@ -0,0 +1,93 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.action; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.StrategyType; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +/* + * client api + * + * @author lidongbo + */ +public interface IClient extends io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IAction, IGroupAction { + + /** + * start. + * + * @throws IOException IO Exception + * @throws InterruptedException InterruptedException + */ + void start() 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. + */ + void close(); + + /** + * register watcher. + * + * @param key key + * @param listener listener + */ + void registerWatch(String key, io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener listener); + + /** + * unregister watcher. + * + * @param key key + */ + void unregisterWatch(String key); + + /** + * choice exec strategy. + * + * @param strategyType strategyType + */ + void useExecStrategy(StrategyType strategyType); + + /** + * create transaction. + * + * @return ZKTransaction + */ + io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction transaction(); + /* + void createNamespace(); + void deleteNamespace(); + + Watcher registerWatch(Listener listener); + void setRootNode(String namespace); + + void setAuthorities(String scheme, byte[] auth); + ZooKeeper getZooKeeper(); + */ +} diff --git a/src/main/resources/code/newzk/client/action/IExecStrategy.java b/src/main/resources/code/newzk/client/action/IExecStrategy.java new file mode 100644 index 0000000000000..89ef9dc731ec6 --- /dev/null +++ b/src/main/resources/code/newzk/client/action/IExecStrategy.java @@ -0,0 +1,31 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.action; + +/* + * @author lidongbo + */ +public interface IExecStrategy extends IAction, IGroupAction { + + /** + * get provider. + * + * @return IProvider + */ + IProvider getProvider(); +} diff --git a/src/main/resources/code/newzk/client/action/IGroupAction.java b/src/main/resources/code/newzk/client/action/IGroupAction.java new file mode 100644 index 0000000000000..87891b14c727b --- /dev/null +++ b/src/main/resources/code/newzk/client/action/IGroupAction.java @@ -0,0 +1,56 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.action; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; + +/* + * @author lidongbo + */ +public interface IGroupAction { + + /** + * create target node and all need created. + * + * @param key key + * @param value value + * @param createMode createMode + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + void createAllNeedPath(String key, String value, CreateMode createMode) throws KeeperException, InterruptedException; + + /** + * delete target node and children nodes. + * + * @param key key + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + void deleteAllChildren(String key) throws KeeperException, InterruptedException; + + /** + * delete the current node with force and delete the super node whose only child node is current node recursively. + * + * @param key key + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + void deleteCurrentBranch(String key) throws KeeperException, InterruptedException; +} diff --git a/src/main/resources/code/newzk/client/action/IProvider.java b/src/main/resources/code/newzk/client/action/IProvider.java new file mode 100644 index 0000000000000..93f49a36c7e2a --- /dev/null +++ b/src/main/resources/code/newzk/client/action/IProvider.java @@ -0,0 +1,189 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.action; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.election.LeaderElection; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; + +import java.util.List; +import java.util.Stack; + +/* + * provider api + * + * @author lidongbo + */ +public interface IProvider { + + /** + * get string type data. + * + * @param key key + * @return data String + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + String getDataString(String key) throws KeeperException, InterruptedException; + + /** + * get string type data. + * + * @param key key + * @return data + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + byte[] getData(String key) throws KeeperException, InterruptedException; + + /** + * get string type data. + * + * @param key key + * @param callback callback + * @param ctx ctx + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + void getData(String key, AsyncCallback.DataCallback callback, Object ctx) throws KeeperException, InterruptedException; + + /** + * check exist. + * + * @param key key + * @return exist + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + boolean exists(String key) throws KeeperException, InterruptedException; + + /** + * check exist. + * + * @param key key + * @param watcher watcher + * @return exist + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + boolean exists(String key, Watcher watcher) throws KeeperException, InterruptedException; + + /** + * get children's keys. + * + * @param key key + * @return exist + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + List getChildren(String key) throws KeeperException, InterruptedException; + + /** + * only create target node. + * + * @param key key + * @param value value + * @param createMode createMode + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + void create(String key, String value, CreateMode createMode) throws KeeperException, InterruptedException; + + /** + * update. + * + * @param key key + * @param value value + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + void update(String key, String value) throws KeeperException, InterruptedException; + + /** + * only delete target node.. + * + * @param key key + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + void delete(String key) throws KeeperException, InterruptedException; + + /** + * only delete target node.. + * + * @param key key + * @param callback callback + * @param ctx ctx + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + void delete(String key, AsyncCallback.VoidCallback callback, Object ctx) throws KeeperException, InterruptedException; + + /** + * get real path with root. + * + * @param path path + * @return real path + */ + String getRealPath(String path); + + /** + * get path nodes that needed create. + * + * @param key key + * @return all path nodes + */ + List getNecessaryPaths(String key); + + /** + * get path nodes that needed delete. + * + * @param key key + * @return all path nodes + */ + Stack getDeletingPaths(String key); + + /** + * contention exec. + * + * @param election election + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + void executeContention(LeaderElection election) throws KeeperException, InterruptedException; + + /** + * only create target node. + * + * @param key key + * @param value value + * @param createMode createMode + * @param transaction transaction + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + void createInTransaction(String key, String value, CreateMode createMode, ZKTransaction transaction) throws KeeperException, InterruptedException; + + /** + * reset connection. + */ + void resetConnection(); +} diff --git a/src/main/resources/code/newzk/client/cache/CacheStrategy.java b/src/main/resources/code/newzk/client/cache/CacheStrategy.java new file mode 100644 index 0000000000000..9c34acd8de441 --- /dev/null +++ b/src/main/resources/code/newzk/client/cache/CacheStrategy.java @@ -0,0 +1,27 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache; + +/* + * @author lidongbo + */ +public enum CacheStrategy { + NONE, + WATCH, + ALL +} diff --git a/src/main/resources/code/newzk/client/cache/PathNode.java b/src/main/resources/code/newzk/client/cache/PathNode.java new file mode 100644 index 0000000000000..b366404e78cb0 --- /dev/null +++ b/src/main/resources/code/newzk/client/cache/PathNode.java @@ -0,0 +1,112 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import lombok.Getter; +import lombok.Setter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/* + * zookeeper node cache + * + * @author lidongbo + */ +public class PathNode { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathNode.class); + + private final Map children = new ConcurrentHashMap<>(); + + private final String nodeKey; + + @Getter + @Setter + private byte[] value; + + PathNode(final String key) { + this(key, Constants.RELEASE_VALUE); + } + + PathNode(final String key, final byte[] value) { + this.nodeKey = key; + this.value = value; + } + + /** + * get children. + * + * @return children + */ + public Map getChildren() { + return children; + } + + /** + * get key. + * + * @return node key + */ + public String getKey() { + return this.nodeKey; + } + + /** + * attach child node. + * + * @param node node + */ + public void attachChild(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathNode node) { + this.children.put(node.nodeKey, node); + } + + io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathNode set(final Iterator iterator, final String value) { + String key = iterator.next(); + LOGGER.debug("PathNode set:{},value:{}", key, value); + io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathNode node = children.get(key); + if (node == null) { + LOGGER.debug("set children haven't:{}", key); + node = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathNode(key); + children.put(key, node); + } + if (iterator.hasNext()) { + node.set(iterator, value); + } else { + node.setValue(value.getBytes(Constants.UTF_8)); + } + return node; + } + + io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathNode get(final Iterator iterator) { + String key = iterator.next(); + LOGGER.debug("get:{}", key); + io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathNode node = children.get(key); + if (node == null) { + LOGGER.debug("get children haven't:{}", key); + return null; + } + if (iterator.hasNext()) { + return node.get(iterator); + } + return node; + } +} diff --git a/src/main/resources/code/newzk/client/cache/PathStatus.java b/src/main/resources/code/newzk/client/cache/PathStatus.java new file mode 100644 index 0000000000000..56a23f198ae8f --- /dev/null +++ b/src/main/resources/code/newzk/client/cache/PathStatus.java @@ -0,0 +1,26 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache; + +/* + * @author lidongbo + */ +public enum PathStatus { + CHANGING, + RELEASE +} diff --git a/src/main/resources/code/newzk/client/cache/PathTree.java b/src/main/resources/code/newzk/client/cache/PathTree.java new file mode 100644 index 0000000000000..56d58ccc3648e --- /dev/null +++ b/src/main/resources/code/newzk/client/cache/PathTree.java @@ -0,0 +1,414 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IClient; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil; +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; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.locks.ReentrantLock; + +/* + * zookeeper cache tree + * + * @author lidongbo + */ +public final class PathTree { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathTree.class); + + private final transient ReentrantLock lock = new ReentrantLock(); + + private final AtomicReference rootNode = new AtomicReference<>(); + + private boolean executorStart; + + private ScheduledExecutorService cacheService; + + private final IClient client; + + private final IProvider provider; + + @Getter + @Setter + private io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathStatus status; + + private boolean closed; + + public PathTree(final String root, final IClient client) { + this.rootNode.set(new PathNode(root)); + this.status = io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathStatus.RELEASE; + this.client = client; + // todo It looks unpleasant + this.provider = ((io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.UsualClient) client).getStrategy().getProvider(); + } + + /** + * load data. + * + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + public void load() throws KeeperException, InterruptedException { + final ReentrantLock lock = this.lock; + lock.lockInterruptibly(); + if (closed) { + return; + } + try { + if (status == status.RELEASE) { + LOGGER.debug("loading status:{}", status); + this.setStatus(io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathStatus.CHANGING); + + PathNode newRoot = new PathNode(rootNode.get().getKey()); + List children = provider.getChildren(rootNode.get().getKey()); + children.remove(PathUtil.getRealPath(rootNode.get().getKey(), Constants.CHANGING_KEY)); + this.attechIntoNode(children, newRoot); + rootNode.set(newRoot); + + this.setStatus(io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathStatus.RELEASE); +// watch(); + LOGGER.debug("loading release:{}", status); + } else { + LOGGER.info("loading but cache status not release"); + try { + Thread.sleep(10L); + } catch (InterruptedException e) { + LOGGER.error("loading sleep error:{}", e.getMessage(), e); + } + load(); + } + } finally { + lock.unlock(); + } + } + + private void attechIntoNode(final List children, final PathNode pathNode) throws KeeperException, InterruptedException { + if (closed) { + return; + } + LOGGER.debug("attechIntoNode children:{}", children); + if (children.isEmpty()) { + LOGGER.info("attechIntoNode there are no children"); + return; + } + for (String child : children) { + String childPath = PathUtil.getRealPath(pathNode.getKey(), child); + PathNode current = new PathNode(PathUtil.checkPath(child), provider.getData(childPath)); + pathNode.attachChild(current); + List subs = provider.getChildren(childPath); + this.attechIntoNode(subs, current); + } + } + + /** + * start thread pool period load data. + * + * @param period period + */ + public void refreshPeriodic(final long period) { + final ReentrantLock lock = this.lock; + lock.lock(); + if (closed) { + return; + } + try { + if (executorStart) { + throw new IllegalArgumentException("period already set"); + } + long threadPeriod = period; + if (threadPeriod < 1) { + threadPeriod = Constants.THREAD_PERIOD; + } + LOGGER.debug("refreshPeriodic:{}", period); + cacheService = Executors.newSingleThreadScheduledExecutor(); + cacheService.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + LOGGER.debug("cacheService run:{}", getStatus()); + if (io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathStatus.RELEASE == getStatus()) { + try { + load(); + // CHECKSTYLE:OFF + } catch (Exception e) { + // CHECKSTYLE:ON + LOGGER.error(e.getMessage(), e); + } + } + } + }, Constants.THREAD_INITIAL_DELAY, threadPeriod, TimeUnit.MILLISECONDS); + executorStart = true; + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + @Override + public void run() { + stopRefresh(); + } + })); + } finally { + lock.unlock(); + } + } + + /** + * stop thread pool period load data. + */ + public void stopRefresh() { + cacheService.shutdown(); + executorStart = false; + LOGGER.debug("stopRefresh"); + } + + /** + * watch data change. + */ + public void watch() { + watch(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener(rootNode.get().getKey()) { + @Override + public void process(final WatchedEvent event) { + String path = event.getPath(); + LOGGER.debug("PathTree Watch event:{}", event.toString()); + switch (event.getType()) { + case NodeCreated: + case NodeDataChanged: + case NodeChildrenChanged: + processNodeChange(path); + break; + case NodeDeleted: + delete(path); + break; + default: + break; + } + } + }); + } + + /** + * watch data change. + * + * @param listener listener + */ + public void watch(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener listener) { + if (closed) { + return; + } + final String key = listener.getKey(); + LOGGER.debug("PathTree Watch:{}", key); + client.registerWatch(rootNode.get().getKey(), listener); + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + @Override + public void run() { + LOGGER.debug("PathTree Unregister Watch:{}", key); + client.unregisterWatch(key); + } + })); + } + + private void processNodeChange(final String path) { + try { + String value = Constants.NOTHING_VALUE; + if (!path.equals(getRootNode().getKey())) { + value = provider.getDataString(path); + } + put(path, value); + // CHECKSTYLE:OFF + } catch (Exception e) { + // CHECKSTYLE:ON + LOGGER.error("PathTree put error : " + e.getMessage()); + } + } + + /** + * get root node. + * + * @return root node + */ + public PathNode getRootNode() { + return rootNode.get(); + } + + /** + * get node value. + * + * @param path path + * @return node data + */ + public byte[] getValue(final String path) { + if (closed) { + return null; + } + PathNode node = get(path); + return null == node ? null : node.getValue(); + } + + private Iterator keyIterator(final String path) { + List nodes = PathUtil.getShortPathNodes(path); + LOGGER.debug("keyIterator path{},nodes:{}", path, nodes); + Iterator iterator = nodes.iterator(); + // root + iterator.next(); + return iterator; + } + + /** + * get children. + * + * @param path path + * @return children + */ + public List getChildren(final String path) { + if (closed) { + return null; + } + PathNode node = get(path); + List result = new ArrayList<>(); + if (node == null) { + LOGGER.info("getChildren null"); + return result; + } + if (node.getChildren().isEmpty()) { + LOGGER.info("getChildren no child"); + return result; + } + Iterator children = node.getChildren().values().iterator(); + while (children.hasNext()) { + result.add(new String(children.next().getValue())); + } + return result; + } + + private PathNode get(final String path) { + LOGGER.debug("PathTree get:{}", path); + PathUtils.validatePath(path); + if (path.equals(rootNode.get().getKey())) { + return rootNode.get(); + } + Iterator iterator = keyIterator(path); + if (iterator.hasNext()) { + return rootNode.get().get(iterator); + } + LOGGER.debug("{} not exist", path); + return null; + } + + /** + * put node. + * + * @param path path + * @param value value + */ + public void put(final String path, final String value) { + final ReentrantLock lock = this.lock; + lock.lock(); + if (closed) { + return; + } + try { + LOGGER.debug("cache put:{},value:{}", path, value); + PathUtils.validatePath(path); + LOGGER.debug("put status:{}", status); + if (status == status.RELEASE) { + if (path.equals(rootNode.get().getKey())) { + rootNode.set(new PathNode(rootNode.get().getKey(), value.getBytes(Constants.UTF_8))); + return; + } + this.setStatus(io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathStatus.CHANGING); + rootNode.get().set(keyIterator(path), value); + this.setStatus(io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathStatus.RELEASE); + } else { + try { + LOGGER.debug("put but cache status not release"); + Thread.sleep(10L); + } catch (InterruptedException e) { + LOGGER.error("put sleep error:{}", e.getMessage(), e); + } + put(path, value); + } + } finally { + lock.unlock(); + } + } + + /** + * delete node. + * + * @param path path + */ + public void delete(final String path) { + LOGGER.debug("PathTree begin delete:{}", path); + final ReentrantLock lock = this.lock; + lock.lock(); + if (closed) { + return; + } + try { + PathUtils.validatePath(path); +// String prxpath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); + PathNode node = get(path); + node.getChildren().remove(path); + LOGGER.debug("PathTree end delete:{}", path); + } finally { + lock.unlock(); + } + } + + /** + * close. + */ + public void close() { + final ReentrantLock lock = this.lock; + lock.lock(); + this.closed = true; + try { + if (executorStart) { + stopRefresh(); + } + deleteAllChildren(rootNode.get()); + // CHECKSTYLE:OFF + } catch (Exception ee){ + // CHECKSTYLE:ON + LOGGER.warn("PathTree close:{}", ee.getMessage()); + } finally { + lock.unlock(); + } + } + + private void deleteAllChildren(final PathNode node) { + if (node.getChildren().isEmpty()) { + return; + } + for (String one : node.getChildren().keySet()) { + deleteAllChildren(node.getChildren().get(one)); + node.getChildren().remove(one); + } + } +} diff --git a/src/main/resources/code/newzk/client/election/LeaderElection.java b/src/main/resources/code/newzk/client/election/LeaderElection.java new file mode 100644 index 0000000000000..bb87d24f5b984 --- /dev/null +++ b/src/main/resources/code/newzk/client/election/LeaderElection.java @@ -0,0 +1,129 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.election; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/* + * competition of node write permission + * It is not recommended to be used as a global variable. + * + * @author lidongbo + */ +public abstract class LeaderElection { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.election.LeaderElection.class); + + private boolean done; + + private int retryCount; + + public LeaderElection() { + retryCount = Constants.NODE_ELECTION_RETRY; + } + + private boolean contend(final String node, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener listener) throws KeeperException, InterruptedException { + boolean success = false; + try { + // todo EPHEMERAL_SEQUENTIAL check index value + provider.create(node, Constants.CLIENT_ID, CreateMode.EPHEMERAL); + success = true; + } catch (KeeperException.NodeExistsException e) { + LOGGER.info("contend not success"); + // TODO or changing_key node value == current client id + provider.exists(node, io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.WatcherCreator.deleteWatcher(listener)); + } + return success; + } + + /** + * listener will be register when the contention of the path is unsuccessful. + * + * @param nodeBeContend nodeBeContend + * @param provider provider + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + public void executeContention(final String nodeBeContend, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider) throws KeeperException, InterruptedException { + boolean canBegin; + final String realNode = provider.getRealPath(nodeBeContend); + final String contendNode = io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil.getRealPath(realNode, Constants.CHANGING_KEY); + canBegin = this.contend(contendNode, provider, new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener(contendNode) { + @Override + public void process(final WatchedEvent event) { + try { + retryCount--; + if (retryCount < 0) { + LOGGER.info("Election node exceed retry count"); + return; + } + executeContention(realNode, provider); + // CHECKSTYLE:OFF + } catch (Exception ee){ + // CHECKSTYLE:ON + LOGGER.error("Listener Exception executeContention:{}", ee.getMessage(), ee); + } + } + }); + + if (canBegin) { + try { + action(); + done = true; + callback(); + // CHECKSTYLE:OFF + } catch (Exception e){ + // CHECKSTYLE:ON + LOGGER.error("action Exception executeContention:{}", e.getMessage(), e); + } + provider.delete(contendNode); + } + } + + /** + * wait done. + */ + public void waitDone() { + while (!done) { + try { + Thread.sleep(10L); + } catch (InterruptedException e) { + LOGGER.error("waitDone:{}", e.getMessage(), e); + } + } + } + + /** + * contend exec. + * + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + public abstract void action() throws KeeperException, InterruptedException; + + /** + * callback. + */ + public void callback() { + + } +} diff --git a/src/main/resources/code/newzk/client/retry/AsyncRetryCenter.java b/src/main/resources/code/newzk/client/retry/AsyncRetryCenter.java new file mode 100644 index 0000000000000..dab4a771083d9 --- /dev/null +++ b/src/main/resources/code/newzk/client/retry/AsyncRetryCenter.java @@ -0,0 +1,84 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.DelayQueue; + +/* + * async retry center + * + * @author lidongbo + */ +public enum AsyncRetryCenter { + INSTANCE; + + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.class); + + private final DelayQueue queue = new DelayQueue<>(); + + private final RetryThread retryThread = new RetryThread(queue); + + private boolean started; + + private DelayRetryPolicy delayRetryPolicy; + + /** + * init. + * + * @param delayRetryPolicy delayRetryPolicy + */ + public void init(final DelayRetryPolicy delayRetryPolicy) { + LOGGER.debug("delayRetryPolicy init"); + if (delayRetryPolicy == null) { + LOGGER.warn("delayRetryPolicy is null and auto init with DelayRetryPolicy.newNoInitDelayPolicy"); + this.delayRetryPolicy = DelayRetryPolicy.newNoInitDelayPolicy(); + } + this.delayRetryPolicy = delayRetryPolicy; + } + + /** + * start. + */ + public synchronized void start() { + if (started) { + return; + } + retryThread.setName("retry-thread"); + retryThread.start(); + this.started = true; + } + + /** + * add async operation. + * + * @param operation operation + */ + public void add(final BaseOperation operation) { + if (delayRetryPolicy == null) { + LOGGER.warn("delayRetryPolicy no init and auto init with DelayRetryPolicy.newNoInitDelayPolicy"); + delayRetryPolicy = DelayRetryPolicy.newNoInitDelayPolicy(); + } + operation.setDelayPolicyExecutor(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayPolicyExecutor(delayRetryPolicy)); + queue.offer(operation); + LOGGER.debug("enqueue operation:{}", operation.toString()); + } +} diff --git a/src/main/resources/code/newzk/client/retry/DelayPolicyExecutor.java b/src/main/resources/code/newzk/client/retry/DelayPolicyExecutor.java new file mode 100644 index 0000000000000..484a10de86cf0 --- /dev/null +++ b/src/main/resources/code/newzk/client/retry/DelayPolicyExecutor.java @@ -0,0 +1,83 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Random; + +/* + * delay policy executor + * + * @author lidongbo + */ +public class DelayPolicyExecutor { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayPolicyExecutor.class); + + private final DelayRetryPolicy delayRetryPolicy; + + private final Random random; + + private int executeCount; + + private long executeTick; + + public DelayPolicyExecutor() { + this(DelayRetryPolicy.newNoInitDelayPolicy()); + } + + public DelayPolicyExecutor(final DelayRetryPolicy delayRetryPolicy) { + this.delayRetryPolicy = delayRetryPolicy; + this.executeTick = System.currentTimeMillis(); + this.random = new Random(); +// next(); + } + + /** + * has next. + * + * @return has next + */ + public boolean hasNext() { + return executeCount < delayRetryPolicy.getRetryCount(); + } + + /** + * next exec tick. + * + * @return next exec tick + */ + public long getNextTick() { + return executeTick; + } + + /** + * next. + */ + public void next() { + executeCount++; + long sleep = delayRetryPolicy.getBaseDelay() * Math.max(1, this.random.nextInt(1 << delayRetryPolicy.getRetryCount() + 1)); + if (sleep < delayRetryPolicy.getDelayUpperBound()) { + executeTick += sleep; + } else { + executeTick += delayRetryPolicy.getDelayUpperBound(); + } + LOGGER.debug("next executeCount:{}, executeTick:{}", executeCount, executeTick); + } +} diff --git a/src/main/resources/code/newzk/client/retry/DelayRetryPolicy.java b/src/main/resources/code/newzk/client/retry/DelayRetryPolicy.java new file mode 100644 index 0000000000000..95e9880750533 --- /dev/null +++ b/src/main/resources/code/newzk/client/retry/DelayRetryPolicy.java @@ -0,0 +1,64 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry; + +import lombok.Getter; + +/* + * delay policy + * + * @author lidongbo + */ +public class DelayRetryPolicy { + private static final long BASE_DELAY = 10; + + private static final int BASE_COUNT = 3; + + private static final int RETRY_COUNT_BOUND = 29; + + @Getter + private final int retryCount; + + @Getter + private final long baseDelay; + + @Getter + private final long delayUpperBound; + + /* + * Millis + */ + public DelayRetryPolicy(final long baseDelay) { + this(RETRY_COUNT_BOUND, baseDelay, Integer.MAX_VALUE); + } + + public DelayRetryPolicy(final int retryCount, final long baseDelay, final long delayUpperBound) { + this.retryCount = retryCount; + this.baseDelay = baseDelay; + this.delayUpperBound = delayUpperBound; + } + + /** + * default DelayPolicy. + * + * @return DelayPolicy + */ + public static io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy newNoInitDelayPolicy() { + return new io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy(BASE_COUNT, BASE_DELAY, Integer.MAX_VALUE); + } +} diff --git a/src/main/resources/code/newzk/client/retry/RetryThread.java b/src/main/resources/code/newzk/client/retry/RetryThread.java new file mode 100644 index 0000000000000..80968eace9b07 --- /dev/null +++ b/src/main/resources/code/newzk/client/retry/RetryThread.java @@ -0,0 +1,117 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.DelayQueue; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +/* + * async retry + * + * @author lidongbo + */ +public class RetryThread extends Thread { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.RetryThread.class); + + private final int corePoolSize = Runtime.getRuntime().availableProcessors(); + + private final ThreadPoolExecutor retryExecutor; + + private final int maximumPoolSize = corePoolSize; + + private final long keepAliveTime = 0; + + private final int closeDelay = 60; + + private final DelayQueue queue; + + public RetryThread(final DelayQueue queue) { + this.queue = queue; + retryExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(10), new ThreadFactory() { + private final AtomicInteger threadIndex = new AtomicInteger(0); + @Override + public Thread newThread(final Runnable r) { + Thread thread = new Thread(r); + thread.setDaemon(true); + thread.setName("zk-retry-" + threadIndex.incrementAndGet()); + LOGGER.debug("new thread:{}", thread.getName()); + return thread; + } + }); + addDelayedShutdownHook(retryExecutor, closeDelay, TimeUnit.SECONDS); + } + + @Override + public void run() { + LOGGER.debug("RetryThread start"); + for (;;) { + final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation operation; + try { + operation = queue.take(); + LOGGER.debug("take operation:{}", operation.toString()); + } catch (InterruptedException e) { + LOGGER.error("retry interrupt e:{}", e.getMessage()); + continue; + } + retryExecutor.submit(new Runnable() { + @Override + public void run() { + boolean result; + try { + result = operation.executeOperation(); + // CHECKSTYLE:OFF + } catch (Exception e) { + // CHECKSTYLE:ON + result = false; + LOGGER.error("retry disrupt operation:{}, e:{}", operation.toString(), e.getMessage()); + } + if (result) { + queue.offer(operation); + LOGGER.debug("enqueue again operation:{}", operation.toString()); + } + } + }); + } + } + + final void addDelayedShutdownHook(final ExecutorService service, final long terminationTimeout, final TimeUnit timeUnit) { + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + try { + LOGGER.debug("AsyncRetryCenter stop"); + queue.clear(); + service.shutdown(); + service.awaitTermination(terminationTimeout, timeUnit); + } catch (InterruptedException ignored) { + // shutting down anyway, just ignore. + } + } + }); + thread.setName("retry shutdown hook"); + Runtime.getRuntime().addShutdownHook(thread); + } +} diff --git a/src/main/resources/code/newzk/client/utility/Constants.java b/src/main/resources/code/newzk/client/utility/Constants.java new file mode 100644 index 0000000000000..a211f51cc76c6 --- /dev/null +++ b/src/main/resources/code/newzk/client/utility/Constants.java @@ -0,0 +1,55 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility; + +import java.nio.charset.Charset; + +/* + * constants + * + * @author lidongbo + */ +public final class Constants { + public static final int VERSION = -1; + + public static final int WAIT = 60 * 1000; + + public static final byte[] NOTHING_DATA = new byte[0]; + + public static final String NOTHING_VALUE = ""; + + public static final Charset UTF_8 = Charset.forName("UTF-8"); + + public static final String PATH_SEPARATOR = "/"; + + public static final String GLOBAL_LISTENER_KEY = "globalListener"; + + public static final byte[] CHANGING_VALUE = new byte[]{'c'}; + + public static final byte[] RELEASE_VALUE = new byte[]{'r'}; + + public static final String CHANGING_KEY = "CHANGING_KEY"; + + public static final long THREAD_PERIOD = 3000L; + + public static final long THREAD_INITIAL_DELAY = 1000L; + + public static final int NODE_ELECTION_RETRY = 3; + + public static final String CLIENT_ID = "1"; +} diff --git a/src/main/resources/code/newzk/client/utility/PathUtil.java b/src/main/resources/code/newzk/client/utility/PathUtil.java new file mode 100644 index 0000000000000..49f5361448535 --- /dev/null +++ b/src/main/resources/code/newzk/client/utility/PathUtil.java @@ -0,0 +1,187 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +/* + * path util + * + * @author lidongbo + */ +public class PathUtil { + + /** + * get real path. + * + * @param root root + * @param path path + * @return real path + */ + public static String getRealPath(final String root, final String path) { + return adjustPath(root, path); + } + + private static String adjustPath(final String root, final String path) { + if (io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.StringUtil.isNullOrBlank(path)) { + throw new IllegalArgumentException("path should have content!"); + } + String rootPath = root; + if (!root.startsWith(Constants.PATH_SEPARATOR)) { + rootPath = Constants.PATH_SEPARATOR + root; + } + String subPath = path; + if (!path.startsWith(Constants.PATH_SEPARATOR)) { + subPath = Constants.PATH_SEPARATOR + path; + } + if (!subPath.startsWith(rootPath)) { + return rootPath + subPath; + } + return path; + } + + /** + * get path nodes, child to root. + * + * @param root root + * @param path path + * @return all path nodes + */ + public static Stack getPathReverseNodes(final String root, final String path) { + String realPath = adjustPath(root, path); + Stack pathStack = new Stack<>(); + int index = 1; + int position = realPath.indexOf(Constants.PATH_SEPARATOR, index); + do { + pathStack.push(realPath.substring(0, position)); + index = position + 1; + position = realPath.indexOf(Constants.PATH_SEPARATOR, index); + } + while (position > -1); + pathStack.push(realPath); + return pathStack; + } + + /** + * get path nodes. + * + * @param root root + * @param path path + * @return all path nodes + */ + public static List getPathOrderNodes(final String root, final String path) { + String realPath = adjustPath(root, path); + List paths = new ArrayList<>(); + int index = 1; + int position = realPath.indexOf(Constants.PATH_SEPARATOR, index); + + do { + paths.add(realPath.substring(0, position)); + index = position + 1; + position = realPath.indexOf(Constants.PATH_SEPARATOR, index); + } + while (position > -1); + paths.add(realPath); + return paths; + } + + /** + * get path nodes. + * + * @param path path + * @return all path nodes + */ + public static List getShortPathNodes(final String path) { + String realPath = checkPath(path); + List paths = new ArrayList<>(); + char[] chars = realPath.toCharArray(); + StringBuilder builder = new StringBuilder(Constants.PATH_SEPARATOR); + for (int i = 1; i < chars.length; i++) { + if (chars[i] == Constants.PATH_SEPARATOR.charAt(0)) { + paths.add(builder.toString()); + builder = new StringBuilder(Constants.PATH_SEPARATOR); + continue; + } + builder.append(chars[i]); + if (i == chars.length - 1) { + paths.add(builder.toString()); + } + } + return paths; + } + + /** + * ignore invalid char and // /./ /../. + * code consult zookeeper + * + * @param key key + * @return real path + * @throws IllegalArgumentException IllegalArgumentException + */ + // CHECKSTYLE:OFF + public static String checkPath(final String key) throws IllegalArgumentException { + // CHECKSTYLE:ON + if (io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.StringUtil.isNullOrBlank(key)) { + throw new IllegalArgumentException("path should not be null"); + } + String path = key; + if (path.charAt(0) != 47 || path.charAt(path.length() - 1) == 47) { + path = Constants.PATH_SEPARATOR + path; + } + + if (path.charAt(path.length() - 1) == 47) { + path = Constants.PATH_SEPARATOR + path; + } + + char previous = 47; + char[] chars = path.toCharArray(); + StringBuilder builder = new StringBuilder(); + builder.append(previous); + + for (int i = 1; i < chars.length; ++i) { + char c = chars[i]; + if (c == 0 || (c == 47 && previous == 47)) { + continue; + } + if (c == 46) { + // ignore /./ /../ + boolean preWarn = previous == 47 || (previous == 46 && chars[i - 2] == 47); + if (previous == 47 && (i + 1 == chars.length || chars[i + 1] == 47)) { + // CHECKSTYLE:OFF + i++; + continue; + } + if ((previous == 46 && chars[i - 2] == 47) && (i + 1 == chars.length || chars[i + 1] == 47)) { + i += 2; + continue; + } + } + + if (c > 0 && c < 31 || c > 127 && c < 159 || c > '\ud800' && c < '\uf8ff' || c > '\ufff0' && c < '\uffff') { + // CHECKSTYLE:ON + continue; + } + + builder.append(c); + previous = c; + } + return builder.toString(); + } +} diff --git a/src/main/resources/code/newzk/client/utility/StringUtil.java b/src/main/resources/code/newzk/client/utility/StringUtil.java new file mode 100644 index 0000000000000..8a31fa14e44ca --- /dev/null +++ b/src/main/resources/code/newzk/client/utility/StringUtil.java @@ -0,0 +1,34 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility; + +/* + * @author lidongbo + */ +public class StringUtil { + + /** + * Null Or Blank. + * + * @param string string + * @return isNullOrBlank + */ + public static boolean isNullOrBlank(final String string) { + return string == null || string.trim().length() == 0; + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/CacheClient.java b/src/main/resources/code/newzk/client/zookeeper/CacheClient.java new file mode 100644 index 0000000000000..62b3cdfbfb9ba --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/CacheClient.java @@ -0,0 +1,137 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.CacheStrategy; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathTree; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseContext; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.List; + +/* + * todo + * @author lidongbo + */ +public final class CacheClient extends UsualClient { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.CacheClient.class); + + private PathTree pathTree; + + CacheClient(final BaseContext context) { + super(context); + } + + @Override + public void start() throws IOException, InterruptedException { + super.start(); + try { + useCacheStrategy(CacheStrategy.WATCH); + } catch (KeeperException e) { + LOGGER.error("CacheClient useCacheStrategy : " + e.getMessage()); + } + } + + @Override + public void close() { + super.close(); + this.pathTree.close(); + } + + //todo put it here? + void useCacheStrategy(final CacheStrategy cacheStrategy) throws KeeperException, InterruptedException { + LOGGER.debug("use cache strategy:{}", cacheStrategy); + switch (cacheStrategy) { + case WATCH: { + pathTree = new PathTree(getRootNode(), this); + pathTree.watch(); + return; + } + case ALL: { + pathTree = loadPathTree(); + pathTree.refreshPeriodic(Constants.THREAD_PERIOD); + return; + } + case NONE: + default: { + return; + } + } + } + + private PathTree loadPathTree() throws KeeperException, InterruptedException { + return loadPathTree(getRootNode()); + } + + private PathTree loadPathTree(final String treeRoot) throws KeeperException, InterruptedException { + PathTree tree = new PathTree(treeRoot, this); + LOGGER.debug("load path tree:{}", treeRoot); + tree.load(); + tree.watch(); + return tree; + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + super.createCurrentOnly(key, value, createMode); + pathTree.put(PathUtil.getRealPath(getRootNode(), key), value); + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + super.deleteOnlyCurrent(key); + pathTree.delete(PathUtil.getRealPath(getRootNode(), key)); + } + + @Override + public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + super.deleteOnlyCurrent(key, callback, ctx); + pathTree.delete(PathUtil.getRealPath(getRootNode(), key)); + } + + @Override + public byte[] getData(final String key) throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(getRootNode(), key); + byte[] data = pathTree.getValue(path); + if (data != null) { + LOGGER.debug("getData cache hit:{}", data); + return data; + } + LOGGER.debug("getData cache not hit:{}", data); + return getStrategy().getData(key); + } + + @Override + public List getChildren(final String key) throws KeeperException, InterruptedException { + String path = PathUtil.getRealPath(getRootNode(), key); + List keys = pathTree.getChildren(path); + if (!keys.isEmpty()) { + LOGGER.debug("getChildren cache hit:{}", keys); + return keys; + } + LOGGER.debug("getChildren cache not hit:{}", keys); + return getStrategy().getChildren(PathUtil.getRealPath(getRootNode(), key)); + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/ClientFactory.java b/src/main/resources/code/newzk/client/zookeeper/ClientFactory.java new file mode 100644 index 0000000000000..32eb05510f327 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/ClientFactory.java @@ -0,0 +1,135 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IClient; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseClientFactory; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.ClientContext; +import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.List; + +/* + * @author lidongbo + */ +public class ClientFactory extends BaseClientFactory { + // private static final String CLIENT_EXCLUSIVE_NODE = "ZKC"; + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory.class); + + private DelayRetryPolicy delayRetryPolicy; + + /** + * create a new client. + * + * @param servers servers + * @param sessionTimeoutMilliseconds sessionTimeoutMilliseconds + * @return ClientFactory this + */ + public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory newClient(final String servers, final int sessionTimeoutMilliseconds) { + int wait = sessionTimeoutMilliseconds; + if (sessionTimeoutMilliseconds == 0) { + wait = Constants.WAIT; + } + setContext(new ClientContext(servers, wait)); + setClient(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.UsualClient(getContext())); + LOGGER.debug("new usual client"); + return this; + } + + /* + * used for create new clients through a existing client + * this client is not perhaps the client + */ + synchronized BaseClientFactory newClientByOriginal(final boolean closeOriginal) { + IClient oldClient = getClient(); + setClient(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.UsualClient(getContext())); + if (closeOriginal) { + oldClient.close(); + } + LOGGER.debug("new usual client by a existing client"); + return this; + } + + io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory newCacheClient(final String servers, final int sessionTimeoutMilliseconds) { + setContext(new ClientContext(servers, sessionTimeoutMilliseconds)); + setClient(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.CacheClient(getContext())); + LOGGER.debug("new cache client"); + return this; + } + + /** + * wait to register global listener. + * + * @param globalListener globalListener + * @return ClientFactory this + */ + public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory watch(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener globalListener) { + setGlobalListener(globalListener); + return this; + } + + /** + * set client namespace. + * + * @param namespace namespace + * @return ClientFactory this + */ + public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory setClientNamespace(final String namespace) { + setNamespace(PathUtil.checkPath(namespace)); + return this; + } + + /** + * authorization. + * + * @param scheme scheme + * @param auth auth + * @param authorities authorities + * @return ClientFactory this + */ + public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory authorization(final String scheme, final byte[] auth, final List authorities) { + setScheme(scheme); + setAuth(auth); + setAuthorities(authorities); + return this; + } + + /** + * set delay retry policy. + * + * @param delayRetryPolicy delayRetryPolicy + * @return ClientFactory this + */ + public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory setRetryPolicy(final DelayRetryPolicy delayRetryPolicy) { + this.delayRetryPolicy = delayRetryPolicy; + return this; + } + + @Override + public IClient start() throws IOException, InterruptedException { + ((ClientContext) getContext()).setDelayRetryPolicy(delayRetryPolicy); + ((ClientContext) getContext()).setClientFactory(this); + return super.start(); + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/UsualClient.java b/src/main/resources/code/newzk/client/zookeeper/UsualClient.java new file mode 100644 index 0000000000000..c680ca2679ae6 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/UsualClient.java @@ -0,0 +1,193 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseClient; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseContext; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.ClientContext; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.StrategyType; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.AsyncRetryStrategy; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.ContentionStrategy; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction; +import lombok.Getter; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/* + * @author lidongbo + */ +public class UsualClient extends BaseClient { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.UsualClient.class); + + private final Map strategies = new ConcurrentHashMap<>(); + + private final boolean watched = true; + + @Getter + private io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IExecStrategy strategy; + + UsualClient(final BaseContext context) { + super(context); + } + + @Override + public void start() throws IOException, InterruptedException { + super.start(); + useExecStrategy(StrategyType.USUAL); + } + + @Override + public void close() { + this.strategies.clear(); + super.close(); + } + + @Override + public synchronized void useExecStrategy(final StrategyType strategyType) { + LOGGER.debug("useExecStrategy:{}", strategyType); + if (strategies.containsKey(strategyType)) { + strategy = strategies.get(strategyType); + return; + } + + IProvider provider = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseProvider(getRootNode(), getHolder(), watched, getAuthorities()); + switch (strategyType) { + case USUAL: + strategy = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy(provider); + break; + case CONTEND: + strategy = new ContentionStrategy(provider); + break; + case SYNC_RETRY: + strategy = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.SyncRetryStrategy(provider, ((ClientContext) getContext()).getDelayRetryPolicy()); + break; + case ASYNC_RETRY: + strategy = new AsyncRetryStrategy(provider, ((ClientContext) getContext()).getDelayRetryPolicy()); + break; + default: + strategy = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy(provider); + break; + } + + strategies.put(strategyType, strategy); + } + + @Override + public String getDataString(final String key) throws KeeperException, InterruptedException { + return strategy.getDataString(key); + } + + @Override + public byte[] getData(final String key) throws KeeperException, InterruptedException { + return strategy.getData(key); + } + + @Override + public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { + strategy.getData(key, callback, ctx); + } + + @Override + public boolean checkExists(final String key) throws KeeperException, InterruptedException { + return strategy.checkExists(key); + } + + @Override + public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { + return strategy.checkExists(key, watcher); + } + + @Override + public List getChildren(final String key) throws KeeperException, InterruptedException { + return strategy.getChildren(key); + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + this.createNamespace(); + if (getRootNode().equals(key)) { + return; + } + strategy.createCurrentOnly(key, value, createMode); + } + + @Override + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + this.createNamespace(); + if (getRootNode().equals(key)) { + return; + } + strategy.createAllNeedPath(key, value, createMode); + } + + @Override + public void update(final String key, final String value) throws KeeperException, InterruptedException { + strategy.update(key, value); + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + if (getRootNode().equals(key)) { + deleteNamespace(); + return; + } + strategy.deleteOnlyCurrent(key); + } + + @Override + public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + if (getRootNode().equals(key)) { + deleteNamespace(); + return; + } + strategy.deleteOnlyCurrent(key, callback, ctx); + } + + @Override + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { + strategy.deleteAllChildren(key); + if (getRootNode().equals(key)) { + setRootExist(false); + LOGGER.debug("deleteAllChildren delete root:{}", getRootNode()); + } + } + + @Override + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + strategy.deleteCurrentBranch(key); + if (!strategy.checkExists(getRootNode())) { + setRootExist(false); + LOGGER.debug("deleteCurrentBranch delete root:{}", getRootNode()); + } + } + + @Override + public ZKTransaction transaction() { + return new ZKTransaction(getRootNode(), getHolder()); + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/base/BaseClient.java b/src/main/resources/code/newzk/client/zookeeper/base/BaseClient.java new file mode 100644 index 0000000000000..613afd94a4fec --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/base/BaseClient.java @@ -0,0 +1,172 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IClient; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.TimeUnit; + +/* + * @author lidongbo + */ +public abstract class BaseClient implements IClient { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseClient.class); + + private final int circleWait = 30; + + @Getter(value = AccessLevel.PROTECTED) + private List authorities; + + // false + @Setter(value = AccessLevel.PROTECTED) + private boolean rootExist; + + @Getter(value = AccessLevel.PROTECTED) + private Holder holder; + + @Setter(value = AccessLevel.PROTECTED) + @Getter(value = AccessLevel.PROTECTED) + private String rootNode = "/InitValue"; + + @Getter + private BaseContext context; + + protected BaseClient(final BaseContext context) { + this.context = context; + } + + @Override + public void start() throws IOException, InterruptedException { + holder = new Holder(getContext()); + holder.start(); + } + + @Override + public synchronized boolean blockUntilConnected(final int wait, final TimeUnit units) throws InterruptedException { + long maxWait = units != null ? TimeUnit.MILLISECONDS.convert(wait, units) : 0; + + while (!holder.isConnected()) { + long waitTime = maxWait - circleWait; + if (waitTime <= 0) { + return holder.isConnected(); + } + wait(circleWait); + } + return true; + } + + @Override + public void close() { + context.close(); + try { + this.deleteNamespace(); + // CHECKSTYLE:OFF + } catch (Exception e) { + // CHECKSTYLE:ON + LOGGER.error("zk client close delete root error:{}", e.getMessage(), e); + } + holder.close(); + } + + void registerWatch(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener globalListener) { + if (context.getGlobalListener() != null) { + LOGGER.warn("global listener can only register one"); + return; + } + context.setGlobalListener(globalListener); + LOGGER.debug("globalListenerRegistered:{}", globalListener.getKey()); + } + + @Override + public void registerWatch(final String key, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener listener) { + String path = PathUtil.getRealPath(rootNode, key); + listener.setPath(path); + context.getWatchers().put(listener.getKey(), listener); + LOGGER.debug("register watcher:{}", path); + } + + @Override + public void unregisterWatch(final String key) { + if (io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.StringUtil.isNullOrBlank(key)) { + throw new IllegalArgumentException("key should not be blank"); + } +// String path = PathUtil.getRealPath(rootNode, key); + if (context.getWatchers().containsKey(key)) { + context.getWatchers().remove(key); + LOGGER.debug("unregisterWatch:{}", key); + } + } + + protected void createNamespace() throws KeeperException, InterruptedException { + createNamespace(Constants.NOTHING_DATA); + } + + protected void createNamespace(final byte[] date) throws KeeperException, InterruptedException { + if (rootExist) { + LOGGER.debug("root exist"); + return; + } + try { + if (null == holder.getZooKeeper().exists(rootNode, false)) { + holder.getZooKeeper().create(rootNode, date, authorities, CreateMode.PERSISTENT); + } + rootExist = true; + LOGGER.debug("creating root:{}", rootNode); + } catch (KeeperException.NodeExistsException e) { + LOGGER.warn("root create:{}", e.getMessage()); + rootExist = true; + return; + } + holder.getZooKeeper().exists(rootNode, io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.WatcherCreator.deleteWatcher(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener(rootNode) { + @Override + public void process(final WatchedEvent event) { + rootExist = false; + } + })); + LOGGER.debug("created root:{}", rootNode); + } + + protected void deleteNamespace() throws KeeperException, InterruptedException { + try { + holder.getZooKeeper().delete(rootNode, Constants.VERSION); + } catch (KeeperException.NodeExistsException | KeeperException.NotEmptyException e) { + LOGGER.info("delete root :{}", e.getMessage()); + } + rootExist = false; + LOGGER.debug("delete root:{},rootExist:{}", rootNode, rootExist); + } + + void setAuthorities(final String scheme, final byte[] auth, final List authorities) { + context.setScheme(scheme); + context.setAuth(auth); + this.authorities = authorities; + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/base/BaseClientFactory.java b/src/main/resources/code/newzk/client/zookeeper/base/BaseClientFactory.java new file mode 100644 index 0000000000000..beb29148a1ef8 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/base/BaseClientFactory.java @@ -0,0 +1,69 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IClient; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.ACL; + +import java.io.IOException; +import java.util.List; + +/* + * @author lidongbo + */ +@Setter(value = AccessLevel.PROTECTED) +@Getter(value = AccessLevel.PROTECTED) +public abstract class BaseClientFactory { + private BaseClient client; + + private io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener globalListener; + + private String namespace; + + private String scheme; + + private byte[] auth; + + private List authorities; + + private BaseContext context; + + /** + * start. + * + * @return client + * @throws IOException IO Exception + * @throws InterruptedException InterruptedException + */ + public IClient start() throws IOException, InterruptedException { + client.setRootNode(namespace); + if (scheme == null) { + authorities = ZooDefs.Ids.OPEN_ACL_UNSAFE; + } + client.setAuthorities(scheme, auth, authorities); + client.start(); + if (globalListener != null) { + client.registerWatch(globalListener); + } + return client; + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/base/BaseContext.java b/src/main/resources/code/newzk/client/zookeeper/base/BaseContext.java new file mode 100644 index 0000000000000..41b4bc05103f8 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/base/BaseContext.java @@ -0,0 +1,51 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/* + * @author lidongbo + */ +@Getter +@Setter(value = AccessLevel.PROTECTED) +public abstract class BaseContext { + private String servers; + + private int sessionTimeOut; + + private String scheme; + + private byte[] auth; + + private io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener globalListener; + + private final Map watchers = new ConcurrentHashMap<>(); + + /** + * close. + */ + public void close() { + this.watchers.clear(); + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/base/BaseOperation.java b/src/main/resources/code/newzk/client/zookeeper/base/BaseOperation.java new file mode 100644 index 0000000000000..450c1d173d4cf --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/base/BaseOperation.java @@ -0,0 +1,92 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; + +/* + * base async retry operation + * + * @author lidongbo + */ +@Getter(value = AccessLevel.PROTECTED) +public abstract class BaseOperation implements Delayed { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation.class); + + private final IProvider provider; + + @Setter + private io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayPolicyExecutor delayPolicyExecutor; + + protected BaseOperation(final IProvider provider) { + this.provider = provider; + } + + @Override + public long getDelay(final TimeUnit unit) { + long absoluteBlock = this.delayPolicyExecutor.getNextTick() - System.currentTimeMillis(); + LOGGER.debug("queue getDelay block:{}", absoluteBlock); + return unit.convert(absoluteBlock, TimeUnit.MILLISECONDS); + } + + /** + * queue precedence. + */ + @Override + public int compareTo(final Delayed delayed) { + return (int) (this.getDelay(TimeUnit.MILLISECONDS) - delayed.getDelay(TimeUnit.MILLISECONDS)); + } + + protected abstract void execute() throws KeeperException, InterruptedException; + + /** + * queue precedence. + * + * @return whether or not continue enqueue + * @throws KeeperException Keeper Exception + * @throws InterruptedException InterruptedException + */ + public boolean executeOperation() throws KeeperException, InterruptedException { + boolean result; + try { + execute(); + result = true; + } catch (KeeperException e) { + if (io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Connection.needReset(e)) { + provider.resetConnection(); + result = false; + } else { + throw e; + } + } + if (!result && delayPolicyExecutor.hasNext()) { + delayPolicyExecutor.next(); + return true; + } + return false; + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/base/BaseProvider.java b/src/main/resources/code/newzk/client/zookeeper/base/BaseProvider.java new file mode 100644 index 0000000000000..af55a8c6971ea --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/base/BaseProvider.java @@ -0,0 +1,158 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.election.LeaderElection; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction; +import lombok.AccessLevel; +import lombok.Getter; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Stack; + +/* + * @author lidongbo + */ +public class BaseProvider implements IProvider { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseProvider.class); + + @Getter + private final Holder holder; + + @Getter + private final String rootNode; + + @Getter(value = AccessLevel.PROTECTED) + private final boolean watched; + + @Getter(value = AccessLevel.PROTECTED) + private final List authorities; + + public BaseProvider(final String rootNode, final Holder holder, final boolean watched, final List authorities) { + this.rootNode = rootNode; + this.holder = holder; + this.watched = watched; + this.authorities = authorities; + } + + @Override + public String getDataString(final String key) throws KeeperException, InterruptedException { + return new String(getData(key)); + } + + @Override + public byte[] getData(final String key) throws KeeperException, InterruptedException { + return holder.getZooKeeper().getData(key, watched, null); + } + + @Override + public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { + holder.getZooKeeper().getData(key, watched, callback, ctx); + } + + @Override + public boolean exists(final String key) throws KeeperException, InterruptedException { + return null != holder.getZooKeeper().exists(key, watched); + } + + @Override + public boolean exists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { + return null != holder.getZooKeeper().exists(key, watcher); + } + + @Override + public List getChildren(final String key) throws KeeperException, InterruptedException { + return holder.getZooKeeper().getChildren(key, watched); + } + + @Override + public void create(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + holder.getZooKeeper().create(key, value.getBytes(Constants.UTF_8), authorities, createMode); + LOGGER.debug("BaseProvider createCurrentOnly:{}", key); +// create(key, value, createMode, new AtomicInteger()); + } + + @Override + public void update(final String key, final String value) throws KeeperException, InterruptedException { + holder.getZooKeeper().setData(key, value.getBytes(Constants.UTF_8), Constants.VERSION); + } + + @Override + public void delete(final String key) throws KeeperException, InterruptedException { + holder.getZooKeeper().delete(key, Constants.VERSION); + LOGGER.debug("BaseProvider deleteOnlyCurrent:{}", key); + } + + @Override + public void delete(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + holder.getZooKeeper().delete(key, Constants.VERSION, callback, ctx); + LOGGER.debug("BaseProvider deleteOnlyCurrent:{},ctx:{}", key, ctx); + } + + @Override + public String getRealPath(final String path) { + return PathUtil.getRealPath(rootNode, path); + } + + @Override + public List getNecessaryPaths(final String key) { + List nodes = PathUtil.getPathOrderNodes(rootNode, key); + nodes.remove(rootNode); + return nodes; + } + + @Override + public Stack getDeletingPaths(final String key) { + return PathUtil.getPathReverseNodes(rootNode, key); + } + + @Override + public void executeContention(final LeaderElection election) throws KeeperException, InterruptedException { + this.executeContention(rootNode, election); + } + + private void executeContention(final String nodeBeCompete, final LeaderElection election) throws KeeperException, InterruptedException { + election.executeContention(nodeBeCompete, this); + } + + @Override + public void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { + transaction.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); + } + + @Override + public void resetConnection() { + try { + holder.reset(); + // CHECKSTYLE:OFF + } catch (Exception e) { + // CHECKSTYLE:ON + LOGGER.error("resetConnection Exception:{}", e.getMessage(), e); + } + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/base/BaseStrategy.java b/src/main/resources/code/newzk/client/zookeeper/base/BaseStrategy.java new file mode 100644 index 0000000000000..d9f3776528649 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/base/BaseStrategy.java @@ -0,0 +1,39 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base; + +import lombok.Getter; +import org.apache.zookeeper.KeeperException; + +/* + * @author lidongbo + */ +public abstract class BaseStrategy implements io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IExecStrategy { + + @Getter + private final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider; + + public BaseStrategy(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider) { + this.provider = provider; + } + + @Override + public String getDataString(final String key) throws KeeperException, InterruptedException { + return new String(getData(key)); + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/base/Holder.java b/src/main/resources/code/newzk/client/zookeeper/base/Holder.java new file mode 100644 index 0000000000000..35e2edd82bd90 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/base/Holder.java @@ -0,0 +1,141 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import lombok.AccessLevel; +import lombok.Getter; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; + +/* + * zookeeper connection holder + * + * @author lidongbo + */ +public class Holder { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.Holder.class); + + private static final CountDownLatch CONNECTED = new CountDownLatch(1); + + @Getter(value = AccessLevel.PROTECTED) + private final BaseContext context; + + @Getter + private ZooKeeper zooKeeper; + + @Getter + private boolean connected; + + Holder(final BaseContext context) { + this.context = context; + } + + /** + * start. + * + * @throws IOException IO Exception + * @throws InterruptedException InterruptedException + */ + public void start() throws IOException, InterruptedException { + LOGGER.debug("Holder servers:{},sessionTimeOut:{}", context.getServers(), context.getSessionTimeOut()); + zooKeeper = new ZooKeeper(context.getServers(), context.getSessionTimeOut(), startWatcher()); + if (!io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.StringUtil.isNullOrBlank(context.getScheme())) { + zooKeeper.addAuthInfo(context.getScheme(), context.getAuth()); + LOGGER.debug("Holder scheme:{},auth:{}", context.getScheme(), context.getAuth()); + } + CONNECTED.await(); + } + + private Watcher startWatcher() { + return new Watcher() { + public void process(final WatchedEvent event) { + processConnection(event); + if (context.getGlobalListener() != null) { + context.getGlobalListener().process(event); + LOGGER.debug("BaseClient {} process", Constants.GLOBAL_LISTENER_KEY); + } + if (!context.getWatchers().isEmpty()) { + for (io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener listener : context.getWatchers().values()) { + if (listener.getPath() == null || listener.getPath().equals(event.getPath())) { + LOGGER.debug("listener process:{}, listener:{}", listener.getPath(), listener.getKey()); + listener.process(event); + } + } + } + } + }; + } + + private void processConnection(final WatchedEvent event) { + LOGGER.debug("BaseClient process event:{}", event.toString()); + if (Watcher.Event.EventType.None == event.getType()) { + if (Watcher.Event.KeeperState.SyncConnected == event.getState()) { + CONNECTED.countDown(); + connected = true; + LOGGER.debug("BaseClient startWatcher SyncConnected"); + return; + } else if (Watcher.Event.KeeperState.Expired == event.getState()) { + connected = false; + try { + LOGGER.warn("startWatcher Event.KeeperState.Expired"); + reset(); + // CHECKSTYLE:OFF + } catch (Exception e) { + // CHECKSTYLE:ON + LOGGER.error("event state Expired:{}", e.getMessage(), e); + } + } + } + } + + /** + * reset connection. + * + * @throws IOException IO Exception + * @throws InterruptedException InterruptedException + */ + public void reset() throws IOException, InterruptedException { + LOGGER.debug("zk reset...................................."); + close(); + start(); + LOGGER.debug("....................................zk reset"); + } + + /** + * close. + */ + public void close() { + try { + zooKeeper.close(); + connected = false; + LOGGER.debug("zk closed"); + this.context.close(); + // CHECKSTYLE:OFF + } catch (Exception e) { + // CHECKSTYLE:ON + LOGGER.warn("Holder close:{}", e.getMessage()); + } + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/operation/CreateAllNeedOperation.java b/src/main/resources/code/newzk/client/zookeeper/operation/CreateAllNeedOperation.java new file mode 100644 index 0000000000000..bc451230d7f34 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/operation/CreateAllNeedOperation.java @@ -0,0 +1,54 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; + +/* + * async retry + * + * @author lidongbo + */ +public class CreateAllNeedOperation extends BaseOperation { + private final String key; + + private final String value; + + private final CreateMode createMode; + + public CreateAllNeedOperation(final IProvider provider, final String key, final String value, final CreateMode createMode) { + super(provider); + this.key = key; + this.value = value; + this.createMode = createMode; + } + + @Override + protected void execute() throws KeeperException, InterruptedException { + new UsualStrategy(getProvider()).createAllNeedPath(key, value, createMode); + } + + @Override + public String toString() { + return String.format("CreateAllNeedOperation key:%s,value:%s,createMode:%s", key, value, createMode.name()); + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/operation/CreateCurrentOperation.java b/src/main/resources/code/newzk/client/zookeeper/operation/CreateCurrentOperation.java new file mode 100644 index 0000000000000..4a5216cbc49d3 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/operation/CreateCurrentOperation.java @@ -0,0 +1,53 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; + +/* + * async retry + * + * @author lidongbo + */ +public class CreateCurrentOperation extends BaseOperation { + private final String key; + + private final String value; + + private final CreateMode createMode; + + public CreateCurrentOperation(final IProvider provider, final String key, final String value, final CreateMode createMode) { + super(provider); + this.key = key; + this.value = value; + this.createMode = createMode; + } + + @Override + public void execute() throws KeeperException, InterruptedException { + getProvider().create(getProvider().getRealPath(key), value, createMode); + } + + @Override + public String toString() { + return String.format("CreateCurrentOperation key:%s,value:%s,createMode:%s", key, value, createMode.name()); + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java b/src/main/resources/code/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java new file mode 100644 index 0000000000000..4c5966ae9d265 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java @@ -0,0 +1,44 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation; + +import org.apache.zookeeper.KeeperException; + +/* + * async retry + * + * @author lidongbo + */ +public class DeleteAllChildrenOperation extends io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation { + private final String key; + + public DeleteAllChildrenOperation(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider, final String key) { + super(provider); + this.key = key; + } + + @Override + protected void execute() throws KeeperException, InterruptedException { + new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy(getProvider()).deleteAllChildren(key); + } + + @Override + public String toString() { + return String.format("DeleteAllChildrenOperation key:%s", key); + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/operation/DeleteCurrentBranchOperation.java b/src/main/resources/code/newzk/client/zookeeper/operation/DeleteCurrentBranchOperation.java new file mode 100644 index 0000000000000..03d6ac27f216c --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/operation/DeleteCurrentBranchOperation.java @@ -0,0 +1,47 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy; +import org.apache.zookeeper.KeeperException; + +/* + * async retry + * + * @author lidongbo + */ +public class DeleteCurrentBranchOperation extends BaseOperation { + private final String key; + + public DeleteCurrentBranchOperation(final IProvider provider, final String key) { + super(provider); + this.key = key; + } + + @Override + protected void execute() throws KeeperException, InterruptedException { + new UsualStrategy(getProvider()).deleteCurrentBranch(key); + } + + @Override + public String toString() { + return String.format("DeleteCurrentBranchOperation key:%s", key); + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/operation/DeleteCurrentOperation.java b/src/main/resources/code/newzk/client/zookeeper/operation/DeleteCurrentOperation.java new file mode 100644 index 0000000000000..5644b194bf3a1 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/operation/DeleteCurrentOperation.java @@ -0,0 +1,46 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation; +import org.apache.zookeeper.KeeperException; + +/* + * async retry + * + * @author lidongbo + */ +public class DeleteCurrentOperation extends BaseOperation { + private final String key; + + public DeleteCurrentOperation(final IProvider provider, final String key) { + super(provider); + this.key = key; + } + + @Override + protected void execute() throws KeeperException, InterruptedException { + getProvider().delete(getProvider().getRealPath(key)); + } + + @Override + public String toString() { + return String.format("DeleteCurrentOperation key:%s", key); + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/operation/UpdateOperation.java b/src/main/resources/code/newzk/client/zookeeper/operation/UpdateOperation.java new file mode 100644 index 0000000000000..f485d10cf7d46 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/operation/UpdateOperation.java @@ -0,0 +1,47 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation; + +import org.apache.zookeeper.KeeperException; + +/* + * async retry + * + * @author lidongbo + */ +public class UpdateOperation extends io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation { + private final String key; + + private final String value; + + public UpdateOperation(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider, final String key, final String value) { + super(provider); + this.key = key; + this.value = value; + } + + @Override + protected void execute() throws KeeperException, InterruptedException { + getProvider().update(getProvider().getRealPath(key), value); + } + + @Override + public String toString() { + return String.format("UpdateOperation key:%s,value:%s", key, value); + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/section/Callable.java b/src/main/resources/code/newzk/client/zookeeper/section/Callable.java new file mode 100644 index 0000000000000..5d624a3a51c99 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/section/Callable.java @@ -0,0 +1,109 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section; + +import lombok.Setter; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/* + * sync retry call + * + * @author lidongbo + */ +public abstract class Callable { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Callable.class); + + private final io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayPolicyExecutor delayPolicyExecutor; + + private final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider; + + @Setter + private T result; + + public Callable(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy delayRetryPolicy) { + this.delayPolicyExecutor = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayPolicyExecutor(delayRetryPolicy); + this.provider = provider; + } + + /** + * call. + * + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + public abstract void call() throws KeeperException, InterruptedException; + + /** + * get result. + * + * @return result + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + public T getResult() throws KeeperException, InterruptedException { + if (result == null) { + exec(); + } + return result; + } + + /** + * call without result. + * + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + public void exec() throws KeeperException, InterruptedException { + try { + call(); + } catch (KeeperException e) { + LOGGER.warn("exec KeeperException:{}", e.getMessage()); + delayPolicyExecutor.next(); + if (io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Connection.needReset(e)) { + provider.resetConnection(); + } else { + throw e; + } + execDelay(); + } catch (InterruptedException e) { + throw e; + } + } + + private void execDelay() throws KeeperException, InterruptedException { + for (;;) { + long delay = delayPolicyExecutor.getNextTick() - System.currentTimeMillis(); + if (delay > 0) { + try { + LOGGER.debug("exec delay:{}", delay); + Thread.sleep(delay); + } catch (InterruptedException e) { + throw e; + } + } else { + if (delayPolicyExecutor.hasNext()) { + LOGGER.debug("exec hasNext"); + exec(); + } + break; + } + } + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/section/ClientContext.java b/src/main/resources/code/newzk/client/zookeeper/section/ClientContext.java new file mode 100644 index 0000000000000..d62010ba53d7e --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/section/ClientContext.java @@ -0,0 +1,61 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseContext; +import lombok.Getter; +import lombok.Setter; + +/* + * @author lidongbo + */ +@Setter +@Getter +public final class ClientContext extends BaseContext { + + private io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy delayRetryPolicy; + + private io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseClientFactory clientFactory; + + public ClientContext(final String servers, final int sessionTimeoutMilliseconds) { + super(); + setServers(servers); + setSessionTimeOut(sessionTimeoutMilliseconds); + } + + /** + * call. + */ + public void close() { + super.close(); + this.delayRetryPolicy = null; + this.clientFactory = null; + } + + /** + * update context. + * + * @param context context + */ + public void updateContext(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.ClientContext context) { + this.delayRetryPolicy = context.getDelayRetryPolicy(); + this.clientFactory = context.clientFactory; + getWatchers().clear(); + getWatchers().putAll(context.getWatchers()); + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/section/ClientTask.java b/src/main/resources/code/newzk/client/zookeeper/section/ClientTask.java new file mode 100644 index 0000000000000..b0464d42dd1cf --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/section/ClientTask.java @@ -0,0 +1,54 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/* + * @author lidongbo + */ +public abstract class ClientTask implements Runnable { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.ClientTask.class); + + private final IProvider provider; + + public ClientTask(final IProvider provider) { + this.provider = provider; + } + + /** + * run. + * + * @param provider provider + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + public abstract void run(IProvider provider) throws KeeperException, InterruptedException; + + @Override + public void run() { + try { + run(provider); + } catch (KeeperException | InterruptedException e) { + LOGGER.error(e.getMessage(), e); + } + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/section/Connection.java b/src/main/resources/code/newzk/client/zookeeper/section/Connection.java new file mode 100644 index 0000000000000..63dbde3920408 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/section/Connection.java @@ -0,0 +1,54 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section; + +import org.apache.zookeeper.KeeperException; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/* + * @author lidongbo + */ +public class Connection { + //is need reset + private static final Map EXCEPTION_RESETS = new ConcurrentHashMap<>(); + + static { + EXCEPTION_RESETS.put(KeeperException.Code.SESSIONEXPIRED.intValue(), true); + EXCEPTION_RESETS.put(KeeperException.Code.SESSIONMOVED.intValue(), true); + EXCEPTION_RESETS.put(KeeperException.Code.CONNECTIONLOSS.intValue(), false); + EXCEPTION_RESETS.put(KeeperException.Code.OPERATIONTIMEOUT.intValue(), false); + } + + /** + * need reset. + * + * @param e e + * @return need reset + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + public static boolean needReset(final KeeperException e) throws KeeperException { + int code = e.code().intValue(); + if (!EXCEPTION_RESETS.containsKey(code)) { + throw e; + } + return EXCEPTION_RESETS.get(code); + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/section/Listener.java b/src/main/resources/code/newzk/client/zookeeper/section/Listener.java new file mode 100644 index 0000000000000..745328524557e --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/section/Listener.java @@ -0,0 +1,49 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section; + +import lombok.Getter; +import lombok.Setter; +import org.apache.zookeeper.WatchedEvent; + +/* + * @author lidongbo + */ +@Getter +public abstract class Listener { + private final String key; + + @Setter + private String path; + + public Listener() { + this(null); + } + + public Listener(final String path) { + this.path = path; + this.key = path + System.currentTimeMillis(); + } + + /** + * process. + * + * @param event event + */ + public abstract void process(WatchedEvent event); +} diff --git a/src/main/resources/code/newzk/client/zookeeper/section/StrategyType.java b/src/main/resources/code/newzk/client/zookeeper/section/StrategyType.java new file mode 100644 index 0000000000000..d166088851d07 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/section/StrategyType.java @@ -0,0 +1,29 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section; + +/* + * @author lidongbo + */ +public enum StrategyType { + USUAL, + CONTEND, + SYNC_RETRY, + ASYNC_RETRY, + ALL_ASYNC_RETRY +} diff --git a/src/main/resources/code/newzk/client/zookeeper/section/WatcherCreator.java b/src/main/resources/code/newzk/client/zookeeper/section/WatcherCreator.java new file mode 100644 index 0000000000000..dcdf569b7b3f4 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/section/WatcherCreator.java @@ -0,0 +1,48 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section; + +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/* + * @author lidongbo + */ +public class WatcherCreator { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.WatcherCreator.class); + + /** + * get string type data. + * + * @param listener listener + * @return watcher + */ + public static Watcher deleteWatcher(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener listener) { + return new Watcher() { + @Override + public void process(final WatchedEvent event) { + if (listener.getPath().equals(event.getPath()) && Event.EventType.NodeDeleted.equals(event.getType())) { + listener.process(event); + LOGGER.debug("delete node event:{}", event.toString()); + } + } + }; + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java b/src/main/resources/code/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java new file mode 100644 index 0000000000000..47066e3a8aa8f --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java @@ -0,0 +1,104 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.DeleteAllChildrenOperation; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.UpdateOperation; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/* + * async retry strategy + * + * @author lidongbo + */ +public class AsyncRetryStrategy extends io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.SyncRetryStrategy { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.AsyncRetryStrategy.class); + + public AsyncRetryStrategy(final IProvider provider, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy delayRetryPolicy) { + super(provider, delayRetryPolicy); + io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.init(getDelayRetryPolicy()); + io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.start(); + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + String path = getProvider().getRealPath(key); + try { + getProvider().create(path, value, createMode); + } catch (KeeperException.SessionExpiredException e) { + LOGGER.warn("AsyncRetryStrategy SessionExpiredException createCurrentOnly:{}", path); + io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.add(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.CreateCurrentOperation(getProvider(), path, value, createMode)); + } + } + + @Override + public void update(final String key, final String value) throws KeeperException, InterruptedException { + String path = getProvider().getRealPath(key); + try { + getProvider().update(path, value); + } catch (KeeperException.SessionExpiredException e) { + LOGGER.warn("AsyncRetryStrategy SessionExpiredException update:{}", path); + io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.add(new UpdateOperation(getProvider(), path, value)); + } + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + String path = getProvider().getRealPath(key); + try { + getProvider().delete(path); + } catch (KeeperException.SessionExpiredException e) { + LOGGER.warn("AsyncRetryStrategy SessionExpiredException deleteOnlyCurrent:{}", path); + io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.add(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.DeleteCurrentOperation(getProvider(), path)); + } + } + + @Override + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + try { + super.createAllNeedPath(key, value, createMode); + } catch (KeeperException.SessionExpiredException e) { + LOGGER.warn("AllAsyncRetryStrategy SessionExpiredException CreateAllNeedOperation:{}", key); + io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.add(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.CreateAllNeedOperation(getProvider(), key, value, createMode)); + } + } + + @Override + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { + try { + super.deleteAllChildren(key); + } catch (KeeperException.SessionExpiredException e) { + LOGGER.warn("AllAsyncRetryStrategy SessionExpiredException deleteAllChildren:{}", key); + io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.add(new DeleteAllChildrenOperation(getProvider(), key)); + } + } + + @Override + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + try { + super.deleteCurrentBranch(key); + } catch (KeeperException.SessionExpiredException e) { + LOGGER.warn("AllAsyncRetryStrategy SessionExpiredException deleteCurrentBranch:{}", key); + io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.add(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.DeleteCurrentBranchOperation(getProvider(), key)); + } + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/strategy/ContentionStrategy.java b/src/main/resources/code/newzk/client/zookeeper/strategy/ContentionStrategy.java new file mode 100644 index 0000000000000..a5b4885b8bb3f --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/strategy/ContentionStrategy.java @@ -0,0 +1,260 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.election.LeaderElection; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Stack; + +/* + * The ContentionStrategy is effective only when all the clients of the node which be competitive are using ContentionStrategy. + * + * @author lidongbo + */ +public class ContentionStrategy extends io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.ContentionStrategy.class); + + public ContentionStrategy(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider) { + super(provider); + } + + @Override + /* + * Don't use this if you don't have to use it. + */ + public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { + LeaderElection election = new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + getProvider().getData(getProvider().getRealPath(key), callback, ctx); + LOGGER.debug("ContentionStrategy getData action:{}", key); + } + }; + getProvider().executeContention(election); + LOGGER.debug("ContentionStrategy getData executeContention"); + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + LeaderElection election = buildCreateElection(key, value, createMode, null); + getProvider().executeContention(election); + LOGGER.debug("ContentionStrategy createCurrentOnly executeContention"); + election.waitDone(); + } + + private LeaderElection buildCreateElection(final String key, final String value, final CreateMode createMode, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.Callback callback) { + return new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + getProvider().create(getProvider().getRealPath(key), value, createMode); + } + + @Override + public void callback() { + if (callback != null) { + callback.processResult(); + } + } + }; + } + + @Override + public void update(final String key, final String value) throws KeeperException, InterruptedException { + LeaderElection election = buildUpdateElection(key, value, null); + getProvider().executeContention(election); + LOGGER.debug("ContentionStrategy update executeContention"); + election.waitDone(); + } + + private LeaderElection buildUpdateElection(final String key, final String value, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.Callback callback) { + return new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + getProvider().update(getProvider().getRealPath(key), value); + LOGGER.debug("ContentionStrategy update action:{},value:{}", key, value); + } + + @Override + public void callback() { + if (callback != null) { + callback.processResult(); + } + } + }; + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + LeaderElection election = buildDeleteElection(key, null); + getProvider().executeContention(election); + LOGGER.debug("ContentionStrategy deleteOnlyCurrent executeContention"); + election.waitDone(); + } + + @Override + public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + getProvider().executeContention(new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + getProvider().delete(getProvider().getRealPath(key), callback, ctx); + LOGGER.debug("ContentionStrategy deleteOnlyCurrent action:{},ctx:{}", key, ctx); + } + }); + } + + private LeaderElection buildDeleteElection(final String key, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.Callback callback) { + return new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + getProvider().delete(getProvider().getRealPath(key)); + } + + @Override + public void callback() { + if (callback != null) { + callback.processResult(); + } + } + }; + } + + @Override + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + LeaderElection election = buildCreateAllNeedElection(key, value, createMode, null); + getProvider().executeContention(election); + LOGGER.debug("ContentionStrategy createAllNeedPath executeContention"); + election.waitDone(); + } + + private LeaderElection buildCreateAllNeedElection(final String key, final String value, final CreateMode createMode, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.Callback callback) { + return new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + LOGGER.debug("ContentionStrategy createAllNeedPath action:{}", key); + createBegin(getProvider().getRealPath(key), value, createMode); + } + + @Override + public void callback() { + if (callback != null) { + callback.processResult(); + } + } + }; + } + + private void createBegin(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + if (key.indexOf(Constants.PATH_SEPARATOR) < -1) { + getProvider().create(key, value, createMode); + return; + } + List nodes = getProvider().getNecessaryPaths(key); + for (int i = 0; i < nodes.size(); i++) { + if (getProvider().exists(nodes.get(i))) { + LOGGER.info("create node exist:{}", nodes.get(i)); + continue; + } + LOGGER.debug("create node not exist:", nodes.get(i)); + if (i == nodes.size() - 1) { + getProvider().create(nodes.get(i), value, createMode); + } else { + getProvider().create(nodes.get(i), Constants.NOTHING_VALUE, createMode); + } + } + } + + @Override + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { + getProvider().executeContention(new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + deleteChildren(getProvider().getRealPath(key), true); + } + }); + LOGGER.debug("ContentionStrategy deleteAllChildren executeContention"); + } + + private void deleteChildren(final String key, final boolean deleteCurrentNode) throws KeeperException, InterruptedException { + List children = getProvider().getChildren(key); + LOGGER.debug("deleteChildren:{}", children); + for (int i = 0; i < children.size(); i++) { + String child = PathUtil.getRealPath(key, children.get(i)); + if (!getProvider().exists(child)) { + LOGGER.info("delete not exist:{}", child); + continue; + } + LOGGER.debug("deleteChildren:{}", child); + deleteChildren(child, true); + } + if (deleteCurrentNode) { + getProvider().delete(key); + } + } + + @Override + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + getProvider().executeContention(new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + deleteBranch(getProvider().getRealPath(key)); + } + }); + LOGGER.debug("ContentionStrategy deleteCurrentBranch executeContention"); + } + + private void deleteBranch(final String key) throws KeeperException, InterruptedException { + deleteChildren(key, false); + Stack pathStack = getProvider().getDeletingPaths(key); + while (!pathStack.empty()) { + String node = pathStack.pop(); + // contrast cache + if (getProvider().exists(node)) { + try { + getProvider().delete(node); + } catch (KeeperException.NotEmptyException e) { + LOGGER.warn("deleteBranch {} exist other children:{}", node, this.getChildren(node)); + LOGGER.debug(e.getMessage()); + return; + } + } + LOGGER.info("deleteBranch node not exist:{}", node); + } + } + + //todo Use arbitrary competitive nodes + //IExecStrategy convert to ContentionStrategy + /*public void createCurrentOnly(final String key, final String value, final CreateMode createMode, final Callback callback) throws KeeperException, InterruptedException { + getProvider().executeContention(buildCreateElection(key, value, createMode, callback)); + } + + public void update(final String key, final String value, final Callback callback) throws KeeperException, InterruptedException { + getProvider().executeContention(buildUpdateElection(key, value, null)); + } + + public void deleteOnlyCurrent(final String key, final Callback callback) throws KeeperException, InterruptedException { + getProvider().executeContention(buildDeleteElection(key, null)); + }*/ +} diff --git a/src/main/resources/code/newzk/client/zookeeper/strategy/SyncRetryStrategy.java b/src/main/resources/code/newzk/client/zookeeper/strategy/SyncRetryStrategy.java new file mode 100644 index 0000000000000..a7135275e6832 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/strategy/SyncRetryStrategy.java @@ -0,0 +1,163 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Callable; +import lombok.AccessLevel; +import lombok.Getter; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/* + * sync retry strategy + * + * @author lidongbo + */ +public class SyncRetryStrategy extends io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.SyncRetryStrategy.class); + + @Getter(value = AccessLevel.PROTECTED) + private final DelayRetryPolicy delayRetryPolicy; + + public SyncRetryStrategy(final IProvider provider, final DelayRetryPolicy delayRetryPolicy) { + super(provider); + if (delayRetryPolicy == null) { + LOGGER.warn("Callable constructor context's delayRetryPolicy is null"); + this.delayRetryPolicy = DelayRetryPolicy.newNoInitDelayPolicy(); + } else { + this.delayRetryPolicy = delayRetryPolicy; + } + } + + @Override + public byte[] getData(final String key) throws KeeperException, InterruptedException { + Callable callable = new Callable(getProvider(), delayRetryPolicy) { + @Override + public void call() throws KeeperException, InterruptedException { + setResult(getProvider().getData(getProvider().getRealPath(key))); + } + }; + return callable.getResult(); + } + + @Override + public boolean checkExists(final String key) throws KeeperException, InterruptedException { + Callable callable = new Callable(getProvider(), delayRetryPolicy) { + @Override + public void call() throws KeeperException, InterruptedException { + setResult(getProvider().exists(getProvider().getRealPath(key))); + } + }; + return callable.getResult(); + } + + @Override + public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { + Callable callable = new Callable(getProvider(), delayRetryPolicy) { + @Override + public void call() throws KeeperException, InterruptedException { + setResult(getProvider().exists(getProvider().getRealPath(key), watcher)); + } + }; + return callable.getResult(); + } + + @Override + public List getChildren(final String key) throws KeeperException, InterruptedException { + Callable> callable = new Callable(getProvider(), delayRetryPolicy) { + @Override + public void call() throws KeeperException, InterruptedException { + setResult(getProvider().getChildren(getProvider().getRealPath(key))); + } + }; + return callable.getResult(); + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + Callable callable = new Callable(getProvider(), delayRetryPolicy) { + @Override + public void call() throws KeeperException, InterruptedException { + getProvider().create(getProvider().getRealPath(key), value, createMode); + } + }; + callable.exec(); + } + + @Override + public void update(final String key, final String value) throws KeeperException, InterruptedException { + Callable callable = new Callable(getProvider(), delayRetryPolicy) { + @Override + public void call() throws KeeperException, InterruptedException { + getProvider().update(getProvider().getRealPath(key), value); + } + }; + callable.exec(); + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + Callable callable = new Callable(getProvider(), delayRetryPolicy) { + @Override + public void call() throws KeeperException, InterruptedException { + getProvider().delete(getProvider().getRealPath(key)); + } + }; + callable.exec(); + } + + @Override + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + Callable callable = new Callable(getProvider(), delayRetryPolicy) { + @Override + public void call() throws KeeperException, InterruptedException { + new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy(getProvider()).createAllNeedPath(key, value, createMode); + } + }; + callable.exec(); + } + + @Override + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { + Callable callable = new Callable(getProvider(), delayRetryPolicy) { + @Override + public void call() throws KeeperException, InterruptedException { + new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy(getProvider()).deleteAllChildren(key); + } + }; + callable.exec(); + } + + @Override + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + Callable callable = new Callable(getProvider(), delayRetryPolicy) { + @Override + public void call() throws KeeperException, InterruptedException { + new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy(getProvider()).deleteCurrentBranch(key); + } + }; + callable.exec(); + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/strategy/TransactionContendStrategy.java b/src/main/resources/code/newzk/client/zookeeper/strategy/TransactionContendStrategy.java new file mode 100644 index 0000000000000..208ede1f907bd --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/strategy/TransactionContendStrategy.java @@ -0,0 +1,155 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.election.LeaderElection; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Stack; + +/* + * @author lidongbo + * @since zookeeper 3.4.0 + */ +public class TransactionContendStrategy extends ContentionStrategy { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.TransactionContendStrategy.class); + + public TransactionContendStrategy(final IProvider provider) { + super(provider); + } + + @Override + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + LeaderElection election = buildCreateAllNeedElection(key, value, createMode, null); + getProvider().executeContention(election); + LOGGER.debug("ContentionStrategy createAllNeedPath executeContention"); + election.waitDone(); + } + + private LeaderElection buildCreateAllNeedElection(final String key, final String value, final CreateMode createMode, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.Callback callback) { + return new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + LOGGER.debug("ContentionStrategy createAllNeedPath action:{}", key); + ZKTransaction transaction = new ZKTransaction(((BaseProvider) getProvider()).getRootNode(), ((BaseProvider) getProvider()).getHolder()); + createBegin(key, value, createMode, transaction); + transaction.commit(); + } + + @Override + public void callback() { + if (callback != null) { + callback.processResult(); + } + } + }; + } + + private void createBegin(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { + if (key.indexOf(Constants.PATH_SEPARATOR) < -1) { + getProvider().createInTransaction(key, value, createMode, transaction); + return; + } + List nodes = getProvider().getNecessaryPaths(key); + for (int i = 0; i < nodes.size(); i++) { + if (getProvider().exists(nodes.get(i))) { + LOGGER.info("create node exist:{}", nodes.get(i)); + continue; + } + LOGGER.debug("node not exist and create:", nodes.get(i)); + if (i == nodes.size() - 1) { + getProvider().createInTransaction(nodes.get(i), value, createMode, transaction); + } else { + getProvider().createInTransaction(nodes.get(i), Constants.NOTHING_VALUE, createMode, transaction); + } + } + } + + @Override + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { + getProvider().executeContention(new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + ZKTransaction transaction = new ZKTransaction(((BaseProvider) getProvider()).getRootNode(), ((BaseProvider) getProvider()).getHolder()); + deleteChildren(getProvider().getRealPath(key), true, transaction); + transaction.commit(); + } + }); + LOGGER.debug("ContentionStrategy deleteAllChildren executeContention"); + } + + private void deleteChildren(final String key, final boolean deleteCurrentNode, final ZKTransaction transaction) throws KeeperException, InterruptedException { + List children = getProvider().getChildren(key); + for (int i = 0; i < children.size(); i++) { + String child = PathUtil.getRealPath(key, children.get(i)); + if (!getProvider().exists(child)) { + LOGGER.info("delete not exist:{}", child); + continue; + } + LOGGER.debug("deleteChildren:{}", child); + deleteChildren(child, true, transaction); + } + if (deleteCurrentNode) { + transaction.delete(key, Constants.VERSION); + } + } + + @Override + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + getProvider().executeContention(new LeaderElection() { + @Override + public void action() throws KeeperException, InterruptedException { + ZKTransaction transaction = new ZKTransaction(((BaseProvider) getProvider()).getRootNode(), ((BaseProvider) getProvider()).getHolder()); + deleteBranch(getProvider().getRealPath(key), transaction); + transaction.commit(); + } + }); + LOGGER.debug("ContentionStrategy deleteCurrentBranch executeContention"); + } + + private void deleteBranch(final String key, final ZKTransaction transaction) throws KeeperException, InterruptedException { + deleteChildren(key, false, transaction); + Stack pathStack = getProvider().getDeletingPaths(key); + String prePath = key; + while (!pathStack.empty()) { + String node = pathStack.pop(); + // contrast cache + // Performance needs testing + List children = getProvider().getChildren(node); + boolean canDelete = children.size() == 0 || children.size() == 1; + if (children.size() == 1) { + if (!PathUtil.getRealPath(node, children.get(0)).equals(prePath)) { + canDelete = false; + } + } + if (getProvider().exists(node) && canDelete) { + transaction.delete(node, Constants.VERSION); + } + prePath = node; + } + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/strategy/UsualStrategy.java b/src/main/resources/code/newzk/client/zookeeper/strategy/UsualStrategy.java new file mode 100644 index 0000000000000..762cdf47e8e2f --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/strategy/UsualStrategy.java @@ -0,0 +1,180 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseStrategy; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/* + * usual strategy + * + * @author lidongbo + */ +public class UsualStrategy extends BaseStrategy { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy.class); + + public UsualStrategy(final IProvider provider) { + super(provider); + } + + @Override + public byte[] getData(final String key) throws KeeperException, InterruptedException { + return getProvider().getData(getProvider().getRealPath(key)); + } + + @Override + public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { + getProvider().getData(getProvider().getRealPath(key), callback, ctx); + } + + @Override + public boolean checkExists(final String key) throws KeeperException, InterruptedException { + return getProvider().exists(getProvider().getRealPath(key)); + } + + @Override + public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { + return getProvider().exists(getProvider().getRealPath(key), watcher); + } + + @Override + public List getChildren(final String key) throws KeeperException, InterruptedException { + return getProvider().getChildren(getProvider().getRealPath(key)); + } + + @Override + public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + getProvider().create(getProvider().getRealPath(key), value, createMode); + } + + @Override + public void update(final String key, final String value) throws KeeperException, InterruptedException { + getProvider().update(getProvider().getRealPath(key), value); + } + + @Override + public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { + getProvider().delete(getProvider().getRealPath(key)); + } + + @Override + public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { + getProvider().delete(getProvider().getRealPath(key), callback, ctx); + } + + @Override + public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { + if (key.indexOf(Constants.PATH_SEPARATOR) < -1) { + this.createCurrentOnly(key, value, createMode); + return; + } + List nodes = getProvider().getNecessaryPaths(key); + for (int i = 0; i < nodes.size(); i++) { + try { +// this.deleteAllChildren(nodes.get(i)); + if (i == nodes.size() - 1) { + this.createCurrentOnly(nodes.get(i), value, createMode); + } else { + this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, CreateMode.PERSISTENT); + } + LOGGER.debug("node not exist and create:", nodes.get(i)); + } catch (KeeperException.NodeExistsException e) { + LOGGER.debug("create node exist:{}", nodes.get(i)); + } + } + } + + @Override + public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { + LOGGER.debug("deleteAllChildren:{}", key); + this.deleteChildren(getProvider().getRealPath(key), true); + } + + private void deleteChildren(final String path, final boolean deleteCurrentNode) throws KeeperException, InterruptedException { + LOGGER.debug("deleteChildren:{}", path); + List children; + try { + children = getProvider().getChildren(path); + } catch (KeeperException.NoNodeException e) { + LOGGER.warn("deleteChildren node not exist:{},e:{}", path, e.getMessage()); + return; + } + for (String child : children) { + this.deleteAllChildren(PathUtil.getRealPath(path, child)); + } + if (deleteCurrentNode) { + try { + this.deleteOnlyCurrent(path); + } catch (KeeperException.NotEmptyException e) { + LOGGER.warn("deleteCurrentNode exist children:{},e:{}", path, e.getMessage()); + deleteChildren(path, true); + } catch (KeeperException.NoNodeException e) { + LOGGER.warn("deleteCurrentNode node not exist:{},e:{}", path, e.getMessage()); + } + } + } + + /* + * delete the current node with force and delete the super node whose only child node is current node recursively + */ + @Override + public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { + LOGGER.debug("deleteCurrentBranch:{}", key); + if (key.indexOf(Constants.PATH_SEPARATOR) < -1) { + this.deleteOnlyCurrent(key); + return; + } + String path = getProvider().getRealPath(key); + this.deleteChildren(path, true); + String superPath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); + try { + this.deleteRecursively(superPath); + } catch (KeeperException.NotEmptyException e) { + LOGGER.warn("deleteCurrentBranch exist children:{},e:{}", path, e.getMessage()); + } catch (KeeperException.NoNodeException e) { + LOGGER.warn("deleteCurrentBranch NoNodeException:{},e:{}", superPath, e.getMessage()); + } + } + + private void deleteRecursively(final String path) throws KeeperException, InterruptedException { + LOGGER.debug("deleteRecursively:{}", path); + int index = path.lastIndexOf(Constants.PATH_SEPARATOR); + if (index == 0) { + this.deleteOnlyCurrent(path); + return; + } + String superPath = path.substring(0, index); + try { + this.deleteOnlyCurrent(path); + this.deleteRecursively(superPath); + } catch (KeeperException.NotEmptyException e) { + LOGGER.warn("deleteRecursively exist children:{},e:{}", path, e.getMessage()); + LOGGER.info("deleteRecursively {} exist other children:{}", path, this.getChildren(path)); + } + } +} diff --git a/src/main/resources/code/newzk/client/zookeeper/transaction/ZKTransaction.java b/src/main/resources/code/newzk/client/zookeeper/transaction/ZKTransaction.java new file mode 100644 index 0000000000000..e6e3d1294f056 --- /dev/null +++ b/src/main/resources/code/newzk/client/zookeeper/transaction/ZKTransaction.java @@ -0,0 +1,145 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction; + +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.OpResult; +import org.apache.zookeeper.Transaction; +import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/* + * @author lidongbo + * @since zookeeper 3.4.0 + */ +public class ZKTransaction { + private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction.class); + + private final Transaction transaction; + + private final String rootNode; + + public ZKTransaction(final String root, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.Holder holder) { + transaction = holder.getZooKeeper().transaction(); + rootNode = root; + LOGGER.debug("ZKTransaction root:{}", rootNode); + } + + /** + * create target node. + * + * @param path key + * @param data value + * @param acl acl + * @param createMode createMode + * @return ZKTransaction + */ + public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction create(final String path, final byte[] data, final List acl, final CreateMode createMode) { + this.transaction.create(io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil.getRealPath(rootNode, path), data, acl, createMode); + LOGGER.debug("wait create:{},data:{},acl:{},createMode:{}", new Object[]{path, data, acl, createMode}); + return this; + } + + /** + * delete target node. + * + * @param path key + * @return ZKTransaction + */ + public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction delete(final String path) { + return delete(path, Constants.VERSION); + } + + /** + * delete target node. + * + * @param path key + * @param version version + * @return ZKTransaction + */ + public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction delete(final String path, final int version) { + this.transaction.delete(io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil.getRealPath(rootNode, path), version); + LOGGER.debug("wait delete:{}", path); + return this; + } + + /** + * check target node. + * + * @param path key + * @return ZKTransaction + */ + public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction check(final String path) { + return check(path, Constants.VERSION); + } + + /** + * check target node. + * + * @param path key + * @param version version + * @return ZKTransaction + */ + public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction check(final String path, final int version) { + this.transaction.check(io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil.getRealPath(rootNode, path), version); + LOGGER.debug("wait check:{}", path); + return this; + } + + /** + * update target node. + * + * @param path key + * @param data data + * @return ZKTransaction + */ + public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction setData(final String path, final byte[] data) { + return setData(path, data, Constants.VERSION); + } + + /** + * update target node. + * + * @param path key + * @param data data + * @param version version + * @return ZKTransaction + */ + public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction setData(final String path, final byte[] data, final int version) { + this.transaction.setData(io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil.getRealPath(rootNode, path), data, version); + LOGGER.debug("wait setData:{},data:{}", path, data); + return this; + } + + /** + * commit. + * + * @return operation result + * @throws KeeperException Zookeeper Exception + * @throws InterruptedException InterruptedException + */ + public List commit() throws InterruptedException, KeeperException { + LOGGER.debug("ZKTransaction commit"); + return this.transaction.commit(); + } +} From 0e1d4d44ef44469016d97ecf1d76aed89ddfc7dd Mon Sep 17 00:00:00 2001 From: linjiaqi Date: Fri, 8 Jun 2018 13:45:45 +0800 Subject: [PATCH 147/351] shardingsphere netty backend feature --- .../proxy/backend/ShardingProxyClient.java | 8 ++-- .../common/SQLExecuteBackendHandler.java | 31 +++++-------- .../common/SQLPacketsBackendHandler.java | 29 ++++++------ .../backend/mysql/MySQLBackendHandler.java | 44 +++---------------- .../proxy/config/RuleRegistry.java | 28 ++++++------ .../mysql/packet/command/CommandPacket.java | 5 +++ .../text/fieldlist/ComFieldListPacket.java | 6 +-- .../text/query/ColumnDefinition41Packet.java | 14 +++--- .../command/text/query/ComQueryPacket.java | 25 +++++++---- .../mysql/packet/generic/EofPacket.java | 4 +- .../mysql/packet/generic/ErrPacket.java | 4 +- .../mysql/packet/generic/OKPacket.java | 4 +- 12 files changed, 87 insertions(+), 115 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java index ea6faa829f70b..c76109d830741 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java @@ -25,7 +25,6 @@ import java.util.concurrent.TimeoutException; import com.google.common.collect.Maps; -import com.zaxxer.hikari.HikariConfig; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.PooledByteBufAllocator; @@ -41,6 +40,7 @@ import io.netty.channel.pool.FixedChannelPool; import io.netty.channel.pool.SimpleChannelPool; import io.netty.channel.socket.nio.NioSocketChannel; +import io.shardingsphere.core.yaml.sharding.DataSourceParameter; import io.shardingsphere.proxy.backend.common.NettyChannelPoolHandler; import io.shardingsphere.proxy.config.DataScourceConfig; import io.shardingsphere.proxy.config.RuleRegistry; @@ -77,9 +77,9 @@ public final class ShardingProxyClient { * @throws InterruptedException interrupted exception */ public void start() throws MalformedURLException, InterruptedException { - Map dataSourceConfigurationMap = RuleRegistry.getInstance().getDataSourceConfigurationMap(); - for (Map.Entry each : dataSourceConfigurationMap.entrySet()) { - URL url = new URL(each.getValue().getJdbcUrl().replaceAll("jdbc:mysql:", "http:")); + Map dataSourceConfigurationMap = RuleRegistry.getInstance().getDataSourceConfigurationMap(); + for (Map.Entry each : dataSourceConfigurationMap.entrySet()) { + URL url = new URL(each.getValue().getUrl().replaceAll("jdbc:mysql:", "http:")); final String ip = url.getHost(); final int port = url.getPort(); final String database = url.getPath().substring(1); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java index fa225a10cd05f..ae105015b9b65 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java @@ -17,6 +17,17 @@ package io.shardingsphere.proxy.backend.common; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; + import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.constant.SQLType; import io.shardingsphere.core.exception.ShardingException; @@ -25,7 +36,6 @@ import io.shardingsphere.core.merger.QueryResult; import io.shardingsphere.core.parsing.SQLJudgeEngine; import io.shardingsphere.core.parsing.parser.sql.SQLStatement; -import io.shardingsphere.core.parsing.parser.sql.dml.insert.InsertStatement; import io.shardingsphere.core.routing.SQLExecutionUnit; import io.shardingsphere.core.routing.SQLRouteResult; import io.shardingsphere.core.routing.StatementRoutingEngine; @@ -35,10 +45,8 @@ import io.shardingsphere.proxy.config.RuleRegistry; import io.shardingsphere.proxy.metadata.ProxyShardingRefreshHandler; import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; -import io.shardingsphere.proxy.transport.mysql.constant.ColumnType; import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; -import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet; import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.FieldCountPacket; import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket; import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket; @@ -47,21 +55,6 @@ import lombok.Getter; import lombok.Setter; -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Future; - /** * SQL execute backend handler. * @@ -74,8 +67,6 @@ @Setter public final class SQLExecuteBackendHandler implements BackendHandler { - private static final Integer FETCH_ONE_ROW_A_TIME = Integer.MIN_VALUE; - private final String sql; private List connections; diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java index 88d5bd282dc3a..a563fd250e380 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java @@ -71,10 +71,6 @@ public final class SQLPacketsBackendHandler implements BackendHandler { private final CommandPacket commandPacket; - private final String sql; - - private int connectionId; - private final DatabaseType databaseType; private final boolean showSQL; @@ -89,10 +85,8 @@ public final class SQLPacketsBackendHandler implements BackendHandler { private boolean hasMoreResultValueFlag; - public SQLPacketsBackendHandler(final CommandPacket commandPacket, final String sql, final int connectionId, final DatabaseType databaseType, final boolean showSQL) { + public SQLPacketsBackendHandler(final CommandPacket commandPacket, final DatabaseType databaseType, final boolean showSQL) { this.commandPacket = commandPacket; - this.sql = sql; - this.connectionId = connectionId; this.databaseType = databaseType; this.showSQL = showSQL; isMerged = false; @@ -110,15 +104,15 @@ public CommandResponsePackets execute() { protected CommandResponsePackets executeForMasterSlave() { MasterSlaveRouter masterSlaveRouter = new MasterSlaveRouter(RuleRegistry.getInstance().getMasterSlaveRule()); - SQLStatement sqlStatement = new SQLJudgeEngine(sql).judge(); + SQLStatement sqlStatement = new SQLJudgeEngine(commandPacket.getSql()).judge(); String dataSourceName = masterSlaveRouter.route(sqlStatement.getType()).iterator().next(); synchronizedFuture = new SynchronizedFuture<>(1); - MySQLResultCache.getInstance().putFuture(connectionId, synchronizedFuture); - executeCommand(dataSourceName, connectionId, commandPacket); + MySQLResultCache.getInstance().putFuture(commandPacket.getConnectionId(), synchronizedFuture); + executeCommand(dataSourceName, commandPacket.getConnectionId(), commandPacket); //TODO timeout should be set. List queryResults = synchronizedFuture.get(CONNECT_TIMEOUT, TimeUnit.SECONDS); - MySQLResultCache.getInstance().deleteFuture(connectionId); + MySQLResultCache.getInstance().deleteFuture(commandPacket.getConnectionId()); List packets = new LinkedList<>(); for (QueryResult each : queryResults) { @@ -129,22 +123,25 @@ protected CommandResponsePackets executeForMasterSlave() { protected CommandResponsePackets executeForSharding() { StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), databaseType, showSQL); - SQLRouteResult routeResult = routingEngine.route(sql); + SQLRouteResult routeResult = routingEngine.route(commandPacket.getSql()); if (routeResult.getExecutionUnits().isEmpty()) { return new CommandResponsePackets(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); } synchronizedFuture = new SynchronizedFuture<>(routeResult.getExecutionUnits().size()); - MySQLResultCache.getInstance().putFuture(connectionId, synchronizedFuture); + MySQLResultCache.getInstance().putFuture(commandPacket.getConnectionId(), synchronizedFuture); for (SQLExecutionUnit each : routeResult.getExecutionUnits()) { if (commandPacket instanceof ComQueryPacket) { - ((ComQueryPacket) commandPacket).setSql(each.getSqlUnit().getSql()); + ComQueryPacket queryPacket = (ComQueryPacket) ((ComQueryPacket) commandPacket).clone(); + queryPacket.setSql(each.getSqlUnit().getSql()); + executeCommand(each.getDataSource(), queryPacket.getConnectionId(), queryPacket); + } else { + executeCommand(each.getDataSource(), commandPacket.getConnectionId(), commandPacket); } - executeCommand(each.getDataSource(), connectionId, commandPacket); } //TODO timeout should be set. List queryResults = synchronizedFuture.get(CONNECT_TIMEOUT, TimeUnit.SECONDS); - MySQLResultCache.getInstance().deleteFuture(connectionId); + MySQLResultCache.getInstance().deleteFuture(commandPacket.getConnectionId()); List packets = Lists.newArrayListWithCapacity(queryResults.size()); for (QueryResult each : queryResults) { diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java index e05cb5f36a840..12509cc99e2bd 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java @@ -30,7 +30,6 @@ import io.shardingsphere.proxy.backend.constant.AuthType; import io.shardingsphere.proxy.config.DataScourceConfig; import io.shardingsphere.proxy.transport.mysql.constant.CapabilityFlag; -import io.shardingsphere.proxy.transport.mysql.constant.ColumnType; import io.shardingsphere.proxy.transport.mysql.constant.ServerInfo; import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet; @@ -63,7 +62,7 @@ public void channelRead(final ChannelHandlerContext context, final Object messag MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload((ByteBuf) message); int sequenceId = mysqlPacketPayload.readInt1(); mysqlPacketPayload.getByteBuf().markReaderIndex(); - int header = mysqlPacketPayload.readInt1() & 0xFF; + int header = mysqlPacketPayload.readInt1(); if (AuthType.UN_AUTH == authType) { auth(context, sequenceId, header, mysqlPacketPayload); @@ -112,29 +111,19 @@ protected void auth(final ChannelHandlerContext context, final int sequenceId, f @Override protected void genericResponsePacket(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { mysqlQueryResult = new MySQLQueryResult(); + mysqlPacketPayload.getByteBuf().resetReaderIndex(); switch (header) { case OKPacket.HEADER: if (authType == AuthType.AUTHING) { authType = AuthType.AUTH_SUCCESS; } - long affectedRows = mysqlPacketPayload.readIntLenenc(); - long lastInsertId = mysqlPacketPayload.readIntLenenc(); - int statusFlags = mysqlPacketPayload.readInt2(); - int warnings = mysqlPacketPayload.readInt2(); - String info = mysqlPacketPayload.readStringEOF(); - log.debug("OKPacket[affectedRows={},lastInsertId={},statusFlags={},warnings={},info={}]", affectedRows, lastInsertId, statusFlags, warnings, info); - mysqlQueryResult.setGenericResponse(new OKPacket(sequenceId, affectedRows, lastInsertId, statusFlags, warnings, info)); + mysqlQueryResult.setGenericResponse(new OKPacket(sequenceId, mysqlPacketPayload)); break; case ErrPacket.HEADER: if (authType == AuthType.AUTHING) { authType = AuthType.AUTH_FAILED; } - int errorCode = mysqlPacketPayload.readInt2(); - String sqlStateMarker = mysqlPacketPayload.readStringFix(1); - String sqlState = mysqlPacketPayload.readStringFix(5); - String errorMessage = mysqlPacketPayload.readStringEOF(); - log.debug("ErrPacket[errorCode={},sqlStateMarker={},sqlState={},errorMessage={}]", errorCode, sqlStateMarker, sqlState, errorMessage); - mysqlQueryResult.setGenericResponse(new ErrPacket(sequenceId, errorCode, sqlStateMarker, sqlState, errorMessage)); + mysqlQueryResult.setGenericResponse(new ErrPacket(sequenceId, mysqlPacketPayload)); break; default: break; @@ -153,10 +142,8 @@ protected void genericResponsePacket(final ChannelHandlerContext context, final @Override protected void endOfFilePacket(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { - int warnings = mysqlPacketPayload.readInt2(); - int statusFlags = mysqlPacketPayload.readInt2(); - log.debug("EofPacket[warnings={},statusFlags={}]", warnings, statusFlags); - EofPacket eofPacket = new EofPacket(sequenceId, warnings, statusFlags); + mysqlPacketPayload.getByteBuf().resetReaderIndex(); + EofPacket eofPacket = new EofPacket(sequenceId, mysqlPacketPayload); if (mysqlQueryResult.isColumnFinished()) { mysqlQueryResult.setRowFinished(eofPacket); mysqlQueryResult = null; @@ -176,24 +163,7 @@ protected void executeCommandResponsePackets(final ChannelHandlerContext context mysqlQueryResult = new MySQLQueryResult(sequenceId, header); } else if (mysqlQueryResult.needColumnDefinition()) { mysqlPacketPayload.getByteBuf().resetReaderIndex(); - String catalog = mysqlPacketPayload.readStringLenenc(); - String schema = mysqlPacketPayload.readStringLenenc(); - String table = mysqlPacketPayload.readStringLenenc(); - String orgTable = mysqlPacketPayload.readStringLenenc(); - String name = mysqlPacketPayload.readStringLenenc(); - String orgName = mysqlPacketPayload.readStringLenenc(); - // fixed value and charset - mysqlPacketPayload.skipReserved(1 + 2); - int columnLength = mysqlPacketPayload.readInt4(); - int columnType = mysqlPacketPayload.readInt1(); - // field flags - mysqlPacketPayload.skipReserved(2); - int decimals = mysqlPacketPayload.readInt1(); - // filler value 0x00 0x00 - mysqlPacketPayload.skipReserved(2); - - ColumnDefinition41Packet columnDefinition = new ColumnDefinition41Packet(sequenceId, schema, table, orgTable, name, orgName, - columnLength, ColumnType.valueOfJDBCType(columnType), decimals); + ColumnDefinition41Packet columnDefinition = new ColumnDefinition41Packet(sequenceId, mysqlPacketPayload); mysqlQueryResult.addColumnDefinition(columnDefinition); } else { mysqlPacketPayload.getByteBuf().resetReaderIndex(); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java index 1c3583f853e42..d5d775122d64d 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java @@ -17,10 +17,21 @@ package io.shardingsphere.proxy.config; +import java.io.File; +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.Executors; + +import javax.sql.DataSource; + import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.MoreExecutors; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; + import io.shardingsphere.core.constant.ShardingProperties; import io.shardingsphere.core.constant.ShardingPropertiesConstant; import io.shardingsphere.core.exception.ShardingException; @@ -32,15 +43,6 @@ import io.shardingsphere.proxy.metadata.ProxyShardingMetaData; import lombok.Getter; -import javax.sql.DataSource; -import java.io.File; -import java.io.IOException; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; -import java.util.concurrent.Executors; - /** * Sharding rule registry. * @@ -59,7 +61,7 @@ public final class RuleRegistry { private final Map dataSourceMap; - private Map dataSourceConfigurationMap; + private Map dataSourceConfigurationMap; private final ShardingRule shardingRule; @@ -86,12 +88,10 @@ private RuleRegistry() { dataSourceMap = new HashMap<>(128, 1); Map dataSourceParameters = yamlProxyConfiguration.getDataSources(); for (Map.Entry entry : dataSourceParameters.entrySet()) { - dataSourceMap.put(entry.getKey(), getDataSource(entry.getValue())); - for (String each : dataSourceParameters.keySet()) { if (WITHOUT_JDBC) { - dataSourceConfigurationMap.put(each, getDataSourceConfiguration(dataSourceParameters.get(each))); + dataSourceConfigurationMap.put(entry.getKey(), entry.getValue()); } - dataSourceMap.put(each, getDataSource(dataSourceParameters.get(each))); + dataSourceMap.put(entry.getKey(), getDataSource(entry.getValue())); } shardingRule = yamlProxyConfiguration.obtainShardingRule(Collections.emptyList()); masterSlaveRule = yamlProxyConfiguration.obtainMasterSlaveRule(); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacket.java index f29d84325ff16..ef6177102b361 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacket.java @@ -20,6 +20,7 @@ import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacket; import lombok.Getter; +import lombok.Setter; /** * Command packet. @@ -28,6 +29,10 @@ * @author wangkai */ public abstract class CommandPacket extends MySQLPacket { + @Getter + @Setter + private String sql; + @Getter private final int connectionId; diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java index f012bd521b000..8879d625a1371 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java @@ -47,6 +47,7 @@ public ComFieldListPacket(final int sequenceId, final int connectionId, final My super(sequenceId, connectionId); table = mysqlPacketPayload.readStringNul(); fieldWildcard = mysqlPacketPayload.readStringEOF(); + setSql(String.format("SHOW COLUMNS FROM %s FROM %s", table, ShardingConstant.LOGIC_SCHEMA_NAME)); } @Override @@ -60,12 +61,11 @@ public void write(final MySQLPacketPayload mysqlPacketPayload) { public CommandResponsePackets execute() { log.debug("table name received for Sharding-Proxy: {}", table); log.debug("field wildcard received for Sharding-Proxy: {}", fieldWildcard); - String sql = String.format("SHOW COLUMNS FROM %s FROM %s", table, ShardingConstant.LOGIC_SCHEMA_NAME); // TODO use common database type if (RuleRegistry.WITHOUT_JDBC) { - return new SQLPacketsBackendHandler(this, sql, getConnectionId(), DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()).execute(); + return new SQLPacketsBackendHandler(this, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()).execute(); } else { - return new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()).execute(); + return new SQLExecuteBackendHandler(getSql(), DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()).execute(); } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ColumnDefinition41Packet.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ColumnDefinition41Packet.java index e464fc0916bb1..7ef21c19c64e8 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ColumnDefinition41Packet.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ColumnDefinition41Packet.java @@ -37,9 +37,9 @@ public final class ColumnDefinition41Packet extends MySQLPacket { private final int nextLength = 0x0c; - private final int characterSet = ServerInfo.CHARSET; + private int characterSet = ServerInfo.CHARSET; - private final int flags = 0; + private int flags; private final String schema; @@ -70,8 +70,8 @@ public ColumnDefinition41Packet(final int sequenceId, final String schema, final this.decimals = decimals; } - public ColumnDefinition41Packet(final MySQLPacketPayload mysqlPacketPayload) { - super(mysqlPacketPayload.readInt1()); + public ColumnDefinition41Packet(final int sequenceId, final MySQLPacketPayload mysqlPacketPayload) { + super(sequenceId); Preconditions.checkArgument(catalog.equals(mysqlPacketPayload.readStringLenenc())); schema = mysqlPacketPayload.readStringLenenc(); table = mysqlPacketPayload.readStringLenenc(); @@ -79,10 +79,12 @@ public ColumnDefinition41Packet(final MySQLPacketPayload mysqlPacketPayload) { name = mysqlPacketPayload.readStringLenenc(); orgName = mysqlPacketPayload.readStringLenenc(); Preconditions.checkArgument(nextLength == mysqlPacketPayload.readIntLenenc()); - Preconditions.checkArgument(characterSet == mysqlPacketPayload.readInt2()); + //TODO the characterSet maybe not always equal to ServerInfo.CHARSET + characterSet = mysqlPacketPayload.readInt2(); columnLength = mysqlPacketPayload.readInt4(); columnType = ColumnType.valueOf(mysqlPacketPayload.readInt1()); - Preconditions.checkArgument(flags == mysqlPacketPayload.readInt2()); + //TODO the flags maybe not always equal to 0 + flags = mysqlPacketPayload.readInt2(); decimals = mysqlPacketPayload.readInt1(); mysqlPacketPayload.skipReserved(2); } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java index d6adf3aa640b8..e8ac6f9e54561 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java @@ -28,7 +28,6 @@ import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacketType; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; -import lombok.Setter; import lombok.extern.slf4j.Slf4j; /** @@ -39,9 +38,7 @@ * @author linjiaqi */ @Slf4j -public final class ComQueryPacket extends CommandPacket { - @Setter - private String sql; +public final class ComQueryPacket extends CommandPacket implements Cloneable { private final SQLExecuteBackendHandler sqlExecuteBackendHandler; @@ -49,20 +46,30 @@ public final class ComQueryPacket extends CommandPacket { public ComQueryPacket(final int sequenceId, final int connectionId, final MySQLPacketPayload mysqlPacketPayload) { super(sequenceId, connectionId); - sql = mysqlPacketPayload.readStringEOF(); - sqlExecuteBackendHandler = new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()); - sqlPacketsBackendHandler = new SQLPacketsBackendHandler(this, sql, connectionId, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()); + setSql(mysqlPacketPayload.readStringEOF()); + sqlExecuteBackendHandler = new SQLExecuteBackendHandler(getSql(), DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()); + sqlPacketsBackendHandler = new SQLPacketsBackendHandler(this, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()); + } + + @Override + public Object clone() { + try { + return super.clone(); + } catch (CloneNotSupportedException e) { + log.error(e.getMessage(), e); + } + return null; } @Override public void write(final MySQLPacketPayload mysqlPacketPayload) { mysqlPacketPayload.writeInt1(CommandPacketType.COM_QUERY.getValue()); - mysqlPacketPayload.writeStringEOF(sql); + mysqlPacketPayload.writeStringEOF(getSql()); } @Override public CommandResponsePackets execute() { - log.debug("COM_QUERY received for Sharding-Proxy: {}", sql); + log.debug("COM_QUERY received for Sharding-Proxy: {}", getSql()); if (RuleRegistry.WITHOUT_JDBC) { return sqlPacketsBackendHandler.execute(); } else { diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/EofPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/EofPacket.java index 63cfcee04119a..794efe90f3ae2 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/EofPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/EofPacket.java @@ -44,8 +44,8 @@ public EofPacket(final int sequenceId, final int warnings, final int statusFlags this.statusFlags = statusFlags; } - public EofPacket(final MySQLPacketPayload mysqlPacketPayload) { - super(mysqlPacketPayload.readInt1()); + public EofPacket(final int sequenceId, final MySQLPacketPayload mysqlPacketPayload) { + super(sequenceId); Preconditions.checkArgument(HEADER == mysqlPacketPayload.readInt1()); warnings = mysqlPacketPayload.readInt2(); statusFlags = mysqlPacketPayload.readInt2(); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/ErrPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/ErrPacket.java index 05061fc1021d5..9d5832ef0b6fb 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/ErrPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/ErrPacket.java @@ -50,8 +50,8 @@ public ErrPacket(final int sequenceId, final int errorCode, final String sqlStat this.errorMessage = errorMessage; } - public ErrPacket(final MySQLPacketPayload mysqlPacketPayload) { - super(mysqlPacketPayload.readInt1()); + public ErrPacket(final int sequenceId, final MySQLPacketPayload mysqlPacketPayload) { + super(sequenceId); Preconditions.checkArgument(HEADER == mysqlPacketPayload.readInt1()); errorCode = mysqlPacketPayload.readInt2(); sqlStateMarker = mysqlPacketPayload.readStringFix(1); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/OKPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/OKPacket.java index 27a1718e3e431..6bbfbf60eb08e 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/OKPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/OKPacket.java @@ -53,8 +53,8 @@ public OKPacket(final int sequenceId, final long affectedRows, final long lastIn this.info = info; } - public OKPacket(final MySQLPacketPayload mysqlPacketPayload) { - super(mysqlPacketPayload.readInt1()); + public OKPacket(final int sequenceId, final MySQLPacketPayload mysqlPacketPayload) { + super(sequenceId); Preconditions.checkArgument(HEADER == mysqlPacketPayload.readInt1()); affectedRows = mysqlPacketPayload.readIntLenenc(); lastInsertId = mysqlPacketPayload.readIntLenenc(); From e899bc56b2ca817384c4a49a24c95d220e9ab6ce Mon Sep 17 00:00:00 2001 From: linjiaqi Date: Fri, 8 Jun 2018 14:17:30 +0800 Subject: [PATCH 148/351] shardingsphere netty backend feature --- .../java/io/shardingsphere/proxy/config/RuleRegistry.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java index 4ed3d2cc6f3ba..f7d68a3f258ba 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java @@ -35,6 +35,7 @@ import java.io.File; import java.io.IOException; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.concurrent.Executors; @@ -85,13 +86,10 @@ private RuleRegistry() { transactionType = TransactionType.findByValue(yamlProxyConfiguration.getTransactionMode()); dataSourceMap = ProxyRawDataSourceFactory.create(transactionType, yamlProxyConfiguration); dataSourceConfigurationMap = new HashMap<>(128, 1); - dataSourceMap = new HashMap<>(128, 1); - Map dataSourceParameters = yamlProxyConfiguration.getDataSources(); - for (Map.Entry entry : dataSourceParameters.entrySet()) { + for (Map.Entry entry : yamlProxyConfiguration.getDataSources().entrySet()) { if (WITHOUT_JDBC) { dataSourceConfigurationMap.put(entry.getKey(), entry.getValue()); } - dataSourceMap.put(entry.getKey(), getDataSource(entry.getValue())); } shardingRule = yamlProxyConfiguration.obtainShardingRule(Collections.emptyList()); masterSlaveRule = yamlProxyConfiguration.obtainMasterSlaveRule(); From a6c726120ce6822916fc3646891c6ceb15ffaeb7 Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 8 Jun 2018 18:37:50 +0800 Subject: [PATCH 149/351] trans --- LICENSE | 201 ----------- README.md | 7 - build.gradle | 120 ------- settings.gradle | 2 - .../newzk/NewZookeeperRegistryCenter.java | 0 .../newzk/client/action/Callback.java | 0 .../new _zk}/newzk/client/action/IAction.java | 0 .../new _zk}/newzk/client/action/IClient.java | 0 .../newzk/client/action/IExecStrategy.java | 0 .../newzk/client/action/IGroupAction.java | 0 .../newzk/client/action/IProvider.java | 0 .../newzk/client/cache/CacheStrategy.java | 0 .../new _zk}/newzk/client/cache/PathNode.java | 0 .../newzk/client/cache/PathStatus.java | 0 .../new _zk}/newzk/client/cache/PathTree.java | 0 .../newzk/client/election/LeaderElection.java | 0 .../newzk/client/retry/AsyncRetryCenter.java | 0 .../client/retry/DelayPolicyExecutor.java | 0 .../newzk/client/retry/DelayRetryPolicy.java | 0 .../newzk/client/retry/RetryThread.java | 0 .../newzk/client/utility/Constants.java | 0 .../newzk/client/utility/PathUtil.java | 0 .../newzk/client/utility/StringUtil.java | 0 .../newzk/client/zookeeper/CacheClient.java | 0 .../newzk/client/zookeeper/ClientFactory.java | 0 .../newzk/client/zookeeper/UsualClient.java | 0 .../client/zookeeper/base/BaseClient.java | 0 .../zookeeper/base/BaseClientFactory.java | 0 .../client/zookeeper/base/BaseContext.java | 0 .../client/zookeeper/base/BaseOperation.java | 0 .../client/zookeeper/base/BaseProvider.java | 0 .../client/zookeeper/base/BaseStrategy.java | 0 .../newzk/client/zookeeper/base/Holder.java | 0 .../operation/CreateAllNeedOperation.java | 0 .../operation/CreateCurrentOperation.java | 0 .../operation/DeleteAllChildrenOperation.java | 0 .../DeleteCurrentBranchOperation.java | 0 .../operation/DeleteCurrentOperation.java | 0 .../zookeeper/operation/UpdateOperation.java | 0 .../client/zookeeper/section/Callable.java | 0 .../zookeeper/section/ClientContext.java | 0 .../client/zookeeper/section/ClientTask.java | 0 .../client/zookeeper/section/Connection.java | 0 .../client/zookeeper/section/Listener.java | 0 .../zookeeper/section/StrategyType.java | 0 .../zookeeper/section/WatcherCreator.java | 0 .../strategy/AsyncRetryStrategy.java | 0 .../strategy/ContentionStrategy.java | 0 .../zookeeper/strategy/SyncRetryStrategy.java | 0 .../strategy/TransactionContendStrategy.java | 0 .../zookeeper/strategy/UsualStrategy.java | 0 .../zookeeper/transaction/ZKTransaction.java | 0 .../com/saaavsaaa/client/action/Callback.java | 8 - .../com/saaavsaaa/client/action/IAction.java | 24 -- .../client/action/ICallbackProvider.java | 14 - .../com/saaavsaaa/client/action/IClient.java | 32 -- .../client/action/IExecStrategy.java | 8 - .../saaavsaaa/client/action/IGroupAction.java | 19 - .../saaavsaaa/client/action/IProvider.java | 36 -- .../saaavsaaa/client/cache/CacheStrategy.java | 10 - .../com/saaavsaaa/client/cache/PathNode.java | 98 ------ .../saaavsaaa/client/cache/PathStatus.java | 9 - .../com/saaavsaaa/client/cache/PathTree.java | 332 ------------------ .../client/election/LeaderElection.java | 90 ----- .../client/retry/AsyncRetryCenter.java | 49 --- .../client/retry/DelayPolicyExecutor.java | 48 --- .../client/retry/DelayRetryPolicy.java | 43 --- .../saaavsaaa/client/retry/RetryCount.java | 50 --- .../saaavsaaa/client/retry/RetryThread.java | 86 ----- .../saaavsaaa/client/utility/PathUtil.java | 166 --------- .../saaavsaaa/client/utility/Properties.java | 57 --- .../saaavsaaa/client/utility/StringUtil.java | 10 - .../client/utility/constant/Constants.java | 20 -- .../client/zookeeper/CacheClient.java | 118 ------- .../client/zookeeper/ClientFactory.java | 92 ----- .../client/zookeeper/UsualClient.java | 123 ------- .../client/zookeeper/base/BaseClient.java | 196 ----------- .../zookeeper/base/BaseClientFactory.java | 36 -- .../client/zookeeper/base/BaseContext.java | 42 --- .../client/zookeeper/base/BaseOperation.java | 69 ---- .../client/zookeeper/base/BaseProvider.java | 156 -------- .../client/zookeeper/base/BaseStrategy.java | 24 -- .../client/zookeeper/base/Holder.java | 102 ------ .../operation/CreateAllNeedOperation.java | 33 -- .../operation/CreateCurrentOperation.java | 32 -- .../operation/DeleteAllChildrenOperation.java | 28 -- .../DeleteCurrentBranchOperation.java | 28 -- .../operation/DeleteCurrentOperation.java | 26 -- .../zookeeper/operation/UpdateOperation.java | 29 -- .../zookeeper/provider/RetryProvider.java | 86 ----- .../client/zookeeper/section/Callable.java | 71 ---- .../zookeeper/section/ClientContext.java | 50 --- .../client/zookeeper/section/ClientTask.java | 29 -- .../client/zookeeper/section/Connection.java | 81 ----- .../client/zookeeper/section/Listener.java | 33 -- .../zookeeper/section/StrategyType.java | 12 - .../zookeeper/section/WatcherCreator.java | 26 -- .../strategy/AllAsyncRetryStrategy.java | 52 --- .../strategy/AsyncRetryStrategy.java | 58 --- .../strategy/ContentionStrategy.java | 242 ------------- .../zookeeper/strategy/SyncRetryStrategy.java | 141 -------- .../strategy/TransactionContendStrategy.java | 138 -------- .../zookeeper/strategy/UsualStrategy.java | 164 --------- .../zookeeper/strategy/VersionStrategy.java | 8 - .../zookeeper/transaction/ZKTransaction.java | 69 ---- src/main/resources/client.properties | 5 - .../resources/code/OrchestrationFacade.java | 170 --------- .../code/ZookeeperConfiguration.java | 83 ----- src/main/resources/log4j.properties | 31 -- .../client/retry/AsyncRetryCenterTest.java | 75 ---- .../saaavsaaa/client/retry/TestCallable.java | 34 -- .../retry/TestCreateCurrentOperation.java | 33 -- .../zookeeper/AsyncRetryStrategyTest.java | 32 -- .../client/zookeeper/CacheClientTest.java | 17 - .../client/zookeeper/CacheWathClientTest.java | 18 - .../client/zookeeper/ClientsTest.java | 158 --------- .../zookeeper/ContentionStrategyTest.java | 54 --- .../zookeeper/SyncRetryStrategyTest.java | 230 ------------ .../client/zookeeper/TestSupport.java | 28 -- .../client/zookeeper/UsualClientTest.java | 80 ----- .../zookeeper/UsualWatchClientTest.java | 19 - .../client/zookeeper/base/BaseClientTest.java | 283 --------------- 122 files changed, 5283 deletions(-) delete mode 100644 LICENSE delete mode 100644 README.md delete mode 100644 build.gradle delete mode 100644 settings.gradle rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/NewZookeeperRegistryCenter.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/action/Callback.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/action/IAction.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/action/IClient.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/action/IExecStrategy.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/action/IGroupAction.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/action/IProvider.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/cache/CacheStrategy.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/cache/PathNode.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/cache/PathStatus.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/cache/PathTree.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/election/LeaderElection.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/retry/AsyncRetryCenter.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/retry/DelayPolicyExecutor.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/retry/DelayRetryPolicy.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/retry/RetryThread.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/utility/Constants.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/utility/PathUtil.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/utility/StringUtil.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/CacheClient.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/ClientFactory.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/UsualClient.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/base/BaseClient.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/base/BaseClientFactory.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/base/BaseContext.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/base/BaseOperation.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/base/BaseProvider.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/base/BaseStrategy.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/base/Holder.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/operation/CreateAllNeedOperation.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/operation/CreateCurrentOperation.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/operation/DeleteCurrentBranchOperation.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/operation/DeleteCurrentOperation.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/operation/UpdateOperation.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/section/Callable.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/section/ClientContext.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/section/ClientTask.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/section/Connection.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/section/Listener.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/section/StrategyType.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/section/WatcherCreator.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/strategy/ContentionStrategy.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/strategy/SyncRetryStrategy.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/strategy/TransactionContendStrategy.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/strategy/UsualStrategy.java (100%) rename {src/main/resources/code => sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk}/newzk/client/zookeeper/transaction/ZKTransaction.java (100%) delete mode 100644 src/main/java/com/saaavsaaa/client/action/Callback.java delete mode 100644 src/main/java/com/saaavsaaa/client/action/IAction.java delete mode 100644 src/main/java/com/saaavsaaa/client/action/ICallbackProvider.java delete mode 100644 src/main/java/com/saaavsaaa/client/action/IClient.java delete mode 100644 src/main/java/com/saaavsaaa/client/action/IExecStrategy.java delete mode 100644 src/main/java/com/saaavsaaa/client/action/IGroupAction.java delete mode 100644 src/main/java/com/saaavsaaa/client/action/IProvider.java delete mode 100644 src/main/java/com/saaavsaaa/client/cache/CacheStrategy.java delete mode 100644 src/main/java/com/saaavsaaa/client/cache/PathNode.java delete mode 100644 src/main/java/com/saaavsaaa/client/cache/PathStatus.java delete mode 100644 src/main/java/com/saaavsaaa/client/cache/PathTree.java delete mode 100644 src/main/java/com/saaavsaaa/client/election/LeaderElection.java delete mode 100644 src/main/java/com/saaavsaaa/client/retry/AsyncRetryCenter.java delete mode 100644 src/main/java/com/saaavsaaa/client/retry/DelayPolicyExecutor.java delete mode 100644 src/main/java/com/saaavsaaa/client/retry/DelayRetryPolicy.java delete mode 100644 src/main/java/com/saaavsaaa/client/retry/RetryCount.java delete mode 100644 src/main/java/com/saaavsaaa/client/retry/RetryThread.java delete mode 100644 src/main/java/com/saaavsaaa/client/utility/PathUtil.java delete mode 100644 src/main/java/com/saaavsaaa/client/utility/Properties.java delete mode 100644 src/main/java/com/saaavsaaa/client/utility/StringUtil.java delete mode 100644 src/main/java/com/saaavsaaa/client/utility/constant/Constants.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/base/BaseContext.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/section/Callable.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/section/ClientContext.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/section/ClientTask.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/section/Connection.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/section/Listener.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/section/StrategyType.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/section/WatcherCreator.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/strategy/VersionStrategy.java delete mode 100644 src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java delete mode 100644 src/main/resources/client.properties delete mode 100644 src/main/resources/code/OrchestrationFacade.java delete mode 100644 src/main/resources/code/ZookeeperConfiguration.java delete mode 100644 src/main/resources/log4j.properties delete mode 100644 src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java delete mode 100644 src/test/java/com/saaavsaaa/client/retry/TestCallable.java delete mode 100644 src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java delete mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java delete mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java delete mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java delete mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java delete mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java delete mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java delete mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java delete mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java delete mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java delete mode 100644 src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 261eeb9e9f8b2..0000000000000 --- a/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/README.md b/README.md deleted file mode 100644 index b37dac1eb5c67..0000000000000 --- a/README.md +++ /dev/null @@ -1,7 +0,0 @@ -# ZookeeperClient - -UsualStrategy -SyncRetryStrategy -ContentionStrategy -AsyncRetryStrategy -AllAsyncRetryStrategy \ No newline at end of file diff --git a/build.gradle b/build.gradle deleted file mode 100644 index 1e10bbb175ab8..0000000000000 --- a/build.gradle +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright 2013 Chris Banes - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -group 'com.saaavsaaa' -//version '1.0-SNAPSHOT' -version '2.1-RELEASE' - -apply plugin: 'idea' -apply plugin: 'java' -apply plugin: 'maven' -apply plugin: 'signing' - -repositories { - mavenCentral() -} - -dependencies { - // https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -// compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.3.0' -// compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.4.12' - compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.5.3-beta' - - // https://mvnrepository.com/artifact/org.slf4j/slf4j-api -// compile group: 'org.slf4j', name: 'slf4j-api', version: '1.6.1' - - - testCompile group: 'junit', name: 'junit', version: '4.11' - // https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -// testCompile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.4.12' - testCompile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.5.3-beta' -// testCompile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.3.0' -// testCompile group: 'org.slf4j', name: 'slf4j-api', version: '1.6.1' -} - -uploadArchives { - repositories { - mavenDeployer { - //为Pom文件做数字签名 - beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } - - if (isReleaseBuild()){ - repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2") { - authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) - } - } else { - repository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { - authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) - } - } - - pom.project { - name project.name - packaging 'jar' - description 'zk client' - url 'https://github.com/saaavsaaa/ZookeeperClient' - - scm { - url 'scm:git@github.com:saaavsaaa/ZookeeperClient.git' - connection 'scm:git@github.com:saaavsaaa/ZookeeperClient.git' - developerConnection 'git@github.com:saaavsaaa/ZookeeperClient.git' - } - - licenses { - license { - name 'The Apache Software License, Version 2.0' - url 'http://www.apache.org/licenses/LICENSE-2.0.txt' - distribution 'repo' - } - } - - developers { - developer { - id 'saaav' - name 'lidb' - } - } - } - } - } -} - -task javadocJar(type: Jar, dependsOn: javadoc) { - classifier = 'javadoc' - from 'build/docs/javadoc' -} - -task sourcesJar(type: Jar) { - classifier = 'sources' - from sourceSets.main.allSource -} - -def isReleaseBuild() { - return version.contains("SNAPSHOT") == false -} - -artifacts { - archives jar - archives javadocJar - archives sourcesJar -} - -signing { - sign configurations.archives -} - -// https://issues.sonatype.org/browse/OSSRH-39239 \ No newline at end of file diff --git a/settings.gradle b/settings.gradle deleted file mode 100644 index b93534156f452..0000000000000 --- a/settings.gradle +++ /dev/null @@ -1,2 +0,0 @@ -rootProject.name = 'zookeeper-client' - diff --git a/src/main/resources/code/newzk/NewZookeeperRegistryCenter.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/NewZookeeperRegistryCenter.java similarity index 100% rename from src/main/resources/code/newzk/NewZookeeperRegistryCenter.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/NewZookeeperRegistryCenter.java diff --git a/src/main/resources/code/newzk/client/action/Callback.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/Callback.java similarity index 100% rename from src/main/resources/code/newzk/client/action/Callback.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/Callback.java diff --git a/src/main/resources/code/newzk/client/action/IAction.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IAction.java similarity index 100% rename from src/main/resources/code/newzk/client/action/IAction.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IAction.java diff --git a/src/main/resources/code/newzk/client/action/IClient.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IClient.java similarity index 100% rename from src/main/resources/code/newzk/client/action/IClient.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IClient.java diff --git a/src/main/resources/code/newzk/client/action/IExecStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IExecStrategy.java similarity index 100% rename from src/main/resources/code/newzk/client/action/IExecStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IExecStrategy.java diff --git a/src/main/resources/code/newzk/client/action/IGroupAction.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IGroupAction.java similarity index 100% rename from src/main/resources/code/newzk/client/action/IGroupAction.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IGroupAction.java diff --git a/src/main/resources/code/newzk/client/action/IProvider.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IProvider.java similarity index 100% rename from src/main/resources/code/newzk/client/action/IProvider.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IProvider.java diff --git a/src/main/resources/code/newzk/client/cache/CacheStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/CacheStrategy.java similarity index 100% rename from src/main/resources/code/newzk/client/cache/CacheStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/CacheStrategy.java diff --git a/src/main/resources/code/newzk/client/cache/PathNode.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/PathNode.java similarity index 100% rename from src/main/resources/code/newzk/client/cache/PathNode.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/PathNode.java diff --git a/src/main/resources/code/newzk/client/cache/PathStatus.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/PathStatus.java similarity index 100% rename from src/main/resources/code/newzk/client/cache/PathStatus.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/PathStatus.java diff --git a/src/main/resources/code/newzk/client/cache/PathTree.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/PathTree.java similarity index 100% rename from src/main/resources/code/newzk/client/cache/PathTree.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/PathTree.java diff --git a/src/main/resources/code/newzk/client/election/LeaderElection.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/election/LeaderElection.java similarity index 100% rename from src/main/resources/code/newzk/client/election/LeaderElection.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/election/LeaderElection.java diff --git a/src/main/resources/code/newzk/client/retry/AsyncRetryCenter.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/AsyncRetryCenter.java similarity index 100% rename from src/main/resources/code/newzk/client/retry/AsyncRetryCenter.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/AsyncRetryCenter.java diff --git a/src/main/resources/code/newzk/client/retry/DelayPolicyExecutor.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/DelayPolicyExecutor.java similarity index 100% rename from src/main/resources/code/newzk/client/retry/DelayPolicyExecutor.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/DelayPolicyExecutor.java diff --git a/src/main/resources/code/newzk/client/retry/DelayRetryPolicy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/DelayRetryPolicy.java similarity index 100% rename from src/main/resources/code/newzk/client/retry/DelayRetryPolicy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/DelayRetryPolicy.java diff --git a/src/main/resources/code/newzk/client/retry/RetryThread.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/RetryThread.java similarity index 100% rename from src/main/resources/code/newzk/client/retry/RetryThread.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/RetryThread.java diff --git a/src/main/resources/code/newzk/client/utility/Constants.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/utility/Constants.java similarity index 100% rename from src/main/resources/code/newzk/client/utility/Constants.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/utility/Constants.java diff --git a/src/main/resources/code/newzk/client/utility/PathUtil.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/utility/PathUtil.java similarity index 100% rename from src/main/resources/code/newzk/client/utility/PathUtil.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/utility/PathUtil.java diff --git a/src/main/resources/code/newzk/client/utility/StringUtil.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/utility/StringUtil.java similarity index 100% rename from src/main/resources/code/newzk/client/utility/StringUtil.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/utility/StringUtil.java diff --git a/src/main/resources/code/newzk/client/zookeeper/CacheClient.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/CacheClient.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/CacheClient.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/CacheClient.java diff --git a/src/main/resources/code/newzk/client/zookeeper/ClientFactory.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/ClientFactory.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/ClientFactory.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/ClientFactory.java diff --git a/src/main/resources/code/newzk/client/zookeeper/UsualClient.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/UsualClient.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/UsualClient.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/UsualClient.java diff --git a/src/main/resources/code/newzk/client/zookeeper/base/BaseClient.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseClient.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/base/BaseClient.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseClient.java diff --git a/src/main/resources/code/newzk/client/zookeeper/base/BaseClientFactory.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseClientFactory.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/base/BaseClientFactory.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseClientFactory.java diff --git a/src/main/resources/code/newzk/client/zookeeper/base/BaseContext.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseContext.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/base/BaseContext.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseContext.java diff --git a/src/main/resources/code/newzk/client/zookeeper/base/BaseOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseOperation.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/base/BaseOperation.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseOperation.java diff --git a/src/main/resources/code/newzk/client/zookeeper/base/BaseProvider.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseProvider.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/base/BaseProvider.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseProvider.java diff --git a/src/main/resources/code/newzk/client/zookeeper/base/BaseStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseStrategy.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/base/BaseStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseStrategy.java diff --git a/src/main/resources/code/newzk/client/zookeeper/base/Holder.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/Holder.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/base/Holder.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/Holder.java diff --git a/src/main/resources/code/newzk/client/zookeeper/operation/CreateAllNeedOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/CreateAllNeedOperation.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/operation/CreateAllNeedOperation.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/CreateAllNeedOperation.java diff --git a/src/main/resources/code/newzk/client/zookeeper/operation/CreateCurrentOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/CreateCurrentOperation.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/operation/CreateCurrentOperation.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/CreateCurrentOperation.java diff --git a/src/main/resources/code/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java diff --git a/src/main/resources/code/newzk/client/zookeeper/operation/DeleteCurrentBranchOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/DeleteCurrentBranchOperation.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/operation/DeleteCurrentBranchOperation.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/DeleteCurrentBranchOperation.java diff --git a/src/main/resources/code/newzk/client/zookeeper/operation/DeleteCurrentOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/DeleteCurrentOperation.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/operation/DeleteCurrentOperation.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/DeleteCurrentOperation.java diff --git a/src/main/resources/code/newzk/client/zookeeper/operation/UpdateOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/UpdateOperation.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/operation/UpdateOperation.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/UpdateOperation.java diff --git a/src/main/resources/code/newzk/client/zookeeper/section/Callable.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/Callable.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/section/Callable.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/Callable.java diff --git a/src/main/resources/code/newzk/client/zookeeper/section/ClientContext.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/ClientContext.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/section/ClientContext.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/ClientContext.java diff --git a/src/main/resources/code/newzk/client/zookeeper/section/ClientTask.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/ClientTask.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/section/ClientTask.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/ClientTask.java diff --git a/src/main/resources/code/newzk/client/zookeeper/section/Connection.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/Connection.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/section/Connection.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/Connection.java diff --git a/src/main/resources/code/newzk/client/zookeeper/section/Listener.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/Listener.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/section/Listener.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/Listener.java diff --git a/src/main/resources/code/newzk/client/zookeeper/section/StrategyType.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/StrategyType.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/section/StrategyType.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/StrategyType.java diff --git a/src/main/resources/code/newzk/client/zookeeper/section/WatcherCreator.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/WatcherCreator.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/section/WatcherCreator.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/WatcherCreator.java diff --git a/src/main/resources/code/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java diff --git a/src/main/resources/code/newzk/client/zookeeper/strategy/ContentionStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/ContentionStrategy.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/strategy/ContentionStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/ContentionStrategy.java diff --git a/src/main/resources/code/newzk/client/zookeeper/strategy/SyncRetryStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/SyncRetryStrategy.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/strategy/SyncRetryStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/SyncRetryStrategy.java diff --git a/src/main/resources/code/newzk/client/zookeeper/strategy/TransactionContendStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/TransactionContendStrategy.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/strategy/TransactionContendStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/TransactionContendStrategy.java diff --git a/src/main/resources/code/newzk/client/zookeeper/strategy/UsualStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/UsualStrategy.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/strategy/UsualStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/UsualStrategy.java diff --git a/src/main/resources/code/newzk/client/zookeeper/transaction/ZKTransaction.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/transaction/ZKTransaction.java similarity index 100% rename from src/main/resources/code/newzk/client/zookeeper/transaction/ZKTransaction.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/transaction/ZKTransaction.java diff --git a/src/main/java/com/saaavsaaa/client/action/Callback.java b/src/main/java/com/saaavsaaa/client/action/Callback.java deleted file mode 100644 index 5f2454b53b69a..0000000000000 --- a/src/main/java/com/saaavsaaa/client/action/Callback.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.saaavsaaa.client.action; - -/** - * Created by aaa - */ -public interface Callback { - void processResult(); -} diff --git a/src/main/java/com/saaavsaaa/client/action/IAction.java b/src/main/java/com/saaavsaaa/client/action/IAction.java deleted file mode 100644 index 50bfc2bb37861..0000000000000 --- a/src/main/java/com/saaavsaaa/client/action/IAction.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.saaavsaaa.client.action; - -import org.apache.zookeeper.AsyncCallback; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.Watcher; - -import java.util.List; - -/** - * Created by aaa - */ -public interface IAction { - String getDataString(final String key) throws KeeperException, InterruptedException; - byte[] getData(final String key) throws KeeperException, InterruptedException; - void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException; - boolean checkExists(final String key) throws KeeperException, InterruptedException; - boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException; - List getChildren(final String key) throws KeeperException, InterruptedException; - void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException; - void update(final String key, final String value) throws KeeperException, InterruptedException; - void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException; - void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException; -} diff --git a/src/main/java/com/saaavsaaa/client/action/ICallbackProvider.java b/src/main/java/com/saaavsaaa/client/action/ICallbackProvider.java deleted file mode 100644 index 7d0136d2e4a83..0000000000000 --- a/src/main/java/com/saaavsaaa/client/action/ICallbackProvider.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.saaavsaaa.client.action; - -import org.apache.zookeeper.AsyncCallback; -import org.apache.zookeeper.CreateMode; - -/** - * Created by aaa - */ -public interface ICallbackProvider { - void exists(String path, AsyncCallback.StatCallback cb, Object ctx); - void getChildren(String path, AsyncCallback.ChildrenCallback cb, Object ctx); - void createCurrentOnly(final String path, byte data[], CreateMode createMode, AsyncCallback.StringCallback cb, Object ctx); - void update(final String path, byte data[], AsyncCallback.StatCallback cb, Object ctx); -} diff --git a/src/main/java/com/saaavsaaa/client/action/IClient.java b/src/main/java/com/saaavsaaa/client/action/IClient.java deleted file mode 100644 index 9b2d93f532774..0000000000000 --- a/src/main/java/com/saaavsaaa/client/action/IClient.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.saaavsaaa.client.action; - -import com.saaavsaaa.client.zookeeper.section.Listener; -import com.saaavsaaa.client.zookeeper.section.StrategyType; -import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; - -/** - * Created by aaa - */ -public interface IClient extends IAction, IGroupAction{ - void start() throws IOException, InterruptedException; - boolean blockUntilConnected(int wait, TimeUnit units) throws InterruptedException; - void close(); - void registerWatch(String key, Listener listener); - void unregisterWatch(String key); - void useExecStrategy(StrategyType strategyType); - - ZKTransaction transaction(); - /* - void createNamespace(); - void deleteNamespace(); - - Watcher registerWatch(Listener listener); - void setRootNode(String namespace); - - void setAuthorities(String scheme, byte[] auth); - ZooKeeper getZooKeeper(); - */ -} diff --git a/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java b/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java deleted file mode 100644 index aa36f466edf94..0000000000000 --- a/src/main/java/com/saaavsaaa/client/action/IExecStrategy.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.saaavsaaa.client.action; - -/** - * Created by aaa - */ -public interface IExecStrategy extends IAction, IGroupAction { - IProvider getProvider(); -} diff --git a/src/main/java/com/saaavsaaa/client/action/IGroupAction.java b/src/main/java/com/saaavsaaa/client/action/IGroupAction.java deleted file mode 100644 index 3e2a7f6a18227..0000000000000 --- a/src/main/java/com/saaavsaaa/client/action/IGroupAction.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.saaavsaaa.client.action; - -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; - -/** - * Created by aaa - */ -public interface IGroupAction { - - void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException ; - - void deleteAllChildren(final String key) throws KeeperException, InterruptedException ; - - /* - * delete the current node with force and delete the super node whose only child node is current node recursively - */ - void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException ; -} diff --git a/src/main/java/com/saaavsaaa/client/action/IProvider.java b/src/main/java/com/saaavsaaa/client/action/IProvider.java deleted file mode 100644 index 6ca594389d7c5..0000000000000 --- a/src/main/java/com/saaavsaaa/client/action/IProvider.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.saaavsaaa.client.action; - -import com.saaavsaaa.client.election.LeaderElection; -import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; -import org.apache.zookeeper.AsyncCallback; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.Watcher; - -import java.util.List; -import java.util.Stack; - -/** - * Created by aaa - */ -public interface IProvider { - String getDataString(final String key) throws KeeperException, InterruptedException; - byte[] getData(final String key) throws KeeperException, InterruptedException; - void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException; - boolean exists(final String key) throws KeeperException, InterruptedException; - boolean exists(final String key, final Watcher watcher) throws KeeperException, InterruptedException; - List getChildren(final String key) throws KeeperException, InterruptedException; - void create(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException; - void update(final String key, final String value) throws KeeperException, InterruptedException; - void delete(final String key) throws KeeperException, InterruptedException; - void delete(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException; - - String getRealPath(final String path); - List getNecessaryPaths(final String key); - Stack getDeletingPaths(final String key); - void executeContention(final LeaderElection election) throws KeeperException, InterruptedException; - -// void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException; - - void resetConnection(); -} diff --git a/src/main/java/com/saaavsaaa/client/cache/CacheStrategy.java b/src/main/java/com/saaavsaaa/client/cache/CacheStrategy.java deleted file mode 100644 index b5d446cc17c60..0000000000000 --- a/src/main/java/com/saaavsaaa/client/cache/CacheStrategy.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.saaavsaaa.client.cache; - -/** - * Created by aaa - */ -public enum CacheStrategy { - NONE, - WATCH, - ALL -} diff --git a/src/main/java/com/saaavsaaa/client/cache/PathNode.java b/src/main/java/com/saaavsaaa/client/cache/PathNode.java deleted file mode 100644 index 89f55675f261c..0000000000000 --- a/src/main/java/com/saaavsaaa/client/cache/PathNode.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.saaavsaaa.client.cache; - -import com.saaavsaaa.client.utility.constant.Constants; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * Created by aaa - */ -public class PathNode { - private static final Logger logger = LoggerFactory.getLogger(PathNode.class); - private final Map children = new ConcurrentHashMap<>(); - private final String nodeKey; - private byte[] value; - - PathNode(final String key) { - this(key, Constants.RELEASE_VALUE); - } - - PathNode(final String key, final byte[] value) { - this.nodeKey = key; - this.value = value; - } - - public Map getChildren() { - return children; - } - - public String getKey(){ - return this.nodeKey; - } - - public void attachChild(final PathNode node) { - this.children.put(node.nodeKey, node); - } - - - PathNode set(final Iterator iterator, final String value){ - String key = iterator.next(); - logger.debug("PathNode set:{},value:{}", key, value); - PathNode node = children.get(key); - if (node == null){ - logger.debug("set children haven't:{}", key); - node = new PathNode(key); - children.put(key, node); - } - if (iterator.hasNext()){ - node.set(iterator, value); - } else { - node.setValue(value.getBytes(Constants.UTF_8)); - } - return node; - } - - PathNode get(final Iterator iterator){ - String key = iterator.next(); - logger.debug("get:{}", key); - PathNode node = children.get(key); - if (node == null){ - logger.debug("get children haven't:{}", key); - return null; - } - if (iterator.hasNext()){ - return node.get(iterator); - } - return node; - } - - public byte[] getValue() { - return value; - } - - public void setValue(byte[] value) { - this.value = value; - } - - @Deprecated - PathNode get(final int index, final String path) { - if (children.isEmpty()){ - logger.debug("get children haven't:{},index:{}", path, index); - return null; - } - if (children.containsKey(path)){ - return children.get(path); - } - int nextSeparate = path.indexOf(Constants.PATH_SEPARATOR, index); - logger.debug("get nextSeparate:{}", nextSeparate); - if (nextSeparate == -1){ - nextSeparate = path.length() - 1; - } - - return children.get(path.substring(0, nextSeparate)).get(nextSeparate + 1, path); - } -} diff --git a/src/main/java/com/saaavsaaa/client/cache/PathStatus.java b/src/main/java/com/saaavsaaa/client/cache/PathStatus.java deleted file mode 100644 index e899e19e926d4..0000000000000 --- a/src/main/java/com/saaavsaaa/client/cache/PathStatus.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.saaavsaaa.client.cache; - -/** - * Created by aaa - */ -public enum PathStatus { - CHANGING, - RELEASE -} diff --git a/src/main/java/com/saaavsaaa/client/cache/PathTree.java b/src/main/java/com/saaavsaaa/client/cache/PathTree.java deleted file mode 100644 index 8c6391c76399d..0000000000000 --- a/src/main/java/com/saaavsaaa/client/cache/PathTree.java +++ /dev/null @@ -1,332 +0,0 @@ -package com.saaavsaaa.client.cache; - -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.Properties; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.section.Listener; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.WatchedEvent; -import org.apache.zookeeper.common.PathUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; -import java.util.concurrent.locks.ReentrantLock; - -/* - * Created by aaa - * todo provider - */ -public final class PathTree { - private static final Logger logger = LoggerFactory.getLogger(PathTree.class); - private final transient ReentrantLock lock = new ReentrantLock(); - private final AtomicReference rootNode = new AtomicReference<>(); - private boolean executorStart = false; - private ScheduledExecutorService cacheService; - private final IClient client; - private final IProvider provider; - private PathStatus Status; - private boolean closed = false; - - public PathTree(final String root, final IClient client) { - this.rootNode.set(new PathNode(root)); - this.Status = PathStatus.RELEASE; - this.client = client; - this.provider = ((BaseClient)client).getStrategy().getProvider(); - } - - public void load() throws KeeperException, InterruptedException { - final ReentrantLock lock = this.lock; - lock.lockInterruptibly(); - if (closed){ - return; - } - try { - if (Status == Status.RELEASE) { - logger.debug("loading Status:{}", Status); - this.setStatus(PathStatus.CHANGING); - - PathNode newRoot = new PathNode(rootNode.get().getKey()); - List children = provider.getChildren(rootNode.get().getKey()); - children.remove(PathUtil.getRealPath(rootNode.get().getKey(), Constants.CHANGING_KEY)); - this.attechIntoNode(children, newRoot); - rootNode.set(newRoot); - - this.setStatus(PathStatus.RELEASE); -// watch(); - logger.debug("loading release:{}", Status); - } else { - logger.info("loading but cache status not release"); - try { - Thread.sleep(10L); - } catch (InterruptedException e) { - logger.error("loading sleep error:{}", e.getMessage(), e); - } - load(); - } - } finally { - lock.unlock(); - } - } - - private void attechIntoNode(final List children, final PathNode pathNode) throws KeeperException, InterruptedException { - if (closed){ - return; - } - logger.debug("attechIntoNode children:{}", children); - if (children.isEmpty()){ - logger.info("attechIntoNode there are no children"); - return; - } - for (String child : children) { - String childPath = PathUtil.getRealPath(pathNode.getKey(), child); - PathNode current = new PathNode(PathUtil.checkPath(child), provider.getData(childPath)); - pathNode.attachChild(current); - List subs = provider.getChildren(childPath); - this.attechIntoNode(subs, current); - } - } - - public void refreshPeriodic(final long period){ - final ReentrantLock lock = this.lock; - lock.lock(); - if (closed){ - return; - } - try { - if (executorStart) { - throw new IllegalArgumentException("period already set"); - } - long threadPeriod = period; - if (threadPeriod < 1) { - threadPeriod = Properties.INSTANCE.getThreadPeriod(); - } - logger.debug("refreshPeriodic:{}", period); - cacheService = Executors.newSingleThreadScheduledExecutor(); - cacheService.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { - logger.debug("cacheService run:{}", getStatus()); - if (PathStatus.RELEASE == getStatus()) { - try { - load(); - } catch (Exception e) { - logger.error(e.getMessage(), e); - } - } - } - }, Properties.INSTANCE.getThreadInitialDelay(), threadPeriod, TimeUnit.MILLISECONDS); - executorStart = true; - Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { - @Override - public void run() { - stopRefresh(); - } - })); - } finally { - lock.unlock(); - } - } - - public void stopRefresh(){ - cacheService.shutdown(); - executorStart = false; - logger.debug("stopRefresh"); - } - - public void watch(){ - watch(new Listener(rootNode.get().getKey()) { - @Override - public void process(WatchedEvent event) { - String path = event.getPath(); - logger.debug("PathTree Watch event:{}", event.toString()); - switch (event.getType()) { - case NodeCreated: - case NodeDataChanged: - case NodeChildrenChanged: { - try { - String value = Constants.NOTHING_VALUE; - if (!path.equals(getRootNode().getKey())){ - value = provider.getDataString(path); - } - put(path, value); - } catch (Exception e) { - logger.error("PathTree put error : " + e.getMessage()); - } - break; - } - case NodeDeleted: { - delete(path); - break; - } - default: - break; - } - } - }); - } - - public void watch(final Listener listener){ - if (closed){ - return; - } - final String key = listener.getKey(); - logger.debug("PathTree Watch:{}", key); - client.registerWatch(rootNode.get().getKey(), listener); - Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { - @Override - public void run() { - logger.debug("PathTree Unregister Watch:{}", key); - client.unregisterWatch(key); - } - })); - } - - public PathStatus getStatus() { - return Status; - } - - public void setStatus(final PathStatus status) { - Status = status; - } - - public PathNode getRootNode() { - return rootNode.get(); - } - - public byte[] getValue(final String path){ - if (closed){ - return null; - } - PathNode node = get(path); - return null == node ? null : node.getValue(); - } - - private Iterator keyIterator(final String path){ - List nodes = PathUtil.getShortPathNodes(path); - logger.debug("keyIterator path{},nodes:{}", path, nodes); - Iterator iterator = nodes.iterator(); - iterator.next(); // root - return iterator; - } - - public List getChildren(String path) { - if (closed){ - return null; - } - PathNode node = get(path); - List result = new ArrayList<>(); - if (node == null){ - logger.info("getChildren null"); - return result; - } - if (node.getChildren().isEmpty()) { - logger.info("getChildren no child"); - return result; - } - Iterator children = node.getChildren().values().iterator(); - while (children.hasNext()){ - result.add(new String(children.next().getValue())); - } - return result; - } - - private PathNode get(final String path){ - logger.debug("PathTree get:{}", path); - PathUtils.validatePath(path); - if (path.equals(rootNode.get().getKey())){ - return rootNode.get(); - } - Iterator iterator = keyIterator(path); - if (iterator.hasNext()) { - return rootNode.get().get(iterator); //rootNode.get(1, path); - } - logger.debug("{} not exist", path); - return null; - } - - public void put(final String path, final String value) { - final ReentrantLock lock = this.lock; - lock.lock(); - if (closed){ - return; - } - try { - logger.debug("cache put:{},value:{}", path, value); - PathUtils.validatePath(path); - logger.debug("put Status:{}", Status); - if (Status == Status.RELEASE) { - if (path.equals(rootNode.get().getKey())) { - rootNode.set(new PathNode(rootNode.get().getKey(), value.getBytes(Constants.UTF_8))); - return; - } - this.setStatus(PathStatus.CHANGING); - rootNode.get().set(keyIterator(path), value); - this.setStatus(PathStatus.RELEASE); - } else { - try { - logger.debug("put but cache status not release"); - Thread.sleep(10L); - } catch (InterruptedException e) { - logger.error("put sleep error:{}", e.getMessage(), e); - } - put(path, value); - } - } finally { - lock.unlock(); - } - } - - public void delete(String path) { - logger.debug("PathTree begin delete:{}", path); - final ReentrantLock lock = this.lock; - lock.lock(); - if (closed){ - return; - } - try { - PathUtils.validatePath(path); -// String prxpath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); - PathNode node = get(path); - node.getChildren().remove(path); - logger.debug("PathTree end delete:{}", path); - } finally { - lock.unlock(); - } - } - - public void close(){ - final ReentrantLock lock = this.lock; - lock.lock(); - this.closed = true; - try { - if (executorStart){ - stopRefresh(); - } - deleteAllChildren(rootNode.get()); - } catch (Exception ee){ - logger.warn("PathTree close:{}", ee.getMessage()); - } finally { - lock.unlock(); - } - } - - private void deleteAllChildren(PathNode node){ - if (node.getChildren().isEmpty()){ - return; - } - for (String one : node.getChildren().keySet()) { - deleteAllChildren(node.getChildren().get(one)); - node.getChildren().remove(one); - } - } -} diff --git a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java b/src/main/java/com/saaavsaaa/client/election/LeaderElection.java deleted file mode 100644 index d520a34b5106d..0000000000000 --- a/src/main/java/com/saaavsaaa/client/election/LeaderElection.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.saaavsaaa.client.election; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.zookeeper.section.Listener; -import com.saaavsaaa.client.utility.Properties; -import com.saaavsaaa.client.zookeeper.section.WatcherCreator; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.WatchedEvent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by aaa - * It is not recommended to be used as a global variable - */ -public abstract class LeaderElection { - private static final Logger logger = LoggerFactory.getLogger(LeaderElection.class); - private boolean done = false; - private int retryCount; - - public LeaderElection(){ - retryCount = Properties.INSTANCE.getNodeElectionCount(); - } - - private boolean contend(final String node, final IProvider provider, final Listener listener) throws KeeperException, InterruptedException { - boolean success = false; - try { - provider.create(node, Properties.INSTANCE.getClientId(), CreateMode.EPHEMERAL); // todo EPHEMERAL_SEQUENTIAL check index value - success = true; - } catch (KeeperException.NodeExistsException e) { - logger.info("contend not success"); - // TODO: or changing_key node value == current client id - provider.exists(node, WatcherCreator.deleteWatcher(listener)); - } - return success; - } - - /* - * listener will be register when the contention of the path is unsuccessful - */ - public void executeContention(final String nodeBeCompete, final IProvider provider) throws KeeperException, InterruptedException { - boolean canBegin; - final String realNode = provider.getRealPath(nodeBeCompete); - final String contendNode = PathUtil.getRealPath(realNode, Constants.CHANGING_KEY); - canBegin = this.contend(contendNode, provider, new Listener(contendNode) { - @Override - public void process(WatchedEvent event) { - try { - retryCount--; - if (retryCount < 0){ - logger.info("Election node exceed retry count"); - return; - } - executeContention(realNode, provider); - } catch (Exception ee){ - logger.error("Listener Exception executeContention:{}", ee.getMessage(), ee); - } - } - }); - - if (canBegin){ - try { - action(); - done = true; - callback(); - } catch (Exception ee){ - logger.error("action Exception executeContention:{}", ee.getMessage(), ee); - } - provider.delete(contendNode); - } - } - - public void waitDone(){ - while (!done){ - try { - Thread.sleep(10L); - } catch (InterruptedException e) { - logger.error("waitDone:{}", e.getMessage(), e); - } - } - } - -// public abstract void actionWhenUnreached() throws KeeperException, InterruptedException; - public abstract void action() throws KeeperException, InterruptedException; - - public void callback(){} -} diff --git a/src/main/java/com/saaavsaaa/client/retry/AsyncRetryCenter.java b/src/main/java/com/saaavsaaa/client/retry/AsyncRetryCenter.java deleted file mode 100644 index 8b4416e5208f6..0000000000000 --- a/src/main/java/com/saaavsaaa/client/retry/AsyncRetryCenter.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.saaavsaaa.client.retry; - -import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.concurrent.DelayQueue; - -/** - * Created by aaa - */ -public enum AsyncRetryCenter { - INSTANCE; - - private static final Logger logger = LoggerFactory.getLogger(AsyncRetryCenter.class); - private final DelayQueue queue = new DelayQueue<>(); - private final RetryThread retryThread = new RetryThread(queue); - - private boolean started = false; - private DelayRetryPolicy delayRetryPolicy; - - public void init(DelayRetryPolicy delayRetryPolicy) { - logger.debug("delayRetryPolicy init"); - if (delayRetryPolicy == null){ - logger.warn("delayRetryPolicy is null and auto init with DelayRetryPolicy.newNoInitDelayPolicy"); - delayRetryPolicy = DelayRetryPolicy.newNoInitDelayPolicy(); - } - this.delayRetryPolicy = delayRetryPolicy; - } - - public synchronized void start(){ - if (started){ - return; - } - retryThread.setName("retry-thread"); - retryThread.start(); - this.started = true; - } - - public void add(BaseOperation operation){ - if (delayRetryPolicy == null){ - logger.warn("delayRetryPolicy no init and auto init with DelayRetryPolicy.newNoInitDelayPolicy"); - delayRetryPolicy = DelayRetryPolicy.newNoInitDelayPolicy(); - } - operation.setRetrial(new DelayPolicyExecutor(delayRetryPolicy)); - queue.offer(operation); - logger.debug("enqueue operation:{}", operation.toString()); - } -} diff --git a/src/main/java/com/saaavsaaa/client/retry/DelayPolicyExecutor.java b/src/main/java/com/saaavsaaa/client/retry/DelayPolicyExecutor.java deleted file mode 100644 index 6195826122f17..0000000000000 --- a/src/main/java/com/saaavsaaa/client/retry/DelayPolicyExecutor.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.saaavsaaa.client.retry; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Random; - -/** - * Created by aaa - */ -public class DelayPolicyExecutor { - private static final Logger logger = LoggerFactory.getLogger(DelayPolicyExecutor.class); - private final DelayRetryPolicy delayRetryPolicy; - private final Random random; - - private int executeCount = 0; - private long executeTick; - - public DelayPolicyExecutor(){ - this(DelayRetryPolicy.newNoInitDelayPolicy()); - } - - public DelayPolicyExecutor(final DelayRetryPolicy delayRetryPolicy) { - this.delayRetryPolicy = delayRetryPolicy; - this.executeTick = System.currentTimeMillis(); - this.random = new Random(); -// next(); - } - - public boolean hasNext() { - return executeCount < delayRetryPolicy.getRetryCount(); - } - - public long getNextTick() { - return executeTick; - } - - public void next() { - executeCount ++; - long sleep = delayRetryPolicy.getBaseDelay() * Math.max(1, this.random.nextInt(1 << delayRetryPolicy.getRetryCount() + 1)); - if (sleep < delayRetryPolicy.getDelayUpperBound()){ - executeTick += sleep; - } else { - executeTick += delayRetryPolicy.getDelayUpperBound(); - } - logger.debug("next executeCount:{}, executeTick:{}", executeCount, executeTick); - } -} diff --git a/src/main/java/com/saaavsaaa/client/retry/DelayRetryPolicy.java b/src/main/java/com/saaavsaaa/client/retry/DelayRetryPolicy.java deleted file mode 100644 index d754d6cdc9151..0000000000000 --- a/src/main/java/com/saaavsaaa/client/retry/DelayRetryPolicy.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.saaavsaaa.client.retry; - -/** - * Created by aaa - */ -public class DelayRetryPolicy { - private static final long BASE_DELAY = 10; - private static final int BASE_COUNT = 3; - private static final int RETRY_COUNT_BOUND = 29; - - private final int retryCount; - private final long baseDelay; - private final long delayUpperBound; - - /* - * Millis - */ - public DelayRetryPolicy(long baseDelay) { - this(RETRY_COUNT_BOUND, baseDelay, Integer.MAX_VALUE); - } - - public DelayRetryPolicy(int retryCount, long baseDelay, long delayUpperBound) { - this.retryCount = retryCount; - this.baseDelay = baseDelay; - this.delayUpperBound = delayUpperBound; - } - - public int getRetryCount() { - return retryCount; - } - - public long getBaseDelay() { - return baseDelay; - } - - public long getDelayUpperBound() { - return delayUpperBound; - } - - public static DelayRetryPolicy newNoInitDelayPolicy(){ - return new DelayRetryPolicy(BASE_COUNT, BASE_DELAY, Integer.MAX_VALUE); - } -} diff --git a/src/main/java/com/saaavsaaa/client/retry/RetryCount.java b/src/main/java/com/saaavsaaa/client/retry/RetryCount.java deleted file mode 100644 index ccf64905c7746..0000000000000 --- a/src/main/java/com/saaavsaaa/client/retry/RetryCount.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.saaavsaaa.client.retry; - -/** - * Created by aaa - */ -@Deprecated -public enum RetryCount { - INSTANCE; - - private static final int RETRY_COUNT_BOUND = 29; - private static final ThreadLocal count = new ThreadLocal<>(); - private static final ThreadLocal standCount = new ThreadLocal<>(); - - public void init(final int count){ - standCount.set(count); - } - - public void start(){ - count.set(getStandCount()); - } - - public boolean continueExecute() { - if (count.get() == null){ - start(); - } - int current = count.get().intValue() - 1; - if (current < 0){ - count.remove(); - } else { - count.set(current); - } - return current > -1; - } - - public void reset(){ - start(); - } - - public int getStandCount(){ - Integer sc = standCount.get(); - if (sc == null){ - standCount.set(3); - return 3; - } - if (sc > RETRY_COUNT_BOUND){ - standCount.set(RETRY_COUNT_BOUND); - } - return standCount.get(); - } -} diff --git a/src/main/java/com/saaavsaaa/client/retry/RetryThread.java b/src/main/java/com/saaavsaaa/client/retry/RetryThread.java deleted file mode 100644 index 57fa8b73617f3..0000000000000 --- a/src/main/java/com/saaavsaaa/client/retry/RetryThread.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.saaavsaaa.client.retry; - -import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.concurrent.*; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * Created by aaa - */ -public class RetryThread extends Thread { - private static final Logger logger = LoggerFactory.getLogger(RetryThread.class); - private final ThreadPoolExecutor retryExecutor; - private final int corePoolSize = Runtime.getRuntime().availableProcessors(); - private final int maximumPoolSize = corePoolSize; - private final long keepAliveTime = 0; - private final int closeDelay = 60; - private final DelayQueue queue; - - public RetryThread(DelayQueue queue) { - this.queue = queue; - retryExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(10), new ThreadFactory() { - private final AtomicInteger threadIndex = new AtomicInteger(0); - @Override - public Thread newThread(Runnable r) { - Thread thread = new Thread(r); - thread.setDaemon(true); - thread.setName("zk-retry-" + threadIndex.incrementAndGet()); - logger.debug("new thread:{}", thread.getName()); - return thread; - } - }); - addDelayedShutdownHook(retryExecutor, closeDelay, TimeUnit.SECONDS); - } - - @Override - public void run(){ - logger.debug("RetryThread start"); - for (;;) { - final BaseOperation operation; - try { - operation = queue.take(); - logger.debug("take operation:{}", operation.toString()); - } catch (InterruptedException e) { - logger.error("retry interrupt e:{}", e.getMessage()); - continue; - } - retryExecutor.submit(new Runnable() { - @Override - public void run() { - boolean result; - try { - result = operation.executeOperation(); - } catch (Exception e) { - result = false; - logger.error("retry disrupt operation:{}, e:{}", operation.toString(), e.getMessage()); - } - if (result){ - queue.offer(operation); - logger.debug("enqueue again operation:{}", operation.toString()); - } - } - }); - } - } - - final void addDelayedShutdownHook(final ExecutorService service, final long terminationTimeout, final TimeUnit timeUnit) { - Thread thread = new Thread(new Runnable() { - @Override - public void run() { - try { - logger.debug("AsyncRetryCenter stop"); - queue.clear(); - service.shutdown(); - service.awaitTermination(terminationTimeout, timeUnit); - } catch (InterruptedException ignored) { - // We're shutting down anyway, so just ignore. - } - } - }); - thread.setName("retry shutdown hook"); - Runtime.getRuntime().addShutdownHook(thread); - } -} diff --git a/src/main/java/com/saaavsaaa/client/utility/PathUtil.java b/src/main/java/com/saaavsaaa/client/utility/PathUtil.java deleted file mode 100644 index fb0a36f04f379..0000000000000 --- a/src/main/java/com/saaavsaaa/client/utility/PathUtil.java +++ /dev/null @@ -1,166 +0,0 @@ -package com.saaavsaaa.client.utility; - -import com.saaavsaaa.client.utility.constant.Constants; - -import javax.swing.tree.TreeNode; -import java.util.*; - -/** - * Created by aaa - */ -public class PathUtil { - - public static String getRealPath(final String root, final String path){ - return adjustPath(root, path); - } - - private static String adjustPath(String root, String path){ - if (StringUtil.isNullOrBlank(path)){ - throw new IllegalArgumentException("path should have content!"); - } - if (!root.startsWith(Constants.PATH_SEPARATOR)){ - root = Constants.PATH_SEPARATOR + root; - } - if (!path.startsWith(Constants.PATH_SEPARATOR)){ - path = Constants.PATH_SEPARATOR + path; - } - if (!path.startsWith(root)){ - return root + path; - } - return path; - } - - //child to root - public static Stack getPathReverseNodes(final String root, String path){ - path = adjustPath(root, path); - Stack pathStack = new Stack<>(); - int index = 1; - int position = path.indexOf(Constants.PATH_SEPARATOR, index); - do{ - pathStack.push(path.substring(0, position)); - index = position + 1; - position = path.indexOf(Constants.PATH_SEPARATOR, index); - } - while (position > -1); - pathStack.push(path); - return pathStack; - } - - public static List getPathOrderNodes(final String root, String path){ - path = adjustPath(root, path); - List paths = new ArrayList<>(); - int index = 1; - int position = path.indexOf(Constants.PATH_SEPARATOR, index); - - do{ - paths.add(path.substring(0, position)); - index = position + 1; - position = path.indexOf(Constants.PATH_SEPARATOR, index); - } - while (position > -1); - paths.add(path); - return paths; - } - - public static List getShortPathNodes(String path){ - path = checkPath(path); - List paths = new ArrayList<>(); - char[] chars = path.toCharArray(); - StringBuilder builder = new StringBuilder(Constants.PATH_SEPARATOR); - for (int i = 1; i < chars.length; i++) { - if (chars[i] == Constants.PATH_SEPARATOR.charAt(0)){ - paths.add(builder.toString()); - builder = new StringBuilder(Constants.PATH_SEPARATOR); - continue; - } - builder.append(chars[i]); - if (i == chars.length - 1){ - paths.add(builder.toString()); - } - } - return paths; - } - - public static List breadthToB(TreeNode root) { - List lists = new ArrayList<>(); - if(root==null) - return lists; - Queue queue=new LinkedList<>(); - queue.offer(root); - while(!queue.isEmpty()){ - /*TreeNode tree=queue.poll(); - if(tree.left!=null) - queue.offer(tree.left); - if(tree.right!=null) - queue.offer(tree.right); - lists.add(tree.val);*/ - } - return lists; - } - - public static List depthToB(TreeNode root) { - List lists = new ArrayList<>(); - if(root==null) - return lists; - Stack stack=new Stack(); - stack.push(root); - while(!stack.isEmpty()){ - TreeNode tree=stack.pop(); - //先往栈中压入右节点,再压左节点,这样出栈就是先左节点后右节点了。 - /*if(tree.right!=null) - stack.push(tree.right); - if(tree.left!=null) - stack.push(tree.left); - lists.add(tree.val);*/ - } - return lists; - } - - //isSequential - /* - * ignore invalid char and // /./ /../ - */ - public static String checkPath(String path) throws IllegalArgumentException { - if(path == null || path.length() == 0) { - throw new IllegalArgumentException("path should not be null"); - } - if(path.charAt(0) != 47 || path.charAt(path.length() - 1) == 47){ - path = Constants.PATH_SEPARATOR + path; - } - - if(path.charAt(path.length() - 1) == 47){ - path = Constants.PATH_SEPARATOR + path; - } - - char previous = 47; - char[] chars = path.toCharArray(); - StringBuilder builder = new StringBuilder(); - builder.append(previous); - - for(int i = 1; i < chars.length; ++i) { - char c = chars[i]; - if (c == 0 || (c == 47 && previous == 47)) { - continue; - } - if (c == 46) { - // ignore /./ /../ - boolean preWarn = previous == 47 || (previous == 46 && chars[i - 2] == 47); - if (previous == 47 && (i + 1 == chars.length || chars[i + 1] == 47)) { - i++; - continue; - } - if ((previous == 46 && chars[i - 2] == 47) && (i + 1 == chars.length || chars[i + 1] == 47)) { - i+=2; - continue; - } - } - if (c > 0 && c < 31 || c > 127 && c < 159 || c > '\ud800' && c < '\uf8ff' || c > '\ufff0' && c < '\uffff') { - continue; - } - - builder.append(c); - previous = c; - } - return builder.toString(); - } -} diff --git a/src/main/java/com/saaavsaaa/client/utility/Properties.java b/src/main/java/com/saaavsaaa/client/utility/Properties.java deleted file mode 100644 index c3b5fbae57e16..0000000000000 --- a/src/main/java/com/saaavsaaa/client/utility/Properties.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.saaavsaaa.client.utility; - -import java.util.ResourceBundle; - -/** - * Created by aaa - */ -public enum Properties { - INSTANCE; - - private final ResourceBundle bundle; - - private Properties(){ - bundle = ResourceBundle.getBundle("client"); - } - - public String getClientId(){ - // ResourceBundle caches the value in Thread - String clientId = bundle.getString("client.id"); - if (StringUtil.isNullOrBlank(clientId)){ - throw new IllegalArgumentException("client.id doesn't exist"); - } - return clientId; - } - - public boolean watchOn(){ - String result = bundle.getString("client.watch.on"); - if (StringUtil.isNullOrBlank(result)){ - throw new IllegalArgumentException("client.watch.on doesn't exist"); - } - return "up".equals(result); - } - - public long getThreadInitialDelay(){ - String result = bundle.getString("client.thread.delay"); - if (StringUtil.isNullOrBlank(result)){ - throw new IllegalArgumentException("client.thread.delay doesn't exist"); - } - return Long.valueOf(result); - } - - public long getThreadPeriod(){ - String result = bundle.getString("client.thread.period"); - if (StringUtil.isNullOrBlank(result)){ - throw new IllegalArgumentException("client.thread.period doesn't exist"); - } - return Long.valueOf(result); - } - - public int getNodeElectionCount(){ - String result = bundle.getString("node.election.count"); - if (StringUtil.isNullOrBlank(result)){ - throw new IllegalArgumentException("node.election.count doesn't exist"); - } - return Integer.valueOf(result); - } -} diff --git a/src/main/java/com/saaavsaaa/client/utility/StringUtil.java b/src/main/java/com/saaavsaaa/client/utility/StringUtil.java deleted file mode 100644 index c26a5fdfab88d..0000000000000 --- a/src/main/java/com/saaavsaaa/client/utility/StringUtil.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.saaavsaaa.client.utility; - -/** - * Created by aaa - */ -public class StringUtil { - public static boolean isNullOrBlank(String string) { - return string == null || string.trim().length() == 0; - } -} diff --git a/src/main/java/com/saaavsaaa/client/utility/constant/Constants.java b/src/main/java/com/saaavsaaa/client/utility/constant/Constants.java deleted file mode 100644 index 9a50d60835641..0000000000000 --- a/src/main/java/com/saaavsaaa/client/utility/constant/Constants.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.saaavsaaa.client.utility.constant; - -import java.nio.charset.Charset; - -/** - * Created by aaa - */ -public final class Constants { - public static final int VERSION = -1; - public static final int WAIT = 60 * 1000; - public static final byte[] NOTHING_DATA = new byte[0]; - public static final String NOTHING_VALUE = ""; - public static final Charset UTF_8 = Charset.forName("UTF-8"); - public static final String PATH_SEPARATOR = "/"; - public static final String GLOBAL_LISTENER_KEY = "globalListener"; - - public static final byte[] CHANGING_VALUE = new byte[]{'c'}; - public static final byte[] RELEASE_VALUE = new byte[]{'r'}; - public static final String CHANGING_KEY = "CHANGING_KEY"; -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java deleted file mode 100644 index f44c07b2859e8..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/CacheClient.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -import com.saaavsaaa.client.cache.CacheStrategy; -import com.saaavsaaa.client.cache.PathTree; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.zookeeper.base.BaseContext; -import org.apache.zookeeper.AsyncCallback; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.util.List; - -/** - * Created by aaa - * partially prepared products - */ -public final class CacheClient extends UsualClient { - private static final Logger logger = LoggerFactory.getLogger(CacheClient.class); - protected PathTree pathTree = null; - - CacheClient(final BaseContext context) { - super(context); - } - - @Override - public void start() throws IOException, InterruptedException { - super.start(); - try { - useCacheStrategy(CacheStrategy.WATCH); - } catch (KeeperException e) { - logger.error("CacheClient useCacheStrategy : " + e.getMessage()); - } - } - - @Override - public void close(){ - super.close(); - this.strategies.clear(); - this.pathTree.close(); - } - - //todo put it here? - void useCacheStrategy(CacheStrategy cacheStrategy) throws KeeperException, InterruptedException { - logger.debug("use cache strategy:{}", cacheStrategy); - switch (cacheStrategy){ - case WATCH:{ - pathTree = new PathTree(rootNode, this); - pathTree.watch(); - return; - } - case ALL:{ - pathTree = loadPathTree(); - return; - } - case NONE: - default:{ - return; - } - } - } - - public PathTree loadPathTree() throws KeeperException, InterruptedException { - return loadPathTree(rootNode); - } - - public PathTree loadPathTree(final String treeRoot) throws KeeperException, InterruptedException { - PathTree tree = new PathTree(treeRoot, this); - logger.debug("load path tree:{}", treeRoot); - tree.load(); - return tree; - } - - @Override - public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - super.createCurrentOnly(key, value, createMode); - pathTree.put(PathUtil.getRealPath(rootNode, key), value); - } - - - @Override - public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - super.deleteOnlyCurrent(key); - pathTree.delete(PathUtil.getRealPath(rootNode, key)); - } - - @Override - public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - super.deleteOnlyCurrent(key, callback, ctx); - pathTree.delete(PathUtil.getRealPath(rootNode, key)); - } - - @Override - public byte[] getData(final String key) throws KeeperException, InterruptedException { - String path = PathUtil.getRealPath(rootNode, key); - byte[] data = pathTree.getValue(path); - if (data != null){ - logger.debug("getData cache hit:{}", data); - return data; - } - logger.debug("getData cache not hit:{}", data); - return strategy.getData(key); - } - - @Override - public List getChildren(final String key) throws KeeperException, InterruptedException { - String path = PathUtil.getRealPath(rootNode, key); - List keys = pathTree.getChildren(path); - if (!keys.isEmpty()){ - logger.debug("getChildren cache hit:{}", keys); - return keys; - } - logger.debug("getChildren cache not hit:{}", keys); - return strategy.getChildren(PathUtil.getRealPath(rootNode, key)); - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java deleted file mode 100644 index a3739c3c0796e..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/ClientFactory.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.retry.DelayRetryPolicy; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; -import com.saaavsaaa.client.zookeeper.section.ClientContext; -import com.saaavsaaa.client.zookeeper.section.Listener; -import org.apache.zookeeper.data.ACL; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.util.List; - -/** - * Created by aaa - */ -public class ClientFactory extends BaseClientFactory { - // private static final String CLIENT_EXCLUSIVE_NODE = "ZKC"; - private static final Logger logger = LoggerFactory.getLogger(ClientFactory.class); - private DelayRetryPolicy delayRetryPolicy; - - public ClientFactory(){} - - public ClientFactory newClient(final String servers, final int sessionTimeoutMilliseconds) { - int wait = sessionTimeoutMilliseconds; - if (sessionTimeoutMilliseconds == 0){ - wait = Constants.WAIT; - } - this.context = new ClientContext(servers, wait); - client = new UsualClient(context); - logger.debug("new usual client"); - return this; - } - - /* - * used for create new clients through a existing client - * this client is not perhaps the client - */ - public synchronized BaseClientFactory newClientByOriginal(boolean closeOriginal) { - IClient oldClient = this.client; - client = new UsualClient(context); - if (closeOriginal){ - oldClient.close(); - } - logger.debug("new usual client by a existing client"); - return this; - } - - /* - * partially prepared products - */ - public ClientFactory newCacheClient(final String servers, final int sessionTimeoutMilliseconds) { - this.context = new ClientContext(servers, sessionTimeoutMilliseconds); - client = new CacheClient(context); - logger.debug("new cache client"); - return this; - } - - public ClientFactory watch(final Listener listener){ - globalListener = listener; - return this; - } - - public ClientFactory setNamespace(String namespace) { - if (!namespace.startsWith(Constants.PATH_SEPARATOR)){ - namespace = Constants.PATH_SEPARATOR + namespace; - } - this.namespace = namespace; - return this; - } - - public ClientFactory authorization(final String scheme, final byte[] auth, final List authorities){ - this.scheme = scheme; - this.auth = auth; - this.authorities = authorities; - return this; - } - - public ClientFactory setRetryPolicy(final DelayRetryPolicy delayRetryPolicy){ - this.delayRetryPolicy = delayRetryPolicy; - return this; - } - - @Override - public IClient start() throws IOException, InterruptedException { - ((ClientContext)context).setDelayRetryPolicy(delayRetryPolicy); - ((ClientContext)context).setClientFactory(this); - return super.start(); - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java deleted file mode 100644 index 7ec76b96a9d29..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/UsualClient.java +++ /dev/null @@ -1,123 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.base.BaseContext; -import com.saaavsaaa.client.zookeeper.section.StrategyType; -import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; -import org.apache.zookeeper.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.util.List; - -/** - * Created by aaa - */ -public class UsualClient extends BaseClient { - private static final Logger logger = LoggerFactory.getLogger(UsualClient.class); - - UsualClient(final BaseContext context) { - super(context); - } - - @Override - public void start() throws IOException, InterruptedException { - super.start(); - useExecStrategy(StrategyType.USUAL); - } - - @Override - public String getDataString(final String key) throws KeeperException, InterruptedException { - return strategy.getDataString(key); - } - - @Override - public byte[] getData(final String key) throws KeeperException, InterruptedException { - return strategy.getData(key); - } - - @Override - public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { - strategy.getData(key, callback, ctx); - } - - @Override - public boolean checkExists(final String key) throws KeeperException, InterruptedException { - return strategy.checkExists(key); - } - - @Override - public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { - return strategy.checkExists(key, watcher); - } - - @Override - public List getChildren(final String key) throws KeeperException, InterruptedException { - return strategy.getChildren(key); - } - - @Override - public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - this.createNamespace(); - if (rootNode.equals(key)){ - return; - } - strategy.createCurrentOnly(key, value, createMode); - } - - @Override - public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - this.createNamespace(); - if (rootNode.equals(key)){ - return; - } - strategy.createAllNeedPath(key, value, createMode); - } - - @Override - public void update(final String key, final String value) throws KeeperException, InterruptedException { - strategy.update(key, value); - } - - @Override - public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - if (rootNode.equals(key)){ - deleteNamespace(); - return; - } - strategy.deleteOnlyCurrent(key); - } - - @Override - public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - if (rootNode.equals(key)){ - deleteNamespace(); - return; - } - strategy.deleteOnlyCurrent(key, callback, ctx); - } - - @Override - public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { - strategy.deleteAllChildren(key); - if (rootNode.equals(key)){ - rootExist = false; - logger.debug("deleteAllChildren delete root:{}", rootNode); - } - } - - @Override - public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - strategy.deleteCurrentBranch(key); - if (!strategy.checkExists(rootNode)){ - rootExist = false; - logger.debug("deleteCurrentBranch delete root:{}", rootNode); - } - } - - @Override - public ZKTransaction transaction() { - return new ZKTransaction(rootNode, holder); - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java deleted file mode 100644 index 913d022f691e3..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClient.java +++ /dev/null @@ -1,196 +0,0 @@ -package com.saaavsaaa.client.zookeeper.base; - -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.action.IExecStrategy; -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.StringUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.section.ClientContext; -import com.saaavsaaa.client.zookeeper.section.Listener; -import com.saaavsaaa.client.zookeeper.section.StrategyType; -import com.saaavsaaa.client.zookeeper.section.WatcherCreator; -import com.saaavsaaa.client.zookeeper.strategy.*; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.WatchedEvent; -import org.apache.zookeeper.ZooDefs; -import org.apache.zookeeper.data.ACL; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; - -/** - * Created by aaa - */ -public abstract class BaseClient implements IClient { - private static final Logger logger = LoggerFactory.getLogger(BaseClient.class); - - protected final boolean watched = true; //false - protected final Map strategies = new ConcurrentHashMap<>(); - - protected IExecStrategy strategy; - protected BaseContext context; - protected List authorities; - protected Holder holder; - - protected String rootNode = "/InitValue"; - protected boolean rootExist = false; - - protected BaseClient(final BaseContext context) { - this.context = context; - } - - @Override - public void start() throws IOException, InterruptedException { - holder = new Holder(getContext()); - holder.start(); - } - - @Override - public synchronized boolean blockUntilConnected(int wait, TimeUnit units) throws InterruptedException { - long maxWait = units != null ? TimeUnit.MILLISECONDS.convert(wait, units) : 0; - - while (!holder.isConnected()){ - long waitTime = maxWait - 30; - if (waitTime <= 0){ - return holder.isConnected(); - } - wait(30); - } - return true; - } - - @Override - public void close() { - holder.close(); - context.close(); - this.strategies.clear(); - } - - @Override - public synchronized void useExecStrategy(StrategyType strategyType) { - logger.debug("useExecStrategy:{}", strategyType); - if (strategies.containsKey(strategyType)){ - strategy = strategies.get(strategyType); - return; - } - - IProvider provider = new BaseProvider(rootNode, holder, watched, authorities); - switch (strategyType){ - case USUAL:{ - strategy = new UsualStrategy(provider); - break; - } - case CONTEND:{ - strategy = new ContentionStrategy(provider); - break; - } - case SYNC_RETRY:{ - strategy = new SyncRetryStrategy(provider, ((ClientContext)context).getDelayRetryPolicy()); - break; - } - case ASYNC_RETRY:{ - strategy = new AsyncRetryStrategy(provider, ((ClientContext)context).getDelayRetryPolicy()); - break; - } - case ALL_ASYNC_RETRY:{ - strategy = new AllAsyncRetryStrategy(provider, ((ClientContext)context).getDelayRetryPolicy()); - break; - } - default:{ - strategy = new UsualStrategy(provider); - break; - } - } - - strategies.put(strategyType, strategy); - } - - void registerWatch(final Listener globalListener){ - if (context.globalListener != null){ - logger.warn("global listener can only register one"); - return; - } - context.globalListener = globalListener; - logger.debug("globalListenerRegistered:{}", globalListener.getKey()); - } - - @Override - public void registerWatch(final String key, final Listener listener){ - String path = PathUtil.getRealPath(rootNode, key); - listener.setPath(path); - context.getWatchers().put(listener.getKey(), listener); - logger.debug("register watcher:{}", path); - } - - @Override - public void unregisterWatch(final String key){ - if (StringUtil.isNullOrBlank(key)){ - throw new IllegalArgumentException("key should not be blank"); - } -// String path = PathUtil.getRealPath(rootNode, key); - if (context.getWatchers().containsKey(key)){ - context.getWatchers().remove(key); - logger.debug("unregisterWatch:{}", key); - } - } - - protected void createNamespace() throws KeeperException, InterruptedException { - createNamespace(Constants.NOTHING_DATA); - } - - private void createNamespace(final byte[] date) throws KeeperException, InterruptedException { - if (rootExist){ - logger.debug("root exist"); - return; - } - try { - if (null == holder.getZooKeeper().exists(rootNode, false)){ - holder.zooKeeper.create(rootNode, date, authorities, CreateMode.PERSISTENT); - } - rootExist = true; - logger.debug("creating root:{}", rootNode); - } catch (KeeperException.NodeExistsException ee){ - logger.warn("root create:{}", ee.getMessage()); - rootExist = true; - return; - } - holder.zooKeeper.exists(rootNode, WatcherCreator.deleteWatcher(new Listener(rootNode) { - @Override - public void process(WatchedEvent event) { - rootExist = false; - } - })); - logger.debug("created root:{}", rootNode); - } - - protected void deleteNamespace() throws KeeperException, InterruptedException { - holder.zooKeeper.delete(rootNode, Constants.VERSION); - rootExist = false; - logger.debug("delete root:{},rootExist:{}", rootNode, rootExist); - } - - void setRootNode(final String rootNode) { - this.rootNode = rootNode; - } - - void setAuthorities(final String scheme, final byte[] auth, final List authorities) { - context.scheme = scheme; - context.auth = auth; - this.authorities = authorities; - } - - public BaseContext getContext(){ - return context; - } - - public IExecStrategy getStrategy() { - return strategy; - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java deleted file mode 100644 index bc0060d3720b5..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseClientFactory.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.saaavsaaa.client.zookeeper.base; - -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.zookeeper.section.Listener; -import org.apache.zookeeper.data.ACL; - -import java.io.IOException; -import java.util.List; - -import static org.apache.zookeeper.ZooDefs.Ids.OPEN_ACL_UNSAFE; - -/** - * Created by aaa - */ -public abstract class BaseClientFactory { - protected BaseClient client; - protected Listener globalListener; - protected String namespace; - protected String scheme; - protected byte[] auth; - protected List authorities; - protected BaseContext context; - - public IClient start() throws IOException, InterruptedException { - client.setRootNode(namespace); - if(scheme == null) { - authorities = OPEN_ACL_UNSAFE; - } - client.setAuthorities(scheme , auth, authorities); - client.start(); - if (globalListener != null) { - client.registerWatch(globalListener); - } - return client; - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseContext.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseContext.java deleted file mode 100644 index 9b4e2bc576f7a..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseContext.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.saaavsaaa.client.zookeeper.base; - -import com.saaavsaaa.client.zookeeper.section.Listener; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * Created by aaa - */ -public abstract class BaseContext { - protected String servers; - protected int sessionTimeOut; - protected String scheme; - protected byte[] auth; - protected Listener globalListener; - protected final Map watchers = new ConcurrentHashMap<>(); - - public String getServers() { - return servers; - } - - public int getSessionTimeOut() { - return sessionTimeOut; - } - - public String getScheme() { - return scheme; - } - - public byte[] getAuth() { - return auth; - } - - public Map getWatchers(){ - return watchers; - } - - public void close() { - this.watchers.clear(); - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java deleted file mode 100644 index 71818b754bfc5..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseOperation.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.saaavsaaa.client.zookeeper.base; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.retry.DelayPolicyExecutor; -import com.saaavsaaa.client.zookeeper.section.Connection; -import org.apache.zookeeper.KeeperException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.concurrent.Delayed; -import java.util.concurrent.TimeUnit; - -/** - * Created by aaa - */ -public abstract class BaseOperation implements Delayed { - private static final Logger logger = LoggerFactory.getLogger(BaseOperation.class); - protected final IProvider provider; - protected DelayPolicyExecutor delayPolicyExecutor; - - protected BaseOperation(final IProvider provider) { - this.provider = provider; - } - - public void setRetrial(final DelayPolicyExecutor delayPolicyExecutor){ - this.delayPolicyExecutor = delayPolicyExecutor; - } - - @Override - public long getDelay(TimeUnit unit) { - long absoluteBlock = this.delayPolicyExecutor.getNextTick() - System.currentTimeMillis(); - logger.debug("queue getDelay block:{}", absoluteBlock); - long result = unit.convert(absoluteBlock, TimeUnit.MILLISECONDS); - return result; - } - - /** - * queue precedence - */ - @Override - public int compareTo(Delayed delayed) { - return (int) (this.getDelay(TimeUnit.MILLISECONDS) - delayed.getDelay(TimeUnit.MILLISECONDS)); - } - - protected abstract void execute() throws KeeperException, InterruptedException; - - /* - * @Return whether or not continue enqueue - */ - public boolean executeOperation() throws KeeperException, InterruptedException { - boolean result; - try { - execute(); - result = true; - } catch (KeeperException ee) { - if (Connection.needReset(ee)){ - provider.resetConnection(); - result = false; - } else { - throw ee; - } - } - if (!result && delayPolicyExecutor.hasNext()){ - delayPolicyExecutor.next(); - return true; - } - return false; - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java deleted file mode 100644 index 7beaa54f88c9b..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseProvider.java +++ /dev/null @@ -1,156 +0,0 @@ -package com.saaavsaaa.client.zookeeper.base; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.election.LeaderElection; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; -import org.apache.zookeeper.AsyncCallback; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.Watcher; -import org.apache.zookeeper.data.ACL; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; -import java.util.Stack; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * Created by aaa - */ -public class BaseProvider implements IProvider { - private static final Logger logger = LoggerFactory.getLogger(BaseProvider.class); - protected final Holder holder; - protected final boolean watched; - protected final List authorities; - protected final String rootNode; - - public BaseProvider(final String rootNode, final Holder holder, final boolean watched, final List authorities){ - this.rootNode = rootNode; - this.holder = holder; - this.watched = watched; - this.authorities = authorities; - } - - public String getDataString(final String key) throws KeeperException, InterruptedException { - return new String(getData(key)); - } - - @Override - public byte[] getData(final String key) throws KeeperException, InterruptedException { - return holder.getZooKeeper().getData(key, watched, null); - } - - @Override - public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { - holder.getZooKeeper().getData(key, watched, callback, ctx); - } - - @Override - public boolean exists(final String key) throws KeeperException, InterruptedException { - return null != holder.getZooKeeper().exists(key, watched); - } - - @Override - public boolean exists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { - return null != holder.getZooKeeper().exists(key, watcher); - } - - @Override - public List getChildren(final String key) throws KeeperException, InterruptedException { - return holder.getZooKeeper().getChildren(key, watched); - } - - @Override - public void create(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - holder.getZooKeeper().create(key, value.getBytes(Constants.UTF_8), authorities, createMode); - logger.debug("BaseProvider createCurrentOnly:{}", key); -// create(key, value, createMode, new AtomicInteger()); - } - - @Deprecated - private void create(final String key, final String value, final CreateMode createMode, final AtomicInteger count) throws KeeperException, InterruptedException { - try { - holder.getZooKeeper().create(key, value.getBytes(Constants.UTF_8), authorities, createMode); - logger.debug("BaseProvider createCurrentOnly:{}", key); - } catch (KeeperException.NoNodeException e) { - logger.error("BaseProvider createCurrentOnly:{}", e.getMessage(), e); - // I don't know whether it will happen or not, if root watcher don't update rootExist timely - if (!exists(rootNode)){ - logger.info("BaseProvider createCurrentOnly root not exist:{}", count.get()); - Thread.sleep(50); - if (count.incrementAndGet() < 3) { - this.create(key, value, createMode, count); - } - } - } - } - - @Override - public void update(final String key, final String value) throws KeeperException, InterruptedException { - holder.getZooKeeper().setData(key, value.getBytes(Constants.UTF_8), Constants.VERSION); - } - - @Override - public void delete(final String key) throws KeeperException, InterruptedException { - holder.getZooKeeper().delete(key, Constants.VERSION); - logger.debug("BaseProvider deleteOnlyCurrent:{}", key); - } - - @Override - public void delete(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - holder.getZooKeeper().delete(key, Constants.VERSION, callback, ctx); - logger.debug("BaseProvider deleteOnlyCurrent:{},ctx:{}", key, ctx); - } - - - @Override - public String getRealPath(String path) { - return PathUtil.getRealPath(rootNode, path); - } - - @Override - public List getNecessaryPaths(final String key){ - List nodes = PathUtil.getPathOrderNodes(rootNode, key); - nodes.remove(rootNode); - return nodes; - } - - @Override - public Stack getDeletingPaths(String key) { - return PathUtil.getPathReverseNodes(rootNode, key); - } - - @Override - public void executeContention(final LeaderElection election) throws KeeperException, InterruptedException { - this.executeContention(rootNode, election); - } - - public void executeContention(final String nodeBeCompete, final LeaderElection election) throws KeeperException, InterruptedException { - election.executeContention(rootNode, this); - } - - /*@Override - public void createInTransaction(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { - transaction.create(key, value.getBytes(Constants.UTF_8), authorities, createMode); - }*/ - - @Override - public void resetConnection() { - try { - holder.reset(); - } catch (Exception ee) { - logger.error("resetConnection Exception:{}", ee.getMessage(), ee); - } - } - - public String getRootNode(){ - return rootNode; - } - - public Holder getHolder(){ - return holder; - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java deleted file mode 100644 index 89e615c551bb5..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/BaseStrategy.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.saaavsaaa.client.zookeeper.base; - -import com.saaavsaaa.client.action.IExecStrategy; -import com.saaavsaaa.client.action.IProvider; -import org.apache.zookeeper.KeeperException; - -/** - * Created by aaa - */ -public abstract class BaseStrategy implements IExecStrategy { - protected final IProvider provider; - public BaseStrategy(final IProvider provider){ - this.provider = provider; - } - - @Override - public String getDataString(final String key) throws KeeperException, InterruptedException { - return new String(getData(key)); - } - - public IProvider getProvider() { - return provider; - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java b/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java deleted file mode 100644 index 314ba0341bbf9..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/base/Holder.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.saaavsaaa.client.zookeeper.base; - -import com.saaavsaaa.client.utility.Properties; -import com.saaavsaaa.client.utility.StringUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.section.Listener; -import org.apache.zookeeper.WatchedEvent; -import org.apache.zookeeper.Watcher; -import org.apache.zookeeper.ZooKeeper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.util.concurrent.CountDownLatch; - -/** - * Created by aaa - */ -public class Holder { - private static final Logger logger = LoggerFactory.getLogger(Holder.class); - private static final CountDownLatch CONNECTED = new CountDownLatch(1); - - protected ZooKeeper zooKeeper; - protected final BaseContext context; - private boolean connected = false; - - Holder(final BaseContext context){ - this.context = context; - } - - public void start() throws IOException, InterruptedException { - logger.debug("Holder servers:{},sessionTimeOut:{}", context.servers, context.sessionTimeOut); - zooKeeper = new ZooKeeper(context.servers, context.sessionTimeOut, startWatcher()); - if (!StringUtil.isNullOrBlank(context.scheme)) { - zooKeeper.addAuthInfo(context.scheme, context.auth); - logger.debug("Holder scheme:{},auth:{}", context.scheme, context.auth); - } - CONNECTED.await(); - } - - private Watcher startWatcher() { - return new Watcher(){ - public void process(WatchedEvent event) { - logger.debug("BaseClient process event:{}", event.toString()); - if(Event.EventType.None == event.getType()){ - if(Event.KeeperState.SyncConnected == event.getState()){ - CONNECTED.countDown(); - connected = true; - logger.debug("BaseClient startWatcher SyncConnected"); - return; - } else if (Event.KeeperState.Expired == event.getState()){ - connected = false; - try { - logger.warn("startWatcher Event.KeeperState.Expired"); - reset(); - } catch (Exception ee){ - logger.error("event state Expired:{}", ee.getMessage(), ee); - } - } - } - if (context.globalListener != null){ - context.globalListener.process(event); - logger.debug("BaseClient " + Constants.GLOBAL_LISTENER_KEY + " process"); - } - if (Properties.INSTANCE.watchOn()){ - for (Listener listener : context.getWatchers().values()) { - if (listener.getPath() == null || listener.getPath().equals(event.getPath())){ - logger.debug("listener process:{}, listener:{}", listener.getPath(), listener.getKey()); - listener.process(event); - } - } - } - } - }; - } - - public void reset() throws IOException, InterruptedException { - logger.debug("zk reset...................................."); - close(); - start(); - logger.debug("....................................zk reset"); - } - - public void close() { - try { - zooKeeper.close(); - connected = false; - logger.debug("zk closed"); - this.context.close(); - } catch (Exception ee){ - logger.warn("Holder close:{}", ee.getMessage()); - } - } - - public ZooKeeper getZooKeeper() { - return zooKeeper; - } - - public boolean isConnected() { - return connected; - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java deleted file mode 100644 index d1e29e939f99d..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateAllNeedOperation.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.saaavsaaa.client.zookeeper.operation; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; - -/** - * Created by aaa - */ -public class CreateAllNeedOperation extends BaseOperation { - private final String key; - private final String value; - private final CreateMode createMode; - - public CreateAllNeedOperation(final IProvider provider, final String key, final String value, final CreateMode createMode) { - super(provider); - this.key = key; - this.value = value; - this.createMode = createMode; - } - - @Override - protected void execute() throws KeeperException, InterruptedException { - new UsualStrategy(provider).createAllNeedPath(key, value, createMode); - } - - @Override - public String toString(){ - return String.format("CreateAllNeedOperation key:%s,value:%s,createMode:%s", key, value, createMode.name()); - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java deleted file mode 100644 index bf0c5001bdd83..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/CreateCurrentOperation.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.saaavsaaa.client.zookeeper.operation; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; - -/** - * Created by aaa - */ -public class CreateCurrentOperation extends BaseOperation { - private final String key; - private final String value; - private final CreateMode createMode; - - public CreateCurrentOperation(final IProvider provider, final String key, final String value, final CreateMode createMode) { - super(provider); - this.key = key; - this.value = value; - this.createMode = createMode; - } - - @Override - public void execute() throws KeeperException, InterruptedException { - provider.create(provider.getRealPath(key), value, createMode); - } - - @Override - public String toString(){ - return String.format("CreateCurrentOperation key:%s,value:%s,createMode:%s", key, value, createMode.name()); - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java deleted file mode 100644 index 6587ada50ffba..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteAllChildrenOperation.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.saaavsaaa.client.zookeeper.operation; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; -import org.apache.zookeeper.KeeperException; - -/** - * Created by aaa - */ -public class DeleteAllChildrenOperation extends BaseOperation { - private final String key; - - public DeleteAllChildrenOperation(final IProvider provider, final String key) { - super(provider); - this.key = key; - } - - @Override - protected void execute() throws KeeperException, InterruptedException { - new UsualStrategy(provider).deleteAllChildren(key); - } - - @Override - public String toString(){ - return String.format("DeleteAllChildrenOperation key:%s", key); - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java deleted file mode 100644 index 3f633b0ec3074..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentBranchOperation.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.saaavsaaa.client.zookeeper.operation; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; -import org.apache.zookeeper.KeeperException; - -/** - * Created by aaa - */ -public class DeleteCurrentBranchOperation extends BaseOperation { - private final String key; - - public DeleteCurrentBranchOperation(final IProvider provider, final String key) { - super(provider); - this.key = key; - } - - @Override - protected void execute() throws KeeperException, InterruptedException { - new UsualStrategy(provider).deleteCurrentBranch(key); - } - - @Override - public String toString(){ - return String.format("DeleteCurrentBranchOperation key:%s", key); - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java deleted file mode 100644 index 31c635b28b169..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/DeleteCurrentOperation.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.saaavsaaa.client.zookeeper.operation; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import org.apache.zookeeper.KeeperException; - -/** - * Created by aaa - */ -public class DeleteCurrentOperation extends BaseOperation { - private final String key; - public DeleteCurrentOperation(final IProvider provider, final String key) { - super(provider); - this.key = key; - } - - @Override - protected void execute() throws KeeperException, InterruptedException { - provider.delete(provider.getRealPath(key)); - } - - @Override - public String toString(){ - return String.format("DeleteCurrentOperation key:%s", key); - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java b/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java deleted file mode 100644 index 2658aa50fd390..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/operation/UpdateOperation.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.saaavsaaa.client.zookeeper.operation; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.zookeeper.base.BaseOperation; -import org.apache.zookeeper.KeeperException; - -/** - * Created by aaa - */ -public class UpdateOperation extends BaseOperation { - private final String key; - private final String value; - - public UpdateOperation(final IProvider provider, final String key, final String value) { - super(provider); - this.key = key; - this.value = value; - } - - @Override - protected void execute() throws KeeperException, InterruptedException { - provider.update(provider.getRealPath(key), value); - } - - @Override - public String toString(){ - return String.format("UpdateOperation key:%s,value:%s", key, value); - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java b/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java deleted file mode 100644 index 86db346973797..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/provider/RetryProvider.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.saaavsaaa.client.zookeeper.provider; - -import com.saaavsaaa.client.retry.RetryCount; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; -import com.saaavsaaa.client.zookeeper.base.Holder; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.Watcher; -import org.apache.zookeeper.data.ACL; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -/** - * Created by aaa - */ -@Deprecated -public class RetryProvider extends BaseProvider { - private static final Logger logger = LoggerFactory.getLogger(RetryProvider.class); - - public RetryProvider(String rootNode, Holder holder, boolean watched, List authorities) { - super(rootNode, holder, watched, authorities); - RetryCount.INSTANCE.start(); - } - - // block - @Override - public byte[] getData(final String key) throws KeeperException, InterruptedException { - try { - return holder.getZooKeeper().getData(key, watched, null); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryProvider SessionExpiredException getData:{}", key); - if (RetryCount.INSTANCE.continueExecute()) { - byte[] data = getData(key); - RetryCount.INSTANCE.reset(); - return data; - } - throw ee; - } - } - - @Override - public boolean exists(final String key) throws KeeperException, InterruptedException { - try { - return null != holder.getZooKeeper().exists(key, watched); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryProvider SessionExpiredException checkExists:{}", key); - if (RetryCount.INSTANCE.continueExecute()) { - boolean result = exists(key); - RetryCount.INSTANCE.reset(); - return result; - } - throw ee; - } - } - - @Override - public boolean exists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { - try { - return null != holder.getZooKeeper().exists(key, watcher); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryProvider SessionExpiredException checkExists:{}", key); - if (RetryCount.INSTANCE.continueExecute()) { - boolean result = exists(key, watcher); - RetryCount.INSTANCE.reset(); - return result; - } - throw ee; - } - } - - @Override - public List getChildren(final String key) throws KeeperException, InterruptedException { - try { - return super.getChildren(key); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("RetryProvider SessionExpiredException getChildren:{}", key); - if (RetryCount.INSTANCE.continueExecute()) { - List result = getChildren(key); - RetryCount.INSTANCE.reset(); - return result; - } - throw ee; - } - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/section/Callable.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/Callable.java deleted file mode 100644 index 4380804d2fdf2..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/section/Callable.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.saaavsaaa.client.zookeeper.section; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.retry.DelayPolicyExecutor; -import com.saaavsaaa.client.retry.DelayRetryPolicy; -import org.apache.zookeeper.KeeperException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by aaa - */ -public abstract class Callable { - private static final Logger logger = LoggerFactory.getLogger(Callable.class); - - protected final DelayPolicyExecutor delayPolicyExecutor; - protected final IProvider provider; - private T result; - public Callable(final IProvider provider, final DelayRetryPolicy delayRetryPolicy){ - this.delayPolicyExecutor = new DelayPolicyExecutor(delayRetryPolicy); - this.provider = provider; - } - public abstract void call() throws KeeperException, InterruptedException; - - public void setResult(T result) { - this.result = result; - } - public T getResult() throws KeeperException, InterruptedException { - if (result == null) { - exec(); - } - return result; - } - - public void exec() throws KeeperException, InterruptedException { - try { - call(); - } catch (KeeperException e) { - logger.warn("exec KeeperException:{}", e.getMessage()); - delayPolicyExecutor.next(); - if (Connection.needReset(e)){ - provider.resetConnection(); - } else { - throw e; - } - execDelay(); - } catch (InterruptedException e) { - throw e; - } - } - - protected void execDelay() throws KeeperException, InterruptedException { - for (;;) { - long delay = delayPolicyExecutor.getNextTick() - System.currentTimeMillis(); - if (delay > 0){ - try { - logger.debug("exec delay:{}", delay); - Thread.sleep(delay); - } catch (InterruptedException ee) { - throw ee; - } - } else { - if (delayPolicyExecutor.hasNext()) { - logger.debug("exec hasNext"); - exec(); - } - break; - } - } - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/section/ClientContext.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/ClientContext.java deleted file mode 100644 index fd2dcb919baf2..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/section/ClientContext.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.saaavsaaa.client.zookeeper.section; - -import com.saaavsaaa.client.retry.DelayRetryPolicy; -import com.saaavsaaa.client.zookeeper.base.BaseClientFactory; -import com.saaavsaaa.client.zookeeper.base.BaseContext; - -/** - * Created by aaa - */ -public final class ClientContext extends BaseContext { - - private DelayRetryPolicy delayRetryPolicy; - private BaseClientFactory clientFactory; - - - public ClientContext(final String servers, final int sessionTimeoutMilliseconds) { - super(); - super.servers = servers; - super.sessionTimeOut = sessionTimeoutMilliseconds; - } - - public void setDelayRetryPolicy(DelayRetryPolicy delayRetryPolicy) { - this.delayRetryPolicy = delayRetryPolicy; - } - - public void setClientFactory(BaseClientFactory clientFactory) { - this.clientFactory = clientFactory; - } - - public DelayRetryPolicy getDelayRetryPolicy() { - return delayRetryPolicy; - } - - public BaseClientFactory getClientFactory() { - return clientFactory; - } - - public void close() { - super.close(); - this.delayRetryPolicy = null; - this.clientFactory = null; - } - - public void updateContext(final ClientContext context){ - this.delayRetryPolicy = context.getDelayRetryPolicy(); - this.clientFactory = context.clientFactory; - this.watchers.clear(); - this.watchers.putAll(context.getWatchers()); - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/section/ClientTask.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/ClientTask.java deleted file mode 100644 index 38b59a34df087..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/section/ClientTask.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.saaavsaaa.client.zookeeper.section; - -import com.saaavsaaa.client.action.IProvider; -import org.apache.zookeeper.KeeperException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by aaa - */ -public abstract class ClientTask implements Runnable { - private static final Logger logger = LoggerFactory.getLogger(ClientTask.class); - private final IProvider provider; - - public ClientTask(final IProvider provider){ - this.provider = provider; - } - - public abstract void run(final IProvider provider) throws KeeperException, InterruptedException; - - @Override - public void run() { - try { - run(provider); - } catch (Exception e) { - logger.error(e.getMessage(), e); - } - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/section/Connection.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/Connection.java deleted file mode 100644 index af58bacaac692..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/section/Connection.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.saaavsaaa.client.zookeeper.section; - -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.WatchedEvent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CountDownLatch; - -/** - * Created by aaa - */ -public class Connection { - private static final Logger logger = LoggerFactory.getLogger(Connection.class); - //is need reset - private static final Map exceptionResets = new ConcurrentHashMap<>(); - private ClientContext context; - - static { - exceptionResets.put(KeeperException.Code.SESSIONEXPIRED.intValue(), true); - exceptionResets.put(KeeperException.Code.SESSIONMOVED.intValue(), true); - exceptionResets.put(KeeperException.Code.CONNECTIONLOSS.intValue(), false); - exceptionResets.put(KeeperException.Code.OPERATIONTIMEOUT.intValue(), false); - } - - @Deprecated - public Connection(final ClientContext context){ - this.context = context; - } - - public static boolean needReset(KeeperException e) throws KeeperException { - int code = e.code().intValue(); - if (!exceptionResets.containsKey(code)){ - throw e; - } - return exceptionResets.get(code); - } - - @Deprecated - public void check(KeeperException e) throws KeeperException { - int code = e.code().intValue(); - if (!exceptionResets.containsKey(code)){ - throw e; - } - boolean reset = exceptionResets.get(code); - try { - if (reset){ - resetConnection(); - } else { - // block -// block(); - } - } catch (Exception ee){ - logger.error("check reconnect:{}", ee.getMessage(), ee); - } - } - - private void resetConnection() throws IOException, InterruptedException { - logger.debug("resetConnection......................................................"); -// IClient client = context.getClientFactory().newClientByOriginal(true).start(); -// this.context.updateContext(((BaseClient)client).getContext()); - logger.debug("......................................................connection reset"); - } - - private void block() throws InterruptedException { - logger.debug("block auto reconnection"); - final CountDownLatch autoReconnect = new CountDownLatch(1); - Listener listener = new Listener() { - @Override - public void process(WatchedEvent event) { - autoReconnect.countDown(); - logger.debug("block reconnected"); - } - }; - context.getWatchers().put(listener.getKey(), listener); - autoReconnect.await(); - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/section/Listener.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/Listener.java deleted file mode 100644 index 5596dae8bb245..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/section/Listener.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.saaavsaaa.client.zookeeper.section; - -import org.apache.zookeeper.WatchedEvent; - -/** - * Created by aaa - */ -public abstract class Listener { - private final String key; - private String path; - - public Listener(){ - this(null); - } - public Listener(final String path){ - this.path = path; - this.key = path + System.currentTimeMillis(); - } - - public abstract void process(WatchedEvent event); - - public String getPath() { - return path; - } - - public void setPath(final String path){ - this.path = path; - } - - public String getKey() { - return key; - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/section/StrategyType.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/StrategyType.java deleted file mode 100644 index 9685b100a968c..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/section/StrategyType.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.saaavsaaa.client.zookeeper.section; - -/** - * Created by aaa - */ -public enum StrategyType { - USUAL, - CONTEND, - SYNC_RETRY, - ASYNC_RETRY, - ALL_ASYNC_RETRY -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/section/WatcherCreator.java b/src/main/java/com/saaavsaaa/client/zookeeper/section/WatcherCreator.java deleted file mode 100644 index bc5321f9e3b42..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/section/WatcherCreator.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.saaavsaaa.client.zookeeper.section; - -import org.apache.zookeeper.WatchedEvent; -import org.apache.zookeeper.Watcher; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static org.apache.zookeeper.Watcher.Event.EventType.NodeDeleted; - -/** - * Created by aaa - */ -public class WatcherCreator { - private static final Logger logger = LoggerFactory.getLogger(WatcherCreator.class); - public static Watcher deleteWatcher(Listener listener){ - return new Watcher() { - @Override - public void process(WatchedEvent event) { - if (listener.getPath().equals(event.getPath()) && NodeDeleted.equals(event.getType())){ - listener.process(event); - logger.debug("delete node event:{}", event.toString()); - } - } - }; - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java deleted file mode 100644 index eb9c2be447bab..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AllAsyncRetryStrategy.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.saaavsaaa.client.zookeeper.strategy; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.retry.AsyncRetryCenter; -import com.saaavsaaa.client.retry.DelayRetryPolicy; -import com.saaavsaaa.client.zookeeper.operation.CreateAllNeedOperation; -import com.saaavsaaa.client.zookeeper.operation.DeleteAllChildrenOperation; -import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentBranchOperation; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by aaa - */ -public class AllAsyncRetryStrategy extends AsyncRetryStrategy { - private static final Logger logger = LoggerFactory.getLogger(AllAsyncRetryStrategy.class); - public AllAsyncRetryStrategy(final IProvider provider, final DelayRetryPolicy delayRetryPolicy) { - super(provider, delayRetryPolicy); - } - - @Override - public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - try { - super.createAllNeedPath(key, value, createMode); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("AllAsyncRetryStrategy SessionExpiredException CreateAllNeedOperation:{}", key); - AsyncRetryCenter.INSTANCE.add(new CreateAllNeedOperation(provider, key, value, createMode)); - } - } - - @Override - public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { - try { - super.deleteAllChildren(key); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("AllAsyncRetryStrategy SessionExpiredException deleteAllChildren:{}", key); - AsyncRetryCenter.INSTANCE.add(new DeleteAllChildrenOperation(provider, key)); - } - } - - @Override - public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - try { - super.deleteCurrentBranch(key); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("AllAsyncRetryStrategy SessionExpiredException deleteCurrentBranch:{}", key); - AsyncRetryCenter.INSTANCE.add(new DeleteCurrentBranchOperation(provider, key)); - } - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java deleted file mode 100644 index 20db38a188ecf..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/AsyncRetryStrategy.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.saaavsaaa.client.zookeeper.strategy; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.retry.AsyncRetryCenter; -import com.saaavsaaa.client.retry.DelayRetryPolicy; -import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; -import com.saaavsaaa.client.zookeeper.operation.DeleteCurrentOperation; -import com.saaavsaaa.client.zookeeper.operation.UpdateOperation; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by aaa - */ -public class AsyncRetryStrategy extends SyncRetryStrategy { - private static final Logger logger = LoggerFactory.getLogger(AsyncRetryStrategy.class); - - public AsyncRetryStrategy(final IProvider provider, final DelayRetryPolicy delayRetryPolicy){ - super(provider, delayRetryPolicy); - AsyncRetryCenter.INSTANCE.init(this.delayRetryPolicy); - AsyncRetryCenter.INSTANCE.start(); - } - - @Override - public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - String path = provider.getRealPath(key); - try { - provider.create(path, value, createMode); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("AsyncRetryStrategy SessionExpiredException createCurrentOnly:{}", path); - AsyncRetryCenter.INSTANCE.add(new CreateCurrentOperation(provider, path, value, createMode)); - } - } - - @Override - public void update(final String key, final String value) throws KeeperException, InterruptedException { - String path = provider.getRealPath(key); - try { - provider.update(path, value); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("AsyncRetryStrategy SessionExpiredException update:{}", path); - AsyncRetryCenter.INSTANCE.add(new UpdateOperation(provider, path, value)); - } - } - - @Override - public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - String path = provider.getRealPath(key); - try { - provider.delete(path); - } catch (KeeperException.SessionExpiredException ee){ - logger.warn("AsyncRetryStrategy SessionExpiredException deleteOnlyCurrent:{}", path); - AsyncRetryCenter.INSTANCE.add(new DeleteCurrentOperation(provider, path)); - } - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java deleted file mode 100644 index 525a6d75a73e8..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/ContentionStrategy.java +++ /dev/null @@ -1,242 +0,0 @@ -package com.saaavsaaa.client.zookeeper.strategy; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.election.LeaderElection; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.action.Callback; -import org.apache.zookeeper.AsyncCallback; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; -import java.util.Stack; - -/** - * Created by aaa - * The ContentionStrategy is effective only when all the clients of the node which be competitive are using ContentionStrategy. - */ -public class ContentionStrategy extends UsualStrategy { - private static final Logger logger = LoggerFactory.getLogger(ContentionStrategy.class); - public ContentionStrategy(final IProvider provider) { - super(provider); - } - - @Override - /* - * Don't use it if you don't have to use it. - */ - public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { - LeaderElection election = new LeaderElection() { - @Override - public void action() throws KeeperException, InterruptedException { - provider.getData(provider.getRealPath(key), callback, ctx); - logger.debug("ContentionStrategy getData action:{}", key); - } - }; - provider.executeContention(election); - logger.debug("ContentionStrategy getData executeContention"); - } - - @Override - public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - LeaderElection election = buildCreateElection(key, value, createMode, null); - provider.executeContention(election); - logger.debug("ContentionStrategy createCurrentOnly executeContention"); - election.waitDone(); - } - - private LeaderElection buildCreateElection(final String key, final String value, final CreateMode createMode, final Callback callback){ - return new LeaderElection() { - @Override - public void action() throws KeeperException, InterruptedException { - provider.create(provider.getRealPath(key), value, createMode); - } - @Override - public void callback(){ - if (callback != null) { - callback.processResult(); - } - } - }; - } - - @Override - public void update(final String key, final String value) throws KeeperException, InterruptedException { - LeaderElection election = buildUpdateElection(key, value, null); - provider.executeContention(election); - logger.debug("ContentionStrategy update executeContention"); - election.waitDone(); - } - - private LeaderElection buildUpdateElection(final String key, final String value, final Callback callback){ - return new LeaderElection() { - @Override - public void action() throws KeeperException, InterruptedException { - provider.update(provider.getRealPath(key), value); - logger.debug("ContentionStrategy update action:{},value:{}", key, value); - } - @Override - public void callback(){ - if (callback != null) { - callback.processResult(); - } - } - }; - } - - @Override - public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - LeaderElection election = buildDeleteElection(key, null); - provider.executeContention(election); - logger.debug("ContentionStrategy deleteOnlyCurrent executeContention"); - election.waitDone(); - } - - private LeaderElection buildDeleteElection(final String key, final Callback callback){ - return new LeaderElection() { - @Override - public void action() throws KeeperException, InterruptedException { - provider.delete(provider.getRealPath(key)); - } - @Override - public void callback(){ - if (callback != null) { - callback.processResult(); - } - } - }; - } - - @Override - public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - provider.executeContention(new LeaderElection() { - @Override - public void action() throws KeeperException, InterruptedException { - provider.delete(provider.getRealPath(key), callback, ctx); - logger.debug("ContentionStrategy deleteOnlyCurrent action:{},ctx:{}", key, ctx); - } - }); - } - - @Override - public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - LeaderElection election = buildCreateAllNeedElection(key, value, createMode, null); - provider.executeContention(election); - logger.debug("ContentionStrategy createAllNeedPath executeContention"); - election.waitDone(); - } - - private LeaderElection buildCreateAllNeedElection(final String key, final String value, final CreateMode createMode, final Callback callback){ - return new LeaderElection() { - @Override - public void action() throws KeeperException, InterruptedException { - logger.debug("ContentionStrategy createAllNeedPath action:{}", key); - createBegin(provider.getRealPath(key), value, createMode); - } - @Override - public void callback(){ - if (callback != null) { - callback.processResult(); - } - } - }; - } - - private void createBegin(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - provider.create(key, value, createMode); - return; - } - List nodes = provider.getNecessaryPaths(key); - for (int i = 0; i < nodes.size(); i++) { - if (provider.exists(nodes.get(i))){ - logger.info("create node exist:{}", nodes.get(i)); - continue; - } - logger.debug("create node not exist:", nodes.get(i)); - if (i == nodes.size() - 1){ - provider.create(nodes.get(i), value, createMode); - } else { - provider.create(nodes.get(i), Constants.NOTHING_VALUE, createMode); - } - } - } - - - @Override - public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { - provider.executeContention(new LeaderElection() { - @Override - public void action() throws KeeperException, InterruptedException { - deleteChildren(provider.getRealPath(key), true); - } - }); - logger.debug("ContentionStrategy deleteAllChildren executeContention"); - } - - private void deleteChildren(final String key, final boolean deleteCurrentNode) throws KeeperException, InterruptedException { - List children = provider.getChildren(key); - logger.debug("deleteChildren:{}", children); - for (int i = 0; i < children.size(); i++) { - String child = PathUtil.getRealPath(key, children.get(i)); - if (!provider.exists(child)){ - logger.info("delete not exist:{}", child); - continue; - } - logger.debug("deleteChildren:{}", child); - deleteChildren(child, true); - } - if (deleteCurrentNode){ - provider.delete(key); - } - } - - @Override - public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - provider.executeContention(new LeaderElection() { - @Override - public void action() throws KeeperException, InterruptedException { - deleteBranch(provider.getRealPath(key)); - } - }); - logger.debug("ContentionStrategy deleteCurrentBranch executeContention"); - } - - private void deleteBranch(String key) throws KeeperException, InterruptedException { - deleteChildren(key, false); - Stack pathStack = provider.getDeletingPaths(key); - while (!pathStack.empty()){ - String node = pathStack.pop(); - // contrast cache - if (provider.exists(node)){ - try { - provider.delete(node); - } catch (KeeperException.NotEmptyException ee){ - logger.warn("deleteBranch {} exist other children:{}", node, this.getChildren(node)); - logger.debug(ee.getMessage()); - return; - } - } - logger.info("deleteBranch node not exist:{}", node); - } - } - - - - //todo Use arbitrary competitive nodes - //IExecStrategy convert to ContentionStrategy====================================================================== - public void createCurrentOnly(final String key, final String value, final CreateMode createMode, final Callback callback) throws KeeperException, InterruptedException { - provider.executeContention(buildCreateElection(key, value, createMode, callback)); - } - - public void update(final String key, final String value, final Callback callback) throws KeeperException, InterruptedException { - provider.executeContention(buildUpdateElection(key, value, null)); - } - - public void deleteOnlyCurrent(final String key, final Callback callback) throws KeeperException, InterruptedException { - provider.executeContention(buildDeleteElection(key, null)); - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java deleted file mode 100644 index b797df32c9dfe..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/SyncRetryStrategy.java +++ /dev/null @@ -1,141 +0,0 @@ -package com.saaavsaaa.client.zookeeper.strategy; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.retry.DelayRetryPolicy; -import com.saaavsaaa.client.zookeeper.section.Callable; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.Watcher; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -/** - * Created by aaa - */ -public class SyncRetryStrategy extends UsualStrategy{ - private static final Logger logger = LoggerFactory.getLogger(SyncRetryStrategy.class); - protected final DelayRetryPolicy delayRetryPolicy; - - public SyncRetryStrategy(final IProvider provider, final DelayRetryPolicy delayRetryPolicy) { - super(provider); - if (delayRetryPolicy == null){ - logger.warn("Callable constructor context's delayRetryPolicy is null"); - this.delayRetryPolicy = DelayRetryPolicy.newNoInitDelayPolicy(); - } else { - this.delayRetryPolicy = delayRetryPolicy; - } -// this.delayRetryPolicy = delayRetryPolicy == null? DelayRetryPolicy.newNoInitDelayPolicy() : delayRetryPolicy; - } - - @Override - public byte[] getData(final String key) throws KeeperException, InterruptedException { - Callable callable = new Callable(provider, delayRetryPolicy) { - @Override - public void call() throws KeeperException, InterruptedException { - setResult(provider.getData(provider.getRealPath(key))); - } - }; - return callable.getResult(); - } - - @Override - public boolean checkExists(final String key) throws KeeperException, InterruptedException { - Callable callable = new Callable(provider, delayRetryPolicy) { - @Override - public void call() throws KeeperException, InterruptedException { - setResult(provider.exists(provider.getRealPath(key))); - } - }; - return callable.getResult(); - } - - @Override - public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { - Callable callable = new Callable(provider, delayRetryPolicy) { - @Override - public void call() throws KeeperException, InterruptedException { - setResult(provider.exists(provider.getRealPath(key), watcher)); - } - }; - return callable.getResult(); - } - - @Override - public List getChildren(final String key) throws KeeperException, InterruptedException { - Callable> callable = new Callable(provider, delayRetryPolicy) { - @Override - public void call() throws KeeperException, InterruptedException { - setResult(provider.getChildren(provider.getRealPath(key))); - } - }; - return callable.getResult(); - } - - @Override - public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - Callable callable = new Callable(provider, delayRetryPolicy) { - @Override - public void call() throws KeeperException, InterruptedException { - provider.create(provider.getRealPath(key), value, createMode); - } - }; - callable.exec(); - } - - @Override - public void update(final String key, final String value) throws KeeperException, InterruptedException { - Callable callable = new Callable(provider, delayRetryPolicy) { - @Override - public void call() throws KeeperException, InterruptedException { - provider.update(provider.getRealPath(key), value); - } - }; - callable.exec(); - } - - @Override - public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - Callable callable = new Callable(provider, delayRetryPolicy) { - @Override - public void call() throws KeeperException, InterruptedException { - provider.delete(provider.getRealPath(key)); - } - }; - callable.exec(); - } - - @Override - public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - Callable callable = new Callable(provider, delayRetryPolicy) { - @Override - public void call() throws KeeperException, InterruptedException { - new UsualStrategy(provider).createAllNeedPath(key, value, createMode); - } - }; - callable.exec(); - } - - @Override - public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { - Callable callable = new Callable(provider, delayRetryPolicy) { - @Override - public void call() throws KeeperException, InterruptedException { - new UsualStrategy(provider).deleteAllChildren(key); - } - }; - callable.exec(); - } - - @Override - public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - Callable callable = new Callable(provider, delayRetryPolicy) { - @Override - public void call() throws KeeperException, InterruptedException { - new UsualStrategy(provider).deleteCurrentBranch(key); - } - }; - callable.exec(); - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java deleted file mode 100644 index 4027d4dcbd9dd..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/TransactionContendStrategy.java +++ /dev/null @@ -1,138 +0,0 @@ -package com.saaavsaaa.client.zookeeper.strategy; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.election.LeaderElection; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.action.Callback; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; -import com.saaavsaaa.client.zookeeper.transaction.ZKTransaction; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; -import java.util.Stack; - -/** - * Created by aaa - * nothing use - * since zookeeper 3.4.0 - */ -public class TransactionContendStrategy extends ContentionStrategy { - private static final Logger logger = LoggerFactory.getLogger(TransactionContendStrategy.class); - public TransactionContendStrategy(IProvider provider) { - super(provider); - } - -// @Override -// public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { -// LeaderElection election = buildCreateAllNeedElection(key, value, createMode, null); -// provider.executeContention(election); -// logger.debug("ContentionStrategy createAllNeedPath executeContention"); -// election.waitDone(); -// } -// -// private LeaderElection buildCreateAllNeedElection(final String key, final String value, final CreateMode createMode, final Callback callback){ -// return new LeaderElection() { -// @Override -// public void action() throws KeeperException, InterruptedException { -// logger.debug("ContentionStrategy createAllNeedPath action:{}", key); -// ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getHolder()); -// createBegin(key, value, createMode, transaction); -// transaction.commit(); -// } -// @Override -// public void callback(){ -// if (callback != null) { -// callback.processResult(); -// } -// } -// }; -// } -// -// private void createBegin(final String key, final String value, final CreateMode createMode, final ZKTransaction transaction) throws KeeperException, InterruptedException { -// if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ -// provider.createInTransaction(key, value, createMode, transaction); -// return; -// } -// List nodes = provider.getNecessaryPaths(key); -// for (int i = 0; i < nodes.size(); i++) { -// if (provider.exists(nodes.get(i))){ -// logger.info("create node exist:{}", nodes.get(i)); -// continue; -// } -// logger.debug("node not exist and create:", nodes.get(i)); -// if (i == nodes.size() - 1){ -// provider.createInTransaction(nodes.get(i), value, createMode, transaction); -// } else { -// provider.createInTransaction(nodes.get(i), Constants.NOTHING_VALUE, createMode, transaction); -// } -// } -// } -// -// @Override -// public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { -// provider.executeContention(new LeaderElection() { -// @Override -// public void action() throws KeeperException, InterruptedException { -// ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getHolder()); -// deleteChildren(provider.getRealPath(key), true, transaction); -// transaction.commit(); -// } -// }); -// logger.debug("ContentionStrategy deleteAllChildren executeContention"); -// } -// -// private void deleteChildren(final String key, final boolean deleteCurrentNode, final ZKTransaction transaction) throws KeeperException, InterruptedException { -// List children = provider.getChildren(key); -// for (int i = 0; i < children.size(); i++) { -// String child = PathUtil.getRealPath(key, children.get(i)); -// if (!provider.exists(child)){ -// logger.info("delete not exist:{}", child); -// continue; -// } -// logger.debug("deleteChildren:{}", child); -// deleteChildren(child, true, transaction); -// } -// if (deleteCurrentNode){ -// transaction.delete(key, Constants.VERSION); -// } -// } -// -// @Override -// public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { -// provider.executeContention(new LeaderElection() { -// @Override -// public void action() throws KeeperException, InterruptedException { -// ZKTransaction transaction = new ZKTransaction(((BaseProvider)provider).getRootNode(), ((BaseProvider)provider).getHolder()); -// deleteBranch(provider.getRealPath(key), transaction); -// transaction.commit(); -// } -// }); -// logger.debug("ContentionStrategy deleteCurrentBranch executeContention"); -// } -// -// private void deleteBranch(String key, final ZKTransaction transaction) throws KeeperException, InterruptedException { -// deleteChildren(key, false, transaction); -// Stack pathStack = provider.getDeletingPaths(key); -// String prePath = key; -// while (!pathStack.empty()){ -// String node = pathStack.pop(); -// // contrast cache -// // Performance needs testing -// List children = provider.getChildren(node); -// boolean canDelete = children.size() == 0 || children.size() == 1; -// if (children.size() == 1){ -// if (!PathUtil.getRealPath(node, children.get(0)).equals(prePath)){ -// canDelete = false; -// } -// } -// if (provider.exists(node) && canDelete){ -// transaction.delete(node, Constants.VERSION); -// } -// prePath = node; -// } -// } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java deleted file mode 100644 index ee93a2b1d6ade..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/UsualStrategy.java +++ /dev/null @@ -1,164 +0,0 @@ -package com.saaavsaaa.client.zookeeper.strategy; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.base.BaseStrategy; -import org.apache.zookeeper.AsyncCallback; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.Watcher; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -/** - * Created by aaa - */ -public class UsualStrategy extends BaseStrategy { - private static final Logger logger = LoggerFactory.getLogger(UsualStrategy.class); - public UsualStrategy(final IProvider provider){ - super(provider); - } - - @Override - public byte[] getData(final String key) throws KeeperException, InterruptedException { - return provider.getData(provider.getRealPath(key)); - } - - @Override - public void getData(final String key, final AsyncCallback.DataCallback callback, final Object ctx) throws KeeperException, InterruptedException { - provider.getData(provider.getRealPath(key), callback, ctx); - } - - @Override - public boolean checkExists(final String key) throws KeeperException, InterruptedException { - return provider.exists(provider.getRealPath(key)); - } - - @Override - public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { - return provider.exists(provider.getRealPath(key), watcher); - } - - @Override - public List getChildren(final String key) throws KeeperException, InterruptedException { - return provider.getChildren(provider.getRealPath(key)); - } - - @Override - public void createCurrentOnly(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - provider.create(provider.getRealPath(key), value, createMode); - } - - @Override - public void update(final String key, final String value) throws KeeperException, InterruptedException { - provider.update(provider.getRealPath(key), value); - } - - @Override - public void deleteOnlyCurrent(final String key) throws KeeperException, InterruptedException { - provider.delete(provider.getRealPath(key)); - } - - @Override - public void deleteOnlyCurrent(final String key, final AsyncCallback.VoidCallback callback, final Object ctx) throws KeeperException, InterruptedException { - provider.delete(provider.getRealPath(key), callback, ctx); - } - - @Override - public void createAllNeedPath(final String key, final String value, final CreateMode createMode) throws KeeperException, InterruptedException { - if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - this.createCurrentOnly(key, value, createMode); - return; - } - List nodes = provider.getNecessaryPaths(key); - for (int i = 0; i < nodes.size(); i++) { - try { -// this.deleteAllChildren(nodes.get(i)); - if (i == nodes.size() - 1){ - this.createCurrentOnly(nodes.get(i), value, createMode); - } else { - this.createCurrentOnly(nodes.get(i), Constants.NOTHING_VALUE, CreateMode.PERSISTENT); - } - logger.debug("node not exist and create:", nodes.get(i)); - } catch (KeeperException.NodeExistsException ee){ - logger.debug("create node exist:{}", nodes.get(i)); - continue; - } - } - } - - @Override - public void deleteAllChildren(final String key) throws KeeperException, InterruptedException { - logger.debug("deleteAllChildren:{}", key); - this.deleteChildren(provider.getRealPath(key), true); - } - - private void deleteChildren(final String path, final boolean deleteCurrentNode) throws KeeperException, InterruptedException { - logger.debug("deleteChildren:{}", path); - List children; - try{ - children = provider.getChildren(path); - } catch (KeeperException.NoNodeException e){ - logger.warn("deleteChildren node not exist:{},e:{}", path, e.getMessage()); - return; - } - for (String child : children){ - child = PathUtil.getRealPath(path, child); - this.deleteAllChildren(child); - } - if (deleteCurrentNode){ - try{ - this.deleteOnlyCurrent(path); - } catch(KeeperException.NotEmptyException e){ - logger.warn("deleteCurrentNode exist children:{},e:{}", path, e.getMessage()); - deleteChildren(path, true); - } catch(KeeperException.NoNodeException e){ - logger.warn("deleteCurrentNode node not exist:{},e:{}", path, e.getMessage()); - } - } - } - - /* - * delete the current node with force and delete the super node whose only child node is current node recursively - */ - @Override - public void deleteCurrentBranch(final String key) throws KeeperException, InterruptedException { - logger.debug("deleteCurrentBranch:{}", key); - if (key.indexOf(Constants.PATH_SEPARATOR) < -1){ - this.deleteOnlyCurrent(key); - return; - } - String path = provider.getRealPath(key); - this.deleteChildren(path, true); - String superPath = path.substring(0, path.lastIndexOf(Constants.PATH_SEPARATOR)); - try { - this.deleteRecursively(superPath); - } catch (KeeperException.NotEmptyException ee){ - logger.warn("deleteCurrentBranch exist children:{},e:{}", path, ee.getMessage()); - return; - } catch (KeeperException.NoNodeException ee){ - logger.warn("deleteCurrentBranch NoNodeException:{},e:{}", superPath, ee.getMessage()); - } - } - - private void deleteRecursively(final String path) throws KeeperException, InterruptedException { - logger.debug("deleteRecursively:{}", path); - int index = path.lastIndexOf(Constants.PATH_SEPARATOR); - if (index == 0){ - this.deleteOnlyCurrent(path); - return; - } - String superPath = path.substring(0, index); - try { - this.deleteOnlyCurrent(path); - this.deleteRecursively(superPath); - } catch (KeeperException.NotEmptyException ee){ - logger.warn("deleteRecursively exist children:{},e:{}", path, ee.getMessage()); - logger.info("deleteRecursively {} exist other children:{}", path, this.getChildren(path)); - return; - } - } -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/VersionStrategy.java b/src/main/java/com/saaavsaaa/client/zookeeper/strategy/VersionStrategy.java deleted file mode 100644 index b1a0326fd44f3..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/strategy/VersionStrategy.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.saaavsaaa.client.zookeeper.strategy; - -/** - * Created by aaa - * todo node version - */ -public class VersionStrategy { -} diff --git a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java b/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java deleted file mode 100644 index a78fbed62a931..0000000000000 --- a/src/main/java/com/saaavsaaa/client/zookeeper/transaction/ZKTransaction.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.saaavsaaa.client.zookeeper.transaction; - -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.base.Holder; -import org.apache.zookeeper.*; -import org.apache.zookeeper.data.ACL; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -/** - * Created by aaa - */ -public class ZKTransaction { - private static final Logger logger = LoggerFactory.getLogger(ZKTransaction.class); -// private final Transaction transaction; - private final String rootNode; - - public ZKTransaction(final String root, final Holder holder) { -// transaction = holder.getZooKeeper().transaction(); - rootNode = root; - logger.debug("ZKTransaction root:{}", rootNode); - } - - /*public ZKTransaction create(final String path, final byte[] data, final List acl, final CreateMode createMode) { - this.transaction.create(PathUtil.getRealPath(rootNode, path), data, acl, createMode); - logger.debug("wait create:{},data:{},acl:{},createMode:{}", new Object[]{path, data, acl, createMode}); - return this; - } - - public ZKTransaction delete(final String path){ - return delete(path, Constants.VERSION); - } - public ZKTransaction delete(final String path, final int version) { - this.transaction.delete(PathUtil.getRealPath(rootNode, path), version); - logger.debug("wait delete:{}", path); - return this; - } - - public ZKTransaction check(final String path){ - return check(path, Constants.VERSION); - } - public ZKTransaction check(final String path, final int version) { - this.transaction.check(PathUtil.getRealPath(rootNode, path), version); - logger.debug("wait check:{}", path); - return this; - } - - public ZKTransaction setData(final String path, final byte[] data){ - return setData(path, data, Constants.VERSION); - } - public ZKTransaction setData(final String path, final byte[] data, final int version) { - this.transaction.setData(PathUtil.getRealPath(rootNode, path), data, version); - logger.debug("wait setData:{},data:{}", path, data); - return this; - } - - public List commit() throws InterruptedException, KeeperException { - logger.debug("ZKTransaction commit"); - return this.transaction.commit(); - } - - public void commit(final AsyncCallback.MultiCallback cb, final Object ctx) { - this.transaction.commit(cb, ctx); - logger.debug("ZKTransaction commit ctx:{}", ctx); - }*/ -} diff --git a/src/main/resources/client.properties b/src/main/resources/client.properties deleted file mode 100644 index a4df6245998e0..0000000000000 --- a/src/main/resources/client.properties +++ /dev/null @@ -1,5 +0,0 @@ -client.id=1 -client.watch.on=up -client.thread.delay=1000 -client.thread.period=3000 -node.election.count=3 \ No newline at end of file diff --git a/src/main/resources/code/OrchestrationFacade.java b/src/main/resources/code/OrchestrationFacade.java deleted file mode 100644 index b257be8b9f000..0000000000000 --- a/src/main/resources/code/OrchestrationFacade.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.jdbc.orchestration.internal; - -import com.google.common.base.Preconditions; -import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration; -import io.shardingsphere.core.api.config.ShardingRuleConfiguration; -import io.shardingsphere.core.jdbc.core.datasource.MasterSlaveDataSource; -import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; -import io.shardingsphere.jdbc.orchestration.api.config.OrchestrationConfiguration; -import io.shardingsphere.jdbc.orchestration.internal.config.ConfigurationService; -import io.shardingsphere.jdbc.orchestration.internal.listener.ListenerFactory; -import io.shardingsphere.jdbc.orchestration.internal.state.datasource.DataSourceService; -import io.shardingsphere.jdbc.orchestration.internal.state.instance.InstanceStateService; -import io.shardingsphere.jdbc.orchestration.reg.api.RegistryCenter; -import io.shardingsphere.jdbc.orchestration.reg.api.RegistryCenterConfiguration; -import io.shardingsphere.jdbc.orchestration.reg.etcd.EtcdConfiguration; -import io.shardingsphere.jdbc.orchestration.reg.etcd.EtcdRegistryCenter; -import io.shardingsphere.jdbc.orchestration.reg.newzk.NewZookeeperRegistryCenter; -import io.shardingsphere.jdbc.orchestration.reg.zookeeper.ZookeeperConfiguration; -import io.shardingsphere.jdbc.orchestration.reg.zookeeper.ZookeeperRegistryCenter; -import lombok.Getter; -import lombok.extern.slf4j.Slf4j; - -import javax.sql.DataSource; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Properties; - -/** - * Orchestration service facade. - * - * @author zhangliang - * @author caohao - */ -@Slf4j -public final class OrchestrationFacade implements AutoCloseable { - - private final boolean isOverwrite; - - @Getter - private final ConfigurationService configService; - - private final InstanceStateService instanceStateService; - - private final DataSourceService dataSourceService; - - private final ListenerFactory listenerManager; - - private final RegistryCenter regCenter; - - public OrchestrationFacade(final OrchestrationConfiguration config) { - regCenter = createRegistryCenter(config.getRegCenterConfig()); - isOverwrite = config.isOverwrite(); - configService = new ConfigurationService(config.getName(), regCenter); - instanceStateService = new InstanceStateService(config.getName(), regCenter); - dataSourceService = new DataSourceService(config.getName(), regCenter); - listenerManager = new ListenerFactory(config.getName(), regCenter); - } - - private RegistryCenter createRegistryCenter(final RegistryCenterConfiguration regCenterConfig) { - Preconditions.checkNotNull(regCenterConfig, "Registry center configuration cannot be null."); - if (regCenterConfig instanceof ZookeeperConfiguration) { - return getZookeeperRegistryCenter((ZookeeperConfiguration) regCenterConfig); - } - if (regCenterConfig instanceof EtcdConfiguration) { - return new EtcdRegistryCenter((EtcdConfiguration) regCenterConfig); - } - throw new UnsupportedOperationException(regCenterConfig.getClass().getName()); - } - - private RegistryCenter getZookeeperRegistryCenter(final ZookeeperConfiguration regCenterConfig){ - if (regCenterConfig.isUseNative()){ - return new NewZookeeperRegistryCenter(regCenterConfig); - } else { - return new ZookeeperRegistryCenter(regCenterConfig); - } - } - - /** - * Initialize for sharding orchestration. - * - * @param dataSourceMap data source map - * @param shardingRuleConfig sharding rule configuration - * @param configMap config map - * @param props sharding properties - * @param shardingDataSource sharding data source - */ - public void init(final Map dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig, - final Map configMap, final Properties props, final ShardingDataSource shardingDataSource) { - if (shardingRuleConfig.getMasterSlaveRuleConfigs().isEmpty()) { - reviseShardingRuleConfigurationForMasterSlave(dataSourceMap, shardingRuleConfig); - } - configService.persistShardingConfiguration(getActualDataSourceMapForMasterSlave(dataSourceMap), shardingRuleConfig, configMap, props, isOverwrite); - instanceStateService.persistShardingInstanceOnline(); - dataSourceService.persistDataSourcesNode(); - listenerManager.initShardingListeners(shardingDataSource); - } - - /** - * Initialize for master-slave orchestration. - * - * @param dataSourceMap data source map - * @param masterSlaveRuleConfig master-slave rule configuration - * @param configMap config map - * @param masterSlaveDataSource master-slave source - */ - public void init(final Map dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, - final Map configMap, final MasterSlaveDataSource masterSlaveDataSource) { - configService.persistMasterSlaveConfiguration(dataSourceMap, masterSlaveRuleConfig, configMap, isOverwrite); - instanceStateService.persistMasterSlaveInstanceOnline(); - dataSourceService.persistDataSourcesNode(); - listenerManager.initMasterSlaveListeners(masterSlaveDataSource); - } - - private void reviseShardingRuleConfigurationForMasterSlave(final Map dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig) { - for (Entry entry : dataSourceMap.entrySet()) { - if (entry.getValue() instanceof MasterSlaveDataSource) { - MasterSlaveDataSource masterSlaveDataSource = (MasterSlaveDataSource) entry.getValue(); - shardingRuleConfig.getMasterSlaveRuleConfigs().add(getMasterSlaveRuleConfiguration(masterSlaveDataSource)); - } - } - } - - private Map getActualDataSourceMapForMasterSlave(final Map dataSourceMap) { - Map result = new LinkedHashMap<>(); - for (Entry entry : dataSourceMap.entrySet()) { - if (entry.getValue() instanceof MasterSlaveDataSource) { - MasterSlaveDataSource masterSlaveDataSource = (MasterSlaveDataSource) entry.getValue(); - result.putAll(masterSlaveDataSource.getAllDataSources()); - } else { - result.put(entry.getKey(), entry.getValue()); - } - } - return result; - } - - private MasterSlaveRuleConfiguration getMasterSlaveRuleConfiguration(final MasterSlaveDataSource masterSlaveDataSource) { - return new MasterSlaveRuleConfiguration( - masterSlaveDataSource.getMasterSlaveRule().getName(), masterSlaveDataSource.getMasterSlaveRule().getMasterDataSourceName(), - masterSlaveDataSource.getMasterSlaveRule().getSlaveDataSourceNames(), masterSlaveDataSource.getMasterSlaveRule().getLoadBalanceAlgorithm()); - } - - @Override - public void close() { - try { - regCenter.close(); - // CHECKSTYLE:OFF - } catch (final Exception ex) { - // CHECKSTYLE:ON - log.warn("RegCenter exception for: {}", ex.getMessage()); - } - } -} diff --git a/src/main/resources/code/ZookeeperConfiguration.java b/src/main/resources/code/ZookeeperConfiguration.java deleted file mode 100644 index 9b9794962fa4a..0000000000000 --- a/src/main/resources/code/ZookeeperConfiguration.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.jdbc.orchestration.reg.zookeeper; - -import io.shardingsphere.jdbc.orchestration.reg.api.RegistryCenterConfiguration; -import lombok.Getter; -import lombok.Setter; - -/** - * Zookeeper based registry center configuration. - * - * @author zhangliang - */ -@Getter -@Setter -public final class ZookeeperConfiguration implements RegistryCenterConfiguration { - - /** - * Zookeeper server list. - * - *

Include ip address and port, multiple servers split by comma. Etc: {@code host1:2181,host2:2181}

- */ - private String serverLists; - - /** - * Namespace of zookeeper. - */ - private String namespace; - - /** - * Base sleep time milliseconds. - */ - private int baseSleepTimeMilliseconds = 1000; - - /** - * Max sleep time milliseconds. - */ - private int maxSleepTimeMilliseconds = 3000; - - /** - * Max retries. - */ - private int maxRetries = 3; - - /** - * Session timeout milliseconds. - */ - private int sessionTimeoutMilliseconds; - - /** - * Connection timeout milliseconds. - */ - private int connectionTimeoutMilliseconds; - - /** - * Digest for zookeeper. - * - *

Default is not need digest

- */ - private String digest; - - /** - * RegCenter for zookeeper. - * - *

Default is ZookeeperRegistryCenter

- */ - private boolean useNative; -} diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties deleted file mode 100644 index dadc8aa5d4ac1..0000000000000 --- a/src/main/resources/log4j.properties +++ /dev/null @@ -1,31 +0,0 @@ -log4j.rootLogger=stdout - -log4j.appender.INFO=org.apache.log4j.ConsoleAppender -log4j.appender.INFO.layout=org.apache.log4j.PatternLayout -log4j.appender.INFO.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %p [%c] %m%n - -log4j.appender.stdout=org.apache.log4j.ConsoleAppender -log4j.appender.stdout.layout=org.apache.log4j.PatternLayout -log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %p [%c] %m%n - -log4j.logger.org.apache.zookeeper=INFO,zk -#don't append into root, only this -log4j.additivity.org.apache.zookeeper=false -log4j.appender.zk=org.apache.log4j.DailyRollingFileAppender -log4j.appender.zk.DatePattern='.'yyyy-MM-dd'.log' -log4j.appender.zk.File=logs/zk.log -log4j.appender.zk.layout=org.apache.log4j.PatternLayout -log4j.appender.zk.Append=true -log4j.appender.zk.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p][%c] [%t] - %m%n -log4j.appender.zk.encoding=UTF-8 - -log4j.logger.com.saaavsaaa.client=DEBUG,INFO,client -#don't append into root, only this -log4j.additivity.com.saaavsaaa.client=false -log4j.appender.client=org.apache.log4j.DailyRollingFileAppender -log4j.appender.client.DatePattern='.'yyyy-MM-dd'.log' -log4j.appender.client.File=logs/client.log -log4j.appender.client.layout=org.apache.log4j.PatternLayout -log4j.appender.client.Append=true -log4j.appender.client.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p][%c] [%t] - %m%n -log4j.appender.client.encoding=UTF-8 \ No newline at end of file diff --git a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java b/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java deleted file mode 100644 index 4dee0d0d9e413..0000000000000 --- a/src/test/java/com/saaavsaaa/client/retry/AsyncRetryCenterTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.saaavsaaa.client.retry; - -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; -import com.saaavsaaa.client.zookeeper.section.Listener; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.ClientFactory; -import com.saaavsaaa.client.zookeeper.TestSupport; -import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.section.StrategyType; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.ZooDefs; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; - -/** - * Created by aaa - */ -public class AsyncRetryCenterTest { - private IProvider provider; - private IClient client; - - @Before - public void start() throws IOException, InterruptedException { - client = createClient(); - provider = ((BaseClient)client).getStrategy().getProvider(); - AsyncRetryCenter.INSTANCE.init(new DelayRetryPolicy(3, 3, 10)); - AsyncRetryCenter.INSTANCE.start(); - } - - protected IClient createClient() throws IOException, InterruptedException { - ClientFactory creator = new ClientFactory(); - Listener listener = TestSupport.buildListener(); - IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - client.useExecStrategy(StrategyType.ASYNC_RETRY); - return client; - } - - @After - public void stop(){ - client.close(); - } - - @Test - public void close() throws Exception { - client.close(); - assert !((BaseProvider)provider).getHolder().isConnected(); - } - - @Test - public void create() throws InterruptedException, KeeperException { - String key = "a"; - String value = "bbb11"; - if (!provider.exists("/" + TestSupport.ROOT)) { - System.out.println("exist root"); - provider.create("/" + TestSupport.ROOT, Constants.NOTHING_VALUE, CreateMode.PERSISTENT); - } - AsyncRetryCenter.INSTANCE.add(new TestCreateCurrentOperation(provider, key, value, CreateMode.PERSISTENT)); - Thread.sleep(2000); - String path = PathUtil.getRealPath(TestSupport.ROOT, key); -// assert provider.exists(path); - assert client.checkExists(path); - client.useExecStrategy(StrategyType.USUAL); - client.deleteAllChildren(path); - client.deleteCurrentBranch(path); - client.useExecStrategy(StrategyType.ASYNC_RETRY); - assert !provider.exists(path); - } -} diff --git a/src/test/java/com/saaavsaaa/client/retry/TestCallable.java b/src/test/java/com/saaavsaaa/client/retry/TestCallable.java deleted file mode 100644 index a2413b7640999..0000000000000 --- a/src/test/java/com/saaavsaaa/client/retry/TestCallable.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.saaavsaaa.client.retry; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; -import com.saaavsaaa.client.zookeeper.section.Callable; -import org.apache.zookeeper.KeeperException; - -/** - * Created by aaa - */ -public abstract class TestCallable extends Callable { - private int count = 0; - - public TestCallable(final IProvider provider, final DelayRetryPolicy delayRetryPolicy) { - super(provider, delayRetryPolicy); - } - - @Override - public void call() throws KeeperException, InterruptedException { - if (count < 2){ - count++; -// throw new KeeperException.SessionExpiredException(); -// throw new KeeperException.ConnectionLossException(); - - System.out.println("TestCallable injectSessionExpiration==================================================" + count); - ((BaseProvider)provider).getHolder().getZooKeeper().getTestable().injectSessionExpiration(); -// ((BaseProvider)provider).getHolder().close(); - } - System.out.println("TestCallable ================================================" + count); - test(); - } - - public abstract void test() throws KeeperException, InterruptedException; -} diff --git a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java b/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java deleted file mode 100644 index 1f3a23c20172a..0000000000000 --- a/src/test/java/com/saaavsaaa/client/retry/TestCreateCurrentOperation.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.saaavsaaa.client.retry; - -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.zookeeper.base.BaseProvider; -import com.saaavsaaa.client.zookeeper.operation.CreateCurrentOperation; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; - -/** - * Created by aaa - */ -public class TestCreateCurrentOperation extends CreateCurrentOperation { - private int count = 0; - - public TestCreateCurrentOperation(final IProvider provider, String key, String value, CreateMode createMode) { - super(provider, key, value, createMode); - } - - @Override - public void execute() throws KeeperException, InterruptedException { - if (count < 2){ - count++; -// throw new KeeperException.SessionExpiredException(); -// throw new KeeperException.ConnectionLossException(); - - System.out.println("test injectSessionExpiration==================================================" + count); - ((BaseProvider)provider).getHolder().getZooKeeper().getTestable().injectSessionExpiration(); -// ((BaseProvider)provider).getHolder().close(); - } - System.out.println("test ================================================" + count); - super.execute(); - } -} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java deleted file mode 100644 index 8d6f966b15e39..0000000000000 --- a/src/test/java/com/saaavsaaa/client/zookeeper/AsyncRetryStrategyTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.retry.AsyncRetryCenter; -import com.saaavsaaa.client.retry.DelayRetryPolicy; -import com.saaavsaaa.client.zookeeper.section.Listener; -import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.section.StrategyType; -import org.apache.zookeeper.ZooDefs; -import org.junit.Before; - -import java.io.IOException; - -/** - * Created by aaa - */ -public class AsyncRetryStrategyTest extends UsualClientTest{ - @Before - public void start() throws IOException, InterruptedException { - super.start(); - AsyncRetryCenter.INSTANCE.init(new DelayRetryPolicy(3, 3, 10)); - AsyncRetryCenter.INSTANCE.start(); - } - - @Override - protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { - Listener listener = TestSupport.buildListener(); - IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - client.useExecStrategy(StrategyType.ASYNC_RETRY); - return client; - } -} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java deleted file mode 100644 index ea17944865b3c..0000000000000 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheClientTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -import com.saaavsaaa.client.action.IClient; -import org.apache.zookeeper.ZooDefs; - -import java.io.IOException; - -/** - * Created by aaa - * todo test check cache content - */ -public class CacheClientTest extends UsualClientTest { - @Override - protected IClient createClient(ClientFactory creator) throws IOException, InterruptedException { - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); - } -} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java deleted file mode 100644 index 8d0df3ca25729..0000000000000 --- a/src/test/java/com/saaavsaaa/client/zookeeper/CacheWathClientTest.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.zookeeper.section.Listener; -import org.apache.zookeeper.ZooDefs; - -import java.io.IOException; - -/** - * Created by aaa - */ -public class CacheWathClientTest extends UsualClientTest { - @Override - protected IClient createClient(ClientFactory creator) throws IOException, InterruptedException { - Listener listener = TestSupport.buildListener(); - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newCacheClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - } -} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java deleted file mode 100644 index c4bcfca10da2e..0000000000000 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ClientsTest.java +++ /dev/null @@ -1,158 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.section.Listener; -import com.saaavsaaa.client.zookeeper.base.BaseClientTest; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.ZooDefs; -import org.apache.zookeeper.ZooKeeper; -import org.junit.Test; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -/** - * Created by aaa - */ -public class ClientsTest extends BaseClientTest { - private List clients; - private final int count = 5; - private final int shard = 2; - - @Override - public void start() throws IOException, InterruptedException { - clients = new ArrayList<>(count); - ClientFactory creator = new ClientFactory(); - for (int i = 0; i < count; i++) { - clients.add(createClient(creator)); - } - } - - @Override - protected IClient createClient(ClientFactory creator) throws IOException, InterruptedException { - IClient client; - if (clients.size() % shard == 1){ - System.out.println("create client"); - client = newClient(creator); - } else { - System.out.println("create watch client"); - client = newWatchClient(creator); - } - return client; - } - - private IClient newClient(ClientFactory creator) throws IOException, InterruptedException { - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); - } - - protected IClient newWatchClient(ClientFactory creator) throws IOException, InterruptedException { - Listener listener = TestSupport.buildListener(); - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - } - - @Override - public void stop() throws InterruptedException { - for (IClient client : clients) { - client.close(); - } - clients = null; - } - - @Test - public void createRoot() throws KeeperException, InterruptedException { - for (IClient client : clients) { - super.createRoot(client); - } - } - - @Test - public void createChild() throws KeeperException, InterruptedException { - for (IClient client : clients) { - super.createChild(client); - } - } - - @Test - public void deleteBranch() throws KeeperException, InterruptedException { - for (IClient client : clients) { - super.deleteBranch(client); - } - } - - @Test - public void isExisted() throws KeeperException, InterruptedException { - for (IClient client : clients) { - super.isExisted(client); - } - } - - @Test - public void get() throws KeeperException, InterruptedException { - for (IClient client : clients) { - super.get(client); - } - } - - @Test - public void asynGet() throws KeeperException, InterruptedException { - for (IClient client : clients) { - super.asynGet(client); - } - } - - @Test - public void getChildrenKeys() throws KeeperException, InterruptedException { - for (IClient client : clients) { - super.getChildrenKeys(client); - } - } - - @Test - public void persist() throws KeeperException, InterruptedException { - for (IClient client : clients) { - super.persist(client); - } - } - - @Test - public void persistEphemeral() throws KeeperException, InterruptedException { - for (IClient client : clients) { - super.persistEphemeral(client); - } - } - - @Test - public void delAllChildren() throws KeeperException, InterruptedException { - for (IClient client : clients) { - super.delAllChildren(client); - } - } - - @Test - public void watch() throws KeeperException, InterruptedException { - for (IClient client : clients) { - super.watch(client); - } - } - - @Test - public void close() throws Exception { - for (IClient client : clients) { - ZooKeeper zk = getZooKeeper(client); - client.close(); - assert zk.getState() == ZooKeeper.States.CLOSED; - } - } - - @Test - public void deleteRoot() throws KeeperException, InterruptedException { - for (IClient client : clients) { - createRootOnly(client); - deleteRoot(client); - assert getZooKeeper(client).exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; - } - } -} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java deleted file mode 100644 index 9bc0b550b439d..0000000000000 --- a/src/test/java/com/saaavsaaa/client/zookeeper/ContentionStrategyTest.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.zookeeper.section.Listener; -import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.section.StrategyType; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.ZooDefs; -import org.junit.Test; - -import java.io.IOException; -import java.util.List; - -/** - * Created by aaa - */ -public class ContentionStrategyTest extends UsualClientTest { - @Override - protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { - Listener listener = TestSupport.buildListener(); - IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - ((BaseClient)client).useExecStrategy(StrategyType.CONTEND); - return client; - } - - //todo test node contention case - - @Test - public void deleteBranch() throws KeeperException, InterruptedException { - String keyB = "a/b/bb"; - testClient.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); - assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; - String keyC = "a/c/cc"; - testClient.createAllNeedPath(keyC, "ccc11", CreateMode.PERSISTENT); - assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; - testClient.deleteCurrentBranch(keyC); - assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) == null; - assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), false) != null; - testClient.deleteCurrentBranch(keyB); // because Constants.CHANGING_KEY, root still exist - List children = zooKeeper.getChildren(PathUtil.checkPath(TestSupport.ROOT), false); - assert children.size() == 0; - deleteRoot(testClient); - assert zooKeeper.exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; - testClient.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); - assert zooKeeper.exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; - testClient.deleteCurrentBranch(keyB); - children = zooKeeper.getChildren(PathUtil.checkPath(TestSupport.ROOT), false); - assert children.size() == 0; - deleteRoot(testClient); - assert zooKeeper.exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; - } -} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java deleted file mode 100644 index 23f3aaefbb5fe..0000000000000 --- a/src/test/java/com/saaavsaaa/client/zookeeper/SyncRetryStrategyTest.java +++ /dev/null @@ -1,230 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.action.IProvider; -import com.saaavsaaa.client.retry.AsyncRetryCenter; -import com.saaavsaaa.client.retry.DelayRetryPolicy; -import com.saaavsaaa.client.retry.TestCallable; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.zookeeper.base.BaseClient; -import com.saaavsaaa.client.zookeeper.section.Callable; -import com.saaavsaaa.client.zookeeper.section.Listener; -import com.saaavsaaa.client.zookeeper.section.StrategyType; -import com.saaavsaaa.client.zookeeper.strategy.UsualStrategy; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.ZooDefs; -import org.apache.zookeeper.data.Stat; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -/** - * Created by aaa - */ -public class SyncRetryStrategyTest extends UsualClientTest{ - private IProvider provider; - - @Before - public void start() throws IOException, InterruptedException { - testClient = createClient(); - provider = ((BaseClient)testClient).getStrategy().getProvider(); - } - - protected IClient createClient() throws IOException, InterruptedException { - ClientFactory creator = new ClientFactory(); - Listener listener = TestSupport.buildListener(); - IClient client = creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - client.useExecStrategy(StrategyType.SYNC_RETRY); - return client; - } - - @Test - public void createChild() throws KeeperException, InterruptedException { - String key = "a/b/bb"; - new UsualStrategy(provider).deleteCurrentBranch(key); - TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { - @Override - public void test() throws KeeperException, InterruptedException { - testClient.useExecStrategy(StrategyType.USUAL); - testClient.createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); - testClient.useExecStrategy(StrategyType.SYNC_RETRY); - } - }; - callable.exec(); - - assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) != null; - new UsualStrategy(provider).deleteCurrentBranch(key); - assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) == null; - } - - @Test - public void deleteBranch() throws KeeperException, InterruptedException { - String keyB = "a/b/bb"; - String value = "bbb11"; - testClient.useExecStrategy(StrategyType.USUAL); - testClient.createAllNeedPath(keyB, value, CreateMode.PERSISTENT); - testClient.useExecStrategy(StrategyType.SYNC_RETRY); - - assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; - String keyC = "a/c/cc"; - new UsualStrategy(provider).createAllNeedPath(keyC, "ccc11", CreateMode.PERSISTENT); - assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; - - TestCallable callable = getDeleteBranch(keyC); - callable.exec(); - - assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) == null; - assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), false) != null; - - callable = getDeleteBranch(keyB); - callable.exec(); - - assert getZooKeeper(testClient).exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; - testClient.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); - assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; - - callable.exec(); - - assert getZooKeeper(testClient).exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; - } - - private TestCallable getDeleteBranch(final String key){ - TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { - @Override - public void test() throws KeeperException, InterruptedException { - testClient.useExecStrategy(StrategyType.USUAL); - testClient.deleteCurrentBranch(key); - testClient.useExecStrategy(StrategyType.SYNC_RETRY); - } - }; - return callable; - } - - @Test - public void isExisted() throws KeeperException, InterruptedException { - String key = "a/b/bb"; - testClient.useExecStrategy(StrategyType.USUAL); - testClient.createAllNeedPath(key, "", CreateMode.PERSISTENT); - testClient.useExecStrategy(StrategyType.SYNC_RETRY); - - TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { - @Override - public void test() throws KeeperException, InterruptedException { - setResult(provider.exists(provider.getRealPath(key))); - } - }; - System.out.println(callable.getResult()); - assert callable.getResult().equals(true); - - testClient.useExecStrategy(StrategyType.USUAL); - testClient.deleteCurrentBranch(key); - testClient.useExecStrategy(StrategyType.SYNC_RETRY); - } - - @Test - public void get() throws KeeperException, InterruptedException { - String key = "a/b"; - testClient.useExecStrategy(StrategyType.USUAL); - testClient.createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); - testClient.useExecStrategy(StrategyType.SYNC_RETRY); - - TestCallable callable = getData("a"); - assert callable.getResult().equals(""); - callable = getData(key); - assert callable.getResult().equals("bbb11"); - - testClient.useExecStrategy(StrategyType.USUAL); - testClient.deleteCurrentBranch(key); - testClient.useExecStrategy(StrategyType.SYNC_RETRY); - } - - private TestCallable getData(final String key){ - TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { - @Override - public void test() throws KeeperException, InterruptedException { - setResult(new String(provider.getData(provider.getRealPath(key)))); - } - }; - return callable; - } - - @Test - public void getChildrenKeys() throws KeeperException, InterruptedException { - String key = "a/b"; - String current = "a"; - - testClient.useExecStrategy(StrategyType.USUAL); - testClient.createAllNeedPath(key, "", CreateMode.PERSISTENT); - testClient.createAllNeedPath("a/c", "", CreateMode.PERSISTENT); - testClient.useExecStrategy(StrategyType.SYNC_RETRY); - - TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { - @Override - public void test() throws KeeperException, InterruptedException { - setResult(provider.getChildren(provider.getRealPath(current))); - } - }; - List result = (List) callable.getResult(); - Collections.sort(result, new Comparator() { - public int compare(final String o1, final String o2) { - return o2.compareTo(o1); - } - }); - assert result.get(0).equals("c"); - assert result.get(1).equals("b"); - - testClient.useExecStrategy(StrategyType.USUAL); - testClient.deleteCurrentBranch(key); - testClient.useExecStrategy(StrategyType.SYNC_RETRY); - } - - @Test - public void update() throws KeeperException, InterruptedException { - String key = "a"; - String value = "aa"; - String newValue = "aaa"; - testClient.deleteCurrentBranch(key); - testClient.createAllNeedPath(key, value, CreateMode.PERSISTENT); - String data = testClient.getDataString(key); - System.out.println(data); - assert data.equals(value); - - TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { - @Override - public void test() throws KeeperException, InterruptedException { - provider.update(provider.getRealPath(key), newValue); - } - }; - callable.exec(); - - assert testClient.getDataString(key).equals(newValue); - testClient.deleteCurrentBranch(key); - } - - @Test - public void delAllChildren() throws KeeperException, InterruptedException { - String key = "a/b/bb"; - testClient.createAllNeedPath(key, "bb", CreateMode.PERSISTENT); - key = "a/c/cc"; - testClient.createAllNeedPath(key, "cc", CreateMode.PERSISTENT); - System.out.println(getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), null).getNumChildren()); // nearest children count - assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) != null; - - TestCallable callable = new TestCallable(provider, DelayRetryPolicy.newNoInitDelayPolicy()) { - @Override - public void test() throws KeeperException, InterruptedException { - new UsualStrategy(provider).deleteAllChildren("a"); - } - }; - callable.exec(); - - assert getZooKeeper(testClient).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) == null; - assert getZooKeeper(testClient).exists("/" + TestSupport.ROOT, false) != null; - super.deleteRoot(testClient); - } -} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java b/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java deleted file mode 100644 index a146217ff99ea..0000000000000 --- a/src/test/java/com/saaavsaaa/client/zookeeper/TestSupport.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -import com.saaavsaaa.client.zookeeper.section.Listener; -import org.apache.zookeeper.WatchedEvent; - -/** - * Created by aaa - */ -public class TestSupport { - public static final String AUTH = "digest"; - public static final String SERVERS = "192.168.2.44:2181"; - public static final int SESSION_TIMEOUT = 200000;//ms - public static final String ROOT = "test"; - - public static Listener buildListener(){ - Listener listener = new Listener(null) { - @Override - public void process(WatchedEvent event) { - System.out.println("=========================================================="); - System.out.println(event.getPath()); - System.out.println(event.getState()); - System.out.println(event.getType()); - System.out.println("=========================================================="); - } - }; - return listener; - } -} diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java deleted file mode 100644 index 95ecccc323e02..0000000000000 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualClientTest.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.zookeeper.base.BaseClientTest; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.ZooDefs; -import org.junit.Test; - -import java.io.IOException; - -/** - * Created by aaa - */ -public class UsualClientTest extends BaseClientTest { - - @Override - protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).start(); - } - - @Test - public void createRoot() throws KeeperException, InterruptedException { - super.createRoot(testClient); - } - - @Test - public void createChild() throws KeeperException, InterruptedException { - super.createChild(testClient); - } - - @Test - public void deleteBranch() throws KeeperException, InterruptedException { - super.deleteBranch(testClient); - } - - @Test - public void isExisted() throws KeeperException, InterruptedException { - super.isExisted(testClient); - } - - @Test - public void get() throws KeeperException, InterruptedException { - super.get(testClient); - } - - @Test - public void asynGet() throws KeeperException, InterruptedException { - super.asynGet(testClient); - } - - @Test - public void getChildrenKeys() throws KeeperException, InterruptedException { - super.getChildrenKeys(testClient); - } - - @Test - public void persist() throws KeeperException, InterruptedException { - super.persist(testClient); - } - - @Test - public void persistEphemeral() throws KeeperException, InterruptedException { - super.persistEphemeral(testClient); - } - - @Test - public void delAllChildren() throws KeeperException, InterruptedException { - super.delAllChildren(testClient); - } - - @Test - public void watch() throws KeeperException, InterruptedException { - super.watch(testClient); - } - - @Test - public void close() throws Exception { - super.close(testClient); - } -} \ No newline at end of file diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java deleted file mode 100644 index 9d709ccb35f1c..0000000000000 --- a/src/test/java/com/saaavsaaa/client/zookeeper/UsualWatchClientTest.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.saaavsaaa.client.zookeeper; - -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.zookeeper.section.Listener; -import org.apache.zookeeper.ZooDefs; - -import java.io.IOException; - -/** - * Created by aaa - */ -public class UsualWatchClientTest extends UsualClientTest { - - @Override - protected IClient createClient(final ClientFactory creator) throws IOException, InterruptedException { - Listener listener = TestSupport.buildListener(); - return creator.setNamespace(TestSupport.ROOT).authorization(TestSupport.AUTH, TestSupport.AUTH.getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL).newClient(TestSupport.SERVERS, TestSupport.SESSION_TIMEOUT).watch(listener).start(); - } -} \ No newline at end of file diff --git a/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java b/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java deleted file mode 100644 index 89341f099c927..0000000000000 --- a/src/test/java/com/saaavsaaa/client/zookeeper/base/BaseClientTest.java +++ /dev/null @@ -1,283 +0,0 @@ -package com.saaavsaaa.client.zookeeper.base; - -import com.saaavsaaa.client.action.IClient; -import com.saaavsaaa.client.utility.PathUtil; -import com.saaavsaaa.client.utility.constant.Constants; -import com.saaavsaaa.client.zookeeper.section.Listener; -import com.saaavsaaa.client.zookeeper.ClientFactory; -import com.saaavsaaa.client.zookeeper.TestSupport; -import org.apache.zookeeper.*; -import org.apache.zookeeper.data.Stat; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.concurrent.CountDownLatch; - -/** - * Created by aaa - */ -public abstract class BaseClientTest { - protected IClient testClient = null; - protected ZooKeeper zooKeeper; - - @Before - public void start() throws IOException, InterruptedException { - ClientFactory creator = new ClientFactory(); - testClient = createClient(creator); - getZooKeeper(testClient); - } - - protected ZooKeeper getZooKeeper(IClient client){ - zooKeeper = ((BaseClient)client).holder.getZooKeeper(); - return zooKeeper; - } - - protected abstract IClient createClient(ClientFactory creator) throws IOException, InterruptedException; - - @After - public void stop() throws InterruptedException { - testClient.close(); - testClient = null; - } - - @Test - public void deleteRoot() throws KeeperException, InterruptedException { - ((BaseClient)testClient).createNamespace(); - deleteRoot(testClient); - assert getZooKeeper(testClient).exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; - } - - protected void createRootOnly(IClient client) throws KeeperException, InterruptedException { - ((BaseClient)client).createNamespace(); - } - - protected void deleteRoot(IClient client) throws KeeperException, InterruptedException { - ((BaseClient)client).deleteNamespace(); - } - - protected void createRoot(IClient client) throws KeeperException, InterruptedException { - ((BaseClient)client).createNamespace(); - assert getZooKeeper(client).exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) != null; - ((BaseClient)client).deleteNamespace(); - assert getZooKeeper(client).exists(Constants.PATH_SEPARATOR + TestSupport.ROOT, false) == null; - } - - protected void createChild(IClient client) throws KeeperException, InterruptedException { - String key = "a/b/bb"; - client.createAllNeedPath(key, "bbb11", CreateMode.PERSISTENT); - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) != null; - client.deleteCurrentBranch(key); - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key)/*"/" + ROOT + "/" + key*/, false) == null; - } - - protected void deleteBranch(IClient client) throws KeeperException, InterruptedException { - String keyB = "a/b/bb"; - client.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; - String keyC = "a/c/cc"; - client.createAllNeedPath(keyC, "ccc11", CreateMode.PERSISTENT); - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) != null; - client.deleteCurrentBranch(keyC); - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, keyC), false) == null; - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), false) != null; - client.deleteCurrentBranch(keyB); - assert getZooKeeper(client).exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; - client.createAllNeedPath(keyB, "bbb11", CreateMode.PERSISTENT); - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, keyB), false) != null; - client.deleteCurrentBranch(keyB); - assert getZooKeeper(client).exists(PathUtil.checkPath(TestSupport.ROOT), false) == null; - } - - protected void isExisted(IClient client) throws KeeperException, InterruptedException { - String key = "a/b/bb"; - client.createAllNeedPath(key, "", CreateMode.PERSISTENT); - assert isExisted(key, client); - client.deleteCurrentBranch(key); - } - - protected void get(IClient client) throws KeeperException, InterruptedException { - client.createAllNeedPath("a/b", "bbb11", CreateMode.PERSISTENT); - String key = "a"; - assert getDirectly(key, client).equals(""); - key = "a/b"; - assert getDirectly(key, client).equals("bbb11"); - client.deleteCurrentBranch("a/b"); - } - - protected void asynGet(IClient client) throws KeeperException, InterruptedException { - CountDownLatch ready = new CountDownLatch(1); - String key = "a/b"; - String value = "bbb11"; - client.createAllNeedPath(key, value, CreateMode.PERSISTENT); - AsyncCallback.DataCallback callback = new AsyncCallback.DataCallback() { - @Override - public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) { - String result = new String(data); - System.out.println(new StringBuffer().append("rc:").append(rc).append(",path:").append(path).append(",ctx:").append(ctx).append(",stat:").append(stat)); - assert result.equals(ctx); - ready.countDown(); - } - }; - client.getData(key, callback, value); - ready.await(); - client.deleteCurrentBranch("a/b"); - } - - private String getDirectly(String key, IClient client) throws KeeperException, InterruptedException { - return new String(client.getData(key)); - } - - private boolean isExisted(String key, IClient client) throws KeeperException, InterruptedException { - return client.checkExists(key); - } - - protected void getChildrenKeys(IClient client) throws KeeperException, InterruptedException { - String key = "a/b"; - String current = "a"; - client.createAllNeedPath(key, "", CreateMode.PERSISTENT); - List result = client.getChildren(current); - Collections.sort(result, new Comparator() { - public int compare(final String o1, final String o2) { - return o2.compareTo(o1); - } - }); - assert result.get(0).equals("b"); - client.deleteCurrentBranch(key); - } - - protected void persist(IClient client) throws KeeperException, InterruptedException { - String key = "a"; - String value = "aa"; - String newValue = "aaa"; - if (!isExisted(key, client)) { - client.createAllNeedPath(key, value, CreateMode.PERSISTENT); - } else { - updateWithCheck(key, value, client); - } - - assert getDirectly(key, client).equals(value); - - updateWithCheck(key, newValue, client); - assert getDirectly(key, client).equals(newValue); - client.deleteCurrentBranch(key); - } - - private void updateWithCheck(String key, String value, IClient client) throws KeeperException, InterruptedException { - client.update(key, value); -// client.transaction().check(key, Constants.VERSION).setData(key, value.getBytes(Constants.UTF_8), Constants.VERSION).commit(); - } - - protected void persistEphemeral(IClient client) throws KeeperException, InterruptedException { - String key = "a/b/bb"; - String value = "b1b"; - client.createAllNeedPath(key, value, CreateMode.PERSISTENT); -// assert getZooKeeper(client).exists(PathUtil.getRealPath(ROOT, key), null).getEphemeralOwner() == 0; - Stat stat = new Stat(); - getZooKeeper(client).getData(PathUtil.getRealPath(TestSupport.ROOT, key), false, stat); - assert stat.getEphemeralOwner() == 0; - - client.deleteAllChildren(key); - assert !isExisted(key, client); - client.createAllNeedPath(key, value, CreateMode.EPHEMERAL); - - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key), null).getEphemeralOwner() != 0; // Ephemeral node connection session id - client.deleteCurrentBranch(key); - } - - protected void delAllChildren(IClient client) throws KeeperException, InterruptedException { - String key = "a/b/bb"; - client.createAllNeedPath(key, "bb", CreateMode.PERSISTENT); - key = "a/c/cc"; - client.createAllNeedPath(key, "cc", CreateMode.PERSISTENT); - System.out.println(getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, "a"), null).getNumChildren()); // nearest children count - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) != null; - client.deleteAllChildren("a"); - assert getZooKeeper(client).exists(PathUtil.getRealPath(TestSupport.ROOT, key), false) == null; - assert getZooKeeper(client).exists("/" + TestSupport.ROOT, false) != null; - ((BaseClient)client).deleteNamespace(); - } - - protected void watch(IClient client) throws KeeperException, InterruptedException { - List expected = new ArrayList<>(); - expected.add("update_/test/a_value"); - expected.add("update_/test/a_value1"); - expected.add("update_/test/a_value2"); - expected.add("delete_/test/a_"); - List actual = new ArrayList<>(); - - Listener listener = buildListener(client, actual); - - String key = "a"; - client.registerWatch(key, listener); - client.createCurrentOnly(key, "aaa", CreateMode.EPHEMERAL); - client.checkExists(key, new Watcher() { - @Override - public void process(WatchedEvent event) { - listener.process(event); - } - }); - client.update(key, "value"); - System.out.println(new String(client.getData(key))); - assert client.getDataString(key).equals("value"); - client.update(key, "value1"); - assert client.getDataString(key).equals("value1"); - client.update(key, "value2"); - assert client.getDataString(key).equals("value2"); - Thread.sleep(100); - client.deleteCurrentBranch(key); - assert expected.size() == actual.size(); - assert expected.containsAll(actual); - client.unregisterWatch(listener.getKey()); - } - - protected Listener buildListener(IClient client, List actual){ - Listener listener = new Listener(null) { - @Override - public void process(WatchedEvent event) { - System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); - System.out.println(event.getPath()); - System.out.println(event.getType()); - - switch (event.getType()) { - case NodeDataChanged: - case NodeChildrenChanged: { - String result; - try { - result = new String(getZooKeeper(client).getData(event.getPath(),false, null)); - System.out.println(result); - } catch (KeeperException e) { - result = e.getMessage(); - e.printStackTrace(); - } catch (InterruptedException e) { - result = e.getMessage(); - e.printStackTrace(); - } - actual.add(new StringBuilder().append("update_").append(event.getPath()).append("_").append(result).toString()); - break; - } - case NodeDeleted: { - actual.add(new StringBuilder().append("delete_").append(event.getPath()).append("_").toString()); - break; - } - default: - actual.add(new StringBuilder().append("ignore_").append(event.getPath()).append("_").append(event.getType()).toString()); - break; - } - System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); - } - }; - return listener; - } - - protected void close(IClient client) throws Exception { - client.close(); - assert getZooKeeper(client).getState() == ZooKeeper.States.CLOSED; - } -} From 0e17811e6a603ec687d6b961e8965bdfda7b9322 Mon Sep 17 00:00:00 2001 From: linjiaqi Date: Fri, 8 Jun 2018 20:52:11 +0800 Subject: [PATCH 150/351] fix style errors and bugs --- .../core/yaml/proxy/YamlProxyConfiguration.java | 2 ++ .../proxy/backend/ShardingProxyClient.java | 12 ++++++------ .../proxy/backend/common/BackendHandlerFactory.java | 8 ++++---- .../backend/common/NettyChannelPoolHandler.java | 6 +++--- .../backend/common/SQLExecuteBackendHandler.java | 4 ++-- .../backend/common/SQLPacketsBackendHandler.java | 7 ++++--- .../common/StatementExecuteBackendHandler.java | 4 ++-- .../proxy/backend/mysql/MySQLBackendHandler.java | 8 ++++---- .../proxy/backend/mysql/MySQLQueryResult.java | 2 +- .../backend/netty/ClientHandlerInitializer.java | 6 +++--- ...{DataScourceConfig.java => DataSourceConfig.java} | 4 ++-- .../io/shardingsphere/proxy/config/RuleRegistry.java | 12 +++++++----- .../shardingsphere/proxy/frontend/ShardingProxy.java | 9 ++------- .../proxy/metadata/ProxyShardingMetaData.java | 2 -- .../proxy/metadata/ShardingMetaDataHandler.java | 2 +- .../transport/mysql/packet/MySQLPacketPayload.java | 4 ++-- .../command/text/fieldlist/ComFieldListPacket.java | 2 +- .../command/text/query/ColumnDefinition41Packet.java | 8 ++++---- .../packet/command/text/query/ComQueryPacket.java | 6 +++--- sharding-proxy/src/main/resources/logback.xml | 2 +- 20 files changed, 54 insertions(+), 56 deletions(-) rename sharding-proxy/src/main/java/io/shardingsphere/proxy/config/{DataScourceConfig.java => DataSourceConfig.java} (94%) diff --git a/sharding-core/src/main/java/io/shardingsphere/core/yaml/proxy/YamlProxyConfiguration.java b/sharding-core/src/main/java/io/shardingsphere/core/yaml/proxy/YamlProxyConfiguration.java index 5046c485fdaea..68ae519a076b8 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/yaml/proxy/YamlProxyConfiguration.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/yaml/proxy/YamlProxyConfiguration.java @@ -56,6 +56,8 @@ public class YamlProxyConfiguration { private YamlShardingRuleConfiguration shardingRule = new YamlShardingRuleConfiguration(); + private boolean withoutJdbc; + private String transactionMode; /** diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java index c76109d830741..67698d066921f 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java @@ -42,7 +42,7 @@ import io.netty.channel.socket.nio.NioSocketChannel; import io.shardingsphere.core.yaml.sharding.DataSourceParameter; import io.shardingsphere.proxy.backend.common.NettyChannelPoolHandler; -import io.shardingsphere.proxy.config.DataScourceConfig; +import io.shardingsphere.proxy.config.DataSourceConfig; import io.shardingsphere.proxy.config.RuleRegistry; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @@ -68,7 +68,7 @@ public final class ShardingProxyClient { @Getter private ChannelPoolMap poolMap; - private Map dataScourceConfigMap = Maps.newHashMap(); + private Map dataSourceConfigMap = Maps.newHashMap(); /** * Start Sharding-Proxy. @@ -85,7 +85,7 @@ public void start() throws MalformedURLException, InterruptedException { final String database = url.getPath().substring(1); final String username = (each.getValue()).getUsername(); final String password = (each.getValue()).getPassword(); - dataScourceConfigMap.put(each.getKey(), new DataScourceConfig(ip, port, database, username, password)); + dataSourceConfigMap.put(each.getKey(), new DataSourceConfig(ip, port, database, username, password)); } final Bootstrap bootstrap = new Bootstrap(); if (workerGroup instanceof EpollEventLoopGroup) { @@ -130,13 +130,13 @@ private void initPoolMap(final Bootstrap bootstrap) throws InterruptedException poolMap = new AbstractChannelPoolMap() { @Override protected SimpleChannelPool newPool(final String datasourceName) { - DataScourceConfig dataScourceConfig = dataScourceConfigMap.get(datasourceName); + DataSourceConfig dataSourceConfig = dataSourceConfigMap.get(datasourceName); //TODO maxConnection should be set. - return new FixedChannelPool(bootstrap.remoteAddress(dataScourceConfig.getIp(), dataScourceConfig.getPort()), new NettyChannelPoolHandler(dataScourceConfig), MAX_CONNECTIONS); + return new FixedChannelPool(bootstrap.remoteAddress(dataSourceConfig.getIp(), dataSourceConfig.getPort()), new NettyChannelPoolHandler(dataSourceConfig), MAX_CONNECTIONS); } }; - for (String each : dataScourceConfigMap.keySet()) { + for (String each : dataSourceConfigMap.keySet()) { SimpleChannelPool pool = poolMap.get(each); Channel[] channels = new Channel[MAX_CONNECTIONS]; for (int i = 0; i < MAX_CONNECTIONS; i++) { diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java index b38c85290c839..b3283bb7b8ae4 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/BackendHandlerFactory.java @@ -19,7 +19,7 @@ import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.proxy.backend.mysql.MySQLBackendHandler; -import io.shardingsphere.proxy.config.DataScourceConfig; +import io.shardingsphere.proxy.config.DataSourceConfig; /** * Backend handler factory. @@ -32,13 +32,13 @@ public final class BackendHandlerFactory { * Create bakcend handler instance. * * @param databaseType database type - * @param dataScourceConfig dataScourceConfig + * @param dataSourceConfig dataSourceConfig * @return backend handler instance */ - public static CommandResponsePacketsHandler createBackendHandlerInstance(final DatabaseType databaseType, final DataScourceConfig dataScourceConfig) { + public static CommandResponsePacketsHandler createBackendHandlerInstance(final DatabaseType databaseType, final DataSourceConfig dataSourceConfig) { switch (databaseType) { case MySQL: - return new MySQLBackendHandler(dataScourceConfig); + return new MySQLBackendHandler(dataSourceConfig); default: throw new UnsupportedOperationException(String.format("Cannot support database type '%s'", databaseType)); } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/NettyChannelPoolHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/NettyChannelPoolHandler.java index b427d28e98109..dc3b60bf640c0 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/NettyChannelPoolHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/NettyChannelPoolHandler.java @@ -20,7 +20,7 @@ import io.netty.channel.Channel; import io.netty.channel.pool.ChannelPoolHandler; import io.shardingsphere.proxy.backend.netty.ClientHandlerInitializer; -import io.shardingsphere.proxy.config.DataScourceConfig; +import io.shardingsphere.proxy.config.DataSourceConfig; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -33,7 +33,7 @@ @RequiredArgsConstructor @Slf4j public class NettyChannelPoolHandler implements ChannelPoolHandler { - private final DataScourceConfig dataScourceConfig; + private final DataSourceConfig dataSourceConfig; @Override public void channelReleased(final Channel channel) throws Exception { @@ -48,6 +48,6 @@ public void channelAcquired(final Channel channel) throws Exception { @Override public void channelCreated(final Channel channel) throws Exception { log.info("channelCreated. Channel ID: {}" + channel.id().asShortText()); - channel.pipeline().addLast(new ClientHandlerInitializer(dataScourceConfig)); + channel.pipeline().addLast(new ClientHandlerInitializer(dataSourceConfig)); } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java index ae105015b9b65..1000791008bb4 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java @@ -66,9 +66,9 @@ @Getter @Setter public final class SQLExecuteBackendHandler implements BackendHandler { - + private final String sql; - + private List connections; private List resultSets; diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java index a563fd250e380..a13b1731537de 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java @@ -101,7 +101,7 @@ public CommandResponsePackets execute() { return executeForSharding(); } } - + protected CommandResponsePackets executeForMasterSlave() { MasterSlaveRouter masterSlaveRouter = new MasterSlaveRouter(RuleRegistry.getInstance().getMasterSlaveRule()); SQLStatement sqlStatement = new SQLJudgeEngine(commandPacket.getSql()).judge(); @@ -159,7 +159,7 @@ protected CommandResponsePackets executeForSharding() { ProxyShardingRefreshHandler.build(routeResult).execute(); return result; } - + protected CommandResponsePackets merge(final SQLStatement sqlStatement, final List packets, final List queryResults) { CommandResponsePackets headPackets = new CommandResponsePackets(); for (CommandResponsePackets each : packets) { @@ -184,6 +184,7 @@ private void executeCommand(final String dataSourceName, final int connectionId, Channel channel = null; try { pool = ShardingProxyClient.getInstance().getPoolMap().get(dataSourceName); + //TODO timeout should be set. channel = pool.acquire().get(CONNECT_TIMEOUT, TimeUnit.SECONDS); MySQLResultCache.getInstance().putConnection(channel.id().asShortText(), connectionId); channel.writeAndFlush(commandPacket); @@ -195,7 +196,7 @@ private void executeCommand(final String dataSourceName, final int connectionId, } } } - + private CommandResponsePackets mergeDML(final CommandResponsePackets firstPackets) { int affectedRows = 0; long lastInsertId = 0; diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java index 9d9e34ea01a25..63dc4bd718b0a 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java @@ -156,7 +156,7 @@ private CommandResponsePackets executeForSharding() { ProxyShardingRefreshHandler.build(routeResult).execute(); return result; } - + private void getCommandResponsePackets(final List> resultList, final List packets) { for (Future each : resultList) { try { @@ -169,7 +169,7 @@ private void getCommandResponsePackets(final List } } } - + List getComStmtExecuteParameters() { List result = new ArrayList<>(32); for (PreparedStatementParameter each : preparedStatementParameters) { diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java index 12509cc99e2bd..36915b5770cb3 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java @@ -28,7 +28,7 @@ import io.netty.channel.ChannelHandlerContext; import io.shardingsphere.proxy.backend.common.CommandResponsePacketsHandler; import io.shardingsphere.proxy.backend.constant.AuthType; -import io.shardingsphere.proxy.config.DataScourceConfig; +import io.shardingsphere.proxy.config.DataSourceConfig; import io.shardingsphere.proxy.transport.mysql.constant.CapabilityFlag; import io.shardingsphere.proxy.transport.mysql.constant.ServerInfo; import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; @@ -51,7 +51,7 @@ @Slf4j @RequiredArgsConstructor public class MySQLBackendHandler extends CommandResponsePacketsHandler { - private final DataScourceConfig dataScourceConfig; + private final DataSourceConfig dataSourceConfig; private AuthType authType = AuthType.UN_AUTH; @@ -97,10 +97,10 @@ protected void auth(final ChannelHandlerContext context, final int sequenceId, f mysqlPacketPayload.skipReserved(10); byte[] authPluginDataPart2 = mysqlPacketPayload.readStringNul().getBytes(); byte[] authPluginData = Bytes.concat(authPluginDataPart1, authPluginDataPart2); - byte[] authResponse = securePasswordAuthentication(dataScourceConfig.getPassword().getBytes(), authPluginData); + byte[] authResponse = securePasswordAuthentication(dataSourceConfig.getPassword().getBytes(), authPluginData); //TODO maxSizePactet(16MB) should be set. HandshakeResponse41Packet handshakeResponse41Packet = new HandshakeResponse41Packet(sequenceId + 1, CapabilityFlag.calculateHandshakeCapabilityFlagsLower(), 16777215, ServerInfo.CHARSET, - dataScourceConfig.getUsername(), authResponse, dataScourceConfig.getDatabase()); + dataSourceConfig.getUsername(), authResponse, dataSourceConfig.getDatabase()); context.writeAndFlush(handshakeResponse41Packet); } finally { authType = AuthType.AUTHING; diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java index 1c90e95682d11..3b73def7823e8 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java @@ -54,7 +54,7 @@ public final class MySQLQueryResult implements QueryResult { private final Map columnIndexAndLabelMap; private final Map columnLabelAndIndexMap; - + private final List columnDefinitions; private final BlockingQueue resultSet; diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/netty/ClientHandlerInitializer.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/netty/ClientHandlerInitializer.java index 2a97052fa7731..9c2c8cd65e75e 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/netty/ClientHandlerInitializer.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/netty/ClientHandlerInitializer.java @@ -22,7 +22,7 @@ import io.netty.channel.ChannelPipeline; import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.proxy.backend.common.BackendHandlerFactory; -import io.shardingsphere.proxy.config.DataScourceConfig; +import io.shardingsphere.proxy.config.DataSourceConfig; import io.shardingsphere.proxy.transport.common.codec.PacketCodecFactory; import lombok.RequiredArgsConstructor; @@ -34,13 +34,13 @@ */ @RequiredArgsConstructor public final class ClientHandlerInitializer extends ChannelInitializer { - private final DataScourceConfig dataScourceConfig; + private final DataSourceConfig dataSourceConfig; @Override protected void initChannel(final Channel channel) { ChannelPipeline pipeline = channel.pipeline(); // TODO load database type from yaml or startup arguments pipeline.addLast(PacketCodecFactory.createPacketCodecInstance(DatabaseType.MySQL)); - pipeline.addLast(BackendHandlerFactory.createBackendHandlerInstance(DatabaseType.MySQL, dataScourceConfig)); + pipeline.addLast(BackendHandlerFactory.createBackendHandlerInstance(DatabaseType.MySQL, dataSourceConfig)); } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/DataScourceConfig.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/DataSourceConfig.java similarity index 94% rename from sharding-proxy/src/main/java/io/shardingsphere/proxy/config/DataScourceConfig.java rename to sharding-proxy/src/main/java/io/shardingsphere/proxy/config/DataSourceConfig.java index 4cf0a4e719b1b..86f4ddfe6b20d 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/DataScourceConfig.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/DataSourceConfig.java @@ -21,13 +21,13 @@ import lombok.RequiredArgsConstructor; /** - * DataScourceConfig. + * DataSourceConfig. * * @author linjiaqi */ @RequiredArgsConstructor @Getter -public class DataScourceConfig { +public class DataSourceConfig { private final String ip; private final int port; diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java index f7d68a3f258ba..4f15e4e459a4a 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java @@ -46,20 +46,21 @@ * @author zhangliang * @author zhangyonglun * @author panjuan + * @author zhaojun * @author wangkai */ @Getter public final class RuleRegistry { - public static final boolean WITHOUT_JDBC = true; - private static final int MAX_EXECUTOR_THREADS = Runtime.getRuntime().availableProcessors() * 2; private static final RuleRegistry INSTANCE = new RuleRegistry(); + private final boolean withoutJdbc; + private final Map dataSourceMap; private Map dataSourceConfigurationMap; - + private final ShardingRule shardingRule; private final MasterSlaveRule masterSlaveRule; @@ -75,7 +76,7 @@ public final class RuleRegistry { private final boolean showSQL; private final TransactionType transactionType; - + private RuleRegistry() { YamlProxyConfiguration yamlProxyConfiguration; try { @@ -83,11 +84,12 @@ private RuleRegistry() { } catch (final IOException ex) { throw new ShardingException(ex); } + withoutJdbc = yamlProxyConfiguration.isWithoutJdbc(); transactionType = TransactionType.findByValue(yamlProxyConfiguration.getTransactionMode()); dataSourceMap = ProxyRawDataSourceFactory.create(transactionType, yamlProxyConfiguration); dataSourceConfigurationMap = new HashMap<>(128, 1); for (Map.Entry entry : yamlProxyConfiguration.getDataSources().entrySet()) { - if (WITHOUT_JDBC) { + if (withoutJdbc) { dataSourceConfigurationMap.put(entry.getKey(), entry.getValue()); } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java index 25eee0fc3fd99..11117338fbcc1 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java @@ -34,7 +34,6 @@ import io.netty.handler.logging.LoggingHandler; import io.shardingsphere.proxy.backend.ShardingProxyClient; import io.shardingsphere.proxy.config.RuleRegistry; -import io.shardingsphere.proxy.frontend.netty.ServerHandlerInitializer; import java.net.MalformedURLException; @@ -64,7 +63,7 @@ public final class ShardingProxy { */ public void start(final int port) throws InterruptedException, MalformedURLException { try { - if (RuleRegistry.WITHOUT_JDBC) { + if (RuleRegistry.getInstance().isWithoutJdbc()) { ShardingProxyClient.getInstance().start(); } ServerBootstrap bootstrap = new ServerBootstrap(); @@ -80,7 +79,7 @@ public void start(final int port) throws InterruptedException, MalformedURLExcep workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); userGroup.shutdownGracefully(); - if (RuleRegistry.WITHOUT_JDBC) { + if (RuleRegistry.getInstance().isWithoutJdbc()) { ShardingProxyClient.getInstance().stop(); } } @@ -99,8 +98,6 @@ private void groupsEpoll(final ServerBootstrap bootstrap) { userGroup = new EpollEventLoopGroup(WORKER_MAX_THREADS); bootstrap.group(bossGroup, workerGroup) .channel(EpollServerSocketChannel.class) - .option(EpollChannelOption.TCP_CORK, true) - .option(EpollChannelOption.SO_KEEPALIVE, true) .option(EpollChannelOption.SO_BACKLOG, 128) .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) @@ -113,8 +110,6 @@ private void groupsNio(final ServerBootstrap bootstrap) { userGroup = new NioEventLoopGroup(WORKER_MAX_THREADS); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) - .option(ChannelOption.SO_KEEPALIVE, true) - .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.SO_BACKLOG, 128) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100) .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024 * 1024, 16 * 1024 * 1024)) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/metadata/ProxyShardingMetaData.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/metadata/ProxyShardingMetaData.java index 5468301b7a337..0e88ed1065e7a 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/metadata/ProxyShardingMetaData.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/metadata/ProxyShardingMetaData.java @@ -56,5 +56,3 @@ public Collection getTableNamesFromDefaultDataSource(final String defaul return new ShardingMetaDataHandler(dataSourceMap.get(defaultDataSourceName), "").getTableNamesFromDefaultDataSource(); } } - - diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/metadata/ShardingMetaDataHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/metadata/ShardingMetaDataHandler.java index 9ccfc27272fdc..a660af6bbb164 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/metadata/ShardingMetaDataHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/metadata/ShardingMetaDataHandler.java @@ -67,7 +67,7 @@ private boolean isTableExist(final Statement statement) throws SQLException { return resultSet.next(); } } - + private List getExistColumnMeta(final Statement statement) throws SQLException { List result = new LinkedList<>(); statement.executeQuery(String.format("desc %s;", getActualTableName())); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java index 34160d94009da..8df23553bd070 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java @@ -50,7 +50,7 @@ public final class MySQLPacketPayload { public boolean isReadable() { return byteBuf.isReadable(); } - + /** * Read 1 byte fixed length integer from byte buffers. * @see FixedLengthInteger @@ -280,7 +280,7 @@ public void writeStringFix(final String value) { public void writeBytes(final byte[] value) { byteBuf.writeBytes(value); } - + /** * Read variable length string from byte buffers. * @see FixedLengthString diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java index 8879d625a1371..c78c0b7afefec 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java @@ -62,7 +62,7 @@ public CommandResponsePackets execute() { log.debug("table name received for Sharding-Proxy: {}", table); log.debug("field wildcard received for Sharding-Proxy: {}", fieldWildcard); // TODO use common database type - if (RuleRegistry.WITHOUT_JDBC) { + if (RuleRegistry.getInstance().isWithoutJdbc()) { return new SQLPacketsBackendHandler(this, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()).execute(); } else { return new SQLExecuteBackendHandler(getSql(), DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()).execute(); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ColumnDefinition41Packet.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ColumnDefinition41Packet.java index 7ef21c19c64e8..de5d40b08cb16 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ColumnDefinition41Packet.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ColumnDefinition41Packet.java @@ -37,9 +37,9 @@ public final class ColumnDefinition41Packet extends MySQLPacket { private final int nextLength = 0x0c; - private int characterSet = ServerInfo.CHARSET; + private final int characterSet; - private int flags; + private final int flags; private final String schema; @@ -60,6 +60,8 @@ public final class ColumnDefinition41Packet extends MySQLPacket { public ColumnDefinition41Packet(final int sequenceId, final String schema, final String table, final String orgTable, final String name, final String orgName, final int columnLength, final ColumnType columnType, final int decimals) { super(sequenceId); + this.characterSet = ServerInfo.CHARSET; + this.flags = 0; this.schema = schema; this.table = table; this.orgTable = orgTable; @@ -79,11 +81,9 @@ public ColumnDefinition41Packet(final int sequenceId, final MySQLPacketPayload m name = mysqlPacketPayload.readStringLenenc(); orgName = mysqlPacketPayload.readStringLenenc(); Preconditions.checkArgument(nextLength == mysqlPacketPayload.readIntLenenc()); - //TODO the characterSet maybe not always equal to ServerInfo.CHARSET characterSet = mysqlPacketPayload.readInt2(); columnLength = mysqlPacketPayload.readInt4(); columnType = ColumnType.valueOf(mysqlPacketPayload.readInt1()); - //TODO the flags maybe not always equal to 0 flags = mysqlPacketPayload.readInt2(); decimals = mysqlPacketPayload.readInt1(); mysqlPacketPayload.skipReserved(2); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java index e8ac6f9e54561..3a4c3f42ea890 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java @@ -70,7 +70,7 @@ public void write(final MySQLPacketPayload mysqlPacketPayload) { @Override public CommandResponsePackets execute() { log.debug("COM_QUERY received for Sharding-Proxy: {}", getSql()); - if (RuleRegistry.WITHOUT_JDBC) { + if (RuleRegistry.getInstance().isWithoutJdbc()) { return sqlPacketsBackendHandler.execute(); } else { return sqlExecuteBackendHandler.execute(); @@ -84,7 +84,7 @@ public CommandResponsePackets execute() { */ public boolean hasMoreResultValue() { try { - if (RuleRegistry.WITHOUT_JDBC) { + if (RuleRegistry.getInstance().isWithoutJdbc()) { return sqlPacketsBackendHandler.hasMoreResultValue(); } else { return sqlExecuteBackendHandler.hasMoreResultValue(); @@ -100,7 +100,7 @@ public boolean hasMoreResultValue() { * @return database protocol packet */ public DatabaseProtocolPacket getResultValue() { - if (RuleRegistry.WITHOUT_JDBC) { + if (RuleRegistry.getInstance().isWithoutJdbc()) { return sqlPacketsBackendHandler.getResultValue(); } else { return sqlExecuteBackendHandler.getResultValue(); diff --git a/sharding-proxy/src/main/resources/logback.xml b/sharding-proxy/src/main/resources/logback.xml index e98f8bd7fcbc3..409fd9272c5d7 100644 --- a/sharding-proxy/src/main/resources/logback.xml +++ b/sharding-proxy/src/main/resources/logback.xml @@ -5,7 +5,7 @@ [%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n - + From 80219ba31b0215f23a64b19958d8a1b32004fb9b Mon Sep 17 00:00:00 2001 From: linjiaqi Date: Mon, 11 Jun 2018 10:50:44 +0800 Subject: [PATCH 151/351] Merge remote-tracking branch 'remotes/upstream/dev' into dev # Conflicts: # sharding-core/src/main/java/io/shardingsphere/core/yaml/proxy/YamlProxyConfiguration.java # sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java # sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java # sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java --- .../core/yaml/proxy/YamlProxyConfiguration.java | 6 +++--- .../proxy/backend/ShardingProxyClient.java | 2 +- .../io/shardingsphere/proxy/config/RuleRegistry.java | 10 +++++----- .../proxy/frontend/mysql/MySQLFrontendHandler.java | 2 +- .../transport/mysql/packet/MySQLPacketPayload.java | 6 +++--- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/sharding-core/src/main/java/io/shardingsphere/core/yaml/proxy/YamlProxyConfiguration.java b/sharding-core/src/main/java/io/shardingsphere/core/yaml/proxy/YamlProxyConfiguration.java index 035aadfcd3f6a..5bbc5b3169e64 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/yaml/proxy/YamlProxyConfiguration.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/yaml/proxy/YamlProxyConfiguration.java @@ -53,13 +53,13 @@ public final class YamlProxyConfiguration { private Map dataSources = new HashMap<>(); private YamlMasterSlaveRuleConfiguration masterSlaveRule = new YamlMasterSlaveRuleConfiguration(); - + private YamlShardingRuleConfiguration shardingRule = new YamlShardingRuleConfiguration(); private ProxyAuthority proxyAuthority = new ProxyAuthority(); - + private boolean withoutJdbc; - + private String transactionMode; /** diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java index 67698d066921f..7a0fd938a2df3 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/ShardingProxyClient.java @@ -40,7 +40,7 @@ import io.netty.channel.pool.FixedChannelPool; import io.netty.channel.pool.SimpleChannelPool; import io.netty.channel.socket.nio.NioSocketChannel; -import io.shardingsphere.core.yaml.sharding.DataSourceParameter; +import io.shardingsphere.core.rule.DataSourceParameter; import io.shardingsphere.proxy.backend.common.NettyChannelPoolHandler; import io.shardingsphere.proxy.config.DataSourceConfig; import io.shardingsphere.proxy.config.RuleRegistry; diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java index 8468e72f5216c..e9a82767429e7 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java @@ -57,13 +57,13 @@ public final class RuleRegistry { private static final int MAX_EXECUTOR_THREADS = Runtime.getRuntime().availableProcessors() * 2; private static final RuleRegistry INSTANCE = new RuleRegistry(); - + private final boolean withoutJdbc; - + private final Map dataSourceMap; - + private Map dataSourceConfigurationMap; - + private final ShardingRule shardingRule; private final MasterSlaveRule masterSlaveRule; @@ -79,7 +79,7 @@ public final class RuleRegistry { private final boolean showSQL; private final TransactionType transactionType; - + private final ProxyAuthority proxyAuthority; private RuleRegistry() { diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java index 61960c071bb40..ded4834a57bb3 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java @@ -46,7 +46,7 @@ public final class MySQLFrontendHandler extends FrontendHandler { private final EventLoopGroup eventLoopGroup; private final ProxyAuthorityHandler proxyAuthorityHandler; - + public MySQLFrontendHandler(final EventLoopGroup eventLoopGroup) { this.eventLoopGroup = eventLoopGroup; proxyAuthorityHandler = new ProxyAuthorityHandler(); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java index dd1710147275d..559905d344892 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java @@ -50,7 +50,7 @@ public final class MySQLPacketPayload { public boolean isReadable() { return byteBuf.isReadable(); } - + /** * Read 1 byte fixed length integer from byte buffers. * @see FixedLengthInteger @@ -243,7 +243,7 @@ public byte[] readStringLenencByBytes() { byteBuf.readBytes(result); return result; } - + /** * Write fixed length string to byte buffers. * @see FixedLengthString @@ -292,7 +292,7 @@ public void writeStringFix(final String value) { public void writeBytes(final byte[] value) { byteBuf.writeBytes(value); } - + /** * Read variable length string from byte buffers. * @see FixedLengthString From 3fa0364877265ce89a380eb9940f9e98436d9907 Mon Sep 17 00:00:00 2001 From: linjiaqi Date: Mon, 11 Jun 2018 11:50:07 +0800 Subject: [PATCH 152/351] Merge remote-tracking branch 'remotes/upstream/dev' into dev # Conflicts: # sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java # sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java --- .../mysql/packet/MySQLPacketPayload.java | 585 +----------------- .../command/text/query/ComQueryPacket.java | 8 +- 2 files changed, 15 insertions(+), 578 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java index fe6919d83bc1b..36e6ec024d286 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacketPayload.java @@ -1,576 +1,3 @@ -<<<<<<< HEAD -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.proxy.transport.mysql.packet; - -import com.google.common.base.Strings; -import io.netty.buffer.ByteBuf; -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -import java.sql.Timestamp; -import java.util.Calendar; -import java.util.Date; - -/** - * Payload operation for MySQL packet data types. - * - * @see describing packets - * @see binary protocol value - * - * @author zhangliang - * @author zhangyonglun - * @author wangkai - */ -@RequiredArgsConstructor -@Getter -public final class MySQLPacketPayload { - - private final ByteBuf byteBuf; - - /** - * Returns true if and only if (this.writerIndex - this.readerIndex) is greater than 0. - * - * @return the byteBuf is readable - */ - public boolean isReadable() { - return byteBuf.isReadable(); - } - - /** - * Read 1 byte fixed length integer from byte buffers. - * @see FixedLengthInteger - * - * @return 1 byte fixed length integer - */ - public int readInt1() { - return byteBuf.readByte() & 0xff; - } - - /** - * Write 1 byte fixed length integer to byte buffers. - * @see FixedLengthInteger - * - * @param value 1 byte fixed length integer - */ - public void writeInt1(final int value) { - byteBuf.writeByte(value); - } - - /** - * Read 2 byte fixed length integer from byte buffers. - * @see FixedLengthInteger - * - * @return 2 byte fixed length integer - */ - public int readInt2() { - return byteBuf.readShortLE() & 0xffff; - } - - /** - * Write 2 byte fixed length integer to byte buffers. - * @see FixedLengthInteger - * - * @param value 2 byte fixed length integer - */ - public void writeInt2(final int value) { - byteBuf.writeShortLE(value); - } - - /** - * Read 3 byte fixed length integer from byte buffers. - * @see FixedLengthInteger - * - * @return 3 byte fixed length integer - */ - public int readInt3() { - return byteBuf.readMediumLE() & 0xffffff; - } - - /** - * Write 3 byte fixed length integer to byte buffers. - * @see FixedLengthInteger - * - * @param value 3 byte fixed length integer - */ - public void writeInt3(final int value) { - byteBuf.writeMediumLE(value); - } - - /** - * Read 4 byte fixed length integer from byte buffers. - * @see FixedLengthInteger - * - * @return 4 byte fixed length integer - */ - public int readInt4() { - return byteBuf.readIntLE(); - } - - /** - * Write 4 byte fixed length integer to byte buffers. - * @see FixedLengthInteger - * - * @param value 4 byte fixed length integer - */ - public void writeInt4(final int value) { - byteBuf.writeIntLE(value); - - } - - /** - * Read 6 byte fixed length integer from byte buffers. - * @see FixedLengthInteger - * - * @return 6 byte fixed length integer - */ - public int readInt6() { - // TODO - return 0; - } - - /** - * Write 6 byte fixed length integer to byte buffers. - * @see FixedLengthInteger - * - * @param value 6 byte fixed length integer - */ - public void writeInt6(final int value) { - // TODO - } - - /** - * Read 8 byte fixed length integer from byte buffers. - * @see FixedLengthInteger - * - * @return 8 byte fixed length integer - */ - public long readInt8() { - return byteBuf.readLongLE(); - } - - /** - * Write 8 byte fixed length integer to byte buffers. - * @see FixedLengthInteger - * - * @param value 8 byte fixed length integer - */ - public void writeInt8(final long value) { - byteBuf.writeLongLE(value); - - } - - /** - * Read length encoded integer from byte buffers. - * @see LengthEncodedInteger - * - * @return length encoded integer - */ - public long readIntLenenc() { - int firstByte = readInt1(); - if (firstByte <= 0xfb) { - return firstByte; - } - if (0xfc == firstByte) { - return byteBuf.readShortLE(); - } - if (0xfd == firstByte) { - return byteBuf.readMediumLE(); - } - return byteBuf.readLongLE(); - } - - /** - * Write length encoded integer to byte buffers. - * @see LengthEncodedInteger - * - * @param value length encoded integer - */ - public void writeIntLenenc(final long value) { - if (value < 251) { - byteBuf.writeByte((int) value); - return; - } - if (value < Math.pow(2, 16)) { - byteBuf.writeByte(0xfc); - byteBuf.writeShortLE((int) value); - return; - } - if (value < Math.pow(2, 24)) { - byteBuf.writeByte(0xfd); - byteBuf.writeInt((int) value); - return; - } - byteBuf.writeByte(0xfe); - byteBuf.writeLongLE(value); - } - - /** - * Read fixed length string from byte buffers. - * @see FixedLengthString - * - * @return fixed length string - */ - public String readStringLenenc() { - int length = (int) readIntLenenc(); - byte[] result = new byte[length]; - byteBuf.readBytes(result); - return new String(result); - } - - /** - * Read fixed length string from byte buffers. - * - * @return fixed length bytes - */ - public byte[] readStringLenencByBytes() { - int length = (int) readIntLenenc(); - byte[] result = new byte[length]; - byteBuf.readBytes(result); - return result; - } - - /** - * Write fixed length string to byte buffers. - * @see FixedLengthString - * - * @param value fixed length string - */ - public void writeStringLenenc(final String value) { - if (Strings.isNullOrEmpty(value)) { - byteBuf.writeByte(0); - return; - } - writeIntLenenc(value.getBytes().length); - byteBuf.writeBytes(value.getBytes()); - } - - /** - * Read fixed length string from byte buffers. - * @see FixedLengthString - * - * @param length length of fixed string - * - * @return fixed length string - */ - public String readStringFix(final int length) { - byte[] result = new byte[length]; - byteBuf.readBytes(result); - return new String(result); - } - - /** - * Write variable length string to byte buffers. - * @see FixedLengthString - * - * @param value fixed length string - */ - public void writeStringFix(final String value) { - byteBuf.writeBytes(value.getBytes()); - } - - /** - * Write variable length bytes to byte buffers. - * @see Native41 - * - * @param value fixed length bytes - */ - public void writeBytes(final byte[] value) { - byteBuf.writeBytes(value); - } - - /** - * Read variable length string from byte buffers. - * @see FixedLengthString - * - * @return variable length string - */ - public String readStringVar() { - // TODO - return ""; - } - - /** - * Write fixed length string to byte buffers. - * @see FixedLengthString - * - * @param value variable length string - */ - public void writeStringVar(final String value) { - // TODO - } - - /** - * Read null terminated string from byte buffers. - * @see NulTerminatedString - * - * @return null terminated string - */ - public String readStringNul() { - byte[] result = new byte[byteBuf.bytesBefore((byte) 0)]; - byteBuf.readBytes(result); - byteBuf.skipBytes(1); - return new String(result); - } - - /** - * Write null terminated string to byte buffers. - * @see NulTerminatedString - * - * @param value null terminated string - */ - public void writeStringNul(final String value) { - byteBuf.writeBytes(value.getBytes()); - byteBuf.writeByte(0); - } - - /** - * Read rest of packet string from byte buffers. - * @see RestOfPacketString - * - * @return rest of packet string - */ - public String readStringEOF() { - byte[] result = new byte[byteBuf.readableBytes()]; - byteBuf.readBytes(result); - return new String(result); - } - - /** - * Write rest of packet string to byte buffers. - * @see RestOfPacketString - * - * @param value rest of packet string - */ - public void writeStringEOF(final String value) { - byteBuf.writeBytes(value.getBytes()); - } - - /** - * Skip reserved from byte buffers. - * - * @param length length of reserved - */ - public void skipReserved(final int length) { - byteBuf.skipBytes(length); - } - - /** - * Write null for reserved to byte buffers. - * - * @param length length of reserved - */ - public void writeReserved(final int length) { - for (int i = 0; i < length; i++) { - byteBuf.writeByte(0); - } - } - - /** - * Read 4 byte float from byte buffers. - * @see MYSQL_TYPE_FLOAT - * - * @return 4 byte float - */ - public float readFloat() { - return byteBuf.readFloatLE(); - } - - /** - * Write 4 byte float to byte buffers. - * @see MYSQL_TYPE_FLOAT - * - * @param value 4 byte float - */ - public void writeFloat(final float value) { - byteBuf.writeFloatLE(value); - } - - /** - * Read 8 byte double from byte buffers. - * @see MYSQL_TYPE_DOUBLE - * - * @return 8 byte double - */ - public double readDouble() { - return byteBuf.readDoubleLE(); - } - - /** - * Write 8 byte double to byte buffers. - * @see MYSQL_TYPE_DOUBLE - * - * @param value 8 byte double - */ - public void writeDouble(final double value) { - byteBuf.writeDoubleLE(value); - } - - /** - * Read date from byte buffers. - * @see MYSQL_TYPE_DATE - * - * @return timestamp - */ - public Timestamp readDate() { - Timestamp timestamp; - Calendar calendar = Calendar.getInstance(); - int length = readInt1(); - switch (length) { - case 0: - timestamp = new Timestamp(0); - break; - case 4: - calendar.set(readInt2(), readInt1() - 1, readInt1()); - timestamp = new Timestamp(calendar.getTimeInMillis()); - break; - case 7: - calendar.set(readInt2(), readInt1() - 1, readInt1(), - readInt1(), readInt1(), readInt1()); - timestamp = new Timestamp(calendar.getTimeInMillis()); - break; - case 11: - calendar.set(readInt2(), readInt1() - 1, readInt1(), - readInt1(), readInt1(), readInt1()); - timestamp = new Timestamp(calendar.getTimeInMillis()); - timestamp.setNanos(readInt4()); - break; - default: - throw new IllegalArgumentException(String.format("Wrong length '%d' of MYSQL_TYPE_TIME", length)); - } - return timestamp; - } - - /** - * Write date to byte buffers. - * @see MYSQL_TYPE_DATE - * - * @param timestamp timestamp - */ - public void writeDate(final Timestamp timestamp) { - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(timestamp.getTime()); - int year = calendar.get(Calendar.YEAR); - int month = calendar.get(Calendar.MONTH) + 1; - int day = calendar.get(Calendar.DAY_OF_MONTH); - int hour = calendar.get(Calendar.HOUR_OF_DAY); - int minute = calendar.get(Calendar.MINUTE); - int second = calendar.get(Calendar.SECOND); - int millisecond = timestamp.getNanos(); - boolean isDateValueAbsent = 0 == year && 0 == month && 0 == day; - boolean isTimeValueAbsent = 0 == hour && 0 == minute && 0 == second; - boolean isMillisecondValueAbsent = 0 == millisecond; - if (isDateValueAbsent && isTimeValueAbsent && isMillisecondValueAbsent) { - writeInt1(0); - } else if (isTimeValueAbsent && isMillisecondValueAbsent) { - writeInt1(4); - writeInt2(year); - writeInt1(month); - writeInt1(day); - } else if (isMillisecondValueAbsent) { - writeInt1(7); - writeInt2(year); - writeInt1(month); - writeInt1(day); - writeInt1(hour); - writeInt1(minute); - writeInt1(second); - } else { - writeInt1(11); - writeInt2(year); - writeInt1(month); - writeInt1(day); - writeInt1(hour); - writeInt1(minute); - writeInt1(second); - writeInt4(millisecond); - } - } - - /** - * Read time from byte buffers. - * @see MYSQL_TYPE_TIME - * - * @return timestamp - */ - public Timestamp readTime() { - Timestamp timestamp; - Calendar calendar = Calendar.getInstance(); - int length = readInt1(); - readInt1(); - readInt4(); - switch (length) { - case 0: - timestamp = new Timestamp(0); - break; - case 8: - calendar.set(0, 0, 0, readInt1(), readInt1(), readInt1()); - timestamp = new Timestamp(calendar.getTimeInMillis()); - timestamp.setNanos(0); - break; - case 12: - calendar.set(0, 0, 0, readInt1(), readInt1(), readInt1()); - timestamp = new Timestamp(calendar.getTimeInMillis()); - timestamp.setNanos(readInt4()); - break; - default: - throw new IllegalArgumentException(String.format("Wrong length '%d' of MYSQL_TYPE_DATE", length)); - } - return timestamp; - } - - /** - * Write time to byte buffers. - * @see MYSQL_TYPE_TIME - * - * @param date date - */ - public void writeTime(final Date date) { - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(date.getTime()); - int hour = calendar.get(Calendar.HOUR_OF_DAY); - int minute = calendar.get(Calendar.MINUTE); - int second = calendar.get(Calendar.SECOND); - Timestamp timestamp = new Timestamp(date.getTime()); - int millisecond = timestamp.getNanos(); - boolean isTimeValueAbsent = 0 == hour && 0 == minute && 0 == second; - boolean isMillisecondValueAbsent = 0 == millisecond; - if (isTimeValueAbsent && isMillisecondValueAbsent) { - writeInt1(0); - } else if (isMillisecondValueAbsent) { - writeInt1(8); - writeInt1(0); - writeInt4(0); - writeInt1(hour); - writeInt1(minute); - writeInt1(second); - } else { - writeInt1(12); - writeInt1(0); - writeInt4(0); - writeInt1(hour); - writeInt1(minute); - writeInt1(second); - writeInt4(millisecond); - } - } -} -======= /* * Copyright 2016-2018 shardingsphere.io. *

@@ -607,6 +34,7 @@ public void writeTime(final Date date) { * * @author zhangliang * @author zhangyonglun + * @author wangkai */ @RequiredArgsConstructor @Getter @@ -859,6 +287,16 @@ public void writeStringFix(final String value) { byteBuf.writeBytes(value.getBytes()); } + /** + * Write variable length bytes to byte buffers. + * @see Native41 + * + * @param value fixed length bytes + */ + public void writeBytes(final byte[] value) { + byteBuf.writeBytes(value); + } + /** * Read variable length string from byte buffers. * @see FixedLengthString @@ -1147,4 +585,3 @@ public void writeTime(final Date date) { } } } ->>>>>>> remotes/upstream/dev diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java index 0681a047bc3bb..f3aeb224e7d50 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java @@ -71,7 +71,7 @@ public void write(final MySQLPacketPayload mysqlPacketPayload) { @Override public CommandResponsePackets execute() { - log.debug("COM_QUERY received for Sharding-Proxy: {}", sql); + log.debug("COM_QUERY received for Sharding-Proxy: {}", getSql()); try { doTransactionIntercept(); } catch (final Exception ex) { @@ -127,14 +127,14 @@ private void doTransactionIntercept() throws Exception { } private boolean isXaBegin() { - return "BEGIN".equalsIgnoreCase(sql) || "START TRANSACTION".equalsIgnoreCase(sql) || "SET AUTOCOMMIT=0".equalsIgnoreCase(sql); + return "BEGIN".equalsIgnoreCase(getSql()) || "START TRANSACTION".equalsIgnoreCase(getSql()) || "SET AUTOCOMMIT=0".equalsIgnoreCase(getSql()); } private boolean isXaCommit() { - return "COMMIT".equalsIgnoreCase(sql); + return "COMMIT".equalsIgnoreCase(getSql()); } private boolean isXaRollback() { - return "ROLLBACK".equalsIgnoreCase(sql); + return "ROLLBACK".equalsIgnoreCase(getSql()); } } From e19a9401da8e7aff030178d2c36c6e6c65b593d8 Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 11 Jun 2018 16:10:49 +0800 Subject: [PATCH 153/351] Delete .gitignore --- .gitignore | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 6143e53f9e36d..0000000000000 --- a/.gitignore +++ /dev/null @@ -1,22 +0,0 @@ -# Compiled class file -*.class - -# Log file -*.log - -# BlueJ files -*.ctxt - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.ear -*.zip -*.tar.gz -*.rar - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* From ed6bec3d02702df7942741e138e95f53382ccc1b Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 11 Jun 2018 16:42:35 +0800 Subject: [PATCH 154/351] merge native client --- .../orchestration/internal/OrchestrationFacade.java | 11 ++++++++++- .../newzk/NewZookeeperRegistryCenter.java | 0 .../{new _zk => }/newzk/client/action/Callback.java | 0 .../{new _zk => }/newzk/client/action/IAction.java | 0 .../{new _zk => }/newzk/client/action/IClient.java | 0 .../newzk/client/action/IExecStrategy.java | 0 .../newzk/client/action/IGroupAction.java | 0 .../{new _zk => }/newzk/client/action/IProvider.java | 0 .../newzk/client/cache/CacheStrategy.java | 0 .../{new _zk => }/newzk/client/cache/PathNode.java | 0 .../{new _zk => }/newzk/client/cache/PathStatus.java | 0 .../{new _zk => }/newzk/client/cache/PathTree.java | 0 .../newzk/client/election/LeaderElection.java | 0 .../newzk/client/retry/AsyncRetryCenter.java | 0 .../newzk/client/retry/DelayPolicyExecutor.java | 0 .../newzk/client/retry/DelayRetryPolicy.java | 0 .../{new _zk => }/newzk/client/retry/RetryThread.java | 0 .../{new _zk => }/newzk/client/utility/Constants.java | 0 .../{new _zk => }/newzk/client/utility/PathUtil.java | 0 .../newzk/client/utility/StringUtil.java | 0 .../newzk/client/zookeeper/CacheClient.java | 0 .../newzk/client/zookeeper/ClientFactory.java | 0 .../newzk/client/zookeeper/UsualClient.java | 0 .../newzk/client/zookeeper/base/BaseClient.java | 0 .../client/zookeeper/base/BaseClientFactory.java | 0 .../newzk/client/zookeeper/base/BaseContext.java | 0 .../newzk/client/zookeeper/base/BaseOperation.java | 0 .../newzk/client/zookeeper/base/BaseProvider.java | 0 .../newzk/client/zookeeper/base/BaseStrategy.java | 0 .../newzk/client/zookeeper/base/Holder.java | 0 .../zookeeper/operation/CreateAllNeedOperation.java | 0 .../zookeeper/operation/CreateCurrentOperation.java | 0 .../operation/DeleteAllChildrenOperation.java | 0 .../operation/DeleteCurrentBranchOperation.java | 0 .../zookeeper/operation/DeleteCurrentOperation.java | 0 .../client/zookeeper/operation/UpdateOperation.java | 0 .../newzk/client/zookeeper/section/Callable.java | 0 .../newzk/client/zookeeper/section/ClientContext.java | 0 .../newzk/client/zookeeper/section/ClientTask.java | 0 .../newzk/client/zookeeper/section/Connection.java | 0 .../newzk/client/zookeeper/section/Listener.java | 0 .../newzk/client/zookeeper/section/StrategyType.java | 0 .../client/zookeeper/section/WatcherCreator.java | 0 .../client/zookeeper/strategy/AsyncRetryStrategy.java | 0 .../client/zookeeper/strategy/ContentionStrategy.java | 0 .../client/zookeeper/strategy/SyncRetryStrategy.java | 0 .../strategy/TransactionContendStrategy.java | 0 .../client/zookeeper/strategy/UsualStrategy.java | 0 .../client/zookeeper/transaction/ZKTransaction.java | 0 .../reg/zookeeper/ZookeeperConfiguration.java | 7 +++++++ 50 files changed, 17 insertions(+), 1 deletion(-) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/NewZookeeperRegistryCenter.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/action/Callback.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/action/IAction.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/action/IClient.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/action/IExecStrategy.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/action/IGroupAction.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/action/IProvider.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/cache/CacheStrategy.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/cache/PathNode.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/cache/PathStatus.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/cache/PathTree.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/election/LeaderElection.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/retry/AsyncRetryCenter.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/retry/DelayPolicyExecutor.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/retry/DelayRetryPolicy.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/retry/RetryThread.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/utility/Constants.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/utility/PathUtil.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/utility/StringUtil.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/CacheClient.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/ClientFactory.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/UsualClient.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/base/BaseClient.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/base/BaseClientFactory.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/base/BaseContext.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/base/BaseOperation.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/base/BaseProvider.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/base/BaseStrategy.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/base/Holder.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/operation/CreateAllNeedOperation.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/operation/CreateCurrentOperation.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/operation/DeleteCurrentBranchOperation.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/operation/DeleteCurrentOperation.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/operation/UpdateOperation.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/section/Callable.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/section/ClientContext.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/section/ClientTask.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/section/Connection.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/section/Listener.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/section/StrategyType.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/section/WatcherCreator.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/strategy/ContentionStrategy.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/strategy/SyncRetryStrategy.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/strategy/TransactionContendStrategy.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/strategy/UsualStrategy.java (100%) rename sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/{new _zk => }/newzk/client/zookeeper/transaction/ZKTransaction.java (100%) diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/internal/OrchestrationFacade.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/internal/OrchestrationFacade.java index 123dc96b3c29c..09c7ca996533b 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/internal/OrchestrationFacade.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/internal/OrchestrationFacade.java @@ -31,6 +31,7 @@ import io.shardingsphere.jdbc.orchestration.reg.api.RegistryCenterConfiguration; import io.shardingsphere.jdbc.orchestration.reg.etcd.EtcdConfiguration; import io.shardingsphere.jdbc.orchestration.reg.etcd.EtcdRegistryCenter; +import io.shardingsphere.jdbc.orchestration.reg.newzk.NewZookeeperRegistryCenter; import io.shardingsphere.jdbc.orchestration.reg.zookeeper.ZookeeperConfiguration; import io.shardingsphere.jdbc.orchestration.reg.zookeeper.ZookeeperRegistryCenter; import lombok.Getter; @@ -76,7 +77,7 @@ public OrchestrationFacade(final OrchestrationConfiguration config) { private RegistryCenter createRegistryCenter(final RegistryCenterConfiguration regCenterConfig) { Preconditions.checkNotNull(regCenterConfig, "Registry center configuration cannot be null."); if (regCenterConfig instanceof ZookeeperConfiguration) { - return new ZookeeperRegistryCenter((ZookeeperConfiguration) regCenterConfig); + return getZookeeperRegistryCenter((ZookeeperConfiguration) regCenterConfig); } if (regCenterConfig instanceof EtcdConfiguration) { return new EtcdRegistryCenter((EtcdConfiguration) regCenterConfig); @@ -84,6 +85,14 @@ private RegistryCenter createRegistryCenter(final RegistryCenterConfiguration re throw new UnsupportedOperationException(regCenterConfig.getClass().getName()); } + private RegistryCenter getZookeeperRegistryCenter(final ZookeeperConfiguration regCenterConfig) { + if (regCenterConfig.isUseNative()) { + return new NewZookeeperRegistryCenter(regCenterConfig); + } else { + return new ZookeeperRegistryCenter(regCenterConfig); + } + } + /** * Initialize for sharding orchestration. * diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/NewZookeeperRegistryCenter.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/NewZookeeperRegistryCenter.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/NewZookeeperRegistryCenter.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/NewZookeeperRegistryCenter.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/Callback.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/Callback.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/Callback.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/Callback.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IAction.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IAction.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IAction.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IAction.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IClient.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IClient.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IClient.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IClient.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IExecStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IExecStrategy.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IExecStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IExecStrategy.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IGroupAction.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IGroupAction.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IGroupAction.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IGroupAction.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IProvider.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IProvider.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/action/IProvider.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IProvider.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/CacheStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/CacheStrategy.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/CacheStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/CacheStrategy.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/PathNode.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/PathNode.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/PathNode.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/PathNode.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/PathStatus.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/PathStatus.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/PathStatus.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/PathStatus.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/PathTree.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/PathTree.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/cache/PathTree.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/PathTree.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/election/LeaderElection.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/election/LeaderElection.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/election/LeaderElection.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/election/LeaderElection.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/AsyncRetryCenter.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/AsyncRetryCenter.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/AsyncRetryCenter.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/AsyncRetryCenter.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/DelayPolicyExecutor.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/DelayPolicyExecutor.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/DelayPolicyExecutor.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/DelayPolicyExecutor.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/DelayRetryPolicy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/DelayRetryPolicy.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/DelayRetryPolicy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/DelayRetryPolicy.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/RetryThread.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/RetryThread.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/retry/RetryThread.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/RetryThread.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/utility/Constants.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/utility/Constants.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/utility/Constants.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/utility/Constants.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/utility/PathUtil.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/utility/PathUtil.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/utility/PathUtil.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/utility/PathUtil.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/utility/StringUtil.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/utility/StringUtil.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/utility/StringUtil.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/utility/StringUtil.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/CacheClient.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/CacheClient.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/CacheClient.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/CacheClient.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/ClientFactory.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/ClientFactory.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/ClientFactory.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/ClientFactory.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/UsualClient.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/UsualClient.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/UsualClient.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/UsualClient.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseClient.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseClient.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseClient.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseClient.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseClientFactory.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseClientFactory.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseClientFactory.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseClientFactory.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseContext.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseContext.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseContext.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseContext.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseOperation.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseOperation.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseOperation.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseProvider.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseProvider.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseProvider.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseProvider.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseStrategy.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/BaseStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseStrategy.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/Holder.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/Holder.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/base/Holder.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/Holder.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/CreateAllNeedOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/CreateAllNeedOperation.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/CreateAllNeedOperation.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/CreateAllNeedOperation.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/CreateCurrentOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/CreateCurrentOperation.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/CreateCurrentOperation.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/CreateCurrentOperation.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/DeleteCurrentBranchOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/DeleteCurrentBranchOperation.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/DeleteCurrentBranchOperation.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/DeleteCurrentBranchOperation.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/DeleteCurrentOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/DeleteCurrentOperation.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/DeleteCurrentOperation.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/DeleteCurrentOperation.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/UpdateOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/UpdateOperation.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/operation/UpdateOperation.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/UpdateOperation.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/Callable.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/Callable.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/Callable.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/Callable.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/ClientContext.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/ClientContext.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/ClientContext.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/ClientContext.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/ClientTask.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/ClientTask.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/ClientTask.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/ClientTask.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/Connection.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/Connection.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/Connection.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/Connection.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/Listener.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/Listener.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/Listener.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/Listener.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/StrategyType.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/StrategyType.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/StrategyType.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/StrategyType.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/WatcherCreator.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/WatcherCreator.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/section/WatcherCreator.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/WatcherCreator.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/ContentionStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/ContentionStrategy.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/ContentionStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/ContentionStrategy.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/SyncRetryStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/SyncRetryStrategy.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/SyncRetryStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/SyncRetryStrategy.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/TransactionContendStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/TransactionContendStrategy.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/TransactionContendStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/TransactionContendStrategy.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/UsualStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/UsualStrategy.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/strategy/UsualStrategy.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/UsualStrategy.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/transaction/ZKTransaction.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/transaction/ZKTransaction.java similarity index 100% rename from sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/new _zk/newzk/client/zookeeper/transaction/ZKTransaction.java rename to sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/transaction/ZKTransaction.java diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/zookeeper/ZookeeperConfiguration.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/zookeeper/ZookeeperConfiguration.java index a182d5fb99596..9b9794962fa4a 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/zookeeper/ZookeeperConfiguration.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/zookeeper/ZookeeperConfiguration.java @@ -73,4 +73,11 @@ public final class ZookeeperConfiguration implements RegistryCenterConfiguration *

Default is not need digest

*/ private String digest; + + /** + * RegCenter for zookeeper. + * + *

Default is ZookeeperRegistryCenter

+ */ + private boolean useNative; } From 80253636f058b786c9f6c659030bbe3b1920df9f Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 16:50:59 +0800 Subject: [PATCH 155/351] for #601, add case select_or_with_same_sharding_columns --- .../src/test/resources/parser/select_or.xml | 2 +- .../select_or_with_same_sharding_columns.xml | 5 ++ .../select_or_with_same_sharding_columns.xml | 5 ++ .../select_or_with_same_sharding_columns.xml | 5 ++ .../cases/select/dql-integrate-test-cases.xml | 52 +++++++++++++++++++ .../resources/integrate/assert/select_or.xml | 2 +- .../src/main/resources/sql/dql/select_or.xml | 3 +- 7 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_same_sharding_columns.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_same_sharding_columns.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_same_sharding_columns.xml diff --git a/sharding-core/src/test/resources/parser/select_or.xml b/sharding-core/src/test/resources/parser/select_or.xml index d8088b3a3cc06..4740eafb1887e 100644 --- a/sharding-core/src/test/resources/parser/select_or.xml +++ b/sharding-core/src/test/resources/parser/select_or.xml @@ -1,6 +1,6 @@ - + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_same_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_same_sharding_columns.xml new file mode 100644 index 0000000000000..ed6d26eb08897 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_same_sharding_columns.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_same_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_same_sharding_columns.xml new file mode 100644 index 0000000000000..1026341c2c762 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_same_sharding_columns.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_same_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_same_sharding_columns.xml new file mode 100644 index 0000000000000..ed6d26eb08897 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_same_sharding_columns.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 064cb943b0e9c..7954533ef8a95 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -253,4 +253,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml index 25f9c4eb7ad19..8b35dfe43a007 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml @@ -1,6 +1,6 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml index 4a8fe99808924..04eb22b5bc27e 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml @@ -1,6 +1,7 @@ - + + From cc89da6e5c049cdd663091323c5c1bbe266c05cc Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 17:08:14 +0800 Subject: [PATCH 156/351] for #601, add case select_or_with_different_sharding_columns --- .../src/test/resources/parser/select_or.xml | 4 ++-- ...select_or_with_different_sharding_columns.xml | 6 ++++++ ...select_or_with_different_sharding_columns.xml | 14 ++++++++++++++ ...select_or_with_different_sharding_columns.xml | 14 ++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 16 ++++++---------- .../resources/integrate/assert/select_or.xml | 2 +- .../src/main/resources/sql/dql/select_or.xml | 3 ++- 7 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_different_sharding_columns.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_different_sharding_columns.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_different_sharding_columns.xml diff --git a/sharding-core/src/test/resources/parser/select_or.xml b/sharding-core/src/test/resources/parser/select_or.xml index 4740eafb1887e..e1abc30fdbf04 100644 --- a/sharding-core/src/test/resources/parser/select_or.xml +++ b/sharding-core/src/test/resources/parser/select_or.xml @@ -20,8 +20,8 @@ - - + +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_different_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_different_sharding_columns.xml new file mode 100644 index 0000000000000..daeb235c02fa0 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_different_sharding_columns.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_different_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_different_sharding_columns.xml new file mode 100644 index 0000000000000..1ec56c28390b7 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_different_sharding_columns.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_different_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_different_sharding_columns.xml new file mode 100644 index 0000000000000..dedc3c05ea345 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_different_sharding_columns.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 7954533ef8a95..b225ab4175bb0 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -259,6 +259,12 @@ + + + + + + @@ -270,16 +276,6 @@ - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml index 8b35dfe43a007..06194f26c8f9c 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml @@ -5,7 +5,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml index 04eb22b5bc27e..d58bcf0abfa18 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml @@ -2,7 +2,8 @@ - + + From e637b9cd78713b735ffdcfa2176be564b0eb8800 Mon Sep 17 00:00:00 2001 From: saaav Date: Mon, 11 Jun 2018 17:20:17 +0800 Subject: [PATCH 157/351] clear package --- .../reg/newzk/NewZookeeperRegistryCenter.java | 41 +++++++++++-------- .../reg/newzk/client/action/IClient.java | 3 +- .../reg/newzk/client/cache/PathNode.java | 18 ++++---- .../reg/newzk/client/cache/PathTree.java | 25 ++++++----- .../newzk/client/election/LeaderElection.java | 9 ++-- .../newzk/client/retry/AsyncRetryCenter.java | 4 +- .../client/retry/DelayPolicyExecutor.java | 2 +- .../newzk/client/retry/DelayRetryPolicy.java | 4 +- .../reg/newzk/client/retry/RetryThread.java | 9 ++-- .../reg/newzk/client/utility/PathUtil.java | 4 +- .../newzk/client/zookeeper/CacheClient.java | 11 ++--- .../newzk/client/zookeeper/ClientFactory.java | 21 +++++----- .../newzk/client/zookeeper/UsualClient.java | 16 +++++--- .../client/zookeeper/base/BaseClient.java | 13 +++--- .../zookeeper/base/BaseClientFactory.java | 3 +- .../client/zookeeper/base/BaseContext.java | 5 ++- .../client/zookeeper/base/BaseOperation.java | 8 ++-- .../client/zookeeper/base/BaseProvider.java | 2 +- .../client/zookeeper/base/BaseStrategy.java | 8 ++-- .../newzk/client/zookeeper/base/Holder.java | 2 +- .../operation/DeleteAllChildrenOperation.java | 9 ++-- .../zookeeper/operation/UpdateOperation.java | 6 ++- .../client/zookeeper/section/Callable.java | 15 ++++--- .../zookeeper/section/ClientContext.java | 8 ++-- .../client/zookeeper/section/ClientTask.java | 2 +- .../zookeeper/section/WatcherCreator.java | 4 +- .../strategy/AsyncRetryStrategy.java | 28 ++++++++----- .../strategy/ContentionStrategy.java | 16 ++++---- .../zookeeper/strategy/SyncRetryStrategy.java | 18 ++++---- .../strategy/TransactionContendStrategy.java | 5 ++- .../zookeeper/strategy/UsualStrategy.java | 2 +- .../zookeeper/transaction/ZKTransaction.java | 28 +++++++------ 32 files changed, 198 insertions(+), 151 deletions(-) diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/NewZookeeperRegistryCenter.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/NewZookeeperRegistryCenter.java index 7754d1999057a..df1d7a682f15a 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/NewZookeeperRegistryCenter.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/NewZookeeperRegistryCenter.java @@ -23,7 +23,14 @@ import io.shardingsphere.jdbc.orchestration.reg.exception.RegExceptionHandler; import io.shardingsphere.jdbc.orchestration.reg.listener.DataChangedEvent; import io.shardingsphere.jdbc.orchestration.reg.listener.EventListener; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IClient; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathTree; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.StringUtil; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.StrategyType; +import io.shardingsphere.jdbc.orchestration.reg.zookeeper.ZookeeperConfiguration; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; @@ -44,28 +51,28 @@ */ public final class NewZookeeperRegistryCenter implements RegistryCenter { - private final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IClient client; + private final IClient client; - private final Map caches = new HashMap<>(); + private final Map caches = new HashMap<>(); - public NewZookeeperRegistryCenter(final io.shardingsphere.jdbc.orchestration.reg.zookeeper.ZookeeperConfiguration zkConfig) { - io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory creator = buildCreator(zkConfig); + public NewZookeeperRegistryCenter(final ZookeeperConfiguration zkConfig) { + ClientFactory creator = buildCreator(zkConfig); client = initClient(creator, zkConfig); } - private io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory buildCreator(final io.shardingsphere.jdbc.orchestration.reg.zookeeper.ZookeeperConfiguration zkConfig) { - io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory creator = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory(); + private ClientFactory buildCreator(final ZookeeperConfiguration zkConfig) { + ClientFactory creator = new ClientFactory(); creator.setClientNamespace(zkConfig.getNamespace()) .newClient(zkConfig.getServerLists(), zkConfig.getSessionTimeoutMilliseconds()) - .setRetryPolicy(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy(zkConfig.getBaseSleepTimeMilliseconds(), zkConfig.getMaxRetries(), zkConfig.getMaxSleepTimeMilliseconds())); + .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); } return creator; } - private io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IClient initClient(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory creator, final io.shardingsphere.jdbc.orchestration.reg.zookeeper.ZookeeperConfiguration zkConfig) { - io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IClient newClient = null; + private IClient initClient(final ClientFactory creator, final ZookeeperConfiguration zkConfig) { + IClient newClient = null; try { newClient = creator.start(); // block, slowly @@ -84,7 +91,7 @@ private io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IClient ini @Override public String get(final String key) { - io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathTree cache = findTreeCache(key); + PathTree cache = findTreeCache(key); if (null == cache) { return getDirectly(key); } @@ -95,8 +102,8 @@ public String get(final String key) { return getDirectly(key); } - private io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathTree findTreeCache(final String key) { - for (Entry entry : caches.entrySet()) { + private PathTree findTreeCache(final String key) { + for (Entry entry : caches.entrySet()) { if (key.startsWith(entry.getKey())) { return entry.getValue(); } @@ -194,11 +201,11 @@ public void watch(final String key, final EventListener eventListener) { if (!caches.containsKey(path)) { addCacheData(key); } - io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathTree cache = caches.get(path); - cache.watch(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener() { + final PathTree cache = caches.get(path); + cache.watch(new Listener() { @Override public void process(final WatchedEvent event) { - if (!io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.StringUtil.isNullOrBlank(event.getPath())) { + if (!StringUtil.isNullOrBlank(event.getPath())) { eventListener.onChange(new DataChangedEvent(getEventType(event), event.getPath(), getWithoutCache(event.getPath()))); } } @@ -232,7 +239,7 @@ private synchronized String getWithoutCache(final String key) { } private void addCacheData(final String cachePath) { - io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathTree cache = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathTree(cachePath, client); + PathTree cache = new PathTree(cachePath, client); try { cache.load(); cache.watch(); @@ -246,7 +253,7 @@ private void addCacheData(final String cachePath) { @Override public void close() { - for (Entry each : caches.entrySet()) { + for (Entry each : caches.entrySet()) { each.getValue().close(); } client.close(); diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IClient.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IClient.java index aad9963578780..f0f38368d2bdd 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IClient.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IClient.java @@ -17,6 +17,7 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.action; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.StrategyType; import java.io.IOException; @@ -58,7 +59,7 @@ public interface IClient extends io.shardingsphere.jdbc.orchestration.reg.newzk. * @param key key * @param listener listener */ - void registerWatch(String key, io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener listener); + void registerWatch(String key, Listener listener); /** * unregister watcher. diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/PathNode.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/PathNode.java index b366404e78cb0..04538f7e33a79 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/PathNode.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/PathNode.java @@ -33,9 +33,9 @@ * @author lidongbo */ public class PathNode { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathNode.class); + private static final Logger LOGGER = LoggerFactory.getLogger(PathNode.class); - private final Map children = new ConcurrentHashMap<>(); + private final Map children = new ConcurrentHashMap<>(); private final String nodeKey; @@ -57,7 +57,7 @@ public class PathNode { * * @return children */ - public Map getChildren() { + public Map getChildren() { return children; } @@ -75,17 +75,17 @@ public String getKey() { * * @param node node */ - public void attachChild(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathNode node) { + public void attachChild(final PathNode node) { this.children.put(node.nodeKey, node); } - io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathNode set(final Iterator iterator, final String value) { + PathNode set(final Iterator iterator, final String value) { String key = iterator.next(); LOGGER.debug("PathNode set:{},value:{}", key, value); - io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathNode node = children.get(key); + PathNode node = children.get(key); if (node == null) { LOGGER.debug("set children haven't:{}", key); - node = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathNode(key); + node = new PathNode(key); children.put(key, node); } if (iterator.hasNext()) { @@ -96,10 +96,10 @@ io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathNode set(final I return node; } - io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathNode get(final Iterator iterator) { + PathNode get(final Iterator iterator) { String key = iterator.next(); LOGGER.debug("get:{}", key); - io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathNode node = children.get(key); + PathNode node = children.get(key); if (node == null) { LOGGER.debug("get children haven't:{}", key); return null; diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/PathTree.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/PathTree.java index 56d58ccc3648e..afb91639b5c26 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/PathTree.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/cache/PathTree.java @@ -21,6 +21,8 @@ import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.UsualClient; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener; import lombok.Getter; import lombok.Setter; import org.apache.zookeeper.KeeperException; @@ -44,7 +46,7 @@ * @author lidongbo */ public final class PathTree { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathTree.class); + private static final Logger LOGGER = LoggerFactory.getLogger(PathTree.class); private final transient ReentrantLock lock = new ReentrantLock(); @@ -60,16 +62,16 @@ public final class PathTree { @Getter @Setter - private io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathStatus status; + private PathStatus status; private boolean closed; public PathTree(final String root, final IClient client) { this.rootNode.set(new PathNode(root)); - this.status = io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathStatus.RELEASE; + this.status = PathStatus.RELEASE; this.client = client; // todo It looks unpleasant - this.provider = ((io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.UsualClient) client).getStrategy().getProvider(); + this.provider = ((UsualClient) client).getStrategy().getProvider(); } /** @@ -87,7 +89,7 @@ public void load() throws KeeperException, InterruptedException { try { if (status == status.RELEASE) { LOGGER.debug("loading status:{}", status); - this.setStatus(io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathStatus.CHANGING); + this.setStatus(PathStatus.CHANGING); PathNode newRoot = new PathNode(rootNode.get().getKey()); List children = provider.getChildren(rootNode.get().getKey()); @@ -95,7 +97,7 @@ public void load() throws KeeperException, InterruptedException { this.attechIntoNode(children, newRoot); rootNode.set(newRoot); - this.setStatus(io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathStatus.RELEASE); + this.setStatus(PathStatus.RELEASE); // watch(); LOGGER.debug("loading release:{}", status); } else { @@ -155,7 +157,7 @@ public void refreshPeriodic(final long period) { @Override public void run() { LOGGER.debug("cacheService run:{}", getStatus()); - if (io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathStatus.RELEASE == getStatus()) { + if (PathStatus.RELEASE == getStatus()) { try { load(); // CHECKSTYLE:OFF @@ -191,7 +193,7 @@ public void stopRefresh() { * watch data change. */ public void watch() { - watch(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener(rootNode.get().getKey()) { + watch(new Listener(rootNode.get().getKey()) { @Override public void process(final WatchedEvent event) { String path = event.getPath(); @@ -217,7 +219,7 @@ public void process(final WatchedEvent event) { * * @param listener listener */ - public void watch(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener listener) { + public void watch(final Listener listener) { if (closed) { return; } @@ -312,6 +314,7 @@ private PathNode get(final String path) { if (path.equals(rootNode.get().getKey())) { return rootNode.get(); } + // todo iterator -> token Iterator iterator = keyIterator(path); if (iterator.hasNext()) { return rootNode.get().get(iterator); @@ -341,9 +344,9 @@ public void put(final String path, final String value) { rootNode.set(new PathNode(rootNode.get().getKey(), value.getBytes(Constants.UTF_8))); return; } - this.setStatus(io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathStatus.CHANGING); + this.setStatus(PathStatus.CHANGING); rootNode.get().set(keyIterator(path), value); - this.setStatus(io.shardingsphere.jdbc.orchestration.reg.newzk.client.cache.PathStatus.RELEASE); + this.setStatus(PathStatus.RELEASE); } else { try { LOGGER.debug("put but cache status not release"); diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/election/LeaderElection.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/election/LeaderElection.java index bb87d24f5b984..336f6651503e8 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/election/LeaderElection.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/election/LeaderElection.java @@ -17,7 +17,10 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.election; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.WatcherCreator; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; @@ -31,7 +34,7 @@ * @author lidongbo */ public abstract class LeaderElection { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.election.LeaderElection.class); + private static final Logger LOGGER = LoggerFactory.getLogger(LeaderElection.class); private boolean done; @@ -41,7 +44,7 @@ public LeaderElection() { retryCount = Constants.NODE_ELECTION_RETRY; } - private boolean contend(final String node, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener listener) throws KeeperException, InterruptedException { + private boolean contend(final String node, final IProvider provider, final Listener listener) throws KeeperException, InterruptedException { boolean success = false; try { // todo EPHEMERAL_SEQUENTIAL check index value @@ -50,7 +53,7 @@ private boolean contend(final String node, final io.shardingsphere.jdbc.orchestr } catch (KeeperException.NodeExistsException e) { LOGGER.info("contend not success"); // TODO or changing_key node value == current client id - provider.exists(node, io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.WatcherCreator.deleteWatcher(listener)); + provider.exists(node, WatcherCreator.deleteWatcher(listener)); } return success; } diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/AsyncRetryCenter.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/AsyncRetryCenter.java index dab4a771083d9..c730d47c72c6d 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/AsyncRetryCenter.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/AsyncRetryCenter.java @@ -31,7 +31,7 @@ public enum AsyncRetryCenter { INSTANCE; - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.class); + private static final Logger LOGGER = LoggerFactory.getLogger(AsyncRetryCenter.class); private final DelayQueue queue = new DelayQueue<>(); @@ -77,7 +77,7 @@ public void add(final BaseOperation operation) { LOGGER.warn("delayRetryPolicy no init and auto init with DelayRetryPolicy.newNoInitDelayPolicy"); delayRetryPolicy = DelayRetryPolicy.newNoInitDelayPolicy(); } - operation.setDelayPolicyExecutor(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayPolicyExecutor(delayRetryPolicy)); + operation.setDelayPolicyExecutor(new DelayPolicyExecutor(delayRetryPolicy)); queue.offer(operation); LOGGER.debug("enqueue operation:{}", operation.toString()); } diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/DelayPolicyExecutor.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/DelayPolicyExecutor.java index 484a10de86cf0..61238db2b96e2 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/DelayPolicyExecutor.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/DelayPolicyExecutor.java @@ -28,7 +28,7 @@ * @author lidongbo */ public class DelayPolicyExecutor { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayPolicyExecutor.class); + private static final Logger LOGGER = LoggerFactory.getLogger(DelayPolicyExecutor.class); private final DelayRetryPolicy delayRetryPolicy; diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/DelayRetryPolicy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/DelayRetryPolicy.java index 95e9880750533..2fb3f0d95c745 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/DelayRetryPolicy.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/DelayRetryPolicy.java @@ -58,7 +58,7 @@ public DelayRetryPolicy(final int retryCount, final long baseDelay, final long d * * @return DelayPolicy */ - public static io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy newNoInitDelayPolicy() { - return new io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy(BASE_COUNT, BASE_DELAY, Integer.MAX_VALUE); + public static DelayRetryPolicy newNoInitDelayPolicy() { + return new DelayRetryPolicy(BASE_COUNT, BASE_DELAY, Integer.MAX_VALUE); } } diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/RetryThread.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/RetryThread.java index 80968eace9b07..901466ddfa1e8 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/RetryThread.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/RetryThread.java @@ -17,6 +17,7 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,7 +35,7 @@ * @author lidongbo */ public class RetryThread extends Thread { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.RetryThread.class); + private static final Logger LOGGER = LoggerFactory.getLogger(RetryThread.class); private final int corePoolSize = Runtime.getRuntime().availableProcessors(); @@ -46,9 +47,9 @@ public class RetryThread extends Thread { private final int closeDelay = 60; - private final DelayQueue queue; + private final DelayQueue queue; - public RetryThread(final DelayQueue queue) { + public RetryThread(final DelayQueue queue) { this.queue = queue; retryExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(10), new ThreadFactory() { private final AtomicInteger threadIndex = new AtomicInteger(0); @@ -68,7 +69,7 @@ public Thread newThread(final Runnable r) { public void run() { LOGGER.debug("RetryThread start"); for (;;) { - final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation operation; + final BaseOperation operation; try { operation = queue.take(); LOGGER.debug("take operation:{}", operation.toString()); diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/utility/PathUtil.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/utility/PathUtil.java index 49f5361448535..88622e6a24dfc 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/utility/PathUtil.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/utility/PathUtil.java @@ -40,7 +40,7 @@ public static String getRealPath(final String root, final String path) { } private static String adjustPath(final String root, final String path) { - if (io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.StringUtil.isNullOrBlank(path)) { + if (StringUtil.isNullOrBlank(path)) { throw new IllegalArgumentException("path should have content!"); } String rootPath = root; @@ -138,7 +138,7 @@ public static List getShortPathNodes(final String path) { // CHECKSTYLE:OFF public static String checkPath(final String key) throws IllegalArgumentException { // CHECKSTYLE:ON - if (io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.StringUtil.isNullOrBlank(key)) { + if (StringUtil.isNullOrBlank(key)) { throw new IllegalArgumentException("path should not be null"); } String path = key; diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/CacheClient.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/CacheClient.java index 62b3cdfbfb9ba..bbdf503c8fcb7 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/CacheClient.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/CacheClient.java @@ -36,7 +36,7 @@ * @author lidongbo */ public final class CacheClient extends UsualClient { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.CacheClient.class); + private static final Logger LOGGER = LoggerFactory.getLogger(CacheClient.class); private PathTree pathTree; @@ -64,20 +64,17 @@ public void close() { void useCacheStrategy(final CacheStrategy cacheStrategy) throws KeeperException, InterruptedException { LOGGER.debug("use cache strategy:{}", cacheStrategy); switch (cacheStrategy) { - case WATCH: { + case WATCH: pathTree = new PathTree(getRootNode(), this); pathTree.watch(); return; - } - case ALL: { + case ALL: pathTree = loadPathTree(); pathTree.refreshPeriodic(Constants.THREAD_PERIOD); return; - } case NONE: - default: { + default: return; - } } } diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/ClientFactory.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/ClientFactory.java index 32eb05510f327..33d3d37db7085 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/ClientFactory.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/ClientFactory.java @@ -23,6 +23,7 @@ import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseClientFactory; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.ClientContext; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener; import org.apache.zookeeper.data.ACL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,7 +36,7 @@ */ public class ClientFactory extends BaseClientFactory { // private static final String CLIENT_EXCLUSIVE_NODE = "ZKC"; - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory.class); + private static final Logger LOGGER = LoggerFactory.getLogger(ClientFactory.class); private DelayRetryPolicy delayRetryPolicy; @@ -46,13 +47,13 @@ public class ClientFactory extends BaseClientFactory { * @param sessionTimeoutMilliseconds sessionTimeoutMilliseconds * @return ClientFactory this */ - public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory newClient(final String servers, final int sessionTimeoutMilliseconds) { + public ClientFactory newClient(final String servers, final int sessionTimeoutMilliseconds) { int wait = sessionTimeoutMilliseconds; if (sessionTimeoutMilliseconds == 0) { wait = Constants.WAIT; } setContext(new ClientContext(servers, wait)); - setClient(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.UsualClient(getContext())); + setClient(new UsualClient(getContext())); LOGGER.debug("new usual client"); return this; } @@ -63,7 +64,7 @@ public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFac */ synchronized BaseClientFactory newClientByOriginal(final boolean closeOriginal) { IClient oldClient = getClient(); - setClient(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.UsualClient(getContext())); + setClient(new UsualClient(getContext())); if (closeOriginal) { oldClient.close(); } @@ -71,9 +72,9 @@ synchronized BaseClientFactory newClientByOriginal(final boolean closeOriginal) return this; } - io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory newCacheClient(final String servers, final int sessionTimeoutMilliseconds) { + ClientFactory newCacheClient(final String servers, final int sessionTimeoutMilliseconds) { setContext(new ClientContext(servers, sessionTimeoutMilliseconds)); - setClient(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.CacheClient(getContext())); + setClient(new CacheClient(getContext())); LOGGER.debug("new cache client"); return this; } @@ -84,7 +85,7 @@ io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory ne * @param globalListener globalListener * @return ClientFactory this */ - public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory watch(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener globalListener) { + public ClientFactory watch(final Listener globalListener) { setGlobalListener(globalListener); return this; } @@ -95,7 +96,7 @@ public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFac * @param namespace namespace * @return ClientFactory this */ - public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory setClientNamespace(final String namespace) { + public ClientFactory setClientNamespace(final String namespace) { setNamespace(PathUtil.checkPath(namespace)); return this; } @@ -108,7 +109,7 @@ public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFac * @param authorities authorities * @return ClientFactory this */ - public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory authorization(final String scheme, final byte[] auth, final List authorities) { + public ClientFactory authorization(final String scheme, final byte[] auth, final List authorities) { setScheme(scheme); setAuth(auth); setAuthorities(authorities); @@ -121,7 +122,7 @@ public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFac * @param delayRetryPolicy delayRetryPolicy * @return ClientFactory this */ - public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.ClientFactory setRetryPolicy(final DelayRetryPolicy delayRetryPolicy) { + public ClientFactory setRetryPolicy(final DelayRetryPolicy delayRetryPolicy) { this.delayRetryPolicy = delayRetryPolicy; return this; } diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/UsualClient.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/UsualClient.java index c680ca2679ae6..9e5c715b5cc25 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/UsualClient.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/UsualClient.java @@ -17,13 +17,17 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IExecStrategy; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseClient; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseContext; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseProvider; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.ClientContext; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.StrategyType; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.AsyncRetryStrategy; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.ContentionStrategy; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.SyncRetryStrategy; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction; import lombok.Getter; import org.apache.zookeeper.AsyncCallback; @@ -42,9 +46,9 @@ * @author lidongbo */ public class UsualClient extends BaseClient { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.UsualClient.class); + private static final Logger LOGGER = LoggerFactory.getLogger(UsualClient.class); - private final Map strategies = new ConcurrentHashMap<>(); + private final Map strategies = new ConcurrentHashMap<>(); private final boolean watched = true; @@ -75,22 +79,22 @@ public synchronized void useExecStrategy(final StrategyType strategyType) { return; } - IProvider provider = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseProvider(getRootNode(), getHolder(), watched, getAuthorities()); + IProvider provider = new BaseProvider(getRootNode(), getHolder(), watched, getAuthorities()); switch (strategyType) { case USUAL: - strategy = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy(provider); + strategy = new UsualStrategy(provider); break; case CONTEND: strategy = new ContentionStrategy(provider); break; case SYNC_RETRY: - strategy = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.SyncRetryStrategy(provider, ((ClientContext) getContext()).getDelayRetryPolicy()); + strategy = new SyncRetryStrategy(provider, ((ClientContext) getContext()).getDelayRetryPolicy()); break; case ASYNC_RETRY: strategy = new AsyncRetryStrategy(provider, ((ClientContext) getContext()).getDelayRetryPolicy()); break; default: - strategy = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy(provider); + strategy = new UsualStrategy(provider); break; } diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseClient.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseClient.java index 613afd94a4fec..416979e44dfdd 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseClient.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseClient.java @@ -20,6 +20,9 @@ import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IClient; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.StringUtil; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.WatcherCreator; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; @@ -38,7 +41,7 @@ * @author lidongbo */ public abstract class BaseClient implements IClient { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseClient.class); + private static final Logger LOGGER = LoggerFactory.getLogger(BaseClient.class); private final int circleWait = 30; @@ -96,7 +99,7 @@ public void close() { holder.close(); } - void registerWatch(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener globalListener) { + void registerWatch(final Listener globalListener) { if (context.getGlobalListener() != null) { LOGGER.warn("global listener can only register one"); return; @@ -106,7 +109,7 @@ void registerWatch(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.z } @Override - public void registerWatch(final String key, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener listener) { + public void registerWatch(final String key, final Listener listener) { String path = PathUtil.getRealPath(rootNode, key); listener.setPath(path); context.getWatchers().put(listener.getKey(), listener); @@ -115,7 +118,7 @@ public void registerWatch(final String key, final io.shardingsphere.jdbc.orchest @Override public void unregisterWatch(final String key) { - if (io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.StringUtil.isNullOrBlank(key)) { + if (StringUtil.isNullOrBlank(key)) { throw new IllegalArgumentException("key should not be blank"); } // String path = PathUtil.getRealPath(rootNode, key); @@ -145,7 +148,7 @@ protected void createNamespace(final byte[] date) throws KeeperException, Interr rootExist = true; return; } - holder.getZooKeeper().exists(rootNode, io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.WatcherCreator.deleteWatcher(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener(rootNode) { + holder.getZooKeeper().exists(rootNode, WatcherCreator.deleteWatcher(new Listener(rootNode) { @Override public void process(final WatchedEvent event) { rootExist = false; diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseClientFactory.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseClientFactory.java index beb29148a1ef8..4a92b25518417 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseClientFactory.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseClientFactory.java @@ -18,6 +18,7 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IClient; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; @@ -35,7 +36,7 @@ public abstract class BaseClientFactory { private BaseClient client; - private io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener globalListener; + private Listener globalListener; private String namespace; diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseContext.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseContext.java index 41b4bc05103f8..8aeaa0dc352e0 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseContext.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseContext.java @@ -17,6 +17,7 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; @@ -38,9 +39,9 @@ public abstract class BaseContext { private byte[] auth; - private io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener globalListener; + private Listener globalListener; - private final Map watchers = new ConcurrentHashMap<>(); + private final Map watchers = new ConcurrentHashMap<>(); /** * close. diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseOperation.java index 450c1d173d4cf..f0fbbe4bebe39 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseOperation.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseOperation.java @@ -18,6 +18,8 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayPolicyExecutor; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Connection; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; @@ -35,12 +37,12 @@ */ @Getter(value = AccessLevel.PROTECTED) public abstract class BaseOperation implements Delayed { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation.class); + private static final Logger LOGGER = LoggerFactory.getLogger(BaseOperation.class); private final IProvider provider; @Setter - private io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayPolicyExecutor delayPolicyExecutor; + private DelayPolicyExecutor delayPolicyExecutor; protected BaseOperation(final IProvider provider) { this.provider = provider; @@ -76,7 +78,7 @@ public boolean executeOperation() throws KeeperException, InterruptedException { execute(); result = true; } catch (KeeperException e) { - if (io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Connection.needReset(e)) { + if (Connection.needReset(e)) { provider.resetConnection(); result = false; } else { diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseProvider.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseProvider.java index af55a8c6971ea..934f8613ebda2 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseProvider.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseProvider.java @@ -39,7 +39,7 @@ * @author lidongbo */ public class BaseProvider implements IProvider { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseProvider.class); + private static final Logger LOGGER = LoggerFactory.getLogger(BaseProvider.class); @Getter private final Holder holder; diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseStrategy.java index d9f3776528649..9424e576cfc98 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseStrategy.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/BaseStrategy.java @@ -17,18 +17,20 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IExecStrategy; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; import lombok.Getter; import org.apache.zookeeper.KeeperException; /* * @author lidongbo */ -public abstract class BaseStrategy implements io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IExecStrategy { +public abstract class BaseStrategy implements IExecStrategy { @Getter - private final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider; + private final IProvider provider; - public BaseStrategy(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider) { + public BaseStrategy(final IProvider provider) { this.provider = provider; } diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/Holder.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/Holder.java index 35e2edd82bd90..66f9e13e19580 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/Holder.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/Holder.java @@ -35,7 +35,7 @@ * @author lidongbo */ public class Holder { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.Holder.class); + private static final Logger LOGGER = LoggerFactory.getLogger(Holder.class); private static final CountDownLatch CONNECTED = new CountDownLatch(1); diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java index 4c5966ae9d265..d6b475a5553a9 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/DeleteAllChildrenOperation.java @@ -17,6 +17,9 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy; import org.apache.zookeeper.KeeperException; /* @@ -24,17 +27,17 @@ * * @author lidongbo */ -public class DeleteAllChildrenOperation extends io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation { +public class DeleteAllChildrenOperation extends BaseOperation { private final String key; - public DeleteAllChildrenOperation(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider, final String key) { + public DeleteAllChildrenOperation(final IProvider provider, final String key) { super(provider); this.key = key; } @Override protected void execute() throws KeeperException, InterruptedException { - new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy(getProvider()).deleteAllChildren(key); + new UsualStrategy(getProvider()).deleteAllChildren(key); } @Override diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/UpdateOperation.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/UpdateOperation.java index f485d10cf7d46..b6cdaa5012de2 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/UpdateOperation.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/operation/UpdateOperation.java @@ -17,6 +17,8 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation; import org.apache.zookeeper.KeeperException; /* @@ -24,12 +26,12 @@ * * @author lidongbo */ -public class UpdateOperation extends io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseOperation { +public class UpdateOperation extends BaseOperation { private final String key; private final String value; - public UpdateOperation(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider, final String key, final String value) { + public UpdateOperation(final IProvider provider, final String key, final String value) { super(provider); this.key = key; this.value = value; diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/Callable.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/Callable.java index 5d624a3a51c99..a961e800425ba 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/Callable.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/Callable.java @@ -17,6 +17,9 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayPolicyExecutor; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy; import lombok.Setter; import org.apache.zookeeper.KeeperException; import org.slf4j.Logger; @@ -28,17 +31,17 @@ * @author lidongbo */ public abstract class Callable { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Callable.class); + private static final Logger LOGGER = LoggerFactory.getLogger(Callable.class); - private final io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayPolicyExecutor delayPolicyExecutor; + private final DelayPolicyExecutor delayPolicyExecutor; - private final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider; + private final IProvider provider; @Setter private T result; - public Callable(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy delayRetryPolicy) { - this.delayPolicyExecutor = new io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayPolicyExecutor(delayRetryPolicy); + public Callable(final IProvider provider, final DelayRetryPolicy delayRetryPolicy) { + this.delayPolicyExecutor = new DelayPolicyExecutor(delayRetryPolicy); this.provider = provider; } @@ -76,7 +79,7 @@ public void exec() throws KeeperException, InterruptedException { } catch (KeeperException e) { LOGGER.warn("exec KeeperException:{}", e.getMessage()); delayPolicyExecutor.next(); - if (io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Connection.needReset(e)) { + if (Connection.needReset(e)) { provider.resetConnection(); } else { throw e; diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/ClientContext.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/ClientContext.java index d62010ba53d7e..8d202a52699a0 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/ClientContext.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/ClientContext.java @@ -17,6 +17,8 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseClientFactory; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseContext; import lombok.Getter; import lombok.Setter; @@ -28,9 +30,9 @@ @Getter public final class ClientContext extends BaseContext { - private io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy delayRetryPolicy; + private DelayRetryPolicy delayRetryPolicy; - private io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.BaseClientFactory clientFactory; + private BaseClientFactory clientFactory; public ClientContext(final String servers, final int sessionTimeoutMilliseconds) { super(); @@ -52,7 +54,7 @@ public void close() { * * @param context context */ - public void updateContext(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.ClientContext context) { + public void updateContext(final ClientContext context) { this.delayRetryPolicy = context.getDelayRetryPolicy(); this.clientFactory = context.clientFactory; getWatchers().clear(); diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/ClientTask.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/ClientTask.java index b0464d42dd1cf..373149ab45438 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/ClientTask.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/ClientTask.java @@ -26,7 +26,7 @@ * @author lidongbo */ public abstract class ClientTask implements Runnable { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.ClientTask.class); + private static final Logger LOGGER = LoggerFactory.getLogger(ClientTask.class); private final IProvider provider; diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/WatcherCreator.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/WatcherCreator.java index dcdf569b7b3f4..2b6505b0455c3 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/WatcherCreator.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/WatcherCreator.java @@ -26,7 +26,7 @@ * @author lidongbo */ public class WatcherCreator { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.WatcherCreator.class); + private static final Logger LOGGER = LoggerFactory.getLogger(WatcherCreator.class); /** * get string type data. @@ -34,7 +34,7 @@ public class WatcherCreator { * @param listener listener * @return watcher */ - public static Watcher deleteWatcher(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener listener) { + public static Watcher deleteWatcher(final Listener listener) { return new Watcher() { @Override public void process(final WatchedEvent event) { diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java index 47066e3a8aa8f..c34c72b01ded9 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/AsyncRetryStrategy.java @@ -18,7 +18,13 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.CreateAllNeedOperation; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.CreateCurrentOperation; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.DeleteAllChildrenOperation; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.DeleteCurrentBranchOperation; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.DeleteCurrentOperation; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.UpdateOperation; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -30,13 +36,13 @@ * * @author lidongbo */ -public class AsyncRetryStrategy extends io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.SyncRetryStrategy { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.AsyncRetryStrategy.class); +public class AsyncRetryStrategy extends SyncRetryStrategy { + private static final Logger LOGGER = LoggerFactory.getLogger(AsyncRetryStrategy.class); - public AsyncRetryStrategy(final IProvider provider, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.DelayRetryPolicy delayRetryPolicy) { + public AsyncRetryStrategy(final IProvider provider, final DelayRetryPolicy delayRetryPolicy) { super(provider, delayRetryPolicy); - io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.init(getDelayRetryPolicy()); - io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.start(); + AsyncRetryCenter.INSTANCE.init(getDelayRetryPolicy()); + AsyncRetryCenter.INSTANCE.start(); } @Override @@ -46,7 +52,7 @@ public void createCurrentOnly(final String key, final String value, final Create getProvider().create(path, value, createMode); } catch (KeeperException.SessionExpiredException e) { LOGGER.warn("AsyncRetryStrategy SessionExpiredException createCurrentOnly:{}", path); - io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.add(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.CreateCurrentOperation(getProvider(), path, value, createMode)); + AsyncRetryCenter.INSTANCE.add(new CreateCurrentOperation(getProvider(), path, value, createMode)); } } @@ -57,7 +63,7 @@ public void update(final String key, final String value) throws KeeperException, getProvider().update(path, value); } catch (KeeperException.SessionExpiredException e) { LOGGER.warn("AsyncRetryStrategy SessionExpiredException update:{}", path); - io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.add(new UpdateOperation(getProvider(), path, value)); + AsyncRetryCenter.INSTANCE.add(new UpdateOperation(getProvider(), path, value)); } } @@ -68,7 +74,7 @@ public void deleteOnlyCurrent(final String key) throws KeeperException, Interrup getProvider().delete(path); } catch (KeeperException.SessionExpiredException e) { LOGGER.warn("AsyncRetryStrategy SessionExpiredException deleteOnlyCurrent:{}", path); - io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.add(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.DeleteCurrentOperation(getProvider(), path)); + AsyncRetryCenter.INSTANCE.add(new DeleteCurrentOperation(getProvider(), path)); } } @@ -78,7 +84,7 @@ public void createAllNeedPath(final String key, final String value, final Create super.createAllNeedPath(key, value, createMode); } catch (KeeperException.SessionExpiredException e) { LOGGER.warn("AllAsyncRetryStrategy SessionExpiredException CreateAllNeedOperation:{}", key); - io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.add(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.CreateAllNeedOperation(getProvider(), key, value, createMode)); + AsyncRetryCenter.INSTANCE.add(new CreateAllNeedOperation(getProvider(), key, value, createMode)); } } @@ -88,7 +94,7 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup super.deleteAllChildren(key); } catch (KeeperException.SessionExpiredException e) { LOGGER.warn("AllAsyncRetryStrategy SessionExpiredException deleteAllChildren:{}", key); - io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.add(new DeleteAllChildrenOperation(getProvider(), key)); + AsyncRetryCenter.INSTANCE.add(new DeleteAllChildrenOperation(getProvider(), key)); } } @@ -98,7 +104,7 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr super.deleteCurrentBranch(key); } catch (KeeperException.SessionExpiredException e) { LOGGER.warn("AllAsyncRetryStrategy SessionExpiredException deleteCurrentBranch:{}", key); - io.shardingsphere.jdbc.orchestration.reg.newzk.client.retry.AsyncRetryCenter.INSTANCE.add(new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.operation.DeleteCurrentBranchOperation(getProvider(), key)); + AsyncRetryCenter.INSTANCE.add(new DeleteCurrentBranchOperation(getProvider(), key)); } } } diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/ContentionStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/ContentionStrategy.java index a5b4885b8bb3f..3b7220a6f7ca7 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/ContentionStrategy.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/ContentionStrategy.java @@ -17,6 +17,8 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.Callback; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.election.LeaderElection; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil; @@ -34,10 +36,10 @@ * * @author lidongbo */ -public class ContentionStrategy extends io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.ContentionStrategy.class); +public class ContentionStrategy extends UsualStrategy { + private static final Logger LOGGER = LoggerFactory.getLogger(ContentionStrategy.class); - public ContentionStrategy(final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider) { + public ContentionStrategy(final IProvider provider) { super(provider); } @@ -65,7 +67,7 @@ public void createCurrentOnly(final String key, final String value, final Create election.waitDone(); } - private LeaderElection buildCreateElection(final String key, final String value, final CreateMode createMode, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.Callback callback) { + private LeaderElection buildCreateElection(final String key, final String value, final CreateMode createMode, final Callback callback) { return new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { @@ -89,7 +91,7 @@ public void update(final String key, final String value) throws KeeperException, election.waitDone(); } - private LeaderElection buildUpdateElection(final String key, final String value, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.Callback callback) { + private LeaderElection buildUpdateElection(final String key, final String value, final Callback callback) { return new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { @@ -125,7 +127,7 @@ public void action() throws KeeperException, InterruptedException { }); } - private LeaderElection buildDeleteElection(final String key, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.Callback callback) { + private LeaderElection buildDeleteElection(final String key, final Callback callback) { return new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { @@ -149,7 +151,7 @@ public void createAllNeedPath(final String key, final String value, final Create election.waitDone(); } - private LeaderElection buildCreateAllNeedElection(final String key, final String value, final CreateMode createMode, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.Callback callback) { + private LeaderElection buildCreateAllNeedElection(final String key, final String value, final CreateMode createMode, final Callback callback) { return new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/SyncRetryStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/SyncRetryStrategy.java index a7135275e6832..2e235fbf1243a 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/SyncRetryStrategy.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/SyncRetryStrategy.java @@ -35,8 +35,8 @@ * * @author lidongbo */ -public class SyncRetryStrategy extends io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.SyncRetryStrategy.class); +public class SyncRetryStrategy extends UsualStrategy { + private static final Logger LOGGER = LoggerFactory.getLogger(SyncRetryStrategy.class); @Getter(value = AccessLevel.PROTECTED) private final DelayRetryPolicy delayRetryPolicy; @@ -53,7 +53,7 @@ public SyncRetryStrategy(final IProvider provider, final DelayRetryPolicy delayR @Override public byte[] getData(final String key) throws KeeperException, InterruptedException { - Callable callable = new Callable(getProvider(), delayRetryPolicy) { + Callable callable = new Callable(getProvider(), delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { setResult(getProvider().getData(getProvider().getRealPath(key))); @@ -64,7 +64,7 @@ public void call() throws KeeperException, InterruptedException { @Override public boolean checkExists(final String key) throws KeeperException, InterruptedException { - Callable callable = new Callable(getProvider(), delayRetryPolicy) { + Callable callable = new Callable(getProvider(), delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { setResult(getProvider().exists(getProvider().getRealPath(key))); @@ -75,7 +75,7 @@ public void call() throws KeeperException, InterruptedException { @Override public boolean checkExists(final String key, final Watcher watcher) throws KeeperException, InterruptedException { - Callable callable = new Callable(getProvider(), delayRetryPolicy) { + Callable callable = new Callable(getProvider(), delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { setResult(getProvider().exists(getProvider().getRealPath(key), watcher)); @@ -86,7 +86,7 @@ public void call() throws KeeperException, InterruptedException { @Override public List getChildren(final String key) throws KeeperException, InterruptedException { - Callable> callable = new Callable(getProvider(), delayRetryPolicy) { + Callable> callable = new Callable>(getProvider(), delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { setResult(getProvider().getChildren(getProvider().getRealPath(key))); @@ -133,7 +133,7 @@ public void createAllNeedPath(final String key, final String value, final Create Callable callable = new Callable(getProvider(), delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { - new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy(getProvider()).createAllNeedPath(key, value, createMode); + new UsualStrategy(getProvider()).createAllNeedPath(key, value, createMode); } }; callable.exec(); @@ -144,7 +144,7 @@ public void deleteAllChildren(final String key) throws KeeperException, Interrup Callable callable = new Callable(getProvider(), delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { - new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy(getProvider()).deleteAllChildren(key); + new UsualStrategy(getProvider()).deleteAllChildren(key); } }; callable.exec(); @@ -155,7 +155,7 @@ public void deleteCurrentBranch(final String key) throws KeeperException, Interr Callable callable = new Callable(getProvider(), delayRetryPolicy) { @Override public void call() throws KeeperException, InterruptedException { - new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy(getProvider()).deleteCurrentBranch(key); + new UsualStrategy(getProvider()).deleteCurrentBranch(key); } }; callable.exec(); diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/TransactionContendStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/TransactionContendStrategy.java index 208ede1f907bd..efcaec321694e 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/TransactionContendStrategy.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/TransactionContendStrategy.java @@ -17,6 +17,7 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.Callback; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.election.LeaderElection; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; @@ -36,7 +37,7 @@ * @since zookeeper 3.4.0 */ public class TransactionContendStrategy extends ContentionStrategy { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.TransactionContendStrategy.class); + private static final Logger LOGGER = LoggerFactory.getLogger(TransactionContendStrategy.class); public TransactionContendStrategy(final IProvider provider) { super(provider); @@ -50,7 +51,7 @@ public void createAllNeedPath(final String key, final String value, final Create election.waitDone(); } - private LeaderElection buildCreateAllNeedElection(final String key, final String value, final CreateMode createMode, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.Callback callback) { + private LeaderElection buildCreateAllNeedElection(final String key, final String value, final CreateMode createMode, final Callback callback) { return new LeaderElection() { @Override public void action() throws KeeperException, InterruptedException { diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/UsualStrategy.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/UsualStrategy.java index 762cdf47e8e2f..36ed618d1b907 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/UsualStrategy.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/strategy/UsualStrategy.java @@ -36,7 +36,7 @@ * @author lidongbo */ public class UsualStrategy extends BaseStrategy { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.strategy.UsualStrategy.class); + private static final Logger LOGGER = LoggerFactory.getLogger(UsualStrategy.class); public UsualStrategy(final IProvider provider) { super(provider); diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/transaction/ZKTransaction.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/transaction/ZKTransaction.java index e6e3d1294f056..83b6fdac174e1 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/transaction/ZKTransaction.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/transaction/ZKTransaction.java @@ -18,6 +18,8 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.Holder; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.OpResult; @@ -33,13 +35,13 @@ * @since zookeeper 3.4.0 */ public class ZKTransaction { - private static final Logger LOGGER = LoggerFactory.getLogger(io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction.class); + private static final Logger LOGGER = LoggerFactory.getLogger(ZKTransaction.class); private final Transaction transaction; private final String rootNode; - public ZKTransaction(final String root, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base.Holder holder) { + public ZKTransaction(final String root, final Holder holder) { transaction = holder.getZooKeeper().transaction(); rootNode = root; LOGGER.debug("ZKTransaction root:{}", rootNode); @@ -54,8 +56,8 @@ public ZKTransaction(final String root, final io.shardingsphere.jdbc.orchestrati * @param createMode createMode * @return ZKTransaction */ - public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction create(final String path, final byte[] data, final List acl, final CreateMode createMode) { - this.transaction.create(io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil.getRealPath(rootNode, path), data, acl, createMode); + public ZKTransaction create(final String path, final byte[] data, final List acl, final CreateMode createMode) { + this.transaction.create(PathUtil.getRealPath(rootNode, path), data, acl, createMode); LOGGER.debug("wait create:{},data:{},acl:{},createMode:{}", new Object[]{path, data, acl, createMode}); return this; } @@ -66,7 +68,7 @@ public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transacti * @param path key * @return ZKTransaction */ - public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction delete(final String path) { + public ZKTransaction delete(final String path) { return delete(path, Constants.VERSION); } @@ -77,8 +79,8 @@ public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transacti * @param version version * @return ZKTransaction */ - public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction delete(final String path, final int version) { - this.transaction.delete(io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil.getRealPath(rootNode, path), version); + public ZKTransaction delete(final String path, final int version) { + this.transaction.delete(PathUtil.getRealPath(rootNode, path), version); LOGGER.debug("wait delete:{}", path); return this; } @@ -89,7 +91,7 @@ public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transacti * @param path key * @return ZKTransaction */ - public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction check(final String path) { + public ZKTransaction check(final String path) { return check(path, Constants.VERSION); } @@ -100,8 +102,8 @@ public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transacti * @param version version * @return ZKTransaction */ - public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction check(final String path, final int version) { - this.transaction.check(io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil.getRealPath(rootNode, path), version); + public ZKTransaction check(final String path, final int version) { + this.transaction.check(PathUtil.getRealPath(rootNode, path), version); LOGGER.debug("wait check:{}", path); return this; } @@ -113,7 +115,7 @@ public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transacti * @param data data * @return ZKTransaction */ - public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction setData(final String path, final byte[] data) { + public ZKTransaction setData(final String path, final byte[] data) { return setData(path, data, Constants.VERSION); } @@ -125,8 +127,8 @@ public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transacti * @param version version * @return ZKTransaction */ - public io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction setData(final String path, final byte[] data, final int version) { - this.transaction.setData(io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil.getRealPath(rootNode, path), data, version); + public ZKTransaction setData(final String path, final byte[] data, final int version) { + this.transaction.setData(PathUtil.getRealPath(rootNode, path), data, version); LOGGER.debug("wait setData:{},data:{}", path, data); return this; } From bdf960e2c4e09dd2ee41f3ce6bbef720c9da4d98 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 17:25:57 +0800 Subject: [PATCH 158/351] for #601, add case select_or_with_none_sharding_columns --- .../src/test/resources/parser/select_or.xml | 12 +++--- .../select_or_with_none_sharding_columns.xml | 43 +++++++++++++++++++ .../select_or_with_none_sharding_columns.xml | 4 ++ .../select_or_with_none_sharding_columns.xml | 23 ++++++++++ .../cases/select/dql-integrate-test-cases.xml | 12 +++--- .../resources/integrate/assert/select_or.xml | 2 +- .../src/main/resources/sql/dql/select_or.xml | 3 +- 7 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_none_sharding_columns.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_none_sharding_columns.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_none_sharding_columns.xml diff --git a/sharding-core/src/test/resources/parser/select_or.xml b/sharding-core/src/test/resources/parser/select_or.xml index e1abc30fdbf04..dea3d107881b7 100644 --- a/sharding-core/src/test/resources/parser/select_or.xml +++ b/sharding-core/src/test/resources/parser/select_or.xml @@ -41,8 +41,8 @@ - - + +
@@ -50,7 +50,7 @@ - +
@@ -74,7 +74,7 @@ - +
@@ -117,7 +117,7 @@ - +
@@ -146,7 +146,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_none_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_none_sharding_columns.xml new file mode 100644 index 0000000000000..26b5ae8f98d09 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_none_sharding_columns.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_none_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_none_sharding_columns.xml new file mode 100644 index 0000000000000..b9054412bac9f --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_none_sharding_columns.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_none_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_none_sharding_columns.xml new file mode 100644 index 0000000000000..7f2b350c4718b --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_none_sharding_columns.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index b225ab4175bb0..e50fa8273ebce 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -265,6 +265,12 @@ + + + + + + @@ -275,12 +281,6 @@ - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml index 06194f26c8f9c..be1d41182d819 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml @@ -10,7 +10,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml index d58bcf0abfa18..910db90ae5111 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml @@ -1,10 +1,9 @@ - + - From 365ae72c90d0a6c9c1b3bad97472a65b8cfea0ac Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 17:42:56 +0800 Subject: [PATCH 159/351] for #601, add case select_or_mix_and_for_simple_pattern --- .../src/test/resources/parser/select_or.xml | 2 +- .../db/select_or_mix_and_for_simple_pattern.xml | 5 +++++ .../select_or_mix_and_for_simple_pattern.xml | 3 +++ .../tbl/select_or_mix_and_for_simple_pattern.xml | 13 +++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 12 ++++++------ .../test/resources/integrate/assert/select_or.xml | 2 +- .../src/main/resources/sql/dql/select_or.xml | 2 +- 7 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_simple_pattern.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_simple_pattern.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_simple_pattern.xml diff --git a/sharding-core/src/test/resources/parser/select_or.xml b/sharding-core/src/test/resources/parser/select_or.xml index dea3d107881b7..637b555cde30d 100644 --- a/sharding-core/src/test/resources/parser/select_or.xml +++ b/sharding-core/src/test/resources/parser/select_or.xml @@ -51,7 +51,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_simple_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_simple_pattern.xml new file mode 100644 index 0000000000000..85f2e22aa2447 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_simple_pattern.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_simple_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_simple_pattern.xml new file mode 100644 index 0000000000000..d490bba9c699e --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_simple_pattern.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_simple_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_simple_pattern.xml new file mode 100644 index 0000000000000..55e8e24a1d190 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_simple_pattern.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index e50fa8273ebce..29c1fb9c3a0ef 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -271,6 +271,12 @@ + + + + + + @@ -280,12 +286,6 @@ - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml index be1d41182d819..a242c7655b10b 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml @@ -15,7 +15,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml index 910db90ae5111..08c14d194a7fa 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml @@ -3,8 +3,8 @@ + - From ce93bf600797dac991aa6c7e8e1cc6106e9c17f4 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 17:54:08 +0800 Subject: [PATCH 160/351] for #601, add case select_or_mix_and_for_complex_pattern --- .../src/test/resources/parser/select_or.xml | 2 +- .../db/select_or_mix_and_for_complex_pattern.xml | 5 +++++ .../select_or_mix_and_for_complex_pattern.xml | 3 +++ .../tbl/select_or_mix_and_for_complex_pattern.xml | 5 +++++ .../cases/select/dql-integrate-test-cases.xml | 12 ++++++------ .../test/resources/integrate/assert/select_or.xml | 2 +- .../src/main/resources/sql/dql/select_or.xml | 2 +- 7 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_complex_pattern.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_complex_pattern.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_complex_pattern.xml diff --git a/sharding-core/src/test/resources/parser/select_or.xml b/sharding-core/src/test/resources/parser/select_or.xml index 637b555cde30d..f485a310e12c3 100644 --- a/sharding-core/src/test/resources/parser/select_or.xml +++ b/sharding-core/src/test/resources/parser/select_or.xml @@ -75,7 +75,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_complex_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_complex_pattern.xml new file mode 100644 index 0000000000000..f0ed4fd012c4c --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_complex_pattern.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_complex_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_complex_pattern.xml new file mode 100644 index 0000000000000..d490bba9c699e --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_complex_pattern.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_complex_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_complex_pattern.xml new file mode 100644 index 0000000000000..f0ed4fd012c4c --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_complex_pattern.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 29c1fb9c3a0ef..3c657d45250ec 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -277,6 +277,12 @@ + + + + + + @@ -285,12 +291,6 @@ - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml index a242c7655b10b..fa24629cd8954 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml @@ -20,7 +20,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml index 08c14d194a7fa..184a438ae5099 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml @@ -4,8 +4,8 @@ + - From 13d5991fc8c159d46a058691e3c10615aee43cf5 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 18:05:31 +0800 Subject: [PATCH 161/351] for #601, add case select_or_mix_and_for_simple_pattern_with_binding_tables --- .../src/test/resources/parser/select_or.xml | 2 +- ...ix_and_for_simple_pattern_with_binding_tables.xml | 5 +++++ ...ix_and_for_simple_pattern_with_binding_tables.xml | 5 +++++ ...ix_and_for_simple_pattern_with_binding_tables.xml | 5 +++++ .../cases/select/dql-integrate-test-cases.xml | 12 ++++++------ .../test/resources/integrate/assert/select_or.xml | 2 +- .../src/main/resources/sql/dql/select_or.xml | 2 +- 7 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_simple_pattern_with_binding_tables.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_simple_pattern_with_binding_tables.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_simple_pattern_with_binding_tables.xml diff --git a/sharding-core/src/test/resources/parser/select_or.xml b/sharding-core/src/test/resources/parser/select_or.xml index f485a310e12c3..e96be3a01fdd4 100644 --- a/sharding-core/src/test/resources/parser/select_or.xml +++ b/sharding-core/src/test/resources/parser/select_or.xml @@ -118,7 +118,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_simple_pattern_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_simple_pattern_with_binding_tables.xml new file mode 100644 index 0000000000000..bb578ceca60a7 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_simple_pattern_with_binding_tables.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_simple_pattern_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_simple_pattern_with_binding_tables.xml new file mode 100644 index 0000000000000..86a6f1975e9e7 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_simple_pattern_with_binding_tables.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_simple_pattern_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_simple_pattern_with_binding_tables.xml new file mode 100644 index 0000000000000..2be7c9dbfecc1 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_simple_pattern_with_binding_tables.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 3c657d45250ec..3e276511f5a95 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -283,6 +283,12 @@ + + + + + + @@ -290,12 +296,6 @@ - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml index fa24629cd8954..9af0d9cbd5b8b 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml @@ -25,7 +25,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml index 184a438ae5099..ce96ebe30ffb6 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml @@ -5,7 +5,7 @@ + - From aec01d8ceb32fba60d126715ee293cece9c2a311 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 18:09:23 +0800 Subject: [PATCH 162/351] for #601, select_or_mix_and_for_simple_pattern_with_binding_tables => select_or_mix_and_with_binding_tables --- sharding-core/src/test/resources/parser/select_or.xml | 2 +- ...bles.xml => select_or_mix_and_with_binding_tables.xml} | 0 ...bles.xml => select_or_mix_and_with_binding_tables.xml} | 0 ...bles.xml => select_or_mix_and_with_binding_tables.xml} | 0 .../asserts/cases/select/dql-integrate-test-cases.xml | 8 ++++---- .../src/test/resources/integrate/assert/select_or.xml | 2 +- .../src/main/resources/sql/dql/select_or.xml | 3 +-- 7 files changed, 7 insertions(+), 8 deletions(-) rename sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/{select_or_mix_and_for_simple_pattern_with_binding_tables.xml => select_or_mix_and_with_binding_tables.xml} (100%) rename sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/{select_or_mix_and_for_simple_pattern_with_binding_tables.xml => select_or_mix_and_with_binding_tables.xml} (100%) rename sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/{select_or_mix_and_for_simple_pattern_with_binding_tables.xml => select_or_mix_and_with_binding_tables.xml} (100%) diff --git a/sharding-core/src/test/resources/parser/select_or.xml b/sharding-core/src/test/resources/parser/select_or.xml index e96be3a01fdd4..95e8b7d86311d 100644 --- a/sharding-core/src/test/resources/parser/select_or.xml +++ b/sharding-core/src/test/resources/parser/select_or.xml @@ -118,7 +118,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_simple_pattern_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_simple_pattern_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_simple_pattern_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_simple_pattern_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_simple_pattern_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_simple_pattern_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 3e276511f5a95..29915a32c8f1d 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -284,10 +284,10 @@ - - - - + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml index 9af0d9cbd5b8b..3a039633ae2d0 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml @@ -25,7 +25,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml index ce96ebe30ffb6..acb7dcd0774ea 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml @@ -5,7 +5,6 @@ - - + From 9cffcabe4cc35697c0491d0eb05c223dcec7497e Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 18:33:16 +0800 Subject: [PATCH 163/351] for #601, add case select_or_mix_and_with_binding_and_broadcast_tables --- .../src/test/resources/parser/select_or.xml | 2 +- ...x_and_with_binding_and_broadcast_tables.xml | 5 +++++ ...x_and_with_binding_and_broadcast_tables.xml | 3 +++ ...x_and_with_binding_and_broadcast_tables.xml | 5 +++++ .../cases/select/dql-integrate-test-cases.xml | 18 ++++++------------ .../resources/integrate/assert/select_or.xml | 2 +- .../src/main/resources/sql/dql/select_or.xml | 2 +- 7 files changed, 22 insertions(+), 15 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_with_binding_and_broadcast_tables.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_with_binding_and_broadcast_tables.xml diff --git a/sharding-core/src/test/resources/parser/select_or.xml b/sharding-core/src/test/resources/parser/select_or.xml index 95e8b7d86311d..f88d517683648 100644 --- a/sharding-core/src/test/resources/parser/select_or.xml +++ b/sharding-core/src/test/resources/parser/select_or.xml @@ -147,7 +147,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_with_binding_and_broadcast_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_with_binding_and_broadcast_tables.xml new file mode 100644 index 0000000000000..bb578ceca60a7 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_with_binding_and_broadcast_tables.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml new file mode 100644 index 0000000000000..a3fd94d8db9be --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_with_binding_and_broadcast_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_with_binding_and_broadcast_tables.xml new file mode 100644 index 0000000000000..2be7c9dbfecc1 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_with_binding_and_broadcast_tables.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 29915a32c8f1d..7b530a0c9affe 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -289,16 +289,10 @@ - - - - - - - - - - - - + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml index 3a039633ae2d0..cb8484a4a665b 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml @@ -30,7 +30,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml index acb7dcd0774ea..dba29c8a0bad8 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_or.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_or.xml @@ -6,5 +6,5 @@ - + From 7a0b780de7cd90af01438bf75021385d0f0d5b24 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 21:51:56 +0800 Subject: [PATCH 164/351] for #601, add case select_order_by_desc --- .../test/resources/parser/select_order_by.xml | 2 +- .../dataset/db/select_order_by_desc.xml | 43 ++++++++ .../select_order_by_desc.xml | 103 ++++++++++++++++++ .../dataset/tbl/select_order_by_desc.xml | 23 ++++ .../cases/select/dql-integrate-test-cases.xml | 44 ++++++++ .../integrate/assert/select_order_by.xml | 2 +- .../resources/sql/dql/select_order_by.xml | 2 +- 7 files changed, 216 insertions(+), 3 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_desc.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_desc.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_desc.xml diff --git a/sharding-core/src/test/resources/parser/select_order_by.xml b/sharding-core/src/test/resources/parser/select_order_by.xml index ed6cf43a9349e..4337801b047ec 100644 --- a/sharding-core/src/test/resources/parser/select_order_by.xml +++ b/sharding-core/src/test/resources/parser/select_order_by.xml @@ -1,6 +1,6 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_desc.xml new file mode 100644 index 0000000000000..bad2d7e8d2902 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_desc.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_desc.xml new file mode 100644 index 0000000000000..4ea69af20e5b6 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_desc.xml @@ -0,0 +1,103 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_desc.xml new file mode 100644 index 0000000000000..83db0bee2977c --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_desc.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 7b530a0c9affe..0a73d6da077fc 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -295,4 +295,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml index 080948bc9f454..abbafc61151a1 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml @@ -1,6 +1,6 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml b/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml index e7368b9b477e0..73c65885295b9 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml @@ -1,6 +1,6 @@ - + From aaf39178139f84410228d83e8fda55c0714a1aed Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 22:22:16 +0800 Subject: [PATCH 165/351] for #601, add case select_order_by_desc_and_index_asc --- .../test/resources/parser/select_order_by.xml | 2 +- .../db/select_order_by_desc_and_index_asc.xml | 83 +++++++++++++++++++ .../select_order_by_desc_and_index_asc.xml | 3 + .../select_order_by_desc_and_index_asc.xml | 43 ++++++++++ .../cases/select/dql-integrate-test-cases.xml | 10 ++- .../integrate/assert/select_order_by.xml | 2 +- .../resources/sql/dql/select_order_by.xml | 3 +- 7 files changed, 140 insertions(+), 6 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_desc_and_index_asc.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_desc_and_index_asc.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_desc_and_index_asc.xml diff --git a/sharding-core/src/test/resources/parser/select_order_by.xml b/sharding-core/src/test/resources/parser/select_order_by.xml index 4337801b047ec..83bc3f082bd20 100644 --- a/sharding-core/src/test/resources/parser/select_order_by.xml +++ b/sharding-core/src/test/resources/parser/select_order_by.xml @@ -13,7 +13,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_desc_and_index_asc.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_desc_and_index_asc.xml new file mode 100644 index 0000000000000..80babf3e91749 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_desc_and_index_asc.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_desc_and_index_asc.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_desc_and_index_asc.xml new file mode 100644 index 0000000000000..a3fd94d8db9be --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_desc_and_index_asc.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_desc_and_index_asc.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_desc_and_index_asc.xml new file mode 100644 index 0000000000000..2115843cd5d92 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_desc_and_index_asc.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 0a73d6da077fc..b487e4705ef2f 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -301,6 +301,12 @@ + + + + + + @@ -310,9 +316,7 @@ - - - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml index abbafc61151a1..61127bbf00b10 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml @@ -5,7 +5,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml b/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml index 73c65885295b9..f028aecc511a5 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml @@ -1,7 +1,8 @@ - + + From 405f8ab4f14fc9a92da3e2a84c4b1e6f6b8244f6 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 22:25:35 +0800 Subject: [PATCH 166/351] for #601, select_order_by_desc => select_order_by_desc_and_index_asc --- .../src/test/resources/parser/select_order_by.xml | 2 +- ...by_desc.xml => select_order_by_asc_and_index_desc.xml} | 0 ...by_desc.xml => select_order_by_asc_and_index_desc.xml} | 0 ...by_desc.xml => select_order_by_asc_and_index_desc.xml} | 0 .../asserts/cases/select/dql-integrate-test-cases.xml | 8 ++++---- .../test/resources/integrate/assert/select_order_by.xml | 2 +- .../src/main/resources/sql/dql/select_order_by.xml | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) rename sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/{select_order_by_desc.xml => select_order_by_asc_and_index_desc.xml} (100%) rename sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/{select_order_by_desc.xml => select_order_by_asc_and_index_desc.xml} (100%) rename sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/{select_order_by_desc.xml => select_order_by_asc_and_index_desc.xml} (100%) diff --git a/sharding-core/src/test/resources/parser/select_order_by.xml b/sharding-core/src/test/resources/parser/select_order_by.xml index 83bc3f082bd20..0da8386dc38f7 100644 --- a/sharding-core/src/test/resources/parser/select_order_by.xml +++ b/sharding-core/src/test/resources/parser/select_order_by.xml @@ -1,6 +1,6 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_asc_and_index_desc.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_desc.xml rename to sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_asc_and_index_desc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_asc_and_index_desc.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_desc.xml rename to sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_asc_and_index_desc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_asc_and_index_desc.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_desc.xml rename to sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_asc_and_index_desc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index b487e4705ef2f..4dc87ec361dfb 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -296,10 +296,10 @@ - - - - + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml index 61127bbf00b10..3d270700db18e 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml @@ -1,6 +1,6 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml b/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml index f028aecc511a5..7e9a1791702fd 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml @@ -1,6 +1,6 @@ - + From 0e193803ea28c59860e90484d9f6e718342d7762 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 22:45:43 +0800 Subject: [PATCH 167/351] for #601, add case select_order_by_with_ordered_column --- .../test/resources/parser/select_order_by.xml | 2 +- .../select_order_by_with_ordered_column.xml | 43 ++++++++ .../select_order_by_with_ordered_column.xml | 103 ++++++++++++++++++ .../select_order_by_with_ordered_column.xml | 23 ++++ .../cases/select/dql-integrate-test-cases.xml | 19 ++-- .../integrate/assert/select_order_by.xml | 2 +- .../resources/sql/dql/select_order_by.xml | 2 +- 7 files changed, 179 insertions(+), 15 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_with_ordered_column.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_with_ordered_column.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_with_ordered_column.xml diff --git a/sharding-core/src/test/resources/parser/select_order_by.xml b/sharding-core/src/test/resources/parser/select_order_by.xml index 0da8386dc38f7..d72a10ed6ba63 100644 --- a/sharding-core/src/test/resources/parser/select_order_by.xml +++ b/sharding-core/src/test/resources/parser/select_order_by.xml @@ -28,7 +28,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_with_ordered_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_with_ordered_column.xml new file mode 100644 index 0000000000000..0ee55c37fe619 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_with_ordered_column.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_with_ordered_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_with_ordered_column.xml new file mode 100644 index 0000000000000..0e74be7cb34a2 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_with_ordered_column.xml @@ -0,0 +1,103 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_with_ordered_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_with_ordered_column.xml new file mode 100644 index 0000000000000..d6adadea0698d --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_with_ordered_column.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 4dc87ec361dfb..499f153d61416 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -307,6 +307,12 @@ + + + + + + @@ -314,18 +320,7 @@ - - - - - - - - - - - - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml index 3d270700db18e..888485f1a7c39 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml @@ -10,7 +10,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml b/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml index 7e9a1791702fd..be1cdbcfbc696 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml @@ -2,8 +2,8 @@ + - From bebd77e69159412ad4618a6e1b7be7cd4c745af8 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 23:03:21 +0800 Subject: [PATCH 168/351] for #601, add case select_order_by_with_date --- .../test/resources/parser/select_order_by.xml | 6 +- .../dataset/db/select_order_by_with_date.xml | 83 +++++++++++++++++++ .../select_order_by_with_date.xml | 3 + .../dataset/tbl/select_order_by_with_date.xml | 43 ++++++++++ .../cases/select/dql-integrate-test-cases.xml | 14 ++-- .../integrate/assert/select_order_by.xml | 2 +- .../resources/sql/dql/select_order_by.xml | 2 +- 7 files changed, 141 insertions(+), 12 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_with_date.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_with_date.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_with_date.xml diff --git a/sharding-core/src/test/resources/parser/select_order_by.xml b/sharding-core/src/test/resources/parser/select_order_by.xml index d72a10ed6ba63..8789e2ddd5e64 100644 --- a/sharding-core/src/test/resources/parser/select_order_by.xml +++ b/sharding-core/src/test/resources/parser/select_order_by.xml @@ -39,8 +39,8 @@ - - + +
@@ -50,7 +50,7 @@ - + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_with_date.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_with_date.xml new file mode 100644 index 0000000000000..80babf3e91749 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_with_date.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_with_date.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_with_date.xml new file mode 100644 index 0000000000000..a3fd94d8db9be --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_with_date.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_with_date.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_with_date.xml new file mode 100644 index 0000000000000..2115843cd5d92 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_with_date.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 499f153d61416..9a415c437fb4b 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -313,19 +313,19 @@ - - + + + + + + - - - - - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml index 888485f1a7c39..1da93cac7cbdb 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml @@ -15,7 +15,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml b/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml index be1cdbcfbc696..018ce47de8995 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml @@ -3,8 +3,8 @@ + - From d1e1fbbef8bf6fc19a095f658f0cbd5bc5a66264 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 23:08:19 +0800 Subject: [PATCH 169/351] for #601, add case select_order_by_for_nulls_first --- .../src/test/resources/parser/select_order_by.xml | 2 +- .../asserts/cases/select/dql-integrate-test-cases.xml | 10 +++++++--- .../resources/integrate/assert/select_order_by.xml | 2 +- .../src/main/resources/sql/dql/select_order_by.xml | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/sharding-core/src/test/resources/parser/select_order_by.xml b/sharding-core/src/test/resources/parser/select_order_by.xml index 8789e2ddd5e64..820f7b4b195be 100644 --- a/sharding-core/src/test/resources/parser/select_order_by.xml +++ b/sharding-core/src/test/resources/parser/select_order_by.xml @@ -57,7 +57,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 9a415c437fb4b..1080c77edc4cb 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -319,15 +319,19 @@ - - + + + + + + - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml index 1da93cac7cbdb..4b55b756fc24e 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml @@ -21,7 +21,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml b/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml index 018ce47de8995..0e83bf582a81c 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml @@ -4,7 +4,7 @@ + - From d0d78114a861cb40e44033174c9647d64df0ad3d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 23:10:24 +0800 Subject: [PATCH 170/351] for #601, add case select_order_by_for_nulls_last --- .../test/resources/parser/select_order_by.xml | 2 +- .../cases/select/dql-integrate-test-cases.xml | 25 ++++++------------- .../integrate/assert/select_order_by.xml | 2 +- .../resources/sql/dql/select_order_by.xml | 3 +-- 4 files changed, 11 insertions(+), 21 deletions(-) diff --git a/sharding-core/src/test/resources/parser/select_order_by.xml b/sharding-core/src/test/resources/parser/select_order_by.xml index 820f7b4b195be..3ae3db596444c 100644 --- a/sharding-core/src/test/resources/parser/select_order_by.xml +++ b/sharding-core/src/test/resources/parser/select_order_by.xml @@ -70,7 +70,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 1080c77edc4cb..903faf19f34aa 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -319,27 +319,18 @@ - + + - - - - - - - - - - - - - - - - + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml index 4b55b756fc24e..d72e845946fbe 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml @@ -27,7 +27,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml b/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml index 0e83bf582a81c..514ed2889912f 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_order_by.xml @@ -5,6 +5,5 @@ - - + From 7e2f8689a537ded06de9e4943ded32c5275d1978 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 11 Jun 2018 23:24:57 +0800 Subject: [PATCH 171/351] for #601, add case select_pagination_with_offset --- .../resources/parser/select_pagination.xml | 2 +- .../db/select_pagination_with_offset.xml | 5 + .../select_pagination_with_offset.xml | 5 + .../tbl/select_pagination_with_offset.xml | 5 + .../cases/select/dql-integrate-test-cases.xml | 100 +++++++++++++++++- .../integrate/assert/select_pagination.xml | 2 +- .../resources/sql/dql/select_pagination.xml | 3 +- 7 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset.xml diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index 0400afe0ea617..a6e17caa3d28f 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -1,6 +1,6 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset.xml new file mode 100644 index 0000000000000..2c579de734cc7 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset.xml new file mode 100644 index 0000000000000..b837c2c848016 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset.xml new file mode 100644 index 0000000000000..5e06db1b0c468 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 903faf19f34aa..f87ebe16f7622 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -319,18 +319,114 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index 6d0fac477d3ec..21f18bee58199 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -1,6 +1,6 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index 6ddb9f08f6e30..0b2c9026b80ac 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -1,6 +1,7 @@ - + + From 9a2977e9526d6f69f42e3da387f775f5a6e291d1 Mon Sep 17 00:00:00 2001 From: tuohai666 Date: Tue, 12 Jun 2018 09:59:56 +0800 Subject: [PATCH 172/351] for #888, missed Epoll mode --- .../java/io/shardingsphere/proxy/frontend/ShardingProxy.java | 1 + 1 file changed, 1 insertion(+) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java index 65d7abe754817..25ff8fb3a8aff 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/ShardingProxy.java @@ -87,6 +87,7 @@ private void groupsEpoll(final ServerBootstrap bootstrap) { bootstrap.group(bossGroup, workerGroup) .channel(EpollServerSocketChannel.class) .option(EpollChannelOption.SO_BACKLOG, 128) + .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024 * 1024, 16 * 1024 * 1024)) .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)) From a095eb5d2e5a56a07118975706cfbf9dd131151d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 11:31:26 +0800 Subject: [PATCH 173/351] for #601, add case select_pagination_with_row_count --- .../src/test/resources/parser/select_pagination.xml | 2 +- .../dataset/db/select_pagination_with_row_count.xml | 5 +++++ .../select_pagination_with_row_count.xml | 5 +++++ .../dataset/tbl/select_pagination_with_row_count.xml | 5 +++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 10 +++++++--- .../resources/integrate/assert/select_pagination.xml | 2 +- .../src/main/resources/sql/dql/select_pagination.xml | 2 +- 7 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_count.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_count.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_count.xml diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index a6e17caa3d28f..9632f86cda98f 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -28,7 +28,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_count.xml new file mode 100644 index 0000000000000..871c7f9012d2e --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_count.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_count.xml new file mode 100644 index 0000000000000..15880ca21588f --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_count.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_count.xml new file mode 100644 index 0000000000000..aaaca0380603a --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_count.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index f87ebe16f7622..7c0b7585fb8bb 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -339,6 +339,12 @@ + + + + + + @@ -347,9 +353,7 @@ - - - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index 21f18bee58199..8ff13d4e63feb 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -11,7 +11,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index 0b2c9026b80ac..6bd7a85fad326 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -1,8 +1,8 @@ + - From a832429a1476645be49483783b9b8a31dcdd8120 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 11:38:00 +0800 Subject: [PATCH 174/351] for #601, add case select_pagination_with_top --- .../resources/parser/select_pagination.xml | 2 +- .../cases/select/dql-integrate-test-cases.xml | 25 ++++++------------- .../integrate/assert/select_pagination.xml | 2 +- .../resources/sql/dql/select_pagination.xml | 2 +- 4 files changed, 11 insertions(+), 20 deletions(-) diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index 9632f86cda98f..7fc27d444c706 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -100,7 +100,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 7c0b7585fb8bb..41188610f78ff 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -345,19 +345,14 @@ + + + + + + + - - - - - - - - - - - - @@ -378,11 +373,7 @@ - - - - - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index 8ff13d4e63feb..7ae49cab16eb6 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -36,7 +36,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index 6bd7a85fad326..b87d1e3b02f52 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -2,8 +2,8 @@ + - From 69016c373a7407c984bc5426fe942b620c77ad18 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 11:40:26 +0800 Subject: [PATCH 175/351] for #601, add case select_pagination_with_row_number --- .../test/resources/parser/select_pagination.xml | 2 +- .../cases/select/dql-integrate-test-cases.xml | 15 ++++++++++----- .../integrate/assert/select_pagination.xml | 2 +- .../main/resources/sql/dql/select_pagination.xml | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index 7fc27d444c706..d8a9fa8e5dc0d 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -235,7 +235,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 41188610f78ff..102aa24218227 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -352,6 +352,15 @@ + + + + + + + + + @@ -363,11 +372,7 @@ - - - - - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index 7ae49cab16eb6..d074a797cc22d 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -26,7 +26,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index b87d1e3b02f52..204e4a8153260 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -3,8 +3,8 @@ + - From e3404873ab2d00c0e869b574ae08acad24d7deb2 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 12:09:30 +0800 Subject: [PATCH 176/351] for #601, add case select_pagination_with_limit --- .../src/test/resources/parser/select_pagination.xml | 2 +- .../select_pagination_with_offset_and_row_count.xml | 5 +++++ .../select_pagination_with_offset_and_row_count.xml | 5 +++++ .../select_pagination_with_offset_and_row_count.xml | 5 +++++ .../cases/select/dql-integrate-test-cases.xml | 12 ++++++------ .../resources/integrate/assert/select_pagination.xml | 2 +- .../src/main/resources/sql/dql/select_pagination.xml | 3 ++- 7 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset_and_row_count.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_row_count.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset_and_row_count.xml diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index d8a9fa8e5dc0d..63e5127f1049f 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -56,7 +56,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset_and_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset_and_row_count.xml new file mode 100644 index 0000000000000..0dbd4a4027001 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset_and_row_count.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_row_count.xml new file mode 100644 index 0000000000000..572071bb99245 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_row_count.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset_and_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset_and_row_count.xml new file mode 100644 index 0000000000000..abda39ed5e1f4 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset_and_row_count.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 102aa24218227..fe0816bc72c69 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -358,15 +358,15 @@ + + + + + + - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index d074a797cc22d..73430de544a46 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -16,7 +16,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index 204e4a8153260..ebc60a333cb7c 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -5,7 +5,8 @@ - + + From ee0508b618ab631c15e9228c87e2054727878e2f Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 12:11:28 +0800 Subject: [PATCH 177/351] for #601, remove case assertSelectSingleTablePaginationWithLimit --- .../test/resources/parser/select_pagination.xml | 15 --------------- .../cases/select/dql-integrate-test-cases.xml | 7 +------ .../integrate/assert/select_pagination.xml | 5 ----- .../main/resources/sql/dql/select_pagination.xml | 2 -- 4 files changed, 1 insertion(+), 28 deletions(-) diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index 63e5127f1049f..dd7e342033239 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -85,21 +85,6 @@ - - -
- - - - - - - - - - - -
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index fe0816bc72c69..e11d10722a27d 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -366,12 +366,7 @@ - - - - - - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index 73430de544a46..28eab41c266c7 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -21,11 +21,6 @@ - - - - - diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index ebc60a333cb7c..ed12044406f67 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -4,10 +4,8 @@ - - From f5c84f28821da5b90c1978c16effe1f0a2638274 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 12:21:26 +0800 Subject: [PATCH 178/351] for #601, add case select_pagination_with_offset_and_limit --- .../src/test/resources/parser/select_pagination.xml | 2 +- .../db/select_pagination_with_offset_and_limit.xml | 5 +++++ .../select_pagination_with_offset_and_limit.xml | 5 +++++ .../tbl/select_pagination_with_offset_and_limit.xml | 5 +++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 10 +++++++--- .../resources/integrate/assert/select_pagination.xml | 2 +- .../src/main/resources/sql/dql/select_pagination.xml | 2 +- 7 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset_and_limit.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_limit.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset_and_limit.xml diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index dd7e342033239..390362eca0ee1 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -113,7 +113,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset_and_limit.xml new file mode 100644 index 0000000000000..5f1b25274cd25 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset_and_limit.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_limit.xml new file mode 100644 index 0000000000000..a856419245706 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_limit.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset_and_limit.xml new file mode 100644 index 0000000000000..9fc337a3d8020 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset_and_limit.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index e11d10722a27d..27508a748b585 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -363,12 +363,16 @@ - - + + + + + + - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index 28eab41c266c7..27eb7336544f6 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -26,7 +26,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index ed12044406f67..2de22b094f612 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -5,8 +5,8 @@ + - From e733a04d219555ee771bcd8ef8f49dd28201c37b Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 12:27:46 +0800 Subject: [PATCH 179/351] for #601, add case select_pagination_with_top_for_range --- .../test/resources/parser/select_pagination.xml | 2 +- .../db/select_pagination_with_top_for_range.xml | 5 +++++ .../select_pagination_with_top_for_range.xml | 5 +++++ .../tbl/select_pagination_with_top_for_range.xml | 5 +++++ .../cases/select/dql-integrate-test-cases.xml | 15 ++++++++------- .../integrate/assert/select_pagination.xml | 2 +- .../main/resources/sql/dql/select_pagination.xml | 2 +- 7 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_top_for_range.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_top_for_range.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_top_for_range.xml diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index 390362eca0ee1..203b9c7127419 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -142,7 +142,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_top_for_range.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_top_for_range.xml new file mode 100644 index 0000000000000..0dbd4a4027001 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_top_for_range.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_top_for_range.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_top_for_range.xml new file mode 100644 index 0000000000000..572071bb99245 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_top_for_range.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_top_for_range.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_top_for_range.xml new file mode 100644 index 0000000000000..abda39ed5e1f4 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_top_for_range.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 27508a748b585..f0b54f622605f 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -363,22 +363,23 @@ - + + + + + + + - - - - - - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index 27eb7336544f6..ab8025ef7b4a2 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -36,7 +36,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index 2de22b094f612..2b3dd87c23121 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -6,8 +6,8 @@ + - From 4b670fea64d7dd760c727c01a30c73d2073f96df Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 12:34:20 +0800 Subject: [PATCH 180/351] for #601, select_pagination_with_top_for_range => select_pagination_with_top_for_greater_than --- .../src/test/resources/parser/select_pagination.xml | 2 +- ...=> select_pagination_with_top_for_greater_than.xml} | 0 ...=> select_pagination_with_top_for_greater_than.xml} | 0 ...=> select_pagination_with_top_for_greater_than.xml} | 0 .../asserts/cases/select/dql-integrate-test-cases.xml | 10 +++++----- .../resources/integrate/assert/select_pagination.xml | 2 +- .../src/main/resources/sql/dql/select_pagination.xml | 3 ++- 7 files changed, 9 insertions(+), 8 deletions(-) rename sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/{select_pagination_with_top_for_range.xml => select_pagination_with_top_for_greater_than.xml} (100%) rename sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/{select_pagination_with_top_for_range.xml => select_pagination_with_top_for_greater_than.xml} (100%) rename sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/{select_pagination_with_top_for_range.xml => select_pagination_with_top_for_greater_than.xml} (100%) diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index 203b9c7127419..32981def280b1 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -142,7 +142,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_top_for_range.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_top_for_greater_than.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_top_for_range.xml rename to sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_top_for_greater_than.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_top_for_range.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_top_for_greater_than.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_top_for_range.xml rename to sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_top_for_greater_than.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_top_for_range.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_top_for_greater_than.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_top_for_range.xml rename to sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_top_for_greater_than.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index f0b54f622605f..b0131a1475aa8 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -370,16 +370,16 @@ - - - - + + + + - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index ab8025ef7b4a2..275116bacea85 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -36,7 +36,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index 2b3dd87c23121..07c0cf8acbed2 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -6,9 +6,10 @@ - + + From 0ac7db3d1fba99a9dcb82c90e341a68002df74b9 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 12:39:02 +0800 Subject: [PATCH 181/351] for #601, add case select_pagination_with_top_for_greater_than_and_equal --- .../resources/parser/select_pagination.xml | 2 +- ... select_pagination_with_top_and_limit.xml} | 0 ... select_pagination_with_top_and_limit.xml} | 0 ... select_pagination_with_top_and_limit.xml} | 0 .../cases/select/dql-integrate-test-cases.xml | 24 ++++++++----------- .../integrate/assert/select_pagination.xml | 2 +- .../resources/sql/dql/select_pagination.xml | 3 +-- 7 files changed, 13 insertions(+), 18 deletions(-) rename sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/{select_pagination_with_top_for_greater_than.xml => select_pagination_with_top_and_limit.xml} (100%) rename sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/{select_pagination_with_top_for_greater_than.xml => select_pagination_with_top_and_limit.xml} (100%) rename sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/{select_pagination_with_top_for_greater_than.xml => select_pagination_with_top_and_limit.xml} (100%) diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index 32981def280b1..ba147401a3eda 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -171,7 +171,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_top_for_greater_than.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_top_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_top_for_greater_than.xml rename to sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_top_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_top_for_greater_than.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_top_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_top_for_greater_than.xml rename to sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_top_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_top_for_greater_than.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_top_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_top_for_greater_than.xml rename to sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_top_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index b0131a1475aa8..fb5b53ffbdb5c 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -371,29 +371,25 @@ - - - + + + + + + + + + - - - - - - - - - - - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index 275116bacea85..30b4304026073 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -46,7 +46,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index 07c0cf8acbed2..1e3443000f501 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -7,8 +7,7 @@ - - + From 1e0f72119e9772f36fe8d60c2dcec68f7538462d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 12:41:28 +0800 Subject: [PATCH 182/351] for #601, remove case assertSelectSingleTablePaginationWithTopAndRange --- .../resources/parser/select_pagination.xml | 20 ------------------- .../cases/select/dql-integrate-test-cases.xml | 7 +------ .../integrate/assert/select_pagination.xml | 5 ----- .../resources/sql/dql/select_pagination.xml | 1 - 4 files changed, 1 insertion(+), 32 deletions(-) diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index ba147401a3eda..59b5ed76d5451 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -200,26 +200,6 @@ - - -
- - - - - - o.order_id AS ORDER_BY_DERIVED_0 - - - - - - - - - - -
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index fb5b53ffbdb5c..d021b6b5efc58 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -383,12 +383,7 @@ - - - - - - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index 30b4304026073..81ba095b9596d 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -41,11 +41,6 @@ - - - - - diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index 1e3443000f501..0d297507f561e 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -9,7 +9,6 @@ - From 507bb436ae34d86203555016cbca6c5ef4bf05d0 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 12:42:26 +0800 Subject: [PATCH 183/351] for #601, remove case assertSelectSingleTablePaginationWithTopAndRangeWithEqual --- .../resources/parser/select_pagination.xml | 20 ------------------- .../cases/select/dql-integrate-test-cases.xml | 7 +------ .../integrate/assert/select_pagination.xml | 5 ----- .../resources/sql/dql/select_pagination.xml | 1 - 4 files changed, 1 insertion(+), 32 deletions(-) diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index 59b5ed76d5451..598d4da399132 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -346,26 +346,6 @@ - - -
- - - - - - o.order_id AS ORDER_BY_DERIVED_0 - - - - - - - - - - -
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index d021b6b5efc58..68b357280d9bb 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -384,12 +384,7 @@ - - - - - - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index 81ba095b9596d..23a4b69273f0b 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -46,11 +46,6 @@ - - - - - diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index 0d297507f561e..544a5247851cd 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -9,7 +9,6 @@ - From ce73e3d0f944d2c9eeee084492937018ba2b6f56 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 12:50:33 +0800 Subject: [PATCH 184/351] for #601, add case select_pagination_with_row_number_for_greater_than --- .../src/test/resources/parser/select_pagination.xml | 2 +- .../db/select_pagination_with_row_number_and_limit.xml | 5 +++++ .../select_pagination_with_row_number_and_limit.xml | 5 +++++ .../select_pagination_with_row_number_and_limit.xml | 5 +++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 10 +++++++--- .../resources/integrate/assert/select_pagination.xml | 2 +- .../src/main/resources/sql/dql/select_pagination.xml | 2 +- 7 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_number_and_limit.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_number_and_limit.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_number_and_limit.xml diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index 598d4da399132..b2a4c646c6798 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -233,7 +233,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_number_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_number_and_limit.xml new file mode 100644 index 0000000000000..463c8a1bf0f96 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_number_and_limit.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_number_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_number_and_limit.xml new file mode 100644 index 0000000000000..eebe75b4da717 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_number_and_limit.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_number_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_number_and_limit.xml new file mode 100644 index 0000000000000..6af522666fa52 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_number_and_limit.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 68b357280d9bb..ad28111236887 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -381,11 +381,15 @@ - - + + + + + + - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index 23a4b69273f0b..7408e563ec844 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -46,7 +46,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index 544a5247851cd..e41805ca53fa4 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -8,8 +8,8 @@ + - From 240fc81a03af876aa1b9a4f500ef21cc6d14839d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 12:51:59 +0800 Subject: [PATCH 185/351] for #601, add case select_pagination_with_row_number_for_greater_than_and_equal --- .../test/resources/parser/select_pagination.xml | 2 +- .../cases/select/dql-integrate-test-cases.xml | 16 ++++++---------- .../integrate/assert/select_pagination.xml | 2 +- .../main/resources/sql/dql/select_pagination.xml | 3 ++- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index b2a4c646c6798..9f81d76666979 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -267,7 +267,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index ad28111236887..75f271eda385c 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -388,17 +388,13 @@ + + + + + - - - - - - - - - - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index 7408e563ec844..6296b963f6b5b 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -51,7 +51,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index e41805ca53fa4..419b578eedce4 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -10,7 +10,8 @@ - + + From cd2141a11d61f1ba0429f70855aad5522530fd6d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 12:55:41 +0800 Subject: [PATCH 186/351] for #601, remove case assertSelectSingleTablePaginationWithRowNumberAndRange --- .../resources/parser/select_pagination.xml | 45 ------------------- .../cases/select/dql-integrate-test-cases.xml | 16 +------ .../integrate/assert/select_pagination.xml | 15 ------- .../resources/sql/dql/select_pagination.xml | 4 -- 4 files changed, 1 insertion(+), 79 deletions(-) diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index 9f81d76666979..c93c0f8092e60 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -301,51 +301,6 @@ - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - -
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 75f271eda385c..1d0f8e003af16 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -395,21 +395,7 @@ - - - - - - - - - - - - - - - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index 6296b963f6b5b..1f37c3f51dc7a 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -56,21 +56,6 @@ - - - - - - - - - - - - - - - diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index 419b578eedce4..cb3dedb5450cc 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -9,11 +9,7 @@ - - - - From 186e8491ff314055968e316a43b539976ca47a2d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 14:18:13 +0800 Subject: [PATCH 187/351] for #601, remove case assertSelectLeftJoinAndLimit --- .../resources/parser/select_pagination.xml | 27 ------------------- .../cases/select/dql-integrate-test-cases.xml | 8 ------ .../integrate/assert/select_pagination.xml | 5 ---- .../resources/sql/dql/select_pagination.xml | 2 -- 4 files changed, 42 deletions(-) diff --git a/sharding-core/src/test/resources/parser/select_pagination.xml b/sharding-core/src/test/resources/parser/select_pagination.xml index c93c0f8092e60..0f283c38fe28c 100644 --- a/sharding-core/src/test/resources/parser/select_pagination.xml +++ b/sharding-core/src/test/resources/parser/select_pagination.xml @@ -300,31 +300,4 @@ - - - -
-
- - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 1d0f8e003af16..73f7874478a5b 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -393,12 +393,4 @@ - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml index 1f37c3f51dc7a..b27b014ffa700 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml @@ -56,9 +56,4 @@ - - - - - diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml index cb3dedb5450cc..4fc76fcb0489c 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_pagination.xml @@ -10,6 +10,4 @@ - - From 570cacc5ecbe61c7471d3b84a19a18f52cf4fec8 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 14:37:07 +0800 Subject: [PATCH 188/351] for #601, add case select_inner_join_related_with_alias --- .../test/resources/parser/select_relation.xml | 2 +- .../select/dataset/db/select_inner_join.xml | 5 ++++ .../select_inner_join.xml | 5 ++++ .../select/dataset/tbl/select_inner_join.xml | 5 ++++ .../cases/select/dql-integrate-test-cases.xml | 25 +++++++++++++++++++ .../integrate/assert/select_relation.xml | 2 +- .../resources/sql/dql/select_relation.xml | 2 +- 7 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_inner_join.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_inner_join.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_inner_join.xml diff --git a/sharding-core/src/test/resources/parser/select_relation.xml b/sharding-core/src/test/resources/parser/select_relation.xml index 7ce3ebcce7f49..e1fbbde1403be 100644 --- a/sharding-core/src/test/resources/parser/select_relation.xml +++ b/sharding-core/src/test/resources/parser/select_relation.xml @@ -1,6 +1,6 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_inner_join.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_inner_join.xml new file mode 100644 index 0000000000000..6c4c9ebfb4908 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_inner_join.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_inner_join.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_inner_join.xml new file mode 100644 index 0000000000000..3aec4a0a9797d --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_inner_join.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_inner_join.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_inner_join.xml new file mode 100644 index 0000000000000..6c72b54477a13 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_inner_join.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 73f7874478a5b..e57e6e699072e 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -393,4 +393,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml index 69116d857248f..6f7a67c39e396 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml @@ -1,6 +1,6 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_relation.xml b/sharding-sql-test/src/main/resources/sql/dql/select_relation.xml index c0932edb07845..061bdcd79cc3f 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_relation.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_relation.xml @@ -1,6 +1,6 @@ - + From b79aab0fabf0a03100b4a681f86ed94df2d0b6c6 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 14:39:18 +0800 Subject: [PATCH 189/351] for #601, add case select_inner_join_related_with_name --- .../src/test/resources/parser/select_relation.xml | 2 +- .../asserts/cases/select/dql-integrate-test-cases.xml | 9 +++++++-- .../test/resources/integrate/assert/select_relation.xml | 2 +- .../src/main/resources/sql/dql/select_relation.xml | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/sharding-core/src/test/resources/parser/select_relation.xml b/sharding-core/src/test/resources/parser/select_relation.xml index e1fbbde1403be..80e1f567dec0a 100644 --- a/sharding-core/src/test/resources/parser/select_relation.xml +++ b/sharding-core/src/test/resources/parser/select_relation.xml @@ -18,7 +18,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index e57e6e699072e..4bd800aa4e551 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -399,7 +399,12 @@ - + + + + + + @@ -408,7 +413,7 @@ - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml index 6f7a67c39e396..ecd42308f03de 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml @@ -5,7 +5,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_relation.xml b/sharding-sql-test/src/main/resources/sql/dql/select_relation.xml index 061bdcd79cc3f..de1eb5864220e 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_relation.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_relation.xml @@ -1,6 +1,6 @@ - + From 28e318aaf1cc274e8d7960e499214a28e3091b7a Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 14:48:49 +0800 Subject: [PATCH 190/351] for #601, add case select_join_using --- .../test/resources/parser/select_relation.xml | 2 +- .../cases/select/dql-integrate-test-cases.xml | 24 +++++-------------- .../integrate/assert/select_relation.xml | 2 +- .../resources/sql/dql/select_relation.xml | 2 +- 4 files changed, 9 insertions(+), 21 deletions(-) diff --git a/sharding-core/src/test/resources/parser/select_relation.xml b/sharding-core/src/test/resources/parser/select_relation.xml index 80e1f567dec0a..e57c88b6f7f3e 100644 --- a/sharding-core/src/test/resources/parser/select_relation.xml +++ b/sharding-core/src/test/resources/parser/select_relation.xml @@ -40,7 +40,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 4bd800aa4e551..d2de50aa6b282 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -405,22 +405,10 @@ - - - - - - - - - - - - - - - - - - + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml index ecd42308f03de..58cd7083a0e68 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml @@ -10,7 +10,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_relation.xml b/sharding-sql-test/src/main/resources/sql/dql/select_relation.xml index de1eb5864220e..1eb880a595504 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_relation.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_relation.xml @@ -2,5 +2,5 @@ - + From fbc8033e42b08a99ed2192fb2d4940cf311eab3f Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 15:13:22 +0800 Subject: [PATCH 191/351] for #601, fix for psql --- .../dbtest/env/datasource/DatabaseEnvironment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/datasource/DatabaseEnvironment.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/datasource/DatabaseEnvironment.java index bb388d670946c..c82fde76cc4b5 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/datasource/DatabaseEnvironment.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/datasource/DatabaseEnvironment.java @@ -75,7 +75,7 @@ public String getURL() { case MySQL: return String.format("jdbc:mysql://%s:%s", host, port); case PostgreSQL: - return String.format("jdbc:postgresql://%s:%s", host, port); + return String.format("jdbc:postgresql://%s:%s/", host, port); case SQLServer: return String.format("jdbc:sqlserver://%s:%s", host, port); case Oracle: From b0c9dfaf2692a74f961b6c9404d5a2a1ca0861ce Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 15:30:16 +0800 Subject: [PATCH 192/351] for #601, add case select_sub_query_with_single_table --- .../resources/parser/select_sub_query.xml | 2 +- .../db/select_sub_query_with_single_table.xml | 5 +++ .../select_sub_query_with_single_table.xml | 5 +++ .../select_sub_query_with_single_table.xml | 5 +++ .../cases/select/dql-integrate-test-cases.xml | 33 +++++++++++++++++++ .../integrate/assert/select_sub_query.xml | 2 +- .../resources/sql/dql/select_sub_query.xml | 2 +- 7 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_single_table.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_single_table.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_single_table.xml diff --git a/sharding-core/src/test/resources/parser/select_sub_query.xml b/sharding-core/src/test/resources/parser/select_sub_query.xml index 4a16d52d5e7cc..a42968f9d5b56 100644 --- a/sharding-core/src/test/resources/parser/select_sub_query.xml +++ b/sharding-core/src/test/resources/parser/select_sub_query.xml @@ -1,6 +1,6 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_single_table.xml new file mode 100644 index 0000000000000..ed6d26eb08897 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_single_table.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_single_table.xml new file mode 100644 index 0000000000000..1026341c2c762 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_single_table.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_single_table.xml new file mode 100644 index 0000000000000..ed6d26eb08897 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_single_table.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index d2de50aa6b282..5eafdf251998a 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -411,4 +411,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml index e4ec1e5a3982e..61b8602baf8c2 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml @@ -1,6 +1,6 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_sub_query.xml b/sharding-sql-test/src/main/resources/sql/dql/select_sub_query.xml index 72beb84e307f4..9c05735c514c9 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_sub_query.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_sub_query.xml @@ -1,6 +1,6 @@ - + From 5c26fc472c260d449f375bf52d82725628345ff6 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 15:35:19 +0800 Subject: [PATCH 193/351] for #601, add case select_sub_query_with_multiple_tables --- .../resources/parser/select_sub_query.xml | 2 +- .../select_sub_query_with_multiple_tables.xml | 7 +++++++ .../select_sub_query_with_multiple_tables.xml | 7 +++++++ .../select_sub_query_with_multiple_tables.xml | 7 +++++++ .../cases/select/dql-integrate-test-cases.xml | 19 ++++++------------- .../integrate/assert/select_sub_query.xml | 2 +- .../resources/sql/dql/select_sub_query.xml | 2 +- 7 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_multiple_tables.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_multiple_tables.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_multiple_tables.xml diff --git a/sharding-core/src/test/resources/parser/select_sub_query.xml b/sharding-core/src/test/resources/parser/select_sub_query.xml index a42968f9d5b56..37e5e62b827e9 100644 --- a/sharding-core/src/test/resources/parser/select_sub_query.xml +++ b/sharding-core/src/test/resources/parser/select_sub_query.xml @@ -18,7 +18,7 @@ - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml index 61b8602baf8c2..277c2a5b40e82 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml @@ -6,7 +6,7 @@ From d33ae1f58fb34d0051d33af40ebf08f6063db541 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 15:40:13 +0800 Subject: [PATCH 194/351] for #601, add case select_sub_query_with_order_by --- .../test/resources/parser/select_sub_query.xml | 2 +- .../db/select_sub_query_with_order_by.xml | 4 ++++ .../select_sub_query_with_order_by.xml | 4 ++++ .../tbl/select_sub_query_with_order_by.xml | 4 ++++ .../cases/select/dql-integrate-test-cases.xml | 18 +++++++++++------- .../integrate/assert/select_sub_query.xml | 2 +- .../resources/sql/dql/select_sub_query.xml | 2 +- 7 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_order_by.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_order_by.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_order_by.xml diff --git a/sharding-core/src/test/resources/parser/select_sub_query.xml b/sharding-core/src/test/resources/parser/select_sub_query.xml index 37e5e62b827e9..b8031a0a401c1 100644 --- a/sharding-core/src/test/resources/parser/select_sub_query.xml +++ b/sharding-core/src/test/resources/parser/select_sub_query.xml @@ -36,7 +36,7 @@ --> - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_order_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_order_by.xml new file mode 100644 index 0000000000000..deeb58cbcbb9f --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_order_by.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_order_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_order_by.xml new file mode 100644 index 0000000000000..f3bffafcf3171 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_order_by.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_order_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_order_by.xml new file mode 100644 index 0000000000000..f8505441e35f1 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_order_by.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 1ba2789781eac..e51bcfd48c760 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -418,16 +418,20 @@ - - - - + + + + + + + + + + - - - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml index 277c2a5b40e82..7ef8fed6829c9 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml @@ -12,7 +12,7 @@ --> - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_sub_query.xml b/sharding-sql-test/src/main/resources/sql/dql/select_sub_query.xml index 0efec87e48107..bfb85b7e223dd 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_sub_query.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_sub_query.xml @@ -4,6 +4,6 @@ - + From 1a80386751d55f4385ac1031317f60ed48208b9c Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 15:43:10 +0800 Subject: [PATCH 195/351] for #601, add case select_sub_query_with_group_by --- .../test/resources/parser/select_sub_query.xml | 2 +- .../db/select_sub_query_with_group_by.xml | 4 ++++ .../select_sub_query_with_group_by.xml | 4 ++++ .../tbl/select_sub_query_with_group_by.xml | 4 ++++ .../cases/select/dql-integrate-test-cases.xml | 18 ++++++------------ .../integrate/assert/select_sub_query.xml | 2 +- .../resources/sql/dql/select_sub_query.xml | 2 +- 7 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_group_by.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_group_by.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_group_by.xml diff --git a/sharding-core/src/test/resources/parser/select_sub_query.xml b/sharding-core/src/test/resources/parser/select_sub_query.xml index b8031a0a401c1..88478fd0778ee 100644 --- a/sharding-core/src/test/resources/parser/select_sub_query.xml +++ b/sharding-core/src/test/resources/parser/select_sub_query.xml @@ -48,7 +48,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_group_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_group_by.xml new file mode 100644 index 0000000000000..59807cecf4073 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_group_by.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_group_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_group_by.xml new file mode 100644 index 0000000000000..44846969b47e7 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_group_by.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_group_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_group_by.xml new file mode 100644 index 0000000000000..d242e3e491580 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_group_by.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index e51bcfd48c760..08da11f68bb67 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -429,16 +429,10 @@ - - - - - - - - - - - - + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml index 7ef8fed6829c9..1f74be4286632 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml @@ -17,7 +17,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_sub_query.xml b/sharding-sql-test/src/main/resources/sql/dql/select_sub_query.xml index bfb85b7e223dd..7a6e0e277c175 100644 --- a/sharding-sql-test/src/main/resources/sql/dql/select_sub_query.xml +++ b/sharding-sql-test/src/main/resources/sql/dql/select_sub_query.xml @@ -5,5 +5,5 @@ --> - + From f9e69a611e9f88d4010889064243915c70718bfe Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 18:05:21 +0800 Subject: [PATCH 196/351] for #601, remove useless origin test cases --- .../resources/integrate/assert/select.xml | 147 ------------------ .../integrate/assert/select_aggregate.xml | 33 ---- .../integrate/assert/select_expression.xml | 31 ---- .../integrate/assert/select_group_by.xml | 60 ------- .../resources/integrate/assert/select_or.xml | 38 ----- .../integrate/assert/select_order_by.xml | 35 ----- .../integrate/assert/select_pagination.xml | 59 ------- .../integrate/assert/select_relation.xml | 18 --- .../integrate/assert/select_sub_query.xml | 25 --- 9 files changed, 446 deletions(-) delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/select.xml delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/select_aggregate.xml delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/select_expression.xml delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/select_group_by.xml delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/select_or.xml delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select.xml b/sharding-jdbc/src/test/resources/integrate/assert/select.xml deleted file mode 100644 index 6cf0b543d4b60..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/select.xml +++ /dev/null @@ -1,147 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_aggregate.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_aggregate.xml deleted file mode 100644 index a436950653647..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_aggregate.xml +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_expression.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_expression.xml deleted file mode 100644 index 4ca97f02407b9..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_expression.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_group_by.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_group_by.xml deleted file mode 100644 index 3684b0385c497..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_group_by.xml +++ /dev/null @@ -1,60 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml deleted file mode 100644 index cb8484a4a665b..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_or.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml deleted file mode 100644 index d72e845946fbe..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_order_by.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml deleted file mode 100644 index b27b014ffa700..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_pagination.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml deleted file mode 100644 index 58cd7083a0e68..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_relation.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml deleted file mode 100644 index 1f74be4286632..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_sub_query.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - - - - - - - From 3b585e6abf73fb6cb1d818d37446bea5ca8f4656 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 18:36:29 +0800 Subject: [PATCH 197/351] for #601, refactor case select_equal_with_single_table --- .../select/dataset/db/select_equal_with_single_table.xml | 2 +- .../dbtbl_with_masterslave/select_equal_with_single_table.xml | 2 +- .../asserts/cases/select/dql-integrate-test-cases.xml | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_equal_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_equal_with_single_table.xml index 972fd8c821ee0..e83a97b99be03 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_equal_with_single_table.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_equal_with_single_table.xml @@ -1,4 +1,4 @@ - + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_equal_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_equal_with_single_table.xml index 8b31047eb77b5..b9054412bac9f 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_equal_with_single_table.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_equal_with_single_table.xml @@ -1,4 +1,4 @@ - + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 08da11f68bb67..68908a5a61375 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -30,9 +30,9 @@ - + - + From ff387a6be1087d947348c275531493f60ed20b58 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 18:45:57 +0800 Subject: [PATCH 198/351] for #601, refactor case select_in_with_single_table --- ...ingle_table_0.xml => select_in_with_single_table.xml} | 0 ...ingle_table_0.xml => select_in_with_single_table.xml} | 2 +- .../select_in_with_single_table_1.xml | 5 ----- .../select_in_with_single_table.xml} | 1 - .../select/dataset/tbl/select_in_with_single_table_0.xml | 5 ----- .../select/dataset/tbl/select_in_with_single_table_1.xml | 4 ---- .../asserts/cases/select/dql-integrate-test-cases.xml | 9 +++------ 7 files changed, 4 insertions(+), 22 deletions(-) rename sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/{select_in_with_single_table_0.xml => select_in_with_single_table.xml} (100%) rename sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/{select_in_with_single_table_0.xml => select_in_with_single_table.xml} (61%) delete mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_in_with_single_table_1.xml rename sharding-jdbc/src/test/resources/asserts/cases/select/dataset/{db/select_in_with_single_table_1.xml => tbl/select_in_with_single_table.xml} (74%) delete mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_in_with_single_table_0.xml delete mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_in_with_single_table_1.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_in_with_single_table_0.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_in_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_in_with_single_table_0.xml rename to sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_in_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_in_with_single_table_0.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_in_with_single_table.xml similarity index 61% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_in_with_single_table_0.xml rename to sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_in_with_single_table.xml index 1bd069e5fe1af..b9054412bac9f 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_in_with_single_table_0.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_in_with_single_table.xml @@ -1,4 +1,4 @@ - + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_in_with_single_table_1.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_in_with_single_table_1.xml deleted file mode 100644 index d37ccb80056ac..0000000000000 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_in_with_single_table_1.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_in_with_single_table_1.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_in_with_single_table.xml similarity index 74% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_in_with_single_table_1.xml rename to sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_in_with_single_table.xml index 60db6a333d0b1..e83a97b99be03 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_in_with_single_table_1.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_in_with_single_table.xml @@ -1,5 +1,4 @@ - diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_in_with_single_table_0.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_in_with_single_table_0.xml deleted file mode 100644 index ebff546d1f717..0000000000000 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_in_with_single_table_0.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_in_with_single_table_1.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_in_with_single_table_1.xml deleted file mode 100644 index 56a5886cf560e..0000000000000 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_in_with_single_table_1.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 68908a5a61375..c0a198434894d 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -51,12 +51,9 @@ - - - - - - + + + From dd02a2ef01e5446442ea969fc4cc617e21e46fe7 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 21:46:47 +0800 Subject: [PATCH 199/351] for #601, refactor case select_count_like_concat --- .../resources/asserts/cases/select/dql-integrate-test-cases.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index c0a198434894d..95a0ea8538778 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -70,7 +70,7 @@ - + From 1cc85bf4f7432e378e4c28be9ebc79028e75b6e4 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 22:01:22 +0800 Subject: [PATCH 200/351] for #601, refactor case select_sharding_route_with_binding_tables --- ...ect_sharding_route_with_binding_tables.xml | 20 +++++++++++++++++++ ...ect_sharding_route_with_binding_tables.xml | 20 +++++++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 8 ++++---- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sharding_route_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sharding_route_with_binding_tables.xml index 2cfed811abfa5..4dfa9c82bc674 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sharding_route_with_binding_tables.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sharding_route_with_binding_tables.xml @@ -1,5 +1,23 @@ + + + + + + + + + + + + + + + + + + @@ -20,4 +38,6 @@ + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sharding_route_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sharding_route_with_binding_tables.xml index 3f0c39969e00d..aaefb8528fda5 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sharding_route_with_binding_tables.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sharding_route_with_binding_tables.xml @@ -1,5 +1,23 @@ + + + + + + + + + + + + + + + + + + @@ -20,4 +38,6 @@ + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 95a0ea8538778..b16088ec22d6f 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -76,8 +76,8 @@ - - + + @@ -88,8 +88,8 @@ - - + + From 60064c2f79f9eda15a48007e6e289b8171abd209 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 12 Jun 2018 23:40:21 +0800 Subject: [PATCH 201/351] for #601, refactor case select_sharding_route_with_binding_tables --- ...ct_sharding_route_with_broadcast_table.xml | 20 ------------ ...ct_sharding_route_with_broadcast_table.xml | 32 +++++++++---------- .../cases/select/dql-integrate-test-cases.xml | 4 +-- 3 files changed, 18 insertions(+), 38 deletions(-) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sharding_route_with_broadcast_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sharding_route_with_broadcast_table.xml index 2cfed811abfa5..a3fd94d8db9be 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sharding_route_with_broadcast_table.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sharding_route_with_broadcast_table.xml @@ -1,23 +1,3 @@ - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sharding_route_with_broadcast_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sharding_route_with_broadcast_table.xml index 3f0c39969e00d..2ed558e2c45a1 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sharding_route_with_broadcast_table.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sharding_route_with_broadcast_table.xml @@ -1,23 +1,23 @@ + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index b16088ec22d6f..a2244b35e67cb 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -94,8 +94,8 @@ - - + + From 71484ead8516e52abbcc787d6e57b4a868594989 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 11:10:01 +0800 Subject: [PATCH 202/351] for #601, add masterslave sharding rule type --- .../dbtest/engine/BaseIntegrateTest.java | 2 +- .../cases/select/dql-integrate-test-cases.xml | 1 + .../asserts/env/masterslave/data-init.xml | 388 ++++++++++++++++++ .../schema.xml | 10 +- .../env/masterslave/sharding-rule.yaml | 6 + 5 files changed, 402 insertions(+), 5 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml rename sharding-jdbc/src/test/resources/asserts/env/{masterslaveonly => masterslave}/schema.xml (66%) create mode 100644 sharding-jdbc/src/test/resources/asserts/env/masterslave/sharding-rule.yaml diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/BaseIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/BaseIntegrateTest.java index d36059d9c9c6b..97378f198c96f 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/BaseIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/BaseIntegrateTest.java @@ -98,7 +98,7 @@ private Map createDataSourceMap(final IntegrateTestCaseAsser } private DataSource createDataSource(final Map dataSourceMap) throws SQLException, IOException { - return "masterslaveonly".equals(assertion.getShardingRuleType()) + return "masterslave".equals(assertion.getShardingRuleType()) ? YamlMasterSlaveDataSourceFactory.createDataSource(dataSourceMap, new File(EnvironmentPath.getShardingRuleResourceFile(assertion.getShardingRuleType()))) : YamlShardingDataSourceFactory.createDataSource(dataSourceMap, new File(EnvironmentPath.getShardingRuleResourceFile(assertion.getShardingRuleType()))); } diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index a2244b35e67cb..8020ba17a8581 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -3,6 +3,7 @@ + diff --git a/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml b/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml new file mode 100644 index 0000000000000..1d2378489721e --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml @@ -0,0 +1,388 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/env/masterslaveonly/schema.xml b/sharding-jdbc/src/test/resources/asserts/env/masterslave/schema.xml similarity index 66% rename from sharding-jdbc/src/test/resources/asserts/env/masterslaveonly/schema.xml rename to sharding-jdbc/src/test/resources/asserts/env/masterslave/schema.xml index 6e2366b55b01d..e67109f34461b 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/masterslaveonly/schema.xml +++ b/sharding-jdbc/src/test/resources/asserts/env/masterslave/schema.xml @@ -1,13 +1,15 @@ - master_only - slave_only + master + slave_0 + slave_1 CREATE TABLE t_order (order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id)) - CREATE TABLE t_order_item (item_id INT NOT NULL, order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (item_id)) - CREATE TABLE t_config (id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (id)) + CREATE INDEX order_index ON t_order (order_id) + CREATE TABLE t_order_item (item_id INT NOT NULL, order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, creation_date DATE, PRIMARY KEY (item_id)) + CREATE TABLE t_broadcast_table (id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (id)) CREATE TABLE t_global (id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (id)) diff --git a/sharding-jdbc/src/test/resources/asserts/env/masterslave/sharding-rule.yaml b/sharding-jdbc/src/test/resources/asserts/env/masterslave/sharding-rule.yaml new file mode 100644 index 0000000000000..5c6f29bc0fda2 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/env/masterslave/sharding-rule.yaml @@ -0,0 +1,6 @@ +masterSlaveRule: + name: master-slave-ds + masterDataSourceName: master + slaveDataSourceNames: + - slave_0 + - slave_1 From 49ea41d712197f313344cb68b7ca9b3dd2e133d3 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 11:25:37 +0800 Subject: [PATCH 203/351] for #601, add masterslave sharding rule type for case select_not_equal_with_single_table --- .../select_not_equal_with_single_table.xml | 82 +++++ .../cases/select/dql-integrate-test-cases.xml | 1 + .../asserts/env/masterslave/data-init.xml | 320 +++++++++--------- 3 files changed, 243 insertions(+), 160 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_not_equal_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_not_equal_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_not_equal_with_single_table.xml new file mode 100644 index 0000000000000..a1d5191595237 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_not_equal_with_single_table.xml @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 8020ba17a8581..c8ab735100ac6 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -10,6 +10,7 @@ + diff --git a/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml b/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml index 1d2378489721e..bf573e014b518 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml +++ b/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml @@ -21,120 +21,120 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -143,120 +143,120 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -265,120 +265,120 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From c7574b722d6a7949233fd3347e50346293615f87 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 11:26:52 +0800 Subject: [PATCH 204/351] for #601, add masterslave sharding rule type for case select_exclamation_equal_with_single_table --- .../resources/asserts/cases/select/dql-integrate-test-cases.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index c8ab735100ac6..a524870faaaed 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -17,6 +17,7 @@ + From 125d328ed701594c1f2361e879cc154b74821908 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 11:32:38 +0800 Subject: [PATCH 205/351] for #601, add masterslave sharding rule type for case select_not_in_with_single_table --- .../select_not_in_with_single_table.xml | 82 +++++++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 83 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_not_in_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_not_in_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_not_in_with_single_table.xml new file mode 100644 index 0000000000000..a1d5191595237 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_not_in_with_single_table.xml @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index a524870faaaed..31a537eea4b6e 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -24,6 +24,7 @@ + From c6025ef224e146074efd632aca9ebed2a002e70d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 11:33:49 +0800 Subject: [PATCH 206/351] for #601, add masterslave sharding rule type for case select_not_between_with_single_table --- .../resources/asserts/cases/select/dql-integrate-test-cases.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 31a537eea4b6e..771a9d47782f3 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -31,6 +31,7 @@ + From 8d68440997e97307be57d667b44e2566551e7013 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 11:36:33 +0800 Subject: [PATCH 207/351] for #601, add masterslave sharding rule type for case select_equal_with_single_table --- .../dataset/masterslave/select_equal_with_single_table.xml | 4 ++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 2 ++ 2 files changed, 6 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_equal_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_equal_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_equal_with_single_table.xml new file mode 100644 index 0000000000000..b9054412bac9f --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_equal_with_single_table.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 771a9d47782f3..94f2bc2bec734 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -38,9 +38,11 @@ + + From 1678117805f115495949ae50c570535a2f1c7b52 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 11:37:55 +0800 Subject: [PATCH 208/351] for #601, add masterslave sharding rule type for case select_equal_with_same_sharding_column --- .../resources/asserts/cases/select/dql-integrate-test-cases.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 94f2bc2bec734..adc20b00bb2b7 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -49,6 +49,7 @@ + From 34c58b1e3f17ee716f2e1117b92b270feb8ac544 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 11:41:06 +0800 Subject: [PATCH 209/351] for #601, add masterslave sharding rule type for case select_in_with_same_sharding_column --- .../masterslave/select_in_with_same_sharding_column.xml | 4 ++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 5 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_in_with_same_sharding_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_in_with_same_sharding_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_in_with_same_sharding_column.xml new file mode 100644 index 0000000000000..e9ffb8c72efc8 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_in_with_same_sharding_column.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index adc20b00bb2b7..2a69542159c78 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -56,6 +56,7 @@ + From 1906c7b2f37515edcd6694eb1de2d1f533ffda99 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 11:44:15 +0800 Subject: [PATCH 210/351] for #601, add masterslave sharding rule type for case select_in_with_single_table --- .../dataset/masterslave/select_in_with_single_table.xml | 4 ++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 2 ++ 2 files changed, 6 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_in_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_in_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_in_with_single_table.xml new file mode 100644 index 0000000000000..b9054412bac9f --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_in_with_single_table.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 2a69542159c78..af138d756f1df 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -63,9 +63,11 @@ + + From de62009a6609bbfefe190df8583f0d065a48e302 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 11:50:33 +0800 Subject: [PATCH 211/351] for #601, add masterslave sharding rule type for case select_between_with_single_table --- .../dataset/masterslave/select_between_with_single_table.xml | 5 +++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 2 ++ 2 files changed, 7 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_between_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_between_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_between_with_single_table.xml new file mode 100644 index 0000000000000..25bd1f96fff69 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_between_with_single_table.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index af138d756f1df..281637c4e6cc0 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -74,9 +74,11 @@ + + From 969c272cb42265df25f15a38635e9fae65f37b50 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 11:53:56 +0800 Subject: [PATCH 212/351] for #601, add masterslave sharding rule type for case select_count_like_concat --- .../select/dataset/masterslave/select_count_like_concat.xml | 4 ++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 5 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count_like_concat.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count_like_concat.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count_like_concat.xml new file mode 100644 index 0000000000000..4d4a1ab9360ff --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count_like_concat.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 281637c4e6cc0..4a9d388da058c 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -85,6 +85,7 @@ + From 73fdd436bb025e42cee5dc3dc8dae939bc1e4d39 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:02:20 +0800 Subject: [PATCH 213/351] for #601, add masterslave sharding rule type for select_sharding_route_with_binding_tables --- .../select_sharding_route_with_binding_tables.xml | 11 +++++++++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 12 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sharding_route_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sharding_route_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sharding_route_with_binding_tables.xml new file mode 100644 index 0000000000000..610bb824302ac --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sharding_route_with_binding_tables.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 4a9d388da058c..1d74f70942bd5 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -92,6 +92,7 @@ + From b689536503a53efdb34d2a7233dd3e0034ac2040 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:11:57 +0800 Subject: [PATCH 214/351] for #601, add masterslave sharding rule type for select_full_route_with_binding_tables --- .../select_full_route_with_binding_tables.xml | 83 +++++++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 3 +- 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_full_route_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_full_route_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_full_route_with_binding_tables.xml new file mode 100644 index 0000000000000..21e18ae7118a0 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_full_route_with_binding_tables.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 1d74f70942bd5..e8d49d072ec5c 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -99,8 +99,9 @@ + - + From 87007f57adf1e992b19223f00af930aed236ca72 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:13:44 +0800 Subject: [PATCH 215/351] for #601, add masterslave sharding rule type for select_full_route_for_upper_case_with_binding_tables --- .../resources/asserts/cases/select/dql-integrate-test-cases.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index e8d49d072ec5c..d993729477bd3 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -106,6 +106,7 @@ + From 65821538ad334f41f8a78082f174ebaec1ec0a67 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:16:54 +0800 Subject: [PATCH 216/351] for #601, add masterslave sharding rule type for select_sharding_route_with_broadcast_table --- .../masterslave/select_sharding_route_with_broadcast_table.xml | 3 +++ .../asserts/cases/select/dql-integrate-test-cases.xml | 2 ++ 2 files changed, 5 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sharding_route_with_broadcast_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sharding_route_with_broadcast_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sharding_route_with_broadcast_table.xml new file mode 100644 index 0000000000000..a3fd94d8db9be --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sharding_route_with_broadcast_table.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index d993729477bd3..efd79f7322d3e 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -113,9 +113,11 @@ + + From 2e3bf5203e0e83fe6d21d9c66a557b8fb01c0a58 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:25:58 +0800 Subject: [PATCH 217/351] for #601, add masterslave sharding rule type for select_alias_as_keyword --- .../select/dataset/masterslave/select_alias_as_keyword.xml | 4 ++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 5 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_alias_as_keyword.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_alias_as_keyword.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_alias_as_keyword.xml new file mode 100644 index 0000000000000..01edbf5c8d3ce --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_alias_as_keyword.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index efd79f7322d3e..4de59b65c4fd4 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -124,6 +124,7 @@ + From 7fb52249fae87729fd4f1237cf848527a3622bdd Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:29:08 +0800 Subject: [PATCH 218/351] for #601, add masterslave sharding rule type for select_with_force_index_join --- .../dataset/masterslave/select_with_force_index_join.xml | 5 +++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 6 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_force_index_join.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_force_index_join.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_force_index_join.xml new file mode 100644 index 0000000000000..be25108ecc8b4 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_force_index_join.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 4de59b65c4fd4..81f2601192f67 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -131,6 +131,7 @@ + From 020c0168c4014ff299c08ca330240baf62898105 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:30:54 +0800 Subject: [PATCH 219/351] for #601, add masterslave sharding rule type for select_sum --- .../asserts/cases/select/dataset/masterslave/select_sum.xml | 4 ++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 5 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sum.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sum.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sum.xml new file mode 100644 index 0000000000000..4f3e25d399ad3 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sum.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 81f2601192f67..c541c4ebc7bda 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -138,6 +138,7 @@ + From c11fd99f4e4ac5740a5b22b11e0da9659297e25e Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:32:45 +0800 Subject: [PATCH 220/351] for #601, add masterslave sharding rule type for select_count --- .../asserts/cases/select/dataset/masterslave/select_count.xml | 4 ++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 5 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count.xml new file mode 100644 index 0000000000000..deeb58cbcbb9f --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index c541c4ebc7bda..b26b877594627 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -145,6 +145,7 @@ + From 17605d1c14ec0c06a40d950209c11bcf6465aadf Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:35:43 +0800 Subject: [PATCH 221/351] for #601, add masterslave sharding rule type for select_max --- .../asserts/cases/select/dataset/masterslave/select_max.xml | 4 ++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 5 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_max.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_max.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_max.xml new file mode 100644 index 0000000000000..56a6efb989657 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_max.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index b26b877594627..a14cc493ebc54 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -152,6 +152,7 @@ + From f9e97712fac209bb89af54b21fa3f8ca8928bdbd Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:37:17 +0800 Subject: [PATCH 222/351] for #601, add masterslave sharding rule type for select_min --- .../asserts/cases/select/dataset/masterslave/select_min.xml | 4 ++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 5 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_min.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_min.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_min.xml new file mode 100644 index 0000000000000..f25acc536bddb --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_min.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index a14cc493ebc54..21725a5deaccf 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -159,6 +159,7 @@ + From 17dea824ae70464662d2854ecccd5cca5b3d7712 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:40:13 +0800 Subject: [PATCH 223/351] for #601, add masterslave sharding rule type for select_avg --- .../asserts/cases/select/dataset/masterslave/select_avg.xml | 4 ++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 5 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_avg.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_avg.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_avg.xml new file mode 100644 index 0000000000000..01128f0fa27c6 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_avg.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 21725a5deaccf..24ea1d68c11ae 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -166,6 +166,7 @@ + From 6b75b945c1baaa914ab7ca4dce2fcc34d9167616 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:43:17 +0800 Subject: [PATCH 224/351] for #601, add masterslave sharding rule type for select_count_with_binding_tables_without_join --- .../dataset/masterslave/select_count_with_binding_tables.xml | 4 ++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 5 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count_with_binding_tables.xml new file mode 100644 index 0000000000000..7aa10e2a78eab --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count_with_binding_tables.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 24ea1d68c11ae..212969e4b3d4f 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -173,6 +173,7 @@ + From b0442df1f7b7d0d996106941258fc43669008746 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:45:08 +0800 Subject: [PATCH 225/351] for #601, add masterslave sharding rule type for select_count_with_binding_tables_with_join --- .../resources/asserts/cases/select/dql-integrate-test-cases.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 212969e4b3d4f..aff53504f5f49 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -180,6 +180,7 @@ + From 53aa2690e741a15f82309a35fb18c73db91196c7 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:49:54 +0800 Subject: [PATCH 226/351] for #601, add masterslave sharding rule type for select_count_with_escape_character --- .../resources/asserts/cases/select/dql-integrate-test-cases.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index aff53504f5f49..eac313b67b0e5 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -187,6 +187,7 @@ + From ee60943bc71347f78fb38a1173738f240050c493 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:55:03 +0800 Subject: [PATCH 227/351] for #601, add masterslave sharding rule type for select_with_expression --- .../masterslave/select_with_expression.xml | 43 +++++++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 1 + .../asserts/env/masterslave/data-init.xml | 4 +- 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_expression.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_expression.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_expression.xml new file mode 100644 index 0000000000000..48ffd4ebbe953 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_expression.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index eac313b67b0e5..8c41d2e5743a0 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -194,6 +194,7 @@ + diff --git a/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml b/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml index bf573e014b518..71036f855ec1f 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml +++ b/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml @@ -35,8 +35,6 @@ - - @@ -55,6 +53,8 @@ + + From de7eb0cc9c5f7237fcaa9271ac47c55926edc9aa Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:57:17 +0800 Subject: [PATCH 228/351] for #601, add masterslave sharding rule type for select_with_date_function --- .../masterslave/select_with_date_function.xml | 43 +++++++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 44 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_date_function.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_date_function.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_date_function.xml new file mode 100644 index 0000000000000..0d175c3da58ce --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_date_function.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 8c41d2e5743a0..11fddaf6fd11c 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -201,6 +201,7 @@ + From d4368439afc890a47e44f2cc4c3366d8658bb952 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 12:58:55 +0800 Subject: [PATCH 229/351] for #601, add masterslave sharding rule type for select_with_regexp --- .../cases/select/dataset/masterslave/select_with_regexp.xml | 4 ++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 5 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_regexp.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_regexp.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_regexp.xml new file mode 100644 index 0000000000000..b784f80c4ee5e --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_regexp.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 11fddaf6fd11c..a3eda77de0e26 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -208,6 +208,7 @@ + From 9e586c502436ae8e9b624614eadadf9688c10e5b Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 18:45:35 +0800 Subject: [PATCH 230/351] for #601, add masterslave sharding rule type for select_group_by_with_sum --- .../masterslave/select_group_by_with_sum.xml | 23 +++++++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 24 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_sum.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_sum.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_sum.xml new file mode 100644 index 0000000000000..fb52fa1aa65c1 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_sum.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index a3eda77de0e26..64b491a82c2e8 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -215,6 +215,7 @@ + From 531d70d293c23c008d35da993b8432f39e9ad70d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 18:49:25 +0800 Subject: [PATCH 231/351] for #601, add masterslave sharding rule type for select_group_by_with_count --- .../select_group_by_with_count.xml | 23 +++++++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 24 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_count.xml new file mode 100644 index 0000000000000..e3d5dc0f48aac --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_count.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 64b491a82c2e8..a79083283a4b7 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -222,6 +222,7 @@ + From 254dedaab795d158dc561721286b065782b19e9e Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 18:50:51 +0800 Subject: [PATCH 232/351] for #601, add masterslave sharding rule type for select_group_by_with_max --- .../masterslave/select_group_by_with_max.xml | 23 +++++++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 24 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_max.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_max.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_max.xml new file mode 100644 index 0000000000000..51e9b39bd4079 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_max.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index a79083283a4b7..06609d77bca28 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -229,6 +229,7 @@ + From d74de225869ddb4823dad86c839a269fd53fb4bc Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 18:52:04 +0800 Subject: [PATCH 233/351] for #601, add masterslave sharding rule type for select_group_by_with_min --- .../masterslave/select_group_by_with_min.xml | 23 +++++++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 24 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_min.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_min.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_min.xml new file mode 100644 index 0000000000000..a901fad6a5cc0 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_min.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 06609d77bca28..0b3afc44c01e8 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -236,6 +236,7 @@ + From 80c47f5dac7e4d30780be36fbbb0607b6d077973 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 18:55:09 +0800 Subject: [PATCH 234/351] for #601, add masterslave sharding rule type for select_group_by_with_avg --- .../masterslave/select_group_by_with_avg.xml | 23 +++++++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 24 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_avg.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_avg.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_avg.xml new file mode 100644 index 0000000000000..1198693d61d5f --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_avg.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 0b3afc44c01e8..6dec6e3b40a50 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -243,6 +243,7 @@ + From 171b6b68af22bd7b6a183ca36716790123c3740c Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 18:56:42 +0800 Subject: [PATCH 235/351] for #601, add masterslave sharding rule type for select_group_by_with_order_by_desc --- .../select_group_by_with_order_by_desc.xml | 23 +++++++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 24 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_order_by_desc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_order_by_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_order_by_desc.xml new file mode 100644 index 0000000000000..e7daa25ab81c4 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_order_by_desc.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 6dec6e3b40a50..9608c0c298e14 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -250,6 +250,7 @@ + From 7c701df88608713ef8dd3b6ea656310df54c86b1 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 20:58:40 +0800 Subject: [PATCH 236/351] for #601, add masterslave sharding rule type for select_group_by_without_grouped_column --- .../masterslave/select_group_by_without_grouped_column.xml | 5 +++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 6 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_without_grouped_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_without_grouped_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_without_grouped_column.xml new file mode 100644 index 0000000000000..e3db105c428da --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_without_grouped_column.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 9608c0c298e14..398b8fa77f36a 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -257,6 +257,7 @@ + From 74ec016eaa0ee5705806ef9a803585ab5da426e3 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 21:01:00 +0800 Subject: [PATCH 237/351] for #601, add masterslave sharding rule type for select_group_by_with_limit --- .../select/dataset/masterslave/select_group_by_with_limit.xml | 4 ++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 5 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_limit.xml new file mode 100644 index 0000000000000..b85ff0a9d450a --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_limit.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 398b8fa77f36a..256b2350d1e81 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -264,6 +264,7 @@ + From 076b41a4ff9df4aae549d3a8bb95ab9811e10585 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 21:03:58 +0800 Subject: [PATCH 238/351] for #601, add masterslave sharding rule type for select_group_by_with_order_by_and_limit --- .../masterslave/select_group_by_with_order_by_and_limit.xml | 4 ++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 5 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_order_by_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_order_by_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_order_by_and_limit.xml new file mode 100644 index 0000000000000..2d00bd17a6864 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_order_by_and_limit.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 256b2350d1e81..ab7b1b61e92e5 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -271,6 +271,7 @@ + From 46b604effaf3e16faf9c935a574923b3fa00e315 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 21:05:22 +0800 Subject: [PATCH 239/351] for #601, add masterslave sharding rule type for select_with_item_alias_match_order_by_and_group_by_items --- ...lias_match_order_by_and_group_by_items.xml | 23 +++++++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 24 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_item_alias_match_order_by_and_group_by_items.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_item_alias_match_order_by_and_group_by_items.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_item_alias_match_order_by_and_group_by_items.xml new file mode 100644 index 0000000000000..51337e450f724 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_item_alias_match_order_by_and_group_by_items.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index ab7b1b61e92e5..5b1ee9dd5c2f7 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -278,6 +278,7 @@ + From 30fdda9547c50504d85d43696730d75470480dd3 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 21:06:51 +0800 Subject: [PATCH 240/351] for #601, add masterslave sharding rule type for select_group_by_with_key_word_alias --- .../select_group_by_with_key_word_alias.xml | 23 +++++++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 24 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_key_word_alias.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_key_word_alias.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_key_word_alias.xml new file mode 100644 index 0000000000000..4a62da51b2542 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_key_word_alias.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 5b1ee9dd5c2f7..3854a3cf05a5a 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -285,6 +285,7 @@ + From 133a47a690b4a1e8d7f969ca89830f5869df5e84 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 21:09:54 +0800 Subject: [PATCH 241/351] for #601, add masterslave sharding rule type for select_or_with_same_sharding_columns --- .../masterslave/select_or_with_same_sharding_columns.xml | 5 +++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 6 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_same_sharding_columns.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_same_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_same_sharding_columns.xml new file mode 100644 index 0000000000000..1026341c2c762 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_same_sharding_columns.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 3854a3cf05a5a..ba27d378682a8 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -298,6 +298,7 @@ + From 003908dc14776d135c5081b10681e6fdbbc01bda Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 21:11:58 +0800 Subject: [PATCH 242/351] for #601, add masterslave sharding rule type for select_or_with_different_sharding_columns --- .../select_or_with_different_sharding_columns.xml | 6 ++++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 7 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_different_sharding_columns.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_different_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_different_sharding_columns.xml new file mode 100644 index 0000000000000..321f83fe2735a --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_different_sharding_columns.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index ba27d378682a8..52e9171f85f81 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -305,6 +305,7 @@ + From 85086ba3f3eea3fc043a8b6e74afdb67594d7dfb Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 21:13:35 +0800 Subject: [PATCH 243/351] for #601, add masterslave sharding rule type for select_or_with_none_sharding_columns --- .../masterslave/select_or_with_none_sharding_columns.xml | 3 +++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 4 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_none_sharding_columns.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_none_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_none_sharding_columns.xml new file mode 100644 index 0000000000000..d490bba9c699e --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_none_sharding_columns.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 52e9171f85f81..798436f39c724 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -312,6 +312,7 @@ + From 898077f5ae87354a0161c27c2677f54f096243f2 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 21:14:54 +0800 Subject: [PATCH 244/351] for #601, add masterslave sharding rule type for select_or_mix_and_for_simple_pattern --- .../masterslave/select_or_mix_and_for_simple_pattern.xml | 3 +++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 4 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_for_simple_pattern.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_for_simple_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_for_simple_pattern.xml new file mode 100644 index 0000000000000..d490bba9c699e --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_for_simple_pattern.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 798436f39c724..381610a674ab4 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -319,6 +319,7 @@ + From 09f4db98f5f882c409a9b74c1db3de42229bc398 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 21:16:28 +0800 Subject: [PATCH 245/351] for #601, add masterslave sharding rule type for select_or_mix_and_for_complex_pattern --- .../masterslave/select_or_mix_and_for_complex_pattern.xml | 3 +++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 4 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_for_complex_pattern.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_for_complex_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_for_complex_pattern.xml new file mode 100644 index 0000000000000..d490bba9c699e --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_for_complex_pattern.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 381610a674ab4..56b8e4974caf7 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -326,6 +326,7 @@ + From d7d22d5cb60b823a9ddf63c04eafba101383836d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 21:23:28 +0800 Subject: [PATCH 246/351] for #601, add masterslave sharding rule type for select_or_mix_and_with_binding_tables --- .../select_or_mix_and_with_binding_tables.xml | 5 + .../cases/select/dql-integrate-test-cases.xml | 1 + .../asserts/env/masterslave/data-init.xml | 480 +++++++++--------- 3 files changed, 246 insertions(+), 240 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_with_binding_tables.xml new file mode 100644 index 0000000000000..4b01a86a48819 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_with_binding_tables.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 56b8e4974caf7..66c432187a450 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -333,6 +333,7 @@ + diff --git a/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml b/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml index 71036f855ec1f..f1bd64b42c493 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml +++ b/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml @@ -59,86 +59,86 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -181,86 +181,86 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -303,86 +303,86 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 8021302595866620ca0d2ee090544e4a302a815d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 21:25:41 +0800 Subject: [PATCH 247/351] for #601, add masterslave sharding rule type for select_or_mix_and_with_binding_and_broadcast_tables --- .../select_or_mix_and_with_binding_and_broadcast_tables.xml | 3 +++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 4 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml new file mode 100644 index 0000000000000..a3fd94d8db9be --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 66c432187a450..198433b1478ba 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -340,6 +340,7 @@ + From e158a7d74220392feb37c183041421dd8f1ae717 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 21:27:40 +0800 Subject: [PATCH 248/351] for #601, add masterslave sharding rule type for select_order_by_asc_and_index_desc --- .../select_order_by_asc_and_index_desc.xml | 43 +++++++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 44 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_asc_and_index_desc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_asc_and_index_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_asc_and_index_desc.xml new file mode 100644 index 0000000000000..3d75a58c2b0c6 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_asc_and_index_desc.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 198433b1478ba..62c74b7b29f09 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -347,6 +347,7 @@ + From c77701ab73418498a4bb98a8c79a200b3b3ac22c Mon Sep 17 00:00:00 2001 From: terrymanu Date: Wed, 13 Jun 2018 21:34:22 +0800 Subject: [PATCH 249/351] for #601, add masterslave sharding rule type for select_order_by_desc_and_index_asc --- .../dataset/masterslave/select_order_by_desc_and_index_asc.xml | 3 +++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 4 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_desc_and_index_asc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_desc_and_index_asc.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_desc_and_index_asc.xml new file mode 100644 index 0000000000000..a3fd94d8db9be --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_desc_and_index_asc.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 62c74b7b29f09..d490acc1141ca 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -354,6 +354,7 @@ + From 25cd0058158c3aff6dbd16d93fe009e87c8c0268 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Thu, 14 Jun 2018 10:19:12 +0800 Subject: [PATCH 250/351] for #601, add masterslave sharding rule type for select_order_by_with_ordered_column --- .../select_order_by_with_ordered_column.xml | 43 +++++++++++++++++++ .../cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 44 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_with_ordered_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_with_ordered_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_with_ordered_column.xml new file mode 100644 index 0000000000000..0ee55c37fe619 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_with_ordered_column.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index d490acc1141ca..53cb7e89f9c9f 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -361,6 +361,7 @@ + From 58f7ea040976a8de3889fd55728c28ae50f09273 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Thu, 14 Jun 2018 10:22:53 +0800 Subject: [PATCH 251/351] for #601, add masterslave sharding rule type for select_order_by_with_date --- .../select/dataset/masterslave/select_order_by_with_date.xml | 3 +++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 4 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_with_date.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_with_date.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_with_date.xml new file mode 100644 index 0000000000000..a3fd94d8db9be --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_with_date.xml @@ -0,0 +1,3 @@ + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 53cb7e89f9c9f..ca1f6b4d72d2b 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -368,6 +368,7 @@ + From a65738ce2ea07ab9d35d7f21b67c6f6a266444ea Mon Sep 17 00:00:00 2001 From: terrymanu Date: Thu, 14 Jun 2018 10:25:30 +0800 Subject: [PATCH 252/351] for #601, add masterslave sharding rule type for select_order_by_for_nulls_first&last --- .../resources/asserts/cases/select/dql-integrate-test-cases.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index ca1f6b4d72d2b..6e33ee3991279 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -376,6 +376,7 @@ + @@ -383,6 +384,7 @@ + From f0eccf6c235c40f3e70e9cb667185fb36d924239 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Thu, 14 Jun 2018 10:55:09 +0800 Subject: [PATCH 253/351] for #601, fix case select_with_date_function --- .../masterslave/select_with_date_function.xml | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_date_function.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_date_function.xml index 0d175c3da58ce..2e3f0bd765149 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_date_function.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_date_function.xml @@ -40,4 +40,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 259718bf33ef2008b80d714e6abe6a34e05d5358 Mon Sep 17 00:00:00 2001 From: linjiaqi Date: Thu, 14 Jun 2018 15:01:04 +0800 Subject: [PATCH 254/351] Merge remote-tracking branch 'remotes/upstream/dev' into dev fix errors --- .../common/CommandResponsePacketsHandler.java | 11 +- .../common/SQLPacketsBackendHandler.java | 32 ++-- .../StatementExecuteBackendHandler.java | 7 - .../backend/mysql/MySQLBackendHandler.java | 146 ++++++++---------- .../proxy/backend/mysql/MySQLQueryResult.java | 16 +- .../transport/mysql/packet/MySQLPacket.java | 8 +- .../mysql/packet/command/CommandPacket.java | 14 -- .../text/fieldlist/ComFieldListPacket.java | 40 ++++- .../text/query/ColumnDefinition41Packet.java | 4 +- .../command/text/query/ComQueryPacket.java | 77 ++++++--- .../command/text/query/FieldCountPacket.java | 5 + .../text/query/TextResultSetRowPacket.java | 10 ++ .../mysql/packet/generic/EofPacket.java | 6 +- .../mysql/packet/generic/ErrPacket.java | 6 +- .../mysql/packet/generic/OKPacket.java | 6 +- .../packet/handshake/AuthPluginData.java | 6 + .../packet/handshake/HandshakePacket.java | 19 +++ 17 files changed, 237 insertions(+), 176 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java index e51f9511ecf9c..e29a7fba4267e 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/CommandResponsePacketsHandler.java @@ -28,11 +28,14 @@ * @author linjiaqi */ public abstract class CommandResponsePacketsHandler extends ChannelInboundHandlerAdapter { - protected abstract void auth(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload); - protected abstract void endOfFilePacket(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload); + protected abstract void auth(ChannelHandlerContext context, MySQLPacketPayload mysqlPacketPayload); - protected abstract void genericResponsePacket(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload); + protected abstract void eofPacket(ChannelHandlerContext context, MySQLPacketPayload mysqlPacketPayload); - protected abstract void executeCommandResponsePackets(ChannelHandlerContext context, int sequenceId, int header, MySQLPacketPayload mysqlPacketPayload); + protected abstract void okPacket(ChannelHandlerContext context, MySQLPacketPayload mysqlPacketPayload); + + protected abstract void errPacket(ChannelHandlerContext context, MySQLPacketPayload mysqlPacketPayload); + + protected abstract void commonPacket(MySQLPacketPayload mysqlPacketPayload); } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java index a13b1731537de..3b02783cde4b6 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java @@ -44,11 +44,11 @@ import io.shardingsphere.proxy.backend.mysql.MySQLQueryResult; import io.shardingsphere.proxy.config.RuleRegistry; import io.shardingsphere.proxy.metadata.ProxyShardingRefreshHandler; +import io.shardingsphere.proxy.transport.common.packet.CommandPacketRebuilder; import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; -import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.ComQueryPacket; import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket; import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket; import io.shardingsphere.proxy.transport.mysql.packet.generic.ErrPacket; @@ -69,7 +69,7 @@ public final class SQLPacketsBackendHandler implements BackendHandler { private SynchronizedFuture> synchronizedFuture; - private final CommandPacket commandPacket; + private final CommandPacketRebuilder rebuilder; private final DatabaseType databaseType; @@ -85,8 +85,8 @@ public final class SQLPacketsBackendHandler implements BackendHandler { private boolean hasMoreResultValueFlag; - public SQLPacketsBackendHandler(final CommandPacket commandPacket, final DatabaseType databaseType, final boolean showSQL) { - this.commandPacket = commandPacket; + public SQLPacketsBackendHandler(final CommandPacketRebuilder rebuilder, final DatabaseType databaseType, final boolean showSQL) { + this.rebuilder = rebuilder; this.databaseType = databaseType; this.showSQL = showSQL; isMerged = false; @@ -104,15 +104,16 @@ public CommandResponsePackets execute() { protected CommandResponsePackets executeForMasterSlave() { MasterSlaveRouter masterSlaveRouter = new MasterSlaveRouter(RuleRegistry.getInstance().getMasterSlaveRule()); - SQLStatement sqlStatement = new SQLJudgeEngine(commandPacket.getSql()).judge(); + SQLStatement sqlStatement = new SQLJudgeEngine(rebuilder.sql()).judge(); String dataSourceName = masterSlaveRouter.route(sqlStatement.getType()).iterator().next(); synchronizedFuture = new SynchronizedFuture<>(1); - MySQLResultCache.getInstance().putFuture(commandPacket.getConnectionId(), synchronizedFuture); - executeCommand(dataSourceName, commandPacket.getConnectionId(), commandPacket); + MySQLResultCache.getInstance().putFuture(rebuilder.connectionId(), synchronizedFuture); + CommandPacket commandPacket = rebuilder.rebuild(new Object[]{rebuilder.sequenceId(), rebuilder.connectionId(), rebuilder.sql()}); + executeCommand(dataSourceName, rebuilder.connectionId(), commandPacket); //TODO timeout should be set. List queryResults = synchronizedFuture.get(CONNECT_TIMEOUT, TimeUnit.SECONDS); - MySQLResultCache.getInstance().deleteFuture(commandPacket.getConnectionId()); + MySQLResultCache.getInstance().deleteFuture(rebuilder.connectionId()); List packets = new LinkedList<>(); for (QueryResult each : queryResults) { @@ -123,25 +124,20 @@ protected CommandResponsePackets executeForMasterSlave() { protected CommandResponsePackets executeForSharding() { StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), databaseType, showSQL); - SQLRouteResult routeResult = routingEngine.route(commandPacket.getSql()); + SQLRouteResult routeResult = routingEngine.route(rebuilder.sql()); if (routeResult.getExecutionUnits().isEmpty()) { return new CommandResponsePackets(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); } synchronizedFuture = new SynchronizedFuture<>(routeResult.getExecutionUnits().size()); - MySQLResultCache.getInstance().putFuture(commandPacket.getConnectionId(), synchronizedFuture); + MySQLResultCache.getInstance().putFuture(rebuilder.connectionId(), synchronizedFuture); for (SQLExecutionUnit each : routeResult.getExecutionUnits()) { - if (commandPacket instanceof ComQueryPacket) { - ComQueryPacket queryPacket = (ComQueryPacket) ((ComQueryPacket) commandPacket).clone(); - queryPacket.setSql(each.getSqlUnit().getSql()); - executeCommand(each.getDataSource(), queryPacket.getConnectionId(), queryPacket); - } else { - executeCommand(each.getDataSource(), commandPacket.getConnectionId(), commandPacket); - } + CommandPacket commandPacket = rebuilder.rebuild(new Object[]{rebuilder.sequenceId(), rebuilder.connectionId(), each.getSqlUnit().getSql()}); + executeCommand(each.getDataSource(), rebuilder.connectionId(), commandPacket); } //TODO timeout should be set. List queryResults = synchronizedFuture.get(CONNECT_TIMEOUT, TimeUnit.SECONDS); - MySQLResultCache.getInstance().deleteFuture(commandPacket.getConnectionId()); + MySQLResultCache.getInstance().deleteFuture(rebuilder.connectionId()); List packets = Lists.newArrayListWithCapacity(queryResults.size()); for (QueryResult each : queryResults) { diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java index 63dc4bd718b0a..24eeb2359c717 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java @@ -25,7 +25,6 @@ import io.shardingsphere.core.merger.QueryResult; import io.shardingsphere.core.parsing.SQLJudgeEngine; import io.shardingsphere.core.parsing.parser.sql.SQLStatement; -import io.shardingsphere.core.parsing.parser.sql.dml.insert.InsertStatement; import io.shardingsphere.core.routing.PreparedStatementRoutingEngine; import io.shardingsphere.core.routing.SQLExecutionUnit; import io.shardingsphere.core.routing.SQLRouteResult; @@ -41,7 +40,6 @@ import io.shardingsphere.proxy.transport.mysql.packet.command.statement.PreparedStatementRegistry; import io.shardingsphere.proxy.transport.mysql.packet.command.statement.execute.BinaryResultSetRowPacket; import io.shardingsphere.proxy.transport.mysql.packet.command.statement.execute.PreparedStatementParameter; -import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet; import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.FieldCountPacket; import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket; import io.shardingsphere.proxy.transport.mysql.packet.generic.ErrPacket; @@ -49,16 +47,11 @@ import lombok.Getter; import lombok.Setter; -import javax.sql.DataSource; import java.sql.Connection; -import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.sql.ResultSetMetaData; import java.sql.SQLException; -import java.sql.Statement; import java.util.ArrayList; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ExecutionException; diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java index 36915b5770cb3..877c8d6e173b7 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLBackendHandler.java @@ -19,10 +19,6 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import java.util.ArrayList; -import java.util.List; - -import com.google.common.primitives.Bytes; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; @@ -30,6 +26,7 @@ import io.shardingsphere.proxy.backend.constant.AuthType; import io.shardingsphere.proxy.config.DataSourceConfig; import io.shardingsphere.proxy.transport.mysql.constant.CapabilityFlag; +import io.shardingsphere.proxy.transport.mysql.constant.PacketHeader; import io.shardingsphere.proxy.transport.mysql.constant.ServerInfo; import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet; @@ -37,6 +34,7 @@ import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket; import io.shardingsphere.proxy.transport.mysql.packet.generic.ErrPacket; import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket; +import io.shardingsphere.proxy.transport.mysql.packet.handshake.HandshakePacket; import io.shardingsphere.proxy.transport.mysql.packet.handshake.HandshakeResponse41Packet; import io.shardingsphere.proxy.util.MySQLResultCache; import lombok.RequiredArgsConstructor; @@ -60,80 +58,70 @@ public class MySQLBackendHandler extends CommandResponsePacketsHandler { @Override public void channelRead(final ChannelHandlerContext context, final Object message) { MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload((ByteBuf) message); - int sequenceId = mysqlPacketPayload.readInt1(); mysqlPacketPayload.getByteBuf().markReaderIndex(); + mysqlPacketPayload.readInt1(); int header = mysqlPacketPayload.readInt1(); + mysqlPacketPayload.getByteBuf().resetReaderIndex(); if (AuthType.UN_AUTH == authType) { - auth(context, sequenceId, header, mysqlPacketPayload); + auth(context, mysqlPacketPayload); + authType = AuthType.AUTHING; } else if (AuthType.AUTHING == authType) { - genericResponsePacket(context, sequenceId, header, mysqlPacketPayload); + if (PacketHeader.OK.getValue() == header) { + okPacket(context, mysqlPacketPayload); + authType = AuthType.AUTH_SUCCESS; + } else { + errPacket(context, mysqlPacketPayload); + authType = AuthType.AUTH_FAILED; + } } else if (AuthType.AUTH_FAILED == authType) { log.error("mysql auth failed, cannot handle channel read message"); } else { - if (EofPacket.HEADER == header) { - endOfFilePacket(context, sequenceId, header, mysqlPacketPayload); - } else if (OKPacket.HEADER == header || ErrPacket.HEADER == header) { - genericResponsePacket(context, sequenceId, header, mysqlPacketPayload); + if (PacketHeader.EOF.getValue() == header) { + eofPacket(context, mysqlPacketPayload); + } else if (PacketHeader.OK.getValue() == header) { + okPacket(context, mysqlPacketPayload); + } else if (PacketHeader.ERR.getValue() == header) { + errPacket(context, mysqlPacketPayload); } else { - executeCommandResponsePackets(context, sequenceId, header, mysqlPacketPayload); + commonPacket(mysqlPacketPayload); } } } @Override - protected void auth(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { + protected void auth(final ChannelHandlerContext context, final MySQLPacketPayload mysqlPacketPayload) { try { - int protocolVersion = header; - String serverVersion = mysqlPacketPayload.readStringNul(); - int connectionId = mysqlPacketPayload.readInt4(); - MySQLResultCache.getInstance().putConnection(context.channel().id().asShortText(), connectionId); - byte[] authPluginDataPart1 = mysqlPacketPayload.readStringNul().getBytes(); - int capabilityFlagsLower = mysqlPacketPayload.readInt2(); - int charset = mysqlPacketPayload.readInt1(); - int statusFlag = mysqlPacketPayload.readInt2(); - int capabilityFlagsUpper = mysqlPacketPayload.readInt2(); - int authPluginDataLength = mysqlPacketPayload.readInt1(); - mysqlPacketPayload.skipReserved(10); - byte[] authPluginDataPart2 = mysqlPacketPayload.readStringNul().getBytes(); - byte[] authPluginData = Bytes.concat(authPluginDataPart1, authPluginDataPart2); - byte[] authResponse = securePasswordAuthentication(dataSourceConfig.getPassword().getBytes(), authPluginData); - //TODO maxSizePactet(16MB) should be set. - HandshakeResponse41Packet handshakeResponse41Packet = new HandshakeResponse41Packet(sequenceId + 1, CapabilityFlag.calculateHandshakeCapabilityFlagsLower(), 16777215, ServerInfo.CHARSET, - dataSourceConfig.getUsername(), authResponse, dataSourceConfig.getDatabase()); + HandshakePacket handshakePacket = new HandshakePacket(mysqlPacketPayload); + int capabilityFlags = CapabilityFlag.calculateHandshakeCapabilityFlagsLower(); + byte[] authResponse = securePasswordAuthentication(dataSourceConfig.getPassword().getBytes(), handshakePacket.getAuthPluginData().getAuthPluginData()); + HandshakeResponse41Packet handshakeResponse41Packet = new HandshakeResponse41Packet(handshakePacket.getSequenceId() + 1, capabilityFlags, 16777215, + ServerInfo.CHARSET, dataSourceConfig.getUsername(), authResponse, dataSourceConfig.getDatabase()); + MySQLResultCache.getInstance().putConnection(context.channel().id().asShortText(), handshakePacket.getConnectionId()); context.writeAndFlush(handshakeResponse41Packet); } finally { - authType = AuthType.AUTHING; mysqlPacketPayload.getByteBuf().release(); } } @Override - protected void genericResponsePacket(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { - mysqlQueryResult = new MySQLQueryResult(); - mysqlPacketPayload.getByteBuf().resetReaderIndex(); - switch (header) { - case OKPacket.HEADER: - if (authType == AuthType.AUTHING) { - authType = AuthType.AUTH_SUCCESS; - } - mysqlQueryResult.setGenericResponse(new OKPacket(sequenceId, mysqlPacketPayload)); - break; - case ErrPacket.HEADER: - if (authType == AuthType.AUTHING) { - authType = AuthType.AUTH_FAILED; - } - mysqlQueryResult.setGenericResponse(new ErrPacket(sequenceId, mysqlPacketPayload)); - break; - default: - break; + protected void okPacket(final ChannelHandlerContext context, final MySQLPacketPayload mysqlPacketPayload) { + try { + mysqlQueryResult = new MySQLQueryResult(); + mysqlQueryResult.setGenericResponse(new OKPacket(mysqlPacketPayload)); + setResponse(context); + } finally { + mysqlQueryResult = null; + mysqlPacketPayload.getByteBuf().release(); } - + } + + @Override + protected void errPacket(final ChannelHandlerContext context, final MySQLPacketPayload mysqlPacketPayload) { try { - int connectionId = MySQLResultCache.getInstance().getConnection(context.channel().id().asShortText()); - if (MySQLResultCache.getInstance().getFuture(connectionId) != null) { - MySQLResultCache.getInstance().getFuture(connectionId).setResponse(mysqlQueryResult); - } + mysqlQueryResult = new MySQLQueryResult(); + mysqlQueryResult.setGenericResponse(new ErrPacket(mysqlPacketPayload)); + setResponse(context); } finally { mysqlQueryResult = null; mysqlPacketPayload.getByteBuf().release(); @@ -141,39 +129,26 @@ protected void genericResponsePacket(final ChannelHandlerContext context, final } @Override - protected void endOfFilePacket(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { - mysqlPacketPayload.getByteBuf().resetReaderIndex(); - EofPacket eofPacket = new EofPacket(sequenceId, mysqlPacketPayload); + protected void eofPacket(final ChannelHandlerContext context, final MySQLPacketPayload mysqlPacketPayload) { + EofPacket eofPacket = new EofPacket(mysqlPacketPayload); if (mysqlQueryResult.isColumnFinished()) { mysqlQueryResult.setRowFinished(eofPacket); mysqlQueryResult = null; mysqlPacketPayload.getByteBuf().release(); } else { mysqlQueryResult.setColumnFinished(eofPacket); - int connectionId = MySQLResultCache.getInstance().getConnection(context.channel().id().asShortText()); - if (MySQLResultCache.getInstance().getFuture(connectionId) != null) { - MySQLResultCache.getInstance().getFuture(connectionId).setResponse(mysqlQueryResult); - } + setResponse(context); } } @Override - protected void executeCommandResponsePackets(final ChannelHandlerContext context, final int sequenceId, final int header, final MySQLPacketPayload mysqlPacketPayload) { + protected void commonPacket(final MySQLPacketPayload mysqlPacketPayload) { if (mysqlQueryResult == null) { - mysqlQueryResult = new MySQLQueryResult(sequenceId, header); + mysqlQueryResult = new MySQLQueryResult(mysqlPacketPayload); } else if (mysqlQueryResult.needColumnDefinition()) { - mysqlPacketPayload.getByteBuf().resetReaderIndex(); - ColumnDefinition41Packet columnDefinition = new ColumnDefinition41Packet(sequenceId, mysqlPacketPayload); - mysqlQueryResult.addColumnDefinition(columnDefinition); + mysqlQueryResult.addColumnDefinition(new ColumnDefinition41Packet(mysqlPacketPayload)); } else { - mysqlPacketPayload.getByteBuf().resetReaderIndex(); - List data = new ArrayList<>(mysqlQueryResult.getColumnCount()); - for (int i = 1; i <= mysqlQueryResult.getColumnCount(); i++) { - data.add(mysqlPacketPayload.readStringLenenc()); - } - - TextResultSetRowPacket textResultSetRow = new TextResultSetRowPacket(sequenceId, data); - mysqlQueryResult.addTextResultSetRow(textResultSetRow); + mysqlQueryResult.addTextResultSetRow(new TextResultSetRowPacket(mysqlPacketPayload, mysqlQueryResult.getColumnCount())); } } @@ -185,20 +160,27 @@ public void channelInactive(final ChannelHandlerContext ctx) throws Exception { private byte[] securePasswordAuthentication(final byte[] password, final byte[] authPluginData) { try { - MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); - byte[] part1 = sha1.digest(password); - sha1.reset(); - byte[] part2 = sha1.digest(part1); - sha1.reset(); - sha1.update(authPluginData); - byte[] authResponse = sha1.digest(part2); + MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); + byte[] part1 = messageDigest.digest(password); + messageDigest.reset(); + byte[] part2 = messageDigest.digest(part1); + messageDigest.reset(); + messageDigest.update(authPluginData); + byte[] authResponse = messageDigest.digest(part2); for (int i = 0; i < authResponse.length; i++) { authResponse[i] = (byte) (authResponse[i] ^ part1[i]); } return authResponse; } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); + log.error(e.getMessage(), e); } return null; } + + private void setResponse(final ChannelHandlerContext context) { + int connectionId = MySQLResultCache.getInstance().getConnection(context.channel().id().asShortText()); + if (MySQLResultCache.getInstance().getFuture(connectionId) != null) { + MySQLResultCache.getInstance().getFuture(connectionId).setResponse(mysqlQueryResult); + } + } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java index 3b73def7823e8..26e83f4ab9981 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/mysql/MySQLQueryResult.java @@ -29,6 +29,7 @@ import io.shardingsphere.core.merger.QueryResult; import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacket; +import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet; import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.FieldCountPacket; @@ -76,13 +77,14 @@ public MySQLQueryResult() { resultSet = null; } - public MySQLQueryResult(final int sequenceId, final int columnCount) { - commandResponsePackets = new CommandResponsePackets(new FieldCountPacket(sequenceId, columnCount)); - this.columnCount = columnCount; - columnIndexAndLabelMap = new HashMap<>(columnCount, 1); - columnLabelAndIndexMap = new HashMap<>(columnCount, 1); - columnDefinitions = Lists.newArrayListWithCapacity(columnCount); - currentSequenceId = sequenceId; + public MySQLQueryResult(final MySQLPacketPayload mysqlPacketPayload) { + FieldCountPacket fieldCountPacket = new FieldCountPacket(mysqlPacketPayload); + commandResponsePackets = new CommandResponsePackets(fieldCountPacket); + columnCount = fieldCountPacket.getColumnCount(); + columnIndexAndLabelMap = new HashMap<>(fieldCountPacket.getColumnCount(), 1); + columnLabelAndIndexMap = new HashMap<>(fieldCountPacket.getColumnCount(), 1); + columnDefinitions = Lists.newArrayListWithCapacity(fieldCountPacket.getColumnCount()); + currentSequenceId = fieldCountPacket.getSequenceId(); resultSet = new LinkedBlockingQueue<>(); } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacket.java index 2eded13ad29d5..63845bf2b3902 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/MySQLPacket.java @@ -18,25 +18,23 @@ package io.shardingsphere.proxy.transport.mysql.packet; import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; -import lombok.AllArgsConstructor; import lombok.Getter; -import lombok.Setter; +import lombok.RequiredArgsConstructor; /** * MySQL packet. * * @author zhangliang */ -@AllArgsConstructor +@RequiredArgsConstructor @Getter -@Setter public abstract class MySQLPacket implements DatabaseProtocolPacket { public static final int PAYLOAD_LENGTH = 3; public static final int SEQUENCE_LENGTH = 1; - private int sequenceId; + private final int sequenceId; /** * Write packet to byte buffer. diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacket.java index ef6177102b361..b3d8f358bb329 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/CommandPacket.java @@ -19,8 +19,6 @@ import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacket; -import lombok.Getter; -import lombok.Setter; /** * Command packet. @@ -29,21 +27,9 @@ * @author wangkai */ public abstract class CommandPacket extends MySQLPacket { - @Getter - @Setter - private String sql; - - @Getter - private final int connectionId; - - public CommandPacket(final int sequenceId, final int connectionId) { - super(sequenceId); - this.connectionId = connectionId; - } public CommandPacket(final int sequenceId) { super(sequenceId); - this.connectionId = 0; } /** diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java index c78c0b7afefec..373a17bcf071f 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/fieldlist/ComFieldListPacket.java @@ -22,11 +22,13 @@ import io.shardingsphere.proxy.backend.common.SQLExecuteBackendHandler; import io.shardingsphere.proxy.backend.common.SQLPacketsBackendHandler; import io.shardingsphere.proxy.config.RuleRegistry; +import io.shardingsphere.proxy.transport.common.packet.CommandPacketRebuilder; import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacketType; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandResponsePackets; +import io.shardingsphere.proxy.transport.mysql.packet.command.text.query.ComQueryPacket; import lombok.extern.slf4j.Slf4j; /** @@ -37,17 +39,26 @@ * @see COM_FIELD_LIST */ @Slf4j -public final class ComFieldListPacket extends CommandPacket { +public final class ComFieldListPacket extends CommandPacket implements CommandPacketRebuilder { + + private final int connectionId; private final String table; private final String fieldWildcard; public ComFieldListPacket(final int sequenceId, final int connectionId, final MySQLPacketPayload mysqlPacketPayload) { - super(sequenceId, connectionId); + super(sequenceId); + this.connectionId = connectionId; table = mysqlPacketPayload.readStringNul(); fieldWildcard = mysqlPacketPayload.readStringEOF(); - setSql(String.format("SHOW COLUMNS FROM %s FROM %s", table, ShardingConstant.LOGIC_SCHEMA_NAME)); + } + + public ComFieldListPacket(final int sequenceId, final int connectionId, final String table, final String fieldWildcard) { + super(sequenceId); + this.connectionId = connectionId; + this.table = table; + this.fieldWildcard = fieldWildcard; } @Override @@ -61,11 +72,12 @@ public void write(final MySQLPacketPayload mysqlPacketPayload) { public CommandResponsePackets execute() { log.debug("table name received for Sharding-Proxy: {}", table); log.debug("field wildcard received for Sharding-Proxy: {}", fieldWildcard); + String sql = String.format("SHOW COLUMNS FROM %s FROM %s", table, ShardingConstant.LOGIC_SCHEMA_NAME); // TODO use common database type if (RuleRegistry.getInstance().isWithoutJdbc()) { return new SQLPacketsBackendHandler(this, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()).execute(); } else { - return new SQLExecuteBackendHandler(getSql(), DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()).execute(); + return new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()).execute(); } } @@ -78,4 +90,24 @@ public boolean hasMoreResultValue() { public DatabaseProtocolPacket getResultValue() { return null; } + + @Override + public int connectionId() { + return connectionId; + } + + @Override + public int sequenceId() { + return getSequenceId(); + } + + @Override + public String sql() { + return String.format("SHOW COLUMNS FROM %s FROM %s", table, ShardingConstant.LOGIC_SCHEMA_NAME); + } + + @Override + public CommandPacket rebuild(final Object... params) { + return new ComQueryPacket((int) params[0], (int) params[1], (String) params[2]); + } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ColumnDefinition41Packet.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ColumnDefinition41Packet.java index de5d40b08cb16..c789bb0297a2c 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ColumnDefinition41Packet.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ColumnDefinition41Packet.java @@ -72,8 +72,8 @@ public ColumnDefinition41Packet(final int sequenceId, final String schema, final this.decimals = decimals; } - public ColumnDefinition41Packet(final int sequenceId, final MySQLPacketPayload mysqlPacketPayload) { - super(sequenceId); + public ColumnDefinition41Packet(final MySQLPacketPayload mysqlPacketPayload) { + super(mysqlPacketPayload.readInt1()); Preconditions.checkArgument(catalog.equals(mysqlPacketPayload.readStringLenenc())); schema = mysqlPacketPayload.readStringLenenc(); table = mysqlPacketPayload.readStringLenenc(); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java index f3aeb224e7d50..fdce8012a1445 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/ComQueryPacket.java @@ -25,6 +25,7 @@ import io.shardingsphere.proxy.config.RuleRegistry; import io.shardingsphere.proxy.transaction.AtomikosUserTransaction; import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; +import io.shardingsphere.proxy.transport.common.packet.CommandPacketRebuilder; import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket; import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacketType; @@ -40,38 +41,46 @@ * @author linjiaqi */ @Slf4j -public final class ComQueryPacket extends CommandPacket implements Cloneable { +public final class ComQueryPacket extends CommandPacket implements CommandPacketRebuilder { + + private final int connectionId; + + private final String sql; private final SQLExecuteBackendHandler sqlExecuteBackendHandler; private final SQLPacketsBackendHandler sqlPacketsBackendHandler; - + public ComQueryPacket(final int sequenceId, final int connectionId, final MySQLPacketPayload mysqlPacketPayload) { - super(sequenceId, connectionId); - setSql(mysqlPacketPayload.readStringEOF()); - sqlExecuteBackendHandler = new SQLExecuteBackendHandler(getSql(), DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()); - sqlPacketsBackendHandler = new SQLPacketsBackendHandler(this, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()); - } - - @Override - public Object clone() { - try { - return super.clone(); - } catch (CloneNotSupportedException e) { - log.error(e.getMessage(), e); + super(sequenceId); + this.connectionId = connectionId; + sql = mysqlPacketPayload.readStringEOF(); + if (RuleRegistry.getInstance().isWithoutJdbc()) { + sqlExecuteBackendHandler = null; + sqlPacketsBackendHandler = new SQLPacketsBackendHandler(this, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()); + } else { + sqlPacketsBackendHandler = null; + sqlExecuteBackendHandler = new SQLExecuteBackendHandler(sql, DatabaseType.MySQL, RuleRegistry.getInstance().isShowSQL()); } - return null; + } + + public ComQueryPacket(final int sequenceId, final int connectionId, final String sql) { + super(sequenceId); + this.connectionId = connectionId; + this.sql = sql; + sqlExecuteBackendHandler = null; + sqlPacketsBackendHandler = null; } @Override public void write(final MySQLPacketPayload mysqlPacketPayload) { mysqlPacketPayload.writeInt1(CommandPacketType.COM_QUERY.getValue()); - mysqlPacketPayload.writeStringEOF(getSql()); + mysqlPacketPayload.writeStringEOF(sql); } @Override public CommandResponsePackets execute() { - log.debug("COM_QUERY received for Sharding-Proxy: {}", getSql()); + log.debug("COM_QUERY received for Sharding-Proxy: {}", sql); try { doTransactionIntercept(); } catch (final Exception ex) { @@ -113,7 +122,27 @@ public DatabaseProtocolPacket getResultValue() { return sqlExecuteBackendHandler.getResultValue(); } } - + + @Override + public int connectionId() { + return connectionId; + } + + @Override + public int sequenceId() { + return getSequenceId(); + } + + @Override + public String sql() { + return sql; + } + + @Override + public CommandPacket rebuild(final Object... params) { + return new ComQueryPacket((int) params[0], (int) params[1], (String) params[2]); + } + private void doTransactionIntercept() throws Exception { if (RuleRegistry.isXaTransaction()) { if (isXaBegin()) { @@ -125,16 +154,16 @@ private void doTransactionIntercept() throws Exception { } } } - + private boolean isXaBegin() { - return "BEGIN".equalsIgnoreCase(getSql()) || "START TRANSACTION".equalsIgnoreCase(getSql()) || "SET AUTOCOMMIT=0".equalsIgnoreCase(getSql()); + return "BEGIN".equalsIgnoreCase(sql) || "START TRANSACTION".equalsIgnoreCase(sql) || "SET AUTOCOMMIT=0".equalsIgnoreCase(sql); } - + private boolean isXaCommit() { - return "COMMIT".equalsIgnoreCase(getSql()); + return "COMMIT".equalsIgnoreCase(sql); } - + private boolean isXaRollback() { - return "ROLLBACK".equalsIgnoreCase(getSql()); + return "ROLLBACK".equalsIgnoreCase(sql); } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/FieldCountPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/FieldCountPacket.java index 89ae8c75282a9..663c4a6052f81 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/FieldCountPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/FieldCountPacket.java @@ -37,6 +37,11 @@ public FieldCountPacket(final int sequenceId, final int columnCount) { this.columnCount = columnCount; } + public FieldCountPacket(final MySQLPacketPayload mysqlPacketPayload) { + super(mysqlPacketPayload.readInt1()); + columnCount = mysqlPacketPayload.readInt1(); + } + @Override public void write(final MySQLPacketPayload mysqlPacketPayload) { mysqlPacketPayload.writeIntLenenc(columnCount); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/TextResultSetRowPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/TextResultSetRowPacket.java index 3bc9f52132fc8..53f7fcd285c9e 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/TextResultSetRowPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/command/text/query/TextResultSetRowPacket.java @@ -23,6 +23,8 @@ import java.util.List; +import com.google.common.collect.Lists; + /** * Text result set row packet. * @see ResultsetRow @@ -41,6 +43,14 @@ public TextResultSetRowPacket(final int sequenceId, final List data) { this.data = data; } + public TextResultSetRowPacket(final MySQLPacketPayload mysqlPacketPayload, final int columnCount) { + super(mysqlPacketPayload.readInt1()); + data = Lists.newArrayListWithExpectedSize(columnCount); + for (int i = 1; i <= columnCount; i++) { + data.add(mysqlPacketPayload.readStringLenenc()); + } + } + @Override public void write(final MySQLPacketPayload mysqlPacketPayload) { for (Object each : data) { diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/EofPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/EofPacket.java index 794efe90f3ae2..0c87be544e812 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/EofPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/EofPacket.java @@ -32,7 +32,7 @@ @Getter public class EofPacket extends MySQLPacket { - public static final int HEADER = 0xfe; + private static final int HEADER = 0xfe; private final int warnings; @@ -44,8 +44,8 @@ public EofPacket(final int sequenceId, final int warnings, final int statusFlags this.statusFlags = statusFlags; } - public EofPacket(final int sequenceId, final MySQLPacketPayload mysqlPacketPayload) { - super(sequenceId); + public EofPacket(final MySQLPacketPayload mysqlPacketPayload) { + super(mysqlPacketPayload.readInt1()); Preconditions.checkArgument(HEADER == mysqlPacketPayload.readInt1()); warnings = mysqlPacketPayload.readInt2(); statusFlags = mysqlPacketPayload.readInt2(); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/ErrPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/ErrPacket.java index 9d5832ef0b6fb..5b2752d31a258 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/ErrPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/ErrPacket.java @@ -32,7 +32,7 @@ @Getter public class ErrPacket extends MySQLPacket { - public static final int HEADER = 0xff; + private static final int HEADER = 0xff; private final int errorCode; @@ -50,8 +50,8 @@ public ErrPacket(final int sequenceId, final int errorCode, final String sqlStat this.errorMessage = errorMessage; } - public ErrPacket(final int sequenceId, final MySQLPacketPayload mysqlPacketPayload) { - super(sequenceId); + public ErrPacket(final MySQLPacketPayload mysqlPacketPayload) { + super(mysqlPacketPayload.readInt1()); Preconditions.checkArgument(HEADER == mysqlPacketPayload.readInt1()); errorCode = mysqlPacketPayload.readInt2(); sqlStateMarker = mysqlPacketPayload.readStringFix(1); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/OKPacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/OKPacket.java index 6bbfbf60eb08e..7b6256259240b 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/OKPacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/generic/OKPacket.java @@ -32,7 +32,7 @@ @Getter public class OKPacket extends MySQLPacket { - public static final int HEADER = 0x00; + private static final int HEADER = 0x00; private final long affectedRows; @@ -53,8 +53,8 @@ public OKPacket(final int sequenceId, final long affectedRows, final long lastIn this.info = info; } - public OKPacket(final int sequenceId, final MySQLPacketPayload mysqlPacketPayload) { - super(sequenceId); + public OKPacket(final MySQLPacketPayload mysqlPacketPayload) { + super(mysqlPacketPayload.readInt1()); Preconditions.checkArgument(HEADER == mysqlPacketPayload.readInt1()); affectedRows = mysqlPacketPayload.readIntLenenc(); lastInsertId = mysqlPacketPayload.readIntLenenc(); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/handshake/AuthPluginData.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/handshake/AuthPluginData.java index 719734ed3f25d..6dad5ce2d7cc4 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/handshake/AuthPluginData.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/handshake/AuthPluginData.java @@ -44,4 +44,10 @@ public AuthPluginData() { authPluginDataPart2 = RandomGenerator.getInstance().generateRandomBytes(12); authPluginData = Bytes.concat(authPluginDataPart1, authPluginDataPart2); } + + public AuthPluginData(final byte[] authPluginDataPart1, final byte[] authPluginDataPart2) { + this.authPluginDataPart1 = authPluginDataPart1; + this.authPluginDataPart2 = authPluginDataPart2; + authPluginData = Bytes.concat(this.authPluginDataPart1, this.authPluginDataPart2); + } } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/handshake/HandshakePacket.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/handshake/HandshakePacket.java index 50157e49507ce..8be411032ac5d 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/handshake/HandshakePacket.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/packet/handshake/HandshakePacket.java @@ -17,6 +17,8 @@ package io.shardingsphere.proxy.transport.mysql.packet.handshake; +import com.google.common.base.Preconditions; + import io.shardingsphere.proxy.transport.mysql.constant.CapabilityFlag; import io.shardingsphere.proxy.transport.mysql.constant.ServerInfo; import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; @@ -30,6 +32,7 @@ * @see Handshake * * @author zhangliang + * @author linjiaqi */ @Getter public class HandshakePacket extends MySQLPacket { @@ -56,6 +59,22 @@ public HandshakePacket(final int connectionId, final AuthPluginData authPluginDa this.authPluginData = authPluginData; } + public HandshakePacket(final MySQLPacketPayload mysqlPacketPayload) { + super(mysqlPacketPayload.readInt1()); + Preconditions.checkArgument(protocolVersion == mysqlPacketPayload.readInt1()); + mysqlPacketPayload.readStringNul(); + connectionId = mysqlPacketPayload.readInt4(); + byte[] authPluginDataPart1 = mysqlPacketPayload.readStringNul().getBytes(); + mysqlPacketPayload.readInt2(); + mysqlPacketPayload.readInt1(); + Preconditions.checkArgument(statusFlag.getValue() == mysqlPacketPayload.readInt2()); + mysqlPacketPayload.readInt2(); + mysqlPacketPayload.readInt1(); + mysqlPacketPayload.skipReserved(10); + byte[] authPluginDataPart2 = mysqlPacketPayload.readStringNul().getBytes(); + authPluginData = new AuthPluginData(authPluginDataPart1, authPluginDataPart2); + } + @Override public void write(final MySQLPacketPayload mysqlPacketPayload) { mysqlPacketPayload.writeInt1(protocolVersion); From 7c9b5a1b47de23461feadefe8b9e33ccd50d7ecc Mon Sep 17 00:00:00 2001 From: saaav Date: Thu, 14 Jun 2018 15:20:15 +0800 Subject: [PATCH 255/351] clear packet path --- .../jdbc/orchestration/reg/newzk/client/action/IClient.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IClient.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IClient.java index f0f38368d2bdd..903ac958cc86f 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IClient.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/action/IClient.java @@ -19,6 +19,7 @@ import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.StrategyType; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction; import java.io.IOException; import java.util.concurrent.TimeUnit; @@ -28,7 +29,7 @@ * * @author lidongbo */ -public interface IClient extends io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IAction, IGroupAction { +public interface IClient extends IAction, IGroupAction { /** * start. @@ -80,7 +81,7 @@ public interface IClient extends io.shardingsphere.jdbc.orchestration.reg.newzk. * * @return ZKTransaction */ - io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.transaction.ZKTransaction transaction(); + ZKTransaction transaction(); /* void createNamespace(); void deleteNamespace(); From 6fb31bd1371c5b840aa813063c08832acdd12eb9 Mon Sep 17 00:00:00 2001 From: tuohai666 Date: Thu, 14 Jun 2018 17:13:02 +0800 Subject: [PATCH 256/351] infinite looping is not necessary --- .../proxy/backend/common/SQLExecuteBackendHandler.java | 3 --- .../proxy/backend/common/StatementExecuteBackendHandler.java | 3 --- 2 files changed, 6 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java index 26d7cd51949d8..7decec105aeb1 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java @@ -141,9 +141,6 @@ private CommandResponsePackets executeForSharding() { private void getCommandResponsePackets(final List> resultList, final List packets) { for (Future each : resultList) { try { - while (!each.isDone()) { - continue; - } packets.add(each.get()); } catch (final InterruptedException | ExecutionException ex) { throw new ShardingException(ex.getMessage(), ex); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java index 24eeb2359c717..e5885e2ccf872 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java @@ -153,9 +153,6 @@ private CommandResponsePackets executeForSharding() { private void getCommandResponsePackets(final List> resultList, final List packets) { for (Future each : resultList) { try { - while (!each.isDone()) { - continue; - } packets.add(each.get()); } catch (final InterruptedException | ExecutionException ex) { throw new ShardingException(ex.getMessage(), ex); From 0223010c861d5ba60d2a3826ec9f64a658365bb0 Mon Sep 17 00:00:00 2001 From: linjiaqi Date: Thu, 14 Jun 2018 18:28:56 +0800 Subject: [PATCH 257/351] fix compile errors --- .../common/packet/CommandPacketRebuilder.java | 57 +++++++++++++++++++ .../mysql/constant/PacketHeader.java | 39 +++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/common/packet/CommandPacketRebuilder.java create mode 100644 sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/constant/PacketHeader.java diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/common/packet/CommandPacketRebuilder.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/common/packet/CommandPacketRebuilder.java new file mode 100644 index 0000000000000..89976bf055cdd --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/common/packet/CommandPacketRebuilder.java @@ -0,0 +1,57 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.transport.common.packet; + +import io.shardingsphere.proxy.transport.mysql.packet.command.CommandPacket; + +/** + * CommandPacket Rebuilder. + * + * @author linjiaqi + */ +public interface CommandPacketRebuilder { + + /** + * get connectionId. + * + * @return connectionId + */ + int connectionId(); + + /** + * get sequenceId. + * + * @return sequenceId + */ + int sequenceId(); + + /** + * get sql. + * + * @return sql + */ + String sql(); + + /** + * CommandPacket rebuild by params. + * + * @param params rebuild params + * @return rebuild CommandPacket + */ + CommandPacket rebuild(Object... params); +} diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/constant/PacketHeader.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/constant/PacketHeader.java new file mode 100644 index 0000000000000..e59b7f11e12c6 --- /dev/null +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/transport/mysql/constant/PacketHeader.java @@ -0,0 +1,39 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.proxy.transport.mysql.constant; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +/** + * Generic Packet Header. + * + * @author linjiaqi + */ +@RequiredArgsConstructor +@Getter +public enum PacketHeader { + + OK(0x00), + + EOF(0xfe), + + ERR(0xff); + + private final int value; +} From 2935aee40f2a1f4a041df6a37a308b667187a0be Mon Sep 17 00:00:00 2001 From: terrymanu Date: Thu, 14 Jun 2018 18:29:30 +0800 Subject: [PATCH 258/351] for #601, add select_sub_query_with_group_by for masterslave sharding rule --- .../resources/asserts/cases/select/dql-integrate-test-cases.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 6e33ee3991279..52532189a9c38 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -487,5 +487,6 @@ + From 4ffa7e12cf463dbcbcc45201f59f26077eeb4567 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Thu, 14 Jun 2018 18:30:32 +0800 Subject: [PATCH 259/351] for #601, add select_sub_query_with_order_by for masterslave sharding rule --- .../resources/asserts/cases/select/dql-integrate-test-cases.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 52532189a9c38..2f9af0f84b0dd 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -481,6 +481,7 @@ + From d5094819b0930aa5289c20d7918a3df0611a65fd Mon Sep 17 00:00:00 2001 From: terrymanu Date: Thu, 14 Jun 2018 18:43:50 +0800 Subject: [PATCH 260/351] for #601, add select_sub_query_with_order_by for select_sub_query_with_single_table sharding rule --- .../dataset/masterslave/select_sub_query_with_group_by.xml | 4 ++++ .../masterslave/select_sub_query_with_multiple_tables.xml | 7 +++++++ .../dataset/masterslave/select_sub_query_with_order_by.xml | 4 ++++ .../masterslave/select_sub_query_with_single_table.xml | 5 +++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 2 ++ 5 files changed, 22 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_group_by.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_multiple_tables.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_order_by.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_group_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_group_by.xml new file mode 100644 index 0000000000000..59807cecf4073 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_group_by.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_multiple_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_multiple_tables.xml new file mode 100644 index 0000000000000..d70f001558d9a --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_multiple_tables.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_order_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_order_by.xml new file mode 100644 index 0000000000000..deeb58cbcbb9f --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_order_by.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_single_table.xml new file mode 100644 index 0000000000000..1026341c2c762 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_single_table.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 2f9af0f84b0dd..600e29cc04400 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -469,12 +469,14 @@ + + From 4c2bd79fcb3dbabfc9a7940adf9625872612ed1c Mon Sep 17 00:00:00 2001 From: tuohai666 Date: Thu, 14 Jun 2018 20:14:52 +0800 Subject: [PATCH 261/351] for #930, update mysql-connector-java to 8.0.11 --- sharding-proxy/pom.xml | 1 + .../proxy/config/DefaultProxyRawDataSource.java | 2 +- sharding-proxy/src/main/resources/conf/config.yaml | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/sharding-proxy/pom.xml b/sharding-proxy/pom.xml index 3154be9d39dd6..7c4d45189418a 100644 --- a/sharding-proxy/pom.xml +++ b/sharding-proxy/pom.xml @@ -44,6 +44,7 @@ mysql mysql-connector-java + 8.0.11 runtime diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/DefaultProxyRawDataSource.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/DefaultProxyRawDataSource.java index 727bef9fdb42f..66a3b8386e1cb 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/DefaultProxyRawDataSource.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/DefaultProxyRawDataSource.java @@ -40,7 +40,7 @@ public DefaultProxyRawDataSource(final Map dataSour protected Map buildInternal(final String key, final DataSourceParameter dataSourceParameter) { final Map result = new HashMap<>(128, 1); HikariConfig config = new HikariConfig(); - config.setDriverClassName("com.mysql.jdbc.Driver"); + config.setDriverClassName("com.mysql.cj.jdbc.Driver"); config.setJdbcUrl(dataSourceParameter.getUrl()); config.setUsername(dataSourceParameter.getUsername()); config.setPassword(dataSourceParameter.getPassword()); diff --git a/sharding-proxy/src/main/resources/conf/config.yaml b/sharding-proxy/src/main/resources/conf/config.yaml index d5caa8de45704..cf915b75cf16e 100644 --- a/sharding-proxy/src/main/resources/conf/config.yaml +++ b/sharding-proxy/src/main/resources/conf/config.yaml @@ -13,7 +13,7 @@ #dataSources: # ds_0: -# url: jdbc:mysql://127.0.0.1:3306/ds_0 +# url: jdbc:mysql://127.0.0.1:3306/ds_0?serverTimezone=UTC&useSSL=false # username: root # password: # autoCommit: true @@ -22,7 +22,7 @@ # maxLifetime: 1800000 # maximumPoolSize: 65 # ds_1: -# url: jdbc:mysql://127.0.0.1:3306/ds_1 +# url: jdbc:mysql://127.0.0.1:3306/ds_1?serverTimezone=UTC&useSSL=false # username: root # password: # autoCommit: true From 464dc630ec1c9906b5ca5f957859566d0d0d2b1c Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 12:51:50 +0800 Subject: [PATCH 262/351] for #601, add select_join_using sharding rule --- pom.xml | 2 +- .../internal/json/DataSourceJsonConverterTest.java | 12 ++++++------ .../src/main/resources/conf/config.yaml | 12 ++++++------ .../select/dataset/masterslave/select_inner_join.xml | 5 +++++ .../cases/select/dql-integrate-test-cases.xml | 1 + sharding-proxy/src/main/resources/conf/config.yaml | 4 ++-- sharding-proxy/src/test/resources/conf/config.yaml | 12 ++++++------ 7 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_inner_join.xml diff --git a/pom.xml b/pom.xml index a3a5ac365180a..52c72078d2f4a 100644 --- a/pom.xml +++ b/pom.xml @@ -38,7 +38,7 @@ 4.1.16.Final [4.3.6.RELEASE,5.0.0.M1) [1.5.0.RELEASE,2.0.0.M1) - 5.1.30 + 8.0.11 1.4.196 9.1-901-1.jdbc4 6.1.7.jre7-preview diff --git a/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/jdbc/orchestration/internal/json/DataSourceJsonConverterTest.java b/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/jdbc/orchestration/internal/json/DataSourceJsonConverterTest.java index 14d0fe81dc02b..fe76279e9c481 100644 --- a/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/jdbc/orchestration/internal/json/DataSourceJsonConverterTest.java +++ b/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/jdbc/orchestration/internal/json/DataSourceJsonConverterTest.java @@ -17,7 +17,7 @@ package io.shardingsphere.jdbc.orchestration.internal.json; -import com.mysql.jdbc.Driver; +import com.mysql.cj.jdbc.Driver; import org.apache.commons.dbcp2.BasicDataSource; import org.junit.Test; @@ -31,20 +31,20 @@ public final class DataSourceJsonConverterTest { private final String dataSourceMapJson = "[{\"shardingJdbcDataSourceName\":\"ds_jdbc_1\",\"shardingJdbcDataSourceClazz\":\"org.apache.commons.dbcp2.BasicDataSource\"," - + "\"abandonedUsageTracking\":\"false\",\"cacheState\":\"true\",\"defaultTransactionIsolation\":\"-1\",\"driverClassName\":\"com.mysql.jdbc.Driver\"," + + "\"abandonedUsageTracking\":\"false\",\"cacheState\":\"true\",\"defaultTransactionIsolation\":\"-1\",\"driverClassName\":\"com.mysql.cj.jdbc.Driver\"," + "\"enableAutoCommitOnReturn\":\"true\",\"evictionPolicyClassName\":\"org.apache.commons.pool2.impl.DefaultEvictionPolicy\",\"fastFailValidation\":\"false\"," + "\"initialSize\":\"0\",\"lifo\":\"true\",\"logAbandoned\":\"false\",\"logExpiredConnections\":\"true\",\"maxConnLifetimeMillis\":\"-1\",\"maxIdle\":\"8\"," + "\"maxOpenPreparedStatements\":\"-1\",\"maxTotal\":\"8\",\"maxWaitMillis\":\"-1\",\"minEvictableIdleTimeMillis\":\"1800000\",\"minIdle\":\"0\",\"numTestsPerEvictionRun\":\"3\"," + "\"password\":\"\",\"removeAbandonedOnBorrow\":\"false\",\"removeAbandonedOnMaintenance\":\"false\",\"removeAbandonedTimeout\":\"300\",\"rollbackOnReturn\":\"true\"," + "\"softMinEvictableIdleTimeMillis\":\"-1\",\"testOnBorrow\":\"true\",\"testOnCreate\":\"false\",\"testOnReturn\":\"false\",\"testWhileIdle\":\"false\"," - + "\"timeBetweenEvictionRunsMillis\":\"-1\",\"url\":\"jdbc:mysql://localhost:3306/ds_jdbc_1\",\"username\":\"root\",\"validationQueryTimeout\":\"-1\"}," + + "\"timeBetweenEvictionRunsMillis\":\"-1\",\"url\":\"jdbc:mysql://localhost:3306/ds_jdbc_1?serverTimezone=UTC&useSSL=false\",\"username\":\"root\",\"validationQueryTimeout\":\"-1\"}," + "{\"shardingJdbcDataSourceName\":\"ds_jdbc_0\",\"shardingJdbcDataSourceClazz\":\"org.apache.commons.dbcp2.BasicDataSource\",\"abandonedUsageTracking\":\"false\"," - + "\"cacheState\":\"true\",\"defaultTransactionIsolation\":\"-1\",\"driverClassName\":\"com.mysql.jdbc.Driver\",\"enableAutoCommitOnReturn\":\"true\"," + + "\"cacheState\":\"true\",\"defaultTransactionIsolation\":\"-1\",\"driverClassName\":\"com.mysql.cj.jdbc.Driver\",\"enableAutoCommitOnReturn\":\"true\"," + "\"evictionPolicyClassName\":\"org.apache.commons.pool2.impl.DefaultEvictionPolicy\",\"fastFailValidation\":\"false\",\"initialSize\":\"0\",\"lifo\":\"true\"," + "\"logAbandoned\":\"false\",\"logExpiredConnections\":\"true\",\"maxConnLifetimeMillis\":\"-1\",\"maxIdle\":\"8\",\"maxOpenPreparedStatements\":\"-1\",\"maxTotal\":\"8\"," + "\"maxWaitMillis\":\"-1\",\"minEvictableIdleTimeMillis\":\"1800000\",\"minIdle\":\"0\",\"numTestsPerEvictionRun\":\"3\",\"password\":\"\",\"removeAbandonedOnBorrow\":\"false\"," + "\"removeAbandonedOnMaintenance\":\"false\",\"removeAbandonedTimeout\":\"300\",\"rollbackOnReturn\":\"true\",\"softMinEvictableIdleTimeMillis\":\"-1\",\"testOnBorrow\":\"true\"," - + "\"testOnCreate\":\"false\",\"testOnReturn\":\"false\",\"testWhileIdle\":\"false\",\"timeBetweenEvictionRunsMillis\":\"-1\",\"url\":\"jdbc:mysql://localhost:3306/ds_jdbc_0\"," + + "\"testOnCreate\":\"false\",\"testOnReturn\":\"false\",\"testWhileIdle\":\"false\",\"timeBetweenEvictionRunsMillis\":\"-1\",\"url\":\"jdbc:mysql://localhost:3306/ds_jdbc_0?serverTimezone=UTC&useSSL=false\"," + "\"username\":\"root\",\"validationQueryTimeout\":\"-1\"}]"; @Test @@ -77,7 +77,7 @@ private Map createDataSourceMap() { private DataSource createDataSource(final String dataSourceName) { BasicDataSource result = new BasicDataSource(); result.setDriverClassName(Driver.class.getName()); - result.setUrl(String.format("jdbc:mysql://localhost:3306/%s", dataSourceName)); + result.setUrl(String.format("jdbc:mysql://localhost:3306/%s?serverTimezone=UTC&useSSL=false", dataSourceName)); result.setUsername("root"); result.setPassword(""); return result; diff --git a/sharding-jdbc-transaction-parent/sharding-jdbc-transaction-async-job/src/main/resources/conf/config.yaml b/sharding-jdbc-transaction-parent/sharding-jdbc-transaction-async-job/src/main/resources/conf/config.yaml index e6015f049378a..4926c35ffdad1 100644 --- a/sharding-jdbc-transaction-parent/sharding-jdbc-transaction-async-job/src/main/resources/conf/config.yaml +++ b/sharding-jdbc-transaction-parent/sharding-jdbc-transaction-async-job/src/main/resources/conf/config.yaml @@ -1,21 +1,21 @@ #Transaction log data source. targetDataSource: ds_trans_0: !!org.apache.commons.dbcp2.BasicDataSource - driverClassName: com.mysql.jdbc.Driver - url: jdbc:mysql://localhost:3306/ds_trans_0 + driverClassName: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/ds_trans_0?serverTimezone=UTC&useSSL=false username: root password: ds_trans_1: !!org.apache.commons.dbcp2.BasicDataSource - driverClassName: com.mysql.jdbc.Driver - url: jdbc:mysql://localhost:3306/ds_trans_1 + driverClassName: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/ds_trans_1?serverTimezone=UTC&useSSL=false username: root password: #Data source for transaction manager. transactionLogDataSource: ds_trans: !!org.apache.commons.dbcp2.BasicDataSource - driverClassName: com.mysql.jdbc.Driver - url: jdbc:mysql://localhost:3306/trans_log + driverClassName: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/trans_log?serverTimezone=UTC&useSSL=false username: root password: diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_inner_join.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_inner_join.xml new file mode 100644 index 0000000000000..be25108ecc8b4 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_inner_join.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 600e29cc04400..cf91b2958191c 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -463,6 +463,7 @@ + diff --git a/sharding-proxy/src/main/resources/conf/config.yaml b/sharding-proxy/src/main/resources/conf/config.yaml index cf915b75cf16e..dd122fefa95eb 100644 --- a/sharding-proxy/src/main/resources/conf/config.yaml +++ b/sharding-proxy/src/main/resources/conf/config.yaml @@ -73,7 +73,7 @@ #dataSources: # ds_0: -# url: jdbc:mysql://127.0.0.1:3306/ds_0 +# url: jdbc:mysql://127.0.0.1:3306/ds_0?serverTimezone=UTC&useSSL=false # username: root # password: # autoCommit: true @@ -82,7 +82,7 @@ # maxLifetime: 1800000 # maximumPoolSize: 65 # ds_1: -# url: jdbc:mysql://127.0.0.1:3306/ds_1 +# url: jdbc:mysql://127.0.0.1:3306/ds_1?serverTimezone=UTC&useSSL=false # username: root # password: # autoCommit: true diff --git a/sharding-proxy/src/test/resources/conf/config.yaml b/sharding-proxy/src/test/resources/conf/config.yaml index fb2c07623100b..5d1286df15753 100644 --- a/sharding-proxy/src/test/resources/conf/config.yaml +++ b/sharding-proxy/src/test/resources/conf/config.yaml @@ -1,26 +1,26 @@ dataSources: ds_master_0: - url: jdbc:mysql://localhost:3306/demo_ds_master_0 + url: jdbc:mysql://localhost:3306/demo_ds_master_0?serverTimezone=UTC&useSSL=false username: root password: ds_master_0_slave_0: - url: jdbc:mysql://localhost:3306/demo_ds_master_0_slave_0 + url: jdbc:mysql://localhost:3306/demo_ds_master_0_slave_0?serverTimezone=UTC&useSSL=false username: root password: ds_master_0_slave_1: - url: jdbc:mysql://localhost:3306/demo_ds_master_0_slave_1 + url: jdbc:mysql://localhost:3306/demo_ds_master_0_slave_1?serverTimezone=UTC&useSSL=false username: root password: ds_master_1: - url: jdbc:mysql://localhost:3306/demo_ds_master_1 + url: jdbc:mysql://localhost:3306/demo_ds_master_1?serverTimezone=UTC&useSSL=false username: root password: ds_master_1_slave_0: - url: jdbc:mysql://localhost:3306/demo_ds_master_1_slave_0 + url: jdbc:mysql://localhost:3306/demo_ds_master_1_slave_0?serverTimezone=UTC&useSSL=false username: root password: ds_master_1_slave_1: - url: jdbc:mysql://localhost:3306/demo_ds_master_1_slave_1 + url: jdbc:mysql://localhost:3306/demo_ds_master_1_slave_1?serverTimezone=UTC&useSSL=false username: root password: From 7a4b90ef93f68ed1e6e86b061301a4364a0b4d7a Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 12:54:30 +0800 Subject: [PATCH 263/351] upgrade mysql driver to 8.x --- .../core/common/env/DatabaseEnvironment.java | 4 +- .../env/ShardingJdbcDatabaseTester.java | 2 +- .../dbtest/cases/assertion/root/SQLValue.java | 4 +- .../dbtest/engine/DMLIntegrateTest.java | 4 +- .../dbtest/engine/DQLIntegrateTest.java | 6 +- .../env/datasource/DatabaseEnvironment.java | 6 +- .../shardingsphere/dbtest/util/DateUtil.java | 59 +++++++++++++++++++ sharding-proxy/pom.xml | 1 - 8 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/dbtest/util/DateUtil.java diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/common/env/DatabaseEnvironment.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/common/env/DatabaseEnvironment.java index e343f322ce108..06cf180a7397a 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/common/env/DatabaseEnvironment.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/common/env/DatabaseEnvironment.java @@ -52,8 +52,8 @@ private void fillData() { PASSWORD.put(DatabaseType.H2, ""); SCHEMA.put(DatabaseType.H2, null); - DRIVER_CLASS_NAME.put(DatabaseType.MySQL, com.mysql.jdbc.Driver.class.getName()); - URL.put(DatabaseType.MySQL, "jdbc:mysql://db.mysql:3306/%s"); + DRIVER_CLASS_NAME.put(DatabaseType.MySQL, com.mysql.cj.jdbc.Driver.class.getName()); + URL.put(DatabaseType.MySQL, "jdbc:mysql://db.mysql:3306/%s?serverTimezone=UTC&useSSL=false"); USERNAME.put(DatabaseType.MySQL, "root"); PASSWORD.put(DatabaseType.MySQL, ""); SCHEMA.put(DatabaseType.MySQL, null); diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/common/env/ShardingJdbcDatabaseTester.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/common/env/ShardingJdbcDatabaseTester.java index d7c2227343f66..0dffb3427249a 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/common/env/ShardingJdbcDatabaseTester.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/common/env/ShardingJdbcDatabaseTester.java @@ -47,7 +47,7 @@ public IDatabaseConnection getConnection() throws Exception { case "org.h2.Driver": dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new H2DataTypeFactory()); break; - case "com.mysql.jdbc.Driver": + case "com.mysql.cj.jdbc.Driver": dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory()); break; case "org.postgresql.Driver": diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/assertion/root/SQLValue.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/assertion/root/SQLValue.java index 0c9401d7927a4..3725ec87dc3c5 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/assertion/root/SQLValue.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/assertion/root/SQLValue.java @@ -17,11 +17,11 @@ package io.shardingsphere.dbtest.cases.assertion.root; +import io.shardingsphere.dbtest.util.DateUtil; import lombok.Getter; import java.sql.Date; import java.text.ParseException; -import java.text.SimpleDateFormat; /** * SQL value. @@ -54,7 +54,7 @@ private Object getValue(final String value, final String type) throws ParseExcep return Double.valueOf(value); } if ("datetime".equals(type)) { - return new Date(new SimpleDateFormat("yyyy-MM-dd").parse(value).getTime()); + return new Date(DateUtil.parse(value).getTime()); } throw new UnsupportedOperationException(String.format("Cannot support type: '%s'", type)); } diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java index 77b6e5d9562f8..a2d5246fe882b 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java @@ -31,6 +31,7 @@ import io.shardingsphere.dbtest.env.DatabaseTypeEnvironment; import io.shardingsphere.dbtest.env.EnvironmentPath; import io.shardingsphere.dbtest.env.dataset.DataSetEnvironmentManager; +import io.shardingsphere.dbtest.util.DateUtil; import io.shardingsphere.test.sql.SQLCaseType; import io.shardingsphere.test.sql.SQLCasesLoader; import org.junit.After; @@ -52,7 +53,6 @@ import java.sql.Statement; import java.sql.Types; import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Collection; import java.util.LinkedList; @@ -212,7 +212,7 @@ private void assertDataSets(final ResultSet actualResultSet, final List + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.dbtest.util; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +/** + * Date utility. + * + * @author zhangliang + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class DateUtil { + + /** + * Parse date string to date. + * @param date date string + * @return date + * @throws ParseException parse exception + */ + public static Date parse(final String date) throws ParseException { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); + simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + return simpleDateFormat.parse(date); + } + + /** + * Parse date to string. + * + * @param date date + * @return date string + */ + public static String format(final Date date) { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); + simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + return simpleDateFormat.format(date); + } +} diff --git a/sharding-proxy/pom.xml b/sharding-proxy/pom.xml index 7c4d45189418a..3154be9d39dd6 100644 --- a/sharding-proxy/pom.xml +++ b/sharding-proxy/pom.xml @@ -44,7 +44,6 @@ mysql mysql-connector-java - 8.0.11 runtime From b2183b3b296a502cad11a2772805a832c7d62697 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 13:09:32 +0800 Subject: [PATCH 264/351] for # 601: add masterslave rule for case select_inner_join_related_with_name --- .../resources/asserts/cases/select/dql-integrate-test-cases.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index cf91b2958191c..eb29452f22c06 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -457,6 +457,7 @@ +
From 053d6c8c0cdc17a5a3ae011c2427d5d469d228b3 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 13:09:58 +0800 Subject: [PATCH 265/351] for # 601: add default timezone --- .../io/shardingsphere/dbtest/engine/BaseIntegrateTest.java | 5 +++++ .../test/java/io/shardingsphere/dbtest/util/DateUtil.java | 5 ++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/BaseIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/BaseIntegrateTest.java index 97378f198c96f..142d3203f8b8c 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/BaseIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/BaseIntegrateTest.java @@ -48,6 +48,7 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.Map; +import java.util.TimeZone; @RunWith(Parameterized.class) @Getter(AccessLevel.PROTECTED) @@ -71,6 +72,10 @@ public abstract class BaseIntegrateTest { private final DataSource dataSource; + static { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + } + public BaseIntegrateTest(final String sqlCaseId, final String path, final IntegrateTestCaseAssertion assertion, final DatabaseTypeEnvironment databaseTypeEnvironment, final SQLCaseType caseType, final int countInSameCase) throws IOException, JAXBException, SQLException { this.databaseTypeEnvironment = databaseTypeEnvironment; diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/util/DateUtil.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/util/DateUtil.java index d226f6b2865b8..5c38013527d82 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/util/DateUtil.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/util/DateUtil.java @@ -23,7 +23,6 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; -import java.util.TimeZone; /** * Date utility. @@ -41,7 +40,7 @@ public final class DateUtil { */ public static Date parse(final String date) throws ParseException { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); - simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); +// simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); return simpleDateFormat.parse(date); } @@ -53,7 +52,7 @@ public static Date parse(final String date) throws ParseException { */ public static String format(final Date date) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); - simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); +// simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); return simpleDateFormat.format(date); } } From f89bebc9cd8bc9237f22323cd0e83682680bd296 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 14:07:27 +0800 Subject: [PATCH 266/351] for # 601: remove DateUtil --- .../dbtest/cases/assertion/root/SQLValue.java | 4 +- .../dbtest/engine/DMLIntegrateTest.java | 4 +- .../dbtest/engine/DQLIntegrateTest.java | 6 +- .../shardingsphere/dbtest/util/DateUtil.java | 58 ------------------- 4 files changed, 7 insertions(+), 65 deletions(-) delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/dbtest/util/DateUtil.java diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/assertion/root/SQLValue.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/assertion/root/SQLValue.java index 3725ec87dc3c5..0c9401d7927a4 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/assertion/root/SQLValue.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/assertion/root/SQLValue.java @@ -17,11 +17,11 @@ package io.shardingsphere.dbtest.cases.assertion.root; -import io.shardingsphere.dbtest.util.DateUtil; import lombok.Getter; import java.sql.Date; import java.text.ParseException; +import java.text.SimpleDateFormat; /** * SQL value. @@ -54,7 +54,7 @@ private Object getValue(final String value, final String type) throws ParseExcep return Double.valueOf(value); } if ("datetime".equals(type)) { - return new Date(DateUtil.parse(value).getTime()); + return new Date(new SimpleDateFormat("yyyy-MM-dd").parse(value).getTime()); } throw new UnsupportedOperationException(String.format("Cannot support type: '%s'", type)); } diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java index a2d5246fe882b..d066d896c19c9 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java @@ -31,7 +31,6 @@ import io.shardingsphere.dbtest.env.DatabaseTypeEnvironment; import io.shardingsphere.dbtest.env.EnvironmentPath; import io.shardingsphere.dbtest.env.dataset.DataSetEnvironmentManager; -import io.shardingsphere.dbtest.util.DateUtil; import io.shardingsphere.test.sql.SQLCaseType; import io.shardingsphere.test.sql.SQLCasesLoader; import org.junit.After; @@ -53,6 +52,7 @@ import java.sql.Statement; import java.sql.Types; import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Collection; import java.util.LinkedList; @@ -212,7 +212,7 @@ private void assertDataSets(final ResultSet actualResultSet, final List - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.dbtest.util; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; - -/** - * Date utility. - * - * @author zhangliang - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class DateUtil { - - /** - * Parse date string to date. - * @param date date string - * @return date - * @throws ParseException parse exception - */ - public static Date parse(final String date) throws ParseException { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); -// simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - return simpleDateFormat.parse(date); - } - - /** - * Parse date to string. - * - * @param date date - * @return date string - */ - public static String format(final Date date) { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); -// simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - return simpleDateFormat.format(date); - } -} From e6cfb7cab9d5b5e8883ef5f5e0199d40584f0259 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 14:08:37 +0800 Subject: [PATCH 267/351] for # 601: add masterslave rule for case select_inner_join_related_with_alias --- .../resources/asserts/cases/select/dql-integrate-test-cases.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index eb29452f22c06..f451e626cd774 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -451,6 +451,7 @@ +
From 3ac8a63b47fac1968296b6de3a6857b0cb51115f Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 14:13:09 +0800 Subject: [PATCH 268/351] for # 601: add masterslave rule for case select_pagination_with_row_number_and_limit --- .../select_pagination_with_row_number_and_limit.xml | 5 +++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 6 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_number_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_number_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_number_and_limit.xml new file mode 100644 index 0000000000000..1403c0657f46a --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_number_and_limit.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index f451e626cd774..8a12f7ba01065 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -445,6 +445,7 @@ + From 464876225d41e2112059d143119e9b53f40a7953 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 14:13:41 +0800 Subject: [PATCH 269/351] for # 601: add masterslave rule for case select_pagination_with_row_number_for_greater_than --- .../resources/asserts/cases/select/dql-integrate-test-cases.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 8a12f7ba01065..01d5c0b2d7579 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -439,6 +439,7 @@ + From ccc65d5840687cbe3bbe67412e9c65a881d6376d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 14:18:12 +0800 Subject: [PATCH 270/351] for # 601: add masterslave rule for case select_pagination_with_top_for_greater_than_and_equal --- .../db/select_pagination_with_row_number_and_limit.xml | 4 ++-- .../select_pagination_with_row_number_and_limit.xml | 4 ++-- .../select_pagination_with_row_number_and_limit.xml | 6 +++--- .../masterslave/select_pagination_with_top_and_limit.xml | 5 +++++ .../tbl/select_pagination_with_row_number_and_limit.xml | 4 ++-- .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 6 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_top_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_number_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_number_and_limit.xml index 463c8a1bf0f96..794208d2a73c2 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_number_and_limit.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_number_and_limit.xml @@ -1,5 +1,5 @@ - - + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_number_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_number_and_limit.xml index eebe75b4da717..ade9d20589e19 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_number_and_limit.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_number_and_limit.xml @@ -1,5 +1,5 @@ - - + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_number_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_number_and_limit.xml index 1403c0657f46a..8a74aa12393d8 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_number_and_limit.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_number_and_limit.xml @@ -1,5 +1,5 @@ - - - + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_top_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_top_and_limit.xml new file mode 100644 index 0000000000000..e5d5155cd7a0c --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_top_and_limit.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_number_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_number_and_limit.xml index 6af522666fa52..3f879dce89fe9 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_number_and_limit.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_number_and_limit.xml @@ -1,5 +1,5 @@ - - + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 01d5c0b2d7579..e67f2f1d34b54 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -433,6 +433,7 @@ + From 99f0e31229d44944e0ac9d3458977378906a9bbf Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 14:21:32 +0800 Subject: [PATCH 271/351] for # 601: add masterslave rule for case select_pagination_with_top_for_greater_than --- .../resources/asserts/cases/select/dql-integrate-test-cases.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index e67f2f1d34b54..10defc6bd7ea0 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -427,6 +427,7 @@ + From 7662e5bc3bf77c795f9c53bccf17f9c4ba887aca Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 14:24:44 +0800 Subject: [PATCH 272/351] for # 601: add masterslave rule for case select_pagination_with_offset_and_limit --- .../masterslave/select_pagination_with_offset_and_limit.xml | 5 +++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 6 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset_and_limit.xml new file mode 100644 index 0000000000000..5f901993ffa1f --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset_and_limit.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 10defc6bd7ea0..98b1949713540 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -421,6 +421,7 @@ + From 6202dba915f477730aaa9eec8f541b648e613684 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 14:28:08 +0800 Subject: [PATCH 273/351] for # 601: add masterslave rule for case select_pagination_with_limit --- .../select_pagination_with_offset_and_row_count.xml | 5 +++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 6 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset_and_row_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset_and_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset_and_row_count.xml new file mode 100644 index 0000000000000..ba1eef8ed0fcf --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset_and_row_count.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 98b1949713540..c3befc90f3b02 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -415,6 +415,7 @@ + From c2f80e8e56685b423c0ac351edcb22a9dd192a7e Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 14:30:45 +0800 Subject: [PATCH 274/351] for # 601: add masterslave rule for case select_pagination_with_row_number --- .../dataset/masterslave/select_pagination_with_row_count.xml | 5 +++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 1 + 2 files changed, 6 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_count.xml new file mode 100644 index 0000000000000..15880ca21588f --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_count.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index c3befc90f3b02..4cfcbbc2dc1cc 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -409,6 +409,7 @@ + From ce578a482a1c89bf6d0b0eeb81fb43f88dec0de9 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 14:48:19 +0800 Subject: [PATCH 275/351] for # 601: add masterslave rule for case select_pagination_with_row_count --- .../dataset/masterslave/select_pagination_with_row_count.xml | 4 ++-- .../asserts/cases/select/dql-integrate-test-cases.xml | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_count.xml index 15880ca21588f..5f901993ffa1f 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_count.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_count.xml @@ -1,5 +1,5 @@ - - + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 4cfcbbc2dc1cc..459185c427fbb 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -397,12 +397,14 @@ + + From dfd11c20f9ad4c790e4823c42264166fde574836 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 14:58:38 +0800 Subject: [PATCH 276/351] for # 601: add masterslave rule for case select_pagination_with_offset --- .../dataset/masterslave/select_pagination_with_offset.xml | 5 +++++ .../asserts/cases/select/dql-integrate-test-cases.xml | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset.xml new file mode 100644 index 0000000000000..5d798a71ae15c --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml index 459185c427fbb..59eb7f9a391ea 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml @@ -388,9 +388,10 @@ - - + + + From 0829e343017c8d19a9edc88a3498e0dff82c8e70 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 15:08:18 +0800 Subject: [PATCH 277/351] fix test case --- .../internal/json/DataSourceJsonConverterTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/jdbc/orchestration/internal/json/DataSourceJsonConverterTest.java b/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/jdbc/orchestration/internal/json/DataSourceJsonConverterTest.java index fe76279e9c481..67691ed11b576 100644 --- a/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/jdbc/orchestration/internal/json/DataSourceJsonConverterTest.java +++ b/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/jdbc/orchestration/internal/json/DataSourceJsonConverterTest.java @@ -37,14 +37,14 @@ public final class DataSourceJsonConverterTest { + "\"maxOpenPreparedStatements\":\"-1\",\"maxTotal\":\"8\",\"maxWaitMillis\":\"-1\",\"minEvictableIdleTimeMillis\":\"1800000\",\"minIdle\":\"0\",\"numTestsPerEvictionRun\":\"3\"," + "\"password\":\"\",\"removeAbandonedOnBorrow\":\"false\",\"removeAbandonedOnMaintenance\":\"false\",\"removeAbandonedTimeout\":\"300\",\"rollbackOnReturn\":\"true\"," + "\"softMinEvictableIdleTimeMillis\":\"-1\",\"testOnBorrow\":\"true\",\"testOnCreate\":\"false\",\"testOnReturn\":\"false\",\"testWhileIdle\":\"false\"," - + "\"timeBetweenEvictionRunsMillis\":\"-1\",\"url\":\"jdbc:mysql://localhost:3306/ds_jdbc_1?serverTimezone=UTC&useSSL=false\",\"username\":\"root\",\"validationQueryTimeout\":\"-1\"}," + + "\"timeBetweenEvictionRunsMillis\":\"-1\",\"url\":\"jdbc:mysql://localhost:3306/ds_jdbc_1?serverTimezone\\u003dUTC\\u0026useSSL\\u003dfalse\",\"username\":\"root\",\"validationQueryTimeout\":\"-1\"}," + "{\"shardingJdbcDataSourceName\":\"ds_jdbc_0\",\"shardingJdbcDataSourceClazz\":\"org.apache.commons.dbcp2.BasicDataSource\",\"abandonedUsageTracking\":\"false\"," + "\"cacheState\":\"true\",\"defaultTransactionIsolation\":\"-1\",\"driverClassName\":\"com.mysql.cj.jdbc.Driver\",\"enableAutoCommitOnReturn\":\"true\"," + "\"evictionPolicyClassName\":\"org.apache.commons.pool2.impl.DefaultEvictionPolicy\",\"fastFailValidation\":\"false\",\"initialSize\":\"0\",\"lifo\":\"true\"," + "\"logAbandoned\":\"false\",\"logExpiredConnections\":\"true\",\"maxConnLifetimeMillis\":\"-1\",\"maxIdle\":\"8\",\"maxOpenPreparedStatements\":\"-1\",\"maxTotal\":\"8\"," + "\"maxWaitMillis\":\"-1\",\"minEvictableIdleTimeMillis\":\"1800000\",\"minIdle\":\"0\",\"numTestsPerEvictionRun\":\"3\",\"password\":\"\",\"removeAbandonedOnBorrow\":\"false\"," + "\"removeAbandonedOnMaintenance\":\"false\",\"removeAbandonedTimeout\":\"300\",\"rollbackOnReturn\":\"true\",\"softMinEvictableIdleTimeMillis\":\"-1\",\"testOnBorrow\":\"true\"," - + "\"testOnCreate\":\"false\",\"testOnReturn\":\"false\",\"testWhileIdle\":\"false\",\"timeBetweenEvictionRunsMillis\":\"-1\",\"url\":\"jdbc:mysql://localhost:3306/ds_jdbc_0?serverTimezone=UTC&useSSL=false\"," + + "\"testOnCreate\":\"false\",\"testOnReturn\":\"false\",\"testWhileIdle\":\"false\",\"timeBetweenEvictionRunsMillis\":\"-1\",\"url\":\"jdbc:mysql://localhost:3306/ds_jdbc_0?serverTimezone\\u003dUTC\\u0026useSSL\\u003dfalse\"," + "\"username\":\"root\",\"validationQueryTimeout\":\"-1\"}]"; @Test From 6fd385191deff5bcbf5ddf400698cd9e296e6d76 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 16:22:58 +0800 Subject: [PATCH 278/351] for #601: add case insert_with_all_placeholders --- .../src/test/resources/parser/insert.xml | 2 +- .../core/merger/JDBCQueryResult.java | 2 +- .../dbtest/engine/BaseIntegrateTest.java | 4 +- .../dataset/db/insert.xml | 48 ++++ .../dataset/dbtbl_with_masterslave/insert.xml | 208 ++++++++++++++++++ .../dataset/masterslave/insert.xml | 128 +++++++++++ .../dataset/tbl/insert.xml | 28 +++ .../dml-integrate-test-cases.xml | 7 +- .../asserts/env/masterslave/data-init.xml | 2 +- .../resources/integrate/assert/insert.xml | 20 +- .../src/main/resources/sql/dml/insert.xml | 12 +- 11 files changed, 426 insertions(+), 35 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/db/insert.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/dbtbl_with_masterslave/insert.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/masterslave/insert.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/tbl/insert.xml diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index 6cedcced20fb1..9eff068d40a51 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -1,6 +1,6 @@ - +
diff --git a/sharding-jdbc/src/main/java/io/shardingsphere/core/merger/JDBCQueryResult.java b/sharding-jdbc/src/main/java/io/shardingsphere/core/merger/JDBCQueryResult.java index c55b83e523299..2685b19079c2e 100644 --- a/sharding-jdbc/src/main/java/io/shardingsphere/core/merger/JDBCQueryResult.java +++ b/sharding-jdbc/src/main/java/io/shardingsphere/core/merger/JDBCQueryResult.java @@ -41,7 +41,7 @@ @RequiredArgsConstructor public final class JDBCQueryResult implements QueryResult { - private final ResultSet resultSet; + private final ResultSet resultSet; @Override public boolean next() throws SQLException { diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/BaseIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/BaseIntegrateTest.java index 142d3203f8b8c..8dce2d177eab6 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/BaseIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/BaseIntegrateTest.java @@ -21,6 +21,7 @@ import io.shardingsphere.core.api.yaml.YamlShardingDataSourceFactory; import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; +import io.shardingsphere.core.parsing.cache.ParsingResultCache; import io.shardingsphere.dbtest.cases.assertion.IntegrateTestCasesLoader; import io.shardingsphere.dbtest.cases.assertion.root.IntegrateTestCase; import io.shardingsphere.dbtest.cases.assertion.root.IntegrateTestCaseAssertion; @@ -148,9 +149,10 @@ public static void dropDatabases() throws JAXBException, IOException { } @After - public void closeShardingDataSource() { + public void tearDown() { if (dataSource instanceof ShardingDataSource) { ((ShardingDataSource) dataSource).close(); } + ParsingResultCache.getInstance().clear(); } } diff --git a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/db/insert.xml b/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/db/insert.xml new file mode 100644 index 0000000000000..663ec6ab706ca --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/db/insert.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/dbtbl_with_masterslave/insert.xml b/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/dbtbl_with_masterslave/insert.xml new file mode 100644 index 0000000000000..7c29223f5cc41 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/dbtbl_with_masterslave/insert.xml @@ -0,0 +1,208 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/masterslave/insert.xml b/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/masterslave/insert.xml new file mode 100644 index 0000000000000..d441336e7de38 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/masterslave/insert.xml @@ -0,0 +1,128 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/tbl/insert.xml b/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/tbl/insert.xml new file mode 100644 index 0000000000000..eb0d1ffbf6b77 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/tbl/insert.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dml-integrate-test-cases.xml index 959abb79cd192..4793a760c99d5 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dml-integrate-test-cases.xml @@ -1,6 +1,9 @@ - - + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml b/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml index f1bd64b42c493..0ff1e36074225 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml +++ b/sharding-jdbc/src/test/resources/asserts/env/masterslave/data-init.xml @@ -299,8 +299,8 @@ - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/insert.xml b/sharding-jdbc/src/test/resources/integrate/assert/insert.xml index 2f8fd6603b5ad..6c547a6fef4b5 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/insert.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/insert.xml @@ -1,31 +1,13 @@ - + - - - - - - - - - - - - - - - - - - diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index 6c6fe09219044..9bf5aaab38815 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -1,28 +1,20 @@ - + + - - - - - - - - - From f37ba6bfc56bffe45e34b0a9b21ba2aca254e675 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 16:49:09 +0800 Subject: [PATCH 279/351] for #601: modify dbtbl_with_masterslave's data source name --- .../dataset/dbtbl_with_masterslave/insert.xml | 404 +++--- .../env/dbtbl_with_masterslave/data-init.xml | 1252 ++++++++--------- .../env/dbtbl_with_masterslave/schema.xml | 40 +- .../dbtbl_with_masterslave/sharding-rule.yaml | 40 +- 4 files changed, 868 insertions(+), 868 deletions(-) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/dbtbl_with_masterslave/insert.xml b/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/dbtbl_with_masterslave/insert.xml index 7c29223f5cc41..ebedaae785917 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/dbtbl_with_masterslave/insert.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/dbtbl_with_masterslave/insert.xml @@ -1,208 +1,208 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/data-init.xml b/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/data-init.xml index 4c7a03e45ce28..f2a204281c702 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/data-init.xml +++ b/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/data-init.xml @@ -1,644 +1,644 @@ - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/schema.xml b/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/schema.xml index a7f549629056d..32ddb3b9506ca 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/schema.xml +++ b/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/schema.xml @@ -1,26 +1,26 @@ - master_0 - master_1 - master_2 - master_3 - master_4 - master_5 - master_6 - master_7 - master_8 - master_9 - slave_0 - slave_1 - slave_2 - slave_3 - slave_4 - slave_5 - slave_6 - slave_7 - slave_8 - slave_9 + master_db_0 + master_db_1 + master_db_2 + master_db_3 + master_db_4 + master_db_5 + master_db_6 + master_db_7 + master_db_8 + master_db_9 + slave_db_0 + slave_db_1 + slave_db_2 + slave_db_3 + slave_db_4 + slave_db_5 + slave_db_6 + slave_db_7 + slave_db_8 + slave_db_9 CREATE TABLE t_order_0 (order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id)) diff --git a/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/sharding-rule.yaml b/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/sharding-rule.yaml index 84e1e88259c35..001bdd607993d 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/sharding-rule.yaml +++ b/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/sharding-rule.yaml @@ -36,54 +36,54 @@ shardingRule: masterSlaveRules: db_ms_0: - masterDataSourceName: master_0 + masterDataSourceName: master_db_0 slaveDataSourceNames: - - slave_0 + - slave_db_0 loadBalanceAlgorithmType: ROUND_ROBIN db_ms_1: - masterDataSourceName: master_1 + masterDataSourceName: master_db_1 slaveDataSourceNames: - - slave_1 + - slave_db_1 loadBalanceAlgorithmType: ROUND_ROBIN db_ms_2: - masterDataSourceName: master_2 + masterDataSourceName: master_db_2 slaveDataSourceNames: - - slave_2 + - slave_db_2 loadBalanceAlgorithmType: ROUND_ROBIN db_ms_3: - masterDataSourceName: master_3 + masterDataSourceName: master_db_3 slaveDataSourceNames: - - slave_3 + - slave_db_3 loadBalanceAlgorithmType: ROUND_ROBIN db_ms_4: - masterDataSourceName: master_4 + masterDataSourceName: master_db_4 slaveDataSourceNames: - - slave_4 + - slave_db_4 loadBalanceAlgorithmType: ROUND_ROBIN db_ms_5: - masterDataSourceName: master_5 + masterDataSourceName: master_db_5 slaveDataSourceNames: - - slave_5 + - slave_db_5 loadBalanceAlgorithmType: ROUND_ROBIN db_ms_6: - masterDataSourceName: master_6 + masterDataSourceName: master_db_6 slaveDataSourceNames: - - slave_6 + - slave_db_6 loadBalanceAlgorithmType: ROUND_ROBIN db_ms_7: - masterDataSourceName: master_7 + masterDataSourceName: master_db_7 slaveDataSourceNames: - - slave_7 + - slave_db_7 loadBalanceAlgorithmType: ROUND_ROBIN db_ms_8: - masterDataSourceName: master_8 + masterDataSourceName: master_db_8 slaveDataSourceNames: - - slave_8 + - slave_db_8 loadBalanceAlgorithmType: ROUND_ROBIN db_ms_9: - masterDataSourceName: master_9 + masterDataSourceName: master_db_9 slaveDataSourceNames: - - slave_9 + - slave_db_9 loadBalanceAlgorithmType: ROUND_ROBIN configMap: key1: value1 From 1cacbc5f54e17521a5e08e0dfb939a15c1f0c5cb Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 16:52:56 +0800 Subject: [PATCH 280/351] for #601: refactor folder for dml --- .../dataset/db/insert.xml | 0 .../dataset/dbtbl_with_masterslave/insert.xml | 0 .../dataset/delete.xml | 0 .../dataset/insert.xml | 0 .../dataset/masterslave/insert.xml | 0 .../dataset/tbl/insert.xml | 0 .../dataset/update.xml | 0 .../dml-integrate-test-cases.xml | 0 8 files changed, 0 insertions(+), 0 deletions(-) rename sharding-jdbc/src/test/resources/asserts/cases/{assert-sharding-database-only-with-hint-for-dml-test => dml}/dataset/db/insert.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{assert-sharding-database-only-with-hint-for-dml-test => dml}/dataset/dbtbl_with_masterslave/insert.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{assert-sharding-database-only-with-hint-for-dml-test => dml}/dataset/delete.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{assert-sharding-database-only-with-hint-for-dml-test => dml}/dataset/insert.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{assert-sharding-database-only-with-hint-for-dml-test => dml}/dataset/masterslave/insert.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{assert-sharding-database-only-with-hint-for-dml-test => dml}/dataset/tbl/insert.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{assert-sharding-database-only-with-hint-for-dml-test => dml}/dataset/update.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{assert-sharding-database-only-with-hint-for-dml-test => dml}/dml-integrate-test-cases.xml (100%) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/db/insert.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/db/insert.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/dbtbl_with_masterslave/insert.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/dbtbl_with_masterslave/insert.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/delete.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/delete.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/delete.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/delete.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/insert.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/insert.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/insert.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/insert.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/masterslave/insert.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/masterslave/insert.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/tbl/insert.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/tbl/insert.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/update.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/update.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dataset/update.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/update.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/assert-sharding-database-only-with-hint-for-dml-test/dml-integrate-test-cases.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml From 30f51d3e7dfdaba7b1711266e6aa0597771b3b72 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 17:00:15 +0800 Subject: [PATCH 281/351] for #601: refactor folder select for dql --- .../{select => dql}/dataset/db/select_alias_as_keyword.xml | 0 .../asserts/cases/{select => dql}/dataset/db/select_avg.xml | 0 .../dataset/db/select_between_with_single_table.xml | 0 .../asserts/cases/{select => dql}/dataset/db/select_count.xml | 0 .../{select => dql}/dataset/db/select_count_like_concat.xml | 0 .../dataset/db/select_count_with_binding_tables.xml | 0 .../dataset/db/select_equal_with_single_table.xml | 0 .../dataset/db/select_full_route_with_binding_tables.xml | 0 .../{select => dql}/dataset/db/select_group_by_with_avg.xml | 0 .../{select => dql}/dataset/db/select_group_by_with_count.xml | 0 .../dataset/db/select_group_by_with_date_function.xml | 0 .../dataset/db/select_group_by_with_key_word_alias.xml | 0 .../{select => dql}/dataset/db/select_group_by_with_limit.xml | 0 .../{select => dql}/dataset/db/select_group_by_with_max.xml | 0 .../{select => dql}/dataset/db/select_group_by_with_min.xml | 0 .../dataset/db/select_group_by_with_order_by_and_limit.xml | 0 .../dataset/db/select_group_by_with_order_by_desc.xml | 0 .../{select => dql}/dataset/db/select_group_by_with_sum.xml | 0 .../dataset/db/select_group_by_without_grouped_column.xml | 0 .../dataset/db/select_in_with_same_sharding_column.xml | 0 .../{select => dql}/dataset/db/select_in_with_single_table.xml | 0 .../cases/{select => dql}/dataset/db/select_inner_join.xml | 0 .../asserts/cases/{select => dql}/dataset/db/select_max.xml | 0 .../asserts/cases/{select => dql}/dataset/db/select_min.xml | 0 .../dataset/db/select_not_equal_with_single_table.xml | 0 .../dataset/db/select_not_in_with_single_table.xml | 0 .../dataset/db/select_or_mix_and_for_complex_pattern.xml | 0 .../dataset/db/select_or_mix_and_for_simple_pattern.xml | 0 .../db/select_or_mix_and_with_binding_and_broadcast_tables.xml | 0 .../dataset/db/select_or_mix_and_with_binding_tables.xml | 0 .../dataset/db/select_or_with_different_sharding_columns.xml | 0 .../dataset/db/select_or_with_none_sharding_columns.xml | 0 .../dataset/db/select_or_with_same_sharding_columns.xml | 0 .../dataset/db/select_order_by_asc_and_index_desc.xml | 0 .../dataset/db/select_order_by_desc_and_index_asc.xml | 0 .../{select => dql}/dataset/db/select_order_by_with_date.xml | 0 .../dataset/db/select_order_by_with_ordered_column.xml | 0 .../dataset/db/select_pagination_with_offset.xml | 0 .../dataset/db/select_pagination_with_offset_and_limit.xml | 0 .../dataset/db/select_pagination_with_offset_and_row_count.xml | 0 .../dataset/db/select_pagination_with_row_count.xml | 0 .../dataset/db/select_pagination_with_row_number_and_limit.xml | 0 .../dataset/db/select_pagination_with_top_and_limit.xml | 0 .../dataset/db/select_sharding_route_with_binding_tables.xml | 0 .../dataset/db/select_sharding_route_with_broadcast_table.xml | 0 .../dataset/db/select_sub_query_with_group_by.xml | 0 .../dataset/db/select_sub_query_with_multiple_tables.xml | 0 .../dataset/db/select_sub_query_with_order_by.xml | 0 .../dataset/db/select_sub_query_with_single_table.xml | 0 .../asserts/cases/{select => dql}/dataset/db/select_sum.xml | 0 .../{select => dql}/dataset/db/select_with_date_function.xml | 0 .../cases/{select => dql}/dataset/db/select_with_expression.xml | 2 +- .../{select => dql}/dataset/db/select_with_force_index_join.xml | 0 ...select_with_item_alias_match_order_by_and_group_by_items.xml | 0 .../cases/{select => dql}/dataset/db/select_with_regexp.xml | 0 .../dataset/dbtbl_with_masterslave/select_alias_as_keyword.xml | 0 .../dataset/dbtbl_with_masterslave/select_avg.xml | 0 .../dbtbl_with_masterslave/select_between_with_single_table.xml | 0 .../dataset/dbtbl_with_masterslave/select_count.xml | 0 .../dataset/dbtbl_with_masterslave/select_count_like_concat.xml | 0 .../dbtbl_with_masterslave/select_count_with_binding_tables.xml | 0 .../dbtbl_with_masterslave/select_equal_with_single_table.xml | 0 .../select_full_route_with_binding_tables.xml | 0 .../dataset/dbtbl_with_masterslave/select_group_by_with_avg.xml | 0 .../dbtbl_with_masterslave/select_group_by_with_count.xml | 0 .../select_group_by_with_date_function.xml | 0 .../select_group_by_with_key_word_alias.xml | 0 .../dbtbl_with_masterslave/select_group_by_with_limit.xml | 0 .../dataset/dbtbl_with_masterslave/select_group_by_with_max.xml | 0 .../dataset/dbtbl_with_masterslave/select_group_by_with_min.xml | 0 .../select_group_by_with_order_by_and_limit.xml | 0 .../select_group_by_with_order_by_desc.xml | 0 .../dataset/dbtbl_with_masterslave/select_group_by_with_sum.xml | 0 .../select_group_by_without_grouped_column.xml | 0 .../select_in_with_same_sharding_column.xml | 0 .../dbtbl_with_masterslave/select_in_with_single_table.xml | 0 .../dataset/dbtbl_with_masterslave/select_inner_join.xml | 0 .../dataset/dbtbl_with_masterslave/select_max.xml | 0 .../dataset/dbtbl_with_masterslave/select_min.xml | 0 .../select_not_equal_with_single_table.xml | 0 .../dbtbl_with_masterslave/select_not_in_with_single_table.xml | 0 .../select_or_mix_and_for_complex_pattern.xml | 0 .../select_or_mix_and_for_simple_pattern.xml | 0 .../select_or_mix_and_with_binding_and_broadcast_tables.xml | 0 .../select_or_mix_and_with_binding_tables.xml | 0 .../select_or_with_different_sharding_columns.xml | 0 .../select_or_with_none_sharding_columns.xml | 0 .../select_or_with_same_sharding_columns.xml | 0 .../select_order_by_asc_and_index_desc.xml | 0 .../select_order_by_desc_and_index_asc.xml | 0 .../dbtbl_with_masterslave/select_order_by_with_date.xml | 0 .../select_order_by_with_ordered_column.xml | 0 .../dbtbl_with_masterslave/select_pagination_with_offset.xml | 0 .../select_pagination_with_offset_and_limit.xml | 0 .../select_pagination_with_offset_and_row_count.xml | 0 .../dbtbl_with_masterslave/select_pagination_with_row_count.xml | 0 .../select_pagination_with_row_number_and_limit.xml | 0 .../select_pagination_with_top_and_limit.xml | 0 .../select_sharding_route_with_binding_tables.xml | 0 .../select_sharding_route_with_broadcast_table.xml | 0 .../dbtbl_with_masterslave/select_sub_query_with_group_by.xml | 0 .../select_sub_query_with_multiple_tables.xml | 0 .../dbtbl_with_masterslave/select_sub_query_with_order_by.xml | 0 .../select_sub_query_with_single_table.xml | 0 .../dataset/dbtbl_with_masterslave/select_sum.xml | 0 .../dbtbl_with_masterslave/select_with_date_function.xml | 0 .../dataset/dbtbl_with_masterslave/select_with_expression.xml | 2 +- .../dbtbl_with_masterslave/select_with_force_index_join.xml | 0 ...select_with_item_alias_match_order_by_and_group_by_items.xml | 0 .../dataset/dbtbl_with_masterslave/select_with_regexp.xml | 0 .../asserts/cases/{select => dql}/dataset/empty_order.xml | 0 .../asserts/cases/{select => dql}/dataset/empty_order_item.xml | 0 .../dataset/masterslave/select_alias_as_keyword.xml | 0 .../cases/{select => dql}/dataset/masterslave/select_avg.xml | 0 .../dataset/masterslave/select_between_with_single_table.xml | 0 .../cases/{select => dql}/dataset/masterslave/select_count.xml | 0 .../dataset/masterslave/select_count_like_concat.xml | 0 .../dataset/masterslave/select_count_with_binding_tables.xml | 0 .../dataset/masterslave/select_equal_with_single_table.xml | 0 .../masterslave/select_full_route_with_binding_tables.xml | 0 .../dataset/masterslave/select_group_by_with_avg.xml | 0 .../dataset/masterslave/select_group_by_with_count.xml | 0 .../dataset/masterslave/select_group_by_with_key_word_alias.xml | 0 .../dataset/masterslave/select_group_by_with_limit.xml | 0 .../dataset/masterslave/select_group_by_with_max.xml | 0 .../dataset/masterslave/select_group_by_with_min.xml | 0 .../masterslave/select_group_by_with_order_by_and_limit.xml | 0 .../dataset/masterslave/select_group_by_with_order_by_desc.xml | 0 .../dataset/masterslave/select_group_by_with_sum.xml | 0 .../masterslave/select_group_by_without_grouped_column.xml | 0 .../dataset/masterslave/select_in_with_same_sharding_column.xml | 0 .../dataset/masterslave/select_in_with_single_table.xml | 0 .../{select => dql}/dataset/masterslave/select_inner_join.xml | 0 .../cases/{select => dql}/dataset/masterslave/select_max.xml | 0 .../cases/{select => dql}/dataset/masterslave/select_min.xml | 0 .../dataset/masterslave/select_not_equal_with_single_table.xml | 0 .../dataset/masterslave/select_not_in_with_single_table.xml | 0 .../masterslave/select_or_mix_and_for_complex_pattern.xml | 0 .../masterslave/select_or_mix_and_for_simple_pattern.xml | 0 .../select_or_mix_and_with_binding_and_broadcast_tables.xml | 0 .../masterslave/select_or_mix_and_with_binding_tables.xml | 0 .../masterslave/select_or_with_different_sharding_columns.xml | 0 .../masterslave/select_or_with_none_sharding_columns.xml | 0 .../masterslave/select_or_with_same_sharding_columns.xml | 0 .../dataset/masterslave/select_order_by_asc_and_index_desc.xml | 0 .../dataset/masterslave/select_order_by_desc_and_index_asc.xml | 0 .../dataset/masterslave/select_order_by_with_date.xml | 0 .../dataset/masterslave/select_order_by_with_ordered_column.xml | 0 .../dataset/masterslave/select_pagination_with_offset.xml | 0 .../masterslave/select_pagination_with_offset_and_limit.xml | 0 .../masterslave/select_pagination_with_offset_and_row_count.xml | 0 .../dataset/masterslave/select_pagination_with_row_count.xml | 0 .../masterslave/select_pagination_with_row_number_and_limit.xml | 0 .../masterslave/select_pagination_with_top_and_limit.xml | 0 .../masterslave/select_sharding_route_with_binding_tables.xml | 0 .../masterslave/select_sharding_route_with_broadcast_table.xml | 0 .../dataset/masterslave/select_sub_query_with_group_by.xml | 0 .../masterslave/select_sub_query_with_multiple_tables.xml | 0 .../dataset/masterslave/select_sub_query_with_order_by.xml | 0 .../dataset/masterslave/select_sub_query_with_single_table.xml | 0 .../cases/{select => dql}/dataset/masterslave/select_sum.xml | 0 .../dataset/masterslave/select_with_date_function.xml | 0 .../dataset/masterslave/select_with_expression.xml | 2 +- .../dataset/masterslave/select_with_force_index_join.xml | 0 ...select_with_item_alias_match_order_by_and_group_by_items.xml | 0 .../{select => dql}/dataset/masterslave/select_with_regexp.xml | 0 .../{select => dql}/dataset/select_constant_without_table.xml | 0 .../{select => dql}/dataset/tbl/select_alias_as_keyword.xml | 0 .../asserts/cases/{select => dql}/dataset/tbl/select_avg.xml | 0 .../dataset/tbl/select_between_with_single_table.xml | 0 .../asserts/cases/{select => dql}/dataset/tbl/select_count.xml | 0 .../{select => dql}/dataset/tbl/select_count_like_concat.xml | 0 .../dataset/tbl/select_count_with_binding_tables.xml | 0 .../dataset/tbl/select_equal_with_single_table.xml | 0 .../dataset/tbl/select_full_route_with_binding_tables.xml | 0 .../{select => dql}/dataset/tbl/select_group_by_with_avg.xml | 0 .../{select => dql}/dataset/tbl/select_group_by_with_count.xml | 0 .../dataset/tbl/select_group_by_with_date_function.xml | 0 .../dataset/tbl/select_group_by_with_key_word_alias.xml | 0 .../{select => dql}/dataset/tbl/select_group_by_with_limit.xml | 0 .../{select => dql}/dataset/tbl/select_group_by_with_max.xml | 0 .../{select => dql}/dataset/tbl/select_group_by_with_min.xml | 0 .../dataset/tbl/select_group_by_with_order_by_and_limit.xml | 0 .../dataset/tbl/select_group_by_with_order_by_desc.xml | 0 .../{select => dql}/dataset/tbl/select_group_by_with_sum.xml | 0 .../dataset/tbl/select_group_by_without_grouped_column.xml | 0 .../dataset/tbl/select_in_with_same_sharding_column.xml | 0 .../{select => dql}/dataset/tbl/select_in_with_single_table.xml | 0 .../cases/{select => dql}/dataset/tbl/select_inner_join.xml | 0 .../asserts/cases/{select => dql}/dataset/tbl/select_max.xml | 0 .../asserts/cases/{select => dql}/dataset/tbl/select_min.xml | 0 .../dataset/tbl/select_not_equal_with_single_table.xml | 0 .../dataset/tbl/select_not_in_with_single_table.xml | 0 .../dataset/tbl/select_or_mix_and_for_complex_pattern.xml | 0 .../dataset/tbl/select_or_mix_and_for_simple_pattern.xml | 0 .../tbl/select_or_mix_and_with_binding_and_broadcast_tables.xml | 0 .../dataset/tbl/select_or_mix_and_with_binding_tables.xml | 0 .../dataset/tbl/select_or_with_different_sharding_columns.xml | 0 .../dataset/tbl/select_or_with_none_sharding_columns.xml | 0 .../dataset/tbl/select_or_with_same_sharding_columns.xml | 0 .../dataset/tbl/select_order_by_asc_and_index_desc.xml | 0 .../dataset/tbl/select_order_by_desc_and_index_asc.xml | 0 .../{select => dql}/dataset/tbl/select_order_by_with_date.xml | 0 .../dataset/tbl/select_order_by_with_ordered_column.xml | 0 .../dataset/tbl/select_pagination_with_offset.xml | 0 .../dataset/tbl/select_pagination_with_offset_and_limit.xml | 0 .../dataset/tbl/select_pagination_with_offset_and_row_count.xml | 0 .../dataset/tbl/select_pagination_with_row_count.xml | 0 .../dataset/tbl/select_pagination_with_row_number_and_limit.xml | 0 .../dataset/tbl/select_pagination_with_top_and_limit.xml | 0 .../dataset/tbl/select_sharding_route_with_binding_tables.xml | 0 .../dataset/tbl/select_sharding_route_with_broadcast_table.xml | 0 .../dataset/tbl/select_sub_query_with_group_by.xml | 0 .../dataset/tbl/select_sub_query_with_multiple_tables.xml | 0 .../dataset/tbl/select_sub_query_with_order_by.xml | 0 .../dataset/tbl/select_sub_query_with_single_table.xml | 0 .../asserts/cases/{select => dql}/dataset/tbl/select_sum.xml | 0 .../{select => dql}/dataset/tbl/select_with_date_function.xml | 0 .../{select => dql}/dataset/tbl/select_with_expression.xml | 2 +- .../dataset/tbl/select_with_force_index_join.xml | 0 ...select_with_item_alias_match_order_by_and_group_by_items.xml | 0 .../cases/{select => dql}/dataset/tbl/select_with_regexp.xml | 0 .../asserts/cases/{select => dql}/dql-integrate-test-cases.xml | 0 223 files changed, 4 insertions(+), 4 deletions(-) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_alias_as_keyword.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_avg.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_between_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_count_like_concat.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_count_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_equal_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_full_route_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_group_by_with_avg.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_group_by_with_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_group_by_with_date_function.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_group_by_with_key_word_alias.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_group_by_with_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_group_by_with_max.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_group_by_with_min.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_group_by_with_order_by_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_group_by_with_order_by_desc.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_group_by_with_sum.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_group_by_without_grouped_column.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_in_with_same_sharding_column.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_in_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_inner_join.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_max.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_min.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_not_equal_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_not_in_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_or_mix_and_for_complex_pattern.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_or_mix_and_for_simple_pattern.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_or_mix_and_with_binding_and_broadcast_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_or_mix_and_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_or_with_different_sharding_columns.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_or_with_none_sharding_columns.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_or_with_same_sharding_columns.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_order_by_asc_and_index_desc.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_order_by_desc_and_index_asc.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_order_by_with_date.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_order_by_with_ordered_column.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_pagination_with_offset.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_pagination_with_offset_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_pagination_with_offset_and_row_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_pagination_with_row_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_pagination_with_row_number_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_pagination_with_top_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_sharding_route_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_sharding_route_with_broadcast_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_sub_query_with_group_by.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_sub_query_with_multiple_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_sub_query_with_order_by.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_sub_query_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_sum.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_with_date_function.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_with_expression.xml (99%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_with_force_index_join.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_with_item_alias_match_order_by_and_group_by_items.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/db/select_with_regexp.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_alias_as_keyword.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_avg.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_between_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_count_like_concat.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_count_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_equal_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_full_route_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_group_by_with_avg.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_group_by_with_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_group_by_with_date_function.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_group_by_with_key_word_alias.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_group_by_with_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_group_by_with_max.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_group_by_with_min.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_group_by_with_order_by_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_group_by_with_order_by_desc.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_group_by_with_sum.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_group_by_without_grouped_column.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_in_with_same_sharding_column.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_in_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_inner_join.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_max.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_min.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_not_equal_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_not_in_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_or_mix_and_for_complex_pattern.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_or_mix_and_for_simple_pattern.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_or_with_different_sharding_columns.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_or_with_none_sharding_columns.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_or_with_same_sharding_columns.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_order_by_asc_and_index_desc.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_order_by_desc_and_index_asc.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_order_by_with_date.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_order_by_with_ordered_column.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_pagination_with_offset.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_row_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_pagination_with_row_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_pagination_with_row_number_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_pagination_with_top_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_sharding_route_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_sharding_route_with_broadcast_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_sub_query_with_group_by.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_sub_query_with_multiple_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_sub_query_with_order_by.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_sub_query_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_sum.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_with_date_function.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_with_expression.xml (99%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_with_force_index_join.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_with_item_alias_match_order_by_and_group_by_items.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/dbtbl_with_masterslave/select_with_regexp.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/empty_order.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/empty_order_item.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_alias_as_keyword.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_avg.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_between_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_count_like_concat.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_count_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_equal_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_full_route_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_group_by_with_avg.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_group_by_with_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_group_by_with_key_word_alias.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_group_by_with_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_group_by_with_max.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_group_by_with_min.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_group_by_with_order_by_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_group_by_with_order_by_desc.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_group_by_with_sum.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_group_by_without_grouped_column.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_in_with_same_sharding_column.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_in_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_inner_join.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_max.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_min.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_not_equal_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_not_in_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_or_mix_and_for_complex_pattern.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_or_mix_and_for_simple_pattern.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_or_mix_and_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_or_with_different_sharding_columns.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_or_with_none_sharding_columns.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_or_with_same_sharding_columns.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_order_by_asc_and_index_desc.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_order_by_desc_and_index_asc.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_order_by_with_date.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_order_by_with_ordered_column.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_pagination_with_offset.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_pagination_with_offset_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_pagination_with_offset_and_row_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_pagination_with_row_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_pagination_with_row_number_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_pagination_with_top_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_sharding_route_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_sharding_route_with_broadcast_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_sub_query_with_group_by.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_sub_query_with_multiple_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_sub_query_with_order_by.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_sub_query_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_sum.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_with_date_function.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_with_expression.xml (99%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_with_force_index_join.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_with_item_alias_match_order_by_and_group_by_items.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/masterslave/select_with_regexp.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/select_constant_without_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_alias_as_keyword.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_avg.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_between_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_count_like_concat.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_count_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_equal_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_full_route_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_group_by_with_avg.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_group_by_with_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_group_by_with_date_function.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_group_by_with_key_word_alias.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_group_by_with_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_group_by_with_max.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_group_by_with_min.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_group_by_with_order_by_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_group_by_with_order_by_desc.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_group_by_with_sum.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_group_by_without_grouped_column.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_in_with_same_sharding_column.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_in_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_inner_join.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_max.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_min.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_not_equal_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_not_in_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_or_mix_and_for_complex_pattern.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_or_mix_and_for_simple_pattern.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_or_mix_and_with_binding_and_broadcast_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_or_mix_and_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_or_with_different_sharding_columns.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_or_with_none_sharding_columns.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_or_with_same_sharding_columns.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_order_by_asc_and_index_desc.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_order_by_desc_and_index_asc.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_order_by_with_date.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_order_by_with_ordered_column.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_pagination_with_offset.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_pagination_with_offset_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_pagination_with_offset_and_row_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_pagination_with_row_count.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_pagination_with_row_number_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_pagination_with_top_and_limit.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_sharding_route_with_binding_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_sharding_route_with_broadcast_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_sub_query_with_group_by.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_sub_query_with_multiple_tables.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_sub_query_with_order_by.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_sub_query_with_single_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_sum.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_with_date_function.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_with_expression.xml (98%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_with_force_index_join.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_with_item_alias_match_order_by_and_group_by_items.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dataset/tbl/select_with_regexp.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{select => dql}/dql-integrate-test-cases.xml (100%) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_alias_as_keyword.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_alias_as_keyword.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_alias_as_keyword.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_alias_as_keyword.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_avg.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_avg.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_avg.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_avg.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_between_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_between_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_between_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_between_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_count_like_concat.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_count_like_concat.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_count_like_concat.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_count_like_concat.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_count_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_count_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_count_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_count_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_equal_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_equal_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_equal_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_equal_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_full_route_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_full_route_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_full_route_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_full_route_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_avg.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_avg.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_avg.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_avg.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_date_function.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_date_function.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_date_function.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_date_function.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_key_word_alias.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_key_word_alias.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_key_word_alias.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_key_word_alias.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_max.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_max.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_max.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_max.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_min.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_min.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_min.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_min.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_order_by_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_order_by_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_order_by_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_order_by_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_order_by_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_order_by_desc.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_order_by_desc.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_order_by_desc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_sum.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_sum.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_with_sum.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_with_sum.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_without_grouped_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_without_grouped_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_group_by_without_grouped_column.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_group_by_without_grouped_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_in_with_same_sharding_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_in_with_same_sharding_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_in_with_same_sharding_column.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_in_with_same_sharding_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_in_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_in_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_in_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_in_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_inner_join.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_inner_join.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_inner_join.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_inner_join.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_max.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_max.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_max.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_max.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_min.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_min.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_min.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_min.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_not_equal_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_not_equal_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_not_equal_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_not_equal_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_not_in_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_not_in_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_not_in_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_not_in_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_complex_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_or_mix_and_for_complex_pattern.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_complex_pattern.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_or_mix_and_for_complex_pattern.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_simple_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_or_mix_and_for_simple_pattern.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_for_simple_pattern.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_or_mix_and_for_simple_pattern.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_with_binding_and_broadcast_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_or_mix_and_with_binding_and_broadcast_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_with_binding_and_broadcast_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_or_mix_and_with_binding_and_broadcast_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_or_mix_and_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_mix_and_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_or_mix_and_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_different_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_or_with_different_sharding_columns.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_different_sharding_columns.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_or_with_different_sharding_columns.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_none_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_or_with_none_sharding_columns.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_none_sharding_columns.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_or_with_none_sharding_columns.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_same_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_or_with_same_sharding_columns.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_or_with_same_sharding_columns.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_or_with_same_sharding_columns.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_asc_and_index_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_order_by_asc_and_index_desc.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_asc_and_index_desc.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_order_by_asc_and_index_desc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_desc_and_index_asc.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_order_by_desc_and_index_asc.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_desc_and_index_asc.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_order_by_desc_and_index_asc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_with_date.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_order_by_with_date.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_with_date.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_order_by_with_date.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_with_ordered_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_order_by_with_ordered_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_order_by_with_ordered_column.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_order_by_with_ordered_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_pagination_with_offset.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_pagination_with_offset.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_pagination_with_offset_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_pagination_with_offset_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset_and_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_pagination_with_offset_and_row_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_offset_and_row_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_pagination_with_offset_and_row_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_pagination_with_row_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_pagination_with_row_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_number_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_pagination_with_row_number_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_row_number_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_pagination_with_row_number_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_top_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_pagination_with_top_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_pagination_with_top_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_pagination_with_top_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sharding_route_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_sharding_route_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sharding_route_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_sharding_route_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sharding_route_with_broadcast_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_sharding_route_with_broadcast_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sharding_route_with_broadcast_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_sharding_route_with_broadcast_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_group_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_sub_query_with_group_by.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_group_by.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_sub_query_with_group_by.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_multiple_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_sub_query_with_multiple_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_multiple_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_sub_query_with_multiple_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_order_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_sub_query_with_order_by.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_order_by.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_sub_query_with_order_by.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_sub_query_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sub_query_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_sub_query_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sum.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_sum.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_sum.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_sum.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_with_date_function.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_with_date_function.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_with_date_function.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_with_date_function.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_with_expression.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_with_expression.xml similarity index 99% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_with_expression.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_with_expression.xml index 291ed4f8631f1..ac15cd910b9e5 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_with_expression.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_with_expression.xml @@ -40,4 +40,4 @@ - \ No newline at end of file + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_with_force_index_join.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_with_force_index_join.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_with_force_index_join.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_with_force_index_join.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_with_item_alias_match_order_by_and_group_by_items.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_with_item_alias_match_order_by_and_group_by_items.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_with_item_alias_match_order_by_and_group_by_items.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_with_item_alias_match_order_by_and_group_by_items.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_with_regexp.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_with_regexp.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/db/select_with_regexp.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/db/select_with_regexp.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_alias_as_keyword.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_alias_as_keyword.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_alias_as_keyword.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_alias_as_keyword.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_avg.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_avg.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_avg.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_avg.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_between_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_between_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_between_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_between_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_count_like_concat.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_count_like_concat.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_count_like_concat.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_count_like_concat.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_count_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_count_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_count_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_count_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_equal_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_equal_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_equal_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_equal_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_full_route_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_full_route_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_full_route_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_full_route_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_avg.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_avg.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_avg.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_avg.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_date_function.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_date_function.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_date_function.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_date_function.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_key_word_alias.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_key_word_alias.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_key_word_alias.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_key_word_alias.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_max.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_max.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_max.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_max.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_min.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_min.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_min.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_min.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_order_by_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_order_by_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_order_by_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_order_by_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_order_by_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_order_by_desc.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_order_by_desc.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_order_by_desc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_sum.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_sum.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_with_sum.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_with_sum.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_without_grouped_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_without_grouped_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_group_by_without_grouped_column.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_group_by_without_grouped_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_in_with_same_sharding_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_in_with_same_sharding_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_in_with_same_sharding_column.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_in_with_same_sharding_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_in_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_in_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_in_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_in_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_inner_join.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_inner_join.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_inner_join.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_inner_join.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_max.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_max.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_max.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_max.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_min.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_min.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_min.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_min.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_not_equal_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_not_equal_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_not_equal_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_not_equal_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_not_in_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_not_in_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_not_in_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_not_in_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_complex_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_or_mix_and_for_complex_pattern.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_complex_pattern.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_or_mix_and_for_complex_pattern.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_simple_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_or_mix_and_for_simple_pattern.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_for_simple_pattern.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_or_mix_and_for_simple_pattern.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_or_mix_and_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_different_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_or_with_different_sharding_columns.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_different_sharding_columns.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_or_with_different_sharding_columns.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_none_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_or_with_none_sharding_columns.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_none_sharding_columns.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_or_with_none_sharding_columns.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_same_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_or_with_same_sharding_columns.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_or_with_same_sharding_columns.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_or_with_same_sharding_columns.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_asc_and_index_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_order_by_asc_and_index_desc.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_asc_and_index_desc.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_order_by_asc_and_index_desc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_desc_and_index_asc.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_order_by_desc_and_index_asc.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_desc_and_index_asc.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_order_by_desc_and_index_asc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_with_date.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_order_by_with_date.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_with_date.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_order_by_with_date.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_with_ordered_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_order_by_with_ordered_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_order_by_with_ordered_column.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_order_by_with_ordered_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_pagination_with_offset.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_pagination_with_offset.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_row_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_row_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_pagination_with_offset_and_row_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_pagination_with_row_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_pagination_with_row_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_number_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_pagination_with_row_number_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_row_number_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_pagination_with_row_number_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_top_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_pagination_with_top_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_pagination_with_top_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_pagination_with_top_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sharding_route_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_sharding_route_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sharding_route_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_sharding_route_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sharding_route_with_broadcast_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_sharding_route_with_broadcast_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sharding_route_with_broadcast_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_sharding_route_with_broadcast_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_group_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_sub_query_with_group_by.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_group_by.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_sub_query_with_group_by.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_multiple_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_sub_query_with_multiple_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_multiple_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_sub_query_with_multiple_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_order_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_sub_query_with_order_by.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_order_by.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_sub_query_with_order_by.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_sub_query_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sub_query_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_sub_query_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sum.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_sum.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_sum.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_sum.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_with_date_function.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_with_date_function.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_with_date_function.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_with_date_function.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_with_expression.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_with_expression.xml similarity index 99% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_with_expression.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_with_expression.xml index 8247e02c94aea..5130902c60999 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_with_expression.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_with_expression.xml @@ -100,4 +100,4 @@ - \ No newline at end of file + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_with_force_index_join.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_with_force_index_join.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_with_force_index_join.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_with_force_index_join.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_with_item_alias_match_order_by_and_group_by_items.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_with_item_alias_match_order_by_and_group_by_items.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_with_item_alias_match_order_by_and_group_by_items.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_with_item_alias_match_order_by_and_group_by_items.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_with_regexp.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_with_regexp.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/dbtbl_with_masterslave/select_with_regexp.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/dbtbl_with_masterslave/select_with_regexp.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/empty_order.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/empty_order.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/empty_order.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/empty_order.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/empty_order_item.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/empty_order_item.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/empty_order_item.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/empty_order_item.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_alias_as_keyword.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_alias_as_keyword.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_alias_as_keyword.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_alias_as_keyword.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_avg.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_avg.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_avg.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_avg.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_between_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_between_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_between_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_between_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count_like_concat.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_count_like_concat.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count_like_concat.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_count_like_concat.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_count_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_count_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_count_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_equal_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_equal_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_equal_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_equal_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_full_route_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_full_route_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_full_route_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_full_route_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_avg.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_avg.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_avg.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_avg.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_key_word_alias.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_key_word_alias.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_key_word_alias.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_key_word_alias.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_max.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_max.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_max.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_max.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_min.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_min.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_min.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_min.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_order_by_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_order_by_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_order_by_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_order_by_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_order_by_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_order_by_desc.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_order_by_desc.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_order_by_desc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_sum.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_sum.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_with_sum.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_with_sum.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_without_grouped_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_without_grouped_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_group_by_without_grouped_column.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_group_by_without_grouped_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_in_with_same_sharding_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_in_with_same_sharding_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_in_with_same_sharding_column.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_in_with_same_sharding_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_in_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_in_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_in_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_in_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_inner_join.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_inner_join.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_inner_join.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_inner_join.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_max.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_max.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_max.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_max.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_min.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_min.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_min.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_min.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_not_equal_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_not_equal_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_not_equal_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_not_equal_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_not_in_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_not_in_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_not_in_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_not_in_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_for_complex_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_or_mix_and_for_complex_pattern.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_for_complex_pattern.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_or_mix_and_for_complex_pattern.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_for_simple_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_or_mix_and_for_simple_pattern.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_for_simple_pattern.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_or_mix_and_for_simple_pattern.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_or_mix_and_with_binding_and_broadcast_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_or_mix_and_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_mix_and_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_or_mix_and_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_different_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_or_with_different_sharding_columns.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_different_sharding_columns.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_or_with_different_sharding_columns.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_none_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_or_with_none_sharding_columns.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_none_sharding_columns.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_or_with_none_sharding_columns.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_same_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_or_with_same_sharding_columns.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_or_with_same_sharding_columns.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_or_with_same_sharding_columns.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_asc_and_index_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_order_by_asc_and_index_desc.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_asc_and_index_desc.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_order_by_asc_and_index_desc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_desc_and_index_asc.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_order_by_desc_and_index_asc.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_desc_and_index_asc.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_order_by_desc_and_index_asc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_with_date.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_order_by_with_date.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_with_date.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_order_by_with_date.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_with_ordered_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_order_by_with_ordered_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_order_by_with_ordered_column.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_order_by_with_ordered_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_pagination_with_offset.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_pagination_with_offset.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_pagination_with_offset_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_pagination_with_offset_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset_and_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_pagination_with_offset_and_row_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_offset_and_row_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_pagination_with_offset_and_row_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_pagination_with_row_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_pagination_with_row_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_number_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_pagination_with_row_number_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_row_number_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_pagination_with_row_number_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_top_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_pagination_with_top_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_pagination_with_top_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_pagination_with_top_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sharding_route_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_sharding_route_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sharding_route_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_sharding_route_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sharding_route_with_broadcast_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_sharding_route_with_broadcast_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sharding_route_with_broadcast_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_sharding_route_with_broadcast_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_group_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_sub_query_with_group_by.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_group_by.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_sub_query_with_group_by.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_multiple_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_sub_query_with_multiple_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_multiple_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_sub_query_with_multiple_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_order_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_sub_query_with_order_by.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_order_by.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_sub_query_with_order_by.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_sub_query_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sub_query_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_sub_query_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sum.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_sum.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_sum.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_sum.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_date_function.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_with_date_function.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_date_function.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_with_date_function.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_expression.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_with_expression.xml similarity index 99% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_expression.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_with_expression.xml index 48ffd4ebbe953..df210b2df30df 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_expression.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_with_expression.xml @@ -40,4 +40,4 @@ - \ No newline at end of file + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_force_index_join.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_with_force_index_join.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_force_index_join.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_with_force_index_join.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_item_alias_match_order_by_and_group_by_items.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_with_item_alias_match_order_by_and_group_by_items.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_item_alias_match_order_by_and_group_by_items.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_with_item_alias_match_order_by_and_group_by_items.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_regexp.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_with_regexp.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/masterslave/select_with_regexp.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/masterslave/select_with_regexp.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/select_constant_without_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/select_constant_without_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/select_constant_without_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/select_constant_without_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_alias_as_keyword.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_alias_as_keyword.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_alias_as_keyword.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_alias_as_keyword.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_avg.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_avg.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_avg.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_avg.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_between_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_between_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_between_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_between_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_count_like_concat.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_count_like_concat.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_count_like_concat.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_count_like_concat.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_count_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_count_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_count_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_count_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_equal_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_equal_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_equal_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_equal_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_full_route_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_full_route_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_full_route_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_full_route_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_avg.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_avg.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_avg.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_avg.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_date_function.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_date_function.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_date_function.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_date_function.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_key_word_alias.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_key_word_alias.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_key_word_alias.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_key_word_alias.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_max.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_max.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_max.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_max.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_min.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_min.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_min.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_min.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_order_by_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_order_by_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_order_by_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_order_by_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_order_by_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_order_by_desc.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_order_by_desc.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_order_by_desc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_sum.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_sum.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_with_sum.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_with_sum.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_without_grouped_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_without_grouped_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_group_by_without_grouped_column.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_group_by_without_grouped_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_in_with_same_sharding_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_in_with_same_sharding_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_in_with_same_sharding_column.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_in_with_same_sharding_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_in_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_in_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_in_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_in_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_inner_join.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_inner_join.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_inner_join.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_inner_join.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_max.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_max.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_max.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_max.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_min.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_min.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_min.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_min.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_not_equal_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_not_equal_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_not_equal_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_not_equal_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_not_in_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_not_in_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_not_in_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_not_in_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_complex_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_or_mix_and_for_complex_pattern.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_complex_pattern.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_or_mix_and_for_complex_pattern.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_simple_pattern.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_or_mix_and_for_simple_pattern.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_for_simple_pattern.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_or_mix_and_for_simple_pattern.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_with_binding_and_broadcast_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_or_mix_and_with_binding_and_broadcast_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_with_binding_and_broadcast_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_or_mix_and_with_binding_and_broadcast_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_or_mix_and_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_mix_and_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_or_mix_and_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_different_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_or_with_different_sharding_columns.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_different_sharding_columns.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_or_with_different_sharding_columns.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_none_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_or_with_none_sharding_columns.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_none_sharding_columns.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_or_with_none_sharding_columns.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_same_sharding_columns.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_or_with_same_sharding_columns.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_or_with_same_sharding_columns.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_or_with_same_sharding_columns.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_asc_and_index_desc.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_order_by_asc_and_index_desc.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_asc_and_index_desc.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_order_by_asc_and_index_desc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_desc_and_index_asc.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_order_by_desc_and_index_asc.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_desc_and_index_asc.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_order_by_desc_and_index_asc.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_with_date.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_order_by_with_date.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_with_date.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_order_by_with_date.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_with_ordered_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_order_by_with_ordered_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_order_by_with_ordered_column.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_order_by_with_ordered_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_pagination_with_offset.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_pagination_with_offset.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_pagination_with_offset_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_pagination_with_offset_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset_and_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_pagination_with_offset_and_row_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_offset_and_row_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_pagination_with_offset_and_row_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_count.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_pagination_with_row_count.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_count.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_pagination_with_row_count.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_number_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_pagination_with_row_number_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_row_number_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_pagination_with_row_number_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_top_and_limit.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_pagination_with_top_and_limit.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_pagination_with_top_and_limit.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_pagination_with_top_and_limit.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sharding_route_with_binding_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_sharding_route_with_binding_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sharding_route_with_binding_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_sharding_route_with_binding_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sharding_route_with_broadcast_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_sharding_route_with_broadcast_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sharding_route_with_broadcast_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_sharding_route_with_broadcast_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_group_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_sub_query_with_group_by.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_group_by.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_sub_query_with_group_by.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_multiple_tables.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_sub_query_with_multiple_tables.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_multiple_tables.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_sub_query_with_multiple_tables.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_order_by.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_sub_query_with_order_by.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_order_by.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_sub_query_with_order_by.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_single_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_sub_query_with_single_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sub_query_with_single_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_sub_query_with_single_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sum.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_sum.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_sum.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_sum.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_with_date_function.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_with_date_function.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_with_date_function.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_with_date_function.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_with_expression.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_with_expression.xml similarity index 98% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_with_expression.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_with_expression.xml index 935f9ac1a8143..0d5091ab7ce96 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_with_expression.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_with_expression.xml @@ -20,4 +20,4 @@ - \ No newline at end of file + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_with_force_index_join.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_with_force_index_join.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_with_force_index_join.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_with_force_index_join.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_with_item_alias_match_order_by_and_group_by_items.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_with_item_alias_match_order_by_and_group_by_items.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_with_item_alias_match_order_by_and_group_by_items.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_with_item_alias_match_order_by_and_group_by_items.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_with_regexp.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_with_regexp.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dataset/tbl/select_with_regexp.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dataset/tbl/select_with_regexp.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dql/dql-integrate-test-cases.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/select/dql-integrate-test-cases.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dql/dql-integrate-test-cases.xml From 2fc04ee5ca8b1c7079f9d77ca92f6acfa4adbc3d Mon Sep 17 00:00:00 2001 From: cherrylzhao Date: Fri, 15 Jun 2018 17:06:36 +0800 Subject: [PATCH 282/351] for sharding-proxy, resolve can't query from slave after executing dml at first time. optimize ExecuteWorker, delete master-slave judge logic. --- .../backend/common/SQLExecuteWorker.java | 20 +----------------- .../common/StatementExecuteWorker.java | 21 +------------------ .../frontend/mysql/MySQLFrontendHandler.java | 2 ++ 3 files changed, 4 insertions(+), 39 deletions(-) diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteWorker.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteWorker.java index 55a8392f40a72..9d513bfea042b 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteWorker.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteWorker.java @@ -74,8 +74,7 @@ private CommandResponsePackets execute(final SQLStatement sqlStatement, final St return executeQuery(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql); case DML: case DDL: - return RuleRegistry.getInstance().isOnlyMasterSlave() ? executeUpdate(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql) - : executeUpdate(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql, sqlStatement); + return executeUpdate(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql, sqlStatement); default: return executeCommon(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql); } @@ -139,23 +138,6 @@ private CommandResponsePackets executeUpdate(final DataSource dataSource, final } } - private CommandResponsePackets executeUpdate(final DataSource dataSource, final String sql) { - try (Connection connection = dataSource.getConnection(); - Statement statement = connection.createStatement()) { - int affectedRows = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS); - ResultSet resultSet = statement.getGeneratedKeys(); - long lastInsertId = 0; - while (resultSet.next()) { - lastInsertId = resultSet.getLong(1); - } - return new CommandResponsePackets(new OKPacket(1, affectedRows, lastInsertId, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); - } catch (final SQLException ex) { - return new CommandResponsePackets(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage())); - } finally { - MasterVisitedManager.clear(); - } - } - private CommandResponsePackets executeCommon(final DataSource dataSource, final String sql) { try ( Connection connection = dataSource.getConnection(); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteWorker.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteWorker.java index 86a377bb6a4e7..00925a35a3205 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteWorker.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteWorker.java @@ -75,8 +75,7 @@ private CommandResponsePackets execute(final SQLStatement sqlStatement, final St return executeQuery(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql); case DML: case DDL: - return RuleRegistry.getInstance().isOnlyMasterSlave() ? executeUpdate(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql) - : executeUpdate(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql, sqlStatement); + return executeUpdate(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql, sqlStatement); default: return executeCommon(RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName), sql); } @@ -158,24 +157,6 @@ private CommandResponsePackets executeUpdate(final DataSource dataSource, final } } - private CommandResponsePackets executeUpdate(final DataSource dataSource, final String sql) { - try (Connection connection = dataSource.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { - setJDBCPreparedStatementParameters(preparedStatement); - int affectedRows = preparedStatement.executeUpdate(); - ResultSet resultSet = preparedStatement.getGeneratedKeys(); - long lastInsertId = 0; - while (resultSet.next()) { - lastInsertId = resultSet.getLong(1); - } - return new CommandResponsePackets(new OKPacket(1, affectedRows, lastInsertId, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); - } catch (final SQLException ex) { - return new CommandResponsePackets(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage())); - } finally { - MasterVisitedManager.clear(); - } - } - private CommandResponsePackets executeCommon(final DataSource dataSource, final String sql) { try ( Connection connection = dataSource.getConnection(); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java index ded4834a57bb3..a6158ee05923a 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java @@ -20,6 +20,7 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.EventLoopGroup; +import io.shardingsphere.core.routing.router.masterslave.MasterVisitedManager; import io.shardingsphere.proxy.frontend.common.FrontendHandler; import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket; import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag; @@ -95,6 +96,7 @@ public void run() { context.writeAndFlush(commandPacket.getResultValue()); } } finally { + MasterVisitedManager.clear(); mysqlPacketPayload.getByteBuf().release(); } } From be63202cc1d1fb1f1ce3dc606814631d8d7fec4f Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 17:20:58 +0800 Subject: [PATCH 283/351] for #601: add judge condition for load cases --- .../io/shardingsphere/dbtest/engine/DDLIntegrateTest.java | 5 +++++ .../io/shardingsphere/dbtest/engine/DMLIntegrateTest.java | 5 +++++ .../io/shardingsphere/dbtest/engine/DQLIntegrateTest.java | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java index 7e96d9ab07a47..8332440169b93 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java @@ -19,6 +19,8 @@ import com.google.common.base.Strings; import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.constant.SQLType; +import io.shardingsphere.core.parsing.SQLJudgeEngine; import io.shardingsphere.dbtest.cases.assertion.IntegrateTestCasesLoader; import io.shardingsphere.dbtest.cases.assertion.ddl.DDLIntegrateTestCase; import io.shardingsphere.dbtest.cases.assertion.ddl.DDLIntegrateTestCaseAssertion; @@ -76,6 +78,9 @@ public static Collection getParameters() { Collection result = new LinkedList<>(); for (Object[] each : sqlCasesLoader.getSupportedSQLTestParameters(Arrays.asList(DatabaseType.values()), DatabaseType.class)) { String sqlCaseId = each[0].toString(); + if (SQLType.DDL != new SQLJudgeEngine(sqlCasesLoader.getSupportedSQL(sqlCaseId)).judge().getType()) { + continue; + } DatabaseType databaseType = (DatabaseType) each[1]; SQLCaseType caseType = (SQLCaseType) each[2]; DDLIntegrateTestCase integrateTestCase = integrateTestCasesLoader.getDDLIntegrateTestCase(sqlCaseId); diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java index d066d896c19c9..45e639eb9373b 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java @@ -18,6 +18,8 @@ package io.shardingsphere.dbtest.engine; import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.constant.SQLType; +import io.shardingsphere.core.parsing.SQLJudgeEngine; import io.shardingsphere.core.rule.DataNode; import io.shardingsphere.core.util.InlineExpressionParser; import io.shardingsphere.dbtest.cases.assertion.IntegrateTestCasesLoader; @@ -84,6 +86,9 @@ public static Collection getParameters() { Collection result = new LinkedList<>(); for (Object[] each : sqlCasesLoader.getSupportedSQLTestParameters(Arrays.asList(DatabaseType.values()), DatabaseType.class)) { String sqlCaseId = each[0].toString(); + if (SQLType.DML != new SQLJudgeEngine(sqlCasesLoader.getSupportedSQL(sqlCaseId)).judge().getType()) { + continue; + } DatabaseType databaseType = (DatabaseType) each[1]; SQLCaseType caseType = (SQLCaseType) each[2]; DMLIntegrateTestCase integrateTestCase = integrateTestCasesLoader.getDMLIntegrateTestCase(sqlCaseId); diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java index 1f59a9fda8d34..3b05e49f04a8c 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java @@ -18,6 +18,8 @@ package io.shardingsphere.dbtest.engine; import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.constant.SQLType; +import io.shardingsphere.core.parsing.SQLJudgeEngine; import io.shardingsphere.dbtest.cases.assertion.IntegrateTestCasesLoader; import io.shardingsphere.dbtest.cases.assertion.dql.DQLIntegrateTestCase; import io.shardingsphere.dbtest.cases.assertion.dql.DQLIntegrateTestCaseAssertion; @@ -86,6 +88,9 @@ public static Collection getParameters() { Collection result = new LinkedList<>(); for (Object[] each : sqlCasesLoader.getSupportedSQLTestParameters(Arrays.asList(DatabaseType.values()), DatabaseType.class)) { String sqlCaseId = each[0].toString(); + if (SQLType.DQL != new SQLJudgeEngine(sqlCasesLoader.getSupportedSQL(sqlCaseId)).judge().getType()) { + continue; + } DatabaseType databaseType = (DatabaseType) each[1]; SQLCaseType caseType = (SQLCaseType) each[2]; DQLIntegrateTestCase integrateTestCase = integrateTestCasesLoader.getDQLIntegrateTestCase(sqlCaseId); From 31b704c8f81870507ef67fc41629b8f3dd5d6865 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 17:24:44 +0800 Subject: [PATCH 284/351] for #601: add case insert_with_partial_placeholders --- sharding-core/src/test/resources/parser/insert.xml | 2 +- .../asserts/cases/dml/dml-integrate-test-cases.xml | 7 +++++-- .../src/test/resources/integrate/assert/insert.xml | 2 +- sharding-sql-test/src/main/resources/sql/dml/insert.xml | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index 9eff068d40a51..b9abf7524fa51 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -20,7 +20,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index 4793a760c99d5..fcc4f27187923 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -6,8 +6,11 @@ - - + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/insert.xml b/sharding-jdbc/src/test/resources/integrate/assert/insert.xml index 6c547a6fef4b5..4430caeb28337 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/insert.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/insert.xml @@ -5,7 +5,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index 9bf5aaab38815..a49773f8ebdf8 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -1,8 +1,8 @@ + - From fffe74b4abe602dea3fb21c284e33451e8b0637f Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 17:31:47 +0800 Subject: [PATCH 285/351] for #601: add case update_with_alias --- .../src/test/resources/parser/update.xml | 8 +- .../cases/dml/dataset/{ => db}/update.xml | 0 .../dataset/dbtbl_with_masterslave/update.xml | 207 ++++++++++++++++++ .../asserts/cases/dml/dataset/insert.xml | 48 ---- .../cases/dml/dataset/masterslave/update.xml | 127 +++++++++++ .../asserts/cases/dml/dataset/tbl/update.xml | 27 +++ .../cases/dml/dml-integrate-test-cases.xml | 16 +- .../resources/integrate/assert/update.xml | 2 +- .../src/main/resources/sql/dml/insert.xml | 2 +- .../src/main/resources/sql/dml/update.xml | 2 +- 10 files changed, 380 insertions(+), 59 deletions(-) rename sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/{ => db}/update.xml (100%) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/update.xml delete mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/insert.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/update.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/update.xml diff --git a/sharding-core/src/test/resources/parser/update.xml b/sharding-core/src/test/resources/parser/update.xml index 6470caac78d54..6763d56e74e60 100644 --- a/sharding-core/src/test/resources/parser/update.xml +++ b/sharding-core/src/test/resources/parser/update.xml @@ -1,8 +1,8 @@ - + -
+
@@ -19,9 +19,9 @@ - + -
+
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/update.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/update.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/update.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/update.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/update.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/update.xml new file mode 100644 index 0000000000000..e15088161ea04 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/update.xml @@ -0,0 +1,207 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/insert.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/insert.xml deleted file mode 100644 index 663ec6ab706ca..0000000000000 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/insert.xml +++ /dev/null @@ -1,48 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/update.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/update.xml new file mode 100644 index 0000000000000..f668a91642fc2 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/update.xml @@ -0,0 +1,127 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/update.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/update.xml new file mode 100644 index 0000000000000..b8d97f6f4af5b --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/update.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index fcc4f27187923..4c03e50478c1f 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -13,12 +13,20 @@ - - + + + + + + + + + + - - + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/update.xml b/sharding-jdbc/src/test/resources/integrate/assert/update.xml index 4f9d1e1c83d3e..e87f5f24c81aa 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/update.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/update.xml @@ -1,6 +1,6 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index a49773f8ebdf8..a0f0397dac353 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -2,8 +2,8 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dml/update.xml b/sharding-sql-test/src/main/resources/sql/dml/update.xml index 65ed1433857fd..25ccd9c01df9a 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/update.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/update.xml @@ -1,6 +1,6 @@ - + From 245121e84085163e2d1c5d361f7671dad6064164 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 17:33:27 +0800 Subject: [PATCH 286/351] for #601: add case update_without_alias --- .../src/test/resources/parser/update.xml | 2 +- .../cases/dml/dml-integrate-test-cases.xml | 17 +++++++++-------- .../test/resources/integrate/assert/update.xml | 2 +- .../src/main/resources/sql/dml/update.xml | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/sharding-core/src/test/resources/parser/update.xml b/sharding-core/src/test/resources/parser/update.xml index 6763d56e74e60..8a75c88ed0332 100644 --- a/sharding-core/src/test/resources/parser/update.xml +++ b/sharding-core/src/test/resources/parser/update.xml @@ -1,6 +1,6 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index 4c03e50478c1f..92f95f05c97fc 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -12,21 +12,22 @@ - - - - - - - + + + + + - + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/update.xml b/sharding-jdbc/src/test/resources/integrate/assert/update.xml index e87f5f24c81aa..d6389e6732844 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/update.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/update.xml @@ -38,7 +38,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dml/update.xml b/sharding-sql-test/src/main/resources/sql/dml/update.xml index 25ccd9c01df9a..4e7d38d493e9a 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/update.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/update.xml @@ -1,6 +1,6 @@ - + From d04935a7ca746c382ab9d7ebfc8c6640e7b10909 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 17:37:33 +0800 Subject: [PATCH 287/351] for #601: add case delete_with_sharding_value --- .../src/test/resources/parser/delete.xml | 2 +- .../asserts/cases/dml/dataset/db/delete.xml | 46 ++++ .../dataset/dbtbl_with_masterslave/delete.xml | 206 ++++++++++++++++++ .../cases/dml/dataset/masterslave/delete.xml | 126 +++++++++++ .../asserts/cases/dml/dataset/tbl/delete.xml | 26 +++ .../cases/dml/dml-integrate-test-cases.xml | 7 +- .../resources/integrate/assert/delete.xml | 2 +- .../src/main/resources/sql/dml/delete.xml | 2 +- 8 files changed, 412 insertions(+), 5 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/delete.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/delete.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/delete.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/delete.xml diff --git a/sharding-core/src/test/resources/parser/delete.xml b/sharding-core/src/test/resources/parser/delete.xml index 36912d40b5abb..2ca81a8880518 100644 --- a/sharding-core/src/test/resources/parser/delete.xml +++ b/sharding-core/src/test/resources/parser/delete.xml @@ -1,6 +1,6 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/delete.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/delete.xml new file mode 100644 index 0000000000000..319a5643369cb --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/delete.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/delete.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/delete.xml new file mode 100644 index 0000000000000..3035e80bd3e9f --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/delete.xml @@ -0,0 +1,206 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/delete.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/delete.xml new file mode 100644 index 0000000000000..649fa18f52137 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/delete.xml @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/delete.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/delete.xml new file mode 100644 index 0000000000000..29670b1f445d8 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/delete.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index 92f95f05c97fc..696f9576418a3 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -30,7 +30,10 @@ - - + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/delete.xml b/sharding-jdbc/src/test/resources/integrate/assert/delete.xml index 4f952c1e0acf8..ea6ddf38eb9f3 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/delete.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/delete.xml @@ -1,6 +1,6 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dml/delete.xml b/sharding-sql-test/src/main/resources/sql/dml/delete.xml index f8b2077e866a9..9ef198f020cb4 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/delete.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/delete.xml @@ -1,5 +1,5 @@ - + From 3247cd9d82679439a3597c6c2afba352e089c985 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 17:38:29 +0800 Subject: [PATCH 288/351] for #601: add case delete_with_sharding_value --- .../db/{delete.xml => delete_with_sharding_value.xml} | 0 .../{delete.xml => delete_with_sharding_value.xml} | 0 .../{delete.xml => delete_with_sharding_value.xml} | 0 .../tbl/{delete.xml => delete_with_sharding_value.xml} | 0 .../asserts/cases/dml/dml-integrate-test-cases.xml | 8 ++++---- 5 files changed, 4 insertions(+), 4 deletions(-) rename sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/{delete.xml => delete_with_sharding_value.xml} (100%) rename sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/{delete.xml => delete_with_sharding_value.xml} (100%) rename sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/{delete.xml => delete_with_sharding_value.xml} (100%) rename sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/{delete.xml => delete_with_sharding_value.xml} (100%) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/delete.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/delete_with_sharding_value.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/delete.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/delete_with_sharding_value.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/delete.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/delete_with_sharding_value.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/delete.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/delete_with_sharding_value.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/delete.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/delete_with_sharding_value.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/delete.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/delete_with_sharding_value.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/delete.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/delete_with_sharding_value.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/delete.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/delete_with_sharding_value.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index 696f9576418a3..680fb73a10d8e 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -31,9 +31,9 @@ - - - - + + + + From 306eb74d5005e0933395645ee37a02c25ae0ea40 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 15 Jun 2018 17:45:49 +0800 Subject: [PATCH 289/351] for #601: add case delete_without_sharding_value --- .../src/test/resources/parser/delete.xml | 2 +- .../cases/dataset/init/DataSetsRoot.java | 3 +- .../db/delete_without_sharding_value.xml | 7 ++ .../delete_without_sharding_value.xml | 107 ++++++++++++++++++ .../delete_without_sharding_value.xml | 87 ++++++++++++++ .../tbl/delete_without_sharding_value.xml | 7 ++ .../cases/dml/dml-integrate-test-cases.xml | 7 ++ .../resources/integrate/assert/delete.xml | 2 +- .../src/main/resources/sql/dml/delete.xml | 2 +- 9 files changed, 220 insertions(+), 4 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/delete_without_sharding_value.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/delete_without_sharding_value.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/delete_without_sharding_value.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/delete_without_sharding_value.xml diff --git a/sharding-core/src/test/resources/parser/delete.xml b/sharding-core/src/test/resources/parser/delete.xml index 2ca81a8880518..a1d660544b1f7 100644 --- a/sharding-core/src/test/resources/parser/delete.xml +++ b/sharding-core/src/test/resources/parser/delete.xml @@ -19,7 +19,7 @@ - +
diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/dataset/init/DataSetsRoot.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/dataset/init/DataSetsRoot.java index c7c0b70968f95..e620be331423c 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/dataset/init/DataSetsRoot.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/dataset/init/DataSetsRoot.java @@ -24,6 +24,7 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; @Getter @@ -34,7 +35,7 @@ public final class DataSetsRoot { private List metadataList; @XmlElement(name = "dataset") - private List dataSetRows; + private List dataSetRows = new LinkedList<>(); /** * Find data set meta data via data node. diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/delete_without_sharding_value.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/delete_without_sharding_value.xml new file mode 100644 index 0000000000000..5df73dec3e05b --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/delete_without_sharding_value.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/delete_without_sharding_value.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/delete_without_sharding_value.xml new file mode 100644 index 0000000000000..46fa4ec23ce55 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/delete_without_sharding_value.xml @@ -0,0 +1,107 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/delete_without_sharding_value.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/delete_without_sharding_value.xml new file mode 100644 index 0000000000000..5636200800de2 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/delete_without_sharding_value.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/delete_without_sharding_value.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/delete_without_sharding_value.xml new file mode 100644 index 0000000000000..94d92669ab3c1 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/delete_without_sharding_value.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index 680fb73a10d8e..ccdab4f85b893 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -36,4 +36,11 @@ + + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/delete.xml b/sharding-jdbc/src/test/resources/integrate/assert/delete.xml index ea6ddf38eb9f3..4219d76e679f1 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/delete.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/delete.xml @@ -38,7 +38,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dml/delete.xml b/sharding-sql-test/src/main/resources/sql/dml/delete.xml index 9ef198f020cb4..14ed907991bef 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/delete.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/delete.xml @@ -1,5 +1,5 @@ - + From d44d1c0189ad168761cdf9bd0a8d7b66d5917d5b Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 15 Jun 2018 18:24:55 +0800 Subject: [PATCH 290/351] retry init --- .../orchestration/reg/newzk/client/retry/AsyncRetryCenter.java | 1 + .../orchestration/reg/newzk/client/zookeeper/base/Holder.java | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/AsyncRetryCenter.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/AsyncRetryCenter.java index c730d47c72c6d..f5fe9853cc232 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/AsyncRetryCenter.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/retry/AsyncRetryCenter.java @@ -51,6 +51,7 @@ public void init(final DelayRetryPolicy delayRetryPolicy) { if (delayRetryPolicy == null) { LOGGER.warn("delayRetryPolicy is null and auto init with DelayRetryPolicy.newNoInitDelayPolicy"); this.delayRetryPolicy = DelayRetryPolicy.newNoInitDelayPolicy(); + return; } this.delayRetryPolicy = delayRetryPolicy; } diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/Holder.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/Holder.java index 66f9e13e19580..328fe56d324a2 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/Holder.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/base/Holder.java @@ -18,6 +18,7 @@ package io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.base; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener; import lombok.AccessLevel; import lombok.Getter; import org.apache.zookeeper.WatchedEvent; @@ -77,7 +78,7 @@ public void process(final WatchedEvent event) { LOGGER.debug("BaseClient {} process", Constants.GLOBAL_LISTENER_KEY); } if (!context.getWatchers().isEmpty()) { - for (io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener listener : context.getWatchers().values()) { + for (Listener listener : context.getWatchers().values()) { if (listener.getPath() == null || listener.getPath().equals(event.getPath())) { LOGGER.debug("listener process:{}, listener:{}", listener.getPath(), listener.getKey()); listener.process(event); From cba2c793b7bf1fb6d36cb4d30cc9409c72066b2a Mon Sep 17 00:00:00 2001 From: saaav Date: Fri, 15 Jun 2018 18:25:49 +0800 Subject: [PATCH 291/351] clear packet --- .../reg/newzk/client/election/LeaderElection.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/election/LeaderElection.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/election/LeaderElection.java index 336f6651503e8..1459e6045ac23 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/election/LeaderElection.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/election/LeaderElection.java @@ -19,6 +19,7 @@ import io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.Constants; +import io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener; import io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.WatcherCreator; import org.apache.zookeeper.CreateMode; @@ -66,11 +67,11 @@ private boolean contend(final String node, final IProvider provider, final Liste * @throws KeeperException Zookeeper Exception * @throws InterruptedException InterruptedException */ - public void executeContention(final String nodeBeContend, final io.shardingsphere.jdbc.orchestration.reg.newzk.client.action.IProvider provider) throws KeeperException, InterruptedException { + public void executeContention(final String nodeBeContend, final IProvider provider) throws KeeperException, InterruptedException { boolean canBegin; final String realNode = provider.getRealPath(nodeBeContend); - final String contendNode = io.shardingsphere.jdbc.orchestration.reg.newzk.client.utility.PathUtil.getRealPath(realNode, Constants.CHANGING_KEY); - canBegin = this.contend(contendNode, provider, new io.shardingsphere.jdbc.orchestration.reg.newzk.client.zookeeper.section.Listener(contendNode) { + final String contendNode = PathUtil.getRealPath(realNode, Constants.CHANGING_KEY); + canBegin = this.contend(contendNode, provider, new Listener(contendNode) { @Override public void process(final WatchedEvent event) { try { From 4f841c03763bdc504ea7c22195a55b983e457da6 Mon Sep 17 00:00:00 2001 From: haocao Date: Sat, 16 Jun 2018 18:59:38 +0800 Subject: [PATCH 292/351] Fixed the unit test initialization problem for PostgreSQL. --- .../dbtest/env/schema/SchemaEnvironmentManager.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java index 7f931628daf7b..e01f0aa8168bf 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java @@ -77,6 +77,7 @@ public static void createDatabase(final String shardingRuleType) throws IOExcept RunScript.execute(connection, stringReader); } catch (final SQLException ex) { // TODO schema maybe exist for oracle only + ex.printStackTrace(); } } } @@ -98,6 +99,7 @@ public static void dropDatabase(final String shardingRuleType) throws IOExceptio RunScript.execute(connection, stringReader); } catch (final SQLException ex) { // TODO schema maybe not exist for oracle only + ex.printStackTrace(); } } } @@ -112,7 +114,7 @@ private static Collection generateCreateDatabaseSQLs(final DatabaseType if (DatabaseType.H2 == databaseType) { return Collections.emptyList(); } - String sql = DatabaseType.Oracle == databaseType ? "CREATE SCHEMA %s" : "CREATE DATABASE IF NOT EXISTS %s"; + String sql = DatabaseType.Oracle == databaseType ? "CREATE SCHEMA %s" : "CREATE DATABASE %s"; Collection result = new LinkedList<>(); for (String each : databases) { result.add(String.format(sql, each)); @@ -154,6 +156,7 @@ private static void createTable(final SchemaEnvironment databaseEnvironmentSchem RunScript.execute(connection, stringReader); } catch (final SQLException ex) { // TODO schema maybe not exist for oracle only + ex.printStackTrace(); } } } @@ -193,6 +196,7 @@ private static void dropTable(final SchemaEnvironment databaseEnvironmentSchema, RunScript.execute(connection, stringReader); } catch (final SQLException ex) { // TODO schema maybe not exist for oracle only + ex.printStackTrace(); } } } From 7b0281c89f611c65dca9d02fcef9dfe2bb4068fb Mon Sep 17 00:00:00 2001 From: haocao Date: Sat, 16 Jun 2018 22:16:03 +0800 Subject: [PATCH 293/351] Fixed the unit test initialization schema issues. --- .../asserts/env/dbtbl_with_masterslave/schema.xml | 10 ---------- .../src/test/resources/asserts/env/jdbc/schema.xml | 4 ++-- .../src/test/resources/asserts/env/tbl/schema.xml | 10 ---------- 3 files changed, 2 insertions(+), 22 deletions(-) diff --git a/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/schema.xml b/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/schema.xml index 32ddb3b9506ca..ad874acc6a1f5 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/schema.xml +++ b/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/schema.xml @@ -33,16 +33,6 @@ CREATE TABLE t_order_7 (order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id)) CREATE TABLE t_order_8 (order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id)) CREATE TABLE t_order_9 (order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id)) - CREATE INDEX order_index ON t_order_0 (order_id) - CREATE INDEX order_index ON t_order_1 (order_id) - CREATE INDEX order_index ON t_order_2 (order_id) - CREATE INDEX order_index ON t_order_3 (order_id) - CREATE INDEX order_index ON t_order_4 (order_id) - CREATE INDEX order_index ON t_order_5 (order_id) - CREATE INDEX order_index ON t_order_6 (order_id) - CREATE INDEX order_index ON t_order_7 (order_id) - CREATE INDEX order_index ON t_order_8 (order_id) - CREATE INDEX order_index ON t_order_9 (order_id) CREATE TABLE t_order_item_0 (item_id INT NOT NULL, order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, creation_date DATE, PRIMARY KEY (item_id)) CREATE TABLE t_order_item_1 (item_id INT NOT NULL, order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, creation_date DATE, PRIMARY KEY (item_id)) CREATE TABLE t_order_item_2 (item_id INT NOT NULL, order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, creation_date DATE, PRIMARY KEY (item_id)) diff --git a/sharding-jdbc/src/test/resources/asserts/env/jdbc/schema.xml b/sharding-jdbc/src/test/resources/asserts/env/jdbc/schema.xml index 6ee3e75a34914..97aed2d3d74f0 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/jdbc/schema.xml +++ b/sharding-jdbc/src/test/resources/asserts/env/jdbc/schema.xml @@ -7,8 +7,8 @@ CREATE TABLE t_order_0 (order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id)) CREATE TABLE t_order_1 (order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id)) - CREATE TABLE t_order_item_0 (item_id INT NOT NULL, order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (item_id)) - CREATE TABLE t_order_item_1 (item_id INT NOT NULL, order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (item_id)) + CREATE TABLE t_order_item_0 (item_id INT NOT NULL, order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, creation_date DATE, PRIMARY KEY (item_id)) + CREATE TABLE t_order_item_1 (item_id INT NOT NULL, order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, creation_date DATE, PRIMARY KEY (item_id)) CREATE TABLE t_config (id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (id)) CREATE TABLE t_global (id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (id)) diff --git a/sharding-jdbc/src/test/resources/asserts/env/tbl/schema.xml b/sharding-jdbc/src/test/resources/asserts/env/tbl/schema.xml index b35a1bf15769a..d0a74f0c1ea53 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/tbl/schema.xml +++ b/sharding-jdbc/src/test/resources/asserts/env/tbl/schema.xml @@ -14,16 +14,6 @@ CREATE TABLE t_order_7 (order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id)) CREATE TABLE t_order_8 (order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id)) CREATE TABLE t_order_9 (order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id)) - CREATE INDEX order_index ON t_order_0 (order_id) - CREATE INDEX order_index ON t_order_1 (order_id) - CREATE INDEX order_index ON t_order_2 (order_id) - CREATE INDEX order_index ON t_order_3 (order_id) - CREATE INDEX order_index ON t_order_4 (order_id) - CREATE INDEX order_index ON t_order_5 (order_id) - CREATE INDEX order_index ON t_order_6 (order_id) - CREATE INDEX order_index ON t_order_7 (order_id) - CREATE INDEX order_index ON t_order_8 (order_id) - CREATE INDEX order_index ON t_order_9 (order_id) CREATE TABLE t_order_item_0 (item_id INT NOT NULL, order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, creation_date DATE, PRIMARY KEY (item_id)) CREATE TABLE t_order_item_1 (item_id INT NOT NULL, order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, creation_date DATE, PRIMARY KEY (item_id)) CREATE TABLE t_order_item_2 (item_id INT NOT NULL, order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, creation_date DATE, PRIMARY KEY (item_id)) From e464542f67f5ae7a350a7f93a77d6717810594e6 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 17 Jun 2018 11:01:21 +0800 Subject: [PATCH 294/351] for #601: refactor case update_equal_with_geography --- sharding-core/src/test/resources/parser/update.xml | 2 +- sharding-sql-test/src/main/resources/sql/dml/update.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sharding-core/src/test/resources/parser/update.xml b/sharding-core/src/test/resources/parser/update.xml index 8a75c88ed0332..391c50f318f4b 100644 --- a/sharding-core/src/test/resources/parser/update.xml +++ b/sharding-core/src/test/resources/parser/update.xml @@ -38,7 +38,7 @@ - +
diff --git a/sharding-sql-test/src/main/resources/sql/dml/update.xml b/sharding-sql-test/src/main/resources/sql/dml/update.xml index 4e7d38d493e9a..c13b27c74832d 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/update.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/update.xml @@ -2,5 +2,5 @@ - + From 3b34f26fde408f962c543da51fb96af2ce9775d1 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 17 Jun 2018 12:14:03 +0800 Subject: [PATCH 295/351] for #601: add case insert_with_generate_key_column --- .../src/test/resources/parser/insert.xml | 4 +- .../env/schema/SchemaEnvironmentManager.java | 1 - .../db/{insert.xml => insert_for_order.xml} | 0 .../dml/dataset/db/insert_for_order_item.xml | 90 ++++ .../{insert.xml => insert_for_order.xml} | 0 .../insert_for_order_item.xml | 410 ++++++++++++++++++ .../{insert.xml => insert_for_order.xml} | 0 .../masterslave/insert_for_order_item.xml | 250 +++++++++++ .../tbl/{insert.xml => insert_for_order.xml} | 0 .../dml/dataset/tbl/insert_for_order_item.xml | 50 +++ .../cases/dml/dml-integrate-test-cases.xml | 24 +- .../asserts/env/db/sharding-rule.yaml | 4 +- .../dbtbl_with_masterslave/sharding-rule.yaml | 4 +- .../asserts/env/tbl/sharding-rule.yaml | 4 +- .../src/main/resources/sql/dml/insert.xml | 2 +- 15 files changed, 824 insertions(+), 19 deletions(-) rename sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/{insert.xml => insert_for_order.xml} (100%) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert_for_order_item.xml rename sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/{insert.xml => insert_for_order.xml} (100%) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert_for_order_item.xml rename sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/{insert.xml => insert_for_order.xml} (100%) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert_for_order_item.xml rename sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/{insert.xml => insert_for_order.xml} (100%) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert_for_order_item.xml diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index b9abf7524fa51..d1261a88309f7 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -40,13 +40,13 @@ - +
- + diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java index e01f0aa8168bf..81c62f7549afe 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java @@ -196,7 +196,6 @@ private static void dropTable(final SchemaEnvironment databaseEnvironmentSchema, RunScript.execute(connection, stringReader); } catch (final SQLException ex) { // TODO schema maybe not exist for oracle only - ex.printStackTrace(); } } } diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert_for_order.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert_for_order.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert_for_order_item.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert_for_order_item.xml new file mode 100644 index 0000000000000..87cf94275baf3 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert_for_order_item.xml @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert_for_order.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert_for_order.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert_for_order_item.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert_for_order_item.xml new file mode 100644 index 0000000000000..6b135f3df0ed1 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert_for_order_item.xml @@ -0,0 +1,410 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert_for_order.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert_for_order.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert_for_order_item.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert_for_order_item.xml new file mode 100644 index 0000000000000..52a03cccd102a --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert_for_order_item.xml @@ -0,0 +1,250 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert_for_order.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert_for_order.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert_for_order_item.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert_for_order_item.xml new file mode 100644 index 0000000000000..99ee4a8575e6c --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert_for_order_item.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index ccdab4f85b893..73bff58eeebf0 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -1,21 +1,27 @@ - - - - + + + + - - - - + + + + + + + + + + - + diff --git a/sharding-jdbc/src/test/resources/asserts/env/db/sharding-rule.yaml b/sharding-jdbc/src/test/resources/asserts/env/db/sharding-rule.yaml index 8954ab27c93b6..e00728f3e03e5 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/db/sharding-rule.yaml +++ b/sharding-jdbc/src/test/resources/asserts/env/db/sharding-rule.yaml @@ -9,13 +9,13 @@ shardingRule: rangeAlgorithmClassName: io.shardingsphere.dbtest.fixture.RangeModuloAlgorithm t_order_item: actualDataNodes: db_${0..9}.t_order_item - keyGeneratorColumnName: order_item_id - keyGeneratorClassName: io.shardingsphere.dbtest.fixture.DecrementKeyGenerator databaseStrategy: standard: shardingColumn: user_id preciseAlgorithmClassName: io.shardingsphere.dbtest.fixture.PreciseModuloAlgorithm rangeAlgorithmClassName: io.shardingsphere.dbtest.fixture.RangeModuloAlgorithm + keyGeneratorColumnName: item_id + keyGeneratorClassName: io.shardingsphere.dbtest.fixture.DecrementKeyGenerator bindingTables: - t_order,t_order_item defaultDataSourceName: db_0 diff --git a/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/sharding-rule.yaml b/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/sharding-rule.yaml index 001bdd607993d..2ef241f843a1a 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/sharding-rule.yaml +++ b/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/sharding-rule.yaml @@ -14,8 +14,6 @@ shardingRule: shardingColumn: order_id preciseAlgorithmClassName: io.shardingsphere.dbtest.fixture.PreciseModuloAlgorithm rangeAlgorithmClassName: io.shardingsphere.dbtest.fixture.RangeModuloAlgorithm - keyGeneratorColumnName: order_id - keyGeneratorClassName: io.shardingsphere.core.integrate.api.yaml.fixture.IncrementKeyGenerator logicIndex: t_order_index t_order_item: actualDataNodes: db_ms_${0..9}.t_order_item_${0..9} @@ -29,6 +27,8 @@ shardingRule: shardingColumn: order_id preciseAlgorithmClassName: io.shardingsphere.dbtest.fixture.PreciseModuloAlgorithm rangeAlgorithmClassName: io.shardingsphere.dbtest.fixture.RangeModuloAlgorithm + keyGeneratorColumnName: item_id + keyGeneratorClassName: io.shardingsphere.core.integrate.api.yaml.fixture.IncrementKeyGenerator bindingTables: - t_order,t_order_item defaultDataSourceName: db_ms_0 diff --git a/sharding-jdbc/src/test/resources/asserts/env/tbl/sharding-rule.yaml b/sharding-jdbc/src/test/resources/asserts/env/tbl/sharding-rule.yaml index 8065556f30687..14ffe01eb9412 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/tbl/sharding-rule.yaml +++ b/sharding-jdbc/src/test/resources/asserts/env/tbl/sharding-rule.yaml @@ -9,13 +9,13 @@ shardingRule: rangeAlgorithmClassName: io.shardingsphere.dbtest.fixture.RangeModuloAlgorithm t_order_item: actualDataNodes: tbl.t_order_item_${0..9} - keyGeneratorColumnName: order_item_id - keyGeneratorClassName: io.shardingsphere.dbtest.fixture.DecrementKeyGenerator tableStrategy: standard: shardingColumn: order_id preciseAlgorithmClassName: io.shardingsphere.dbtest.fixture.PreciseModuloAlgorithm rangeAlgorithmClassName: io.shardingsphere.dbtest.fixture.RangeModuloAlgorithm + keyGeneratorColumnName: item_id + keyGeneratorClassName: io.shardingsphere.dbtest.fixture.IncrementKeyGenerator bindingTables: - t_order,t_order_item defaultDataSourceName: tbl diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index a0f0397dac353..c62b6995a5a59 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -2,7 +2,7 @@ - + From 224c38e4cd2aea617db5f566c8eda0b64b7bde70 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 17 Jun 2018 16:59:28 +0800 Subject: [PATCH 296/351] for #601: add case insert_without_generate_key_column --- .../src/test/resources/parser/insert.xml | 6 +- .../dbtest/fixture/ConstantKeyGenerator.java | 28 ++ ...ml => insert_with_generate_key_column.xml} | 0 .../db/insert_without_generate_key_column.xml | 90 ++++ ...ml => insert_with_generate_key_column.xml} | 0 .../insert_without_generate_key_column.xml | 410 ++++++++++++++++++ ...ml => insert_with_generate_key_column.xml} | 0 .../insert_without_generate_key_column.xml | 250 +++++++++++ ...ml => insert_with_generate_key_column.xml} | 0 .../insert_without_generate_key_column.xml | 50 +++ .../cases/dml/dml-integrate-test-cases.xml | 18 +- .../asserts/env/db/sharding-rule.yaml | 2 +- .../dbtbl_with_masterslave/sharding-rule.yaml | 5 +- .../asserts/env/tbl/sharding-rule.yaml | 3 +- .../src/main/resources/sql/dml/insert.xml | 2 +- 15 files changed, 848 insertions(+), 16 deletions(-) create mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/dbtest/fixture/ConstantKeyGenerator.java rename sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/{insert_for_order_item.xml => insert_with_generate_key_column.xml} (100%) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert_without_generate_key_column.xml rename sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/{insert_for_order_item.xml => insert_with_generate_key_column.xml} (100%) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert_without_generate_key_column.xml rename sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/{insert_for_order_item.xml => insert_with_generate_key_column.xml} (100%) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert_without_generate_key_column.xml rename sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/{insert_for_order_item.xml => insert_with_generate_key_column.xml} (100%) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert_without_generate_key_column.xml diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index d1261a88309f7..9904a81f972dd 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -63,18 +63,18 @@ - +
- + item_id - + diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/fixture/ConstantKeyGenerator.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/fixture/ConstantKeyGenerator.java new file mode 100644 index 0000000000000..e89ca37e1333b --- /dev/null +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/fixture/ConstantKeyGenerator.java @@ -0,0 +1,28 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *

+ */ + +package io.shardingsphere.dbtest.fixture; + +import io.shardingsphere.core.keygen.KeyGenerator; + +public final class ConstantKeyGenerator implements KeyGenerator { + + @Override + public Number generateKey() { + return 1; + } +} diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert_for_order_item.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert_with_generate_key_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert_for_order_item.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert_with_generate_key_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert_without_generate_key_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert_without_generate_key_column.xml new file mode 100644 index 0000000000000..87cf94275baf3 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/insert_without_generate_key_column.xml @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert_for_order_item.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert_with_generate_key_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert_for_order_item.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert_with_generate_key_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert_without_generate_key_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert_without_generate_key_column.xml new file mode 100644 index 0000000000000..6b135f3df0ed1 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/insert_without_generate_key_column.xml @@ -0,0 +1,410 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert_for_order_item.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert_with_generate_key_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert_for_order_item.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert_with_generate_key_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert_without_generate_key_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert_without_generate_key_column.xml new file mode 100644 index 0000000000000..5d396a71a8f70 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/insert_without_generate_key_column.xml @@ -0,0 +1,250 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert_for_order_item.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert_with_generate_key_column.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert_for_order_item.xml rename to sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert_with_generate_key_column.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert_without_generate_key_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert_without_generate_key_column.xml new file mode 100644 index 0000000000000..99ee4a8575e6c --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/insert_without_generate_key_column.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index 73bff58eeebf0..29bed5cffeda6 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -12,14 +12,20 @@ - + - - - - + + + + + + + + + + + - diff --git a/sharding-jdbc/src/test/resources/asserts/env/db/sharding-rule.yaml b/sharding-jdbc/src/test/resources/asserts/env/db/sharding-rule.yaml index e00728f3e03e5..c14a7a431ecb4 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/db/sharding-rule.yaml +++ b/sharding-jdbc/src/test/resources/asserts/env/db/sharding-rule.yaml @@ -15,7 +15,7 @@ shardingRule: preciseAlgorithmClassName: io.shardingsphere.dbtest.fixture.PreciseModuloAlgorithm rangeAlgorithmClassName: io.shardingsphere.dbtest.fixture.RangeModuloAlgorithm keyGeneratorColumnName: item_id - keyGeneratorClassName: io.shardingsphere.dbtest.fixture.DecrementKeyGenerator + keyGeneratorClassName: io.shardingsphere.dbtest.fixture.ConstantKeyGenerator bindingTables: - t_order,t_order_item defaultDataSourceName: db_0 diff --git a/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/sharding-rule.yaml b/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/sharding-rule.yaml index 2ef241f843a1a..5764abbada028 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/sharding-rule.yaml +++ b/sharding-jdbc/src/test/resources/asserts/env/dbtbl_with_masterslave/sharding-rule.yaml @@ -28,12 +28,11 @@ shardingRule: preciseAlgorithmClassName: io.shardingsphere.dbtest.fixture.PreciseModuloAlgorithm rangeAlgorithmClassName: io.shardingsphere.dbtest.fixture.RangeModuloAlgorithm keyGeneratorColumnName: item_id - keyGeneratorClassName: io.shardingsphere.core.integrate.api.yaml.fixture.IncrementKeyGenerator + keyGeneratorClassName: io.shardingsphere.dbtest.fixture.ConstantKeyGenerator bindingTables: - t_order,t_order_item defaultDataSourceName: db_ms_0 - defaultKeyGeneratorClassName: io.shardingsphere.dbtest.fixture.IncrementKeyGenerator - + masterSlaveRules: db_ms_0: masterDataSourceName: master_db_0 diff --git a/sharding-jdbc/src/test/resources/asserts/env/tbl/sharding-rule.yaml b/sharding-jdbc/src/test/resources/asserts/env/tbl/sharding-rule.yaml index 14ffe01eb9412..9199aea9f430c 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/tbl/sharding-rule.yaml +++ b/sharding-jdbc/src/test/resources/asserts/env/tbl/sharding-rule.yaml @@ -15,8 +15,7 @@ shardingRule: preciseAlgorithmClassName: io.shardingsphere.dbtest.fixture.PreciseModuloAlgorithm rangeAlgorithmClassName: io.shardingsphere.dbtest.fixture.RangeModuloAlgorithm keyGeneratorColumnName: item_id - keyGeneratorClassName: io.shardingsphere.dbtest.fixture.IncrementKeyGenerator + keyGeneratorClassName: io.shardingsphere.dbtest.fixture.ConstantKeyGenerator bindingTables: - t_order,t_order_item defaultDataSourceName: tbl - defaultKeyGeneratorClassName: io.shardingsphere.dbtest.fixture.IncrementKeyGenerator diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index c62b6995a5a59..038d9c065752d 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -3,8 +3,8 @@ + - From 025a025617b24e79b0a35fe585fa87e7bf349c1d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 17 Jun 2018 17:06:42 +0800 Subject: [PATCH 297/351] for #601: remove useless codes --- .../dbtest/fixture/DecrementKeyGenerator.java | 32 ------------------- .../dbtest/fixture/IncrementKeyGenerator.java | 32 ------------------- .../asserts/env/db/sharding-rule.yaml | 1 - .../asserts/env/nullable/sharding-rule.yaml | 4 +-- 4 files changed, 2 insertions(+), 67 deletions(-) delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/dbtest/fixture/DecrementKeyGenerator.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/dbtest/fixture/IncrementKeyGenerator.java diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/fixture/DecrementKeyGenerator.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/fixture/DecrementKeyGenerator.java deleted file mode 100644 index e3ce846682336..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/fixture/DecrementKeyGenerator.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.dbtest.fixture; - -import io.shardingsphere.core.keygen.KeyGenerator; - -import java.util.concurrent.atomic.AtomicInteger; - -public final class DecrementKeyGenerator implements KeyGenerator { - - private final AtomicInteger sequence = new AtomicInteger(100); - - @Override - public Number generateKey() { - return sequence.decrementAndGet(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/fixture/IncrementKeyGenerator.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/fixture/IncrementKeyGenerator.java deleted file mode 100644 index 37a7bd1b30235..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/fixture/IncrementKeyGenerator.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.dbtest.fixture; - -import io.shardingsphere.core.keygen.KeyGenerator; - -import java.util.concurrent.atomic.AtomicInteger; - -public final class IncrementKeyGenerator implements KeyGenerator { - - private static final AtomicInteger SEQUENCE = new AtomicInteger(100); - - @Override - public Number generateKey() { - return SEQUENCE.incrementAndGet(); - } -} diff --git a/sharding-jdbc/src/test/resources/asserts/env/db/sharding-rule.yaml b/sharding-jdbc/src/test/resources/asserts/env/db/sharding-rule.yaml index c14a7a431ecb4..b78f70bd9c878 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/db/sharding-rule.yaml +++ b/sharding-jdbc/src/test/resources/asserts/env/db/sharding-rule.yaml @@ -19,4 +19,3 @@ shardingRule: bindingTables: - t_order,t_order_item defaultDataSourceName: db_0 - defaultKeyGeneratorClassName: io.shardingsphere.dbtest.fixture.IncrementKeyGenerator diff --git a/sharding-jdbc/src/test/resources/asserts/env/nullable/sharding-rule.yaml b/sharding-jdbc/src/test/resources/asserts/env/nullable/sharding-rule.yaml index 208cb2d4c7816..202cedf8f3084 100644 --- a/sharding-jdbc/src/test/resources/asserts/env/nullable/sharding-rule.yaml +++ b/sharding-jdbc/src/test/resources/asserts/env/nullable/sharding-rule.yaml @@ -1,6 +1,4 @@ shardingRule: - defaultDataSourceName: nullable_0 - defaultKeyGeneratorClassName: io.shardingsphere.dbtest.fixture.IncrementKeyGenerator tables: t_order: actualDataNodes: nullable_${0..9}.t_order @@ -10,3 +8,5 @@ shardingRule: preciseAlgorithmClassName: io.shardingsphere.dbtest.fixture.SingleAlgorithm bindingTables: - t_order + defaultDataSourceName: nullable_0 + defaultKeyGeneratorClassName: io.shardingsphere.dbtest.fixture.ConstantKeyGenerator From ff47ea22098c9b67295a1d3615b95a47e22164a7 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 17 Jun 2018 17:16:24 +0800 Subject: [PATCH 298/351] for #601: add case insert_set_with_all_placeholders --- sharding-core/src/test/resources/parser/insert.xml | 2 +- .../asserts/cases/dml/dml-integrate-test-cases.xml | 12 +++++++++++- .../src/main/resources/sql/dml/insert.xml | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index 9904a81f972dd..7dd1c4e90b39c 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -88,7 +88,7 @@
- +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index 29bed5cffeda6..fe73717084998 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -26,7 +26,17 @@ - + + + + + + + + + + + diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index 038d9c065752d..b0cdf9b199cfe 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -4,8 +4,8 @@ + - From 9f78c797f8add97764cf7d764d99647ca48dad53 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 17 Jun 2018 17:22:44 +0800 Subject: [PATCH 299/351] for #601: add case insert_set_with_partial_placeholders --- .../src/test/resources/parser/insert.xml | 30 +++++++++---------- .../cases/dml/dml-integrate-test-cases.xml | 6 ++++ .../src/main/resources/sql/dml/insert.xml | 2 +- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index 7dd1c4e90b39c..1c2cac40e63c9 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -87,7 +87,7 @@ - +
@@ -107,8 +107,8 @@ - - + +
@@ -127,7 +127,7 @@ - +
@@ -150,7 +150,7 @@ - +
@@ -170,7 +170,7 @@ - +
@@ -190,7 +190,7 @@ - +
@@ -213,7 +213,7 @@ - +
@@ -236,7 +236,7 @@ - + @@ -261,7 +261,7 @@ - +
@@ -295,7 +295,7 @@ - +
@@ -376,7 +376,7 @@ - +
@@ -404,7 +404,7 @@ - +
@@ -438,7 +438,7 @@ - +
@@ -469,7 +469,7 @@ - + + + + + + + + + + + + + + diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index 226a19c4acfda..35a7799c74ebd 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -4,12 +4,11 @@ - + - From deca6f9e219778a06a1742aaf9a44a2b03e86008 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 17 Jun 2018 17:52:14 +0800 Subject: [PATCH 302/351] for #601: add case insert_with_all_placeholders_for_table_identifier but ignore now --- sharding-core/src/test/resources/parser/insert.xml | 2 +- .../asserts/cases/dml/dml-integrate-test-cases.xml | 8 ++++++++ sharding-sql-test/src/main/resources/sql/dml/insert.xml | 3 +-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index c5868f8f0f64c..6599f9a1233e3 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -191,7 +191,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index 7077f3e1425dc..9e2a041be4554 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -5,6 +5,14 @@ + + + + + + + + diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index 35a7799c74ebd..68367f09ca22f 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -1,6 +1,7 @@ + @@ -9,9 +10,7 @@ - - From 00eb6a54e520eff984fb1c8cd27ce4fa90df0ed0 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 17 Jun 2018 17:55:37 +0800 Subject: [PATCH 303/351] for #601: add case insert_set_with_all_placeholders_for_table_identifier but ignore now --- sharding-core/src/test/resources/parser/insert.xml | 2 +- .../asserts/cases/dml/dml-integrate-test-cases.xml | 8 ++++++++ sharding-sql-test/src/main/resources/sql/dml/insert.xml | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index 6599f9a1233e3..1159e189c0ac7 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -214,7 +214,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index 9e2a041be4554..8c4357b213780 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -20,6 +20,14 @@ + + + + + + + + diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index 68367f09ca22f..55370d144ff26 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -3,6 +3,7 @@ + @@ -11,7 +12,6 @@ - From 2dc3acea5da8588402b9ecbf8c822dc552fe56db Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 17 Jun 2018 18:04:04 +0800 Subject: [PATCH 304/351] for #601: add case insert_without_columns_with_all_placeholders --- .../src/test/resources/parser/insert.xml | 102 +++++++++--------- .../cases/dml/dml-integrate-test-cases.xml | 11 +- .../resources/integrate/assert/insert.xml | 2 +- .../src/main/resources/sql/dml/insert.xml | 15 +-- 4 files changed, 70 insertions(+), 60 deletions(-) diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index 1159e189c0ac7..f989c3599c247 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -20,6 +20,57 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + order_id + user_id + + + + + + + + + + + + + + + + +
@@ -191,29 +242,6 @@ - - -
- - - - - - - - - - - - - - - - - - - -
@@ -348,34 +376,6 @@ - - - -
- - - - - - - order_id - user_id - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index 8c4357b213780..d4e073b6d9d9b 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -5,7 +5,7 @@ - + @@ -14,13 +14,20 @@ + + + + + + + - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/insert.xml b/sharding-jdbc/src/test/resources/integrate/assert/insert.xml index 4430caeb28337..0f269b26f7c6f 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/insert.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/insert.xml @@ -10,7 +10,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index 55370d144ff26..3b1d68b3b241d 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -2,6 +2,7 @@ + @@ -11,13 +12,15 @@ - - - - - - + + + + + + + + From efc2d781d36cc51376c9267aabda32f4d09f2196 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 17 Jun 2018 18:05:35 +0800 Subject: [PATCH 305/351] for #601: remove case assertInsertWithoutColumnsWithPartialPlaceholder --- .../src/test/resources/parser/insert.xml | 28 ------------------- .../resources/integrate/assert/insert.xml | 14 ---------- .../src/main/resources/sql/dml/insert.xml | 1 - 3 files changed, 43 deletions(-) diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index f989c3599c247..925f6a2847eac 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -377,34 +377,6 @@ - - -
- - - - - - - order_id - user_id - - - - - - - - - - - - - - - - -
diff --git a/sharding-jdbc/src/test/resources/integrate/assert/insert.xml b/sharding-jdbc/src/test/resources/integrate/assert/insert.xml index 0f269b26f7c6f..b7eae70cf862c 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/insert.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/insert.xml @@ -24,20 +24,6 @@ - - - - - - - - - - - - - - diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index 3b1d68b3b241d..95aa60d04b545 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -13,7 +13,6 @@ - From f866a37d92d299e85c2020469223db85c43aae2f Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 17 Jun 2018 23:31:07 +0800 Subject: [PATCH 306/351] for #601: add case insert_with_batch --- .../src/test/resources/parser/insert.xml | 28 +++ .../dml/dataset/db/batch_insert_for_order.xml | 49 ++++ .../batch_insert_for_order.xml | 209 ++++++++++++++++++ .../masterslave/batch_insert_for_order.xml | 129 +++++++++++ .../dataset/tbl/batch_insert_for_order.xml | 29 +++ .../cases/dml/dml-integrate-test-cases.xml | 11 + .../src/main/resources/sql/dml/insert.xml | 1 + 7 files changed, 456 insertions(+) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/batch_insert_for_order.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/batch_insert_for_order.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/batch_insert_for_order.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/batch_insert_for_order.xml diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index 925f6a2847eac..6299f5dbf01b1 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -290,6 +290,34 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/batch_insert_for_order.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/batch_insert_for_order.xml new file mode 100644 index 0000000000000..b97d2ad8fc64a --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/batch_insert_for_order.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/batch_insert_for_order.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/batch_insert_for_order.xml new file mode 100644 index 0000000000000..502e8af12a7c2 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/batch_insert_for_order.xml @@ -0,0 +1,209 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/batch_insert_for_order.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/batch_insert_for_order.xml new file mode 100644 index 0000000000000..e5c0ee53ea106 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/batch_insert_for_order.xml @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/batch_insert_for_order.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/batch_insert_for_order.xml new file mode 100644 index 0000000000000..b1d6d9eb4810b --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/batch_insert_for_order.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index d4e073b6d9d9b..9375699e3db9c 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -79,6 +79,17 @@ + + + + + + + + + + + diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index 95aa60d04b545..ce666526f5698 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -11,6 +11,7 @@ + From c6a1a963d06b08e8fe4deb57c5b9b9d2930f7ec1 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 17 Jun 2018 23:39:38 +0800 Subject: [PATCH 307/351] for #601: add case insert_without_columns_and_with_generate_key_column --- .../src/test/resources/parser/insert.xml | 2 +- .../cases/dml/dml-integrate-test-cases.xml | 38 +++++++++++++++++++ .../resources/integrate/assert/insert.xml | 2 +- .../src/main/resources/sql/dml/insert.xml | 9 +++-- 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index 6299f5dbf01b1..11522690cfd61 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -405,7 +405,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index 9375699e3db9c..4a01b7529f238 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -57,6 +57,13 @@ + + + + + + + @@ -79,6 +86,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/insert.xml b/sharding-jdbc/src/test/resources/integrate/assert/insert.xml index b7eae70cf862c..547e78c564656 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/insert.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/insert.xml @@ -24,7 +24,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index ce666526f5698..cebf3d0ff3637 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -7,15 +7,18 @@ + + - - - + + + + From a387b364c15e44ccbec459137f69b19776a46084 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Sun, 17 Jun 2018 23:47:28 +0800 Subject: [PATCH 308/351] for #601: add case insert_without_columns_and_without_generate_key_column --- .../cases/dml/dml-integrate-test-cases.xml | 39 ++++--------------- .../src/main/resources/sql/dml/insert.xml | 3 +- 2 files changed, 10 insertions(+), 32 deletions(-) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index 4a01b7529f238..7469e09b12849 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -71,6 +71,14 @@ + + + + + + + + @@ -86,37 +94,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index cebf3d0ff3637..54d50afbdd342 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -12,11 +12,12 @@ + - + From 864842a704cd26c11a046fc4a3f657d2aabcd62a Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 18 Jun 2018 00:12:31 +0800 Subject: [PATCH 309/351] for #601: add case insert_with_batch_and_with_generate_key_column --- .../src/test/resources/parser/insert.xml | 2 +- .../batch_insert_with_generate_key_column.xml | 91 ++++ .../batch_insert_with_generate_key_column.xml | 411 ++++++++++++++++++ .../batch_insert_with_generate_key_column.xml | 251 +++++++++++ .../batch_insert_with_generate_key_column.xml | 51 +++ .../cases/dml/dml-integrate-test-cases.xml | 12 +- .../src/main/resources/sql/dml/insert.xml | 10 +- 7 files changed, 814 insertions(+), 14 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/batch_insert_with_generate_key_column.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/batch_insert_with_generate_key_column.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/batch_insert_with_generate_key_column.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/batch_insert_with_generate_key_column.xml diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index 11522690cfd61..4ebbdff10f8bd 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -318,7 +318,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/batch_insert_with_generate_key_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/batch_insert_with_generate_key_column.xml new file mode 100644 index 0000000000000..91bad283f5c66 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/batch_insert_with_generate_key_column.xml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/batch_insert_with_generate_key_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/batch_insert_with_generate_key_column.xml new file mode 100644 index 0000000000000..39ed144a773ac --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/batch_insert_with_generate_key_column.xml @@ -0,0 +1,411 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/batch_insert_with_generate_key_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/batch_insert_with_generate_key_column.xml new file mode 100644 index 0000000000000..9178b74fcae24 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/batch_insert_with_generate_key_column.xml @@ -0,0 +1,251 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/batch_insert_with_generate_key_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/batch_insert_with_generate_key_column.xml new file mode 100644 index 0000000000000..a42388fa77d4d --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/batch_insert_with_generate_key_column.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index 7469e09b12849..a66ad62d7d5e8 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -100,11 +100,13 @@ - - - - - + + + + + + + diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index 54d50afbdd342..ddc2863154ceb 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -7,22 +7,16 @@ - - - - - - + - - + From 24f09fa41c27d5f60b277e3f84ed992f9e93c8c7 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 18 Jun 2018 00:39:29 +0800 Subject: [PATCH 310/351] for #601: add case insert_with_batch_and_without_generate_key_column --- .../src/test/resources/parser/insert.xml | 2 +- ...tch_insert_without_generate_key_column.xml | 91 ++++ ...tch_insert_without_generate_key_column.xml | 411 ++++++++++++++++++ ...tch_insert_without_generate_key_column.xml | 251 +++++++++++ ...tch_insert_without_generate_key_column.xml | 51 +++ .../cases/dml/dml-integrate-test-cases.xml | 9 +- .../src/main/resources/sql/dml/insert.xml | 3 +- 7 files changed, 813 insertions(+), 5 deletions(-) create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/batch_insert_without_generate_key_column.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/batch_insert_without_generate_key_column.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/batch_insert_without_generate_key_column.xml create mode 100644 sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/batch_insert_without_generate_key_column.xml diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index 4ebbdff10f8bd..fec81606b9f6e 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -352,7 +352,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/batch_insert_without_generate_key_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/batch_insert_without_generate_key_column.xml new file mode 100644 index 0000000000000..8aa8bfbf4f018 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/db/batch_insert_without_generate_key_column.xml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/batch_insert_without_generate_key_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/batch_insert_without_generate_key_column.xml new file mode 100644 index 0000000000000..e13756db9bd14 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/dbtbl_with_masterslave/batch_insert_without_generate_key_column.xml @@ -0,0 +1,411 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/batch_insert_without_generate_key_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/batch_insert_without_generate_key_column.xml new file mode 100644 index 0000000000000..9178b74fcae24 --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/masterslave/batch_insert_without_generate_key_column.xml @@ -0,0 +1,251 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/batch_insert_without_generate_key_column.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/batch_insert_without_generate_key_column.xml new file mode 100644 index 0000000000000..06185ae00e22b --- /dev/null +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dataset/tbl/batch_insert_without_generate_key_column.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml index a66ad62d7d5e8..bc53e4587e0be 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/dml/dml-integrate-test-cases.xml @@ -108,8 +108,13 @@ - - + + + + + + + diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index ddc2863154ceb..4f67644f2a028 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -15,8 +15,7 @@ - - + From c493519e8429f32f9037936e164fdbfc7c45a235 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 18 Jun 2018 00:44:27 +0800 Subject: [PATCH 311/351] for #601: finish all cases for dml --- sharding-core/src/test/resources/parser/insert.xml | 8 ++++---- sharding-sql-test/src/main/resources/sql/dml/insert.xml | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index fec81606b9f6e..bdd7ce8328a31 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -222,7 +222,7 @@ - +
@@ -265,7 +265,7 @@ - + @@ -385,8 +385,8 @@ - - assertInsertWithJsonAndGeo + +
diff --git a/sharding-sql-test/src/main/resources/sql/dml/insert.xml b/sharding-sql-test/src/main/resources/sql/dml/insert.xml index 4f67644f2a028..b2f9f7ad2728d 100644 --- a/sharding-sql-test/src/main/resources/sql/dml/insert.xml +++ b/sharding-sql-test/src/main/resources/sql/dml/insert.xml @@ -17,7 +17,7 @@ - - - + + + From 52f065b02fc81bbe9d50d20e6ec16380667c653c Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 18 Jun 2018 23:34:42 +0800 Subject: [PATCH 312/351] for #601: finish all cases for dml --- sharding-core/src/test/resources/parser/insert.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sharding-core/src/test/resources/parser/insert.xml b/sharding-core/src/test/resources/parser/insert.xml index bdd7ce8328a31..6cd8fc76270b7 100644 --- a/sharding-core/src/test/resources/parser/insert.xml +++ b/sharding-core/src/test/resources/parser/insert.xml @@ -324,7 +324,7 @@ - + @@ -358,12 +358,12 @@ - + item_id - + @@ -439,7 +439,7 @@ - +
From 6c952bfcc1ac9b08edbfbe5e1775b3cb6516255c Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 18 Jun 2018 23:35:18 +0800 Subject: [PATCH 313/351] for #601: add case create_table --- sharding-core/src/test/resources/parser/create.xml | 2 +- .../dataset/{assertCreateTable.xml => create_table.xml} | 0 .../asserts/cases/create/ddl-integrate-test-cases.xml | 9 +++++---- .../src/test/resources/integrate/assert/create.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/alter.xml | 1 - sharding-sql-test/src/main/resources/sql/ddl/create.xml | 3 +-- sharding-sql-test/src/main/resources/sql/ddl/drop.xml | 1 - 7 files changed, 8 insertions(+), 10 deletions(-) rename sharding-jdbc/src/test/resources/asserts/cases/create/dataset/{assertCreateTable.xml => create_table.xml} (100%) diff --git a/sharding-core/src/test/resources/parser/create.xml b/sharding-core/src/test/resources/parser/create.xml index 8f8c15974a8fe..b0df0f0b2e92c 100644 --- a/sharding-core/src/test/resources/parser/create.xml +++ b/sharding-core/src/test/resources/parser/create.xml @@ -1,6 +1,6 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/dataset/assertCreateTable.xml b/sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/create/dataset/assertCreateTable.xml rename to sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml index 7c21824fce491..67873787f622b 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml @@ -1,8 +1,9 @@ - - - - + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/create.xml b/sharding-jdbc/src/test/resources/integrate/assert/create.xml index e69ac290a9899..672fa29fb0542 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/create.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/create.xml @@ -1,6 +1,6 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/alter.xml b/sharding-sql-test/src/main/resources/sql/ddl/alter.xml index e26e4a4bd316f..63d5594214379 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/alter.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/alter.xml @@ -2,5 +2,4 @@ - diff --git a/sharding-sql-test/src/main/resources/sql/ddl/create.xml b/sharding-sql-test/src/main/resources/sql/ddl/create.xml index b6537efd745d5..de2f7635d8775 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/create.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/create.xml @@ -1,6 +1,6 @@ - + @@ -8,5 +8,4 @@ - diff --git a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml index 7c3bf4912c59a..4da65e7050c51 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml @@ -9,5 +9,4 @@ - From 2d6048d81f1ce5b3aa0d0efe298af4a1924a806a Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 18 Jun 2018 23:41:02 +0800 Subject: [PATCH 314/351] for #601: add case create_table_if_not_exists --- sharding-core/src/test/resources/parser/create.xml | 2 +- .../asserts/cases/create/ddl-integrate-test-cases.xml | 9 +++++---- .../src/test/resources/integrate/assert/create.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/create.xml | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/sharding-core/src/test/resources/parser/create.xml b/sharding-core/src/test/resources/parser/create.xml index b0df0f0b2e92c..8007503e7ad5f 100644 --- a/sharding-core/src/test/resources/parser/create.xml +++ b/sharding-core/src/test/resources/parser/create.xml @@ -9,7 +9,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml index 67873787f622b..9d8964de6f606 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml @@ -6,10 +6,11 @@ - - - - + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/create.xml b/sharding-jdbc/src/test/resources/integrate/assert/create.xml index 672fa29fb0542..fcc367206ec2c 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/create.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/create.xml @@ -5,7 +5,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/create.xml b/sharding-sql-test/src/main/resources/sql/ddl/create.xml index de2f7635d8775..12dfdda03fd00 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/create.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/create.xml @@ -1,7 +1,7 @@ - + From 4222d5ae848e4da1a20dea3817b406172bdf0725 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 18 Jun 2018 23:56:02 +0800 Subject: [PATCH 315/351] for #601: add case create_temporary_table_if_not_exists --- sharding-core/src/test/resources/parser/create.xml | 2 +- .../shardingsphere/dbtest/engine/DDLIntegrateTest.java | 9 ++++++++- ...CreateTableTempLog.xml => create_temporary_table.xml} | 0 .../asserts/cases/create/ddl-integrate-test-cases.xml | 9 +++++---- .../src/test/resources/integrate/assert/create.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/create.xml | 6 +++--- 6 files changed, 18 insertions(+), 10 deletions(-) rename sharding-jdbc/src/test/resources/asserts/cases/create/dataset/{assertCreateTableTempLog.xml => create_temporary_table.xml} (100%) diff --git a/sharding-core/src/test/resources/parser/create.xml b/sharding-core/src/test/resources/parser/create.xml index 8007503e7ad5f..8b7357aaba5ac 100644 --- a/sharding-core/src/test/resources/parser/create.xml +++ b/sharding-core/src/test/resources/parser/create.xml @@ -18,7 +18,7 @@ - +
diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java index 8332440169b93..7640267ced15b 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java @@ -65,10 +65,13 @@ public final class DDLIntegrateTest extends BaseIntegrateTest { private final DDLIntegrateTestCaseAssertion assertion; + private final DatabaseType databaseType; + public DDLIntegrateTest(final String sqlCaseId, final String path, final DDLIntegrateTestCaseAssertion assertion, final DatabaseTypeEnvironment databaseTypeEnvironment, final SQLCaseType caseType, final int countInSameCase) throws IOException, JAXBException, SQLException { super(sqlCaseId, path, assertion, databaseTypeEnvironment, caseType, countInSameCase); this.assertion = assertion; + databaseType = databaseTypeEnvironment.getDatabaseType(); } @Parameters(name = "{0}[{5}] -> {2} -> {3} -> {4}") @@ -162,7 +165,11 @@ private void assertMetadata(final List actual, final ExpectedMet private void assertMetadata(final List actual, final ExpectedColumn expect) { for (ExpectedColumn each : actual) { if (expect.getName().equals(each.getName())) { - assertThat(each.getType(), is(expect.getType())); + if (DatabaseType.MySQL == databaseType && "integer".equals(expect.getType())) { + assertThat(each.getType(), is("int")); + } else { + assertThat(each.getType(), is(expect.getType())); + } } } } diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/dataset/assertCreateTableTempLog.xml b/sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_temporary_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/create/dataset/assertCreateTableTempLog.xml rename to sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_temporary_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml index 9d8964de6f606..1cb72a3010cfe 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml @@ -13,10 +13,11 @@ - - - - + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/create.xml b/sharding-jdbc/src/test/resources/integrate/assert/create.xml index fcc367206ec2c..62d44ec36438c 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/create.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/create.xml @@ -10,7 +10,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/create.xml b/sharding-sql-test/src/main/resources/sql/ddl/create.xml index 12dfdda03fd00..b9aeeea626a22 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/create.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/create.xml @@ -1,8 +1,8 @@ - - - + + + From db5265b9d6e024c8963a097e15f91c98e89120a1 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 18 Jun 2018 23:57:32 +0800 Subject: [PATCH 316/351] for #601: add case create_global_temporary_table --- sharding-core/src/test/resources/parser/create.xml | 2 +- .../asserts/cases/create/ddl-integrate-test-cases.xml | 9 +++++---- .../src/test/resources/integrate/assert/create.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/create.xml | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/sharding-core/src/test/resources/parser/create.xml b/sharding-core/src/test/resources/parser/create.xml index 8b7357aaba5ac..903c3faf6c5c0 100644 --- a/sharding-core/src/test/resources/parser/create.xml +++ b/sharding-core/src/test/resources/parser/create.xml @@ -27,7 +27,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml index 1cb72a3010cfe..ef6a26cc86f6c 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml @@ -20,10 +20,11 @@ - - - - + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/create.xml b/sharding-jdbc/src/test/resources/integrate/assert/create.xml index 62d44ec36438c..87930d81bedbf 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/create.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/create.xml @@ -15,7 +15,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/create.xml b/sharding-sql-test/src/main/resources/sql/ddl/create.xml index b9aeeea626a22..572e2515b8e04 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/create.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/create.xml @@ -3,7 +3,7 @@ - + From afa2e97865880e45c29466729bcb70a46dccf305 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Mon, 18 Jun 2018 23:58:44 +0800 Subject: [PATCH 317/351] for #601: add case create_local_temporary_table --- sharding-core/src/test/resources/parser/create.xml | 2 +- .../asserts/cases/create/ddl-integrate-test-cases.xml | 9 +++++---- .../src/test/resources/integrate/assert/create.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/create.xml | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/sharding-core/src/test/resources/parser/create.xml b/sharding-core/src/test/resources/parser/create.xml index 903c3faf6c5c0..9e63cdd8a41dd 100644 --- a/sharding-core/src/test/resources/parser/create.xml +++ b/sharding-core/src/test/resources/parser/create.xml @@ -36,7 +36,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml index ef6a26cc86f6c..b7aabc2963e2e 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml @@ -27,10 +27,11 @@ - - - - + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/create.xml b/sharding-jdbc/src/test/resources/integrate/assert/create.xml index 87930d81bedbf..e36c4c0c48a6b 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/create.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/create.xml @@ -20,7 +20,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/create.xml b/sharding-sql-test/src/main/resources/sql/ddl/create.xml index 572e2515b8e04..d2bb7823719f4 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/create.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/create.xml @@ -4,7 +4,7 @@ - + From de3449ac711d28df56f30a82d1477e12d7c18e06 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 00:00:23 +0800 Subject: [PATCH 318/351] for #601: add case create_unlogged_table --- sharding-core/src/test/resources/parser/create.xml | 2 +- .../asserts/cases/create/ddl-integrate-test-cases.xml | 9 +++++---- .../src/test/resources/integrate/assert/create.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/create.xml | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/sharding-core/src/test/resources/parser/create.xml b/sharding-core/src/test/resources/parser/create.xml index 9e63cdd8a41dd..45143e9719e62 100644 --- a/sharding-core/src/test/resources/parser/create.xml +++ b/sharding-core/src/test/resources/parser/create.xml @@ -45,7 +45,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml index b7aabc2963e2e..0fd98e8b28dde 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml @@ -34,10 +34,11 @@ - - - - + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/create.xml b/sharding-jdbc/src/test/resources/integrate/assert/create.xml index e36c4c0c48a6b..82dc0ddeb8de0 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/create.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/create.xml @@ -25,7 +25,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/create.xml b/sharding-sql-test/src/main/resources/sql/ddl/create.xml index d2bb7823719f4..79587958094e4 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/create.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/create.xml @@ -5,7 +5,7 @@ - + From 5a6aaca24cf035660ff93bbbe318a53b6dc951a2 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 00:01:39 +0800 Subject: [PATCH 319/351] for #601: refactor case create_local_temp_table --- sharding-core/src/test/resources/parser/create.xml | 2 +- .../resources/asserts/cases/create/ddl-integrate-test-cases.xml | 2 +- sharding-jdbc/src/test/resources/integrate/assert/create.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/create.xml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sharding-core/src/test/resources/parser/create.xml b/sharding-core/src/test/resources/parser/create.xml index 45143e9719e62..24a3e52fbc21f 100644 --- a/sharding-core/src/test/resources/parser/create.xml +++ b/sharding-core/src/test/resources/parser/create.xml @@ -36,7 +36,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml index 0fd98e8b28dde..b75f06c9eae23 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml @@ -27,7 +27,7 @@ - + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/create.xml b/sharding-jdbc/src/test/resources/integrate/assert/create.xml index 82dc0ddeb8de0..ea2769c9ed645 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/create.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/create.xml @@ -20,7 +20,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/create.xml b/sharding-sql-test/src/main/resources/sql/ddl/create.xml index 79587958094e4..e68269f9e3a61 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/create.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/create.xml @@ -4,7 +4,7 @@ - + From 83dfd27b5d67990220ea6a9242e8addc4126358d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 00:03:07 +0800 Subject: [PATCH 320/351] for #601: add case create_index --- sharding-core/src/test/resources/parser/create.xml | 2 +- .../dataset/{assertCreateIndex.xml => create_index.xml} | 0 .../asserts/cases/create/ddl-integrate-test-cases.xml | 9 +++++---- .../src/test/resources/integrate/assert/create.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/create.xml | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) rename sharding-jdbc/src/test/resources/asserts/cases/create/dataset/{assertCreateIndex.xml => create_index.xml} (100%) diff --git a/sharding-core/src/test/resources/parser/create.xml b/sharding-core/src/test/resources/parser/create.xml index 24a3e52fbc21f..9b5b6217e09cd 100644 --- a/sharding-core/src/test/resources/parser/create.xml +++ b/sharding-core/src/test/resources/parser/create.xml @@ -54,7 +54,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/dataset/assertCreateIndex.xml b/sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_index.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/create/dataset/assertCreateIndex.xml rename to sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_index.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml index b75f06c9eae23..62bab2a38de25 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml @@ -41,10 +41,11 @@ - - - - + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/create.xml b/sharding-jdbc/src/test/resources/integrate/assert/create.xml index ea2769c9ed645..9710017bca8aa 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/create.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/create.xml @@ -30,7 +30,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/create.xml b/sharding-sql-test/src/main/resources/sql/ddl/create.xml index e68269f9e3a61..385f4e7957989 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/create.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/create.xml @@ -6,6 +6,6 @@ - + From 6e78e80a66ec8d2c6214497a14675ffd1e9d8ad3 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 00:04:58 +0800 Subject: [PATCH 321/351] for #601: add case create_unique_index --- sharding-core/src/test/resources/parser/create.xml | 2 +- ...sertCreateUniqueIndex.xml => create_unique_index.xml} | 0 .../asserts/cases/create/ddl-integrate-test-cases.xml | 9 +++++---- .../src/test/resources/integrate/assert/create.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/create.xml | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) rename sharding-jdbc/src/test/resources/asserts/cases/create/dataset/{assertCreateUniqueIndex.xml => create_unique_index.xml} (100%) diff --git a/sharding-core/src/test/resources/parser/create.xml b/sharding-core/src/test/resources/parser/create.xml index 9b5b6217e09cd..edf63dfea9f66 100644 --- a/sharding-core/src/test/resources/parser/create.xml +++ b/sharding-core/src/test/resources/parser/create.xml @@ -64,7 +64,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/dataset/assertCreateUniqueIndex.xml b/sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_unique_index.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/create/dataset/assertCreateUniqueIndex.xml rename to sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_unique_index.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml index 62bab2a38de25..4aee73f6b4fd7 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml @@ -48,9 +48,10 @@ - - - - + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/create.xml b/sharding-jdbc/src/test/resources/integrate/assert/create.xml index 9710017bca8aa..ae86a5102ec6f 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/create.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/create.xml @@ -35,7 +35,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/create.xml b/sharding-sql-test/src/main/resources/sql/ddl/create.xml index 385f4e7957989..8703e9d12d331 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/create.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/create.xml @@ -7,5 +7,5 @@ - + From 41e8ed1d6b303734968b9b7cd5e1f49723e29717 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 00:08:16 +0800 Subject: [PATCH 322/351] for #601: add case alter_table --- sharding-core/src/test/resources/parser/alter.xml | 2 +- .../{t_order_update.xml => alter_table.xml} | 0 .../cases/alter/ddl-integrate-test-cases.xml | 15 ++++++++------- .../asserts/cases/create/dataset/update.xml | 7 ------- .../src/test/resources/integrate/assert/alter.xml | 2 +- .../src/main/resources/sql/ddl/alter.xml | 2 +- 6 files changed, 11 insertions(+), 17 deletions(-) rename sharding-jdbc/src/test/resources/asserts/cases/alter/dataset/{t_order_update.xml => alter_table.xml} (100%) delete mode 100644 sharding-jdbc/src/test/resources/asserts/cases/create/dataset/update.xml diff --git a/sharding-core/src/test/resources/parser/alter.xml b/sharding-core/src/test/resources/parser/alter.xml index 5acb73c8e1963..dd6dacf500536 100644 --- a/sharding-core/src/test/resources/parser/alter.xml +++ b/sharding-core/src/test/resources/parser/alter.xml @@ -1,6 +1,6 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/alter/dataset/t_order_update.xml b/sharding-jdbc/src/test/resources/asserts/cases/alter/dataset/alter_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/alter/dataset/t_order_update.xml rename to sharding-jdbc/src/test/resources/asserts/cases/alter/dataset/alter_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/alter/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/alter/ddl-integrate-test-cases.xml index e9e3348c41c9c..a8c0706dba153 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/alter/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/alter/ddl-integrate-test-cases.xml @@ -1,13 +1,14 @@ - - - - + + + + + - - - + + + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/dataset/update.xml b/sharding-jdbc/src/test/resources/asserts/cases/create/dataset/update.xml deleted file mode 100644 index f429a9faa2e52..0000000000000 --- a/sharding-jdbc/src/test/resources/asserts/cases/create/dataset/update.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/alter.xml b/sharding-jdbc/src/test/resources/integrate/assert/alter.xml index 9e37fb05f641e..b97b4725445d3 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/alter.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/alter.xml @@ -1,6 +1,6 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/alter.xml b/sharding-sql-test/src/main/resources/sql/ddl/alter.xml index 63d5594214379..0cab9d0fe23c3 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/alter.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/alter.xml @@ -1,5 +1,5 @@ - + From f4e94a40fa6c2c7bc139d2932dfbee88daadfca7 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 00:09:32 +0800 Subject: [PATCH 323/351] for #601: add case alter_table_if_exists_only --- sharding-core/src/test/resources/parser/alter.xml | 2 +- .../resources/asserts/cases/alter/ddl-integrate-test-cases.xml | 3 ++- sharding-jdbc/src/test/resources/integrate/assert/alter.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/alter.xml | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/sharding-core/src/test/resources/parser/alter.xml b/sharding-core/src/test/resources/parser/alter.xml index dd6dacf500536..73996be15e466 100644 --- a/sharding-core/src/test/resources/parser/alter.xml +++ b/sharding-core/src/test/resources/parser/alter.xml @@ -9,7 +9,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/alter/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/alter/ddl-integrate-test-cases.xml index a8c0706dba153..11b862cf0021d 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/alter/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/alter/ddl-integrate-test-cases.xml @@ -6,9 +6,10 @@ - + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/alter.xml b/sharding-jdbc/src/test/resources/integrate/assert/alter.xml index b97b4725445d3..b1789ca76d58e 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/alter.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/alter.xml @@ -5,7 +5,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/alter.xml b/sharding-sql-test/src/main/resources/sql/ddl/alter.xml index 0cab9d0fe23c3..19ce2431c3fee 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/alter.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/alter.xml @@ -1,5 +1,5 @@ - + From 9c8f3d4a87ad18d30efb5469babd369ef20b43c1 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 00:10:41 +0800 Subject: [PATCH 324/351] for #601: merge ddl cases --- .../cases/alter/ddl-integrate-test-cases.xml | 15 --------------- .../cases/{alter => ddl}/dataset/alter_table.xml | 0 .../{create => ddl}/dataset/create_index.xml | 0 .../{create => ddl}/dataset/create_table.xml | 0 .../dataset/create_temporary_table.xml | 0 .../dataset/create_unique_index.xml | 0 .../{create => ddl}/ddl-integrate-test-cases.xml | 14 ++++++++++++++ 7 files changed, 14 insertions(+), 15 deletions(-) delete mode 100644 sharding-jdbc/src/test/resources/asserts/cases/alter/ddl-integrate-test-cases.xml rename sharding-jdbc/src/test/resources/asserts/cases/{alter => ddl}/dataset/alter_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{create => ddl}/dataset/create_index.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{create => ddl}/dataset/create_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{create => ddl}/dataset/create_temporary_table.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{create => ddl}/dataset/create_unique_index.xml (100%) rename sharding-jdbc/src/test/resources/asserts/cases/{create => ddl}/ddl-integrate-test-cases.xml (76%) diff --git a/sharding-jdbc/src/test/resources/asserts/cases/alter/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/alter/ddl-integrate-test-cases.xml deleted file mode 100644 index 11b862cf0021d..0000000000000 --- a/sharding-jdbc/src/test/resources/asserts/cases/alter/ddl-integrate-test-cases.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/asserts/cases/alter/dataset/alter_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/dataset/alter_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/alter/dataset/alter_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/ddl/dataset/alter_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_index.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/dataset/create_index.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_index.xml rename to sharding-jdbc/src/test/resources/asserts/cases/ddl/dataset/create_index.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/dataset/create_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/ddl/dataset/create_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_temporary_table.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/dataset/create_temporary_table.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_temporary_table.xml rename to sharding-jdbc/src/test/resources/asserts/cases/ddl/dataset/create_temporary_table.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_unique_index.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/dataset/create_unique_index.xml similarity index 100% rename from sharding-jdbc/src/test/resources/asserts/cases/create/dataset/create_unique_index.xml rename to sharding-jdbc/src/test/resources/asserts/cases/ddl/dataset/create_unique_index.xml diff --git a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml similarity index 76% rename from sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml rename to sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml index 4aee73f6b4fd7..a5b86b413a54d 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/create/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml @@ -54,4 +54,18 @@ + + + + + + + + + + + + + + From dca9d5d3d99c6513e8c984ad6083c54018adb41f Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 10:29:38 +0800 Subject: [PATCH 325/351] for #601: remove nullable rule --- .../test/resources/parser/select_nullable.xml | 14 ---- .../asserts/env/nullable/data-init.xml | 47 ----------- .../resources/asserts/env/nullable/schema.xml | 21 ----- .../asserts/env/nullable/sharding-rule.yaml | 12 --- .../test/resources/integrate/assert/alter.xml | 13 --- .../resources/integrate/assert/create.xml | 43 ---------- .../resources/integrate/assert/delete.xml | 46 ----------- .../resources/integrate/assert/insert.xml | 55 ------------- .../integrate/assert/select_nullable.xml | 8 -- .../resources/integrate/assert/update.xml | 79 ------------------- .../resources/sql/dql/select_nullable.xml | 4 - 11 files changed, 342 deletions(-) delete mode 100644 sharding-core/src/test/resources/parser/select_nullable.xml delete mode 100644 sharding-jdbc/src/test/resources/asserts/env/nullable/data-init.xml delete mode 100644 sharding-jdbc/src/test/resources/asserts/env/nullable/schema.xml delete mode 100644 sharding-jdbc/src/test/resources/asserts/env/nullable/sharding-rule.yaml delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/alter.xml delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/create.xml delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/delete.xml delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/insert.xml delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/select_nullable.xml delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/update.xml delete mode 100644 sharding-sql-test/src/main/resources/sql/dql/select_nullable.xml diff --git a/sharding-core/src/test/resources/parser/select_nullable.xml b/sharding-core/src/test/resources/parser/select_nullable.xml deleted file mode 100644 index 3a8f90388fa9b..0000000000000 --- a/sharding-core/src/test/resources/parser/select_nullable.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - -
- - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/asserts/env/nullable/data-init.xml b/sharding-jdbc/src/test/resources/asserts/env/nullable/data-init.xml deleted file mode 100644 index 5300424b53319..0000000000000 --- a/sharding-jdbc/src/test/resources/asserts/env/nullable/data-init.xml +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/asserts/env/nullable/schema.xml b/sharding-jdbc/src/test/resources/asserts/env/nullable/schema.xml deleted file mode 100644 index 65a43bee1dce5..0000000000000 --- a/sharding-jdbc/src/test/resources/asserts/env/nullable/schema.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - nullable_0 - nullable_1 - nullable_2 - nullable_3 - nullable_4 - nullable_5 - nullable_6 - nullable_7 - nullable_8 - nullable_9 - - - CREATE TABLE t_order (order_id INT NOT NULL, user_id INT NULL, sku_num INT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id)) - - - DROP TABLE t_order - - diff --git a/sharding-jdbc/src/test/resources/asserts/env/nullable/sharding-rule.yaml b/sharding-jdbc/src/test/resources/asserts/env/nullable/sharding-rule.yaml deleted file mode 100644 index 202cedf8f3084..0000000000000 --- a/sharding-jdbc/src/test/resources/asserts/env/nullable/sharding-rule.yaml +++ /dev/null @@ -1,12 +0,0 @@ -shardingRule: - tables: - t_order: - actualDataNodes: nullable_${0..9}.t_order - databaseStrategy: - standard: - shardingColumn: user_id - preciseAlgorithmClassName: io.shardingsphere.dbtest.fixture.SingleAlgorithm - bindingTables: - - t_order - defaultDataSourceName: nullable_0 - defaultKeyGeneratorClassName: io.shardingsphere.dbtest.fixture.ConstantKeyGenerator diff --git a/sharding-jdbc/src/test/resources/integrate/assert/alter.xml b/sharding-jdbc/src/test/resources/integrate/assert/alter.xml deleted file mode 100644 index b1789ca76d58e..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/alter.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/create.xml b/sharding-jdbc/src/test/resources/integrate/assert/create.xml deleted file mode 100644 index ae86a5102ec6f..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/create.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/delete.xml b/sharding-jdbc/src/test/resources/integrate/assert/delete.xml deleted file mode 100644 index 4219d76e679f1..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/delete.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/insert.xml b/sharding-jdbc/src/test/resources/integrate/assert/insert.xml deleted file mode 100644 index 547e78c564656..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/insert.xml +++ /dev/null @@ -1,55 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/select_nullable.xml b/sharding-jdbc/src/test/resources/integrate/assert/select_nullable.xml deleted file mode 100644 index 2c69024d0b3f0..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/select_nullable.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/update.xml b/sharding-jdbc/src/test/resources/integrate/assert/update.xml deleted file mode 100644 index d6389e6732844..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/update.xml +++ /dev/null @@ -1,79 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-sql-test/src/main/resources/sql/dql/select_nullable.xml b/sharding-sql-test/src/main/resources/sql/dql/select_nullable.xml deleted file mode 100644 index cef10ffc9ff87..0000000000000 --- a/sharding-sql-test/src/main/resources/sql/dql/select_nullable.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - From f0c6cfd9430ad4c91db43fd7c5a16ca511582976 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 10:32:57 +0800 Subject: [PATCH 326/351] for #601: remove useless original test cases --- .../core/integrate/AllIntegrateTests.java | 14 --- .../type/ms/MasterSlaveOnlyDQLTest.java | 48 --------- ...AbstractNullableShardingTableOnlyTest.java | 97 ------------------- ...ullableShardingTableOnlyDMLAndDDLTest.java | 46 --------- .../NullableShardingTableOnlyDQLTest.java | 47 --------- .../ShardingDatabaseAndTableDQLTest.java | 48 --------- .../sharding/ShardingDatabaseOnlyDQLTest.java | 48 --------- .../sharding/ShardingMasterSlaveDQLTest.java | 48 --------- .../sharding/ShardingTableOnlyDQLTest.java | 48 --------- 9 files changed, 444 deletions(-) delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/ms/MasterSlaveOnlyDQLTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractNullableShardingTableOnlyTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/NullableShardingTableOnlyDMLAndDDLTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/NullableShardingTableOnlyDQLTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseAndTableDQLTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseOnlyDQLTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingMasterSlaveDQLTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingTableOnlyDQLTest.java diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java index 66db9c5d636db..56888eae95846 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java @@ -19,17 +19,10 @@ import io.shardingsphere.core.integrate.api.AllAPIIntegrateTests; import io.shardingsphere.core.integrate.type.ms.MasterSlaveOnlyDMLAndDDLTest; -import io.shardingsphere.core.integrate.type.ms.MasterSlaveOnlyDQLTest; -import io.shardingsphere.core.integrate.type.sharding.NullableShardingTableOnlyDMLAndDDLTest; -import io.shardingsphere.core.integrate.type.sharding.NullableShardingTableOnlyDQLTest; import io.shardingsphere.core.integrate.type.sharding.ShardingDatabaseAndTableDMLAndDDLTest; -import io.shardingsphere.core.integrate.type.sharding.ShardingDatabaseAndTableDQLTest; import io.shardingsphere.core.integrate.type.sharding.ShardingDatabaseOnlyDMLAndDDLTest; -import io.shardingsphere.core.integrate.type.sharding.ShardingDatabaseOnlyDQLTest; import io.shardingsphere.core.integrate.type.sharding.ShardingMasterSlaveDMLAndDDLTest; -import io.shardingsphere.core.integrate.type.sharding.ShardingMasterSlaveDQLTest; import io.shardingsphere.core.integrate.type.sharding.ShardingTableOnlyDMLAndDDLTest; -import io.shardingsphere.core.integrate.type.sharding.ShardingTableOnlyDQLTest; import io.shardingsphere.core.integrate.type.sharding.hint.AllHintDatabaseOnlyTests; import org.junit.runner.RunWith; import org.junit.runners.Suite; @@ -37,18 +30,11 @@ @RunWith(Suite.class) @SuiteClasses({ - ShardingDatabaseOnlyDQLTest.class, ShardingDatabaseOnlyDMLAndDDLTest.class, - ShardingTableOnlyDQLTest.class, ShardingTableOnlyDMLAndDDLTest.class, - ShardingMasterSlaveDQLTest.class, ShardingMasterSlaveDMLAndDDLTest.class, - MasterSlaveOnlyDQLTest.class, MasterSlaveOnlyDMLAndDDLTest.class, - ShardingDatabaseAndTableDQLTest.class, ShardingDatabaseAndTableDMLAndDDLTest.class, - NullableShardingTableOnlyDQLTest.class, - NullableShardingTableOnlyDMLAndDDLTest.class, AllHintDatabaseOnlyTests.class, AllAPIIntegrateTests.class }) diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/ms/MasterSlaveOnlyDQLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/ms/MasterSlaveOnlyDQLTest.java deleted file mode 100644 index 6e87947ec6696..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/ms/MasterSlaveOnlyDQLTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.ms; - -import io.shardingsphere.core.common.base.AbstractSQLAssertTest; -import io.shardingsphere.core.common.base.AbstractSQLTest; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import org.junit.BeforeClass; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Collection; -import java.util.List; - -@RunWith(Parameterized.class) -public class MasterSlaveOnlyDQLTest extends AbstractMasterSlaveOnlyTest { - - public MasterSlaveOnlyDQLTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - @BeforeClass - public static void cleanAndInitTable() throws Exception { - AbstractSQLAssertTest.importAllDataSet(AbstractMasterSlaveOnlyTest.getInitFiles()); - } - - @Parameterized.Parameters(name = "{0}In{2}") - public static Collection dataParameters() { - return AbstractSQLTest.dataParameters(SQLType.DQL); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractNullableShardingTableOnlyTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractNullableShardingTableOnlyTest.java deleted file mode 100644 index ad87c3f303145..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractNullableShardingTableOnlyTest.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding; - -import io.shardingsphere.core.api.config.ShardingRuleConfiguration; -import io.shardingsphere.core.api.config.TableRuleConfiguration; -import io.shardingsphere.core.api.config.strategy.ComplexShardingStrategyConfiguration; -import io.shardingsphere.core.common.base.AbstractSQLAssertTest; -import io.shardingsphere.core.common.env.ShardingTestStrategy; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.integrate.fixture.ComplexKeysModuloDatabaseShardingAlgorithm; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import io.shardingsphere.core.integrate.jaxb.helper.SQLAssertJAXBHelper; -import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; -import io.shardingsphere.core.rule.ShardingRule; - -import javax.sql.DataSource; -import java.sql.SQLException; -import java.util.Arrays; -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -public abstract class AbstractNullableShardingTableOnlyTest extends AbstractSQLAssertTest { - - public AbstractNullableShardingTableOnlyTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - protected static Collection dataParameters(final SQLType... sqlTypes) { - List result = new LinkedList<>(); - for (SQLType each : sqlTypes) { - result.addAll(SQLAssertJAXBHelper.getDataParameters("integrate/assert/select_aggregate.xml", each)); - result.addAll(SQLAssertJAXBHelper.getDataParameters("integrate/assert/select_nullable.xml", each)); - } - return result; - } - - protected static List getInitFiles() { - return Arrays.asList( - "integrate/dataset/sharding/nullable/init/nullable_0.xml", - "integrate/dataset/sharding/nullable/init/nullable_1.xml", - "integrate/dataset/sharding/nullable/init/nullable_2.xml", - "integrate/dataset/sharding/nullable/init/nullable_3.xml", - "integrate/dataset/sharding/nullable/init/nullable_4.xml", - "integrate/dataset/sharding/nullable/init/nullable_5.xml", - "integrate/dataset/sharding/nullable/init/nullable_6.xml", - "integrate/dataset/sharding/nullable/init/nullable_7.xml", - "integrate/dataset/sharding/nullable/init/nullable_8.xml", - "integrate/dataset/sharding/nullable/init/nullable_9.xml"); - } - - @Override - protected ShardingTestStrategy getShardingStrategy() { - return ShardingTestStrategy.nullable; - } - - @Override - protected List getInitDataSetFiles() { - return AbstractNullableShardingTableOnlyTest.getInitFiles(); - } - - @Override - protected final Map getDataSources() throws SQLException { - if (!getShardingDataSources().isEmpty()) { - return getShardingDataSources(); - } - Map> dataSourceMap = createDataSourceMap(); - for (Map.Entry> each : dataSourceMap.entrySet()) { - ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); - TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration(); - tableRuleConfig.setLogicTable("t_order"); - shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig); - shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new ComplexShardingStrategyConfiguration("user_id", new ComplexKeysModuloDatabaseShardingAlgorithm())); - ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, each.getValue().keySet()); - getShardingDataSources().put(each.getKey(), new ShardingDataSource(each.getValue(), shardingRule)); - } - return getShardingDataSources(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/NullableShardingTableOnlyDMLAndDDLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/NullableShardingTableOnlyDMLAndDDLTest.java deleted file mode 100644 index 18bdaced745d1..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/NullableShardingTableOnlyDMLAndDDLTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding; - -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import org.junit.Before; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Collection; -import java.util.List; - -@RunWith(Parameterized.class) -public class NullableShardingTableOnlyDMLAndDDLTest extends AbstractNullableShardingTableOnlyTest { - - public NullableShardingTableOnlyDMLAndDDLTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - @Parameterized.Parameters(name = "{0}In{2}") - public static Collection dataParameters() { - return dataParameters(SQLType.DML, SQLType.DDL); - } - - @Before - public void cleanAndInitTable() throws Exception { - importDataSet(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/NullableShardingTableOnlyDQLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/NullableShardingTableOnlyDQLTest.java deleted file mode 100644 index faff9f8a1b979..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/NullableShardingTableOnlyDQLTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding; - -import io.shardingsphere.core.common.base.AbstractSQLAssertTest; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import org.junit.BeforeClass; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Collection; -import java.util.List; - -@RunWith(Parameterized.class) -public class NullableShardingTableOnlyDQLTest extends AbstractNullableShardingTableOnlyTest { - - public NullableShardingTableOnlyDQLTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - @BeforeClass - public static void cleanAndInitTable() throws Exception { - AbstractSQLAssertTest.importAllDataSet(getInitFiles()); - } - - @Parameterized.Parameters(name = "{0}In{2}") - public static Collection dataParameters() { - return dataParameters(SQLType.DQL); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseAndTableDQLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseAndTableDQLTest.java deleted file mode 100644 index 5e69a0ea64afe..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseAndTableDQLTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding; - -import io.shardingsphere.core.common.base.AbstractSQLAssertTest; -import io.shardingsphere.core.common.base.AbstractSQLTest; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import org.junit.BeforeClass; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Collection; -import java.util.List; - -@RunWith(Parameterized.class) -public class ShardingDatabaseAndTableDQLTest extends AbstractShardingDatabaseAndTableTest { - - public ShardingDatabaseAndTableDQLTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - @BeforeClass - public static void cleanAndInitTable() throws Exception { - AbstractSQLAssertTest.importAllDataSet(AbstractShardingDatabaseAndTableTest.getInitFiles()); - } - - @Parameterized.Parameters(name = "{0}In{2}") - public static Collection dataParameters() { - return AbstractSQLTest.dataParameters(SQLType.DQL); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseOnlyDQLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseOnlyDQLTest.java deleted file mode 100644 index a70d13a7f183d..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseOnlyDQLTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding; - -import io.shardingsphere.core.common.base.AbstractSQLAssertTest; -import io.shardingsphere.core.common.base.AbstractSQLTest; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import org.junit.BeforeClass; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Collection; -import java.util.List; - -@RunWith(Parameterized.class) -public class ShardingDatabaseOnlyDQLTest extends AbstractShardingDatabaseOnlyTest { - - public ShardingDatabaseOnlyDQLTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - @BeforeClass - public static void cleanAndInitTable() throws Exception { - AbstractSQLAssertTest.importAllDataSet(AbstractShardingDatabaseOnlyTest.getInitFiles()); - } - - @Parameterized.Parameters(name = "{0}In{2}") - public static Collection dataParameters() { - return AbstractSQLTest.dataParameters(SQLType.DQL); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingMasterSlaveDQLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingMasterSlaveDQLTest.java deleted file mode 100644 index 7c99e1383249c..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingMasterSlaveDQLTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding; - -import io.shardingsphere.core.common.base.AbstractSQLAssertTest; -import io.shardingsphere.core.common.base.AbstractSQLTest; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import org.junit.BeforeClass; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Collection; -import java.util.List; - -@RunWith(Parameterized.class) -public class ShardingMasterSlaveDQLTest extends AbstractShardingMasterSlaveTest { - - public ShardingMasterSlaveDQLTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - @BeforeClass - public static void cleanAndInitTable() throws Exception { - AbstractSQLAssertTest.importAllDataSet(getInitFiles()); - } - - @Parameterized.Parameters(name = "{0}In{2}") - public static Collection dataParameters() { - return AbstractSQLTest.dataParameters(SQLType.DQL); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingTableOnlyDQLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingTableOnlyDQLTest.java deleted file mode 100644 index 4f954c1703894..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingTableOnlyDQLTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding; - -import io.shardingsphere.core.common.base.AbstractSQLAssertTest; -import io.shardingsphere.core.common.base.AbstractSQLTest; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import org.junit.BeforeClass; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Collection; -import java.util.List; - -@RunWith(Parameterized.class) -public class ShardingTableOnlyDQLTest extends AbstractShardingTableOnlyTest { - - public ShardingTableOnlyDQLTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - @BeforeClass - public static void cleanAndInitTable() throws Exception { - AbstractSQLAssertTest.importAllDataSet(getInitFiles()); - } - - @Parameterized.Parameters(name = "{0}In{2}") - public static Collection dataParameters() { - return AbstractSQLTest.dataParameters(SQLType.DQL); - } -} From 842affe01a61c53cb83c6194cd207bf76471b8f8 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 10:49:53 +0800 Subject: [PATCH 327/351] for #601: add case drop_table --- sharding-core/src/test/resources/parser/drop.xml | 2 +- .../io/shardingsphere/dbtest/engine/DDLIntegrateTest.java | 4 ++++ .../asserts/cases/ddl/ddl-integrate-test-cases.xml | 7 +++++++ sharding-jdbc/src/test/resources/integrate/assert/drop.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/drop.xml | 2 +- 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/sharding-core/src/test/resources/parser/drop.xml b/sharding-core/src/test/resources/parser/drop.xml index ee53e9a069b8d..b53130b7aeca8 100644 --- a/sharding-core/src/test/resources/parser/drop.xml +++ b/sharding-core/src/test/resources/parser/drop.xml @@ -1,6 +1,6 @@ - +
diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java index 7640267ced15b..a67cae74280cd 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java @@ -147,6 +147,10 @@ public void assertExecute() throws JAXBException, IOException, SQLException { } private void assertMetadata(final Connection connection) throws IOException, JAXBException, SQLException { + // TODO case for drop, add assert later + if (null == assertion.getExpectedDataFile()) { + return; + } ExpectedMetadataRoot expected; try (FileReader reader = new FileReader(getExpectedDataFile())) { expected = (ExpectedMetadataRoot) JAXBContext.newInstance(ExpectedMetadataRoot.class).createUnmarshaller().unmarshal(reader); diff --git a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml index a5b86b413a54d..ceaee4d3f1e33 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml @@ -68,4 +68,11 @@ + + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml index 02cbba4870d74..c35d396f24ba2 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml @@ -1,6 +1,6 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml index 4da65e7050c51..76157ff3c1997 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml @@ -1,6 +1,6 @@ - + From aec681c08a99a6c7add12d9167b24a671a5a5815 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 10:52:14 +0800 Subject: [PATCH 328/351] for #601: add case drop_table_if_exists --- sharding-core/src/test/resources/parser/drop.xml | 2 +- .../asserts/cases/ddl/ddl-integrate-test-cases.xml | 7 +++++++ sharding-jdbc/src/test/resources/integrate/assert/drop.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/drop.xml | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/sharding-core/src/test/resources/parser/drop.xml b/sharding-core/src/test/resources/parser/drop.xml index b53130b7aeca8..6be24835eb13a 100644 --- a/sharding-core/src/test/resources/parser/drop.xml +++ b/sharding-core/src/test/resources/parser/drop.xml @@ -9,7 +9,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml index ceaee4d3f1e33..67ee1024cd96f 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml @@ -75,4 +75,11 @@ + + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml index c35d396f24ba2..019cc3fa51762 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml @@ -5,7 +5,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml index 76157ff3c1997..09a42a3ddefd8 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml @@ -1,7 +1,7 @@ - + From cdb9e4e80c9741a45cee176f7ad1a24b7fb15a3a Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 10:54:21 +0800 Subject: [PATCH 329/351] for #601: add case drop_temporary_table_if_exists --- sharding-core/src/test/resources/parser/drop.xml | 2 +- .../asserts/cases/ddl/ddl-integrate-test-cases.xml | 8 ++++++++ sharding-sql-test/src/main/resources/sql/ddl/drop.xml | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/sharding-core/src/test/resources/parser/drop.xml b/sharding-core/src/test/resources/parser/drop.xml index 6be24835eb13a..ff8773102b24c 100644 --- a/sharding-core/src/test/resources/parser/drop.xml +++ b/sharding-core/src/test/resources/parser/drop.xml @@ -18,7 +18,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml index 67ee1024cd96f..d7754d63e8769 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml @@ -82,4 +82,12 @@ + + + + + + + + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml index 09a42a3ddefd8..df0521a7448f3 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml @@ -2,7 +2,7 @@ - + From b3329402762629a66897850c7952ecbda2a1d1e8 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 10:55:57 +0800 Subject: [PATCH 330/351] for #601: add case drop_table_restrict --- sharding-core/src/test/resources/parser/drop.xml | 2 +- .../asserts/cases/ddl/ddl-integrate-test-cases.xml | 7 +++++++ sharding-jdbc/src/test/resources/integrate/assert/drop.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/drop.xml | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/sharding-core/src/test/resources/parser/drop.xml b/sharding-core/src/test/resources/parser/drop.xml index ff8773102b24c..18d37f9f6fb20 100644 --- a/sharding-core/src/test/resources/parser/drop.xml +++ b/sharding-core/src/test/resources/parser/drop.xml @@ -27,7 +27,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml index d7754d63e8769..a678ed0da016e 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml @@ -90,4 +90,11 @@ + + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml index 019cc3fa51762..31ab4349b5394 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml @@ -10,7 +10,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml index df0521a7448f3..6ab2f8181f36a 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml @@ -3,7 +3,7 @@ - + From 67025f5e4d0373b5e58a4e4196135d98d9c680c7 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 10:56:57 +0800 Subject: [PATCH 331/351] for #601: add case drop_table_cascade --- sharding-core/src/test/resources/parser/drop.xml | 2 +- .../asserts/cases/ddl/ddl-integrate-test-cases.xml | 7 +++++++ sharding-jdbc/src/test/resources/integrate/assert/drop.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/drop.xml | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/sharding-core/src/test/resources/parser/drop.xml b/sharding-core/src/test/resources/parser/drop.xml index 18d37f9f6fb20..849be5b047acc 100644 --- a/sharding-core/src/test/resources/parser/drop.xml +++ b/sharding-core/src/test/resources/parser/drop.xml @@ -36,7 +36,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml index a678ed0da016e..aff5ca7d13156 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml @@ -97,4 +97,11 @@ + + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml index 31ab4349b5394..b1f43008737cf 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml @@ -15,7 +15,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml index 6ab2f8181f36a..3100dc215ebe1 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml @@ -4,7 +4,7 @@ - + From 1f333fe3cfcd6a5154cd5fc4eb89eaac7506fdef Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 10:58:32 +0800 Subject: [PATCH 332/351] for #601: add case drop_table_cascade_constraints_and_purge --- sharding-core/src/test/resources/parser/drop.xml | 2 +- .../asserts/cases/ddl/ddl-integrate-test-cases.xml | 8 +++++++- .../src/test/resources/integrate/assert/drop.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/drop.xml | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/sharding-core/src/test/resources/parser/drop.xml b/sharding-core/src/test/resources/parser/drop.xml index 849be5b047acc..73fa11463f822 100644 --- a/sharding-core/src/test/resources/parser/drop.xml +++ b/sharding-core/src/test/resources/parser/drop.xml @@ -45,7 +45,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml index aff5ca7d13156..f40e127de3587 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml @@ -103,5 +103,11 @@ - + + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml index b1f43008737cf..a70c1183575f2 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml @@ -20,7 +20,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml index 3100dc215ebe1..210387340f453 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml @@ -5,7 +5,7 @@ - + From 248e9ec688540b1c0265b578af178ebe08b6ecc4 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 11:00:02 +0800 Subject: [PATCH 333/351] for #601: add case drop_index --- sharding-core/src/test/resources/parser/drop.xml | 2 +- .../asserts/cases/ddl/ddl-integrate-test-cases.xml | 10 +++++++++- .../src/test/resources/integrate/assert/drop.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/drop.xml | 2 +- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/sharding-core/src/test/resources/parser/drop.xml b/sharding-core/src/test/resources/parser/drop.xml index 73fa11463f822..c156d3d53ea29 100644 --- a/sharding-core/src/test/resources/parser/drop.xml +++ b/sharding-core/src/test/resources/parser/drop.xml @@ -54,7 +54,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml index f40e127de3587..caeb2b432d5d6 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml @@ -103,11 +103,19 @@ - + + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml index a70c1183575f2..756b28b19d930 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml @@ -25,7 +25,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml index 210387340f453..cd6243c21b952 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml @@ -6,7 +6,7 @@ - + From 62f6582df0f327ec35e20f3a14a44eae07a1aa5d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 11:01:08 +0800 Subject: [PATCH 334/351] for #601: add case drop_index_without_on --- sharding-core/src/test/resources/parser/drop.xml | 2 +- .../asserts/cases/ddl/ddl-integrate-test-cases.xml | 7 +++++++ sharding-jdbc/src/test/resources/integrate/assert/drop.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/drop.xml | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/sharding-core/src/test/resources/parser/drop.xml b/sharding-core/src/test/resources/parser/drop.xml index c156d3d53ea29..4fdd4c7a13c5b 100644 --- a/sharding-core/src/test/resources/parser/drop.xml +++ b/sharding-core/src/test/resources/parser/drop.xml @@ -64,7 +64,7 @@ - + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml index caeb2b432d5d6..ccf31bc69643f 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml @@ -118,4 +118,11 @@ + + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml index 756b28b19d930..7195926c7f70c 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml @@ -30,7 +30,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml index cd6243c21b952..cb2e670f7b08e 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml @@ -7,6 +7,6 @@ - + From 206f509d3d6f80a6c5b7ff2320e39bfea7d6bad0 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 11:05:22 +0800 Subject: [PATCH 335/351] for #601: add case drop_index_if_exists --- sharding-core/src/test/resources/parser/drop.xml | 2 +- .../asserts/cases/ddl/ddl-integrate-test-cases.xml | 6 ++++++ sharding-jdbc/src/test/resources/integrate/assert/drop.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/drop.xml | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/sharding-core/src/test/resources/parser/drop.xml b/sharding-core/src/test/resources/parser/drop.xml index 4fdd4c7a13c5b..00c930f8d1bc3 100644 --- a/sharding-core/src/test/resources/parser/drop.xml +++ b/sharding-core/src/test/resources/parser/drop.xml @@ -70,7 +70,7 @@ - + diff --git a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml index ccf31bc69643f..9e76441472cc2 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml @@ -125,4 +125,10 @@ + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml index 7195926c7f70c..0df393ea9d691 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml @@ -35,7 +35,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml index cb2e670f7b08e..43de808bc0bd8 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/drop.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/drop.xml @@ -8,5 +8,5 @@ - + From 16b72cb0b455115045658a38181d9e280aa51d1f Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 11:07:54 +0800 Subject: [PATCH 336/351] for #601: add case truncate_table --- sharding-core/src/test/resources/parser/truncate.xml | 2 +- .../io/shardingsphere/dbtest/engine/DDLIntegrateTest.java | 2 +- .../asserts/cases/ddl/ddl-integrate-test-cases.xml | 8 ++++++++ .../src/test/resources/integrate/assert/truncate.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/truncate.xml | 2 +- 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/sharding-core/src/test/resources/parser/truncate.xml b/sharding-core/src/test/resources/parser/truncate.xml index 44e95c3714ff6..17b3211edf2c1 100644 --- a/sharding-core/src/test/resources/parser/truncate.xml +++ b/sharding-core/src/test/resources/parser/truncate.xml @@ -1,6 +1,6 @@ - +
diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java index a67cae74280cd..01d607beb0521 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DDLIntegrateTest.java @@ -147,7 +147,7 @@ public void assertExecute() throws JAXBException, IOException, SQLException { } private void assertMetadata(final Connection connection) throws IOException, JAXBException, SQLException { - // TODO case for drop, add assert later + // TODO case for drop (table, index) and truncate, add assert later if (null == assertion.getExpectedDataFile()) { return; } diff --git a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml index 9e76441472cc2..46a9f5c91845c 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml @@ -131,4 +131,12 @@ + + + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/truncate.xml b/sharding-jdbc/src/test/resources/integrate/assert/truncate.xml index 8f6f5930e8aac..46871df6473df 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/truncate.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/truncate.xml @@ -1,6 +1,6 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/truncate.xml b/sharding-sql-test/src/main/resources/sql/ddl/truncate.xml index 8997647a74adc..d33912fff807f 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/truncate.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/truncate.xml @@ -1,5 +1,5 @@ - + From 4d9612731057be9806ee3f20c7bc4cf4d570a0b7 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 11:12:08 +0800 Subject: [PATCH 337/351] for #601: add case truncate_table_only --- sharding-core/src/test/resources/parser/truncate.xml | 2 +- .../asserts/cases/ddl/ddl-integrate-test-cases.xml | 6 ++++++ .../src/test/resources/integrate/assert/truncate.xml | 2 +- sharding-sql-test/src/main/resources/sql/ddl/truncate.xml | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/sharding-core/src/test/resources/parser/truncate.xml b/sharding-core/src/test/resources/parser/truncate.xml index 17b3211edf2c1..2cf024694f2e2 100644 --- a/sharding-core/src/test/resources/parser/truncate.xml +++ b/sharding-core/src/test/resources/parser/truncate.xml @@ -9,7 +9,7 @@ - +
diff --git a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml index 46a9f5c91845c..0df813206a607 100644 --- a/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml +++ b/sharding-jdbc/src/test/resources/asserts/cases/ddl/ddl-integrate-test-cases.xml @@ -139,4 +139,10 @@ + + + + + + diff --git a/sharding-jdbc/src/test/resources/integrate/assert/truncate.xml b/sharding-jdbc/src/test/resources/integrate/assert/truncate.xml index 46871df6473df..fb48ed4320ce1 100644 --- a/sharding-jdbc/src/test/resources/integrate/assert/truncate.xml +++ b/sharding-jdbc/src/test/resources/integrate/assert/truncate.xml @@ -5,7 +5,7 @@ - + diff --git a/sharding-sql-test/src/main/resources/sql/ddl/truncate.xml b/sharding-sql-test/src/main/resources/sql/ddl/truncate.xml index d33912fff807f..e1aad054902de 100644 --- a/sharding-sql-test/src/main/resources/sql/ddl/truncate.xml +++ b/sharding-sql-test/src/main/resources/sql/ddl/truncate.xml @@ -1,5 +1,5 @@ - + From 10c529ee1e200d36c309d34ab8f9ae4b73368657 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 11:16:26 +0800 Subject: [PATCH 338/351] for #601: remove useless original cases --- .../core/integrate/AllIntegrateTests.java | 10 -- .../type/ms/AbstractMasterSlaveOnlyTest.java | 111 ------------- .../type/ms/MasterSlaveOnlyDMLAndDDLTest.java | 47 ------ .../AbstractShardingDatabaseAndTableTest.java | 117 ------------- .../AbstractShardingDatabaseOnlyTest.java | 100 ----------- .../AbstractShardingMasterSlaveTest.java | 155 ------------------ .../AbstractShardingTableOnlyTest.java | 102 ------------ ...ShardingDatabaseAndTableDMLAndDDLTest.java | 47 ------ .../ShardingDatabaseOnlyDMLAndDDLTest.java | 47 ------ .../ShardingMasterSlaveDMLAndDDLTest.java | 47 ------ .../ShardingTableOnlyDMLAndDDLTest.java | 47 ------ .../test/resources/integrate/assert/drop.xml | 43 ----- .../resources/integrate/assert/truncate.xml | 13 -- 13 files changed, 886 deletions(-) delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/ms/AbstractMasterSlaveOnlyTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/ms/MasterSlaveOnlyDMLAndDDLTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingDatabaseAndTableTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingDatabaseOnlyTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingMasterSlaveTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingTableOnlyTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseAndTableDMLAndDDLTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseOnlyDMLAndDDLTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingMasterSlaveDMLAndDDLTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingTableOnlyDMLAndDDLTest.java delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/drop.xml delete mode 100644 sharding-jdbc/src/test/resources/integrate/assert/truncate.xml diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java index 56888eae95846..f9adfd72fb2b8 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java @@ -18,11 +18,6 @@ package io.shardingsphere.core.integrate; import io.shardingsphere.core.integrate.api.AllAPIIntegrateTests; -import io.shardingsphere.core.integrate.type.ms.MasterSlaveOnlyDMLAndDDLTest; -import io.shardingsphere.core.integrate.type.sharding.ShardingDatabaseAndTableDMLAndDDLTest; -import io.shardingsphere.core.integrate.type.sharding.ShardingDatabaseOnlyDMLAndDDLTest; -import io.shardingsphere.core.integrate.type.sharding.ShardingMasterSlaveDMLAndDDLTest; -import io.shardingsphere.core.integrate.type.sharding.ShardingTableOnlyDMLAndDDLTest; import io.shardingsphere.core.integrate.type.sharding.hint.AllHintDatabaseOnlyTests; import org.junit.runner.RunWith; import org.junit.runners.Suite; @@ -30,11 +25,6 @@ @RunWith(Suite.class) @SuiteClasses({ - ShardingDatabaseOnlyDMLAndDDLTest.class, - ShardingTableOnlyDMLAndDDLTest.class, - ShardingMasterSlaveDMLAndDDLTest.class, - MasterSlaveOnlyDMLAndDDLTest.class, - ShardingDatabaseAndTableDMLAndDDLTest.class, AllHintDatabaseOnlyTests.class, AllAPIIntegrateTests.class }) diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/ms/AbstractMasterSlaveOnlyTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/ms/AbstractMasterSlaveOnlyTest.java deleted file mode 100644 index 12a976ed78ea9..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/ms/AbstractMasterSlaveOnlyTest.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.ms; - -import io.shardingsphere.core.api.MasterSlaveDataSourceFactory; -import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration; -import io.shardingsphere.core.common.base.AbstractSQLAssertTest; -import io.shardingsphere.core.common.env.ShardingTestStrategy; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.hint.HintManagerHolder; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import io.shardingsphere.core.jdbc.core.datasource.MasterSlaveDataSource; -import io.shardingsphere.core.routing.router.masterslave.MasterVisitedManager; -import org.apache.commons.dbcp2.BasicDataSource; -import org.junit.After; -import org.junit.AfterClass; - -import javax.sql.DataSource; -import java.io.File; -import java.net.URL; -import java.sql.SQLException; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -public abstract class AbstractMasterSlaveOnlyTest extends AbstractSQLAssertTest { - - private static Map masterSlaveDataSources = new HashMap<>(); - - public AbstractMasterSlaveOnlyTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - protected static List getInitFiles() { - return Arrays.asList("integrate/dataset/masterslave/init/master_only.xml", "integrate/dataset/masterslave/init/slave_only.xml"); - } - - @Override - protected File getExpectedFile(final String expected) { - String expectedFile = null == expected ? "integrate/dataset/EmptyTable.xml" - : String.format("integrate/dataset/masterslave/expect/" + expected, getShardingStrategy()); - URL url = AbstractSQLAssertTest.class.getClassLoader().getResource(expectedFile); - if (null == url) { - throw new RuntimeException("Wrong expected file:" + expectedFile); - } - return new File(url.getPath()); - } - - @Override - protected ShardingTestStrategy getShardingStrategy() { - return ShardingTestStrategy.masterslaveonly; - } - - @Override - protected List getInitDataSetFiles() { - return AbstractMasterSlaveOnlyTest.getInitFiles(); - } - - @Override - protected final Map getDataSources() throws SQLException { - if (!masterSlaveDataSources.isEmpty()) { - return masterSlaveDataSources; - } - Map> dataSourceMap = createDataSourceMap(); - for (Entry> each : dataSourceMap.entrySet()) { - masterSlaveDataSources.put(each.getKey(), getMasterSlaveDataSource(each.getValue())); - } - return masterSlaveDataSources; - } - - private MasterSlaveDataSource getMasterSlaveDataSource(final Map masterSlaveDataSourceMap) throws SQLException { - return (MasterSlaveDataSource) MasterSlaveDataSourceFactory.createDataSource(masterSlaveDataSourceMap, - new MasterSlaveRuleConfiguration("ds_ms", "dataSource_master_only", Collections.singletonList("dataSource_slave_only")), Collections.emptyMap()); - } - - @After - public final void clearFlag() { - HintManagerHolder.clear(); - MasterVisitedManager.clear(); - } - - @AfterClass - public static void clear() throws SQLException { - if (!masterSlaveDataSources.isEmpty()) { - for (MasterSlaveDataSource each : masterSlaveDataSources.values()) { - for (DataSource innerEach : each.getAllDataSources().values()) { - ((BasicDataSource) innerEach).close(); - } - } - masterSlaveDataSources.clear(); - } - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/ms/MasterSlaveOnlyDMLAndDDLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/ms/MasterSlaveOnlyDMLAndDDLTest.java deleted file mode 100644 index 792e062dea910..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/ms/MasterSlaveOnlyDMLAndDDLTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.ms; - -import io.shardingsphere.core.common.base.AbstractSQLTest; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import org.junit.Before; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Collection; -import java.util.List; - -@RunWith(Parameterized.class) -public class MasterSlaveOnlyDMLAndDDLTest extends AbstractMasterSlaveOnlyTest { - - public MasterSlaveOnlyDMLAndDDLTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - @Parameterized.Parameters(name = "{0}In{2}") - public static Collection dataParameters() { - return AbstractSQLTest.dataParameters(SQLType.DML, SQLType.DDL); - } - - @Before - public void cleanAndInitTable() throws Exception { - importDataSet(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingDatabaseAndTableTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingDatabaseAndTableTest.java deleted file mode 100644 index df63e681e260c..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingDatabaseAndTableTest.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding; - -import com.google.common.base.Joiner; -import io.shardingsphere.core.api.config.ShardingRuleConfiguration; -import io.shardingsphere.core.api.config.TableRuleConfiguration; -import io.shardingsphere.core.api.config.strategy.StandardShardingStrategyConfiguration; -import io.shardingsphere.core.common.base.AbstractSQLAssertTest; -import io.shardingsphere.core.common.env.ShardingTestStrategy; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.integrate.fixture.PreciseModuloDatabaseShardingAlgorithm; -import io.shardingsphere.core.integrate.fixture.PreciseModuloTableShardingAlgorithm; -import io.shardingsphere.core.integrate.fixture.RangeModuloDatabaseShardingAlgorithm; -import io.shardingsphere.core.integrate.fixture.RangeModuloTableShardingAlgorithm; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; -import io.shardingsphere.core.rule.ShardingRule; - -import javax.sql.DataSource; -import java.sql.SQLException; -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -public abstract class AbstractShardingDatabaseAndTableTest extends AbstractSQLAssertTest { - - public AbstractShardingDatabaseAndTableTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - protected static List getInitFiles() { - return Arrays.asList( - "integrate/dataset/sharding/dbtbl/init/dbtbl_0.xml", - "integrate/dataset/sharding/dbtbl/init/dbtbl_1.xml", - "integrate/dataset/sharding/dbtbl/init/dbtbl_2.xml", - "integrate/dataset/sharding/dbtbl/init/dbtbl_3.xml", - "integrate/dataset/sharding/dbtbl/init/dbtbl_4.xml", - "integrate/dataset/sharding/dbtbl/init/dbtbl_5.xml", - "integrate/dataset/sharding/dbtbl/init/dbtbl_6.xml", - "integrate/dataset/sharding/dbtbl/init/dbtbl_7.xml", - "integrate/dataset/sharding/dbtbl/init/dbtbl_8.xml", - "integrate/dataset/sharding/dbtbl/init/dbtbl_9.xml"); - } - - @Override - protected ShardingTestStrategy getShardingStrategy() { - return ShardingTestStrategy.dbtbl; - } - - @Override - protected List getInitDataSetFiles() { - return AbstractShardingDatabaseAndTableTest.getInitFiles(); - } - - @Override - protected final Map getDataSources() throws SQLException { - if (!getShardingDataSources().isEmpty()) { - return getShardingDataSources(); - } - Map> dataSourceMap = createDataSourceMap(); - for (Map.Entry> entry : dataSourceMap.entrySet()) { - ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); - shardingRuleConfig.setDefaultDataSourceName("dataSource_dbtbl_0"); - TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration(); - orderTableRuleConfig.setLogicTable("t_order"); - orderTableRuleConfig.setLogicIndex("t_order_index"); - List orderActualDataNodes = new LinkedList<>(); - for (String dataSourceName : entry.getValue().keySet()) { - orderActualDataNodes.add(dataSourceName + ".t_order_${0..9}"); - } - orderTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderActualDataNodes)); - shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig); - TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration(); - orderItemTableRuleConfig.setLogicTable("t_order_item"); - List orderItemActualDataNodes = new LinkedList<>(); - for (String dataSourceName : entry.getValue().keySet()) { - orderItemActualDataNodes.add(dataSourceName + ".t_order_item_${0..9}"); - } - orderItemTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderItemActualDataNodes)); - shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig); - TableRuleConfiguration configTableRuleConfig = new TableRuleConfiguration(); - configTableRuleConfig.setLogicTable("t_config"); - TableRuleConfiguration logTableRuleConfig = new TableRuleConfiguration(); - logTableRuleConfig.setLogicIndex("t_log_index"); - logTableRuleConfig.setLogicTable("t_log"); - TableRuleConfiguration tempLogTableRuleConfig = new TableRuleConfiguration(); - tempLogTableRuleConfig.setLogicTable("t_temp_log"); - shardingRuleConfig.getTableRuleConfigs().add(logTableRuleConfig); - shardingRuleConfig.getTableRuleConfigs().add(tempLogTableRuleConfig); - shardingRuleConfig.getTableRuleConfigs().add(configTableRuleConfig); - shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item"); - shardingRuleConfig.setDefaultTableShardingStrategyConfig( - new StandardShardingStrategyConfiguration("order_id", new PreciseModuloTableShardingAlgorithm(), new RangeModuloTableShardingAlgorithm())); - shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig( - new StandardShardingStrategyConfiguration("user_id", new PreciseModuloDatabaseShardingAlgorithm(), new RangeModuloDatabaseShardingAlgorithm())); - getShardingDataSources().put(entry.getKey(), new ShardingDataSource(entry.getValue(), new ShardingRule(shardingRuleConfig, entry.getValue().keySet()))); - } - return getShardingDataSources(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingDatabaseOnlyTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingDatabaseOnlyTest.java deleted file mode 100644 index d5fa8d69d5249..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingDatabaseOnlyTest.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding; - -import io.shardingsphere.core.api.config.ShardingRuleConfiguration; -import io.shardingsphere.core.api.config.TableRuleConfiguration; -import io.shardingsphere.core.api.config.strategy.ComplexShardingStrategyConfiguration; -import io.shardingsphere.core.api.config.strategy.NoneShardingStrategyConfiguration; -import io.shardingsphere.core.common.base.AbstractSQLAssertTest; -import io.shardingsphere.core.common.env.ShardingTestStrategy; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.fixture.IncrementKeyGenerator; -import io.shardingsphere.core.integrate.fixture.ComplexKeysModuloDatabaseShardingAlgorithm; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; -import io.shardingsphere.core.rule.ShardingRule; - -import javax.sql.DataSource; -import java.sql.SQLException; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -public abstract class AbstractShardingDatabaseOnlyTest extends AbstractSQLAssertTest { - - public AbstractShardingDatabaseOnlyTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - protected static List getInitFiles() { - return Arrays.asList( - "integrate/dataset/sharding/db/init/db_0.xml", - "integrate/dataset/sharding/db/init/db_1.xml", - "integrate/dataset/sharding/db/init/db_2.xml", - "integrate/dataset/sharding/db/init/db_3.xml", - "integrate/dataset/sharding/db/init/db_4.xml", - "integrate/dataset/sharding/db/init/db_5.xml", - "integrate/dataset/sharding/db/init/db_6.xml", - "integrate/dataset/sharding/db/init/db_7.xml", - "integrate/dataset/sharding/db/init/db_8.xml", - "integrate/dataset/sharding/db/init/db_9.xml"); - } - - @Override - protected ShardingTestStrategy getShardingStrategy() { - return ShardingTestStrategy.db; - } - - @Override - protected List getInitDataSetFiles() { - return AbstractShardingDatabaseOnlyTest.getInitFiles(); - } - - @Override - protected Map getDataSources() throws SQLException { - if (!getShardingDataSources().isEmpty()) { - return getShardingDataSources(); - } - Map> dataSourceMap = createDataSourceMap(); - for (Map.Entry> each : dataSourceMap.entrySet()) { - final ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); - TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration(); - orderTableRuleConfig.setLogicTable("t_order"); - orderTableRuleConfig.setLogicIndex("t_order_index"); - orderTableRuleConfig.setKeyGeneratorColumnName("order_id"); - orderTableRuleConfig.setKeyGenerator(new IncrementKeyGenerator()); - shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig); - TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration(); - orderItemTableRuleConfig.setLogicTable("t_order_item"); - TableRuleConfiguration logTableRuleConfig = new TableRuleConfiguration(); - logTableRuleConfig.setLogicIndex("t_log_index"); - logTableRuleConfig.setLogicTable("t_log"); - TableRuleConfiguration tempLogTableRuleConfig = new TableRuleConfiguration(); - tempLogTableRuleConfig.setLogicTable("t_temp_log"); - shardingRuleConfig.getTableRuleConfigs().add(logTableRuleConfig); - shardingRuleConfig.getTableRuleConfigs().add(tempLogTableRuleConfig); - shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig); - shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item"); - shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new ComplexShardingStrategyConfiguration("user_id", new ComplexKeysModuloDatabaseShardingAlgorithm())); - shardingRuleConfig.setDefaultTableShardingStrategyConfig(new NoneShardingStrategyConfiguration()); - getShardingDataSources().put(each.getKey(), new ShardingDataSource(each.getValue(), new ShardingRule(shardingRuleConfig, each.getValue().keySet()))); - } - return getShardingDataSources(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingMasterSlaveTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingMasterSlaveTest.java deleted file mode 100644 index f31c10e700817..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingMasterSlaveTest.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding; - -import com.google.common.base.Joiner; -import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration; -import io.shardingsphere.core.api.config.ShardingRuleConfiguration; -import io.shardingsphere.core.api.config.TableRuleConfiguration; -import io.shardingsphere.core.api.config.strategy.StandardShardingStrategyConfiguration; -import io.shardingsphere.core.common.base.AbstractSQLAssertTest; -import io.shardingsphere.core.common.env.ShardingTestStrategy; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.hint.HintManagerHolder; -import io.shardingsphere.core.integrate.fixture.PreciseModuloDatabaseShardingAlgorithm; -import io.shardingsphere.core.integrate.fixture.RangeModuloDatabaseShardingAlgorithm; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; -import io.shardingsphere.core.routing.router.masterslave.MasterVisitedManager; -import io.shardingsphere.core.rule.ShardingRule; -import org.junit.After; - -import javax.sql.DataSource; -import java.sql.SQLException; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -public abstract class AbstractShardingMasterSlaveTest extends AbstractSQLAssertTest { - - public AbstractShardingMasterSlaveTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - protected static List getInitFiles() { - return Arrays.asList( - "integrate/dataset/sharding/masterslave/init/master_0.xml", - "integrate/dataset/sharding/masterslave/init/master_1.xml", - "integrate/dataset/sharding/masterslave/init/master_2.xml", - "integrate/dataset/sharding/masterslave/init/master_3.xml", - "integrate/dataset/sharding/masterslave/init/master_4.xml", - "integrate/dataset/sharding/masterslave/init/master_5.xml", - "integrate/dataset/sharding/masterslave/init/master_6.xml", - "integrate/dataset/sharding/masterslave/init/master_7.xml", - "integrate/dataset/sharding/masterslave/init/master_8.xml", - "integrate/dataset/sharding/masterslave/init/master_9.xml", - "integrate/dataset/sharding/masterslave/init/slave_0.xml", - "integrate/dataset/sharding/masterslave/init/slave_1.xml", - "integrate/dataset/sharding/masterslave/init/slave_2.xml", - "integrate/dataset/sharding/masterslave/init/slave_3.xml", - "integrate/dataset/sharding/masterslave/init/slave_4.xml", - "integrate/dataset/sharding/masterslave/init/slave_5.xml", - "integrate/dataset/sharding/masterslave/init/slave_6.xml", - "integrate/dataset/sharding/masterslave/init/slave_7.xml", - "integrate/dataset/sharding/masterslave/init/slave_8.xml", - "integrate/dataset/sharding/masterslave/init/slave_9.xml"); - } - - @Override - protected ShardingTestStrategy getShardingStrategy() { - return ShardingTestStrategy.masterslave; - } - - @Override - protected List getInitDataSetFiles() { - return AbstractShardingMasterSlaveTest.getInitFiles(); - } - - @Override - protected final Map getDataSources() throws SQLException { - if (!getShardingDataSources().isEmpty()) { - return getShardingDataSources(); - } - Map> dataSourceMap = createDataSourceMap(); - for (Entry> entry : dataSourceMap.entrySet()) { - final ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); - TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration(); - orderTableRuleConfig.setLogicTable("t_order"); - orderTableRuleConfig.setLogicIndex("t_order_index"); - List orderActualDataNodes = new LinkedList<>(); - Collection masterSlaveRuleConfigs = getMasterSlaveRuleConfigurations(); - for (MasterSlaveRuleConfiguration each : masterSlaveRuleConfigs) { - orderActualDataNodes.add(each.getName() + ".t_order_${0..9}"); - } - orderTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderActualDataNodes)); - shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig); - TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration(); - orderItemTableRuleConfig.setLogicTable("t_order_item"); - List itemOrderActualDataNodes = new LinkedList<>(); - for (MasterSlaveRuleConfiguration each : masterSlaveRuleConfigs) { - itemOrderActualDataNodes.add(each.getName() + ".t_order_item_${0..9}"); - } - orderItemTableRuleConfig.setActualDataNodes(Joiner.on(",").join(itemOrderActualDataNodes)); - shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig); - TableRuleConfiguration configTableRuleConfig = new TableRuleConfiguration(); - configTableRuleConfig.setLogicTable("t_config"); - TableRuleConfiguration logTableRuleConfig = new TableRuleConfiguration(); - logTableRuleConfig.setLogicIndex("t_log_index"); - logTableRuleConfig.setLogicTable("t_log"); - TableRuleConfiguration tempLogTableRuleConfig = new TableRuleConfiguration(); - tempLogTableRuleConfig.setLogicTable("t_temp_log"); - shardingRuleConfig.getTableRuleConfigs().add(logTableRuleConfig); - shardingRuleConfig.getTableRuleConfigs().add(tempLogTableRuleConfig); - shardingRuleConfig.getTableRuleConfigs().add(configTableRuleConfig); - shardingRuleConfig.setDefaultTableShardingStrategyConfig( - new StandardShardingStrategyConfiguration("t_order_item", new PreciseModuloDatabaseShardingAlgorithm(), new RangeModuloDatabaseShardingAlgorithm())); - shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig( - new StandardShardingStrategyConfiguration("user_id", new PreciseModuloDatabaseShardingAlgorithm(), new RangeModuloDatabaseShardingAlgorithm())); - shardingRuleConfig.getMasterSlaveRuleConfigs().addAll(masterSlaveRuleConfigs); - Map masterSlaveDataSourceMap = entry.getValue(); - ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, masterSlaveDataSourceMap.keySet()); - getShardingDataSources().put(entry.getKey(), new ShardingDataSource(masterSlaveDataSourceMap, shardingRule)); - } - return getShardingDataSources(); - } - - private Collection getMasterSlaveRuleConfigurations() { - Collection result = new LinkedList<>(); - result.add(new MasterSlaveRuleConfiguration("ms_0", "dataSource_master_0", Collections.singletonList("dataSource_slave_0"))); - result.add(new MasterSlaveRuleConfiguration("ms_1", "dataSource_master_1", Collections.singletonList("dataSource_slave_1"))); - result.add(new MasterSlaveRuleConfiguration("ms_2", "dataSource_master_2", Collections.singletonList("dataSource_slave_2"))); - result.add(new MasterSlaveRuleConfiguration("ms_3", "dataSource_master_3", Collections.singletonList("dataSource_slave_3"))); - result.add(new MasterSlaveRuleConfiguration("ms_4", "dataSource_master_4", Collections.singletonList("dataSource_slave_4"))); - result.add(new MasterSlaveRuleConfiguration("ms_5", "dataSource_master_5", Collections.singletonList("dataSource_slave_5"))); - result.add(new MasterSlaveRuleConfiguration("ms_6", "dataSource_master_6", Collections.singletonList("dataSource_slave_6"))); - result.add(new MasterSlaveRuleConfiguration("ms_7", "dataSource_master_7", Collections.singletonList("dataSource_slave_7"))); - result.add(new MasterSlaveRuleConfiguration("ms_8", "dataSource_master_8", Collections.singletonList("dataSource_slave_8"))); - result.add(new MasterSlaveRuleConfiguration("ms_9", "dataSource_master_9", Collections.singletonList("dataSource_slave_9"))); - return result; - } - - @After - public final void clearFlag() { - HintManagerHolder.clear(); - MasterVisitedManager.clear(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingTableOnlyTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingTableOnlyTest.java deleted file mode 100644 index 3ee45cef8b400..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/AbstractShardingTableOnlyTest.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding; - -import com.google.common.base.Joiner; -import io.shardingsphere.core.api.config.ShardingRuleConfiguration; -import io.shardingsphere.core.api.config.TableRuleConfiguration; -import io.shardingsphere.core.api.config.strategy.NoneShardingStrategyConfiguration; -import io.shardingsphere.core.api.config.strategy.StandardShardingStrategyConfiguration; -import io.shardingsphere.core.common.base.AbstractSQLAssertTest; -import io.shardingsphere.core.common.env.ShardingTestStrategy; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.integrate.fixture.PreciseModuloTableShardingAlgorithm; -import io.shardingsphere.core.integrate.fixture.RangeModuloTableShardingAlgorithm; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; -import io.shardingsphere.core.rule.ShardingRule; - -import javax.sql.DataSource; -import java.sql.SQLException; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -public abstract class AbstractShardingTableOnlyTest extends AbstractSQLAssertTest { - - public AbstractShardingTableOnlyTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - protected static List getInitFiles() { - return Collections.singletonList("integrate/dataset/sharding/tbl/init/tbl.xml"); - } - - @Override - protected ShardingTestStrategy getShardingStrategy() { - return ShardingTestStrategy.tbl; - } - - @Override - protected List getInitDataSetFiles() { - return AbstractShardingTableOnlyTest.getInitFiles(); - } - - @Override - protected final Map getDataSources() throws SQLException { - if (!getShardingDataSources().isEmpty()) { - return getShardingDataSources(); - } - Map> dataSourceMap = createDataSourceMap(); - for (Map.Entry> entry : dataSourceMap.entrySet()) { - final ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); - TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration(); - orderTableRuleConfig.setLogicTable("t_order"); - orderTableRuleConfig.setLogicIndex("t_order_index"); - List orderActualDataNodes = new LinkedList<>(); - for (String dataSourceName : entry.getValue().keySet()) { - orderActualDataNodes.add(dataSourceName + ".t_order_${0..9}"); - } - orderTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderActualDataNodes)); - shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig); - TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration(); - List orderItemActualDataNodes = new LinkedList<>(); - for (String dataSourceName : entry.getValue().keySet()) { - orderItemActualDataNodes.add(dataSourceName + ".t_order_item_${0..9}"); - } - orderItemTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderItemActualDataNodes)); - orderItemTableRuleConfig.setLogicTable("t_order_item"); - orderItemTableRuleConfig.setKeyGeneratorColumnName("item_id"); - shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig); - TableRuleConfiguration logTableRuleConfig = new TableRuleConfiguration(); - logTableRuleConfig.setLogicIndex("t_log_index"); - logTableRuleConfig.setLogicTable("t_log"); - TableRuleConfiguration tempLogTableRuleConfig = new TableRuleConfiguration(); - tempLogTableRuleConfig.setLogicTable("t_temp_log"); - shardingRuleConfig.getTableRuleConfigs().add(logTableRuleConfig); - shardingRuleConfig.getTableRuleConfigs().add(tempLogTableRuleConfig); - shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item"); - shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new NoneShardingStrategyConfiguration()); - shardingRuleConfig.setDefaultTableShardingStrategyConfig( - new StandardShardingStrategyConfiguration("order_id", new PreciseModuloTableShardingAlgorithm(), new RangeModuloTableShardingAlgorithm())); - getShardingDataSources().put(entry.getKey(), new ShardingDataSource(entry.getValue(), new ShardingRule(shardingRuleConfig, entry.getValue().keySet()))); - } - return getShardingDataSources(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseAndTableDMLAndDDLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseAndTableDMLAndDDLTest.java deleted file mode 100644 index b18c1d6c88aa6..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseAndTableDMLAndDDLTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding; - -import io.shardingsphere.core.common.base.AbstractSQLTest; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import org.junit.Before; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Collection; -import java.util.List; - -@RunWith(Parameterized.class) -public class ShardingDatabaseAndTableDMLAndDDLTest extends AbstractShardingDatabaseAndTableTest { - - public ShardingDatabaseAndTableDMLAndDDLTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - @Parameterized.Parameters(name = "{0}In{2}") - public static Collection dataParameters() { - return AbstractSQLTest.dataParameters(SQLType.DML, SQLType.DDL); - } - - @Before - public void cleanAndInitTable() throws Exception { - importDataSet(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseOnlyDMLAndDDLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseOnlyDMLAndDDLTest.java deleted file mode 100644 index 188c2f10043de..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingDatabaseOnlyDMLAndDDLTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding; - -import io.shardingsphere.core.common.base.AbstractSQLTest; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import org.junit.Before; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Collection; -import java.util.List; - -@RunWith(Parameterized.class) -public class ShardingDatabaseOnlyDMLAndDDLTest extends AbstractShardingDatabaseOnlyTest { - - public ShardingDatabaseOnlyDMLAndDDLTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - @Parameterized.Parameters(name = "{0}In{2}") - public static Collection dataParameters() { - return AbstractSQLTest.dataParameters(SQLType.DML, SQLType.DDL); - } - - @Before - public void cleanAndInitTable() throws Exception { - importDataSet(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingMasterSlaveDMLAndDDLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingMasterSlaveDMLAndDDLTest.java deleted file mode 100644 index b62e6924aceee..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingMasterSlaveDMLAndDDLTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding; - -import io.shardingsphere.core.common.base.AbstractSQLTest; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import org.junit.Before; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Collection; -import java.util.List; - -@RunWith(Parameterized.class) -public class ShardingMasterSlaveDMLAndDDLTest extends AbstractShardingMasterSlaveTest { - - public ShardingMasterSlaveDMLAndDDLTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - @Parameterized.Parameters(name = "{0}In{2}") - public static Collection dataParameters() { - return AbstractSQLTest.dataParameters(SQLType.DML, SQLType.DDL); - } - - @Before - public void cleanAndInitTable() throws Exception { - importDataSet(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingTableOnlyDMLAndDDLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingTableOnlyDMLAndDDLTest.java deleted file mode 100644 index 54a8f351a1076..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/ShardingTableOnlyDMLAndDDLTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding; - -import io.shardingsphere.core.common.base.AbstractSQLTest; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import org.junit.Before; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Collection; -import java.util.List; - -@RunWith(Parameterized.class) -public class ShardingTableOnlyDMLAndDDLTest extends AbstractShardingTableOnlyTest { - - public ShardingTableOnlyDMLAndDDLTest(final String testCaseName, final String sql, final DatabaseType type, final List sqlShardingRules) { - super(testCaseName, sql, type, sqlShardingRules); - } - - @Parameterized.Parameters(name = "{0}In{2}") - public static Collection dataParameters() { - return AbstractSQLTest.dataParameters(SQLType.DML, SQLType.DDL); - } - - @Before - public void cleanAndInitTable() throws Exception { - importDataSet(); - } -} diff --git a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml b/sharding-jdbc/src/test/resources/integrate/assert/drop.xml deleted file mode 100644 index 0df393ea9d691..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/drop.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-jdbc/src/test/resources/integrate/assert/truncate.xml b/sharding-jdbc/src/test/resources/integrate/assert/truncate.xml deleted file mode 100644 index fb48ed4320ce1..0000000000000 --- a/sharding-jdbc/src/test/resources/integrate/assert/truncate.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - From e9440c70dde6ec7ce08a2372b1d81a258e48efb6 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 11:27:21 +0800 Subject: [PATCH 339/351] for #601: remove useless original cases --- .../common/base/AbstractSQLAssertTest.java | 277 ------------------ .../core/common/base/AbstractSQLTest.java | 11 - .../core/common/env/ShardingTestStrategy.java | 23 -- .../core/integrate/jaxb/SQLAssert.java | 39 --- .../core/integrate/jaxb/SQLAssertData.java | 37 --- .../core/integrate/jaxb/SQLAsserts.java | 33 --- .../core/integrate/jaxb/SQLParameters.java | 36 --- .../core/integrate/jaxb/SQLShardingRule.java | 39 --- .../jaxb/helper/SQLAssertJAXBHelper.java | 131 --------- 9 files changed, 626 deletions(-) delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractSQLAssertTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/common/env/ShardingTestStrategy.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLAssert.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLAssertData.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLAsserts.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLParameters.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLShardingRule.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/helper/SQLAssertJAXBHelper.java diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractSQLAssertTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractSQLAssertTest.java deleted file mode 100644 index b14556042f94d..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractSQLAssertTest.java +++ /dev/null @@ -1,277 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.common.base; - -import com.google.common.base.Strings; -import com.google.common.collect.Lists; -import io.shardingsphere.core.common.env.DatabaseEnvironment; -import io.shardingsphere.core.common.env.ShardingJdbcDatabaseTester; -import io.shardingsphere.core.common.env.ShardingTestStrategy; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.integrate.jaxb.SQLAssertData; -import io.shardingsphere.core.integrate.jaxb.SQLShardingRule; -import io.shardingsphere.core.jdbc.adapter.AbstractDataSourceAdapter; -import io.shardingsphere.core.jdbc.core.datasource.MasterSlaveDataSource; -import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; -import io.shardingsphere.core.parsing.cache.ParsingResultCache; -import io.shardingsphere.core.util.SQLAssertHelper; -import lombok.Getter; -import org.dbunit.DatabaseUnitException; -import org.dbunit.IDatabaseTester; -import org.dbunit.dataset.IDataSet; -import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; -import org.dbunit.operation.DatabaseOperation; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.io.File; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.MalformedURLException; -import java.net.URL; -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -public abstract class AbstractSQLAssertTest extends AbstractSQLTest { - - private final String testCaseName; - - @Getter - private final String sql; - - private final DatabaseType type; - - private final List shardingRules; - - private final SQLAssertHelper sqlAssertHelper; - - protected AbstractSQLAssertTest(final String testCaseName, final String sql, final DatabaseType type, final List shardingRules) { - this.testCaseName = testCaseName; - this.sql = sql; - this.type = type; - this.shardingRules = shardingRules; - sqlAssertHelper = new SQLAssertHelper(sql); - } - - protected static void importAllDataSet(final List dataSetFiles) throws Exception { - for (DatabaseType databaseType : getDatabaseTypes()) { - DatabaseEnvironment dbEnv = new DatabaseEnvironment(databaseType); - for (String each : dataSetFiles) { - InputStream is = AbstractSQLTest.class.getClassLoader().getResourceAsStream(each); - IDataSet dataSet = new FlatXmlDataSetBuilder().build(new InputStreamReader(is)); - IDatabaseTester databaseTester = new ShardingJdbcDatabaseTester(dbEnv.getDriverClassName(), dbEnv.getURL(getDatabaseName(each)), - dbEnv.getUsername(), dbEnv.getPassword(), dbEnv.getSchema(getDatabaseName(each))); - databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT); - databaseTester.setDataSet(dataSet); - databaseTester.onSetup(); - } - } - } - - protected abstract ShardingTestStrategy getShardingStrategy(); - - protected abstract Map getDataSources() throws SQLException; - - protected File getExpectedFile(final String expected) { - String strategyName = getShardingStrategy().name(); - String expectedFile = null == expected ? "integrate/dataset/EmptyTable.xml" : String.format("integrate/dataset/sharding/%s/expect/" + expected, strategyName, strategyName); - URL url = AbstractSQLAssertTest.class.getClassLoader().getResource(expectedFile); - if (null == url) { - throw new RuntimeException("Wrong expected file:" + expectedFile); - } - return new File(url.getPath()); - } - - @Override - public DatabaseType getCurrentDatabaseType() { - return type; - } - - @Before - public void initDDLTables() throws SQLException { - if (getSql().startsWith("ALTER") || getSql().startsWith("TRUNCATE") || getSql().startsWith("DROP TABLE") || getSql().startsWith("CREATE UNIQUE INDEX") || getSql().startsWith("CREATE INDEX")) { - if (getSql().contains("TEMP")) { - executeSQL("CREATE TEMPORARY TABLE t_temp_log(id int, status varchar(10))"); - } else { - executeSQL("CREATE TABLE t_log(id int, status varchar(10))"); - } - } - if (getSql().startsWith("DROP INDEX")) { - executeSQL("CREATE TABLE t_log(id int, status varchar(10))"); - executeSQL("CREATE INDEX t_log_index ON t_log(status)"); - } - } - - @After - public void cleanupDDLTables() throws SQLException { - if (getSql().startsWith("CREATE UNIQUE INDEX") || getSql().startsWith("CREATE INDEX")) { - executeSQL("DROP TABLE t_log"); - } else if (getSql().startsWith("ALTER") || getSql().startsWith("TRUNCATE") || getSql().startsWith("CREATE") || getSql().startsWith("DROP INDEX")) { - if (getSql().contains("TEMP")) { - executeSQL("DROP TABLE t_temp_log"); - } else { - executeSQL("DROP TABLE t_log"); - } - } - } - - @After - public void cleanupParsingResultCache() { - ParsingResultCache.getInstance().clear(); - } - - private void executeSQL(final String sql) throws SQLException { - for (Map.Entry each : getDataSources().entrySet()) { - if (getCurrentDatabaseType() == each.getKey()) { - try (Connection conn = each.getValue().getConnection(); - Statement statement = conn.createStatement()) { - statement.execute(sql); - // CHECKSTYLE:OFF - } catch (final Exception ex) { - // CHECKSTYLE:ON - ex.printStackTrace(); - throw new RuntimeException(ex); - } - } - } - } - - @Test - public void assertExecuteWithPreparedStatement() throws SQLException { - execute(true, false); - } - - @Test - public void assertExecuteWithStatement() throws SQLException { - execute(false, false); - } - - @Test - public void assertExecuteQueryWithPreparedStatement() throws SQLException { - execute(true, true); - } - - @Test - public void assertExecuteQueryWithStatement() throws SQLException { - execute(false, true); - } - - private void execute(final boolean isPreparedStatement, final boolean isExecute) throws SQLException { - for (Map.Entry each : getDataSources().entrySet()) { - if (getCurrentDatabaseType() == each.getKey()) { - try { - executeAndAssertSQL(isPreparedStatement, isExecute, each.getValue()); - // CHECKSTYLE:OFF - } catch (final Exception ex) { - // CHECKSTYLE:ON - ex.printStackTrace(); - throw new RuntimeException(ex); - } - } - } - } - - private void executeAndAssertSQL(final boolean isPreparedStatement, final boolean isExecute, final AbstractDataSourceAdapter abstractDataSourceAdapter) - throws MalformedURLException, SQLException, DatabaseUnitException { - for (SQLShardingRule sqlShardingRule : shardingRules) { - if (!needAssert(sqlShardingRule)) { - continue; - } - for (SQLAssertData each : sqlShardingRule.getData()) { - File expectedDataSetFile = getExpectedFile(each.getExpected()); - if (sql.toUpperCase().startsWith("SELECT")) { - assertDQL(isPreparedStatement, isExecute, abstractDataSourceAdapter, each, expectedDataSetFile); - } else { - assertDMLAndDDL(isPreparedStatement, isExecute, abstractDataSourceAdapter, each, expectedDataSetFile); - } - } - } - } - - private boolean needAssert(final SQLShardingRule sqlShardingRule) { - String shardingRules = sqlShardingRule.getValue(); - if (null == shardingRules) { - return true; - } - for (String each : shardingRules.split(",")) { - if (getShardingStrategy().name().equals(each)) { - return true; - } - } - return false; - } - - private void assertDQL(final boolean isPreparedStatement, final boolean isExecute, final AbstractDataSourceAdapter abstractDataSourceAdapter, - final SQLAssertData data, final File expectedDataSetFile) - throws MalformedURLException, SQLException, DatabaseUnitException { - if (isPreparedStatement) { - sqlAssertHelper.executeQueryWithPreparedStatement(isExecute, abstractDataSourceAdapter, getParameters(data), expectedDataSetFile); - } else { - sqlAssertHelper.executeQueryWithStatement(abstractDataSourceAdapter, getParameters(data), expectedDataSetFile); - } - } - - private void assertDMLAndDDL(final boolean isPreparedStatement, final boolean isExecute, final AbstractDataSourceAdapter abstractDataSourceAdapter, - final SQLAssertData data, final File expectedDataSetFile) - throws MalformedURLException, SQLException, DatabaseUnitException { - if (isPreparedStatement) { - sqlAssertHelper.executeWithPreparedStatement(isExecute, abstractDataSourceAdapter, getParameters(data)); - } else { - sqlAssertHelper.executeWithStatement(isExecute, abstractDataSourceAdapter, getParameters(data)); - } - String dataSourceName = getDataSourceName(data.getExpected()); - if (dataSourceName.contains("ms")) { - dataSourceName = dataSourceName.replace("ms", "dataSource_master"); - } - try (Connection conn = abstractDataSourceAdapter instanceof MasterSlaveDataSource ? ((MasterSlaveDataSource) abstractDataSourceAdapter).getConnection().getConnection(dataSourceName) - : ((ShardingDataSource) abstractDataSourceAdapter).getConnection().getConnection(dataSourceName)) { - sqlAssertHelper.assertResult(conn, expectedDataSetFile); - } - } - - // TODO 标准化文件名 - private String getDataSourceName(final String expected) { - if (ShardingTestStrategy.masterslaveonly == getShardingStrategy()) { - return "dataSource_master_only"; - } - String result = String.format(expected.split("/")[1].split(".xml")[0], getShardingStrategy().name()); - if (!result.contains("_")) { - result = result + "_0"; - } - if (result.startsWith("tbl")) { - result = "tbl"; - } - if (result.contains("masterslave")) { - result = result.replace("masterslave", "ms"); - } else if (result.contains("hint")) { - result = "dataSource_" + result.replace("hint", "db"); - } else { - result = "dataSource_" + result; - } - return result; - } - - private List getParameters(final SQLAssertData data) { - return Strings.isNullOrEmpty(data.getParameter()) ? Collections.emptyList() : Lists.newArrayList(data.getParameter().split(",")); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractSQLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractSQLTest.java index 8abe28850e8f2..8eff193909c88 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractSQLTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractSQLTest.java @@ -22,8 +22,6 @@ import io.shardingsphere.core.common.env.DatabaseEnvironment; import io.shardingsphere.core.common.env.ShardingJdbcDatabaseTester; import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.integrate.jaxb.helper.SQLAssertJAXBHelper; import io.shardingsphere.core.jdbc.core.ShardingContext; import io.shardingsphere.core.jdbc.core.datasource.MasterSlaveDataSource; import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; @@ -47,7 +45,6 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Properties; @@ -163,14 +160,6 @@ protected static String getDatabaseName(final String dataSetFile) { return fileName.substring(0, fileName.lastIndexOf(".")); } - protected static Collection dataParameters(final SQLType... sqlTypes) { - Collection result = new LinkedList<>(); - for (SQLType each : sqlTypes) { - result.addAll(SQLAssertJAXBHelper.getDataParameters("integrate/assert", each)); - } - return result; - } - private static BasicDataSource buildDataSource(final String dbName, final DatabaseType type) { DatabaseEnvironment dbEnv = new DatabaseEnvironment(type); BasicDataSource result = new BasicDataSource(); diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/common/env/ShardingTestStrategy.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/common/env/ShardingTestStrategy.java deleted file mode 100644 index 412d777928f3c..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/common/env/ShardingTestStrategy.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.common.env; - -public enum ShardingTestStrategy { - - tbl, db, dbtbl, masterslave, nullable, masterslaveonly -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLAssert.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLAssert.java deleted file mode 100644 index 11462132161cd..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLAssert.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.jaxb; - -import lombok.Getter; -import lombok.Setter; - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlElement; -import java.util.List; - -@XmlAccessorType(XmlAccessType.FIELD) -@Getter -@Setter -public final class SQLAssert { - - @XmlAttribute - private String id; - - @XmlElement(name = "sharding-rule") - private List sqlShardingRules; -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLAssertData.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLAssertData.java deleted file mode 100644 index 98536236e27de..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLAssertData.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.jaxb; - -import lombok.Getter; -import lombok.Setter; - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlAttribute; - -@XmlAccessorType(XmlAccessType.FIELD) -@Getter -@Setter -public final class SQLAssertData { - - @XmlAttribute - private String parameter; - - @XmlAttribute - private String expected; -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLAsserts.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLAsserts.java deleted file mode 100644 index a57ff9bd6dbc5..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLAsserts.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.jaxb; - -import lombok.Getter; - -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import java.util.ArrayList; -import java.util.List; - -@XmlRootElement(name = "sqls") -@Getter -public final class SQLAsserts { - - @XmlElement(name = "sql") - private List sqlAsserts = new ArrayList<>(); -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLParameters.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLParameters.java deleted file mode 100644 index f5b77c453a572..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLParameters.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.jaxb; - -import lombok.Getter; -import lombok.Setter; - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import java.util.ArrayList; -import java.util.List; - -@XmlAccessorType(XmlAccessType.FIELD) -@Getter -@Setter -public final class SQLParameters { - - @XmlElement - private List parameter = new ArrayList<>(); -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLShardingRule.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLShardingRule.java deleted file mode 100644 index 4c35b1abf8929..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/SQLShardingRule.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.jaxb; - -import lombok.Getter; -import lombok.Setter; - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlElement; -import java.util.List; - -@XmlAccessorType(XmlAccessType.FIELD) -@Getter -@Setter -public final class SQLShardingRule { - - @XmlAttribute(name = "value") - private String value; - - @XmlElement - private List data; -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/helper/SQLAssertJAXBHelper.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/helper/SQLAssertJAXBHelper.java deleted file mode 100644 index 65774296e301d..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/jaxb/helper/SQLAssertJAXBHelper.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.jaxb.helper; - -import com.google.common.base.Strings; -import com.google.common.collect.Sets; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.SQLType; -import io.shardingsphere.core.integrate.jaxb.SQLAssert; -import io.shardingsphere.core.integrate.jaxb.SQLAsserts; -import io.shardingsphere.test.sql.SQLCasesLoader; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import java.io.File; -import java.net.URL; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -public final class SQLAssertJAXBHelper { - - /** - * Get data parameters. - * - * @param filePath file path - * @param sqlType SQL type - * @return data parameters - */ - public static Collection getDataParameters(final String filePath, final SQLType sqlType) { - Collection result = new ArrayList<>(); - URL url = SQLAssertJAXBHelper.class.getClassLoader().getResource(filePath); - if (null == url) { - return Collections.emptyList(); - } - File assertFilePath = new File(url.getPath()); - if (!assertFilePath.exists()) { - return Collections.emptyList(); - } - - if (assertFilePath.isDirectory()) { - File[] files = assertFilePath.listFiles(); - if (null == files) { - return Collections.emptyList(); - } - for (File each : files) { - if (each.isDirectory()) { - continue; - } - if (isTypeMatched(each.getName(), sqlType)) { - result.addAll(dataParameters(each)); - } - } - } else { - if (isTypeMatched(assertFilePath.getName(), sqlType)) { - result.addAll(dataParameters(assertFilePath)); - } - } - return result; - } - - private static Collection dataParameters(final File file) { - SQLAsserts asserts = loadSQLAsserts(file); - List result = new ArrayList<>(); - for (int i = 0; i < asserts.getSqlAsserts().size(); i++) { - SQLAssert assertObj = asserts.getSqlAsserts().get(i); - for (DatabaseType each : getDatabaseTypes(SQLCasesLoader.getInstance().getDatabaseTypes(assertObj.getId()))) { - result.add(getDataParameter(assertObj, each)); - } - } - return result; - } - - private static SQLAsserts loadSQLAsserts(final File file) { - try { - return (SQLAsserts) JAXBContext.newInstance(SQLAsserts.class).createUnmarshaller().unmarshal(file); - } catch (final JAXBException ex) { - throw new RuntimeException(ex); - } - } - - private static Object[] getDataParameter(final SQLAssert sqlAssert, final DatabaseType dbType) { - final Object[] result = new Object[4]; - result[0] = sqlAssert.getId(); - result[1] = SQLCasesLoader.getInstance().getSupportedSQL(sqlAssert.getId()); - result[2] = dbType; - result[3] = sqlAssert.getSqlShardingRules(); - return result; - } - - private static boolean isTypeMatched(final String fileName, final SQLType sqlType) { - switch (sqlType) { - case DDL: - return fileName.startsWith("alter") || fileName.startsWith("create") || fileName.startsWith("drop") || fileName.startsWith("truncate"); - case DML: - return fileName.startsWith("delete") || fileName.startsWith("insert") || fileName.startsWith("update"); - case DQL: - return fileName.startsWith("select"); - default: return false; - } - } - - private static Collection getDatabaseTypes(final String databaseTypes) { - if (Strings.isNullOrEmpty(databaseTypes)) { - return Sets.newHashSet(DatabaseType.values()); - } - Set result = new HashSet<>(); - for (String each : databaseTypes.split(",")) { - result.add(DatabaseType.valueOf(each)); - } - return result; - } -} From 4b5b01e99d42e5f707e3d8dc2ea2773d4e162bcd Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 11:35:04 +0800 Subject: [PATCH 340/351] for #601: remove original hint test cases, will add to new sharding rule type later --- .../core/integrate/AllIntegrateTests.java | 2 - .../hint/AllHintDatabaseOnlyTests.java | 35 ---- .../sharding/hint/base/AbstractHintTest.java | 108 ----------- ...stractRoutingDatabaseOnlyWithHintTest.java | 37 ---- ...tractShardingDatabaseOnlyWithHintTest.java | 48 ----- .../HintDatabaseShardingValueHelper.java | 26 --- .../hint/helper/HintShardingValueHelper.java | 48 ----- ...RoutingDatabaseOnlyWithHintForDMLTest.java | 172 ------------------ ...tingDatabaseOnlyWithHintForSelectTest.java | 54 ------ ...hardingDatabaseOnlyWithHintForDMLTest.java | 171 ----------------- ...dingDatabaseOnlyWithHintForSelectTest.java | 82 --------- 11 files changed, 783 deletions(-) delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/AllHintDatabaseOnlyTests.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/base/AbstractHintTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/base/AbstractRoutingDatabaseOnlyWithHintTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/base/AbstractShardingDatabaseOnlyWithHintTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/helper/HintDatabaseShardingValueHelper.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/helper/HintShardingValueHelper.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/RoutingDatabaseOnlyWithHintForDMLTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/RoutingDatabaseOnlyWithHintForSelectTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/ShardingDatabaseOnlyWithHintForDMLTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/ShardingDatabaseOnlyWithHintForSelectTest.java diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java index f9adfd72fb2b8..26f03ac46d4dd 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java @@ -18,14 +18,12 @@ package io.shardingsphere.core.integrate; import io.shardingsphere.core.integrate.api.AllAPIIntegrateTests; -import io.shardingsphere.core.integrate.type.sharding.hint.AllHintDatabaseOnlyTests; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({ - AllHintDatabaseOnlyTests.class, AllAPIIntegrateTests.class }) public class AllIntegrateTests { diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/AllHintDatabaseOnlyTests.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/AllHintDatabaseOnlyTests.java deleted file mode 100644 index aede15bd97f67..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/AllHintDatabaseOnlyTests.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding.hint; - -import io.shardingsphere.core.integrate.type.sharding.hint.type.RoutingDatabaseOnlyWithHintForDMLTest; -import io.shardingsphere.core.integrate.type.sharding.hint.type.RoutingDatabaseOnlyWithHintForSelectTest; -import io.shardingsphere.core.integrate.type.sharding.hint.type.ShardingDatabaseOnlyWithHintForDMLTest; -import io.shardingsphere.core.integrate.type.sharding.hint.type.ShardingDatabaseOnlyWithHintForSelectTest; -import org.junit.runner.RunWith; -import org.junit.runners.Suite; - -@RunWith(Suite.class) -@Suite.SuiteClasses({ - ShardingDatabaseOnlyWithHintForDMLTest.class, - ShardingDatabaseOnlyWithHintForSelectTest.class, - RoutingDatabaseOnlyWithHintForDMLTest.class, - RoutingDatabaseOnlyWithHintForSelectTest.class - }) -public class AllHintDatabaseOnlyTests { -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/base/AbstractHintTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/base/AbstractHintTest.java deleted file mode 100644 index 5fc97a36d40bf..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/base/AbstractHintTest.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding.hint.base; - -import io.shardingsphere.core.common.base.AbstractSQLTest; -import io.shardingsphere.core.common.env.DatabaseEnvironment; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.integrate.type.sharding.hint.helper.HintShardingValueHelper; -import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; -import io.shardingsphere.core.rule.ShardingRule; -import io.shardingsphere.core.util.DBUnitUtil; -import org.dbunit.Assertion; -import org.dbunit.DatabaseUnitException; -import org.dbunit.dataset.IDataSet; -import org.dbunit.dataset.ITable; -import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; -import org.junit.Before; - -import javax.sql.DataSource; -import java.io.InputStreamReader; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -public abstract class AbstractHintTest extends AbstractSQLTest { - - @Before - public void cleanAndInitTable() throws Exception { - importDataSet(); - } - - @Override - protected DatabaseType getCurrentDatabaseType() { - return null; - } - - @Override - protected List getInitDataSetFiles() { - return Arrays.asList( - "integrate/dataset/sharding/db/init/db_0.xml", - "integrate/dataset/sharding/db/init/db_1.xml", - "integrate/dataset/sharding/db/init/db_2.xml", - "integrate/dataset/sharding/db/init/db_3.xml", - "integrate/dataset/sharding/db/init/db_4.xml", - "integrate/dataset/sharding/db/init/db_5.xml", - "integrate/dataset/sharding/db/init/db_6.xml", - "integrate/dataset/sharding/db/init/db_7.xml", - "integrate/dataset/sharding/db/init/db_8.xml", - "integrate/dataset/sharding/db/init/db_9.xml"); - } - - protected final Map initShardingDataSources() throws SQLException { - Map> dataSourceMap = createDataSourceMap(); - for (Map.Entry> each : dataSourceMap.entrySet()) { - ShardingRule shardingRule = getShardingRule(each); - getShardingDataSources().put(each.getKey(), new ShardingDataSource(each.getValue(), shardingRule)); - } - return getShardingDataSources(); - } - - protected abstract ShardingRule getShardingRule(Map.Entry> dataSourceEntry); - - protected void assertDataSet(final String expectedDataSetFile, final HintShardingValueHelper helper, - final Connection connection, final String sql, - final DatabaseType type, final Object... params) throws SQLException, DatabaseUnitException { - try (HintShardingValueHelper anotherHelper = helper) { - assertDataSetEquals(expectedDataSetFile, connection, sql, type, params); - } - } - - protected void assertDataSet(final String expectedDataSetFile, final Connection connection, final String sql, final DatabaseType type, final Object... params) - throws SQLException, DatabaseUnitException { - assertDataSetEquals(expectedDataSetFile, connection, sql, type, params); - } - - private void assertDataSetEquals(final String expectedDataSetFile, final Connection connection, - final String sql, final DatabaseType type, final Object[] params) throws SQLException, DatabaseUnitException { - try ( - Connection conn = connection; - PreparedStatement ps = conn.prepareStatement(sql)) { - int i = 1; - for (Object param : params) { - ps.setObject(i++, param); - } - ITable actualTable = DBUnitUtil.getConnection(new DatabaseEnvironment(type), connection).createTable("t_order", ps); - IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(new InputStreamReader(AbstractHintTest.class.getClassLoader().getResourceAsStream(expectedDataSetFile))); - Assertion.assertEquals(expectedDataSet.getTable("t_order"), actualTable); - } - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/base/AbstractRoutingDatabaseOnlyWithHintTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/base/AbstractRoutingDatabaseOnlyWithHintTest.java deleted file mode 100644 index 5e89bc884eb6b..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/base/AbstractRoutingDatabaseOnlyWithHintTest.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding.hint.base; - -import io.shardingsphere.core.api.config.ShardingRuleConfiguration; -import io.shardingsphere.core.api.config.strategy.HintShardingStrategyConfiguration; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.integrate.fixture.HintModuloDatabaseShardingAlgorithm; -import io.shardingsphere.core.rule.ShardingRule; - -import javax.sql.DataSource; -import java.util.Map; - -public abstract class AbstractRoutingDatabaseOnlyWithHintTest extends AbstractHintTest { - - @Override - protected ShardingRule getShardingRule(final Map.Entry> dataSourceEntry) { - ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); - shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new HintShardingStrategyConfiguration(new HintModuloDatabaseShardingAlgorithm())); - return new ShardingRule(shardingRuleConfig, dataSourceEntry.getValue().keySet()); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/base/AbstractShardingDatabaseOnlyWithHintTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/base/AbstractShardingDatabaseOnlyWithHintTest.java deleted file mode 100644 index b601210025d7c..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/base/AbstractShardingDatabaseOnlyWithHintTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding.hint.base; - -import io.shardingsphere.core.api.config.ShardingRuleConfiguration; -import io.shardingsphere.core.api.config.TableRuleConfiguration; -import io.shardingsphere.core.api.config.strategy.ComplexShardingStrategyConfiguration; -import io.shardingsphere.core.api.config.strategy.NoneShardingStrategyConfiguration; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.integrate.fixture.ComplexKeysModuloDatabaseShardingAlgorithm; -import io.shardingsphere.core.rule.ShardingRule; - -import javax.sql.DataSource; -import java.util.Map; - -public abstract class AbstractShardingDatabaseOnlyWithHintTest extends AbstractHintTest { - - @Override - protected ShardingRule getShardingRule(final Map.Entry> dataSourceEntry) { - ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); - TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration(); - orderTableRuleConfig.setLogicTable("t_order"); - shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig); - TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration(); - orderItemTableRuleConfig.setLogicTable("t_order_item"); - shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig); - shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item"); - shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new ComplexShardingStrategyConfiguration("user_id", new ComplexKeysModuloDatabaseShardingAlgorithm())); - shardingRuleConfig.setDefaultTableShardingStrategyConfig(new NoneShardingStrategyConfiguration()); - return new ShardingRule(shardingRuleConfig, dataSourceEntry.getValue().keySet()); - } - -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/helper/HintDatabaseShardingValueHelper.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/helper/HintDatabaseShardingValueHelper.java deleted file mode 100644 index cf361d982f01e..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/helper/HintDatabaseShardingValueHelper.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding.hint.helper; - -public class HintDatabaseShardingValueHelper extends HintShardingValueHelper { - - public HintDatabaseShardingValueHelper(final int userId) { - super(userId, 0); - getHintManager().setDatabaseShardingValue(userId); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/helper/HintShardingValueHelper.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/helper/HintShardingValueHelper.java deleted file mode 100644 index 5eef159ae63f2..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/helper/HintShardingValueHelper.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding.hint.helper; - -import io.shardingsphere.core.api.HintManager; -import io.shardingsphere.core.constant.ShardingOperator; -import lombok.AccessLevel; -import lombok.Getter; - -import java.util.List; - -public class HintShardingValueHelper implements AutoCloseable { - - @Getter(AccessLevel.PROTECTED) - private final HintManager hintManager; - - public HintShardingValueHelper(final int userId, final int orderId) { - hintManager = HintManager.getInstance(); - hintManager.addDatabaseShardingValue("t_order", "user_id", userId); - hintManager.addTableShardingValue("t_order", "order_id", orderId); - } - - public HintShardingValueHelper(final List userId, final ShardingOperator userIdOperator, final List orderId, final ShardingOperator orderIdOperator) { - hintManager = HintManager.getInstance(); - hintManager.addDatabaseShardingValue("t_order", "user_id", userIdOperator, userId.toArray(new Comparable[userId.size()])); - hintManager.addTableShardingValue("t_order", "order_id", orderIdOperator, orderId.toArray(new Comparable[orderId.size()])); - } - - @Override - public void close() { - hintManager.close(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/RoutingDatabaseOnlyWithHintForDMLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/RoutingDatabaseOnlyWithHintForDMLTest.java deleted file mode 100644 index c3dd1e14fb218..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/RoutingDatabaseOnlyWithHintForDMLTest.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding.hint.type; - -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.integrate.sql.DatabaseTestSQL; -import io.shardingsphere.core.integrate.type.sharding.hint.base.AbstractRoutingDatabaseOnlyWithHintTest; -import io.shardingsphere.core.integrate.type.sharding.hint.helper.HintDatabaseShardingValueHelper; -import io.shardingsphere.core.integrate.type.sharding.hint.helper.HintShardingValueHelper; -import io.shardingsphere.core.jdbc.core.connection.ShardingConnection; -import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; -import io.shardingsphere.core.util.SQLPlaceholderUtil; -import org.dbunit.DatabaseUnitException; -import org.junit.Before; -import org.junit.Test; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.Map; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -public class RoutingDatabaseOnlyWithHintForDMLTest extends AbstractRoutingDatabaseOnlyWithHintTest { - - private Map shardingDataSources; - - @Before - public void init() throws SQLException { - shardingDataSources = initShardingDataSources(); - } - - @Test - public void assertInsertWithAllPlaceholders() throws SQLException, DatabaseUnitException { - for (Map.Entry each : shardingDataSources.entrySet()) { - for (int i = 1; i <= 10; i++) { - try (HintShardingValueHelper helper = new HintDatabaseShardingValueHelper(i); - Connection connection = each.getValue().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(DatabaseTestSQL.INSERT_ORDER_WITH_ALL_PLACEHOLDERS_SQL)) { - preparedStatement.setInt(1, i); - preparedStatement.setInt(2, i); - preparedStatement.setString(3, "insert"); - preparedStatement.executeUpdate(); - } - } - assertDataSet(each.getValue().getConnection(), each.getKey(), "insert", "insert"); - } - } - - @Test - public void assertInsertWithoutPlaceholder() throws SQLException, DatabaseUnitException { - for (Map.Entry each : shardingDataSources.entrySet()) { - for (int i = 1; i <= 10; i++) { - try (HintShardingValueHelper helper = new HintDatabaseShardingValueHelper(i); - Connection connection = each.getValue().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(String.format(DatabaseTestSQL.INSERT_WITHOUT_PLACEHOLDER_SQL, i, i))) { - preparedStatement.executeUpdate(); - } - } - assertDataSet(each.getValue().getConnection(), each.getKey(), "insert", "insert"); - } - } - - @Test - public void assertInsertWithPartialPlaceholders() throws SQLException, DatabaseUnitException { - for (Map.Entry each : shardingDataSources.entrySet()) { - for (int i = 1; i <= 10; i++) { - try (HintShardingValueHelper helper = new HintDatabaseShardingValueHelper(i); - Connection connection = each.getValue().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(String.format(DatabaseTestSQL.INSERT_WITH_PARTIAL_PLACEHOLDERS_SQL, i, i))) { - preparedStatement.setString(1, "insert"); - preparedStatement.executeUpdate(); - } - } - assertDataSet(each.getValue().getConnection(), each.getKey(), "insert", "insert"); - } - } - - @Test - public void assertUpdateWithoutAlias() throws SQLException, DatabaseUnitException { - for (Map.Entry each : shardingDataSources.entrySet()) { - updateWithoutAlias(each); - assertDataSet(each.getValue().getConnection(), each.getKey(), "update", "updated"); - } - } - - private void updateWithoutAlias(final Map.Entry dataSourceEntry) throws SQLException { - for (int i = 10; i < 30; i++) { - for (int j = 0; j < 2; j++) { - try (HintShardingValueHelper helper = new HintDatabaseShardingValueHelper(i); - Connection connection = dataSourceEntry.getValue().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.UPDATE_WITHOUT_ALIAS_SQL))) { - preparedStatement.setString(1, "updated"); - preparedStatement.setInt(2, i * 100 + j); - preparedStatement.setInt(3, i); - assertThat(preparedStatement.executeUpdate(), is(1)); - } - } - } - } - - @Test - public void assertUpdateWithAlias() throws SQLException, DatabaseUnitException { - for (Map.Entry each : shardingDataSources.entrySet()) { - if (DatabaseType.H2 == each.getKey() || DatabaseType.MySQL == each.getKey()) { - updateWithAlias(each); - } - } - } - - private void updateWithAlias(final Map.Entry dataSourceEntry) throws SQLException, DatabaseUnitException { - for (int i = 10; i < 30; i++) { - for (int j = 0; j < 2; j++) { - try (HintShardingValueHelper helper = new HintDatabaseShardingValueHelper(i); - Connection connection = dataSourceEntry.getValue().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(DatabaseTestSQL.UPDATE_WITH_ALIAS_SQL)) { - preparedStatement.setString(1, "updated"); - preparedStatement.setInt(2, i * 100 + j); - preparedStatement.setInt(3, i); - assertThat(preparedStatement.executeUpdate(), is(1)); - } - } - } - assertDataSet(dataSourceEntry.getValue().getConnection(), dataSourceEntry.getKey(), "update", "updated"); - } - - @Test - public void assertDeleteWithoutAlias() throws SQLException, DatabaseUnitException { - for (Map.Entry each : shardingDataSources.entrySet()) { - deleteWithoutAlias(each); - } - } - - private void deleteWithoutAlias(final Map.Entry dataSourceEntry) throws SQLException, DatabaseUnitException { - for (int i = 10; i < 30; i++) { - for (int j = 0; j < 2; j++) { - try (HintShardingValueHelper helper = new HintDatabaseShardingValueHelper(i); - Connection connection = dataSourceEntry.getValue().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.DELETE_WITHOUT_ALIAS_SQL))) { - preparedStatement.setInt(1, i * 100 + j); - preparedStatement.setInt(2, i); - preparedStatement.setString(3, "init"); - assertThat(preparedStatement.executeUpdate(), is(1)); - } - } - } - assertDataSet(dataSourceEntry.getValue().getConnection(), dataSourceEntry.getKey(), "delete", "init"); - } - - private void assertDataSet(final ShardingConnection connection, final DatabaseType type, final String expectedDataSetPattern, final String status) throws SQLException, DatabaseUnitException { - for (int i = 0; i < 10; i++) { - assertDataSet(String.format("integrate/dataset/sharding/hint/expect/%s/db_%s.xml", expectedDataSetPattern, i), - connection.getConnection(String.format("dataSource_db_%s", i)), SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.ASSERT_SELECT_WITH_STATUS_SQL), type, status); - } - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/RoutingDatabaseOnlyWithHintForSelectTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/RoutingDatabaseOnlyWithHintForSelectTest.java deleted file mode 100644 index cb7e059043fbe..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/RoutingDatabaseOnlyWithHintForSelectTest.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding.hint.type; - -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.integrate.sql.DatabaseTestSQL; -import io.shardingsphere.core.integrate.type.sharding.hint.base.AbstractRoutingDatabaseOnlyWithHintTest; -import io.shardingsphere.core.integrate.type.sharding.hint.helper.HintDatabaseShardingValueHelper; -import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; -import io.shardingsphere.core.util.SQLPlaceholderUtil; -import org.dbunit.DatabaseUnitException; -import org.junit.Before; -import org.junit.Test; - -import java.sql.SQLException; -import java.util.Map; - -public class RoutingDatabaseOnlyWithHintForSelectTest extends AbstractRoutingDatabaseOnlyWithHintTest { - - private Map shardingDataSources; - - @Before - public void init() throws SQLException { - shardingDataSources = initShardingDataSources(); - } - - @Test - public void assertSelectEqualsWithSingleTable() throws SQLException, DatabaseUnitException { - for (Map.Entry each : shardingDataSources.entrySet()) { - String sql = SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.SELECT_EQUALS_WITH_SINGLE_TABLE_SQL); - assertDataSet("integrate/dataset/sharding/db/expect/select/SelectEqualsWithSingleTable_0.xml", new HintDatabaseShardingValueHelper(10), - each.getValue().getConnection(), sql, each.getKey(), 10, 1000); - assertDataSet("integrate/dataset/sharding/db/expect/select/SelectEqualsWithSingleTable_1.xml", new HintDatabaseShardingValueHelper(12), - each.getValue().getConnection(), sql, each.getKey(), 12, 1201); - assertDataSet("integrate/dataset/Empty.xml", new HintDatabaseShardingValueHelper(12), - each.getValue().getConnection(), sql, each.getKey(), 12, 1000); - } - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/ShardingDatabaseOnlyWithHintForDMLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/ShardingDatabaseOnlyWithHintForDMLTest.java deleted file mode 100644 index baf188276075b..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/ShardingDatabaseOnlyWithHintForDMLTest.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding.hint.type; - -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.integrate.sql.DatabaseTestSQL; -import io.shardingsphere.core.integrate.type.sharding.hint.base.AbstractShardingDatabaseOnlyWithHintTest; -import io.shardingsphere.core.integrate.type.sharding.hint.helper.HintShardingValueHelper; -import io.shardingsphere.core.jdbc.core.connection.ShardingConnection; -import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; -import io.shardingsphere.core.util.SQLPlaceholderUtil; -import org.dbunit.DatabaseUnitException; -import org.junit.Before; -import org.junit.Test; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.Map; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -public final class ShardingDatabaseOnlyWithHintForDMLTest extends AbstractShardingDatabaseOnlyWithHintTest { - - private Map shardingDataSources; - - @Before - public void init() throws SQLException { - shardingDataSources = initShardingDataSources(); - } - - @Test - public void assertInsertWithAllPlaceholders() throws SQLException, DatabaseUnitException { - for (Map.Entry each : shardingDataSources.entrySet()) { - for (int i = 1; i <= 10; i++) { - try (HintShardingValueHelper helper = new HintShardingValueHelper(i, i); - Connection connection = each.getValue().getConnection()) { - PreparedStatement preparedStatement = connection.prepareStatement(DatabaseTestSQL.INSERT_ORDER_WITH_ALL_PLACEHOLDERS_SQL); - preparedStatement.setInt(1, i); - preparedStatement.setInt(2, i); - preparedStatement.setString(3, "insert"); - preparedStatement.executeUpdate(); - } - } - assertDataSet(each.getValue().getConnection(), each.getKey(), "insert", "insert"); - } - } - - @Test - public void assertInsertWithoutPlaceholder() throws SQLException, DatabaseUnitException { - for (Map.Entry each : shardingDataSources.entrySet()) { - for (int i = 1; i <= 10; i++) { - try (HintShardingValueHelper helper = new HintShardingValueHelper(i, i); - Connection connection = each.getValue().getConnection()) { - PreparedStatement preparedStatement = connection.prepareStatement(String.format(DatabaseTestSQL.INSERT_WITHOUT_PLACEHOLDER_SQL, i, i)); - preparedStatement.executeUpdate(); - } - } - assertDataSet(each.getValue().getConnection(), each.getKey(), "insert", "insert"); - } - } - - @Test - public void assertInsertWithPartialPlaceholders() throws SQLException, DatabaseUnitException { - for (Map.Entry each : shardingDataSources.entrySet()) { - for (int i = 1; i <= 10; i++) { - try (HintShardingValueHelper helper = new HintShardingValueHelper(i, i); - Connection connection = each.getValue().getConnection()) { - PreparedStatement preparedStatement = connection.prepareStatement(String.format(DatabaseTestSQL.INSERT_WITH_PARTIAL_PLACEHOLDERS_SQL, i, i)); - preparedStatement.setString(1, "insert"); - preparedStatement.executeUpdate(); - } - } - assertDataSet(each.getValue().getConnection(), each.getKey(), "insert", "insert"); - } - } - - @Test - public void assertUpdateWithoutAlias() throws SQLException, DatabaseUnitException { - for (Map.Entry each : shardingDataSources.entrySet()) { - updateWithoutAlias(each); - assertDataSet(each.getValue().getConnection(), each.getKey(), "update", "updated"); - } - } - - private void updateWithoutAlias(final Map.Entry dataSourceEntry) throws SQLException { - for (int i = 10; i < 30; i++) { - for (int j = 0; j < 2; j++) { - try (HintShardingValueHelper helper = new HintShardingValueHelper(i, i * 100 + j); - Connection connection = dataSourceEntry.getValue().getConnection()) { - PreparedStatement preparedStatement = connection.prepareStatement(SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.UPDATE_WITHOUT_ALIAS_SQL)); - preparedStatement.setString(1, "updated"); - preparedStatement.setInt(2, i * 100 + j); - preparedStatement.setInt(3, i); - assertThat(preparedStatement.executeUpdate(), is(1)); - } - } - } - } - - @Test - public void assertUpdateWithAlias() throws SQLException, DatabaseUnitException { - for (Map.Entry each : shardingDataSources.entrySet()) { - if (DatabaseType.H2 == each.getKey() || DatabaseType.MySQL == each.getKey()) { - updateWithAlias(each); - } - } - } - - private void updateWithAlias(final Map.Entry dataSourceEntry) throws SQLException, DatabaseUnitException { - for (int i = 10; i < 30; i++) { - for (int j = 0; j < 2; j++) { - try (HintShardingValueHelper helper = new HintShardingValueHelper(i, i * 100 + j); - Connection connection = dataSourceEntry.getValue().getConnection()) { - PreparedStatement preparedStatement = connection.prepareStatement(DatabaseTestSQL.UPDATE_WITH_ALIAS_SQL); - preparedStatement.setString(1, "updated"); - preparedStatement.setInt(2, i * 100 + j); - preparedStatement.setInt(3, i); - assertThat(preparedStatement.executeUpdate(), is(1)); - } - } - } - assertDataSet(dataSourceEntry.getValue().getConnection(), dataSourceEntry.getKey(), "update", "updated"); - } - - @Test - public void assertDeleteWithoutAlias() throws SQLException, DatabaseUnitException { - for (Map.Entry each : shardingDataSources.entrySet()) { - deleteWithoutAlias(each); - assertDataSet(each.getValue().getConnection(), each.getKey(), "delete", "init"); - } - } - - private void deleteWithoutAlias(final Map.Entry dataSourceEntry) throws SQLException { - for (int i = 10; i < 30; i++) { - for (int j = 0; j < 2; j++) { - try (HintShardingValueHelper helper = new HintShardingValueHelper(i, i * 100 + j); - Connection connection = dataSourceEntry.getValue().getConnection()) { - PreparedStatement preparedStatement = connection.prepareStatement(SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.DELETE_WITHOUT_ALIAS_SQL)); - preparedStatement.setInt(1, i * 100 + j); - preparedStatement.setInt(2, i); - preparedStatement.setString(3, "init"); - assertThat(preparedStatement.executeUpdate(), is(1)); - } - } - } - } - - private void assertDataSet(final ShardingConnection connection, final DatabaseType type, final String expectedDataSetPattern, final String status) throws SQLException, DatabaseUnitException { - for (int i = 0; i < 10; i++) { - assertDataSet(String.format("integrate/dataset/sharding/hint/expect/%s/db_%s.xml", expectedDataSetPattern, i), - connection.getConnection(String.format("dataSource_db_%s", i)), SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.ASSERT_SELECT_WITH_STATUS_SQL), type, status); - } - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/ShardingDatabaseOnlyWithHintForSelectTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/ShardingDatabaseOnlyWithHintForSelectTest.java deleted file mode 100644 index c719b084c296d..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/type/sharding/hint/type/ShardingDatabaseOnlyWithHintForSelectTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.type.sharding.hint.type; - -import com.google.common.collect.Lists; -import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.constant.ShardingOperator; -import io.shardingsphere.core.integrate.sql.DatabaseTestSQL; -import io.shardingsphere.core.integrate.type.sharding.hint.base.AbstractShardingDatabaseOnlyWithHintTest; -import io.shardingsphere.core.integrate.type.sharding.hint.helper.HintShardingValueHelper; -import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; -import io.shardingsphere.core.util.SQLPlaceholderUtil; -import org.dbunit.DatabaseUnitException; -import org.junit.Before; -import org.junit.Test; - -import java.sql.SQLException; -import java.util.Map; - -public final class ShardingDatabaseOnlyWithHintForSelectTest extends AbstractShardingDatabaseOnlyWithHintTest { - - private Map shardingDataSources; - - @Before - public void init() throws SQLException { - shardingDataSources = initShardingDataSources(); - } - - @Test - public void assertSelectEqualsWithSingleTable() throws SQLException, DatabaseUnitException { - String sql = SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.SELECT_EQUALS_WITH_SINGLE_TABLE_SQL); - for (Map.Entry each : shardingDataSources.entrySet()) { - assertDataSet("integrate/dataset/sharding/db/expect/select/SelectEqualsWithSingleTable_0.xml", - new HintShardingValueHelper(10, 1000), each.getValue().getConnection(), sql, each.getKey(), 10, 1000); - assertDataSet("integrate/dataset/sharding/db/expect/select/SelectEqualsWithSingleTable_1.xml", - new HintShardingValueHelper(12, 1201), each.getValue().getConnection(), sql, each.getKey(), 12, 1201); - assertDataSet("integrate/dataset/Empty.xml", - new HintShardingValueHelper(12, 1000), each.getValue().getConnection(), sql, each.getKey(), 12, 1000); - } - } - - @Test - public void assertSelectBetweenWithSingleTable() throws SQLException, DatabaseUnitException { - String sql = SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.SELECT_BETWEEN_WITH_SINGLE_TABLE_SQL); - for (Map.Entry each : shardingDataSources.entrySet()) { - assertDataSet("integrate/dataset/sharding/db/expect/select/SelectBetweenWithSingleTable.xml", new HintShardingValueHelper(Lists.newArrayList(10, 12), - ShardingOperator.BETWEEN, Lists.newArrayList(1001, 1200), ShardingOperator.BETWEEN), each.getValue().getConnection(), - sql, each.getKey(), 10, 12, 1001, 1200); - assertDataSet("integrate/dataset/Empty.xml", new HintShardingValueHelper(Lists.newArrayList(10, 12), - ShardingOperator.BETWEEN, Lists.newArrayList(1309, 1408), ShardingOperator.BETWEEN), each.getValue().getConnection(), sql, each.getKey(), 10, 12, 1309, 1408); - } - } - - @Test - public void assertSelectInWithSingleTable() throws SQLException, DatabaseUnitException { - String sql = SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.SELECT_IN_WITH_SINGLE_TABLE_SQL); - for (Map.Entry each : shardingDataSources.entrySet()) { - assertDataSet("integrate/dataset/sharding/db/expect/select/SelectInWithSingleTable_0.xml", new HintShardingValueHelper(Lists.newArrayList(10, 12, 15), - ShardingOperator.IN, Lists.newArrayList(1000, 1201), ShardingOperator.IN), each.getValue().getConnection(), sql, each.getKey(), 10, 12, 15, 1000, 1201); - assertDataSet("integrate/dataset/sharding/db/expect/select/SelectInWithSingleTable_1.xml", new HintShardingValueHelper(Lists.newArrayList(10, 12, 15), - ShardingOperator.IN, Lists.newArrayList(1000, 1101), ShardingOperator.IN), each.getValue().getConnection(), sql, each.getKey(), 10, 12, 15, 1000, 1101); - assertDataSet("integrate/dataset/Empty.xml", new HintShardingValueHelper(Lists.newArrayList(10, 12, 15), - ShardingOperator.IN, Lists.newArrayList(1309, 1408), ShardingOperator.IN), each.getValue().getConnection(), sql, each.getKey(), 10, 12, 15, 1309, 1408); - } - } - -} From 8a29f0b51dce30ecd38490d61bb5d7f71f38d5ec Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 11:43:14 +0800 Subject: [PATCH 341/351] for #601: remove original yaml test cases, instead of unit test on sharding-core --- .../java/io/shardingsphere/core/AllTests.java | 5 +- .../io/shardingsphere/core/AllUnitTests.java | 4 +- .../core/integrate/AllIntegrateTests.java | 30 ----- .../integrate/api/AllAPIIntegrateTests.java | 27 ---- .../api/yaml/AbstractYamlDataSourceTest.java | 62 ---------- .../core/integrate/api/yaml/AllYamlTests.java | 30 ----- .../yaml/YamlMasterSlaveIntegrateTest.java | 117 ------------------ .../api/yaml/YamlShardingIntegrateTest.java | 100 --------------- ...lShardingWithMasterSlaveIntegrateTest.java | 89 ------------- .../yaml/fixture/DecrementKeyGenerator.java | 32 ----- .../yaml/fixture/IncrementKeyGenerator.java | 32 ----- .../api/yaml/fixture/MultiAlgorithm.java | 31 ----- .../api/yaml/fixture/SingleAlgorithm.java | 36 ------ 13 files changed, 3 insertions(+), 592 deletions(-) delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/AllAPIIntegrateTests.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/AbstractYamlDataSourceTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/AllYamlTests.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/YamlMasterSlaveIntegrateTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/YamlShardingIntegrateTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/YamlShardingWithMasterSlaveIntegrateTest.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/DecrementKeyGenerator.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/IncrementKeyGenerator.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/MultiAlgorithm.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/SingleAlgorithm.java diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/AllTests.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/AllTests.java index c89d1d3ea070e..eff3bff96223d 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/AllTests.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/AllTests.java @@ -17,7 +17,7 @@ package io.shardingsphere.core; -import io.shardingsphere.core.integrate.AllIntegrateTests; +import io.shardingsphere.dbtest.AllIntegrateTests; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @@ -25,8 +25,7 @@ @RunWith(Suite.class) @SuiteClasses({ AllUnitTests.class, - AllIntegrateTests.class, - io.shardingsphere.dbtest.AllIntegrateTests.class + AllIntegrateTests.class }) public class AllTests { } diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/AllUnitTests.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/AllUnitTests.java index 3af9c6b5d0add..4bec3b9aff8f4 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/AllUnitTests.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/AllUnitTests.java @@ -19,7 +19,6 @@ import io.shardingsphere.core.api.AllApiTests; import io.shardingsphere.core.executor.AllExecutorTests; -import io.shardingsphere.core.integrate.api.yaml.AllYamlTests; import io.shardingsphere.core.jdbc.AllJDBCTests; import io.shardingsphere.core.util.AllUtilTests; import org.junit.runner.RunWith; @@ -31,8 +30,7 @@ AllApiTests.class, AllExecutorTests.class, AllJDBCTests.class, - AllUtilTests.class, - AllYamlTests.class + AllUtilTests.class }) public class AllUnitTests { } diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java deleted file mode 100644 index 26f03ac46d4dd..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/AllIntegrateTests.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate; - -import io.shardingsphere.core.integrate.api.AllAPIIntegrateTests; -import org.junit.runner.RunWith; -import org.junit.runners.Suite; -import org.junit.runners.Suite.SuiteClasses; - -@RunWith(Suite.class) -@SuiteClasses({ - AllAPIIntegrateTests.class - }) -public class AllIntegrateTests { -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/AllAPIIntegrateTests.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/AllAPIIntegrateTests.java deleted file mode 100644 index f004e5fde169a..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/AllAPIIntegrateTests.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.api; - -import io.shardingsphere.core.integrate.api.yaml.AllYamlTests; -import org.junit.runner.RunWith; -import org.junit.runners.Suite; - -@RunWith(Suite.class) -@Suite.SuiteClasses(AllYamlTests.class) -public class AllAPIIntegrateTests { -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/AbstractYamlDataSourceTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/AbstractYamlDataSourceTest.java deleted file mode 100644 index b038ea5f98c24..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/AbstractYamlDataSourceTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.api.yaml; - -import org.apache.commons.dbcp2.BasicDataSource; -import org.h2.tools.RunScript; -import org.junit.BeforeClass; - -import javax.sql.DataSource; -import java.io.File; -import java.io.InputStreamReader; -import java.sql.SQLException; -import java.util.Arrays; -import java.util.List; - -public abstract class AbstractYamlDataSourceTest { - - @BeforeClass - public static void createSchema() throws SQLException { - for (String each : getSchemaFiles()) { - RunScript.execute(createDataSource(getFileName(each)).getConnection(), new InputStreamReader(AbstractYamlDataSourceTest.class.getClassLoader().getResourceAsStream(each))); - } - } - - protected static DataSource createDataSource(final String dsName) { - BasicDataSource result = new BasicDataSource(); - result.setDriverClassName(org.h2.Driver.class.getName()); - result.setUrl(String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL", dsName)); - result.setUsername("sa"); - result.setMaxTotal(100); - return result; - } - - private static String getFileName(final String dataSetFile) { - String fileName = new File(dataSetFile).getName(); - if (-1 == fileName.lastIndexOf(".")) { - return fileName; - } - return fileName.substring(0, fileName.lastIndexOf(".")); - } - - private static List getSchemaFiles() { - return Arrays.asList("integrate/api/schema/sharding/db0.sql", "integrate/api/schema/sharding/db1.sql", "integrate/api/schema/ms/db_master.sql", - "integrate/api/schema/ms/db_slave_0.sql", "integrate/api/schema/ms/db_slave_1.sql", "integrate/api/schema/sharding_ms/db0_master.sql", - "integrate/api/schema/sharding_ms/db1_master.sql", "integrate/api/schema/sharding_ms/db0_slave.sql", "integrate/api/schema/sharding_ms/db1_slave.sql"); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/AllYamlTests.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/AllYamlTests.java deleted file mode 100644 index ce02b34caba92..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/AllYamlTests.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.api.yaml; - -import org.junit.runner.RunWith; -import org.junit.runners.Suite; - -@RunWith(Suite.class) -@Suite.SuiteClasses({ - YamlShardingIntegrateTest.class, - YamlMasterSlaveIntegrateTest.class, - YamlShardingWithMasterSlaveIntegrateTest.class - }) -public class AllYamlTests { -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/YamlMasterSlaveIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/YamlMasterSlaveIntegrateTest.java deleted file mode 100644 index ddbdc9e2d70d2..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/YamlMasterSlaveIntegrateTest.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.api.yaml; - -import com.google.common.base.Function; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import io.shardingsphere.core.api.ConfigMapContext; -import io.shardingsphere.core.api.yaml.YamlMasterSlaveDataSourceFactory; -import lombok.RequiredArgsConstructor; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; - -import javax.sql.DataSource; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URISyntaxException; -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Arrays; -import java.util.Collection; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -@RunWith(Parameterized.class) -@RequiredArgsConstructor -public class YamlMasterSlaveIntegrateTest extends AbstractYamlDataSourceTest { - - private final String filePath; - - private final boolean hasDataSource; - - private final boolean isByteArray; - - @Parameters(name = "{index}:{0}-hasDataSource:{1}-isByteArray:{2}") - public static Collection init() { - return Arrays.asList(new Object[][]{ - {"/integrate/api/yaml/ms/configWithMasterSlaveDataSourceWithoutProps.yaml", true, true}, - {"/integrate/api/yaml/ms/configWithMasterSlaveDataSourceWithoutProps.yaml", true, false}, - {"/integrate/api/yaml/ms/configWithMasterSlaveDataSourceWithoutProps.yaml", false, true}, - {"/integrate/api/yaml/ms/configWithMasterSlaveDataSourceWithoutProps.yaml", false, false}, - }); - } - - @Test - public void assertWithDataSource() throws SQLException, URISyntaxException, IOException { - File yamlFile = new File(YamlMasterSlaveIntegrateTest.class.getResource(filePath).toURI()); - DataSource dataSource; - if (hasDataSource) { - if (isByteArray) { - dataSource = YamlMasterSlaveDataSourceFactory.createDataSource(toByteArray(yamlFile.getPath())); - } else { - dataSource = YamlMasterSlaveDataSourceFactory.createDataSource(yamlFile); - } - } else { - if (isByteArray) { - dataSource = YamlMasterSlaveDataSourceFactory.createDataSource(Maps.asMap(Sets.newHashSet("db_master", "db_slave_0", "db_slave_1"), new Function() { - @Override - public DataSource apply(final String key) { - return createDataSource(key); - } - }), toByteArray(yamlFile.getPath())); - } else { - dataSource = YamlMasterSlaveDataSourceFactory.createDataSource(Maps.asMap(Sets.newHashSet("db_master", "db_slave_0", "db_slave_1"), new Function() { - @Override - public DataSource apply(final String key) { - return createDataSource(key); - } - }), yamlFile); - } - } - Map configMap = new ConcurrentHashMap<>(); - configMap.put("key1", "value1"); - assertThat(ConfigMapContext.getInstance().getMasterSlaveConfig(), is(configMap)); - try (Connection conn = dataSource.getConnection(); - Statement stm = conn.createStatement()) { - stm.executeQuery("SELECT * FROM t_order"); - stm.executeQuery("SELECT * FROM t_order_item"); - stm.executeQuery("SELECT * FROM t_config"); - } - } - - private byte[] toByteArray(final String filePath) throws IOException { - final InputStream in = new FileInputStream(filePath); - ByteArrayOutputStream result = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024 * 4]; - int index; - while ((index = in.read(buffer)) != -1) { - result.write(buffer, 0, index); - } - return result.toByteArray(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/YamlShardingIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/YamlShardingIntegrateTest.java deleted file mode 100644 index fd47229688e40..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/YamlShardingIntegrateTest.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.api.yaml; - -import com.google.common.base.Function; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import io.shardingsphere.core.api.ConfigMapContext; -import io.shardingsphere.core.api.yaml.YamlShardingDataSourceFactory; -import io.shardingsphere.core.constant.ShardingProperties; -import io.shardingsphere.core.constant.ShardingPropertiesConstant; -import lombok.RequiredArgsConstructor; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; - -import javax.sql.DataSource; -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Field; -import java.net.URISyntaxException; -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Arrays; -import java.util.Collection; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -@RunWith(Parameterized.class) -@RequiredArgsConstructor -public class YamlShardingIntegrateTest extends AbstractYamlDataSourceTest { - - private final String filePath; - - private final boolean hasDataSource; - - @Parameters(name = "{index}:{0}-{1}") - public static Collection init() { - return Arrays.asList(new Object[][]{ - {"/integrate/api/yaml/sharding/configWithDataSourceWithoutProps.yaml", true}, - {"/integrate/api/yaml/sharding/configWithoutDataSourceWithoutProps.yaml", false}, - {"/integrate/api/yaml/sharding/configWithDataSourceWithProps.yaml", true}, - {"/integrate/api/yaml/sharding/configWithoutDataSourceWithProps.yaml", false}, - }); - } - - @Test - public void assertWithDataSource() throws SQLException, URISyntaxException, IOException, ReflectiveOperationException { - File yamlFile = new File(YamlShardingIntegrateTest.class.getResource(filePath).toURI()); - DataSource dataSource; - if (hasDataSource) { - dataSource = YamlShardingDataSourceFactory.createDataSource(yamlFile); - } else { - dataSource = YamlShardingDataSourceFactory.createDataSource(Maps.asMap(Sets.newHashSet("db0", "db1"), new Function() { - @Override - public DataSource apply(final String key) { - return createDataSource(key); - } - }), yamlFile); - } - if (filePath.contains("WithProps.yaml")) { - Field field = dataSource.getClass().getDeclaredField("shardingProperties"); - if (!field.isAccessible()) { - field.setAccessible(true); - } - ShardingProperties shardingProperties = (ShardingProperties) field.get(dataSource); - assertTrue((Boolean) shardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW)); - } - Map configMap = new ConcurrentHashMap<>(); - configMap.put("key1", "value1"); - assertThat(ConfigMapContext.getInstance().getShardingConfig(), is(configMap)); - try (Connection conn = dataSource.getConnection(); - Statement stm = conn.createStatement()) { - stm.execute(String.format("INSERT INTO t_order(user_id,status) values(%d, %s)", 10, "'insert'")); - stm.executeQuery("SELECT o.*, i.* FROM T_order o JOIN T_order_item i ON o.order_id = i.order_id"); - stm.executeQuery("SELECT * FROM config"); - } - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/YamlShardingWithMasterSlaveIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/YamlShardingWithMasterSlaveIntegrateTest.java deleted file mode 100644 index 4f47f2ef0c8e7..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/YamlShardingWithMasterSlaveIntegrateTest.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.api.yaml; - -import com.google.common.base.Function; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import io.shardingsphere.core.api.yaml.YamlShardingDataSourceFactory; -import lombok.RequiredArgsConstructor; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; - -import javax.sql.DataSource; -import java.io.File; -import java.io.IOException; -import java.net.URISyntaxException; -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -@RunWith(Parameterized.class) -@RequiredArgsConstructor -public class YamlShardingWithMasterSlaveIntegrateTest extends AbstractYamlDataSourceTest { - - private final String filePath; - - private final boolean hasDataSource; - - @Parameters(name = "{index}:{0}-{1}") - public static Collection init() { - return Arrays.asList(new Object[][]{ - {"/integrate/api/yaml/sharding_ms/configWithDataSourceWithoutProps.yaml", true}, - {"/integrate/api/yaml/sharding_ms/configWithoutDataSourceWithoutProps.yaml", false}, - {"/integrate/api/yaml/sharding_ms/configWithDataSourceWithProps.yaml", true}, - {"/integrate/api/yaml/sharding_ms/configWithoutDataSourceWithProps.yaml", false}, - }); - } - - @Test - public void assertWithDataSource() throws SQLException, URISyntaxException, IOException { - File yamlFile = new File(YamlShardingWithMasterSlaveIntegrateTest.class.getResource(filePath).toURI()); - DataSource dataSource; - if (hasDataSource) { - dataSource = YamlShardingDataSourceFactory.createDataSource(yamlFile); - } else { - Map dataSourceMap = Maps.asMap(Sets.newHashSet("db0_master", "db0_slave", "db1_master", "db1_slave"), new Function() { - @Override - public DataSource apply(final String key) { - return createDataSource(key); - } - }); - Map result = new HashMap<>(); - for (Map.Entry each : dataSourceMap.entrySet()) { - result.put(each.getKey(), each.getValue()); - } - dataSource = YamlShardingDataSourceFactory.createDataSource(result, yamlFile); - } - try (Connection conn = dataSource.getConnection(); - Statement stm = conn.createStatement()) { - stm.execute("Create INDEX t_order_index ON t_order(user_id)"); - stm.execute(String.format("INSERT INTO t_order(user_id,status) values(%d, %s)", 10, "'insert'")); - stm.executeQuery("SELECT * FROM t_order"); - stm.executeQuery("SELECT * FROM t_order_item"); - stm.executeQuery("SELECT * FROM config"); - stm.execute("DROP INDEX t_order_index"); - } - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/DecrementKeyGenerator.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/DecrementKeyGenerator.java deleted file mode 100644 index 67a9dc5ad8427..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/DecrementKeyGenerator.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.api.yaml.fixture; - -import io.shardingsphere.core.keygen.KeyGenerator; - -import java.util.concurrent.atomic.AtomicInteger; - -public final class DecrementKeyGenerator implements KeyGenerator { - - private final AtomicInteger sequence = new AtomicInteger(100); - - @Override - public Number generateKey() { - return sequence.decrementAndGet(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/IncrementKeyGenerator.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/IncrementKeyGenerator.java deleted file mode 100644 index 3343352fe074a..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/IncrementKeyGenerator.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.api.yaml.fixture; - -import io.shardingsphere.core.keygen.KeyGenerator; - -import java.util.concurrent.atomic.AtomicInteger; - -public final class IncrementKeyGenerator implements KeyGenerator { - - private static final AtomicInteger SEQUENCE = new AtomicInteger(100); - - @Override - public Number generateKey() { - return SEQUENCE.incrementAndGet(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/MultiAlgorithm.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/MultiAlgorithm.java deleted file mode 100644 index b7a1d4e2178ba..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/MultiAlgorithm.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.api.yaml.fixture; - -import io.shardingsphere.core.api.algorithm.sharding.ShardingValue; -import io.shardingsphere.core.api.algorithm.sharding.complex.ComplexKeysShardingAlgorithm; - -import java.util.Collection; - -public class MultiAlgorithm implements ComplexKeysShardingAlgorithm { - - @Override - public Collection doSharding(final Collection availableTargetNames, final Collection shardingValues) { - return null; - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/SingleAlgorithm.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/SingleAlgorithm.java deleted file mode 100644 index b0d0d487cfd7a..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/api/yaml/fixture/SingleAlgorithm.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.api.yaml.fixture; - -import io.shardingsphere.core.api.algorithm.sharding.PreciseShardingValue; -import io.shardingsphere.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm; - -import java.util.Collection; - -public class SingleAlgorithm implements PreciseShardingAlgorithm { - - @Override - public String doSharding(final Collection availableTargetNames, final PreciseShardingValue shardingValue) { - for (String each : availableTargetNames) { - if (each.endsWith(shardingValue.getValue() % 10 + "")) { - return each; - } - } - throw new UnsupportedOperationException(); - } -} From a962ff0dc5233c0015aa721878a355d2054501a4 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 11:56:47 +0800 Subject: [PATCH 342/351] for #601: refactor JDBCTestSQL --- ...exKeysModuloDatabaseShardingAlgorithm.java | 84 ------------------- .../HintModuloDatabaseShardingAlgorithm.java | 38 --------- ...reciseModuloDatabaseShardingAlgorithm.java | 36 -------- .../PreciseModuloTableShardingAlgorithm.java | 36 -------- .../RangeModuloDatabaseShardingAlgorithm.java | 42 ---------- .../RangeModuloTableShardingAlgorithm.java | 42 ---------- .../JDBCTestSQL.java} | 24 +----- .../ShardingPreparedStatementTest.java | 51 ++++++----- ...pportedOperationPreparedStatementTest.java | 4 +- .../UnsupportedOperationResultSetTest.java | 4 +- 10 files changed, 31 insertions(+), 330 deletions(-) delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/ComplexKeysModuloDatabaseShardingAlgorithm.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/HintModuloDatabaseShardingAlgorithm.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/PreciseModuloDatabaseShardingAlgorithm.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/PreciseModuloTableShardingAlgorithm.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/RangeModuloDatabaseShardingAlgorithm.java delete mode 100644 sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/RangeModuloTableShardingAlgorithm.java rename sharding-jdbc/src/test/java/io/shardingsphere/core/{integrate/sql/DatabaseTestSQL.java => jdbc/JDBCTestSQL.java} (50%) diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/ComplexKeysModuloDatabaseShardingAlgorithm.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/ComplexKeysModuloDatabaseShardingAlgorithm.java deleted file mode 100644 index 523a7d03dae05..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/ComplexKeysModuloDatabaseShardingAlgorithm.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.fixture; - -import com.google.common.collect.Range; -import io.shardingsphere.core.api.algorithm.sharding.ListShardingValue; -import io.shardingsphere.core.api.algorithm.sharding.PreciseShardingValue; -import io.shardingsphere.core.api.algorithm.sharding.RangeShardingValue; -import io.shardingsphere.core.api.algorithm.sharding.ShardingValue; -import io.shardingsphere.core.api.algorithm.sharding.complex.ComplexKeysShardingAlgorithm; - -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashSet; - -public final class ComplexKeysModuloDatabaseShardingAlgorithm implements ComplexKeysShardingAlgorithm { - - @SuppressWarnings("unchecked") - @Override - public Collection doSharding(final Collection availableTargetNames, final Collection shardingValues) { - ShardingValue shardingValue = shardingValues.iterator().next(); - if (shardingValue instanceof PreciseShardingValue) { - return doEqualSharding(availableTargetNames, (PreciseShardingValue) shardingValue); - } - if (shardingValue instanceof ListShardingValue) { - return doInSharding(availableTargetNames, (ListShardingValue) shardingValue); - } - if (shardingValue instanceof RangeShardingValue) { - return doBetweenSharding(availableTargetNames, (RangeShardingValue) shardingValue); - } - throw new UnsupportedOperationException(); - } - - private Collection doEqualSharding(final Collection availableTargetNames, final PreciseShardingValue shardingValue) { - Integer modulo = Integer.parseInt(shardingValue.getValue().toString()) % 10; - for (String each : availableTargetNames) { - if (each.endsWith(modulo.toString())) { - return Collections.singletonList(each); - } - } - throw new UnsupportedOperationException(); - } - - private Collection doInSharding(final Collection availableTargetNames, final ListShardingValue shardingValue) { - Collection result = new LinkedHashSet<>(availableTargetNames.size()); - Collection values = shardingValue.getValues(); - for (Integer value : values) { - for (String dataSourceName : availableTargetNames) { - if (dataSourceName.endsWith(value % 10 + "")) { - result.add(dataSourceName); - } - } - } - return result; - } - - private Collection doBetweenSharding(final Collection availableTargetNames, final RangeShardingValue shardingValue) { - Collection result = new LinkedHashSet<>(availableTargetNames.size()); - Range range = shardingValue.getValueRange(); - for (Integer i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) { - for (String each : availableTargetNames) { - if (each.endsWith(i % 10 + "")) { - result.add(each); - } - } - } - return result; - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/HintModuloDatabaseShardingAlgorithm.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/HintModuloDatabaseShardingAlgorithm.java deleted file mode 100644 index 224728f5b758c..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/HintModuloDatabaseShardingAlgorithm.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.fixture; - -import io.shardingsphere.core.api.algorithm.sharding.ListShardingValue; -import io.shardingsphere.core.api.algorithm.sharding.ShardingValue; -import io.shardingsphere.core.api.algorithm.sharding.hint.HintShardingAlgorithm; - -import java.util.Collection; -import java.util.Collections; - -public class HintModuloDatabaseShardingAlgorithm implements HintShardingAlgorithm { - - @Override - public Collection doSharding(final Collection availableTargetNames, final ShardingValue shardingValue) { - for (String each : availableTargetNames) { - if (each.endsWith((int) ((ListShardingValue) shardingValue).getValues().iterator().next() % 10 + "")) { - return Collections.singletonList(each); - } - } - throw new UnsupportedOperationException(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/PreciseModuloDatabaseShardingAlgorithm.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/PreciseModuloDatabaseShardingAlgorithm.java deleted file mode 100644 index 381f12d694eec..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/PreciseModuloDatabaseShardingAlgorithm.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.fixture; - -import io.shardingsphere.core.api.algorithm.sharding.PreciseShardingValue; -import io.shardingsphere.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm; - -import java.util.Collection; - -public final class PreciseModuloDatabaseShardingAlgorithm implements PreciseShardingAlgorithm { - - @Override - public String doSharding(final Collection availableTargetNames, final PreciseShardingValue shardingValue) { - for (String each : availableTargetNames) { - if (each.endsWith(shardingValue.getValue() % 10 + "")) { - return each; - } - } - throw new UnsupportedOperationException(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/PreciseModuloTableShardingAlgorithm.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/PreciseModuloTableShardingAlgorithm.java deleted file mode 100644 index ebb468e293d7e..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/PreciseModuloTableShardingAlgorithm.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.fixture; - -import io.shardingsphere.core.api.algorithm.sharding.PreciseShardingValue; -import io.shardingsphere.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm; - -import java.util.Collection; - -public final class PreciseModuloTableShardingAlgorithm implements PreciseShardingAlgorithm { - - @Override - public String doSharding(final Collection availableTargetNames, final PreciseShardingValue shardingValue) { - for (String each : availableTargetNames) { - if (each.endsWith(shardingValue.getValue() % 10 + "")) { - return each; - } - } - throw new UnsupportedOperationException(); - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/RangeModuloDatabaseShardingAlgorithm.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/RangeModuloDatabaseShardingAlgorithm.java deleted file mode 100644 index 52d7f9b3104ed..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/RangeModuloDatabaseShardingAlgorithm.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.fixture; - -import com.google.common.collect.Range; -import io.shardingsphere.core.api.algorithm.sharding.RangeShardingValue; -import io.shardingsphere.core.api.algorithm.sharding.standard.RangeShardingAlgorithm; - -import java.util.Collection; -import java.util.LinkedHashSet; - -public final class RangeModuloDatabaseShardingAlgorithm implements RangeShardingAlgorithm { - - @Override - public Collection doSharding(final Collection availableTargetNames, final RangeShardingValue shardingValue) { - Collection result = new LinkedHashSet<>(availableTargetNames.size()); - Range range = shardingValue.getValueRange(); - for (Integer i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) { - for (String each : availableTargetNames) { - if (each.endsWith(i % 10 + "")) { - result.add(each); - } - } - } - return result; - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/RangeModuloTableShardingAlgorithm.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/RangeModuloTableShardingAlgorithm.java deleted file mode 100644 index b52cf3a2b544b..0000000000000 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/fixture/RangeModuloTableShardingAlgorithm.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingsphere.core.integrate.fixture; - -import com.google.common.collect.Range; -import io.shardingsphere.core.api.algorithm.sharding.RangeShardingValue; -import io.shardingsphere.core.api.algorithm.sharding.standard.RangeShardingAlgorithm; - -import java.util.Collection; -import java.util.LinkedHashSet; - -public final class RangeModuloTableShardingAlgorithm implements RangeShardingAlgorithm { - - @Override - public Collection doSharding(final Collection availableTargetNames, final RangeShardingValue shardingValue) { - Collection result = new LinkedHashSet<>(availableTargetNames.size()); - Range range = shardingValue.getValueRange(); - for (Integer i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) { - for (String each : availableTargetNames) { - if (each.endsWith(i % 10 + "")) { - result.add(each); - } - } - } - return result; - } -} diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/sql/DatabaseTestSQL.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/JDBCTestSQL.java similarity index 50% rename from sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/sql/DatabaseTestSQL.java rename to sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/JDBCTestSQL.java index a241b3dd82c0b..c4943aa91e120 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/integrate/sql/DatabaseTestSQL.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/JDBCTestSQL.java @@ -15,38 +15,18 @@ *

*/ -package io.shardingsphere.core.integrate.sql; +package io.shardingsphere.core.jdbc; -public class DatabaseTestSQL { - - public static final String INSERT_ORDER_WITH_ALL_PLACEHOLDERS_SQL = "INSERT INTO t_order (order_id, user_id, status) VALUES (?, ?, ?)"; +public class JDBCTestSQL { public static final String INSERT_ORDER_ITEM_WITH_ALL_PLACEHOLDERS_SQL = "INSERT INTO t_order_item (item_id, order_id, user_id, status) VALUES (?, ?, ?, ?)"; - public static final String INSERT_WITH_PARTIAL_PLACEHOLDERS_SQL = "INSERT INTO t_order (order_id, user_id, status) VALUES (%s, %s, ?)"; - public static final String INSERT_WITH_AUTO_INCREMENT_COLUMN_SQL = "INSERT INTO t_order_item (order_id, user_id, status) VALUES (%s, %s, %s)"; - public static final String INSERT_WITHOUT_PLACEHOLDER_SQL = "INSERT INTO t_order (order_id, user_id, status) VALUES (%s, %s, 'insert')"; - - public static final String UPDATE_WITHOUT_ALIAS_SQL = "UPDATE t_order SET status = %s WHERE order_id = %s AND user_id = %s"; - - public static final String UPDATE_WITH_ALIAS_SQL = "UPDATE t_order AS o SET o.status = ? WHERE o.order_id = ? AND o.user_id = ?"; - public static final String UPDATE_WITHOUT_SHARDING_VALUE_SQL = "UPDATE t_order SET status = %s WHERE status = %s"; - public static final String DELETE_WITHOUT_ALIAS_SQL = "DELETE FROM t_order WHERE order_id = %s AND user_id = %s AND status = %s"; - public static final String DELETE_WITHOUT_SHARDING_VALUE_SQL = "DELETE FROM t_order WHERE status = %s"; - public static final String ASSERT_SELECT_WITH_STATUS_SQL = "SELECT * FROM t_order WHERE status = %s ORDER BY order_id"; - - public static final String SELECT_EQUALS_WITH_SINGLE_TABLE_SQL = "SELECT * FROM t_order WHERE user_id = %s AND order_id = %s"; - - public static final String SELECT_BETWEEN_WITH_SINGLE_TABLE_SQL = "SELECT * FROM t_order WHERE user_id BETWEEN %s AND %s AND order_id BETWEEN %s AND %s ORDER BY user_id, order_id"; - - public static final String SELECT_IN_WITH_SINGLE_TABLE_SQL = "SELECT * FROM t_order WHERE user_id IN (%s, %s, %s) AND order_id IN (%s, %s) ORDER BY user_id, order_id"; - public static final String SELECT_COUNT_AS_ORDERS_COUNT_SQL = "SELECT COUNT(*) AS orders_count FROM t_order WHERE status = ?"; public static final String SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL = "SELECT item_id from t_order_item where user_id = %d and order_id= %s and status = 'BATCH'"; diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatementTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatementTest.java index fcebd43c95cb6..1ab4e7d532313 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatementTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatementTest.java @@ -22,8 +22,7 @@ import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.executor.event.DMLExecutionEvent; import io.shardingsphere.core.executor.event.EventExecutionType; -import io.shardingsphere.core.integrate.sql.DatabaseTestSQL; -import io.shardingsphere.core.jdbc.util.JDBCTestSQL; +import io.shardingsphere.core.jdbc.JDBCTestSQL; import io.shardingsphere.core.util.EventBusInstance; import io.shardingsphere.core.util.SQLPlaceholderUtil; import org.junit.Test; @@ -52,7 +51,7 @@ public ShardingPreparedStatementTest(final DatabaseType databaseType) { public void assertExecuteQueryWithParameter() throws SQLException { try ( Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(DatabaseTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL)) { + PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL)) { preparedStatement.setString(1, "init"); ResultSet resultSet = preparedStatement.executeQuery(); assertTrue(resultSet.next()); @@ -70,7 +69,7 @@ public void assertExecuteQueryWithParameter() throws SQLException { @Test public void assertExecuteQueryWithoutParameter() throws SQLException { - String sql = JDBCTestSQL.SELECT_COUNT_ALIAS_SQL; + String sql = io.shardingsphere.core.jdbc.util.JDBCTestSQL.SELECT_COUNT_ALIAS_SQL; try ( Connection connection = getShardingDataSource().getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { @@ -90,7 +89,7 @@ public void assertExecuteQueryWithoutParameter() throws SQLException { public void assertExecuteUpdateWithParameter() throws SQLException { try ( Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.DELETE_WITHOUT_SHARDING_VALUE_SQL))) { + PreparedStatement preparedStatement = connection.prepareStatement(SQLPlaceholderUtil.replacePreparedStatement(JDBCTestSQL.DELETE_WITHOUT_SHARDING_VALUE_SQL))) { preparedStatement.setString(1, "init"); assertThat(preparedStatement.executeUpdate(), is(4)); preparedStatement.setString(1, "null"); @@ -102,7 +101,7 @@ public void assertExecuteUpdateWithParameter() throws SQLException { @Test public void assertExecuteUpdateWithoutParameter() throws SQLException { - String sql = String.format(DatabaseTestSQL.DELETE_WITHOUT_SHARDING_VALUE_SQL, "'init'"); + String sql = String.format(JDBCTestSQL.DELETE_WITHOUT_SHARDING_VALUE_SQL, "'init'"); try ( Connection connection = getShardingDataSource().getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { @@ -116,7 +115,7 @@ public void assertExecuteUpdateWithoutParameter() throws SQLException { public void assertExecuteWithParameter() throws SQLException { try ( Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(DatabaseTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL)) { + PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL)) { preparedStatement.setString(1, "init"); assertTrue(preparedStatement.execute()); assertTrue(preparedStatement.getResultSet().next()); @@ -140,7 +139,7 @@ public void assertExecuteWithParameter() throws SQLException { @Test public void assertExecuteWithoutParameter() throws SQLException { - String sql = String.format(DatabaseTestSQL.DELETE_WITHOUT_SHARDING_VALUE_SQL, "'init'"); + String sql = String.format(JDBCTestSQL.DELETE_WITHOUT_SHARDING_VALUE_SQL, "'init'"); try ( Connection connection = getShardingDataSource().getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { @@ -154,7 +153,7 @@ public void assertExecuteWithoutParameter() throws SQLException { public void assertExecuteQueryWithResultSetTypeAndResultSetConcurrency() throws SQLException { try ( Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(DatabaseTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) { + PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) { preparedStatement.setString(1, "init"); ResultSet resultSet = preparedStatement.executeQuery(); assertTrue(resultSet.next()); @@ -166,7 +165,7 @@ public void assertExecuteQueryWithResultSetTypeAndResultSetConcurrency() throws public void assertExecuteQueryWithResultSetTypeAndResultSetConcurrencyAndResultSetHoldability() throws SQLException { try ( Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(DatabaseTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL, ResultSet.TYPE_FORWARD_ONLY, + PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT)) { preparedStatement.setString(1, "init"); ResultSet resultSet = preparedStatement.executeQuery(); @@ -179,7 +178,7 @@ public void assertExecuteQueryWithResultSetTypeAndResultSetConcurrencyAndResultS public void assertExecuteQueryWithAutoGeneratedKeys() throws SQLException { try ( Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(DatabaseTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL, Statement.NO_GENERATED_KEYS)) { + PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL, Statement.NO_GENERATED_KEYS)) { preparedStatement.setString(1, "init"); ResultSet resultSet = preparedStatement.executeQuery(); assertTrue(resultSet.next()); @@ -192,7 +191,7 @@ public void assertExecuteQueryWithColumnIndexes() throws SQLException { if (DatabaseType.PostgreSQL != getCurrentDatabaseType()) { try ( Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(DatabaseTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL, new int[]{1})) { + PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL, new int[]{1})) { preparedStatement.setNull(1, java.sql.Types.VARCHAR); ResultSet resultSet = preparedStatement.executeQuery(); assertTrue(resultSet.next()); @@ -206,7 +205,7 @@ public void assertExecuteQueryWithColumnNames() throws SQLException { if (DatabaseType.PostgreSQL != getCurrentDatabaseType()) { try ( Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(DatabaseTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL, new String[]{"orders_count"})) { + PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL, new String[]{"orders_count"})) { preparedStatement.setNull(1, java.sql.Types.VARCHAR); ResultSet resultSet = preparedStatement.executeQuery(); assertTrue(resultSet.next()); @@ -233,7 +232,7 @@ public void subscribe(final DMLExecutionEvent event) { EventBusInstance.getInstance().register(listener); try ( Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(DatabaseTestSQL.INSERT_ORDER_ITEM_WITH_ALL_PLACEHOLDERS_SQL)) { + PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.INSERT_ORDER_ITEM_WITH_ALL_PLACEHOLDERS_SQL)) { preparedStatement.setInt(1, 3101); preparedStatement.setInt(2, 11); preparedStatement.setInt(3, 11); @@ -270,7 +269,7 @@ public void assertAddBatchWithoutGenerateKeyColumn() throws SQLException { if (DatabaseType.Oracle == getCurrentDatabaseType()) { return; } - String sql = SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.INSERT_WITH_AUTO_INCREMENT_COLUMN_SQL); + String sql = SQLPlaceholderUtil.replacePreparedStatement(JDBCTestSQL.INSERT_WITH_AUTO_INCREMENT_COLUMN_SQL); try ( Connection connection = getShardingDataSource().getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); @@ -305,19 +304,19 @@ public void assertAddBatchWithoutGenerateKeyColumn() throws SQLException { assertTrue(generateKeyResultSet.next()); assertThat(generateKeyResultSet.getLong(1), is(4L)); assertFalse(generateKeyResultSet.next()); - try (ResultSet rs = queryStatement.executeQuery(String.format(DatabaseTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 11, 11))) { + try (ResultSet rs = queryStatement.executeQuery(String.format(JDBCTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 11, 11))) { assertTrue(rs.next()); assertThat(rs.getInt(1), is(1)); } - try (ResultSet rs = queryStatement.executeQuery(String.format(DatabaseTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 12, 12))) { + try (ResultSet rs = queryStatement.executeQuery(String.format(JDBCTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 12, 12))) { assertTrue(rs.next()); assertThat(rs.getInt(1), is(2)); } - try (ResultSet rs = queryStatement.executeQuery(String.format(DatabaseTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 21, 21))) { + try (ResultSet rs = queryStatement.executeQuery(String.format(JDBCTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 21, 21))) { assertTrue(rs.next()); assertThat(rs.getInt(1), is(3)); } - try (ResultSet rs = queryStatement.executeQuery(String.format(DatabaseTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 22, 22))) { + try (ResultSet rs = queryStatement.executeQuery(String.format(JDBCTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 22, 22))) { assertTrue(rs.next()); assertThat(rs.getInt(1), is(4)); } @@ -332,7 +331,7 @@ public void assertAddBatchWithGenerateKeyColumn() throws SQLException { } try ( Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(DatabaseTestSQL.INSERT_ORDER_ITEM_WITH_ALL_PLACEHOLDERS_SQL, Statement.RETURN_GENERATED_KEYS); + PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.INSERT_ORDER_ITEM_WITH_ALL_PLACEHOLDERS_SQL, Statement.RETURN_GENERATED_KEYS); Statement queryStatement = connection.createStatement()) { preparedStatement.setInt(1, 1); preparedStatement.setInt(2, 11); @@ -368,19 +367,19 @@ public void assertAddBatchWithGenerateKeyColumn() throws SQLException { assertTrue(generateKeyResultSet.next()); assertThat(generateKeyResultSet.getLong(1), is(4L)); assertFalse(generateKeyResultSet.next()); - try (ResultSet rs = queryStatement.executeQuery(String.format(DatabaseTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 11, 11))) { + try (ResultSet rs = queryStatement.executeQuery(String.format(JDBCTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 11, 11))) { assertTrue(rs.next()); assertThat(rs.getInt(1), is(1)); } - try (ResultSet rs = queryStatement.executeQuery(String.format(DatabaseTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 12, 12))) { + try (ResultSet rs = queryStatement.executeQuery(String.format(JDBCTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 12, 12))) { assertTrue(rs.next()); assertThat(rs.getInt(1), is(2)); } - try (ResultSet rs = queryStatement.executeQuery(String.format(DatabaseTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 21, 21))) { + try (ResultSet rs = queryStatement.executeQuery(String.format(JDBCTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 21, 21))) { assertTrue(rs.next()); assertThat(rs.getInt(1), is(3)); } - try (ResultSet rs = queryStatement.executeQuery(String.format(DatabaseTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 22, 22))) { + try (ResultSet rs = queryStatement.executeQuery(String.format(JDBCTestSQL.SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL, 22, 22))) { assertTrue(rs.next()); assertThat(rs.getInt(1), is(4)); } @@ -389,7 +388,7 @@ public void assertAddBatchWithGenerateKeyColumn() throws SQLException { @Test public void assertUpdateBatch() throws SQLException { - String sql = SQLPlaceholderUtil.replacePreparedStatement(DatabaseTestSQL.UPDATE_WITHOUT_SHARDING_VALUE_SQL); + String sql = SQLPlaceholderUtil.replacePreparedStatement(JDBCTestSQL.UPDATE_WITHOUT_SHARDING_VALUE_SQL); try ( Connection connection = getShardingDataSource().getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { @@ -421,7 +420,7 @@ public void assertUpdateBatch() throws SQLException { public void assertClearBatch() throws SQLException { try ( Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(DatabaseTestSQL.INSERT_ORDER_ITEM_WITH_ALL_PLACEHOLDERS_SQL)) { + PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.INSERT_ORDER_ITEM_WITH_ALL_PLACEHOLDERS_SQL)) { preparedStatement.setInt(1, 3101); preparedStatement.setInt(2, 11); preparedStatement.setInt(3, 11); diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/unsupported/UnsupportedOperationPreparedStatementTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/unsupported/UnsupportedOperationPreparedStatementTest.java index c56014e91d737..a7784e13ffeba 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/unsupported/UnsupportedOperationPreparedStatementTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/unsupported/UnsupportedOperationPreparedStatementTest.java @@ -19,7 +19,7 @@ import io.shardingsphere.core.common.base.AbstractShardingJDBCDatabaseAndTableTest; import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.integrate.sql.DatabaseTestSQL; +import io.shardingsphere.core.jdbc.JDBCTestSQL; import io.shardingsphere.core.jdbc.core.connection.ShardingConnection; import org.junit.After; import org.junit.Before; @@ -47,7 +47,7 @@ public UnsupportedOperationPreparedStatementTest(final DatabaseType databaseType public void init() { ShardingConnection shardingConnection = getShardingDataSource().getConnection(); shardingConnections.add(shardingConnection); - PreparedStatement preparedStatement = shardingConnection.prepareStatement(DatabaseTestSQL.SELECT_WITH_ALIAS_SQL); + PreparedStatement preparedStatement = shardingConnection.prepareStatement(JDBCTestSQL.SELECT_WITH_ALIAS_SQL); statements.add(preparedStatement); } diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/unsupported/UnsupportedOperationResultSetTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/unsupported/UnsupportedOperationResultSetTest.java index 1d0957e495985..6821b653535ca 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/unsupported/UnsupportedOperationResultSetTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/unsupported/UnsupportedOperationResultSetTest.java @@ -19,7 +19,7 @@ import io.shardingsphere.core.common.base.AbstractShardingJDBCDatabaseAndTableTest; import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.integrate.sql.DatabaseTestSQL; +import io.shardingsphere.core.jdbc.JDBCTestSQL; import io.shardingsphere.core.jdbc.core.connection.ShardingConnection; import org.junit.After; import org.junit.Before; @@ -35,7 +35,7 @@ public final class UnsupportedOperationResultSetTest extends AbstractShardingJDBCDatabaseAndTableTest { - private final String sql = DatabaseTestSQL.SELECT_WITH_ALIAS_SQL; + private final String sql = JDBCTestSQL.SELECT_WITH_ALIAS_SQL; private List shardingConnections = new ArrayList<>(); From 1b3e37264fc0a892a71f8a5979f6cbd73d0f3c1d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 12:21:26 +0800 Subject: [PATCH 343/351] for #601: remove sharding and masterslave schema from jdbc test --- .../core/common/base/AbstractSQLTest.java | 42 +------------------ .../env/schema/SchemaEnvironmentManager.java | 3 -- 2 files changed, 2 insertions(+), 43 deletions(-) diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractSQLTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractSQLTest.java index 8eff193909c88..bf7af870a9e81 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractSQLTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractSQLTest.java @@ -41,7 +41,6 @@ import java.lang.reflect.Field; import java.sql.Connection; import java.sql.SQLException; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -78,10 +77,10 @@ private static synchronized void init() { } } if (initialized) { - createSchema(DatabaseType.H2); + createJdbcSchema(DatabaseType.H2); } else { for (DatabaseType each : getDatabaseTypes()) { - createSchema(each); + createJdbcSchema(each); } } } catch (final IOException ex) { @@ -98,43 +97,6 @@ private static DatabaseType findDatabaseType(final String databaseType) { throw new RuntimeException("Can't find database type of:" + databaseType); } - private static void createSchema(final DatabaseType dbType) { - createJdbcSchema(dbType); - createMasterSlaveOnlySchema(dbType); - createShardingSchema(dbType); - } - - private static void createShardingSchema(final DatabaseType dbType) { - try { - Connection conn; - for (int i = 0; i < 10; i++) { - for (String database : Arrays.asList("db", "dbtbl", "nullable", "master", "slave")) { - conn = initialConnection(database + "_" + i, dbType); - RunScript.execute(conn, new InputStreamReader(AbstractSQLTest.class.getClassLoader().getResourceAsStream("integrate/schema/table/" + database + ".sql"))); - conn.close(); - } - } - conn = initialConnection("tbl", dbType); - RunScript.execute(conn, new InputStreamReader(AbstractSQLTest.class.getClassLoader().getResourceAsStream("integrate/schema/table/tbl.sql"))); - conn.close(); - } catch (final SQLException ex) { - ex.printStackTrace(); - } - } - - private static void createMasterSlaveOnlySchema(final DatabaseType dbType) { - try { - Connection conn; - for (String database : Arrays.asList("master_only", "slave_only")) { - conn = initialConnection(database, dbType); - RunScript.execute(conn, new InputStreamReader(AbstractSQLTest.class.getClassLoader().getResourceAsStream("integrate/schema/table/" + database + ".sql"))); - conn.close(); - } - } catch (final SQLException ex) { - ex.printStackTrace(); - } - } - private static void createJdbcSchema(final DatabaseType dbType) { try { Connection conn; diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java index 81c62f7549afe..a9085f1b352eb 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java @@ -77,7 +77,6 @@ public static void createDatabase(final String shardingRuleType) throws IOExcept RunScript.execute(connection, stringReader); } catch (final SQLException ex) { // TODO schema maybe exist for oracle only - ex.printStackTrace(); } } } @@ -99,7 +98,6 @@ public static void dropDatabase(final String shardingRuleType) throws IOExceptio RunScript.execute(connection, stringReader); } catch (final SQLException ex) { // TODO schema maybe not exist for oracle only - ex.printStackTrace(); } } } @@ -156,7 +154,6 @@ private static void createTable(final SchemaEnvironment databaseEnvironmentSchem RunScript.execute(connection, stringReader); } catch (final SQLException ex) { // TODO schema maybe not exist for oracle only - ex.printStackTrace(); } } } From d561199e8d91f1530bea068f0b43db4f17124034 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 12:26:31 +0800 Subject: [PATCH 344/351] fix java doc --- .../reg/newzk/client/zookeeper/section/Connection.java | 1 - 1 file changed, 1 deletion(-) diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/Connection.java b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/Connection.java index 63dbde3920408..c8fb6321c5059 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/Connection.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingsphere/jdbc/orchestration/reg/newzk/client/zookeeper/section/Connection.java @@ -42,7 +42,6 @@ public class Connection { * @param e e * @return need reset * @throws KeeperException Zookeeper Exception - * @throws InterruptedException InterruptedException */ public static boolean needReset(final KeeperException e) throws KeeperException { int code = e.code().intValue(); From 5828f6ab4c8f484ac2d9d51d3606877de07cc8b7 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 13:53:54 +0800 Subject: [PATCH 345/351] for #601: add case assertExecuteQueryWithResultSetTypeAndResultSetConcurrency --- .../dbtest/engine/DQLIntegrateTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java index 3b05e49f04a8c..91561c7b85a9d 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java @@ -177,6 +177,39 @@ private void assertExecuteQueryForPreparedStatement(final Connection connection) } } + @Test + public void assertExecuteQueryWithResultSetTypeAndResultSetConcurrency() throws JAXBException, IOException, SQLException, ParseException { + if (!getDatabaseTypeEnvironment().isEnabled()) { + return; + } + try (Connection connection = getDataSource().getConnection()) { + if (SQLCaseType.Literal == getCaseType()) { + assertExecuteQueryForStatementWithResultSetTypeAndResultSetConcurrency(connection); + } else { + assertExecuteQueryForPreparedStatementWithResultSetTypeAndResultSetConcurrency(connection); + } + } + } + + private void assertExecuteQueryForStatementWithResultSetTypeAndResultSetConcurrency(final Connection connection) throws SQLException, JAXBException, IOException, ParseException { + try ( + Statement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); + ResultSet resultSet = statement.executeQuery(String.format(getSql(), assertion.getSQLValues().toArray()))) { + assertResultSet(resultSet); + } + } + + private void assertExecuteQueryForPreparedStatementWithResultSetTypeAndResultSetConcurrency(final Connection connection) throws SQLException, ParseException, JAXBException, IOException { + try (PreparedStatement preparedStatement = connection.prepareStatement(getSql().replaceAll("%s", "?"), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) { + for (SQLValue each : assertion.getSQLValues()) { + preparedStatement.setObject(each.getIndex(), each.getValue()); + } + try (ResultSet resultSet = preparedStatement.executeQuery()) { + assertResultSet(resultSet); + } + } + } + @Test public void assertExecute() throws JAXBException, IOException, SQLException, ParseException { if (!getDatabaseTypeEnvironment().isEnabled()) { From 6c54d0b984e909b60f1c1aed41a1d599de1116e0 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 14:08:55 +0800 Subject: [PATCH 346/351] for #601: add case assertExecuteUpdateWithAutoGeneratedKeys --- .../dbtest/engine/DMLIntegrateTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java index 45e639eb9373b..ddad4177e1645 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java @@ -148,6 +148,36 @@ private void assertExecuteUpdateForPreparedStatement(final Connection connection } } + @Test + public void assertExecuteUpdateWithAutoGeneratedKeys() throws JAXBException, IOException, SQLException, ParseException { + if (!getDatabaseTypeEnvironment().isEnabled()) { + return; + } + try (Connection connection = getDataSource().getConnection()) { + if (SQLCaseType.Literal == getCaseType()) { + assertExecuteUpdateForStatementWithAutoGeneratedKeys(connection); + } else { + assertExecuteUpdateForPreparedStatementWithAutoGeneratedKeys(connection); + } + } + assertDataSet(); + } + + private void assertExecuteUpdateForStatementWithAutoGeneratedKeys(final Connection connection) throws SQLException, ParseException { + try (Statement statement = connection.createStatement()) { + assertThat(statement.executeUpdate(String.format(getSql(), assertion.getSQLValues().toArray()), Statement.NO_GENERATED_KEYS), is(assertion.getExpectedUpdate())); + } + } + + private void assertExecuteUpdateForPreparedStatementWithAutoGeneratedKeys(final Connection connection) throws SQLException, ParseException { + try (PreparedStatement preparedStatement = connection.prepareStatement(getSql().replaceAll("%s", "?"), Statement.NO_GENERATED_KEYS)) { + for (SQLValue each : assertion.getSQLValues()) { + preparedStatement.setObject(each.getIndex(), each.getValue()); + } + assertThat(preparedStatement.executeUpdate(), is(assertion.getExpectedUpdate())); + } + } + @Test public void assertExecute() throws JAXBException, IOException, SQLException, ParseException { if (!getDatabaseTypeEnvironment().isEnabled()) { From 8919e9ff6ee4010fc3921b04efb73e7d4cfb3b66 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 14:14:33 +0800 Subject: [PATCH 347/351] for #601: add case assertExecuteQueryWithResultSetTypeAndResultSetConcurrencyAndResultSetHoldability --- .../ShardingPreparedStatementTest.java | 139 ------------------ .../core/statement/ShardingStatementTest.java | 62 -------- .../dbtest/engine/DQLIntegrateTest.java | 36 +++++ 3 files changed, 36 insertions(+), 201 deletions(-) diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatementTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatementTest.java index 1ab4e7d532313..2fa550bb71ecc 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatementTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatementTest.java @@ -47,145 +47,6 @@ public ShardingPreparedStatementTest(final DatabaseType databaseType) { super(databaseType); } - @Test - public void assertExecuteQueryWithParameter() throws SQLException { - try ( - Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL)) { - preparedStatement.setString(1, "init"); - ResultSet resultSet = preparedStatement.executeQuery(); - assertTrue(resultSet.next()); - assertThat(resultSet.getLong(1), is(4L)); - preparedStatement.setString(1, "null"); - resultSet = preparedStatement.executeQuery(); - assertTrue(resultSet.next()); - assertThat(resultSet.getLong(1), is(0L)); - preparedStatement.setString(1, "init"); - resultSet = preparedStatement.executeQuery(); - assertTrue(resultSet.next()); - assertThat(resultSet.getLong(1), is(4L)); - } - } - - @Test - public void assertExecuteQueryWithoutParameter() throws SQLException { - String sql = io.shardingsphere.core.jdbc.util.JDBCTestSQL.SELECT_COUNT_ALIAS_SQL; - try ( - Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - ResultSet resultSet = preparedStatement.executeQuery(); - assertTrue(resultSet.next()); - assertThat(resultSet.getLong(1), is(4L)); - resultSet = preparedStatement.executeQuery(); - assertTrue(resultSet.next()); - assertThat(resultSet.getLong(1), is(4L)); - resultSet = preparedStatement.executeQuery(); - assertTrue(resultSet.next()); - assertThat(resultSet.getLong(1), is(4L)); - } - } - - @Test - public void assertExecuteUpdateWithParameter() throws SQLException { - try ( - Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(SQLPlaceholderUtil.replacePreparedStatement(JDBCTestSQL.DELETE_WITHOUT_SHARDING_VALUE_SQL))) { - preparedStatement.setString(1, "init"); - assertThat(preparedStatement.executeUpdate(), is(4)); - preparedStatement.setString(1, "null"); - assertThat(preparedStatement.executeUpdate(), is(0)); - preparedStatement.setString(1, "init"); - assertThat(preparedStatement.executeUpdate(), is(0)); - } - } - - @Test - public void assertExecuteUpdateWithoutParameter() throws SQLException { - String sql = String.format(JDBCTestSQL.DELETE_WITHOUT_SHARDING_VALUE_SQL, "'init'"); - try ( - Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - assertThat(preparedStatement.executeUpdate(), is(4)); - assertThat(preparedStatement.executeUpdate(), is(0)); - assertThat(preparedStatement.executeUpdate(), is(0)); - } - } - - @Test - public void assertExecuteWithParameter() throws SQLException { - try ( - Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL)) { - preparedStatement.setString(1, "init"); - assertTrue(preparedStatement.execute()); - assertTrue(preparedStatement.getResultSet().next()); - assertThat(preparedStatement.getResultSet().getLong(1), is(4L)); - preparedStatement.setString(1, "null"); - assertTrue(preparedStatement.execute()); - // TODO 调研oracle为什么不可以 - if (DatabaseType.Oracle != getCurrentDatabaseType()) { - assertTrue(preparedStatement.getResultSet().next()); - assertThat(preparedStatement.getResultSet().getLong(1), is(0L)); - } - preparedStatement.setString(1, "init"); - assertTrue(preparedStatement.execute()); - // TODO 调研oracle为什么不可以 - if (DatabaseType.Oracle != getCurrentDatabaseType()) { - assertTrue(preparedStatement.getResultSet().next()); - assertThat(preparedStatement.getResultSet().getLong(1), is(4L)); - } - } - } - - @Test - public void assertExecuteWithoutParameter() throws SQLException { - String sql = String.format(JDBCTestSQL.DELETE_WITHOUT_SHARDING_VALUE_SQL, "'init'"); - try ( - Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - assertFalse(preparedStatement.execute()); - assertFalse(preparedStatement.execute()); - assertFalse(preparedStatement.execute()); - } - } - - @Test - public void assertExecuteQueryWithResultSetTypeAndResultSetConcurrency() throws SQLException { - try ( - Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) { - preparedStatement.setString(1, "init"); - ResultSet resultSet = preparedStatement.executeQuery(); - assertTrue(resultSet.next()); - assertThat(resultSet.getLong(1), is(4L)); - } - } - - @Test - public void assertExecuteQueryWithResultSetTypeAndResultSetConcurrencyAndResultSetHoldability() throws SQLException { - try ( - Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL, ResultSet.TYPE_FORWARD_ONLY, - ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT)) { - preparedStatement.setString(1, "init"); - ResultSet resultSet = preparedStatement.executeQuery(); - assertTrue(resultSet.next()); - assertThat(resultSet.getLong(1), is(4L)); - } - } - - @Test - public void assertExecuteQueryWithAutoGeneratedKeys() throws SQLException { - try ( - Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL, Statement.NO_GENERATED_KEYS)) { - preparedStatement.setString(1, "init"); - ResultSet resultSet = preparedStatement.executeQuery(); - assertTrue(resultSet.next()); - assertThat(resultSet.getLong(1), is(4L)); - } - } - @Test public void assertExecuteQueryWithColumnIndexes() throws SQLException { if (DatabaseType.PostgreSQL != getCurrentDatabaseType()) { diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatementTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatementTest.java index 44211b7e5d51e..f827bc13f5d9b 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatementTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatementTest.java @@ -43,68 +43,6 @@ public ShardingStatementTest(final DatabaseType databaseType) { super(databaseType); } - @Test - public void assertExecuteQuery() throws SQLException { - try ( - Connection connection = getShardingDataSource().getConnection(); - Statement stmt = connection.createStatement(); - ResultSet resultSet = stmt.executeQuery(sql)) { - assertTrue(resultSet.next()); - assertThat(resultSet.getLong(1), is(4L)); - } - } - - @Test - public void assertExecuteUpdate() throws SQLException { - try ( - Connection connection = getShardingDataSource().getConnection(); - Statement stmt = connection.createStatement()) { - assertThat(stmt.executeUpdate(sql2), is(4)); - } - } - - @Test - public void assertExecute() throws SQLException { - try ( - Connection connection = getShardingDataSource().getConnection(); - Statement stmt = connection.createStatement()) { - assertTrue(stmt.execute(sql)); - assertTrue(stmt.getResultSet().next()); - assertThat(stmt.getResultSet().getLong(1), is(4L)); - } - } - - @Test - public void assertExecuteQueryWithResultSetTypeAndResultSetConcurrency() throws SQLException { - try ( - Connection connection = getShardingDataSource().getConnection(); - Statement stmt = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); - ResultSet resultSet = stmt.executeQuery(sql)) { - assertTrue(resultSet.next()); - assertThat(resultSet.getLong(1), is(4L)); - } - } - - @Test - public void assertExecuteQueryWithResultSetTypeAndResultSetConcurrencyAndResultSetHoldability() throws SQLException { - try ( - Connection connection = getShardingDataSource().getConnection(); - Statement stmt = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT); - ResultSet resultSet = stmt.executeQuery(sql)) { - assertTrue(resultSet.next()); - assertThat(resultSet.getLong(1), is(4L)); - } - } - - @Test - public void assertExecuteUpdateWithAutoGeneratedKeys() throws SQLException { - try ( - Connection connection = getShardingDataSource().getConnection(); - Statement stmt = connection.createStatement()) { - assertThat(stmt.executeUpdate(sql2, Statement.NO_GENERATED_KEYS), is(4)); - } - } - @Test public void assertExecuteUpdateWithColumnIndexes() throws SQLException { if (DatabaseType.PostgreSQL != getCurrentDatabaseType()) { diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java index 91561c7b85a9d..67786c156632c 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java @@ -210,6 +210,42 @@ private void assertExecuteQueryForPreparedStatementWithResultSetTypeAndResultSet } } + @Test + public void assertExecuteQueryWithResultSetTypeAndResultSetConcurrencyAndResultSetHoldability() throws JAXBException, IOException, SQLException, ParseException { + if (!getDatabaseTypeEnvironment().isEnabled()) { + return; + } + try (Connection connection = getDataSource().getConnection()) { + if (SQLCaseType.Literal == getCaseType()) { + assertExecuteQueryForStatementWithResultSetTypeAndResultSetConcurrencyAndResultSetHoldability(connection); + } else { + assertExecuteQueryForPreparedStatementWithResultSetTypeAndResultSetConcurrencyAndResultSetHoldability(connection); + } + } + } + + private void assertExecuteQueryForStatementWithResultSetTypeAndResultSetConcurrencyAndResultSetHoldability(final Connection connection) + throws SQLException, JAXBException, IOException, ParseException { + try ( + Statement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT); + ResultSet resultSet = statement.executeQuery(String.format(getSql(), assertion.getSQLValues().toArray()))) { + assertResultSet(resultSet); + } + } + + private void assertExecuteQueryForPreparedStatementWithResultSetTypeAndResultSetConcurrencyAndResultSetHoldability(final Connection connection) + throws SQLException, ParseException, JAXBException, IOException { + try (PreparedStatement preparedStatement = connection.prepareStatement( + getSql().replaceAll("%s", "?"), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT)) { + for (SQLValue each : assertion.getSQLValues()) { + preparedStatement.setObject(each.getIndex(), each.getValue()); + } + try (ResultSet resultSet = preparedStatement.executeQuery()) { + assertResultSet(resultSet); + } + } + } + @Test public void assertExecute() throws JAXBException, IOException, SQLException, ParseException { if (!getDatabaseTypeEnvironment().isEnabled()) { From 431719ab568fe65b271889a47397a8b221763fbd Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 14:26:04 +0800 Subject: [PATCH 348/351] for #601: add case assertExecuteUpdateWithColumnIndexes --- .../dbtest/engine/DMLIntegrateTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java index ddad4177e1645..70aff43d488ed 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java @@ -178,6 +178,36 @@ private void assertExecuteUpdateForPreparedStatementWithAutoGeneratedKeys(final } } + @Test + public void assertExecuteUpdateWithColumnIndexes() throws JAXBException, IOException, SQLException, ParseException { + if (!getDatabaseTypeEnvironment().isEnabled()) { + return; + } + try (Connection connection = getDataSource().getConnection()) { + if (SQLCaseType.Literal == getCaseType()) { + assertExecuteUpdateForStatementWithColumnIndexes(connection); + } else { + assertExecuteUpdateForPreparedStatementWithColumnIndexes(connection); + } + } + assertDataSet(); + } + + private void assertExecuteUpdateForStatementWithColumnIndexes(final Connection connection) throws SQLException, ParseException { + try (Statement statement = connection.createStatement()) { + assertThat(statement.executeUpdate(String.format(getSql(), assertion.getSQLValues().toArray()), new int[]{1}), is(assertion.getExpectedUpdate())); + } + } + + private void assertExecuteUpdateForPreparedStatementWithColumnIndexes(final Connection connection) throws SQLException, ParseException { + try (PreparedStatement preparedStatement = connection.prepareStatement(getSql().replaceAll("%s", "?"), new int[]{1})) { + for (SQLValue each : assertion.getSQLValues()) { + preparedStatement.setObject(each.getIndex(), each.getValue()); + } + assertThat(preparedStatement.executeUpdate(), is(assertion.getExpectedUpdate())); + } + } + @Test public void assertExecute() throws JAXBException, IOException, SQLException, ParseException { if (!getDatabaseTypeEnvironment().isEnabled()) { From 0bef3fcf0406cdc6fb6447c798b028e329fcbe7d Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 14:32:00 +0800 Subject: [PATCH 349/351] for #601: add case assertExecuteUpdateWithColumnNames --- .../ShardingPreparedStatementTest.java | 28 ----------------- .../core/statement/ShardingStatementTest.java | 22 -------------- .../dbtest/engine/DMLIntegrateTest.java | 30 +++++++++++++++++++ 3 files changed, 30 insertions(+), 50 deletions(-) diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatementTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatementTest.java index 2fa550bb71ecc..8f017616dc1b2 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatementTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatementTest.java @@ -47,34 +47,6 @@ public ShardingPreparedStatementTest(final DatabaseType databaseType) { super(databaseType); } - @Test - public void assertExecuteQueryWithColumnIndexes() throws SQLException { - if (DatabaseType.PostgreSQL != getCurrentDatabaseType()) { - try ( - Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL, new int[]{1})) { - preparedStatement.setNull(1, java.sql.Types.VARCHAR); - ResultSet resultSet = preparedStatement.executeQuery(); - assertTrue(resultSet.next()); - assertThat(resultSet.getLong(1), is(0L)); - } - } - } - - @Test - public void assertExecuteQueryWithColumnNames() throws SQLException { - if (DatabaseType.PostgreSQL != getCurrentDatabaseType()) { - try ( - Connection connection = getShardingDataSource().getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(JDBCTestSQL.SELECT_COUNT_AS_ORDERS_COUNT_SQL, new String[]{"orders_count"})) { - preparedStatement.setNull(1, java.sql.Types.VARCHAR); - ResultSet resultSet = preparedStatement.executeQuery(); - assertTrue(resultSet.next()); - assertThat(resultSet.getLong(1), is(0L)); - } - } - } - @Test public void assertAddBatch() throws SQLException { Object listener = new Object() { diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatementTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatementTest.java index f827bc13f5d9b..9ed719fdb1c0b 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatementTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatementTest.java @@ -43,28 +43,6 @@ public ShardingStatementTest(final DatabaseType databaseType) { super(databaseType); } - @Test - public void assertExecuteUpdateWithColumnIndexes() throws SQLException { - if (DatabaseType.PostgreSQL != getCurrentDatabaseType()) { - try ( - Connection connection = getShardingDataSource().getConnection(); - Statement stmt = connection.createStatement()) { - assertThat(stmt.executeUpdate(sql2, new int[]{1}), is(4)); - } - } - } - - @Test - public void assertExecuteUpdateWithColumnNames() throws SQLException { - if (DatabaseType.H2 == getCurrentDatabaseType() || DatabaseType.MySQL == getCurrentDatabaseType()) { - try ( - Connection connection = getShardingDataSource().getConnection(); - Statement stmt = connection.createStatement()) { - assertThat(stmt.executeUpdate(sql2, new String[]{"orders_count"}), is(4)); - } - } - } - @Test public void assertExecuteWithAutoGeneratedKeys() throws SQLException { try ( diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java index 70aff43d488ed..19700b95454bc 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java @@ -208,6 +208,36 @@ private void assertExecuteUpdateForPreparedStatementWithColumnIndexes(final Conn } } + @Test + public void assertExecuteUpdateWithColumnNames() throws JAXBException, IOException, SQLException, ParseException { + if (!getDatabaseTypeEnvironment().isEnabled()) { + return; + } + try (Connection connection = getDataSource().getConnection()) { + if (SQLCaseType.Literal == getCaseType()) { + assertExecuteUpdateForStatementWithColumnNames(connection); + } else { + assertExecuteUpdateForPreparedStatementWithColumnNames(connection); + } + } + assertDataSet(); + } + + private void assertExecuteUpdateForStatementWithColumnNames(final Connection connection) throws SQLException, ParseException { + try (Statement statement = connection.createStatement()) { + assertThat(statement.executeUpdate(String.format(getSql(), assertion.getSQLValues().toArray()), new String[]{"TODO"}), is(assertion.getExpectedUpdate())); + } + } + + private void assertExecuteUpdateForPreparedStatementWithColumnNames(final Connection connection) throws SQLException, ParseException { + try (PreparedStatement preparedStatement = connection.prepareStatement(getSql().replaceAll("%s", "?"), new String[]{"TODO"})) { + for (SQLValue each : assertion.getSQLValues()) { + preparedStatement.setObject(each.getIndex(), each.getValue()); + } + assertThat(preparedStatement.executeUpdate(), is(assertion.getExpectedUpdate())); + } + } + @Test public void assertExecute() throws JAXBException, IOException, SQLException, ParseException { if (!getDatabaseTypeEnvironment().isEnabled()) { From f5faf40b7c352c6689e3ed7708ca218ccb1d8457 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 14:57:01 +0800 Subject: [PATCH 350/351] for #601: add case for execute --- .../shardingsphere/core/jdbc/JDBCTestSQL.java | 4 - .../core/statement/ShardingStatementTest.java | 66 ++----------- .../dbtest/engine/DMLIntegrateTest.java | 96 +++++++++++++++++++ .../dbtest/engine/DQLIntegrateTest.java | 73 ++++++++++++++ 4 files changed, 177 insertions(+), 62 deletions(-) diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/JDBCTestSQL.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/JDBCTestSQL.java index c4943aa91e120..c67c20fdd2571 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/JDBCTestSQL.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/JDBCTestSQL.java @@ -25,10 +25,6 @@ public class JDBCTestSQL { public static final String UPDATE_WITHOUT_SHARDING_VALUE_SQL = "UPDATE t_order SET status = %s WHERE status = %s"; - public static final String DELETE_WITHOUT_SHARDING_VALUE_SQL = "DELETE FROM t_order WHERE status = %s"; - - public static final String SELECT_COUNT_AS_ORDERS_COUNT_SQL = "SELECT COUNT(*) AS orders_count FROM t_order WHERE status = ?"; - public static final String SELECT_WITH_AUTO_INCREMENT_COLUMN_SQL = "SELECT item_id from t_order_item where user_id = %d and order_id= %s and status = 'BATCH'"; public static final String SELECT_WITH_ALIAS_SQL = "SELECT user_id AS usr_id FROM t_order WHERE status = 'init'"; diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatementTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatementTest.java index 9ed719fdb1c0b..0f5b2cb5fbf1a 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatementTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatementTest.java @@ -33,88 +33,38 @@ public final class ShardingStatementTest extends AbstractShardingJDBCDatabaseAndTableTest { - private String sql = "SELECT COUNT(*) AS orders_count FROM t_order WHERE status = 'init'"; - - private String sql2 = "DELETE FROM t_order WHERE status ='init'"; - - private String sql3 = "INSERT INTO t_order_item(order_id, user_id, status) VALUES (%d, %d, '%s')"; + private String sql = "INSERT INTO t_order_item(order_id, user_id, status) VALUES (%d, %d, '%s')"; public ShardingStatementTest(final DatabaseType databaseType) { super(databaseType); } - @Test - public void assertExecuteWithAutoGeneratedKeys() throws SQLException { - try ( - Connection connection = getShardingDataSource().getConnection(); - Statement stmt = connection.createStatement()) { - assertTrue(stmt.execute(sql, Statement.NO_GENERATED_KEYS)); - assertTrue(stmt.getResultSet().next()); - assertThat(stmt.getResultSet().getLong(1), is(4L)); - } - } - - @Test - public void assertExecuteWithColumnIndexes() throws SQLException { - if (DatabaseType.PostgreSQL != getCurrentDatabaseType()) { - try ( - Connection connection = getShardingDataSource().getConnection(); - Statement stmt = connection.createStatement()) { - assertTrue(stmt.execute(sql, new int[]{1})); - assertTrue(stmt.getResultSet().next()); - assertThat(stmt.getResultSet().getLong(1), is(4L)); - } - } - } - - @Test - public void assertExecuteWithColumnNames() throws SQLException { - if (DatabaseType.PostgreSQL != getCurrentDatabaseType()) { - try ( - Connection connection = getShardingDataSource().getConnection(); - Statement stmt = connection.createStatement()) { - assertTrue(stmt.execute(sql, new String[]{"orders_count"})); - assertTrue(stmt.getResultSet().next()); - assertThat(stmt.getResultSet().getLong(1), is(4L)); - } - } - } - - @Test - public void assertGetConnection() throws SQLException { - try ( - Connection connection = getShardingDataSource().getConnection(); - Statement stmt = connection.createStatement()) { - assertThat(stmt.getConnection(), is(connection)); - } - } - @Test public void assertGetGeneratedKeys() throws SQLException { if (DatabaseType.PostgreSQL != getCurrentDatabaseType()) { try ( Connection connection = getShardingDataSource().getConnection(); Statement stmt = connection.createStatement()) { - assertFalse(stmt.execute(String.format(sql3, 1, 1, "init"))); - assertFalse(stmt.execute(String.format(sql3, 1, 1, "init"), Statement.NO_GENERATED_KEYS)); - assertFalse(stmt.execute(String.format(sql3, 1, 1, "init"), Statement.RETURN_GENERATED_KEYS)); + assertFalse(stmt.execute(String.format(sql, 1, 1, "init"))); + assertFalse(stmt.execute(String.format(sql, 1, 1, "init"), Statement.NO_GENERATED_KEYS)); + assertFalse(stmt.execute(String.format(sql, 1, 1, "init"), Statement.RETURN_GENERATED_KEYS)); ResultSet generatedKeysResultSet = stmt.getGeneratedKeys(); assertTrue(generatedKeysResultSet.next()); assertThat(generatedKeysResultSet.getLong(1), is(3L)); - assertFalse(stmt.execute(String.format(sql3, 1, 1, "init"), new int[]{1})); + assertFalse(stmt.execute(String.format(sql, 1, 1, "init"), new int[]{1})); generatedKeysResultSet = stmt.getGeneratedKeys(); assertTrue(generatedKeysResultSet.next()); assertThat(generatedKeysResultSet.getLong(1), is(4L)); - assertFalse(stmt.execute(String.format(sql3, 1, 1, "init"), new String[]{"user_id"})); + assertFalse(stmt.execute(String.format(sql, 1, 1, "init"), new String[]{"user_id"})); generatedKeysResultSet = stmt.getGeneratedKeys(); assertTrue(generatedKeysResultSet.next()); assertThat(generatedKeysResultSet.getLong(1), is(5L)); - assertFalse(stmt.execute(String.format(sql3, 1, 1, "init"), new int[]{2})); + assertFalse(stmt.execute(String.format(sql, 1, 1, "init"), new int[]{2})); generatedKeysResultSet = stmt.getGeneratedKeys(); assertTrue(generatedKeysResultSet.next()); assertThat(generatedKeysResultSet.getLong(1), is(6L)); if (DatabaseType.Oracle != getCurrentDatabaseType()) { - assertFalse(stmt.execute(String.format(sql3, 1, 1, "init"), new String[]{"no"})); + assertFalse(stmt.execute(String.format(sql, 1, 1, "init"), new String[]{"no"})); generatedKeysResultSet = stmt.getGeneratedKeys(); assertTrue(generatedKeysResultSet.next()); assertThat(generatedKeysResultSet.getLong(1), is(7L)); diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java index 19700b95454bc..db77f8a72c135 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java @@ -270,6 +270,102 @@ private void assertExecuteForPreparedStatement(final Connection connection) thro } } + @Test + public void assertExecuteWithAutoGeneratedKeys() throws JAXBException, IOException, SQLException, ParseException { + if (!getDatabaseTypeEnvironment().isEnabled()) { + return; + } + try (Connection connection = getDataSource().getConnection()) { + if (SQLCaseType.Literal == getCaseType()) { + assertExecuteForStatementWithAutoGeneratedKeys(connection); + } else { + assertExecuteForPreparedStatementWithAutoGeneratedKeys(connection); + } + } + assertDataSet(); + } + + private void assertExecuteForStatementWithAutoGeneratedKeys(final Connection connection) throws SQLException, ParseException { + try (Statement statement = connection.createStatement()) { + assertFalse("Not a DML statement.", statement.execute(String.format(getSql(), assertion.getSQLValues().toArray()), Statement.NO_GENERATED_KEYS)); + assertThat(statement.getUpdateCount(), is(assertion.getExpectedUpdate())); + } + } + + private void assertExecuteForPreparedStatementWithAutoGeneratedKeys(final Connection connection) throws SQLException, ParseException { + try (PreparedStatement preparedStatement = connection.prepareStatement(getSql().replaceAll("%s", "?"), Statement.NO_GENERATED_KEYS)) { + for (SQLValue each : assertion.getSQLValues()) { + preparedStatement.setObject(each.getIndex(), each.getValue()); + } + assertFalse("Not a DML statement.", preparedStatement.execute()); + assertThat(preparedStatement.getUpdateCount(), is(assertion.getExpectedUpdate())); + } + } + + @Test + public void assertExecuteWithColumnIndexes() throws JAXBException, IOException, SQLException, ParseException { + if (!getDatabaseTypeEnvironment().isEnabled()) { + return; + } + try (Connection connection = getDataSource().getConnection()) { + if (SQLCaseType.Literal == getCaseType()) { + assertExecuteForStatementWithColumnIndexes(connection); + } else { + assertExecuteForPreparedStatementWithColumnIndexes(connection); + } + } + assertDataSet(); + } + + private void assertExecuteForStatementWithColumnIndexes(final Connection connection) throws SQLException, ParseException { + try (Statement statement = connection.createStatement()) { + assertFalse("Not a DML statement.", statement.execute(String.format(getSql(), assertion.getSQLValues().toArray()), new int[]{1})); + assertThat(statement.getUpdateCount(), is(assertion.getExpectedUpdate())); + } + } + + private void assertExecuteForPreparedStatementWithColumnIndexes(final Connection connection) throws SQLException, ParseException { + try (PreparedStatement preparedStatement = connection.prepareStatement(getSql().replaceAll("%s", "?"), new int[]{1})) { + for (SQLValue each : assertion.getSQLValues()) { + preparedStatement.setObject(each.getIndex(), each.getValue()); + } + assertFalse("Not a DML statement.", preparedStatement.execute()); + assertThat(preparedStatement.getUpdateCount(), is(assertion.getExpectedUpdate())); + } + } + + @Test + public void assertExecuteWithColumnNames() throws JAXBException, IOException, SQLException, ParseException { + if (!getDatabaseTypeEnvironment().isEnabled()) { + return; + } + try (Connection connection = getDataSource().getConnection()) { + if (SQLCaseType.Literal == getCaseType()) { + assertExecuteForStatementWithColumnNames(connection); + } else { + assertExecuteForPreparedStatementWithColumnNames(connection); + } + } + assertDataSet(); + } + + private void assertExecuteForStatementWithColumnNames(final Connection connection) throws SQLException, ParseException { + try (Statement statement = connection.createStatement()) { + assertFalse("Not a DML statement.", statement.execute(String.format(getSql(), assertion.getSQLValues().toArray()), new String[]{"TODO"})); + assertThat(statement.getUpdateCount(), is(assertion.getExpectedUpdate())); + } + } + + private void assertExecuteForPreparedStatementWithColumnNames(final Connection connection) throws SQLException, ParseException { + try (PreparedStatement preparedStatement = connection.prepareStatement(getSql().replaceAll("%s", "?"), new String[]{"TODO"})) { + for (SQLValue each : assertion.getSQLValues()) { + preparedStatement.setObject(each.getIndex(), each.getValue()); + } + assertFalse("Not a DML statement.", preparedStatement.execute()); + assertThat(preparedStatement.getUpdateCount(), is(assertion.getExpectedUpdate())); + } + } + private void assertDataSet() throws SQLException, IOException, JAXBException { DataSetsRoot expected; try (FileReader reader = new FileReader(getExpectedDataFile())) { diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java index 67786c156632c..a82f385c3026f 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DQLIntegrateTest.java @@ -281,6 +281,79 @@ private void assertExecuteForPreparedStatement(final Connection connection) thro } } + @Test + public void assertExecuteWithResultSetTypeAndResultSetConcurrency() throws JAXBException, IOException, SQLException, ParseException { + if (!getDatabaseTypeEnvironment().isEnabled()) { + return; + } + try (Connection connection = getDataSource().getConnection()) { + if (SQLCaseType.Literal == getCaseType()) { + assertExecuteForStatementWithResultSetTypeAndResultSetConcurrency(connection); + } else { + assertExecuteForPreparedStatementWithResultSetTypeAndResultSetConcurrency(connection); + } + } + } + + private void assertExecuteForStatementWithResultSetTypeAndResultSetConcurrency(final Connection connection) throws SQLException, JAXBException, IOException, ParseException { + try (Statement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) { + assertTrue("Not a DQL statement.", statement.execute(String.format(getSql(), assertion.getSQLValues().toArray()))); + try (ResultSet resultSet = statement.getResultSet()) { + assertResultSet(resultSet); + } + } + } + + private void assertExecuteForPreparedStatementWithResultSetTypeAndResultSetConcurrency(final Connection connection) throws SQLException, ParseException, JAXBException, IOException { + try (PreparedStatement preparedStatement = connection.prepareStatement(getSql().replaceAll("%s", "?"), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) { + for (SQLValue each : assertion.getSQLValues()) { + preparedStatement.setObject(each.getIndex(), each.getValue()); + } + assertTrue("Not a DQL statement.", preparedStatement.execute()); + try (ResultSet resultSet = preparedStatement.getResultSet()) { + assertResultSet(resultSet); + } + } + } + + @Test + public void assertExecuteWithResultSetTypeAndResultSetConcurrencyAndResultSetHoldability() throws JAXBException, IOException, SQLException, ParseException { + if (!getDatabaseTypeEnvironment().isEnabled()) { + return; + } + try (Connection connection = getDataSource().getConnection()) { + if (SQLCaseType.Literal == getCaseType()) { + assertExecuteForStatementWithResultSetTypeAndResultSetConcurrencyAndResultSetHoldability(connection); + } else { + assertExecuteForPreparedStatementWithResultSetTypeAndResultSetConcurrencyAndResultSetHoldability(connection); + } + } + } + + private void assertExecuteForStatementWithResultSetTypeAndResultSetConcurrencyAndResultSetHoldability(final Connection connection) + throws SQLException, JAXBException, IOException, ParseException { + try (Statement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT)) { + assertTrue("Not a DQL statement.", statement.execute(String.format(getSql(), assertion.getSQLValues().toArray()))); + try (ResultSet resultSet = statement.getResultSet()) { + assertResultSet(resultSet); + } + } + } + + private void assertExecuteForPreparedStatementWithResultSetTypeAndResultSetConcurrencyAndResultSetHoldability(final Connection connection) + throws SQLException, ParseException, JAXBException, IOException { + try (PreparedStatement preparedStatement = connection.prepareStatement( + getSql().replaceAll("%s", "?"), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT)) { + for (SQLValue each : assertion.getSQLValues()) { + preparedStatement.setObject(each.getIndex(), each.getValue()); + } + assertTrue("Not a DQL statement.", preparedStatement.execute()); + try (ResultSet resultSet = preparedStatement.getResultSet()) { + assertResultSet(resultSet); + } + } + } + private void assertResultSet(final ResultSet resultSet) throws SQLException, JAXBException, IOException { ExpectedDataSetsRoot expected; try (FileReader reader = new FileReader(getExpectedDataFile())) { From abfdaa4bf7b1d59792a37d34a77c5092d2fa6d87 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 19 Jun 2018 15:23:16 +0800 Subject: [PATCH 351/351] for #601: add case assertExecuteForStatementWithAutoGeneratedKeys --- ...tractShardingJDBCDatabaseAndTableTest.java | 4 +- .../dbtest/engine/DMLIntegrateTest.java | 38 ++++++++++++++++++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractShardingJDBCDatabaseAndTableTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractShardingJDBCDatabaseAndTableTest.java index 05c734bed5b57..44a8394833dc4 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractShardingJDBCDatabaseAndTableTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/common/base/AbstractShardingJDBCDatabaseAndTableTest.java @@ -93,9 +93,7 @@ public void initShardingDataSources() throws SQLException { @Override protected List getInitDataSetFiles() { - return Arrays.asList( - "integrate/dataset/jdbc/jdbc_0.xml", - "integrate/dataset/jdbc/jdbc_1.xml"); + return Arrays.asList("integrate/dataset/jdbc/jdbc_0.xml", "integrate/dataset/jdbc/jdbc_1.xml"); } @Parameterized.Parameters(name = "{0}") diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java index db77f8a72c135..844b6c738d51b 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/engine/DMLIntegrateTest.java @@ -270,6 +270,38 @@ private void assertExecuteForPreparedStatement(final Connection connection) thro } } + @Test + public void assertExecuteWithoutAutoGeneratedKeys() throws JAXBException, IOException, SQLException, ParseException { + if (!getDatabaseTypeEnvironment().isEnabled()) { + return; + } + try (Connection connection = getDataSource().getConnection()) { + if (SQLCaseType.Literal == getCaseType()) { + assertExecuteForStatementWithoutAutoGeneratedKeys(connection); + } else { + assertExecuteForPreparedStatementWithoutAutoGeneratedKeys(connection); + } + } + assertDataSet(); + } + + private void assertExecuteForStatementWithoutAutoGeneratedKeys(final Connection connection) throws SQLException, ParseException { + try (Statement statement = connection.createStatement()) { + assertFalse("Not a DML statement.", statement.execute(String.format(getSql(), assertion.getSQLValues().toArray()), Statement.NO_GENERATED_KEYS)); + assertThat(statement.getUpdateCount(), is(assertion.getExpectedUpdate())); + } + } + + private void assertExecuteForPreparedStatementWithoutAutoGeneratedKeys(final Connection connection) throws SQLException, ParseException { + try (PreparedStatement preparedStatement = connection.prepareStatement(getSql().replaceAll("%s", "?"), Statement.NO_GENERATED_KEYS)) { + for (SQLValue each : assertion.getSQLValues()) { + preparedStatement.setObject(each.getIndex(), each.getValue()); + } + assertFalse("Not a DML statement.", preparedStatement.execute()); + assertThat(preparedStatement.getUpdateCount(), is(assertion.getExpectedUpdate())); + } + } + @Test public void assertExecuteWithAutoGeneratedKeys() throws JAXBException, IOException, SQLException, ParseException { if (!getDatabaseTypeEnvironment().isEnabled()) { @@ -287,18 +319,20 @@ public void assertExecuteWithAutoGeneratedKeys() throws JAXBException, IOExcepti private void assertExecuteForStatementWithAutoGeneratedKeys(final Connection connection) throws SQLException, ParseException { try (Statement statement = connection.createStatement()) { - assertFalse("Not a DML statement.", statement.execute(String.format(getSql(), assertion.getSQLValues().toArray()), Statement.NO_GENERATED_KEYS)); + assertFalse("Not a DML statement.", statement.execute(String.format(getSql(), assertion.getSQLValues().toArray()), Statement.RETURN_GENERATED_KEYS)); assertThat(statement.getUpdateCount(), is(assertion.getExpectedUpdate())); + // TODO assert statement.getGeneratedKeys(); } } private void assertExecuteForPreparedStatementWithAutoGeneratedKeys(final Connection connection) throws SQLException, ParseException { - try (PreparedStatement preparedStatement = connection.prepareStatement(getSql().replaceAll("%s", "?"), Statement.NO_GENERATED_KEYS)) { + try (PreparedStatement preparedStatement = connection.prepareStatement(getSql().replaceAll("%s", "?"), Statement.RETURN_GENERATED_KEYS)) { for (SQLValue each : assertion.getSQLValues()) { preparedStatement.setObject(each.getIndex(), each.getValue()); } assertFalse("Not a DML statement.", preparedStatement.execute()); assertThat(preparedStatement.getUpdateCount(), is(assertion.getExpectedUpdate())); + // TODO assert preparedStatement.getGeneratedKeys(); } }